From 17f101ef13efe872be1d5ea0d9e1c79ded06ca86 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 20 Jan 2021 16:28:41 -0800 Subject: [PATCH 1/6] Fix for ARM Keil MDK compiler issue with `DECLARE_VAR_INIT`. --- tests/api.c | 21 ++++++++++++++++++--- wolfcrypt/benchmark/benchmark.c | 6 +++++- wolfcrypt/test/test.c | 10 ++++++++-- wolfssl/wolfcrypt/types.h | 12 +----------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/tests/api.c b/tests/api.c index acf6deb9b..3e962493c 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15236,10 +15236,15 @@ static int test_wc_RsaPublicEncryptDecrypt (void) word32 cipherLen = 256; #endif - DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL); + DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(plain, byte, plainLen, NULL); DECLARE_VAR(cipher, byte, cipherLen, NULL); +#ifdef DECLARE_VAR_IS_HEAP_ALLOC + if (in && inStr) + XMEMCPY(in, inStr, inLen); +#endif + ret = wc_InitRsaKey(&key, NULL); if (ret == 0) { ret = wc_InitRng(&rng); @@ -15326,10 +15331,15 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void) const word32 cipherSz = 256; #endif - DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL); + DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(plain, byte, plainSz, NULL); DECLARE_VAR(cipher, byte, cipherSz, NULL); +#ifdef DECLARE_VAR_IS_HEAP_ALLOC + if (in && && inStr) + XMEMCPY(in, inStr, inLen); +#endif + /* Initialize stack structures. */ XMEMSET(&rng, 0, sizeof(rng)); XMEMSET(&key, 0, sizeof(key)); @@ -15443,10 +15453,15 @@ static int test_wc_RsaSSL_SignVerify (void) const word32 outSz = 256; #endif - DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL); + DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(out, byte, outSz, NULL); DECLARE_VAR(plain, byte, plainSz, NULL); +#ifdef DECLARE_VAR_IS_HEAP_ALLOC + if (in && inStr) + XMEMCPY(in, inStr, inLen); +#endif + ret = wc_InitRsaKey(&key, NULL); if (ret == 0) { diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 08c3b4ea5..8d3b3cc12 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -4507,7 +4507,7 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING], double start = 0.0f; const char**desc = bench_desc_words[lng_index]; #ifndef WOLFSSL_RSA_VERIFY_ONLY - DECLARE_VAR_INIT(message, byte, len, messageStr, HEAP_HINT); + DECLARE_VAR(message, byte, len, HEAP_HINT); #endif #if !defined(WOLFSSL_MDK5_COMPLv5) /* MDK5 compiler regard this as a executable statement, and does not allow declarations after the line. */ @@ -4546,6 +4546,10 @@ 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 #endif if (!rsa_sign_verify) { diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index af9e696df..bdc3020cf 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -12729,13 +12729,16 @@ static int rsa_pss_test(WC_RNG* rng, RsaKey* key) #endif }; - DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT); + DECLARE_VAR(in, byte, inLen, HEAP_HINT); DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT); #ifdef DECLARE_VAR_IS_HEAP_ALLOC if ((in == NULL) || (out == NULL) || (sig == NULL)) ERROR_OUT(MEMORY_E, exit_rsa_pss); + + if (in && inStr) + XMEMCPY(in, inStr, inLen); #endif /* Test all combinations of hash and MGF. */ @@ -14084,7 +14087,7 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void) #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \ !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) - DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT); + DECLARE_VAR(in, byte, inLen, HEAP_HINT); DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT); #endif @@ -14092,6 +14095,9 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void) #ifdef DECLARE_VAR_IS_HEAP_ALLOC if ((in == NULL) || (out == NULL) || (plain == NULL)) ERROR_OUT(MEMORY_E, exit_rsa); + + if (in && inStr) + XMEMCPY(in, inStr, inLen); #endif #ifdef WOLFSSL_SMALL_STACK diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index bbab5121f..25a8401a5 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -412,15 +412,7 @@ decouple library dependencies with standard string, memory and so on. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_SMALL_STACK) #define DECLARE_VAR_IS_HEAP_ALLOC #define DECLARE_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \ - VAR_TYPE* VAR_NAME = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT); - #define DECLARE_VAR_INIT(VAR_NAME, VAR_TYPE, VAR_SIZE, INIT_VALUE, HEAP) \ - VAR_TYPE* VAR_NAME = ({ \ - VAR_TYPE* ptr = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT); \ - if (ptr && INIT_VALUE) { \ - XMEMCPY(ptr, INIT_VALUE, sizeof(VAR_TYPE) * VAR_SIZE); \ - } \ - ptr; \ - }) + VAR_TYPE* VAR_NAME = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT) #define DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \ VAR_TYPE* VAR_NAME[VAR_ITEMS]; \ int idx##VAR_NAME, inner_idx_##VAR_NAME; \ @@ -453,8 +445,6 @@ decouple library dependencies with standard string, memory and so on. #undef DECLARE_VAR_IS_HEAP_ALLOC #define DECLARE_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \ VAR_TYPE VAR_NAME[VAR_SIZE] - #define DECLARE_VAR_INIT(VAR_NAME, VAR_TYPE, VAR_SIZE, INIT_VALUE, HEAP) \ - VAR_TYPE* VAR_NAME = (VAR_TYPE*)INIT_VALUE #define DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \ VAR_TYPE VAR_NAME[VAR_ITEMS][VAR_SIZE] #define FREE_VAR(VAR_NAME, HEAP) /* nothing to free, its stack */ From 1ee40ad7bd373ad98b2cf144833a108640af2a07 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 21 Jan 2021 17:12:29 -0800 Subject: [PATCH 2/6] 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 */ From 9012317f5bdcf4b63b499a8a9f7baee2b15562a2 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 21 Jan 2021 17:41:11 -0800 Subject: [PATCH 3/6] Fix copy/paste typo. --- tests/api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index 4b6cf2c00..76e607e51 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15464,7 +15464,7 @@ static int test_wc_RsaSSL_SignVerify (void) DECLARE_VAR(plain, byte, plainSz, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC - if (in == NULL || plain == NULL || cipher == NULL) { + if (in == NULL || out == NULL || plain == NULL) { printf("test_wc_RsaSSL_SignVerify failed\n"); return MEMORY_E; } From 46aee19de321cac3092fc02bb51ee18395e34af1 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 22 Jan 2021 10:44:38 -0800 Subject: [PATCH 4/6] Fix for Visual Studio issue with non-cost in array declaration. --- wolfcrypt/test/test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index ccc8582a8..f22d3d96c 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -12687,8 +12687,8 @@ static int rsa_pss_test(WC_RNG* rng, RsaKey* key) { byte digest[WC_MAX_DIGEST_SIZE]; int ret = 0; - const char* inStr = "Everyone gets Friday off."; - word32 inLen = (word32)XSTRLEN((char*)inStr); + const char inStr[] = "Everyone gets Friday off."; + word32 inLen = (word32)(sizeof(inStr)-1); /* do not include NULL term */ word32 outSz; word32 plainSz; word32 digestSz; @@ -14058,8 +14058,8 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void) #endif #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \ !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) - const char* inStr = "Everyone gets Friday off."; - word32 inLen = (word32)XSTRLEN((char*)inStr); + const char inStr[] = "Everyone gets Friday off."; + const word32 inLen = (word32)(sizeof(inStr)-1); /* do not include NULL */ const word32 outSz = RSA_TEST_BYTES; const word32 plainSz = RSA_TEST_BYTES; #endif From 13468d34e3256a211e380b5b9e303af7b2f07a01 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 22 Jan 2021 10:50:18 -0800 Subject: [PATCH 5/6] Apply same VS fixes to api.c as well. --- tests/api.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/api.c b/tests/api.c index 76e607e51..16933b9b0 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15225,16 +15225,17 @@ static int test_wc_RsaPublicEncryptDecrypt (void) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) RsaKey key; WC_RNG rng; - const char* inStr = "Everyone gets Friday off."; - word32 plainLen = 25; - word32 inLen = (word32)XSTRLEN(inStr); + const char inStr[] = "Everyone gets Friday off."; + const word32 plainLen = 25; + const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) - int bits = 1024; - word32 cipherLen = 128; + int bits = 1024; + const word32 cipherLen = 128; #else - int bits = 2048; - word32 cipherLen = 256; + int bits = 2048; + const word32 cipherLen = 256; #endif + word32 cipherLenResult = cipherLen; DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(plain, byte, plainLen, NULL); @@ -15261,7 +15262,7 @@ static int test_wc_RsaPublicEncryptDecrypt (void) if (ret == 0) { ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng); if (ret >= 0) { - cipherLen = ret; + cipherLenResult = ret; ret = 0; } else { ret = WOLFSSL_FATAL_ERROR; @@ -15284,7 +15285,7 @@ static int test_wc_RsaPublicEncryptDecrypt (void) } #endif if (ret == 0) { - ret = wc_RsaPrivateDecrypt(cipher, cipherLen, plain, plainLen, &key); + ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key); } if (ret >= 0) { ret = XMEMCMP(plain, inStr, plainLen); @@ -15321,8 +15322,8 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void) && !defined(NO_SHA) RsaKey key; WC_RNG rng; - const char* inStr = "Everyone gets Friday off."; - word32 inLen = (word32)XSTRLEN(inStr); + const char inStr[] = "Everyone gets Friday off."; + const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ const word32 plainSz = 25; byte* res = NULL; int idx = 0; @@ -15447,9 +15448,9 @@ static int test_wc_RsaSSL_SignVerify (void) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) RsaKey key; WC_RNG rng; - const char* inStr = "Everyone gets Friday off."; + const char inStr[] = "Everyone gets Friday off."; const word32 plainSz = 25; - word32 inLen = (word32)XSTRLEN(inStr); + const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ word32 idx = 0; #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) int bits = 1024; From 05e1ee169456cadd0aff49d2b50fcd753d411846 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 25 Jan 2021 09:14:12 -0800 Subject: [PATCH 6/6] Cleanup to use fixed sizes from defines for `DECLARE_VAR`. Resolves issue with Visual Studio and using a variable (even const) to declare an array size. --- tests/api.c | 113 +++++++++++++++----------------- wolfcrypt/benchmark/benchmark.c | 11 +++- wolfcrypt/test/test.c | 21 +++--- 3 files changed, 74 insertions(+), 71 deletions(-) mode change 100644 => 100755 wolfcrypt/test/test.c diff --git a/tests/api.c b/tests/api.c index 16933b9b0..d52995643 100644 --- a/tests/api.c +++ b/tests/api.c @@ -396,6 +396,16 @@ typedef struct testVector { static const char* passed = "passed"; static const char* failed = "failed"; +#define TEST_STRING "Everyone gets Friday off." +#define TEST_STRING_SZ 25 + +#if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) +#define TEST_RSA_BITS 1024 +#else +#define TEST_RSA_BITS 2048 +#endif +#define TEST_RSA_BYTES (TEST_RSA_BITS/8) + #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \ (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)) static const char* bogusFile = @@ -2312,8 +2322,8 @@ static void test_ED25519(void) unsigned int privSz = (unsigned int)sizeof(priv); byte pub[ED25519_PUB_KEY_SIZE]; unsigned int pubSz = (unsigned int)sizeof(pub); - const char* msg = "Everyone gets Friday off."; - unsigned int msglen = (unsigned int)XSTRLEN(msg); + const char* msg = TEST_STRING; + unsigned int msglen = (unsigned int)TEST_STRING_SZ; byte sig[ED25519_SIG_SIZE]; unsigned int sigSz = (unsigned int)sizeof(sig); @@ -2338,8 +2348,8 @@ static void test_ED448(void) unsigned int privSz = (unsigned int)sizeof(priv); byte pub[ED448_PUB_KEY_SIZE]; unsigned int pubSz = (unsigned int)sizeof(pub); - const char* msg = "Everyone gets Friday off."; - unsigned int msglen = (unsigned int)XSTRLEN(msg); + const char* msg = TEST_STRING; + unsigned int msglen = (unsigned int)TEST_STRING_SZ; byte sig[ED448_SIG_SIZE]; unsigned int sigSz = (unsigned int)sizeof(sig); @@ -9228,8 +9238,8 @@ static int test_wc_Sha3_224_Copy (void) int ret = 0; #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) wc_Sha3 sha3, sha3Cpy; - const char* msg = "Everyone gets Friday off."; - word32 msglen = (word32)XSTRLEN(msg); + const char* msg = TEST_STRING; + word32 msglen = (word32)TEST_STRING_SZ; byte hash[WC_SHA3_224_DIGEST_SIZE]; byte hashCpy[WC_SHA3_224_DIGEST_SIZE]; @@ -9293,8 +9303,8 @@ static int test_wc_Sha3_256_Copy (void) int ret = 0; #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) wc_Sha3 sha3, sha3Cpy; - const char* msg = "Everyone gets Friday off."; - word32 msglen = (word32)XSTRLEN(msg); + const char* msg = TEST_STRING; + word32 msglen = (word32)TEST_STRING_SZ; byte hash[WC_SHA3_256_DIGEST_SIZE]; byte hashCpy[WC_SHA3_256_DIGEST_SIZE]; @@ -9358,8 +9368,8 @@ static int test_wc_Sha3_384_Copy (void) int ret = 0; #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) wc_Sha3 sha3, sha3Cpy; - const char* msg = "Everyone gets Friday off."; - word32 msglen = (word32)XSTRLEN(msg); + const char* msg = TEST_STRING; + word32 msglen = (word32)TEST_STRING_SZ; byte hash[WC_SHA3_384_DIGEST_SIZE]; byte hashCpy[WC_SHA3_384_DIGEST_SIZE]; @@ -9423,8 +9433,8 @@ static int test_wc_Sha3_512_Copy (void) #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) wc_Sha3 sha3, sha3Cpy; - const char* msg = "Everyone gets Friday off."; - word32 msglen = (word32)XSTRLEN(msg); + const char* msg = TEST_STRING; + word32 msglen = (word32)TEST_STRING_SZ; byte hash[WC_SHA3_512_DIGEST_SIZE]; byte hashCpy[WC_SHA3_512_DIGEST_SIZE]; @@ -9665,8 +9675,8 @@ static int test_wc_Shake256_Copy (void) int ret = 0; #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256) wc_Shake shake, shakeCpy; - const char* msg = "Everyone gets Friday off."; - word32 msglen = (word32)XSTRLEN(msg); + const char* msg = TEST_STRING; + word32 msglen = (word32)TEST_STRING_SZ; byte hash[144]; byte hashCpy[144]; word32 hashLen = sizeof(hash); @@ -13887,8 +13897,8 @@ static int test_wc_RabbitProcess (void) const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B" "\xFE\x36\x3D\x2E\x29\x13\x28\x91"; const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3"; - const char* input = "Everyone gets Friday off."; - unsigned long int inlen = XSTRLEN(input); + const char* input = TEST_STRING; + unsigned long int inlen = (unsigned long int)TEST_STRING_SZ; /* Initialize stack variables. */ XMEMSET(cipher, 0, sizeof(cipher)); @@ -15225,21 +15235,16 @@ static int test_wc_RsaPublicEncryptDecrypt (void) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) RsaKey key; WC_RNG rng; - const char inStr[] = "Everyone gets Friday off."; - const word32 plainLen = 25; - const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ - #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) - int bits = 1024; - const word32 cipherLen = 128; - #else - int bits = 2048; - const word32 cipherLen = 256; - #endif + const char inStr[] = TEST_STRING; + const word32 plainLen = (word32)TEST_STRING_SZ; + const word32 inLen = (word32)TEST_STRING_SZ; + int bits = TEST_RSA_BITS; + const word32 cipherLen = TEST_RSA_BYTES; word32 cipherLenResult = cipherLen; - DECLARE_VAR(in, byte, inLen, NULL); - DECLARE_VAR(plain, byte, plainLen, NULL); - DECLARE_VAR(cipher, byte, cipherLen, NULL); + DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL); + DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL); + DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC if (in == NULL || plain == NULL || cipher == NULL) { @@ -15322,22 +15327,17 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void) && !defined(NO_SHA) RsaKey key; WC_RNG rng; - const char inStr[] = "Everyone gets Friday off."; - const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ - const word32 plainSz = 25; + const char inStr[] = TEST_STRING; + const word32 inLen = (word32)TEST_STRING_SZ; + const word32 plainSz = (word32)TEST_STRING_SZ; byte* res = NULL; int idx = 0; - #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) - int bits = 1024; - const word32 cipherSz = 128; - #else - int bits = 2048; - const word32 cipherSz = 256; - #endif + int bits = TEST_RSA_BITS; + const word32 cipherSz = TEST_RSA_BYTES; - DECLARE_VAR(in, byte, inLen, NULL); - DECLARE_VAR(plain, byte, plainSz, NULL); - DECLARE_VAR(cipher, byte, cipherSz, NULL); + DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL); + DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL); + DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC if (in == NULL || plain == NULL || cipher == NULL) { @@ -15448,21 +15448,16 @@ static int test_wc_RsaSSL_SignVerify (void) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) RsaKey key; WC_RNG rng; - const char inStr[] = "Everyone gets Friday off."; - const word32 plainSz = 25; - const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ + const char inStr[] = TEST_STRING; + const word32 plainSz = (word32)TEST_STRING_SZ; + const word32 inLen = (word32)TEST_STRING_SZ; word32 idx = 0; - #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) - int bits = 1024; - const word32 outSz = 128; - #else - int bits = 2048; - const word32 outSz = 256; - #endif + int bits = TEST_RSA_BITS; + const word32 outSz = TEST_RSA_BYTES; - DECLARE_VAR(in, byte, inLen, NULL); - DECLARE_VAR(out, byte, outSz, NULL); - DECLARE_VAR(plain, byte, plainSz, NULL); + DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL); + DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL); + DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL); #ifdef DECLARE_VAR_IS_HEAP_ALLOC if (in == NULL || out == NULL || plain == NULL) { @@ -20231,8 +20226,8 @@ static int test_wc_ecc_signVerify_hash (void) #endif word32 siglen = ECC_BUFSIZE; byte sig[ECC_BUFSIZE]; - byte digest[] = "Everyone gets Friday off."; - word32 digestlen = (word32)XSTRLEN((char*)digest); + byte digest[] = TEST_STRING; + word32 digestlen = (word32)TEST_STRING_SZ; /* Init stack var */ XMEMSET(sig, 0, siglen); @@ -21778,7 +21773,7 @@ static int test_wc_ecc_verify_hash_ex (void) mp_int s; unsigned char hash[] = "Everyone gets Friday off.EccSig"; unsigned char iHash[] = "Everyone gets Friday off......."; - unsigned char shortHash[] = "Everyone gets Friday off."; + unsigned char shortHash[] = TEST_STRING; word32 hashlen = sizeof(hash); word32 iHashLen = sizeof(iHash); word32 shortHashLen = sizeof(shortHash); @@ -30311,7 +30306,7 @@ static int test_WOLFSSL_ERROR_MSG (void) int ret = 0; #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\ defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) - const char* msg = "Everyone gets Friday off."; + const char* msg = TEST_STRING; printf(testingFmt, "WOLFSSL_ERROR_MSG()"); diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 35a3451e6..f357d7b84 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -210,6 +210,9 @@ #define TEST_SLEEP() #endif +#define TEST_STRING "Everyone gets Friday off." +#define TEST_STRING_SZ 25 + /* Bit values for each algorithm that is able to be benchmarked. * Common grouping of algorithms also. @@ -4502,13 +4505,13 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING], int ret = 0, i, times, count = 0, pending = 0; word32 idx = 0; #ifndef WOLFSSL_RSA_VERIFY_ONLY - const char* messageStr = "Everyone gets Friday off."; - const int len = (int)XSTRLEN((char*)messageStr); + const char* messageStr = TEST_STRING; + const int len = (int)TEST_STRING_SZ; #endif double start = 0.0f; const char**desc = bench_desc_words[lng_index]; #ifndef WOLFSSL_RSA_VERIFY_ONLY - DECLARE_VAR(message, byte, len, HEAP_HINT); + DECLARE_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT); #endif #if !defined(WOLFSSL_MDK5_COMPLv5) /* MDK5 compiler regard this as a executable statement, and does not allow declarations after the line. */ @@ -4547,6 +4550,8 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING], ret = MEMORY_E; goto exit; } +#endif +#ifndef WOLFSSL_RSA_VERIFY_ONLY XMEMCPY(message, messageStr, len); #endif diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c old mode 100644 new mode 100755 index f22d3d96c..1bc6a78bf --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -322,6 +322,9 @@ static int devId = INVALID_DEVID; const char* wnrConfigFile = "wnr-example.conf"; #endif +#define TEST_STRING "Everyone gets Friday off." +#define TEST_STRING_SZ 25 + typedef struct testVector { const char* input; const char* output; @@ -12143,7 +12146,7 @@ static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng) { int ret; word32 sigSz; - WOLFSSL_SMALL_STACK_STATIC const byte in[] = "Everyone gets Friday off."; + WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING; WOLFSSL_SMALL_STACK_STATIC const byte hash[] = { 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59, 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe, @@ -12687,8 +12690,8 @@ static int rsa_pss_test(WC_RNG* rng, RsaKey* key) { byte digest[WC_MAX_DIGEST_SIZE]; int ret = 0; - const char inStr[] = "Everyone gets Friday off."; - word32 inLen = (word32)(sizeof(inStr)-1); /* do not include NULL term */ + const char inStr[] = TEST_STRING; + word32 inLen = (word32)TEST_STRING_SZ; word32 outSz; word32 plainSz; word32 digestSz; @@ -12729,7 +12732,7 @@ static int rsa_pss_test(WC_RNG* rng, RsaKey* key) #endif }; - DECLARE_VAR(in, byte, inLen, HEAP_HINT); + DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT); @@ -14058,8 +14061,8 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void) #endif #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \ !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) - const char inStr[] = "Everyone gets Friday off."; - const word32 inLen = (word32)(sizeof(inStr)-1); /* do not include NULL */ + const char inStr[] = TEST_STRING; + const word32 inLen = (word32)TEST_STRING_SZ; const word32 outSz = RSA_TEST_BYTES; const word32 plainSz = RSA_TEST_BYTES; #endif @@ -14085,7 +14088,7 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void) #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \ !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) - DECLARE_VAR(in, byte, inLen, HEAP_HINT); + DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT); DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT); #endif @@ -18278,7 +18281,7 @@ WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void) EVP_PKEY_CTX *enc = NULL; EVP_PKEY_CTX *dec = NULL; - byte in[] = "Everyone gets Friday off."; + byte in[] = TEST_STRING; byte out[256]; size_t outlen; size_t keySz; @@ -21096,7 +21099,7 @@ static int ecc_sig_test(WC_RNG* rng, ecc_key* key) word32 sigSz; int size; byte out[ECC_MAX_SIG_SIZE]; - byte in[] = "Everyone gets Friday off."; + byte in[] = TEST_STRING; WOLFSSL_SMALL_STACK_STATIC const byte hash[] = { 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59, 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,