From 3f16fba4f879d2d79dc4576b63edb299ab0207e4 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 15 Sep 2017 09:15:50 -0700 Subject: [PATCH 1/4] Fix build warning with possible use of unitialized `ret`. --- wolfcrypt/benchmark/benchmark.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 2de5c16a0..e0d747b06 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -2549,7 +2549,7 @@ void bench_rsaKeyGen(int doAsync) { RsaKey genKey[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times, pending = 0; + int ret = 0, i, count = 0, times, pending = 0; int k, keySz; const int keySizes[2] = {1024, 2048}; const long rsa_e_val = 65537; From a9e540fc07eb775173bcdf1308a74dd38afa220c Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 15 Sep 2017 14:02:06 -0700 Subject: [PATCH 2/4] Moved the bench_ buffer allocation/free into the thread, so the THREAD_LS has a unique one for each which resolves issues with benchmark when using the `USE_QAE_THREAD_LS` option. --- wolfcrypt/benchmark/benchmark.c | 88 +++++++++++++++++---------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index e0d747b06..2fdf7bbaf 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -662,6 +662,8 @@ static INLINE void bench_stats_free(void) static void* benchmarks_do(void* args) { + int bench_buf_size; + #ifdef WOLFSSL_ASYNC_CRYPT #ifndef WC_NO_ASYNC_THREADING ThreadData* threadData = (ThreadData*)args; @@ -694,10 +696,46 @@ static void* benchmarks_do(void* args) #endif if (rngRet < 0) { printf("InitRNG failed\n"); + return NULL; } } #endif + /* setup bench plain, cipher, key and iv globals */ + /* make sure bench buffer is multiple of 16 (AES block size) */ + bench_buf_size = bench_size + BENCH_CIPHER_ADD; + if (bench_buf_size % 16) + bench_buf_size += 16 - (bench_buf_size % 16); + + bench_plain = (byte*)XMALLOC(bench_buf_size, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + bench_cipher = (byte*)XMALLOC(bench_buf_size, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + if (bench_plain == NULL || bench_cipher == NULL) { + XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + + printf("Benchmark block buffer alloc failed!\n"); + return NULL; + } + XMEMSET(bench_plain, 0, bench_buf_size); + XMEMSET(bench_cipher, 0, bench_buf_size); + +#ifdef WOLFSSL_ASYNC_CRYPT + bench_key = (byte*)XMALLOC(sizeof(bench_key_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + if (bench_key == NULL || bench_iv == NULL) { + XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + + printf("Benchmark cipher buffer alloc failed!\n"); + return NULL; + } + XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf)); + XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf)); +#endif + (void)bench_key; + (void)bench_iv; + + #ifndef WC_NO_RNG bench_rng(); #endif /* WC_NO_RNG */ @@ -930,6 +968,14 @@ static void* benchmarks_do(void* args) bench_ed25519KeySign(); #endif + /* free benchmark buffers */ + XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); +#ifdef WOLFSSL_ASYNC_CRYPT + XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); +#endif + #ifdef WOLFSSL_ASYNC_CRYPT /* free event queue */ wolfEventQueue_Free(&eventQueue); @@ -949,7 +995,6 @@ static void* benchmarks_do(void* args) int benchmark_init(void) { int ret = 0; - int block_size; #ifdef WOLFSSL_STATIC_MEMORY ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory, sizeof(gBenchMemory), @@ -980,40 +1025,6 @@ int benchmark_init(void) } #endif /* HAVE_WNR */ - /* make sure bench buffer is multiple of 16 (AES block size) */ - block_size = bench_size + BENCH_CIPHER_ADD; - if (block_size % 16) - block_size += 16 - (block_size % 16); - - /* setup bench plain, cipher, key and iv globals */ - bench_plain = (byte*)XMALLOC(block_size, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - bench_cipher = (byte*)XMALLOC(block_size, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - if (bench_plain == NULL || bench_cipher == NULL) { - XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - - printf("Benchmark block buffer alloc failed!\n"); - return EXIT_FAILURE; - } - XMEMSET(bench_plain, 0, block_size); - XMEMSET(bench_cipher, 0, block_size); - -#ifdef WOLFSSL_ASYNC_CRYPT - bench_key = (byte*)XMALLOC(sizeof(bench_key_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - if (bench_key == NULL || bench_iv == NULL) { - XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - - printf("Benchmark cipher buffer alloc failed!\n"); - return EXIT_FAILURE; - } - XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf)); - XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf)); -#endif - (void)bench_key; - (void)bench_iv; - return ret; } @@ -1021,13 +1032,6 @@ int benchmark_free(void) { int ret; - XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); -#ifdef WOLFSSL_ASYNC_CRYPT - XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); - XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); -#endif - #ifdef HAVE_WNR ret = wc_FreeNetRandom(); if (ret < 0) { From 218f944984b946a69a392386cdf06c7824b4d778 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 22 Sep 2017 11:12:23 -0700 Subject: [PATCH 3/4] Fix for building async with AES XTS. --- wolfcrypt/test/test.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index e9b8b0f23..d5c31e0cf 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -4271,7 +4271,7 @@ static int aes_xts_128_test(void) return -4000; ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4001; @@ -4284,7 +4284,7 @@ static int aes_xts_128_test(void) return -4003; ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4004; @@ -4295,7 +4295,7 @@ static int aes_xts_128_test(void) XMEMSET(cipher, 0, sizeof(cipher)); ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4006; @@ -4308,7 +4308,7 @@ static int aes_xts_128_test(void) return -4007; ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4008; @@ -4319,7 +4319,7 @@ static int aes_xts_128_test(void) XMEMSET(buf, 0, sizeof(buf)); ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4010; @@ -4330,7 +4330,7 @@ static int aes_xts_128_test(void) XMEMSET(buf, 0, sizeof(buf)); ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4012; @@ -4344,7 +4344,7 @@ static int aes_xts_128_test(void) return -4014; ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4015; @@ -4441,7 +4441,7 @@ static int aes_xts_256_test(void) return -4017; ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4018; @@ -4454,7 +4454,7 @@ static int aes_xts_256_test(void) return -4020; ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4021; @@ -4465,7 +4465,7 @@ static int aes_xts_256_test(void) XMEMSET(cipher, 0, sizeof(cipher)); ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4023; @@ -4478,7 +4478,7 @@ static int aes_xts_256_test(void) return -4024; ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4025; @@ -4489,7 +4489,7 @@ static int aes_xts_256_test(void) XMEMSET(buf, 0, sizeof(buf)); ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4027; @@ -4502,7 +4502,7 @@ static int aes_xts_256_test(void) return -4029; ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2)); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4030; @@ -4573,7 +4573,7 @@ static int aes_xts_sector_test(void) return -4032; ret = wc_AesXtsEncryptSector(&aes, buf, p1, sizeof(p1), s1); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4033; @@ -4587,7 +4587,7 @@ static int aes_xts_sector_test(void) return -4035; ret = wc_AesXtsDecryptSector(&aes, buf, c1, sizeof(c1), s1); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4036; @@ -4602,7 +4602,7 @@ static int aes_xts_sector_test(void) return -4038; ret = wc_AesXtsEncryptSector(&aes, buf, p2, sizeof(p2), s2); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4039; @@ -4616,7 +4616,7 @@ static int aes_xts_sector_test(void) return -4041; ret = wc_AesXtsDecryptSector(&aes, buf, c2, sizeof(c2), s2); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) return -4042; @@ -4667,14 +4667,14 @@ static int aes_xts_args_test(void) return -4046; ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret == 0) return -4047; ret = wc_AesXtsEncryptSector(&aes, NULL, p1, sizeof(p1), s1); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret == 0) return -4048; @@ -4686,14 +4686,14 @@ static int aes_xts_args_test(void) return -4046; ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret == 0) return -4049; ret = wc_AesXtsDecryptSector(&aes, NULL, c1, sizeof(c1), s1); #if defined(WOLFSSL_ASYNC_CRYPT) - ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret == 0) return -4050; From eec5f9bb4118d8e4c9bccea004f3cfe721bdf07e Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 22 Sep 2017 11:35:46 -0700 Subject: [PATCH 4/4] Fixes for benchmark after buffers were moved into thread. Needed THREAD_LS_T and fixed benchmark alloc failure cleanup. --- wolfcrypt/benchmark/benchmark.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 2fdf7bbaf..9b934a229 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -407,8 +407,8 @@ static THREAD_LS_T int devId = INVALID_DEVID; #define BENCH_SIZE bench_size /* globals for cipher tests */ -static byte* bench_plain = NULL; -static byte* bench_cipher = NULL; +static THREAD_LS_T byte* bench_plain = NULL; +static THREAD_LS_T byte* bench_cipher = NULL; static const XGEN_ALIGN byte bench_key_buf[] = { @@ -424,8 +424,8 @@ static const XGEN_ALIGN byte bench_iv_buf[] = 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81 }; -static byte* bench_key = (byte*)bench_key_buf; -static byte* bench_iv = (byte*)bench_iv_buf; +static THREAD_LS_T byte* bench_key = NULL; +static THREAD_LS_T byte* bench_iv = NULL; #ifdef WOLFSSL_STATIC_MEMORY #ifdef BENCH_EMBEDDED @@ -712,9 +712,10 @@ static void* benchmarks_do(void* args) if (bench_plain == NULL || bench_cipher == NULL) { XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + bench_plain = bench_cipher = NULL; printf("Benchmark block buffer alloc failed!\n"); - return NULL; + goto exit; } XMEMSET(bench_plain, 0, bench_buf_size); XMEMSET(bench_cipher, 0, bench_buf_size); @@ -725,16 +726,17 @@ static void* benchmarks_do(void* args) if (bench_key == NULL || bench_iv == NULL) { XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); + bench_key = bench_iv = NULL; printf("Benchmark cipher buffer alloc failed!\n"); - return NULL; + goto exit; } XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf)); XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf)); +#else + bench_key = (byte*)bench_key_buf; + bench_iv = (byte*)bench_iv_buf; #endif - (void)bench_key; - (void)bench_iv; - #ifndef WC_NO_RNG bench_rng(); @@ -968,6 +970,7 @@ static void* benchmarks_do(void* args) bench_ed25519KeySign(); #endif +exit: /* free benchmark buffers */ XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT); XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);