From 44a36222489fedaa52af700d342aa7a7a57819a3 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 11 Aug 2017 12:42:33 -0700 Subject: [PATCH] Fixes to better handle threading with async. Fix `wc_CamelliaCbcEncrypt` return code checking. Fix to ensure cycles per byte shows on same line. Refactor of async event state. Refactor to initalize event prior to operation (in case it finishes before adding to queue). Add `HAVE_AES_DECRYPT` to --enable-all option. Cleanup benchmark error display. --- configure.ac | 2 + wolfcrypt/benchmark/benchmark.c | 836 +++++++++++++------------------- wolfcrypt/src/aes.c | 25 +- wolfcrypt/src/des3.c | 10 +- wolfcrypt/src/dh.c | 10 +- wolfcrypt/src/ecc.c | 20 +- wolfcrypt/src/rsa.c | 10 +- wolfcrypt/src/wolfevent.c | 12 +- wolfssl/wolfcrypt/wolfevent.h | 12 +- 9 files changed, 378 insertions(+), 559 deletions(-) diff --git a/configure.ac b/configure.ac index 395d75ddb..f06822354 100644 --- a/configure.ac +++ b/configure.ac @@ -213,6 +213,8 @@ then enable_aeskeywrap=yes enable_x963kdf=yes enable_scrypt=yes + + AM_CFLAGS="-DHAVE_AES_DECRYPT $AM_CFLAGS" fi AM_CONDITIONAL([BUILD_ALL], [test "x$ENABLED_ALL" = "xyes"]) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 9e3ad4200..9a53b743f 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -136,9 +136,9 @@ #define INIT_CYCLE_COUNTER #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles(); #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles; - #define SHOW_INTEL_CYCLES printf(" Cycles per byte = %6.2f", \ - count == 0 ? 0 : \ - (float)total_cycles / ((word64)count*BENCH_SIZE)); + #define SHOW_INTEL_CYCLES(b, n) \ + XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \ + count == 0 ? 0 : (float)total_cycles / ((word64)count*BENCH_SIZE)) #elif defined(LINUX_CYCLE_COUNT) #include #include @@ -161,14 +161,15 @@ total_cycles = total_cycles - begin_cycles; \ } while (0); - #define SHOW_INTEL_CYCLES printf(" Cycles per byte = %6.2f", \ - (float)total_cycles / (count*BENCH_SIZE)); + #define SHOW_INTEL_CYCLES(b, n) \ + XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \ + (float)total_cycles / (count*BENCH_SIZE)) #else #define INIT_CYCLE_COUNTER #define BEGIN_INTEL_CYCLES #define END_INTEL_CYCLES - #define SHOW_INTEL_CYCLES + #define SHOW_INTEL_CYCLES(b, n) b[XSTRLEN(b)] = '\n' #endif /* let's use buffers, we have them */ @@ -218,18 +219,22 @@ static THREAD_LS_T WC_RNG rng; #endif +#if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || defined(HAVE_ECC) || \ + defined(HAVE_ECC) || defined(HAVE_NTRU) || !defined(NO_DH) || \ + !defined(NO_RSA) || defined(HAVE_SCRYPT) + + #define BENCH_ASYM +#endif /* Asynchronous helper macros */ static THREAD_LS_T int devId = INVALID_DEVID; #ifdef WOLFSSL_ASYNC_CRYPT - static THREAD_LS_T WOLF_EVENT_QUEUE eventQueue; - static THREAD_LS_T int asyncPending; + static WOLF_EVENT_QUEUE eventQueue; #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev) #define BENCH_ASYNC_GET_NAME(doAsync) (doAsync) ? "HW" : "SW" - #define BENCH_ASYNC_IS_PEND() (asyncPending > 0) #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING) #ifndef WC_NO_ASYNC_THREADING @@ -240,114 +245,116 @@ static THREAD_LS_T int devId = INVALID_DEVID; static int g_threadCount; #endif - static INLINE int bench_async_begin(void) { - /* init event queue */ - asyncPending = 0; - return wolfEventQueue_Init(&eventQueue); - } - - static INLINE void bench_async_end(void) { - /* free event queue */ - wolfEventQueue_Free(&eventQueue); - } - - static INLINE void bench_async_complete(int* ret, WC_ASYNC_DEV* asyncDev, - int* times) - { - *ret = asyncDev->event.ret; - if (*ret >= 0) { - (*times)++; - asyncDev->event.done = 0; /* reset done flag */ - } - } - - static INLINE int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev, - int callAgain, int* times, int limit) + static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev, + int callAgain, int* times, int limit, int* pending) { int allowNext = 0; + /* this state can be set from a different thread */ + WOLF_EVENT_STATE state = asyncDev->event.state; + /* if algo doesn't require calling again then use this flow */ - if (!callAgain) { - if (asyncDev->event.done) { - /* operation completed */ - bench_async_complete(ret, asyncDev, times); - } - } - /* if algo does require calling again then use this flow */ - else { - if (asyncDev->event.done) { + if (state == WOLF_EVENT_STATE_DONE) { + if (callAgain) { + /* needs called again, so allow it and handle completion in bench_async_handle */ allowNext = 1; } + else { + *ret = asyncDev->event.ret; + asyncDev->event.state = WOLF_EVENT_STATE_READY; + (*times)++; + if (*pending > 0) /* to support case where async blocks */ + (*pending)--; + + if ((*times + *pending) < limit) + allowNext = 1; + } } - if (asyncDev->event.pending == 0 && - (*times + asyncPending) < limit) { + /* if slot is available and we haven't reached limit, start another */ + else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) { allowNext = 1; } return allowNext; } - static INLINE int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev, - int callAgain, int* times) + static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev, + int callAgain, int* times, int* pending) { + WOLF_EVENT_STATE state = asyncDev->event.state; + if (*ret == WC_PENDING_E) { - *ret = wc_AsyncHandle(asyncDev, &eventQueue, - callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE); - if (*ret == 0) - asyncPending++; + if (state == WOLF_EVENT_STATE_DONE) { + *ret = asyncDev->event.ret; + asyncDev->event.state = WOLF_EVENT_STATE_READY; + (*times)++; + (*pending)--; + } + else { + (*pending)++; + *ret = wc_AsyncHandle(asyncDev, &eventQueue, + callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE); + } } else if (*ret >= 0) { - /* operation completed */ - bench_async_complete(ret, asyncDev, times); + *ret = asyncDev->event.ret; + asyncDev->event.state = WOLF_EVENT_STATE_READY; + (*times)++; + if (*pending > 0) /* to support case where async blocks */ + (*pending)--; } return (*ret >= 0) ? 1 : 0; } - static INLINE void bench_async_poll(void) + static INLINE int bench_async_poll(int* pending) { - /* poll until there are events done */ - if (asyncPending > 0) { - int ret, asyncDone = 0; - do { - ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0, + int ret, asyncDone = 0; + + ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0, WOLF_POLL_FLAG_CHECK_HW, &asyncDone); - if (ret != 0) { - printf("Async poll failed %d\n", ret); - return; - } - } while (asyncDone == 0); - asyncPending -= asyncDone; + if (ret != 0) { + printf("Async poll failed %d\n", ret); + return ret; } + + if (asyncDone == 0) { + #ifndef WC_NO_ASYNC_THREADING + /* give time to other threads */ + wc_AsyncThreadYield(); + #endif + } + + (void)pending; + + return asyncDone; } #else #define BENCH_MAX_PENDING (1) #define BENCH_ASYNC_GET_NAME(doAsync) "" #define BENCH_ASYNC_GET_DEV(obj) NULL - #define BENCH_ASYNC_IS_PEND() (0) - - #define bench_async_begin() - #define bench_async_end() (void)doAsync; static INLINE int bench_async_check(int* ret, void* asyncDev, - int callAgain, int* times, int limit) + int callAgain, int* times, int limit, int* pending) { (void)ret; (void)asyncDev; (void)callAgain; (void)times; (void)limit; + (void)pending; return 1; } static INLINE int bench_async_handle(int* ret, void* asyncDev, - int callAgain, int* times) + int callAgain, int* times, int* pending) { (void)asyncDev; (void)callAgain; + (void)pending; if (*ret >= 0) { /* operation completed */ @@ -356,7 +363,7 @@ static THREAD_LS_T int devId = INVALID_DEVID; } return 0; } - #define bench_async_poll() + #define bench_async_poll(p) #endif /* WOLFSSL_ASYNC_CRYPT */ @@ -447,6 +454,7 @@ static byte* bench_iv = (byte*)bench_iv_buf; int doAsync; int finishCount; bench_stat_type_t type; + int lastRet; } bench_stats_t; static bench_stats_t* bench_stats_head; static bench_stats_t* bench_stats_tail; @@ -454,10 +462,11 @@ static byte* bench_iv = (byte*)bench_iv_buf; static bench_stats_t* bench_stats_add(bench_stat_type_t type, const char* algo, int strength, const char* desc, int doAsync, - double perfsec) + double perfsec, int ret) { bench_stats_t* stat; + /* protect bench_stats_head and bench_stats_tail access */ pthread_mutex_lock(&bench_lock); /* locate existing in list */ @@ -496,6 +505,8 @@ static byte* bench_iv = (byte*)bench_iv_buf; stat->doAsync = doAsync; stat->perfsec += perfsec; stat->finishCount++; + if (stat->lastRet > ret) + stat->lastRet = ret; /* track last error */ if (stat->finishCount == g_threadCount) { isLast = 1; @@ -511,7 +522,7 @@ static byte* bench_iv = (byte*)bench_iv_buf; /* print final stat */ if (isLast) { if (stat->type == BENCH_STAT_SYM) { - printf("%-8s%s %8.3f MB/s\n", stat->desc, + printf("%-12s%s %8.3f MB/s\n", stat->desc, BENCH_ASYNC_GET_NAME(stat->doAsync), stat->perfsec); } else { @@ -550,10 +561,11 @@ static INLINE int bench_stats_sym_check(double start) return ((current_time(0) - start) < BENCH_MIN_RUNTIME_SEC); } -static void bench_stats_sym_finish(const char* desc, int doAsync, int count, double start) +static void bench_stats_sym_finish(const char* desc, int doAsync, int count, double start, int ret) { double total, persec = 0, blocks = count; const char* blockType; + char msg[128] = {0}; END_INTEL_CYCLES total = current_time(0) - start; @@ -579,26 +591,29 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count, dou persec = (1 / total) * blocks; } - printf("%-12s%s %5.0f %s took %5.3f seconds, %8.3f %s/s", + XSNPRINTF(msg, sizeof(msg), "%-12s%s %5.0f %s took %5.3f seconds, %8.3f %s/s", desc, BENCH_ASYNC_GET_NAME(doAsync), blocks, blockType, total, persec, blockType); - SHOW_INTEL_CYCLES - printf("\n"); + SHOW_INTEL_CYCLES(msg, sizeof(msg)); + printf("%s", msg); + + /* show errors */ + if (ret < 0) { + printf("Benchmark %s failed: %d\n", desc, ret); + } #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING) /* Add to thread stats */ - bench_stats_add(BENCH_STAT_SYM, NULL, 0, desc, doAsync, persec); + bench_stats_add(BENCH_STAT_SYM, NULL, 0, desc, doAsync, persec, ret); #endif + (void)doAsync; + (void)ret; } -/* declare here rather than creating a static function to avoid warning of not - * used in the case of something like a leanpsk only build */ -void bench_stats_asym_finish(const char* algo, int strength, - const char* desc, int doAsync, int count, double start); - -void bench_stats_asym_finish(const char* algo, int strength, - const char* desc, int doAsync, int count, double start) +#ifdef BENCH_ASYM +static void bench_stats_asym_finish(const char* algo, int strength, + const char* desc, int doAsync, int count, double start, int ret) { double total, each = 0, opsSec, milliEach; @@ -611,13 +626,21 @@ void bench_stats_asym_finish(const char* algo, int strength, printf("%-5s %4d %-9s %s %6d ops took %5.3f sec, avg %5.3f ms," " %.3f ops/sec\n", algo, strength, desc, BENCH_ASYNC_GET_NAME(doAsync), count, total, milliEach, opsSec); - (void)doAsync; + + /* show errors */ + if (ret < 0) { + printf("Benchmark %s %s %d failed: %d\n", algo, desc, strength, ret); + } #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING) /* Add to thread stats */ - bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, doAsync, opsSec); + bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, doAsync, opsSec, ret); #endif + + (void)doAsync; + (void)ret; } +#endif /* BENCH_ASYM */ static INLINE void bench_stats_free(void) { @@ -654,6 +677,12 @@ static void* benchmarks_do(void* args) (void)args; +#ifdef WOLFSSL_ASYNC_CRYPT + if (wolfEventQueue_Init(&eventQueue) != 0) { + printf("Async event queue init failure!\n"); + } +#endif + #if defined(HAVE_LOCAL_RNG) { int rngRet; @@ -898,6 +927,11 @@ static void* benchmarks_do(void* args) bench_ed25519KeySign(); #endif +#ifdef WOLFSSL_ASYNC_CRYPT + /* free event queue */ + wolfEventQueue_Free(&eventQueue); +#endif + #if defined(HAVE_LOCAL_RNG) wc_FreeRng(&rng); #endif @@ -1103,11 +1137,7 @@ void bench_rng(void) count += i; } while (bench_stats_sym_check(start)); exit_rng: - bench_stats_sym_finish("RNG", 0, count, start); - - if (ret < 0) { - printf("wc_RNG_GenerateBlock failed %d\n", ret); - } + bench_stats_sym_finish("RNG", 0, count, start, ret); wc_FreeRng(&myrng); } @@ -1119,12 +1149,10 @@ exit_rng: #ifdef HAVE_AES_CBC void bench_aescbc(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); @@ -1145,15 +1173,15 @@ void bench_aescbc(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesCbcEncrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_enc; } } @@ -1162,7 +1190,7 @@ void bench_aescbc(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_aes_enc: - bench_stats_sym_finish("AES-Enc", doAsync, count, start); + bench_stats_sym_finish("AES-Enc", doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -1180,15 +1208,15 @@ exit_aes_enc: bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesCbcDecrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_dec; } } @@ -1197,36 +1225,28 @@ exit_aes_enc: count += times; } while (bench_stats_sym_check(start)); exit_aes_dec: - bench_stats_sym_finish("AES-Dec", doAsync, count, start); + bench_stats_sym_finish("AES-Dec", doAsync, count, start, ret); #endif /* HAVE_AES_DECRYPT */ exit: - if (ret < 0) { - printf("bench_aescbc failed: %d\n", ret); - } - for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_AesFree(&enc[i]); } - - bench_async_end(); } #endif /* HAVE_AES_CBC */ #ifdef HAVE_AESGCM void bench_aesgcm(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; double start; DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT); DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); #ifdef WOLFSSL_ASYNC_CRYPT @@ -1256,17 +1276,17 @@ void bench_aesgcm(int doAsync) /* GCM uses same routine in backend for both encrypt and decrypt */ bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesGcmEncrypt(&enc[i], bench_cipher, bench_plain, BENCH_SIZE, bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ, bench_additional, AES_AUTH_ADD_SZ); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_gcm; } } @@ -1275,22 +1295,22 @@ void bench_aesgcm(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_aes_gcm: - bench_stats_sym_finish("AES-GCM-Enc", doAsync, count, start); + bench_stats_sym_finish("AES-GCM-Enc", doAsync, count, start, ret); /* GCM uses same routine in backend for both encrypt and decrypt */ bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_AesGcmDecrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE, bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ, bench_additional, AES_AUTH_ADD_SZ); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_gcm_dec; } } @@ -1299,7 +1319,7 @@ exit_aes_gcm: count += times; } while (bench_stats_sym_check(start)); exit_aes_gcm_dec: - bench_stats_sym_finish("AES-GCM-Dec", doAsync, count, start); + bench_stats_sym_finish("AES-GCM-Dec", doAsync, count, start, ret); exit: @@ -1313,8 +1333,6 @@ exit: FREE_VAR(bench_additional, HEAP_HINT); FREE_VAR(bench_tag, HEAP_HINT); - - bench_async_end(); } #endif /* HAVE_AESGCM */ @@ -1338,7 +1356,7 @@ void bench_aesctr(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CTR", 0, count, start); + bench_stats_sym_finish("AES-CTR", 0, count, start, ret); } #endif /* WOLFSSL_AES_COUNTER */ @@ -1367,7 +1385,7 @@ void bench_aesccm(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CCM", 0, count, start); + bench_stats_sym_finish("AES-CCM", 0, count, start, ret); FREE_VAR(bench_additional, HEAP_HINT); FREE_VAR(bench_tag, HEAP_HINT); @@ -1402,7 +1420,7 @@ void bench_poly1305() wc_Poly1305Final(&enc, mac); count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("POLY1305", 0, count, start); + bench_stats_sym_finish("POLY1305", 0, count, start, ret); } #endif /* HAVE_POLY1305 */ @@ -1425,14 +1443,14 @@ void bench_camellia(void) for (i = 0; i < numBlocks; i++) { ret = wc_CamelliaCbcEncrypt(&cam, bench_plain, bench_cipher, BENCH_SIZE); - if (ret <= 0) { + if (ret < 0) { printf("CamelliaCbcEncrypt failed: %d\n", ret); return; } } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("Camellia", 0, count, start); + bench_stats_sym_finish("Camellia", 0, count, start, ret); } #endif @@ -1440,12 +1458,10 @@ void bench_camellia(void) #ifndef NO_DES3 void bench_des(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Des3 enc[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); @@ -1466,15 +1482,15 @@ void bench_des(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_Des3_CbcEncrypt(&enc[i], bench_plain, bench_cipher, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_3des; } } @@ -1483,19 +1499,13 @@ void bench_des(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_3des: - bench_stats_sym_finish("3DES", doAsync, count, start); + bench_stats_sym_finish("3DES", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_des failed: %d\n", ret); - } - for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Des3Free(&enc[i]); } - - bench_async_end(); } #endif /* !NO_DES3 */ @@ -1521,7 +1531,7 @@ void bench_idea(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("IDEA", 0, count, start); + bench_stats_sym_finish("IDEA", 0, count, start, ret); } #endif /* HAVE_IDEA */ @@ -1529,12 +1539,10 @@ void bench_idea(void) #ifndef NO_RC4 void bench_arc4(int doAsync) { - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; Arc4 enc[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); @@ -1555,15 +1563,15 @@ void bench_arc4(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, numBlocks, &pending)) { ret = wc_Arc4Process(&enc[i], bench_cipher, bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_arc4; } } @@ -1572,19 +1580,13 @@ void bench_arc4(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_arc4: - bench_stats_sym_finish("ARC4", doAsync, count, start); + bench_stats_sym_finish("ARC4", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_arc4 failed: %d\n", ret); - } - for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Arc4Free(&enc[i]); } - - bench_async_end(); } #endif /* !NO_RC4 */ @@ -1605,7 +1607,7 @@ void bench_hc128(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("HC128", 0, count, start); + bench_stats_sym_finish("HC128", 0, count, start, 0); } #endif /* HAVE_HC128 */ @@ -1626,7 +1628,7 @@ void bench_rabbit(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("RABBIT", 0, count, start); + bench_stats_sym_finish("RABBIT", 0, count, start, 0); } #endif /* NO_RABBIT */ @@ -1648,7 +1650,7 @@ void bench_chacha(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("CHACHA", 0, count, start); + bench_stats_sym_finish("CHACHA", 0, count, start, 0); } #endif /* HAVE_CHACHA*/ @@ -1673,7 +1675,7 @@ void bench_chacha20_poly1305_aead(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("CHA-POLY", 0, count, start); + bench_stats_sym_finish("CHA-POLY", 0, count, start, ret); } #endif /* HAVE_CHACHA && HAVE_POLY1305 */ @@ -1683,11 +1685,9 @@ void bench_md5(int doAsync) { Md5 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, MD5_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1703,15 +1703,15 @@ void bench_md5(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Md5Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_md5; } } @@ -1721,27 +1721,23 @@ void bench_md5(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Md5Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_md5; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_md5: - bench_stats_sym_finish("MD5", doAsync, count, start); + bench_stats_sym_finish("MD5", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_md5 failed: %d\n", ret); - } - #ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Md5Free(&hash[i]); @@ -1749,8 +1745,6 @@ exit: #endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* !NO_MD5 */ @@ -1760,11 +1754,9 @@ void bench_sha(int doAsync) { Sha hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1780,15 +1772,15 @@ void bench_sha(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_ShaUpdate(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha; } } @@ -1798,36 +1790,28 @@ void bench_sha(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_ShaFinal(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha: - bench_stats_sym_finish("SHA", doAsync, count, start); + bench_stats_sym_finish("SHA", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ShaFree(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* NO_SHA */ @@ -1837,11 +1821,9 @@ void bench_sha224(int doAsync) { Sha224 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA224_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1857,15 +1839,15 @@ void bench_sha224(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha224Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha224; } } @@ -1875,35 +1857,27 @@ void bench_sha224(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha224Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha224; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha224: - bench_stats_sym_finish("SHA-224", doAsync, count, start); + bench_stats_sym_finish("SHA-224", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha224 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha224Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -1912,11 +1886,9 @@ void bench_sha256(int doAsync) { Sha256 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA256_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -1932,15 +1904,15 @@ void bench_sha256(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha256Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha256; } } @@ -1950,35 +1922,27 @@ void bench_sha256(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha256Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha256; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha256: - bench_stats_sym_finish("SHA-256", doAsync, count, start); + bench_stats_sym_finish("SHA-256", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha256 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha256Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -1987,11 +1951,9 @@ void bench_sha384(int doAsync) { Sha384 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA384_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2007,15 +1969,15 @@ void bench_sha384(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha384Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha384; } } @@ -2025,35 +1987,27 @@ void bench_sha384(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha384Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha384; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha384: - bench_stats_sym_finish("SHA-384", doAsync, count, start); + bench_stats_sym_finish("SHA-384", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha384 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha384Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -2062,11 +2016,9 @@ void bench_sha512(int doAsync) { Sha512 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA512_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2082,15 +2034,15 @@ void bench_sha512(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha512Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha512; } } @@ -2100,35 +2052,27 @@ void bench_sha512(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha512Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha512; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha512: - bench_stats_sym_finish("SHA-512", doAsync, count, start); + bench_stats_sym_finish("SHA-512", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha512 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha512Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif @@ -2139,11 +2083,9 @@ void bench_sha3_224(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_224_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2159,15 +2101,15 @@ void bench_sha3_224(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_224_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_224; } } @@ -2177,35 +2119,27 @@ void bench_sha3_224(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_224_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_224; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_224: - bench_stats_sym_finish("SHA3-224", doAsync, count, start); + bench_stats_sym_finish("SHA3-224", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_224 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_224_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_224 */ @@ -2214,11 +2148,9 @@ void bench_sha3_256(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_256_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2234,15 +2166,15 @@ void bench_sha3_256(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_256_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_256; } } @@ -2252,35 +2184,27 @@ void bench_sha3_256(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_256_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_256; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_256: - bench_stats_sym_finish("SHA3-256", doAsync, count, start); + bench_stats_sym_finish("SHA3-256", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_256 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_256_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_256 */ @@ -2289,11 +2213,9 @@ void bench_sha3_384(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_384_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2309,15 +2231,15 @@ void bench_sha3_384(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_384_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_384; } } @@ -2327,35 +2249,27 @@ void bench_sha3_384(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_384_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_384; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_384: - bench_stats_sym_finish("SHA3-384", doAsync, count, start); + bench_stats_sym_finish("SHA3-384", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_384 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_384_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_384 */ @@ -2364,11 +2278,9 @@ void bench_sha3_512(int doAsync) { Sha3 hash[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, SHA3_512_DIGEST_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(hash, 0, sizeof(hash)); @@ -2384,15 +2296,15 @@ void bench_sha3_512(int doAsync) bench_stats_start(&count, &start); do { - for (times = 0; times < numBlocks || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < numBlocks || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_512_Update(&hash[i], bench_plain, BENCH_SIZE); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_512; } } @@ -2402,35 +2314,27 @@ void bench_sha3_512(int doAsync) times = 0; do { - bench_async_poll(); + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, numBlocks, &pending)) { ret = wc_Sha3_512_Final(&hash[i], digest[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, ×, &pending)) { goto exit_sha3_512; } } } /* for i */ - } while (BENCH_ASYNC_IS_PEND()); + } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_512: - bench_stats_sym_finish("SHA3-512", doAsync, count, start); + bench_stats_sym_finish("SHA3-512", doAsync, count, start, ret); exit: - if (ret < 0) { - printf("bench_sha3_512 failed: %d\n", ret); - } - -#ifdef WOLFSSL_ASYNC_CRYPT for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_Sha3_512_Free(&hash[i]); } -#endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* WOLFSSL_NOSHA3_512 */ #endif @@ -2464,7 +2368,7 @@ int bench_ripemd(void) count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("RIPEMD", 0, count, start); + bench_stats_sym_finish("RIPEMD", 0, count, start, ret); return 0; } @@ -2501,7 +2405,7 @@ void bench_blake2(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("BLAKE2b", 0, count, start); + bench_stats_sym_finish("BLAKE2b", 0, count, start, ret); } #endif @@ -2539,7 +2443,7 @@ void bench_cmac(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CMAC", 0, count, start); + bench_stats_sym_finish("AES-CMAC", 0, count, start, ret); } #endif /* WOLFSSL_CMAC */ @@ -2565,7 +2469,7 @@ void bench_scrypt(void) count += i; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("scrypt", 0, "", 0, count, start); + bench_stats_asym_finish("scrypt", 0, "", 0, count, start, ret); } #endif /* HAVE_SCRYPT */ @@ -2577,13 +2481,11 @@ void bench_rsaKeyGen(int doAsync) { RsaKey genKey[BENCH_MAX_PENDING]; double start; - int ret, i, count = 0, times; + int ret, i, count = 0, times, pending = 0; int k, keySz; const int keySizes[2] = {1024, 2048}; const long rsa_e_val = 65537; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(genKey, 0, sizeof(genKey)); @@ -2593,11 +2495,11 @@ void bench_rsaKeyGen(int doAsync) bench_stats_start(&count, &start); do { /* while free pending slots in queue, submit ops */ - for (times = 0; times < genTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < genTimes || pending > 0; ) { + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes, &pending)) { wc_FreeRsaKey(&genKey[i]); ret = wc_InitRsaKey_ex(&genKey[i], HEAP_HINT, @@ -2607,7 +2509,7 @@ void bench_rsaKeyGen(int doAsync) } ret = wc_MakeRsaKey(&genKey[i], keySz, rsa_e_val, &rng); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, &pending)) { goto exit; } } @@ -2616,10 +2518,9 @@ void bench_rsaKeyGen(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("RSA", keySz, "key gen", doAsync, count, start); + bench_stats_asym_finish("RSA", keySz, "key gen", doAsync, count, start, ret); if (ret < 0) { - printf("bench_rsaKeyGen failed: %d\n", ret); break; } } @@ -2628,8 +2529,6 @@ void bench_rsaKeyGen(int doAsync) for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_FreeRsaKey(&genKey[i]); } - - bench_async_end(); } #endif /* WOLFSSL_KEY_GEN */ @@ -2649,7 +2548,7 @@ void bench_rsaKeyGen(int doAsync) void bench_rsa(int doAsync) { - int ret, i, times, count = 0; + int ret, i, times, count = 0, pending = 0; size_t bytes; word32 idx = 0; const byte* tmp; @@ -2674,8 +2573,6 @@ void bench_rsa(int doAsync) #error "need a cert buffer size" #endif /* USE_CERT_BUFFERS */ - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(rsaKey, 0, sizeof(rsaKey)); @@ -2705,15 +2602,15 @@ void bench_rsa(int doAsync) /* begin public RSA */ bench_stats_start(&count, &start); do { - for (times = 0; times < ntimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < ntimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes, &pending)) { ret = wc_RsaPublicEncrypt(message, len, enc[i], RSA_BUF_SIZE, &rsaKey[i], &rng); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, &pending)) { goto exit_rsa_pub; } } @@ -2722,35 +2619,27 @@ void bench_rsa(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_rsa_pub: - bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count, start); + bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count, start, ret); if (ret < 0) { goto exit; } -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&rsaKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif - /* capture resulting encrypt length */ idx = rsaKeySz/8; /* begin private async RSA */ bench_stats_start(&count, &start); do { - for (times = 0; times < ntimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < ntimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, ntimes, &pending)) { ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i], RSA_BUF_SIZE, &rsaKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]), 1, ×, &pending)) { goto exit; } } @@ -2759,11 +2648,7 @@ exit_rsa_pub: count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count, start); - - if (ret < 0) { - printf("bench_rsa failed: %d\n", ret); - } + bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count, start, ret); /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -2773,8 +2658,6 @@ exit: FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT); FREE_ARRAY(out, BENCH_MAX_PENDING, HEAP_HINT); FREE_VAR(message, HEAP_HINT); - - bench_async_end(); } #endif /* !NO_RSA */ @@ -2802,7 +2685,7 @@ exit: void bench_dh(int doAsync) { int ret, i; - int count = 0, times; + int count = 0, times, pending = 0; const byte* tmp = NULL; double start = 0.0f; DhKey dhKey[BENCH_MAX_PENDING]; @@ -2839,8 +2722,6 @@ void bench_dh(int doAsync) #error "need to define a cert buffer size" #endif /* USE_CERT_BUFFERS */ - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(dhKey, 0, sizeof(dhKey)); @@ -2869,15 +2750,15 @@ void bench_dh(int doAsync) bench_stats_start(&count, &start); do { /* while free pending slots in queue, submit ops */ - for (times = 0; times < genTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < genTimes || pending > 0; ) { + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, genTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, genTimes, &pending)) { privSz[i] = 0; ret = wc_DhGenerateKeyPair(&dhKey[i], &rng, priv[i], &privSz[i], pub[i], &pubSz[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, &pending)) { goto exit_dh_gen; } } @@ -2886,7 +2767,7 @@ void bench_dh(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_dh_gen: - bench_stats_asym_finish("DH", dhKeySz, "key gen", doAsync, count, start); + bench_stats_asym_finish("DH", dhKeySz, "key gen", doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -2896,26 +2777,20 @@ exit_dh_gen: ret = wc_DhGenerateKeyPair(&dhKey[0], &rng, priv2, &privSz2, pub2, &pubSz2); #ifdef WOLFSSL_ASYNC_CRYPT ret = wc_AsyncWait(ret, &dhKey[0].asyncDev, WC_ASYNC_FLAG_NONE); - - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&dhKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; #endif /* Key Agree */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, agreeTimes, &pending)) { ret = wc_DhAgree(&dhKey[i], agree[i], &agreeSz[i], priv[i], privSz[i], pub2, pubSz2); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, ×, &pending)) { goto exit; } } @@ -2924,11 +2799,7 @@ exit_dh_gen: count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("DH", dhKeySz, "key agree", doAsync, count, start); - - if (ret < 0) { - printf("bench_dh failed: %d\n", ret); - } + bench_stats_asym_finish("DH", dhKeySz, "key agree", doAsync, count, start, ret); /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -2940,8 +2811,6 @@ exit: FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT); FREE_VAR(priv2, HEAP_HINT); FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } #endif /* !NO_DH */ @@ -3059,7 +2928,7 @@ void bench_ntru(void) return; } } - bench_stats_asym_finish("NTRU", ntruBits, "encryption", 0, i, start); + bench_stats_asym_finish("NTRU", ntruBits, "encryption", 0, i, start, ret); ret = ntru_crypto_drbg_uninstantiate(drbg); if (ret != DRBG_OK) { @@ -3088,7 +2957,7 @@ void bench_ntru(void) return; } } - bench_stats_asym_finish("NTRU", ntruBits, "decryption", 0, i, start); + bench_stats_asym_finish("NTRU", ntruBits, "decryption", 0, i, start, ret); } } @@ -3141,10 +3010,9 @@ void bench_ntruKeyGen(void) public_key, &private_key_len, private_key); } - bench_stats_asym_finish("NTRU", ntruBits, "key gen", 0, i, start); + bench_stats_asym_finish("NTRU", ntruBits, "key gen", 0, i, start, ret); if (ret != NTRU_OK) { - printf("keygen failed\n"); return; } @@ -3159,17 +3027,18 @@ void bench_ntruKeyGen(void) #endif #ifdef HAVE_ECC -#define BENCH_ECC_SIZE 32 + +#ifndef BENCH_ECC_SIZE + #define BENCH_ECC_SIZE 32 +#endif void bench_eccMakeKey(int doAsync) { - int ret, i, times, count; + int ret = 0, i, times, count, pending = 0; const int keySize = BENCH_ECC_SIZE; ecc_key genKey[BENCH_MAX_PENDING]; double start; - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(&genKey, 0, sizeof(genKey)); @@ -3177,11 +3046,11 @@ void bench_eccMakeKey(int doAsync) bench_stats_start(&count, &start); do { /* while free pending slots in queue, submit ops */ - for (times = 0; times < genTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < genTimes || pending > 0; ) { + bench_async_poll(&pending); for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, genTimes, &pending)) { wc_ecc_free(&genKey[i]); ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, doAsync ? devId : INVALID_DEVID); @@ -3190,7 +3059,7 @@ void bench_eccMakeKey(int doAsync) } ret = wc_ecc_make_key(&rng, keySize, &genKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, ×, &pending)) { goto exit; } } @@ -3199,23 +3068,17 @@ void bench_eccMakeKey(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("ECC", keySize * 8, "key gen", doAsync, count, start); - - if (ret < 0) { - printf("bench_eccMakeKey failed: %d\n", ret); - } + bench_stats_asym_finish("ECC", keySize * 8, "key gen", doAsync, count, start, ret); /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ecc_free(&genKey[i]); } - - bench_async_end(); } void bench_ecc(int doAsync) { - int ret, i, times, count; + int ret, i, times, count, pending = 0; const int keySize = BENCH_ECC_SIZE; ecc_key genKey[BENCH_MAX_PENDING]; #ifdef HAVE_ECC_DHE @@ -3237,8 +3100,6 @@ void bench_ecc(int doAsync) #endif DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, BENCH_ECC_SIZE, HEAP_HINT); - bench_async_begin(); - /* clear for done cleanup */ XMEMSET(&genKey, 0, sizeof(genKey)); #ifdef HAVE_ECC_DHE @@ -3271,26 +3132,19 @@ void bench_ecc(int doAsync) } #ifdef HAVE_ECC_DHE -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&genKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif /* ECC Shared Secret */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes, &pending)) { x[i] = keySize; ret = wc_ecc_shared_secret(&genKey[i], &genKey2[i], shared[i], &x[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, &pending)) { goto exit_ecdhe; } } @@ -3299,7 +3153,7 @@ void bench_ecc(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_ecdhe: - bench_stats_asym_finish("ECDHE", keySize * 8, "agree", doAsync, count, start); + bench_stats_asym_finish("ECDHE", keySize * 8, "agree", doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -3307,13 +3161,6 @@ exit_ecdhe: #endif /* HAVE_ECC_DHE */ #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN) -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&genKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif /* Init digest to sign */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -3325,17 +3172,17 @@ exit_ecdhe: /* ECC Sign */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes, &pending)) { if (genKey[i].state == 0) x[i] = ECC_MAX_SIG_SIZE; ret = wc_ecc_sign_hash(digest[i], keySize, sig[i], &x[i], &rng, &genKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, &pending)) { goto exit_ecdsa_sign; } } @@ -3344,35 +3191,28 @@ exit_ecdhe: count += times; } while (bench_stats_sym_check(start)); exit_ecdsa_sign: - bench_stats_asym_finish("ECDSA", keySize * 8, "sign", doAsync, count, start); + bench_stats_asym_finish("ECDSA", keySize * 8, "sign", doAsync, count, start, ret); if (ret < 0) { goto exit; } #ifdef HAVE_ECC_VERIFY -#ifdef WOLFSSL_ASYNC_CRYPT - /* Clear events */ - for (i = 0; i < BENCH_MAX_PENDING; i++) { - XMEMSET(&genKey[i].asyncDev.event, 0, sizeof(WOLF_EVENT)); - } - asyncPending = 0; -#endif /* ECC Verify */ bench_stats_start(&count, &start); do { - for (times = 0; times < agreeTimes || BENCH_ASYNC_IS_PEND(); ) { - bench_async_poll(); + for (times = 0; times < agreeTimes || pending > 0; ) { + bench_async_poll(&pending); /* while free pending slots in queue, submit ops */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes)) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, agreeTimes, &pending)) { if (genKey[i].state == 0) verify[i] = 0; ret = wc_ecc_verify_hash(sig[i], x[i], digest[i], keySize, &verify[i], &genKey[i]); - if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×)) { + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, ×, &pending)) { goto exit_ecdsa_verify; } } @@ -3381,16 +3221,12 @@ exit_ecdsa_sign: count += times; } while (bench_stats_sym_check(start)); exit_ecdsa_verify: - bench_stats_asym_finish("ECDSA", keySize * 8, "verify", doAsync, count, start); + bench_stats_asym_finish("ECDSA", keySize * 8, "verify", doAsync, count, start, ret); #endif /* HAVE_ECC_VERIFY */ #endif /* !NO_ASN && HAVE_ECC_SIGN */ exit: - if (ret < 0) { - printf("bench_ecc failed: %d\n", ret); - } - /* cleanup */ for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ecc_free(&genKey[i]); @@ -3406,8 +3242,6 @@ exit: FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT); #endif FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); - - bench_async_end(); } @@ -3464,7 +3298,7 @@ void bench_eccEncrypt(void) count += i; } while (bench_stats_sym_check(start)); exit_enc: - bench_stats_asym_finish("ECC", keySize * 8, "encrypt", 0, count, start); + bench_stats_asym_finish("ECC", keySize * 8, "encrypt", 0, count, start, ret); bench_stats_start(&count, &start); do { @@ -3479,14 +3313,10 @@ exit_enc: count += i; } while (bench_stats_sym_check(start)); exit_dec: - bench_stats_asym_finish("ECC", keySize * 8, "decrypt", 0, count, start); + bench_stats_asym_finish("ECC", keySize * 8, "decrypt", 0, count, start, ret); exit: - if (ret != 0) { - printf("bench_eccEncrypt failed! %d\n", ret); - } - /* cleanup */ wc_ecc_free(&userB); wc_ecc_free(&userA); @@ -3514,7 +3344,7 @@ void bench_curve25519KeyGen(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_asym_finish("CURVE", 25519, "key gen", 0, count, start); + bench_stats_asym_finish("CURVE", 25519, "key gen", 0, count, start, ret); } #ifdef HAVE_CURVE25519_SHARED_SECRET @@ -3555,7 +3385,7 @@ void bench_curve25519KeyAgree(void) count += i; } while (bench_stats_sym_check(start)); exit: - bench_stats_asym_finish("CURVE", 25519, "key agree", 0, count, start); + bench_stats_asym_finish("CURVE", 25519, "key agree", 0, count, start, ret); wc_curve25519_free(&genKey2); wc_curve25519_free(&genKey); @@ -3580,7 +3410,7 @@ void bench_ed25519KeyGen(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_asym_finish("ED", 25519, "key gen", 0, count, start); + bench_stats_asym_finish("ED", 25519, "key gen", 0, count, start, 0); } @@ -3622,7 +3452,7 @@ void bench_ed25519KeySign(void) count += i; } while (bench_stats_sym_check(start)); exit_ed_sign: - bench_stats_asym_finish("ED", 25519, "sign", 0, count, start); + bench_stats_asym_finish("ED", 25519, "sign", 0, count, start, ret); #ifdef HAVE_ED25519_VERIFY bench_stats_start(&count, &start); @@ -3639,7 +3469,7 @@ exit_ed_sign: count += i; } while (bench_stats_sym_check(start)); exit_ed_verify: - bench_stats_asym_finish("ED", 25519, "verify", 0, count, start); + bench_stats_asym_finish("ED", 25519, "verify", 0, count, start, ret); #endif /* HAVE_ED25519_VERIFY */ #endif /* HAVE_ED25519_SIGN */ diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 39beee02d..786233782 100755 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -2822,9 +2822,8 @@ int wc_AesSetIV(Aes* aes, const byte* iv) (const byte*)aes->asyncKey, aes->keylen, (const byte*)aes->asyncIv, AES_BLOCK_SIZE); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_CBC_ENCRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_ENCRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -2913,9 +2912,8 @@ int wc_AesSetIV(Aes* aes, const byte* iv) (const byte*)aes->asyncKey, aes->keylen, (const byte*)aes->asyncIv, AES_BLOCK_SIZE); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_CBC_DECRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_DECRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -3165,7 +3163,7 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) byte out_block[AES_BLOCK_SIZE]; int odd, even, blocks; byte *tmp; - + if (aes == NULL || out == NULL || in == NULL) { return BAD_FUNC_ARG; } @@ -5494,7 +5492,7 @@ static int AES_GCM_decrypt(const unsigned char *in, unsigned char *out, for (; i < nbytes/16/8; i++) { r0 = _mm_setzero_si128(); r1 = _mm_setzero_si128(); - + tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64); tmp2 = _mm_add_epi32(ctr1, ONE); tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64); @@ -7078,9 +7076,8 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, (const byte*)aes->asyncKey, aes->keylen, iv, ivSz, authTag, authTagSz, authIn, authInSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_GCM_ENCRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_ENCRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; @@ -7091,6 +7088,7 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, testDev->aes.authTagSz = authTagSz; testDev->aes.authIn = authIn; testDev->aes.authInSz = authInSz; + return WC_PENDING_E; } #endif } @@ -7340,9 +7338,8 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz, (const byte*)aes->asyncKey, aes->keylen, iv, ivSz, authTag, authTagSz, authIn, authInSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &aes->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_AES_GCM_DECRYPT; + if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_DECRYPT)) { + WC_ASYNC_TEST* testDev = &aes->asyncDev.test; testDev->aes.aes = aes; testDev->aes.out = out; testDev->aes.in = in; diff --git a/wolfcrypt/src/des3.c b/wolfcrypt/src/des3.c index dd85bb0e7..82e201fbf 100755 --- a/wolfcrypt/src/des3.c +++ b/wolfcrypt/src/des3.c @@ -1582,9 +1582,8 @@ return IntelQaSymDes3CbcEncrypt(&des->asyncDev, out, in, sz, des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &des->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DES3_CBC_ENCRYPT; + if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_ENCRYPT)) { + WC_ASYNC_TEST* testDev = &des->asyncDev.test; testDev->des.des = des; testDev->des.out = out; testDev->des.in = in; @@ -1625,9 +1624,8 @@ return IntelQaSymDes3CbcDecrypt(&des->asyncDev, out, in, sz, des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &des->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DES3_CBC_DECRYPT; + if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_DECRYPT)) { + WC_ASYNC_TEST* testDev = &des->asyncDev.test; testDev->des.des = des; testDev->des.out = out; testDev->des.in = in; diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 5756db6e1..5be449cb2 100755 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -687,9 +687,8 @@ static int wc_DhGenerateKeyPair_Async(DhKey* key, WC_RNG* rng, /* TODO: Not implemented - use software for now */ #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DH_GEN; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_DH_GEN)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->dhGen.key = key; testDev->dhGen.rng = rng; testDev->dhGen.priv = priv; @@ -836,9 +835,8 @@ static int wc_DhAgree_Async(DhKey* key, byte* agree, word32* agreeSz, ret = IntelQaDhAgree(&key->asyncDev, &key->p.raw, agree, agreeSz, priv, privSz, otherPub, pubSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_DH_AGREE; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_DH_AGREE)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->dhAgree.key = key; testDev->dhAgree.agree = agree; testDev->dhAgree.agreeSz = agreeSz; diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 947274dcd..2e78d45d4 100755 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -2803,9 +2803,8 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key, &curve->Af->raw, &curve->Bf->raw, &curve->prime->raw, private_key->dp->cofactor); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ - WC_ASYNC_TEST* testDev = &private_key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_SHARED_SEC; + if (wc_AsyncTestInit(&private_key->asyncDev, ASYNC_TEST_ECC_SHARED_SEC)) { + WC_ASYNC_TEST* testDev = &private_key->asyncDev.test; testDev->eccSharedSec.private_key = private_key; testDev->eccSharedSec.public_point = point; testDev->eccSharedSec.out = out; @@ -3017,9 +3016,8 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) #elif defined(HAVE_INTEL_QA) /* TODO: Not implemented */ #else - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_MAKE; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_MAKE)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->eccMake.rng = rng; testDev->eccMake.key = key; testDev->eccMake.size = keysize; @@ -3465,9 +3463,8 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng, #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \ defined(WOLFSSL_ASYNC_CRYPT_TEST) if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) { - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_SIGN; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_SIGN)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->eccSign.in = in; testDev->eccSign.inSz = inlen; testDev->eccSign.rng = rng; @@ -4011,9 +4008,8 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash, #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \ defined(WOLFSSL_ASYNC_CRYPT_TEST) if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) { - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_ECC_VERIFY; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_VERIFY)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->eccVerify.r = r; testDev->eccVerify.s = s; testDev->eccVerify.hash = hash; diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index fb4426aec..7779d95a0 100755 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -1289,9 +1289,8 @@ static int wc_RsaFunctionAsync(const byte* in, word32 inLen, byte* out, (void)rng; #ifdef WOLFSSL_ASYNC_CRYPT_TEST - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_RSA_FUNC; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_RSA_FUNC)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->rsaFunc.in = in; testDev->rsaFunc.inSz = inLen; testDev->rsaFunc.out = out; @@ -1870,9 +1869,8 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) #elif defined(HAVE_INTEL_QA) /* TODO: Not implemented */ #else - WC_ASYNC_TEST* testDev = &key->asyncDev.test; - if (testDev->type == ASYNC_TEST_NONE) { - testDev->type = ASYNC_TEST_RSA_MAKE; + if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_RSA_MAKE)) { + WC_ASYNC_TEST* testDev = &key->asyncDev.test; testDev->rsaMake.rng = rng; testDev->rsaMake.key = key; testDev->rsaMake.size = size; diff --git a/wolfcrypt/src/wolfevent.c b/wolfcrypt/src/wolfevent.c index 6e3eae2b3..e5f74fe5b 100644 --- a/wolfcrypt/src/wolfevent.c +++ b/wolfcrypt/src/wolfevent.c @@ -41,8 +41,8 @@ int wolfEvent_Init(WOLF_EVENT* event, WOLF_EVENT_TYPE type, void* context) return BAD_FUNC_ARG; } - if (event->pending) { - WOLFSSL_MSG("event already pending!"); + if (event->state == WOLF_EVENT_STATE_PENDING) { + WOLFSSL_MSG("Event already pending!"); return BAD_COND_E; } @@ -99,10 +99,6 @@ int wolfEventQueue_Push(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) } #endif - /* Setup event */ - event->next = NULL; - event->pending = 1; - ret = wolfEventQueue_Add(queue, event); #ifndef SINGLE_THREADED @@ -145,6 +141,8 @@ int wolfEventQueue_Add(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event) return BAD_FUNC_ARG; } + event->next = NULL; /* added to end */ + event->prev = NULL; if (queue->tail == NULL) { queue->head = event; } @@ -218,7 +216,7 @@ int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter, if (ret < 0) break; /* exit for */ /* If event is done then process */ - if (event->done) { + if (event->state == WOLF_EVENT_STATE_DONE) { /* remove from queue */ ret = wolfEventQueue_Remove(queue, event); if (ret < 0) break; /* exit for */ diff --git a/wolfssl/wolfcrypt/wolfevent.h b/wolfssl/wolfcrypt/wolfevent.h index af984c8cd..7b49229e8 100644 --- a/wolfssl/wolfcrypt/wolfevent.h +++ b/wolfssl/wolfcrypt/wolfevent.h @@ -55,6 +55,12 @@ typedef enum WOLF_EVENT_TYPE { #endif /* WOLFSSL_ASYNC_CRYPT */ } WOLF_EVENT_TYPE; +typedef enum WOLF_EVENT_STATE { + WOLF_EVENT_STATE_READY, + WOLF_EVENT_STATE_PENDING, + WOLF_EVENT_STATE_DONE, +} WOLF_EVENT_STATE; + struct WOLF_EVENT { /* double linked list */ WOLF_EVENT* next; @@ -73,11 +79,7 @@ struct WOLF_EVENT { int ret; /* Async return code */ unsigned int flags; WOLF_EVENT_TYPE type; - - /* event flags */ - WOLF_EVENT_FLAG pending:1; - WOLF_EVENT_FLAG done:1; - /* Future event flags can go here */ + WOLF_EVENT_STATE state; }; enum WOLF_POLL_FLAGS {