From 1f72696359d429dd8a6ddaca4101cadfe17bd4b9 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 12 Oct 2017 11:16:03 -0600 Subject: [PATCH 1/8] add HMAC-MD5/SHA/SHA224/SHA384/SHA512 benchmarks --- wolfcrypt/benchmark/benchmark.c | 227 ++++++++++++++++++++++++++++++++ wolfcrypt/benchmark/benchmark.h | 6 + 2 files changed, 233 insertions(+) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 19a2aa2ec..b92b30861 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -82,6 +82,9 @@ #include #include #include +#ifndef NO_HMAC + #include +#endif #ifndef NO_PWDBASED #include #endif @@ -903,6 +906,57 @@ static void* benchmarks_do(void* args) bench_scrypt(); #endif +#ifndef NO_HMAC + #ifndef NO_MD5 + #ifndef NO_SW_BENCH + bench_hmac_md5(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) + bench_hmac_md5(1); + #endif + #endif + #ifndef NO_SHA + #ifndef NO_SW_BENCH + bench_hmac_sha(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) + bench_hmac_sha(1); + #endif + #endif + #ifdef WOLFSSL_SHA224 + #ifndef NO_SW_BENCH + bench_hmac_sha224(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224) + bench_hmac_sha224(1); + #endif + #endif + #ifndef NO_SHA256 + #ifndef NO_SW_BENCH + bench_hmac_sha256(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256) + bench_hmac_sha256(1); + #endif + #endif + #ifdef WOLFSSL_SHA384 + #ifndef NO_SW_BENCH + bench_hmac_sha384(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384) + bench_hmac_sha384(1); + #endif + #endif + #ifdef WOLFSSL_SHA512 + #ifndef NO_SW_BENCH + bench_hmac_sha512(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512) + bench_hmac_sha512(1); + #endif + #endif +#endif /* NO_HMAC */ + #ifndef NO_RSA #ifdef WOLFSSL_KEY_GEN #ifndef NO_SW_BENCH @@ -2549,6 +2603,179 @@ exit: #endif /* HAVE_SCRYPT */ +#ifndef NO_HMAC + +static void bench_hmac(int doAsync, int type, int digestSz, + byte* key, word32 keySz, const char* label) +{ + Hmac hmac[BENCH_MAX_PENDING]; + double start; + int ret, i, count = 0, times, pending = 0; + DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, digestSz, HEAP_HINT); + + /* clear for done cleanup */ + XMEMSET(hmac, 0, sizeof(hmac)); + + /* init keys */ + for (i = 0; i < BENCH_MAX_PENDING; i++) { + ret = wc_HmacInit(&hmac[i], HEAP_HINT, + doAsync ? devId : INVALID_DEVID); + if (ret != 0) { + printf("wc_HmacInit failed for %s, ret = %d\n", label, ret); + goto exit; + } + + ret = wc_HmacSetKey(&hmac[i], type, key, keySz); + if (ret != 0) { + printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret); + goto exit; + } + } + + bench_stats_start(&count, &start); + do { + 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(&hmac[i]), 0, + ×, numBlocks, &pending)) { + ret = wc_HmacUpdate(&hmac[i], bench_plain, BENCH_SIZE); + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), + 0, ×, &pending)) { + goto exit_hmac; + } + } + } /* for i */ + } /* for times */ + count += times; + + times = 0; + do { + bench_async_poll(&pending); + + for (i = 0; i < BENCH_MAX_PENDING; i++) { + if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), 0, + ×, numBlocks, &pending)) { + ret = wc_HmacFinal(&hmac[i], digest[i]); + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), + 0, ×, &pending)) { + goto exit_hmac; + } + } + } /* for i */ + } while (pending > 0); + } while (bench_stats_sym_check(start)); +exit_hmac: + bench_stats_sym_finish(label, doAsync, count, start, ret); + +exit: + +#ifdef WOLFSSL_ASYNC_CRYPT + for (i = 0; i < BENCH_MAX_PENDING; i++) { + wc_HmacFree(&hmac[i]); + } +#endif + + FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT); +} + +#ifndef NO_MD5 + +void bench_hmac_md5(int doAsync) +{ + byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; + + bench_hmac(doAsync, MD5, MD5_DIGEST_SIZE, key, sizeof(key), + "HMAC-MD5"); +} + +#endif /* NO_MD5 */ + +#ifndef NO_SHA + +void bench_hmac_sha(int doAsync) +{ + byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b }; + + bench_hmac(doAsync, SHA, SHA_DIGEST_SIZE, key, sizeof(key), + "HMAC-SHA"); +} + +#endif /* NO_SHA */ + +#ifdef WOLFSSL_SHA224 + +void bench_hmac_sha224(int doAsync) +{ + byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b }; + + bench_hmac(doAsync, SHA224, SHA224_DIGEST_SIZE, key, sizeof(key), + "HMAC-SHA224"); +} + +#endif /* WOLFSSL_SHA224 */ + +#ifndef NO_SHA256 + +void bench_hmac_sha256(int doAsync) +{ + byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; + + bench_hmac(doAsync, SHA256, SHA256_DIGEST_SIZE, key, sizeof(key), + "HMAC-SHA256"); +} + +#endif /* NO_SHA256 */ + +#ifdef WOLFSSL_SHA384 + +void bench_hmac_sha384(int doAsync) +{ + byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; + + bench_hmac(doAsync, SHA384, SHA384_DIGEST_SIZE, key, sizeof(key), + "HMAC-SHA384"); +} + +#endif /* WOLFSSL_SHA384 */ + +#ifdef WOLFSSL_SHA512 + +void bench_hmac_sha512(int doAsync) +{ + byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; + + bench_hmac(doAsync, SHA512, SHA512_DIGEST_SIZE, key, sizeof(key), + "HMAC-SHA512"); +} + +#endif /* WOLFSSL_SHA512 */ + +#endif /* NO_HMAC */ + #ifndef NO_RSA #if defined(WOLFSSL_KEY_GEN) diff --git a/wolfcrypt/benchmark/benchmark.h b/wolfcrypt/benchmark/benchmark.h index 7fc7b31f0..38317a0c2 100644 --- a/wolfcrypt/benchmark/benchmark.h +++ b/wolfcrypt/benchmark/benchmark.h @@ -67,6 +67,12 @@ void bench_sha3_512(int); int bench_ripemd(void); void bench_cmac(void); void bench_scrypt(void); +void bench_hmac_md5(int); +void bench_hmac_sha(int); +void bench_hmac_sha224(int); +void bench_hmac_sha256(int); +void bench_hmac_sha384(int); +void bench_hmac_sha512(int); void bench_rsaKeyGen(int); void bench_rsa(int); void bench_dh(int); From 2b077b279210a5d81fac9e2b4cd7986337457dd7 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 12 Oct 2017 11:51:32 -0600 Subject: [PATCH 2/8] add separate benchmarks for AES-128/192/256-CBC --- wolfcrypt/benchmark/benchmark.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index b92b30861..a68e6f161 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -594,7 +594,7 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count, dou persec = (1 / total) * blocks; } - XSNPRINTF(msg, sizeof(msg), "%-12s%s %5.0f %s took %5.3f seconds, %8.3f %s/s", + XSNPRINTF(msg, sizeof(msg), "%-16s%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(msg, sizeof(msg)); @@ -1211,7 +1211,9 @@ exit_rng: #ifndef NO_AES #ifdef HAVE_AES_CBC -void bench_aescbc(int doAsync) +static void bench_aescbc_internal(int doAsync, const byte* key, word32 keySz, + const byte* iv, const char* encLabel, + const char* decLabel) { int ret, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; @@ -1228,7 +1230,7 @@ void bench_aescbc(int doAsync) goto exit; } - ret = wc_AesSetKey(&enc[i], bench_key, 16, bench_iv, AES_ENCRYPTION); + ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_ENCRYPTION); if (ret != 0) { printf("AesSetKey failed, ret = %d\n", ret); goto exit; @@ -1254,7 +1256,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, ret); + bench_stats_sym_finish(encLabel, doAsync, count, start, ret); if (ret < 0) { goto exit; @@ -1263,7 +1265,7 @@ exit_aes_enc: #ifdef HAVE_AES_DECRYPT /* init keys */ for (i = 0; i < BENCH_MAX_PENDING; i++) { - ret = wc_AesSetKey(&enc[i], bench_key, 16, bench_iv, AES_DECRYPTION); + ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_DECRYPTION); if (ret != 0) { printf("AesSetKey failed, ret = %d\n", ret); goto exit; @@ -1289,7 +1291,7 @@ exit_aes_enc: count += times; } while (bench_stats_sym_check(start)); exit_aes_dec: - bench_stats_sym_finish("AES-Dec", doAsync, count, start, ret); + bench_stats_sym_finish(decLabel, doAsync, count, start, ret); #endif /* HAVE_AES_DECRYPT */ @@ -1299,6 +1301,17 @@ exit: wc_AesFree(&enc[i]); } } + +void bench_aescbc(int doAsync) +{ + bench_aescbc_internal(doAsync, bench_key, 16, bench_iv, + "AES-128-CBC-ENC", "AES-128-CBC-DEC"); + bench_aescbc_internal(doAsync, bench_key, 24, bench_iv, + "AES-192-CBC-ENC", "AES-192-CBC-DEC"); + bench_aescbc_internal(doAsync, bench_key, 32, bench_iv, + "AES-256-CBC-ENC", "AES-256-CBC-DEC"); +} + #endif /* HAVE_AES_CBC */ #ifdef HAVE_AESGCM From 1e445e10a18bb80ba45aa5f589af5af1edc95ed0 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 12 Oct 2017 11:59:23 -0600 Subject: [PATCH 3/8] add separate benchmarks for AES-128/192/256-CTR --- wolfcrypt/benchmark/benchmark.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index a68e6f161..c4bf66939 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -1480,13 +1480,14 @@ void bench_aesxts(void) #ifdef WOLFSSL_AES_COUNTER -void bench_aesctr(void) +static void bench_aesctr_internal(const byte* key, word32 keySz, const byte* iv, + const char* label) { Aes enc; double start; int i, count, ret; - wc_AesSetKeyDirect(&enc, bench_key, AES_BLOCK_SIZE, bench_iv, AES_ENCRYPTION); + wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION); bench_stats_start(&count, &start); do { @@ -1498,7 +1499,14 @@ void bench_aesctr(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CTR", 0, count, start, ret); + bench_stats_sym_finish(label, 0, count, start, ret); +} + +void bench_aesctr(void) +{ + bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR"); + bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR"); + bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR"); } #endif /* WOLFSSL_AES_COUNTER */ From 9a6e4b2939ab2d17c5fd9a51d76be120df4b0a29 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 12 Oct 2017 13:38:20 -0600 Subject: [PATCH 4/8] add separate benchmarks for AES-128/192/256-GCM --- wolfcrypt/benchmark/benchmark.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index c4bf66939..867349cae 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -1315,7 +1315,9 @@ void bench_aescbc(int doAsync) #endif /* HAVE_AES_CBC */ #ifdef HAVE_AESGCM -void bench_aesgcm(int doAsync) +static void bench_aesgcm_internal(int doAsync, const byte* key, word32 keySz, + const byte* iv, word32 ivSz, + const char* encLabel, const char* decLabel) { int ret, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; @@ -1343,7 +1345,7 @@ void bench_aesgcm(int doAsync) goto exit; } - ret = wc_AesGcmSetKey(&enc[i], bench_key, 16); + ret = wc_AesGcmSetKey(&enc[i], key, keySz); if (ret != 0) { printf("AesGcmSetKey failed, ret = %d\n", ret); goto exit; @@ -1361,7 +1363,7 @@ void bench_aesgcm(int doAsync) 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, + iv, ivSz, bench_tag, AES_AUTH_TAG_SZ, bench_additional, AES_AUTH_ADD_SZ); if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_gcm; @@ -1372,7 +1374,7 @@ 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, ret); + bench_stats_sym_finish(encLabel, doAsync, count, start, ret); /* GCM uses same routine in backend for both encrypt and decrypt */ bench_stats_start(&count, &start); @@ -1385,7 +1387,7 @@ exit_aes_gcm: 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, + iv, ivSz, bench_tag, AES_AUTH_TAG_SZ, bench_additional, AES_AUTH_ADD_SZ); if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { goto exit_aes_gcm_dec; @@ -1396,7 +1398,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, ret); + bench_stats_sym_finish(decLabel, doAsync, count, start, ret); exit: @@ -1411,6 +1413,16 @@ exit: FREE_VAR(bench_additional, HEAP_HINT); FREE_VAR(bench_tag, HEAP_HINT); } + +void bench_aesgcm(int doAsync) +{ + bench_aesgcm_internal(doAsync, bench_key, 16, bench_iv, 12, + "AES-128-GCM-ENC", "AES-128-GCM-DEC"); + bench_aesgcm_internal(doAsync, bench_key, 24, bench_iv, 12, + "AES-192-GCM-ENC", "AES-192-GCM-DEC"); + bench_aesgcm_internal(doAsync, bench_key, 32, bench_iv, 12, + "AES-256-GCM-ENC", "AES-256-GCM-DEC"); +} #endif /* HAVE_AESGCM */ From d65704c6b405beae2e01d01697ab5cb23a4f855e Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 12 Oct 2017 14:39:12 -0600 Subject: [PATCH 5/8] add benchmarks for AES-128/192/256-ECB --- wolfcrypt/benchmark/benchmark.c | 194 +++++++++++++++++++++++++------- wolfcrypt/benchmark/benchmark.h | 1 + 2 files changed, 156 insertions(+), 39 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 867349cae..01f2737d5 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -139,9 +139,10 @@ #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(b, n) \ + /* s == size in bytes that 1 count represents, normally BENCH_SIZE */ + #define SHOW_INTEL_CYCLES(b, n, s) \ XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \ - count == 0 ? 0 : (float)total_cycles / ((word64)count*BENCH_SIZE)) + count == 0 ? 0 : (float)total_cycles / ((word64)count*s)) #elif defined(LINUX_CYCLE_COUNT) #include #include @@ -164,15 +165,16 @@ total_cycles = total_cycles - begin_cycles; \ } while (0); + /* s == size in bytes that 1 count represents, normally 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)) + (float)total_cycles / (count*s)) #else #define INIT_CYCLE_COUNTER #define BEGIN_INTEL_CYCLES #define END_INTEL_CYCLES - #define SHOW_INTEL_CYCLES(b, n) b[XSTRLEN(b)] = '\n' + #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n' #endif /* let's use buffers, we have them */ @@ -564,7 +566,10 @@ 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, int ret) +/* countSz is number of bytes that 1 count represents. Normally bench_size, + * except for AES direct that operates on AES_BLOCK_SIZE blocks */ +static void bench_stats_sym_finish(const char* desc, int doAsync, int count, + int countSz, double start, int ret) { double total, persec = 0, blocks = count; const char* blockType; @@ -574,7 +579,7 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count, dou total = current_time(0) - start; /* calculate actual bytes */ - blocks *= bench_size; + blocks *= countSz; /* determine if we should show as KB or MB */ if (blocks > (1024 * 1024)) { @@ -597,7 +602,7 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count, dou XSNPRINTF(msg, sizeof(msg), "%-16s%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(msg, sizeof(msg)); + SHOW_INTEL_CYCLES(msg, sizeof(msg), countSz); printf("%s", msg); /* show errors */ @@ -761,6 +766,14 @@ static void* benchmarks_do(void* args) bench_aesgcm(1); #endif #endif +#ifdef WOLFSSL_AES_DIRECT + #ifndef NO_SW_BENCH + bench_aesecb(0); + #endif + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) + bench_aesecb(1); + #endif +#endif #ifdef WOLFSSL_AES_XTS bench_aesxts(); #endif @@ -1201,7 +1214,7 @@ void bench_rng(void) count += i; } while (bench_stats_sym_check(start)); exit_rng: - bench_stats_sym_finish("RNG", 0, count, start, ret); + bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret); wc_FreeRng(&myrng); } @@ -1256,7 +1269,7 @@ static void bench_aescbc_internal(int doAsync, const byte* key, word32 keySz, count += times; } while (bench_stats_sym_check(start)); exit_aes_enc: - bench_stats_sym_finish(encLabel, doAsync, count, start, ret); + bench_stats_sym_finish(encLabel, doAsync, count, bench_size, start, ret); if (ret < 0) { goto exit; @@ -1291,7 +1304,7 @@ exit_aes_enc: count += times; } while (bench_stats_sym_check(start)); exit_aes_dec: - bench_stats_sym_finish(decLabel, doAsync, count, start, ret); + bench_stats_sym_finish(decLabel, doAsync, count, bench_size, start, ret); #endif /* HAVE_AES_DECRYPT */ @@ -1374,7 +1387,7 @@ static void bench_aesgcm_internal(int doAsync, const byte* key, word32 keySz, count += times; } while (bench_stats_sym_check(start)); exit_aes_gcm: - bench_stats_sym_finish(encLabel, doAsync, count, start, ret); + bench_stats_sym_finish(encLabel, doAsync, count, bench_size, start, ret); /* GCM uses same routine in backend for both encrypt and decrypt */ bench_stats_start(&count, &start); @@ -1398,7 +1411,7 @@ exit_aes_gcm: count += times; } while (bench_stats_sym_check(start)); exit_aes_gcm_dec: - bench_stats_sym_finish(decLabel, doAsync, count, start, ret); + bench_stats_sym_finish(decLabel, doAsync, count, bench_size, start, ret); exit: @@ -1426,6 +1439,109 @@ void bench_aesgcm(int doAsync) #endif /* HAVE_AESGCM */ +#ifdef WOLFSSL_AES_DIRECT +static void bench_aesecb_internal(int doAsync, const byte* key, word32 keySz, + const char* encLabel, const char* decLabel) +{ + int ret, i, count = 0, times, pending = 0; + Aes enc[BENCH_MAX_PENDING]; + double start; + + /* clear for done cleanup */ + XMEMSET(enc, 0, sizeof(enc)); + + /* init keys */ + for (i = 0; i < BENCH_MAX_PENDING; i++) { + if ((ret = wc_AesInit(&enc[i], HEAP_HINT, + doAsync ? devId : INVALID_DEVID)) != 0) { + printf("AesInit failed, ret = %d\n", ret); + goto exit; + } + + ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_ENCRYPTION); + if (ret != 0) { + printf("AesSetKey failed, ret = %d\n", ret); + goto exit; + } + } + + bench_stats_start(&count, &start); + do { + 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, &pending)) { + wc_AesEncryptDirect(&enc[i], bench_cipher, bench_plain); + ret = 0; + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { + goto exit_aes_enc; + } + } + } /* for i */ + } /* for times */ + count += times; + } while (bench_stats_sym_check(start)); +exit_aes_enc: + bench_stats_sym_finish(encLabel, doAsync, count, AES_BLOCK_SIZE, + start, ret); + +#ifdef HAVE_AES_DECRYPT + /* init keys */ + for (i = 0; i < BENCH_MAX_PENDING; i++) { + ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_DECRYPTION); + if (ret != 0) { + printf("AesSetKey failed, ret = %d\n", ret); + goto exit; + } + } + + bench_stats_start(&count, &start); + do { + 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, &pending)) { + wc_AesDecryptDirect(&enc[i], bench_plain, + bench_cipher); + ret = 0; + if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, ×, &pending)) { + goto exit_aes_dec; + } + } + } /* for i */ + } /* for times */ + count += times; + } while (bench_stats_sym_check(start)); +exit_aes_dec: + bench_stats_sym_finish(decLabel, doAsync, count, AES_BLOCK_SIZE, + start, ret); + +#endif /* HAVE_AES_DECRYPT */ + +exit: + + for (i = 0; i < BENCH_MAX_PENDING; i++) { + wc_AesFree(&enc[i]); + } +} + +void bench_aesecb(int doAsync) +{ + bench_aesecb_internal(doAsync, bench_key, 16, + "AES-128-ECB-ENC", "AES-128-ECB-DEC"); + bench_aesecb_internal(doAsync, bench_key, 24, + "AES-192-ECB-ENC", "AES-192-ECB-DEC"); + bench_aesecb_internal(doAsync, bench_key, 32, + "AES-256-ECB-ENC", "AES-256-ECB-DEC"); +} + +#endif /* WOLFSSL_AES_DIRECT */ + + #ifdef WOLFSSL_AES_XTS void bench_aesxts(void) { @@ -1463,7 +1579,7 @@ void bench_aesxts(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-XTS-enc", 0, count, start, ret); + bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret); wc_AesXtsFree(&aes); /* decryption benchmark */ @@ -1485,7 +1601,7 @@ void bench_aesxts(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-XTS-dec", 0, count, start, ret); + bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret); wc_AesXtsFree(&aes); } #endif /* WOLFSSL_AES_XTS */ @@ -1511,7 +1627,7 @@ static void bench_aesctr_internal(const byte* key, word32 keySz, const byte* iv, } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish(label, 0, count, start, ret); + bench_stats_sym_finish(label, 0, count, bench_size, start, ret); } void bench_aesctr(void) @@ -1547,7 +1663,7 @@ void bench_aesccm(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CCM", 0, count, start, ret); + bench_stats_sym_finish("AES-CCM", 0, count, bench_size, start, ret); FREE_VAR(bench_additional, HEAP_HINT); FREE_VAR(bench_tag, HEAP_HINT); @@ -1582,7 +1698,7 @@ void bench_poly1305() wc_Poly1305Final(&enc, mac); count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("POLY1305", 0, count, start, ret); + bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret); } #endif /* HAVE_POLY1305 */ @@ -1612,7 +1728,7 @@ void bench_camellia(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("Camellia", 0, count, start, ret); + bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret); } #endif @@ -1661,7 +1777,7 @@ void bench_des(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_3des: - bench_stats_sym_finish("3DES", doAsync, count, start, ret); + bench_stats_sym_finish("3DES", doAsync, count, bench_size, start, ret); exit: @@ -1693,7 +1809,7 @@ void bench_idea(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("IDEA", 0, count, start, ret); + bench_stats_sym_finish("IDEA", 0, count, bench_size, start, ret); } #endif /* HAVE_IDEA */ @@ -1742,7 +1858,7 @@ void bench_arc4(int doAsync) count += times; } while (bench_stats_sym_check(start)); exit_arc4: - bench_stats_sym_finish("ARC4", doAsync, count, start, ret); + bench_stats_sym_finish("ARC4", doAsync, count, bench_size, start, ret); exit: @@ -1769,7 +1885,7 @@ void bench_hc128(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("HC128", 0, count, start, 0); + bench_stats_sym_finish("HC128", 0, count, bench_size, start, 0); } #endif /* HAVE_HC128 */ @@ -1790,7 +1906,7 @@ void bench_rabbit(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("RABBIT", 0, count, start, 0); + bench_stats_sym_finish("RABBIT", 0, count, bench_size, start, 0); } #endif /* NO_RABBIT */ @@ -1812,7 +1928,7 @@ void bench_chacha(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("CHACHA", 0, count, start, 0); + bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0); } #endif /* HAVE_CHACHA*/ @@ -1837,7 +1953,7 @@ void bench_chacha20_poly1305_aead(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("CHA-POLY", 0, count, start, ret); + bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret); } #endif /* HAVE_CHACHA && HAVE_POLY1305 */ @@ -1896,7 +2012,7 @@ void bench_md5(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_md5: - bench_stats_sym_finish("MD5", doAsync, count, start, ret); + bench_stats_sym_finish("MD5", doAsync, count, bench_size, start, ret); exit: @@ -1965,7 +2081,7 @@ void bench_sha(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha: - bench_stats_sym_finish("WC_SHA", doAsync, count, start, ret); + bench_stats_sym_finish("SHA", doAsync, count, bench_size, start, ret); exit: @@ -2031,7 +2147,7 @@ void bench_sha224(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha224: - bench_stats_sym_finish("WC_SHA-224", doAsync, count, start, ret); + bench_stats_sym_finish("SHA-224", doAsync, count, bench_size, start, ret); exit: @@ -2096,7 +2212,7 @@ void bench_sha256(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha256: - bench_stats_sym_finish("WC_SHA-256", doAsync, count, start, ret); + bench_stats_sym_finish("SHA-256", doAsync, count, bench_size, start, ret); exit: @@ -2161,7 +2277,7 @@ void bench_sha384(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha384: - bench_stats_sym_finish("WC_SHA-384", doAsync, count, start, ret); + bench_stats_sym_finish("SHA-384", doAsync, count, bench_size, start, ret); exit: @@ -2226,7 +2342,7 @@ void bench_sha512(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha512: - bench_stats_sym_finish("WC_SHA-512", doAsync, count, start, ret); + bench_stats_sym_finish("SHA-512", doAsync, count, bench_size, start, ret); exit: @@ -2293,7 +2409,7 @@ void bench_sha3_224(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_224: - bench_stats_sym_finish("SHA3-224", doAsync, count, start, ret); + bench_stats_sym_finish("SHA3-224", doAsync, count, bench_size, start, ret); exit: @@ -2358,7 +2474,7 @@ void bench_sha3_256(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_256: - bench_stats_sym_finish("SHA3-256", doAsync, count, start, ret); + bench_stats_sym_finish("SHA3-256", doAsync, count, bench_size, start, ret); exit: @@ -2423,7 +2539,7 @@ void bench_sha3_384(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_384: - bench_stats_sym_finish("SHA3-384", doAsync, count, start, ret); + bench_stats_sym_finish("SHA3-384", doAsync, count, bench_size, start, ret); exit: @@ -2488,7 +2604,7 @@ void bench_sha3_512(int doAsync) } while (pending > 0); } while (bench_stats_sym_check(start)); exit_sha3_512: - bench_stats_sym_finish("SHA3-512", doAsync, count, start, ret); + bench_stats_sym_finish("SHA3-512", doAsync, count, bench_size, start, ret); exit: @@ -2530,7 +2646,7 @@ int bench_ripemd(void) count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("RIPEMD", 0, count, start, ret); + bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret); return 0; } @@ -2567,7 +2683,7 @@ void bench_blake2(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("BLAKE2b", 0, count, start, ret); + bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret); } #endif @@ -2605,7 +2721,7 @@ void bench_cmac(void) } count += i; } while (bench_stats_sym_check(start)); - bench_stats_sym_finish("AES-CMAC", 0, count, start, ret); + bench_stats_sym_finish("AES-CMAC", 0, count, bench_size, start, ret); } #endif /* WOLFSSL_CMAC */ @@ -2701,7 +2817,7 @@ static void bench_hmac(int doAsync, int type, int digestSz, } while (pending > 0); } while (bench_stats_sym_check(start)); exit_hmac: - bench_stats_sym_finish(label, doAsync, count, start, ret); + bench_stats_sym_finish(label, doAsync, count, bench_size, start, ret); exit: diff --git a/wolfcrypt/benchmark/benchmark.h b/wolfcrypt/benchmark/benchmark.h index 38317a0c2..cc76b7297 100644 --- a/wolfcrypt/benchmark/benchmark.h +++ b/wolfcrypt/benchmark/benchmark.h @@ -50,6 +50,7 @@ void bench_chacha20_poly1305_aead(void); void bench_aescbc(int); void bench_aesgcm(int); void bench_aesccm(void); +void bench_aesecb(int); void bench_aesxts(void); void bench_aesctr(void); void bench_poly1305(void); From d5b7c13fbf2d7e01ec7421a0e6b8dbe216ce9bc6 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 12 Oct 2017 14:49:31 -0600 Subject: [PATCH 6/8] change enc/dec labels for AES, move scrypt bench below HMAC --- wolfcrypt/benchmark/benchmark.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 01f2737d5..e511b0e1e 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -915,10 +915,6 @@ static void* benchmarks_do(void* args) bench_cmac(); #endif -#ifdef HAVE_SCRYPT - bench_scrypt(); -#endif - #ifndef NO_HMAC #ifndef NO_MD5 #ifndef NO_SW_BENCH @@ -970,6 +966,10 @@ static void* benchmarks_do(void* args) #endif #endif /* NO_HMAC */ +#ifdef HAVE_SCRYPT + bench_scrypt(); +#endif + #ifndef NO_RSA #ifdef WOLFSSL_KEY_GEN #ifndef NO_SW_BENCH @@ -1318,11 +1318,11 @@ exit: void bench_aescbc(int doAsync) { bench_aescbc_internal(doAsync, bench_key, 16, bench_iv, - "AES-128-CBC-ENC", "AES-128-CBC-DEC"); + "AES-128-CBC-enc", "AES-128-CBC-dec"); bench_aescbc_internal(doAsync, bench_key, 24, bench_iv, - "AES-192-CBC-ENC", "AES-192-CBC-DEC"); + "AES-192-CBC-enc", "AES-192-CBC-dec"); bench_aescbc_internal(doAsync, bench_key, 32, bench_iv, - "AES-256-CBC-ENC", "AES-256-CBC-DEC"); + "AES-256-CBC-enc", "AES-256-CBC-dec"); } #endif /* HAVE_AES_CBC */ @@ -1430,11 +1430,11 @@ exit: void bench_aesgcm(int doAsync) { bench_aesgcm_internal(doAsync, bench_key, 16, bench_iv, 12, - "AES-128-GCM-ENC", "AES-128-GCM-DEC"); + "AES-128-GCM-enc", "AES-128-GCM-dec"); bench_aesgcm_internal(doAsync, bench_key, 24, bench_iv, 12, - "AES-192-GCM-ENC", "AES-192-GCM-DEC"); + "AES-192-GCM-enc", "AES-192-GCM-dec"); bench_aesgcm_internal(doAsync, bench_key, 32, bench_iv, 12, - "AES-256-GCM-ENC", "AES-256-GCM-DEC"); + "AES-256-GCM-enc", "AES-256-GCM-dec"); } #endif /* HAVE_AESGCM */ @@ -1532,11 +1532,11 @@ exit: void bench_aesecb(int doAsync) { bench_aesecb_internal(doAsync, bench_key, 16, - "AES-128-ECB-ENC", "AES-128-ECB-DEC"); + "AES-128-ECB-enc", "AES-128-ECB-dec"); bench_aesecb_internal(doAsync, bench_key, 24, - "AES-192-ECB-ENC", "AES-192-ECB-DEC"); + "AES-192-ECB-enc", "AES-192-ECB-dec"); bench_aesecb_internal(doAsync, bench_key, 32, - "AES-256-ECB-ENC", "AES-256-ECB-DEC"); + "AES-256-ECB-enc", "AES-256-ECB-dec"); } #endif /* WOLFSSL_AES_DIRECT */ From 7dccd9d478ac94552016c28c5b12f0ff4e37d99f Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Fri, 13 Oct 2017 13:22:18 -0600 Subject: [PATCH 7/8] set hash size for PIC32MZ hardware crypto --- wolfcrypt/benchmark/benchmark.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index e511b0e1e..339081916 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -1977,6 +1977,9 @@ void bench_md5(int doAsync) printf("InitMd5_ex failed, ret = %d\n", ret); goto exit; } + #ifdef WOLFSSL_PIC32MZ_HASH + wc_Md5SizeSet(&hash[i], numBlocks * BENCH_SIZE); + #endif } bench_stats_start(&count, &start); @@ -2046,6 +2049,9 @@ void bench_sha(int doAsync) printf("InitSha failed, ret = %d\n", ret); goto exit; } + #ifdef WOLFSSL_PIC32MZ_HASH + wc_ShaSizeSet(&hash[i], numBlocks * BENCH_SIZE); + #endif } bench_stats_start(&count, &start); @@ -2178,6 +2184,9 @@ void bench_sha256(int doAsync) printf("InitSha256_ex failed, ret = %d\n", ret); goto exit; } + #ifdef WOLFSSL_PIC32MZ_HASH + wc_Sha256SizeSet(&hash[i], numBlocks * BENCH_SIZE); + #endif } bench_stats_start(&count, &start); From e49560fbf05217230fbe8aeee1e7a7837ee0dec1 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Tue, 17 Oct 2017 09:31:21 -0600 Subject: [PATCH 8/8] add missing parameter in SHOW_INTEL_CYCLES --- 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 339081916..b3b7cd9b6 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -166,7 +166,7 @@ } while (0); /* s == size in bytes that 1 count represents, normally BENCH_SIZE */ - #define SHOW_INTEL_CYCLES(b, n) \ + #define SHOW_INTEL_CYCLES(b, n, s) \ XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \ (float)total_cycles / (count*s))