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.

This commit is contained in:
David Garske
2021-01-25 09:14:12 -08:00
parent 13468d34e3
commit 05e1ee1694
3 changed files with 74 additions and 71 deletions

View File

@@ -396,6 +396,16 @@ typedef struct testVector {
static const char* passed = "passed"; static const char* passed = "passed";
static const char* failed = "failed"; 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) && \ #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
(!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)) (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
static const char* bogusFile = static const char* bogusFile =
@@ -2312,8 +2322,8 @@ static void test_ED25519(void)
unsigned int privSz = (unsigned int)sizeof(priv); unsigned int privSz = (unsigned int)sizeof(priv);
byte pub[ED25519_PUB_KEY_SIZE]; byte pub[ED25519_PUB_KEY_SIZE];
unsigned int pubSz = (unsigned int)sizeof(pub); unsigned int pubSz = (unsigned int)sizeof(pub);
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
unsigned int msglen = (unsigned int)XSTRLEN(msg); unsigned int msglen = (unsigned int)TEST_STRING_SZ;
byte sig[ED25519_SIG_SIZE]; byte sig[ED25519_SIG_SIZE];
unsigned int sigSz = (unsigned int)sizeof(sig); unsigned int sigSz = (unsigned int)sizeof(sig);
@@ -2338,8 +2348,8 @@ static void test_ED448(void)
unsigned int privSz = (unsigned int)sizeof(priv); unsigned int privSz = (unsigned int)sizeof(priv);
byte pub[ED448_PUB_KEY_SIZE]; byte pub[ED448_PUB_KEY_SIZE];
unsigned int pubSz = (unsigned int)sizeof(pub); unsigned int pubSz = (unsigned int)sizeof(pub);
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
unsigned int msglen = (unsigned int)XSTRLEN(msg); unsigned int msglen = (unsigned int)TEST_STRING_SZ;
byte sig[ED448_SIG_SIZE]; byte sig[ED448_SIG_SIZE];
unsigned int sigSz = (unsigned int)sizeof(sig); unsigned int sigSz = (unsigned int)sizeof(sig);
@@ -9228,8 +9238,8 @@ static int test_wc_Sha3_224_Copy (void)
int ret = 0; int ret = 0;
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wc_Sha3 sha3, sha3Cpy; wc_Sha3 sha3, sha3Cpy;
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
word32 msglen = (word32)XSTRLEN(msg); word32 msglen = (word32)TEST_STRING_SZ;
byte hash[WC_SHA3_224_DIGEST_SIZE]; byte hash[WC_SHA3_224_DIGEST_SIZE];
byte hashCpy[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; int ret = 0;
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wc_Sha3 sha3, sha3Cpy; wc_Sha3 sha3, sha3Cpy;
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
word32 msglen = (word32)XSTRLEN(msg); word32 msglen = (word32)TEST_STRING_SZ;
byte hash[WC_SHA3_256_DIGEST_SIZE]; byte hash[WC_SHA3_256_DIGEST_SIZE];
byte hashCpy[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; int ret = 0;
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wc_Sha3 sha3, sha3Cpy; wc_Sha3 sha3, sha3Cpy;
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
word32 msglen = (word32)XSTRLEN(msg); word32 msglen = (word32)TEST_STRING_SZ;
byte hash[WC_SHA3_384_DIGEST_SIZE]; byte hash[WC_SHA3_384_DIGEST_SIZE];
byte hashCpy[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) #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wc_Sha3 sha3, sha3Cpy; wc_Sha3 sha3, sha3Cpy;
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
word32 msglen = (word32)XSTRLEN(msg); word32 msglen = (word32)TEST_STRING_SZ;
byte hash[WC_SHA3_512_DIGEST_SIZE]; byte hash[WC_SHA3_512_DIGEST_SIZE];
byte hashCpy[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; int ret = 0;
#if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256) #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
wc_Shake shake, shakeCpy; wc_Shake shake, shakeCpy;
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
word32 msglen = (word32)XSTRLEN(msg); word32 msglen = (word32)TEST_STRING_SZ;
byte hash[144]; byte hash[144];
byte hashCpy[144]; byte hashCpy[144];
word32 hashLen = sizeof(hash); 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" const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
"\xFE\x36\x3D\x2E\x29\x13\x28\x91"; "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3"; const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
const char* input = "Everyone gets Friday off."; const char* input = TEST_STRING;
unsigned long int inlen = XSTRLEN(input); unsigned long int inlen = (unsigned long int)TEST_STRING_SZ;
/* Initialize stack variables. */ /* Initialize stack variables. */
XMEMSET(cipher, 0, sizeof(cipher)); XMEMSET(cipher, 0, sizeof(cipher));
@@ -15225,21 +15235,16 @@ static int test_wc_RsaPublicEncryptDecrypt (void)
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
RsaKey key; RsaKey key;
WC_RNG rng; WC_RNG rng;
const char inStr[] = "Everyone gets Friday off."; const char inStr[] = TEST_STRING;
const word32 plainLen = 25; const word32 plainLen = (word32)TEST_STRING_SZ;
const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ const word32 inLen = (word32)TEST_STRING_SZ;
#if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) int bits = TEST_RSA_BITS;
int bits = 1024; const word32 cipherLen = TEST_RSA_BYTES;
const word32 cipherLen = 128;
#else
int bits = 2048;
const word32 cipherLen = 256;
#endif
word32 cipherLenResult = cipherLen; word32 cipherLenResult = cipherLen;
DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
DECLARE_VAR(plain, byte, plainLen, NULL); DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
DECLARE_VAR(cipher, byte, cipherLen, NULL); DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
#ifdef DECLARE_VAR_IS_HEAP_ALLOC #ifdef DECLARE_VAR_IS_HEAP_ALLOC
if (in == NULL || plain == NULL || cipher == NULL) { if (in == NULL || plain == NULL || cipher == NULL) {
@@ -15322,22 +15327,17 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void)
&& !defined(NO_SHA) && !defined(NO_SHA)
RsaKey key; RsaKey key;
WC_RNG rng; WC_RNG rng;
const char inStr[] = "Everyone gets Friday off."; const char inStr[] = TEST_STRING;
const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ const word32 inLen = (word32)TEST_STRING_SZ;
const word32 plainSz = 25; const word32 plainSz = (word32)TEST_STRING_SZ;
byte* res = NULL; byte* res = NULL;
int idx = 0; int idx = 0;
#if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) int bits = TEST_RSA_BITS;
int bits = 1024; const word32 cipherSz = TEST_RSA_BYTES;
const word32 cipherSz = 128;
#else
int bits = 2048;
const word32 cipherSz = 256;
#endif
DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
DECLARE_VAR(plain, byte, plainSz, NULL); DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
DECLARE_VAR(cipher, byte, cipherSz, NULL); DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
#ifdef DECLARE_VAR_IS_HEAP_ALLOC #ifdef DECLARE_VAR_IS_HEAP_ALLOC
if (in == NULL || plain == NULL || cipher == NULL) { 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) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
RsaKey key; RsaKey key;
WC_RNG rng; WC_RNG rng;
const char inStr[] = "Everyone gets Friday off."; const char inStr[] = TEST_STRING;
const word32 plainSz = 25; const word32 plainSz = (word32)TEST_STRING_SZ;
const word32 inLen = (word32)(sizeof(inStr)-1); /* exclude NULL term */ const word32 inLen = (word32)TEST_STRING_SZ;
word32 idx = 0; word32 idx = 0;
#if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) int bits = TEST_RSA_BITS;
int bits = 1024; const word32 outSz = TEST_RSA_BYTES;
const word32 outSz = 128;
#else
int bits = 2048;
const word32 outSz = 256;
#endif
DECLARE_VAR(in, byte, inLen, NULL); DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
DECLARE_VAR(out, byte, outSz, NULL); DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
DECLARE_VAR(plain, byte, plainSz, NULL); DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
#ifdef DECLARE_VAR_IS_HEAP_ALLOC #ifdef DECLARE_VAR_IS_HEAP_ALLOC
if (in == NULL || out == NULL || plain == NULL) { if (in == NULL || out == NULL || plain == NULL) {
@@ -20231,8 +20226,8 @@ static int test_wc_ecc_signVerify_hash (void)
#endif #endif
word32 siglen = ECC_BUFSIZE; word32 siglen = ECC_BUFSIZE;
byte sig[ECC_BUFSIZE]; byte sig[ECC_BUFSIZE];
byte digest[] = "Everyone gets Friday off."; byte digest[] = TEST_STRING;
word32 digestlen = (word32)XSTRLEN((char*)digest); word32 digestlen = (word32)TEST_STRING_SZ;
/* Init stack var */ /* Init stack var */
XMEMSET(sig, 0, siglen); XMEMSET(sig, 0, siglen);
@@ -21778,7 +21773,7 @@ static int test_wc_ecc_verify_hash_ex (void)
mp_int s; mp_int s;
unsigned char hash[] = "Everyone gets Friday off.EccSig"; unsigned char hash[] = "Everyone gets Friday off.EccSig";
unsigned char iHash[] = "Everyone gets Friday off......."; unsigned char iHash[] = "Everyone gets Friday off.......";
unsigned char shortHash[] = "Everyone gets Friday off."; unsigned char shortHash[] = TEST_STRING;
word32 hashlen = sizeof(hash); word32 hashlen = sizeof(hash);
word32 iHashLen = sizeof(iHash); word32 iHashLen = sizeof(iHash);
word32 shortHashLen = sizeof(shortHash); word32 shortHashLen = sizeof(shortHash);
@@ -30311,7 +30306,7 @@ static int test_WOLFSSL_ERROR_MSG (void)
int ret = 0; int ret = 0;
#if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\ #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
const char* msg = "Everyone gets Friday off."; const char* msg = TEST_STRING;
printf(testingFmt, "WOLFSSL_ERROR_MSG()"); printf(testingFmt, "WOLFSSL_ERROR_MSG()");

View File

@@ -210,6 +210,9 @@
#define TEST_SLEEP() #define TEST_SLEEP()
#endif #endif
#define TEST_STRING "Everyone gets Friday off."
#define TEST_STRING_SZ 25
/* Bit values for each algorithm that is able to be benchmarked. /* Bit values for each algorithm that is able to be benchmarked.
* Common grouping of algorithms also. * 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; int ret = 0, i, times, count = 0, pending = 0;
word32 idx = 0; word32 idx = 0;
#ifndef WOLFSSL_RSA_VERIFY_ONLY #ifndef WOLFSSL_RSA_VERIFY_ONLY
const char* messageStr = "Everyone gets Friday off."; const char* messageStr = TEST_STRING;
const int len = (int)XSTRLEN((char*)messageStr); const int len = (int)TEST_STRING_SZ;
#endif #endif
double start = 0.0f; double start = 0.0f;
const char**desc = bench_desc_words[lng_index]; const char**desc = bench_desc_words[lng_index];
#ifndef WOLFSSL_RSA_VERIFY_ONLY #ifndef WOLFSSL_RSA_VERIFY_ONLY
DECLARE_VAR(message, byte, len, HEAP_HINT); DECLARE_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
#endif #endif
#if !defined(WOLFSSL_MDK5_COMPLv5) #if !defined(WOLFSSL_MDK5_COMPLv5)
/* MDK5 compiler regard this as a executable statement, and does not allow declarations after the line. */ /* 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; ret = MEMORY_E;
goto exit; goto exit;
} }
#endif
#ifndef WOLFSSL_RSA_VERIFY_ONLY
XMEMCPY(message, messageStr, len); XMEMCPY(message, messageStr, len);
#endif #endif

21
wolfcrypt/test/test.c Normal file → Executable file
View File

@@ -322,6 +322,9 @@ static int devId = INVALID_DEVID;
const char* wnrConfigFile = "wnr-example.conf"; const char* wnrConfigFile = "wnr-example.conf";
#endif #endif
#define TEST_STRING "Everyone gets Friday off."
#define TEST_STRING_SZ 25
typedef struct testVector { typedef struct testVector {
const char* input; const char* input;
const char* output; const char* output;
@@ -12143,7 +12146,7 @@ static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
{ {
int ret; int ret;
word32 sigSz; 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[] = { WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59, 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe, 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]; byte digest[WC_MAX_DIGEST_SIZE];
int ret = 0; int ret = 0;
const char inStr[] = "Everyone gets Friday off."; const char inStr[] = TEST_STRING;
word32 inLen = (word32)(sizeof(inStr)-1); /* do not include NULL term */ word32 inLen = (word32)TEST_STRING_SZ;
word32 outSz; word32 outSz;
word32 plainSz; word32 plainSz;
word32 digestSz; word32 digestSz;
@@ -12729,7 +12732,7 @@ static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
#endif #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(out, byte, RSA_TEST_BYTES, HEAP_HINT);
DECLARE_VAR(sig, 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 #endif
#if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \ #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
!defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
const char inStr[] = "Everyone gets Friday off."; const char inStr[] = TEST_STRING;
const word32 inLen = (word32)(sizeof(inStr)-1); /* do not include NULL */ const word32 inLen = (word32)TEST_STRING_SZ;
const word32 outSz = RSA_TEST_BYTES; const word32 outSz = RSA_TEST_BYTES;
const word32 plainSz = RSA_TEST_BYTES; const word32 plainSz = RSA_TEST_BYTES;
#endif #endif
@@ -14085,7 +14088,7 @@ WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
#if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \ #if (!defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_PUBLIC_MP)) && \
!defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) !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(out, byte, RSA_TEST_BYTES, HEAP_HINT);
DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT); DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
#endif #endif
@@ -18278,7 +18281,7 @@ WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
EVP_PKEY_CTX *enc = NULL; EVP_PKEY_CTX *enc = NULL;
EVP_PKEY_CTX *dec = NULL; EVP_PKEY_CTX *dec = NULL;
byte in[] = "Everyone gets Friday off."; byte in[] = TEST_STRING;
byte out[256]; byte out[256];
size_t outlen; size_t outlen;
size_t keySz; size_t keySz;
@@ -21096,7 +21099,7 @@ static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
word32 sigSz; word32 sigSz;
int size; int size;
byte out[ECC_MAX_SIG_SIZE]; byte out[ECC_MAX_SIG_SIZE];
byte in[] = "Everyone gets Friday off."; byte in[] = TEST_STRING;
WOLFSSL_SMALL_STACK_STATIC const byte hash[] = { WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59, 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe, 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,