wolfcrypt/test/test.c: stack->heap refactor for dh_test().

This commit is contained in:
Daniel Pouzzner
2020-08-25 15:24:47 -05:00
parent 66b59bda9b
commit 571bf897c4

View File

@ -13256,7 +13256,7 @@ static int rsa_test(void)
(void)inLen; (void)inLen;
(void)res; (void)res;
{ {
byte signature_2048[] = { static byte signature_2048[] = {
0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79, 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe, 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda, 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
@ -13989,7 +13989,7 @@ static int rsa_test(void)
} }
#ifdef WOLFSSL_EKU_OID #ifdef WOLFSSL_EKU_OID
{ {
const char unique[] = "2.16.840.1.111111.100.1.10.1"; static const char unique[] = "2.16.840.1.111111.100.1.10.1";
if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0, if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
HEAP_HINT) != 0) { HEAP_HINT) != 0) {
ERROR_OUT(-7771, exit_rsa); ERROR_OUT(-7771, exit_rsa);
@ -14524,22 +14524,13 @@ static int dh_test(void)
int ret; int ret;
word32 bytes; word32 bytes;
word32 idx = 0, privSz, pubSz, privSz2, pubSz2; word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
byte tmp[1024]; byte *tmp = NULL,
#if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) *priv = NULL,
byte priv[256]; *pub = NULL,
byte pub[256]; *priv2 = NULL,
byte priv2[256]; *pub2 = NULL,
byte pub2[256]; *agree = NULL,
byte agree[256]; *agree2 = NULL;
byte agree2[256];
#else
byte priv[512];
byte pub[512];
byte priv2[512];
byte pub2[512];
byte agree[512];
byte agree2[512];
#endif
word32 agreeSz = (word32)sizeof(agree); word32 agreeSz = (word32)sizeof(agree);
word32 agreeSz2 = (word32)sizeof(agree2); word32 agreeSz2 = (word32)sizeof(agree2);
DhKey key; DhKey key;
@ -14547,6 +14538,25 @@ static int dh_test(void)
WC_RNG rng; WC_RNG rng;
int keyInit = 0; int keyInit = 0;
#define DH_TEST_TMP_SIZE 1024
tmp = XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
#if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
#define DH_TEST_BUF_SIZE 256
#else
#define DH_TEST_BUF_SIZE 256
#endif
priv = XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
pub = XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
priv2 = XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
pub2 = XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
agree = XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
agree2 = XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if ((tmp == NULL) || (priv == NULL) || (pub == NULL) ||
(priv2 == NULL) || (pub2 == NULL) || (agree == NULL) ||
(agree2 == NULL))
ERROR_OUT(-7960, done);
#ifdef USE_CERT_BUFFERS_1024 #ifdef USE_CERT_BUFFERS_1024
XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024); XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
bytes = (size_t)sizeof_dh_key_der_1024; bytes = (size_t)sizeof_dh_key_der_1024;
@ -14564,9 +14574,9 @@ static int dh_test(void)
#elif !defined(NO_FILESYSTEM) #elif !defined(NO_FILESYSTEM)
XFILE file = XFOPEN(dhParamsFile, "rb"); XFILE file = XFOPEN(dhParamsFile, "rb");
if (!file) if (!file)
return -7900; ERROR_OUT(-7900, done);
bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), file); bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
XFCLOSE(file); XFCLOSE(file);
#else #else
/* No DH key to use. */ /* No DH key to use. */
@ -14691,7 +14701,7 @@ static int dh_test(void)
file = XFOPEN(dhKeyFile, "rb"); file = XFOPEN(dhKeyFile, "rb");
if (!file) if (!file)
return -7950; return -7950;
bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), file); bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
XFCLOSE(file); XFCLOSE(file);
idx = 0; idx = 0;
@ -14701,8 +14711,8 @@ static int dh_test(void)
} }
#endif #endif
privSz = sizeof(priv); privSz = DH_TEST_BUF_SIZE;
pubSz = sizeof(pub); pubSz = DH_TEST_BUF_SIZE;
ret = wc_DhExportKeyPair(&key, priv, &privSz, pub, &pubSz); ret = wc_DhExportKeyPair(&key, priv, &privSz, pub, &pubSz);
if (ret != 0) { if (ret != 0) {
return -7952; return -7952;
@ -14759,7 +14769,24 @@ done:
wc_FreeDhKey(&key2); wc_FreeDhKey(&key2);
wc_FreeRng(&rng); wc_FreeRng(&rng);
if (tmp)
XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (priv)
XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (pub)
XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (priv2)
XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (pub2)
XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (agree)
XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (agree2)
XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return ret; return ret;
#undef DH_TEST_BUF_SIZE
#undef DH_TEST_TMP_SIZE
} }
#endif /* NO_DH */ #endif /* NO_DH */