From 1ee40ad7bd373ad98b2cf144833a108640af2a07 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 21 Jan 2021 17:12:29 -0800 Subject: [PATCH] Fix to always init the variable (not just when from heap). Cleanup of the `DECLARE_` uses to make sure all allocations succeeded. --- tests/api.c | 21 +++++++++++++++------ wolfcrypt/benchmark/benchmark.c | 16 +++++++--------- wolfcrypt/test/test.c | 22 +++++++++------------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/tests/api.c b/tests/api.c index 3e962493c..4b6cf2c00 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15241,9 +15241,12 @@ static int test_wc_RsaPublicEncryptDecrypt (void) DECLARE_VAR(cipher, byte, cipherLen, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if (in && inStr) - XMEMCPY(in, inStr, inLen); + if (in == NULL || plain == NULL || cipher == NULL) { + printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n"); + return MEMORY_E; + } #endif + XMEMCPY(in, inStr, inLen); ret = wc_InitRsaKey(&key, NULL); if (ret == 0) { @@ -15336,9 +15339,12 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void) DECLARE_VAR(cipher, byte, cipherSz, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if (in && && inStr) - XMEMCPY(in, inStr, inLen); + if (in == NULL || plain == NULL || cipher == NULL) { + printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n"); + return MEMORY_E; + } #endif + XMEMCPY(in, inStr, inLen); /* Initialize stack structures. */ XMEMSET(&rng, 0, sizeof(rng)); @@ -15458,9 +15464,12 @@ static int test_wc_RsaSSL_SignVerify (void) DECLARE_VAR(plain, byte, plainSz, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if (in && inStr) - XMEMCPY(in, inStr, inLen); + if (in == NULL || plain == NULL || cipher == NULL) { + printf("test_wc_RsaSSL_SignVerify failed\n"); + return MEMORY_E; + } #endif + XMEMCPY(in, inStr, inLen); ret = wc_InitRsaKey(&key, NULL); diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 8d3b3cc12..35a3451e6 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -2248,8 +2248,8 @@ static void bench_aesgcm_internal(int doAsync, const byte* key, word32 keySz, DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT); DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((bench_additional == NULL) || (bench_tag == NULL)) { - printf("malloc failed\n"); + if (bench_additional == NULL || bench_tag == NULL) { + printf("bench_aesgcm_internal malloc failed\n"); goto exit; } #endif @@ -2692,9 +2692,10 @@ void bench_aesccm(void) DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT); DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT); + #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((bench_additional == NULL) || (bench_tag == NULL)) { - printf("malloc failed\n"); + if (bench_additional == NULL || bench_tag == NULL) { + printf("bench_aesccm malloc failed\n"); goto exit; } #endif @@ -4546,10 +4547,7 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING], ret = MEMORY_E; goto exit; } - #ifndef WOLFSSL_RSA_VERIFY_ONLY - if (message && messageStr) - XMEMCPY(message, messageStr, len); - #endif + XMEMCPY(message, messageStr, len); #endif if (!rsa_sign_verify) { @@ -4900,7 +4898,7 @@ void bench_dh(int doAsync) DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT); DECLARE_VAR(priv2, byte, BENCH_DH_PRIV_SIZE, HEAP_HINT); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((pub[0] == NULL) || (pub2 == NULL) || (agree[0] == NULL) || (priv[0] == NULL) || (priv2 == NULL)) { + if (pub[0] == NULL || pub2 == NULL || agree[0] == NULL || priv[0] == NULL || priv2 == NULL) { ret = MEMORY_E; goto exit; } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index bdc3020cf..ccc8582a8 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -12734,12 +12734,10 @@ static int rsa_pss_test(WC_RNG* rng, RsaKey* key) DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((in == NULL) || (out == NULL) || (sig == NULL)) + if (in == NULL || out == NULL || sig == NULL) ERROR_OUT(MEMORY_E, exit_rsa_pss); - - if (in && inStr) - XMEMCPY(in, inStr, inLen); #endif + XMEMCPY(in, inStr, inLen); /* Test all combinations of hash and MGF. */ for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) { @@ -13027,7 +13025,7 @@ WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void) DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((key == NULL) || (out == NULL) || (plain == NULL)) + if (key == NULL || out == NULL || plain == NULL) ERROR_OUT(MEMORY_E, exit_rsa_nopadding); #endif @@ -13252,7 +13250,7 @@ static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key) DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((out == NULL) || (plain == NULL)) + if (out == NULL || plain == NULL) ERROR_OUT(MEMORY_E, exit_rsa_even_mod); #endif @@ -14093,12 +14091,10 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void) #endif #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if ((in == NULL) || (out == NULL) || (plain == NULL)) + if (in == NULL || out == NULL || plain == NULL) ERROR_OUT(MEMORY_E, exit_rsa); - - if (in && inStr) - XMEMCPY(in, inStr, inLen); #endif + XMEMCPY(in, inStr, inLen); #ifdef WOLFSSL_SMALL_STACK if (key == NULL) @@ -19416,7 +19412,7 @@ static int ecc_test_vector_item(const eccVector* vector) if (sig == NULL) ERROR_OUT(MEMORY_E, done); #if !defined(NO_ASN) && !defined(HAVE_SELFTEST) - if (sigRaw == NULL) + if (sigRaw == NULL || r == NULL || s == NULL) ERROR_OUT(MEMORY_E, done); #endif #endif @@ -20412,7 +20408,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, #ifdef DECLARE_VAR_IS_HEAP_ALLOC #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && \ !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) - if ((sharedA == NULL) || (sharedB == NULL)) + if (sharedA == NULL || sharedB == NULL) ERROR_OUT(-9900, done); #endif @@ -20422,7 +20418,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, #endif #ifdef HAVE_ECC_SIGN - if ((sig == NULL) || (digest == NULL)) + if (sig == NULL || digest == NULL) ERROR_OUT(-9902, done); #endif #endif /* WOLFSSL_SMALL_STACK */