From 34a4f1fae0bc2f4d61e6508b9f511488a8634dcd Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 31 Mar 2017 13:11:23 -0700 Subject: [PATCH 1/2] Move wolfCrypt test/benchmark to move static memory pool to global (not in stack). Fix wolfCrypt test wc_InitRng to use _ex with HEAP_HINT (when not FIPS). Added ability to use HAVE_STACK_SIZE with wolfCrypt test and benchmark. Cleanup of the benchmark_test function main wrapper. --- examples/client/client.c | 2 +- testsuite/testsuite.c | 2 +- wolfcrypt/benchmark/benchmark.c | 109 ++++++++++++++++++++------------ wolfcrypt/benchmark/benchmark.h | 6 +- wolfcrypt/src/wolfmath.c | 4 +- wolfcrypt/test/test.c | 106 ++++++++++++++++++++++++++----- wolfcrypt/test/test.h | 4 ++ wolfssl/test.h | 7 +- wolfssl/wolfcrypt/types.h | 2 + 9 files changed, 180 insertions(+), 62 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index 6774ad08a..99a6fe1af 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -166,7 +166,7 @@ static int ClientBenchmarkConnections(WOLFSSL_CTX* ctx, char* host, word16 port, /* time passed in number of connects give average */ int times = benchmark; int loops = resumeSession ? 2 : 1; - int i = 0; + int i = 0; #ifndef NO_SESSION_CACHE WOLFSSL_SESSION* benchSession = NULL; #endif diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index 60080da5f..144efb140 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -210,7 +210,7 @@ int testsuite_test(int argc, char** argv) #endif /* HAVE_WNR */ printf("\nAll tests passed!\n"); - EXIT_TEST(EXIT_SUCCESS); + return EXIT_SUCCESS; } void simple_test(func_args* args) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index a4f1e6877..794ad6392 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -32,6 +32,10 @@ /* Macro to disable benchmark */ #ifndef NO_CRYPT_BENCHMARK +#ifdef XMALLOC_USER + #include /* we're using malloc / free direct here */ +#endif + #ifdef WOLFSSL_STATIC_MEMORY #include static WOLFSSL_HEAP_HINT* HEAP_HINT; @@ -93,6 +97,12 @@ #include #include +/* only for stack size check */ +#ifdef HAVE_STACK_SIZE + #include + #include +#endif + #ifdef WOLFSSL_ASYNC_CRYPT #include #endif @@ -158,7 +168,7 @@ #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) \ || !defined(NO_DH) /* include test cert and key buffers for use with NO_FILESYSTEM */ - #include + #include #endif @@ -178,6 +188,7 @@ #include "wolfssl/wolfcrypt/mem_track.h" #endif + void bench_des(void); void bench_idea(void); void bench_arc4(void); @@ -235,7 +246,7 @@ void bench_rng(void); #ifdef WOLFSSL_CURRTIME_REMAP #define current_time WOLFSSL_CURRTIME_REMAP -#else +#elif !defined(HAVE_STACK_SIZE) double current_time(int); #endif @@ -282,28 +293,24 @@ static const XGEN_ALIGN byte iv[] = }; -/* so embedded projects can pull in tests on their own */ -#if !defined(NO_MAIN_DRIVER) - -int main(int argc, char** argv) - -{ - (void)argc; - (void)argv; -#else -int benchmark_test(void *args) -{ - (void)args; -#endif - #ifdef WOLFSSL_STATIC_MEMORY #ifdef BENCH_EMBEDDED - byte memory[50000]; + static byte gBenchMemory[50000]; #else - byte memory[400000]; + static byte gBenchMemory[400000]; #endif +#endif - if (wc_LoadStaticMemory(&HEAP_HINT, memory, sizeof(memory), +#ifdef HAVE_STACK_SIZE +THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args) +#else +int benchmark_test(void *args) +#endif +{ + (void)args; + +#ifdef WOLFSSL_STATIC_MEMORY + if (wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory, sizeof(gBenchMemory), WOLFMEM_GENERAL, 1) != 0) { printf("unable to load static memory"); exit(EXIT_FAILURE); @@ -314,7 +321,6 @@ int benchmark_test(void *args) InitMemoryTracker(); #endif - wolfCrypt_Init(); INIT_CYCLE_COUNTER #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) @@ -333,13 +339,6 @@ int benchmark_test(void *args) } #endif /* WOLFSSL_ASYNC_CRYPT */ -#ifdef HAVE_WNR - if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) { - printf("Whitewood netRandom config init failed\n"); - exit(-1); - } -#endif /* HAVE_WNR */ - #if defined(HAVE_LOCAL_RNG) { int rngRet; @@ -351,7 +350,7 @@ int benchmark_test(void *args) #endif if (rngRet < 0) { printf("InitRNG failed\n"); - return rngRet; + EXIT_TEST(rngRet); } } #endif @@ -495,17 +494,6 @@ int benchmark_test(void *args) wolfAsync_DevClose(&devId); #endif -#ifdef HAVE_WNR - if (wc_FreeNetRandom() < 0) { - printf("Failed to free netRandom context\n"); - exit(-1); - } -#endif - - if (wolfCrypt_Cleanup() != 0) { - printf("error with wolfCrypt_Cleanup\n"); - } - #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) ShowMemoryTracker(); #endif @@ -514,6 +502,46 @@ int benchmark_test(void *args) } +#ifndef NO_MAIN_DRIVER +int main(int argc, char** argv) +{ + int ret; + + (void)argc; + (void)argv; + +#ifdef HAVE_WNR + if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) { + printf("Whitewood netRandom config init failed\n"); + exit(-1); + } +#endif /* HAVE_WNR */ + + wolfCrypt_Init(); + +#ifdef HAVE_STACK_SIZE + ret = StackSizeCheck(NULL, benchmark_test); +#else + ret = benchmark_test(NULL); +#endif + + if (wolfCrypt_Cleanup() != 0) { + printf("Error with wolfCrypt_Cleanup!\n"); + exit(-1); + } + +#ifdef HAVE_WNR + if (wc_FreeNetRandom() < 0) { + printf("Failed to free netRandom context\n"); + exit(-1); + } +#endif /* HAVE_WNR */ + + return ret; +} +#endif /* NO_MAIN_DRIVER */ + + #ifdef BENCH_EMBEDDED enum BenchmarkBounds { numBlocks = 25, /* how many kB to test (en/de)cryption */ @@ -2547,7 +2575,7 @@ void bench_ed25519KeySign(void) } #endif /* HAVE_ED25519 */ - +#ifndef HAVE_STACK_SIZE #if defined(_WIN32) && !defined(INTIME_RTOS) #define WIN32_LEAN_AND_MEAN @@ -2659,6 +2687,7 @@ void bench_ed25519KeySign(void) } #endif /* _WIN32 */ +#endif /* !HAVE_STACK_SIZE */ #if defined(HAVE_GET_CYCLES) diff --git a/wolfcrypt/benchmark/benchmark.h b/wolfcrypt/benchmark/benchmark.h index 20feeb45d..4c67bd12d 100644 --- a/wolfcrypt/benchmark/benchmark.h +++ b/wolfcrypt/benchmark/benchmark.h @@ -28,7 +28,11 @@ extern "C" { #endif -int benchmark_test(void* args); +#ifdef HAVE_STACK_SIZE +THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args); +#else +int benchmark_test(void *args); +#endif #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfcrypt/src/wolfmath.c b/wolfcrypt/src/wolfmath.c index 462e89fe0..df80a8406 100644 --- a/wolfcrypt/src/wolfmath.c +++ b/wolfcrypt/src/wolfmath.c @@ -61,6 +61,7 @@ int get_rand_digit(WC_RNG* rng, mp_digit* d) return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit)); } +#ifdef WC_RSA_BLINDING int mp_rand(mp_int* a, int digits, WC_RNG* rng) { int ret; @@ -103,5 +104,6 @@ int mp_rand(mp_int* a, int digits, WC_RNG* rng) return ret; } +#endif /* WC_RSA_BLINDING */ -#endif +#endif /* USE_FAST_MATH || !NO_BIG_INT */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 654742bfa..da7476a19 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -110,6 +110,14 @@ #include #endif +/* only for stack size check */ +#ifdef HAVE_STACK_SIZE + #include + #define err_sys err_sys_remap /* remap err_sys */ + #include + #undef err_sys +#endif + #ifdef _MSC_VER /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ #pragma warning(disable: 4996) @@ -295,22 +303,25 @@ int memcb_test(void); #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; } - +#ifdef HAVE_STACK_SIZE +static THREAD_RETURN err_sys(const char* msg, int es) +#else static int err_sys(const char* msg, int es) - +#endif { printf("%s error = %d\n", msg, es); EXIT_TEST(-1); } -/* func_args from test.h, so don't have to pull in other junk */ +#ifndef HAVE_STACK_SIZE +/* func_args from test.h, so don't have to pull in other stuff */ typedef struct func_args { int argc; char** argv; int return_code; } func_args; - +#endif /* !HAVE_STACK_SIZE */ #ifdef HAVE_FIPS @@ -328,21 +339,26 @@ static void myFipsCb(int ok, int err, const char* hash) #endif /* HAVE_FIPS */ -int wolfcrypt_test(void* args) -{ - int ret = 0; #ifdef WOLFSSL_STATIC_MEMORY #ifdef BENCH_EMBEDDED - byte memory[10000]; + static byte gTestMemory[10000]; #else - byte memory[100000]; + static byte gTestMemory[100000]; #endif #endif +#ifdef HAVE_STACK_SIZE +THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args) +#else +int wolfcrypt_test(void* args) +#endif +{ + int ret; + ((func_args*)args)->return_code = -1; /* error state */ #ifdef WOLFSSL_STATIC_MEMORY - if (wc_LoadStaticMemory(&HEAP_HINT, memory, sizeof(memory), + if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory), WOLFMEM_GENERAL, 1) != 0) { printf("unable to load static memory"); exit(EXIT_FAILURE); @@ -821,7 +837,7 @@ int wolfcrypt_test(void* args) ((func_args*)args)->return_code = ret; - return ret; + EXIT_TEST(ret); } @@ -845,10 +861,14 @@ int wolfcrypt_test(void* args) wolfCrypt_Init(); + #ifdef HAVE_STACK_SIZE + StackSizeCheck(&args, wolfcrypt_test); + #else wolfcrypt_test(&args); + #endif if (wolfCrypt_Cleanup() != 0) { - return err_sys("Error with wolfCrypt_Cleanup!\n", -1239); + err_sys("Error with wolfCrypt_Cleanup!\n", -1239); } #ifdef HAVE_WNR @@ -856,7 +876,7 @@ int wolfcrypt_test(void* args) err_sys("Failed to free netRandom context", -1238); #endif /* HAVE_WNR */ - EXIT_TEST(args.return_code); + return args.return_code; } #endif /* NO_MAIN_DRIVER */ @@ -4937,7 +4957,11 @@ int idea_test(void) rnd[1000], enc[1000], dec[1000]; /* random values */ + #ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); + #else ret = wc_InitRng(&rng); + #endif if (ret != 0) return -39; @@ -5011,7 +5035,11 @@ static int random_rng_test(void) byte block[32]; int ret, i; +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) return -39; XMEMSET(block, 0, sizeof(block)); @@ -6144,7 +6172,12 @@ int rsa_test(void) XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER); return -41; } + +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) { XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER); return -42; @@ -7991,7 +8024,11 @@ int dh_test(void) return -52; #endif +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) return -53; @@ -8063,7 +8100,11 @@ int dsa_test(void) ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes); if (ret != 0) return -61; +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) return -62; ret = wc_DsaSign(hash, signature, &key, &rng); @@ -10651,7 +10692,11 @@ int ecc_test(void) return ret; #endif +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) return -1001; @@ -10779,7 +10824,11 @@ int ecc_encrypt_test(void) word32 plainSz = sizeof(plain); int i; +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) return -3001; @@ -10930,7 +10979,11 @@ int ecc_test_buffers() { if (ret != 0) return -41; +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) return -42; @@ -10990,6 +11043,7 @@ int ecc_test_buffers() { int curve25519_test(void) { WC_RNG rng; + int ret; #ifdef HAVE_CURVE25519_SHARED_SECRET byte sharedA[32]; byte sharedB[32]; @@ -11047,7 +11101,12 @@ int curve25519_test(void) }; #endif /* HAVE_CURVE25519_SHARED_SECRET */ - if (wc_InitRng(&rng) != 0) +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else + ret = wc_InitRng(&rng); +#endif + if (ret != 0) return -1001; wc_curve25519_init(&userA); @@ -11175,7 +11234,7 @@ int ed25519_test(void) byte exportSKey[ED25519_KEY_SIZE]; word32 exportPSz; word32 exportSSz; - int i; + int i, ret; word32 outlen; #ifdef HAVE_ED25519_VERIFY int verify; @@ -11502,7 +11561,14 @@ int ed25519_test(void) #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */ /* create ed25519 keys */ - wc_InitRng(&rng); +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else + ret = wc_InitRng(&rng); +#endif + if (ret != 0) + return -1020; + wc_ed25519_init(&key); wc_ed25519_init(&key2); wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key); @@ -12508,7 +12574,11 @@ int pkcs7signed_test(void) fclose(file); #endif /* USE_CERT_BUFFER_ */ +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) { XFREE(certDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(keyDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -12682,7 +12752,11 @@ int mp_test() mp_init_copy(&p, &a); +#ifndef HAVE_FIPS + ret = wc_InitRng_ex(&rng, HEAP_HINT); +#else ret = wc_InitRng(&rng); +#endif if (ret != 0) goto done; diff --git a/wolfcrypt/test/test.h b/wolfcrypt/test/test.h index 47d8b74c2..d65b27f4f 100644 --- a/wolfcrypt/test/test.h +++ b/wolfcrypt/test/test.h @@ -28,7 +28,11 @@ extern "C" { #endif +#ifdef HAVE_STACK_SIZE +THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args); +#else int wolfcrypt_test(void* args); +#endif #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfssl/test.h b/wolfssl/test.h index d47300a3d..0241bf39a 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -1339,9 +1339,10 @@ static INLINE void CaCb(unsigned char* der, int sz, int type) typedef THREAD_RETURN WOLFSSL_THREAD (*thread_func)(void* args); -static INLINE void StackSizeCheck(func_args* args, thread_func tf) +static INLINE int StackSizeCheck(func_args* args, thread_func tf) { int ret, i, used; + void* status; unsigned char* myStack = NULL; int stackSize = 1024*128; pthread_attr_t myAttr; @@ -1372,7 +1373,7 @@ static INLINE void StackSizeCheck(func_args* args, thread_func tf) exit(EXIT_FAILURE); } - ret = pthread_join(threadId, NULL); + ret = pthread_join(threadId, &status); if (ret != 0) err_sys("pthread_join failed"); @@ -1384,6 +1385,8 @@ static INLINE void StackSizeCheck(func_args* args, thread_func tf) used = stackSize - i; printf("stack used = %d\n", used); + + return (int)status; } diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index e86578b87..b82f2779b 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -440,6 +440,8 @@ #ifdef WOLFSSL_RIOT_OS #define EXIT_TEST(ret) exit(ret) + #elif defined(HAVE_STACK_SIZE) + #define EXIT_TEST(ret) return (void*)((long)(ret)) #else #define EXIT_TEST(ret) return ret #endif From 29eabe5535a0f9fc3eeefacbe055c9dc69bb3b34 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 3 Apr 2017 09:41:12 -0700 Subject: [PATCH 2/2] Better stack size check return code handling. --- wolfssl/test.h | 2 +- wolfssl/wolfcrypt/types.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wolfssl/test.h b/wolfssl/test.h index 0241bf39a..db73f406c 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -1386,7 +1386,7 @@ static INLINE int StackSizeCheck(func_args* args, thread_func tf) used = stackSize - i; printf("stack used = %d\n", used); - return (int)status; + return (int)((size_t)status); } diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index b82f2779b..9b1922609 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -441,7 +441,7 @@ #ifdef WOLFSSL_RIOT_OS #define EXIT_TEST(ret) exit(ret) #elif defined(HAVE_STACK_SIZE) - #define EXIT_TEST(ret) return (void*)((long)(ret)) + #define EXIT_TEST(ret) return (void*)((size_t)(ret)) #else #define EXIT_TEST(ret) return ret #endif