From 3ad7e39dc4dfcdd264d85b80f9eef967e567385b Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Wed, 17 May 2023 01:04:40 -0500 Subject: [PATCH] refactor benchmark.c for linux kernel compatibility -- WOLFSSL_SMALL_STACK and WOLFSSL_NO_FLOAT_FMT codepaths, SAVE/RESTORE_VECTOR_REGISTERS, refactor of several stack array initializations that broke in the kernel, and replacement of an fputs() call with printf(). --- wolfcrypt/benchmark/benchmark.c | 459 +++++++++++++++++++++++--------- 1 file changed, 337 insertions(+), 122 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 5d354d9be..59a51cc0d 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -65,6 +65,28 @@ #include #include +#ifdef WOLFSSL_NO_FLOAT_FMT + #define FLT_FMT "%0ld,%09lu" + #define FLT_FMT_PREC "%0ld.%0*lu" + #define FLT_FMT_PREC2 FLT_FMT_PREC + #define FLT_FMT_ARGS(x) (long)(x), ((x) < 0) ? (unsigned long)(-(((x) - (double)(long)(x)) * 1000000000.0)) : (unsigned long)(((x) - (double)(long)(x)) * 1000000000.0) + static const double pow_10_array[] = { 0.0, 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0 }; + #define FLT_FMT_PREC_ARGS(p, x) \ + (long)(x), \ + p, \ + (x) >= 0.0 ? \ + (unsigned long int)((((x) - (double)(long)(x)) * pow_10_array[(p)+1]) + 0.5) : \ + (unsigned long int)((((-(x)) - (double)((long)-(x))) * pow_10_array[(p)+1]) + 0.5) + #define FLT_FMT_PREC2_ARGS(w, p, x) FLT_FMT_PREC_ARGS(p, x) +#else + #define FLT_FMT "%f" + #define FLT_FMT_PREC "%.*f" + #define FLT_FMT_PREC2 "%*.*f" + #define FLT_FMT_ARGS(x) x + #define FLT_FMT_PREC_ARGS(p, x) p, x + #define FLT_FMT_PREC2_ARGS(w, p, x) w, p, x +#endif + #ifdef WOLFSSL_ESPIDF #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) #include "driver/gptimer.h" @@ -922,12 +944,12 @@ static const char* bench_desc_words[][15] = { #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles; /* s == size in bytes that 1 count represents, normally BENCH_SIZE */ #define SHOW_INTEL_CYCLES(b, n, s) \ - (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), " %s = %6.2f\n", \ + (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), " %s = " FLT_FMT_PREC2 "\n", \ bench_result_words1[lng_index][2], \ - count == 0 ? 0 : (double)total_cycles / ((word64)count*(s))) + FLT_FMT_PREC2_ARGS(6, 2, count == 0 ? 0 : (double)total_cycles / ((word64)count*(s)))) #define SHOW_INTEL_CYCLES_CSV(b, n, s) \ - (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), "%.6f,\n", \ - count == 0 ? 0 : (double)total_cycles / ((word64)count*(s))) + (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), FLT_FMT_PREC ",\n", \ + FLT_FMT_PREC_ARGS(6, count == 0 ? 0 : (double)total_cycles / ((word64)count*(s)))) #elif defined(LINUX_CYCLE_COUNT) #include #include @@ -952,12 +974,12 @@ static const char* bench_desc_words[][15] = { /* s == size in bytes that 1 count represents, normally BENCH_SIZE */ #define SHOW_INTEL_CYCLES(b, n, s) \ - (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \ + (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = " FLT_FMT_PREC2 "\n", \ bench_result_words1[lng_index][2], \ - (float)total_cycles / (count*s)) + FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s))) #define SHOW_INTEL_CYCLES_CSV(b, n, s) \ - (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.6f,\n", \ - (float)total_cycles / (count*s)) + (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \ + FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s))) #elif defined(SYNERGY_CYCLE_COUNT) #include "hal_data.h" @@ -970,12 +992,12 @@ static const char* bench_desc_words[][15] = { /* s == size in bytes that 1 count represents, normally BENCH_SIZE */ #define SHOW_INTEL_CYCLES(b, n, s) \ - (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \ + (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = " FLT_FMT_PREC2 "\n", \ bench_result_words1[lng_index][2], \ - (float)total_cycles / (count*s)) + FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s))) #define SHOW_INTEL_CYCLES_CSV(b, n, s) \ - (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.6f,\n", \ - (float)total_cycles / (count*s)) + (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \ + FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s))) #elif defined(WOLFSSL_ESPIDF) static THREAD_LS_T word64 begin_cycles; static THREAD_LS_T word64 total_cycles; @@ -1005,14 +1027,14 @@ static const char* bench_desc_words[][15] = { total_cycles = (get_xtensa_cycles() - begin_cycles); #define SHOW_ESP_CYCLES(b, n, s) \ - (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \ + (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = " FLT_FMT_PREC2 "\n", \ bench_result_words1[lng_index][2], \ - (float)total_cycles / (count*s) \ + FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)) \ ) #define SHOW_ESP_CYCLES_CSV(b, n, s) \ - (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.6f,\n", \ - (float)total_cycles / (count*s)) + (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \ + FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s))) /* xthal_get_ccount_ex() is a single-overflow-tolerant extension to ** the Espressif `unsigned xthal_get_ccount()` which is known to overflow @@ -1283,7 +1305,7 @@ static const char* bench_result_words2[][5] = { } #else - #define BENCH_MAX_PENDING (1) + #define BENCH_MAX_PENDING 1 #define BENCH_ASYNC_GET_DEV(obj) NULL static WC_INLINE int bench_async_check(int* ret, void* asyncDev, @@ -1590,14 +1612,14 @@ typedef enum bench_stat_type { for (bstat = bench_stats_head; bstat != NULL; ) { if (bstat->type == BENCH_STAT_SYM) { - printf("%-16s%s %8.3f %s/s\n", bstat->desc, - BENCH_DEVID_GET_NAME(bstat->useDeviceID), bstat->perfsec, + printf("%-16s%s " FLT_FMT_PREC2 " %s/s\n", bstat->desc, + BENCH_DEVID_GET_NAME(bstat->useDeviceID), FLT_FMT_PREC2_ARGS(8, 3, bstat->perfsec), base2 ? "MB" : "mB"); } else { - printf("%-5s %4d %-9s %s %.3f ops/sec\n", + printf("%-5s %4d %-9s %s " FLT_FMT_PREC " ops/sec\n", bstat->algo, bstat->strength, bstat->desc, - BENCH_DEVID_GET_NAME(bstat->useDeviceID), bstat->perfsec); + BENCH_DEVID_GET_NAME(bstat->useDeviceID), FLT_FMT_PREC_ARGS(3, bstat->perfsec)); } bstat = bstat->next; @@ -1651,12 +1673,12 @@ typedef enum bench_stat_type { for (i=0; itype == BENCH_STAT_SYM) { - printf("%-16s %8.3f %s/s\n", bstat->desc, bstat->perfsec, + printf("%-16s " FLT_FMT_PREC2 " %s/s\n", bstat->desc, FLT_FMT_PREC2_ARGS(8, 3, bstat->perfsec), base2 ? "MB" : "mB"); } else if (bstat->type == BENCH_STAT_ASYM) { - printf("%-5s %4d %-9s %.3f ops/sec\n", - bstat->algo, bstat->strength, bstat->desc, bstat->perfsec); + printf("%-5s %4d %-9s " FLT_FMT_PREC " ops/sec\n", + bstat->algo, bstat->strength, bstat->desc, FLT_FMT_PREC_ARGS(3, bstat->perfsec)); } } } @@ -1673,12 +1695,14 @@ static WC_INLINE void bench_stats_init(void) static WC_INLINE void bench_stats_start(int* count, double* start) { + SAVE_VECTOR_REGISTERS(pr_err("SAVE_VECTOR_REGISTERS failed for benchmark run.");); + *count = 0; *start = current_time(1); #ifdef WOLFSSL_ESPIDF - ESP_LOGV(TAG, "finish total_cycles = %llu, start=%f", - total_cycles, *start ); + ESP_LOGV(TAG, "finish total_cycles = %llu, start=" FLT_FMT, + total_cycles, FLT_FMT_ARG(*start) ); BEGIN_ESP_CYCLES #else @@ -1785,10 +1809,12 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID, { double total, persec = 0, blocks = (double)count; const char* blockType; - char msg[__BENCHMARK_MAXIMUM_LINE_LENGTH] = {0}; + char msg[__BENCHMARK_MAXIMUM_LINE_LENGTH]; const char** word = bench_result_words1[lng_index]; static int sym_header_printed = 0; + XMEMSET(msg, 0, sizeof(msg)); + #ifdef WOLFSSL_ESPIDF END_ESP_CYCLES #else @@ -1873,9 +1899,9 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID, /* note this codepath brings in all the fields from the non-CSV case. */ #ifdef WOLFSSL_ESPIDF #ifdef HAVE_GET_CYCLES - (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu,%f,%f,%lu,", desc, + (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc, BENCH_DEVID_GET_NAME(useDeviceID), - bytes_processed, total, persec, + bytes_processed, FLT_FMT_ARGS(total), FLT_FMT_ARGS(persec), (long unsigned int) total_cycles); #else #warning "HAVE_GET_CYCLES should be defined for WOLFSSL_ESPIDF" @@ -1885,20 +1911,20 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID, #else #ifdef HAVE_GET_CYCLES - (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu,%f,%f,%lu,", desc, + (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc, BENCH_DEVID_GET_NAME(useDeviceID), - bytes_processed, total, persec, total_cycles); + bytes_processed, FLT_FMT_ARGS(total), FLT_FMT_ARGS(persec), total_cycles); #else - (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu,%f,%f,", desc, + (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",", desc, BENCH_DEVID_GET_NAME(useDeviceID), - bytes_processed, total, persec); + bytes_processed, FLT_FMT_ARGS(total), FLT_FMT_ARGS(persec)); #endif #endif #elif defined(BENCH_DEVID) - (void)XSNPRINTF(msg, sizeof(msg), "%s,%s,%f,", desc, - BENCH_DEVID_GET_NAME(useDeviceID), persec); + (void)XSNPRINTF(msg, sizeof(msg), "%s,%s," FLT_FMT ",", desc, + BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_ARGS(persec)); #else - (void)XSNPRINTF(msg, sizeof(msg), "%s,%f,", desc, persec); + (void)XSNPRINTF(msg, sizeof(msg), "%s," FLT_FMT ",", desc, FLT_FMT_ARGS(persec)); #endif #ifdef WOLFSSL_ESPIDF @@ -1914,23 +1940,23 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID, #ifdef GENERATE_MACHINE_PARSEABLE_REPORT #ifdef HAVE_GET_CYCLES (void)XSNPRINTF(msg, sizeof(msg), - "%-24s%s %5.0f %s %s %5.3f %s, %8.3f %s/s" + "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, " FLT_FMT_PREC2 " %s/s" ", %lu cycles,", - desc, BENCH_DEVID_GET_NAME(useDeviceID), blocks, blockType, - word[0], total, word[1], persec, blockType, + desc, BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType, + word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1], FLT_FMT_PREC2_ARGS(8, 3, persec), blockType, (unsigned long) total_cycles); #else (void)XSNPRINTF(msg, sizeof(msg), - "%-24s%s %5.0f %s %s %5.3f %s, %8.3f %s/s" + "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, " FLT_FMT_PREC2 " %s/s" ",", - desc, BENCH_DEVID_GET_NAME(useDeviceID), blocks, blockType, - word[0], total, word[1], persec, blockType); + desc, BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType, + word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1], FLT_FMT_PREC2_ARGS(8, 3, persec), blockType); #endif /* HAVE_GET_CYCLES */ #else (void)XSNPRINTF(msg, sizeof(msg), - "%-24s%s %5.0f %s %s %5.3f %s, %8.3f %s/s", - desc, BENCH_DEVID_GET_NAME(useDeviceID), blocks, blockType, - word[0], total, word[1], persec, blockType); + "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, " FLT_FMT_PREC2 " %s/s", + desc, BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType, + word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1], FLT_FMT_PREC2_ARGS(8, 3, persec), blockType); #endif #ifdef WOLFSSL_ESPIDF @@ -1961,6 +1987,8 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID, (void)useDeviceID; (void)ret; + RESTORE_VECTOR_REGISTERS(); + TEST_SLEEP(); } /* bench_stats_sym_finish */ @@ -1976,9 +2004,11 @@ static void bench_stats_asym_finish_ex(const char* algo, int strength, double total, each = 0, opsSec, milliEach; const char **word = bench_result_words2[lng_index]; const char* kOpsSec = "Ops/Sec"; - char msg[256] = {0}; + char msg[256]; static int asym_header_printed = 0; + XMEMSET(msg, 0, sizeof(msg)); + total = current_time(0) - start; #ifdef LINUX_RUSAGE_UTIME @@ -2034,19 +2064,19 @@ static void bench_stats_asym_finish_ex(const char* algo, int strength, #ifdef GENERATE_MACHINE_PARSEABLE_REPORT #ifdef HAVE_GET_CYCLES (void)XSNPRINTF(msg, sizeof(msg), - "asym,%s,%d,%s%s,%.3f,%.3f,%d,%f,%lu,%.6f\n", - algo, strength, desc, desc_extra, milliEach, opsSec, - count, total, (unsigned long) total_cycles, - (double)total_cycles / (double)count); + "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d," FLT_FMT ",%lu," FLT_FMT_PREC "\n", + algo, strength, desc, desc_extra, FLT_FMT_PREC_ARGS(3, milliEach), FLT_FMT_PREC_ARGS(3, opsSec), + count, FLT_FMT_ARGS(total), (unsigned long) total_cycles, + FLT_FMT_PREC_ARGS(6, (double)total_cycles / (double)count)); #else (void)XSNPRINTF(msg, sizeof(msg), - "asym,%s,%d,%s%s,%.3f,%.3f,%d,%f\n", - algo, strength, desc, desc_extra, milliEach, opsSec, - count, total); + "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d," FLT_FMT "\n", + algo, strength, desc, desc_extra, FLT_FMT_PREC_ARGS(3, milliEach), FLT_FMT_PREC_ARGS(3, opsSec), + count, FLT_FMT_ARGS(total)); #endif #else - (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s,%.3f,%.3f,\n", algo, - strength, desc, desc_extra, milliEach, opsSec); + (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",\n", algo, + strength, desc, desc_extra, FLT_FMT_PREC_ARGS(3, milliEach), FLT_FMT_PREC_ARGS(3, opsSec)); #endif } /* if (csv_format == 1) */ @@ -2054,25 +2084,25 @@ static void bench_stats_asym_finish_ex(const char* algo, int strength, #ifdef GENERATE_MACHINE_PARSEABLE_REPORT #ifdef HAVE_GET_CYCLES (void)XSNPRINTF(msg, sizeof(msg), - "%-6s %5d %8s%-2s %s %6d %s %5.3f %s, %s %5.3f ms," - " %.3f %s, %lu cycles\n", algo, strength, desc, + "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s " FLT_FMT_PREC2 " ms," + " " FLT_FMT_PREC " %s, %lu cycles\n", algo, strength, desc, desc_extra, BENCH_DEVID_GET_NAME(useDeviceID), - count, word[0], total, word[1], word[2], milliEach, - opsSec, word[3], (unsigned long) total_cycles); + count, word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2], FLT_FMT_PREC2_ARGS(5, 3, milliEach), + FLT_FMT_PREC_ARGS(3, opsSec), word[3], (unsigned long) total_cycles); #else (void)XSNPRINTF(msg, sizeof(msg), - "%-6s %5d %8s%-2s %s %6d %s %5.3f %s, %s %5.3f ms," - " %.3f %s\n", algo, strength, desc, + "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s " FLT_FMT_PREC2 " ms," + " " FLT_FMT_PREC " %s\n", algo, strength, desc, desc_extra, BENCH_DEVID_GET_NAME(useDeviceID), - count, word[0], total, word[1], word[2], milliEach, - opsSec, word[3]); + count, word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2], FLT_FMT_PREC2_ARGS(5, 3, milliEach), + FLT_FMT_PREC_ARGS(3, opsSec), word[3]); #endif /* HAVE_GET_CYCLES */ #else (void)XSNPRINTF(msg, sizeof(msg), - "%-6s %5d %8s%-2s %s %6d %s %5.3f %s, %s %5.3f ms," - " %.3f %s\n", algo, strength, desc, desc_extra, + "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s " FLT_FMT_PREC2 " ms," + " " FLT_FMT_PREC " %s\n", algo, strength, desc, desc_extra, BENCH_DEVID_GET_NAME(useDeviceID), count, word[0], - total, word[1], word[2], milliEach, opsSec, word[3]); + FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2], FLT_FMT_PREC2_ARGS(5, 3, milliEach), FLT_FMT_PREC_ARGS(3, opsSec), word[3]); #endif } printf("%s", msg); @@ -2094,6 +2124,8 @@ static void bench_stats_asym_finish_ex(const char* algo, int strength, (void)useDeviceID; (void)ret; + RESTORE_VECTOR_REGISTERS(); + TEST_SLEEP(); } /* bench_stats_asym_finish_ex */ @@ -2906,8 +2938,8 @@ int benchmark_init(void) wolfSSL_Debugging_ON(); #endif - printf("%swolfCrypt Benchmark (block bytes %d, min %.1f sec each)\n", - info_prefix, (int)bench_size, BENCH_MIN_RUNTIME_SEC); + printf("%swolfCrypt Benchmark (block bytes %d, min " FLT_FMT_PREC " sec each)\n", + info_prefix, (int)bench_size, FLT_FMT_PREC_ARGS(1, BENCH_MIN_RUNTIME_SEC)); #ifndef GENERATE_MACHINE_PARSEABLE_REPORT if (csv_format == 1) { @@ -3280,7 +3312,7 @@ static void bench_aesgcm_internal(int useDeviceID, int ret = 0, i, count = 0, times, pending = 0; Aes enc[BENCH_MAX_PENDING]; #ifdef HAVE_AES_DECRYPT - Aes dec[BENCH_MAX_PENDING]; + Aes dec[BENCH_MAX_PENDING+1]; #endif double start; @@ -3295,9 +3327,6 @@ static void bench_aesgcm_internal(int useDeviceID, /* clear for done cleanup */ XMEMSET(enc, 0, sizeof(enc)); -#ifdef HAVE_AES_DECRYPT - XMEMSET(dec, 0, sizeof(dec)); -#endif #ifdef WOLFSSL_ASYNC_CRYPT if (bench_additional) #endif @@ -3350,6 +3379,8 @@ exit_aes_gcm: start, ret); #ifdef HAVE_AES_DECRYPT + XMEMSET(dec, 0, sizeof(dec)); + /* init keys */ for (i = 0; i < BENCH_MAX_PENDING; i++) { if ((ret = wc_AesInit(&dec[i], HEAP_HINT, @@ -3955,7 +3986,10 @@ static void bench_aesctr_internal(const byte* key, word32 keySz, printf("wc_AesInit failed, ret = %d\n", ret); } - wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION); + if (wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION) < 0) { + printf("wc_AesSetKeyDirect failed, ret = %d\n", ret); + return; + } bench_stats_start(&count, &start); do { @@ -5965,7 +5999,7 @@ exit: #ifndef NO_HMAC static void bench_hmac(int useDeviceID, int type, int digestSz, - byte* key, word32 keySz, const char* label) + const byte* key, word32 keySz, const char* label) { Hmac hmac[BENCH_MAX_PENDING]; double start; @@ -6057,7 +6091,7 @@ exit: void bench_hmac_md5(int useDeviceID) { - byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + static const byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; bench_hmac(useDeviceID, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key), @@ -6070,7 +6104,7 @@ void bench_hmac_md5(int useDeviceID) void bench_hmac_sha(int useDeviceID) { - byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + static const byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; @@ -6084,7 +6118,7 @@ void bench_hmac_sha(int useDeviceID) void bench_hmac_sha224(int useDeviceID) { - byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + static const 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 }; @@ -6100,7 +6134,7 @@ void bench_hmac_sha224(int useDeviceID) void bench_hmac_sha256(int useDeviceID) { - byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + static const 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 }; @@ -6115,7 +6149,7 @@ void bench_hmac_sha256(int useDeviceID) void bench_hmac_sha384(int useDeviceID) { - byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + static const 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, @@ -6132,7 +6166,7 @@ void bench_hmac_sha384(int useDeviceID) void bench_hmac_sha512(int useDeviceID) { - byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + static const 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, @@ -6153,7 +6187,7 @@ void bench_pbkdf2(void) double start; int ret = 0, count = 0; const char* passwd32 = "passwordpasswordpasswordpassword"; - const byte salt32[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06, + static const byte salt32[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06, 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06, 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06, 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 }; @@ -6207,14 +6241,27 @@ void bench_siphash(void) #if defined(WOLFSSL_KEY_GEN) static void bench_rsaKeyGen_helper(int useDeviceID, word32 keySz) { +#ifdef WOLFSSL_SMALL_STACK + RsaKey *genKey; +#else RsaKey genKey[BENCH_MAX_PENDING]; +#endif double start; int ret = 0, i, count = 0, times, pending = 0; const long rsa_e_val = WC_RSA_EXPONENT; const char**desc = bench_desc_words[lng_index]; +#ifdef WOLFSSL_SMALL_STACK + genKey = (RsaKey *)XMALLOC(sizeof(*genKey) * BENCH_MAX_PENDING, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (genKey == NULL) { + printf("bench_rsaKeyGen_helper malloc failed\n"); + return; + } +#endif + /* clear for done cleanup */ - XMEMSET(genKey, 0, sizeof(genKey)); + XMEMSET(genKey, 0, sizeof(*genKey) * BENCH_MAX_PENDING); bench_stats_start(&count, &start); do { @@ -6252,6 +6299,10 @@ exit: for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_FreeRsaKey(&genKey[i]); } + +#ifdef WOLFSSL_SMALL_STACK + XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); +#endif } void bench_rsaKeyGen(int useDeviceID) @@ -6292,7 +6343,7 @@ void bench_rsaKeyGen_size(int useDeviceID, word32 keySz) #if defined(WOLFSSL_RSA_VERIFY_INLINE) || defined(WOLFSSL_RSA_PUBLIC_ONLY) #if defined(USE_CERT_BUFFERS_2048) -static unsigned char rsa_2048_sig[] = { +static const unsigned char rsa_2048_sig[] = { 0x8c, 0x9e, 0x37, 0xbf, 0xc3, 0xa6, 0xba, 0x1c, 0x53, 0x22, 0x40, 0x4b, 0x8b, 0x0d, 0x3c, 0x0e, 0x2e, 0x8c, 0x31, 0x2c, 0x47, 0xbf, 0x03, 0x48, @@ -6327,7 +6378,7 @@ static unsigned char rsa_2048_sig[] = { 0x9e, 0xd2, 0x51, 0xe6, 0x41, 0xbf, 0x4f, 0xa2 }; #elif defined(USE_CERT_BUFFERS_3072) -static unsigned char rsa_3072_sig[] = { +static const unsigned char rsa_3072_sig[] = { 0x1a, 0xd6, 0x0d, 0xfd, 0xe3, 0x41, 0x95, 0x76, 0x27, 0x16, 0x7d, 0xc7, 0x94, 0x16, 0xca, 0xa8, 0x26, 0x08, 0xbe, 0x78, 0x87, 0x72, 0x4c, 0xd9, @@ -6603,7 +6654,11 @@ exit: void bench_rsa(int useDeviceID) { int i; +#ifdef WOLFSSL_SMALL_STACK + RsaKey *rsaKey; +#else RsaKey rsaKey[BENCH_MAX_PENDING]; +#endif int ret = 0; word32 rsaKeySz = 0; const byte* tmp; @@ -6612,6 +6667,15 @@ void bench_rsa(int useDeviceID) word32 idx; #endif +#ifdef WOLFSSL_SMALL_STACK + rsaKey = (RsaKey *)XMALLOC(sizeof(*rsaKey) * BENCH_MAX_PENDING, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (rsaKey == NULL) { + printf("bench_rsa malloc failed\n"); + return; + } +#endif + #ifdef USE_CERT_BUFFERS_1024 tmp = rsa_key_der_1024; bytes = (size_t)sizeof_rsa_key_der_1024; @@ -6633,7 +6697,7 @@ void bench_rsa(int useDeviceID) #endif /* USE_CERT_BUFFERS */ /* clear for done cleanup */ - XMEMSET(rsaKey, 0, sizeof(rsaKey)); + XMEMSET(rsaKey, 0, sizeof(*rsaKey) * BENCH_MAX_PENDING); /* init keys */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -6695,6 +6759,10 @@ exit_bench_rsa: for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_FreeRsaKey(&rsaKey[i]); } + +#ifdef WOLFSSL_SMALL_STACK + XFREE(rsaKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); +#endif } @@ -6703,12 +6771,25 @@ exit_bench_rsa: void bench_rsa_key(int useDeviceID, word32 rsaKeySz) { int ret = 0, i, pending = 0; +#ifdef WOLFSSL_SMALL_STACK + RsaKey *rsaKey; +#else RsaKey rsaKey[BENCH_MAX_PENDING]; +#endif int isPending[BENCH_MAX_PENDING]; long exp = 65537L; +#ifdef WOLFSSL_SMALL_STACK + rsaKey = (RsaKey *)XMALLOC(sizeof(*rsaKey) * BENCH_MAX_PENDING, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (rsaKey == NULL) { + printf("bench_rsa_key malloc failed\n"); + return; + } +#endif + /* clear for done cleanup */ - XMEMSET(rsaKey, 0, sizeof(rsaKey)); + XMEMSET(rsaKey, 0, sizeof(*rsaKey) * BENCH_MAX_PENDING); XMEMSET(isPending, 0, sizeof(isPending)); /* init keys */ @@ -6750,6 +6831,10 @@ exit_bench_rsa_key: for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_FreeRsaKey(&rsaKey[i]); } + +#ifdef WOLFSSL_SMALL_STACK + XFREE(rsaKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); +#endif } #endif /* WOLFSSL_KEY_GEN */ #endif /* !NO_RSA */ @@ -6785,7 +6870,11 @@ void bench_dh(int useDeviceID) int count = 0, times, pending = 0; const byte* tmp = NULL; double start = 0.0F; +#ifdef WOLFSSL_SMALL_STACK + DhKey *dhKey = NULL; +#else DhKey dhKey[BENCH_MAX_PENDING]; +#endif int dhKeySz = BENCH_DH_KEY_SIZE * 8; /* used in printf */ const char**desc = bench_desc_words[lng_index]; #ifndef NO_ASN @@ -6823,6 +6912,14 @@ void bench_dh(int useDeviceID) WC_INIT_ARRAY(priv, byte, BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT); +#ifdef WOLFSSL_SMALL_STACK + dhKey = (DhKey *)XMALLOC(sizeof(DhKey) * BENCH_MAX_PENDING, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (! dhKey) { + ret = MEMORY_E; + goto exit; + } +#endif + #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC if (pub[0] == NULL || pub2 == NULL || agree[0] == NULL || priv[0] == NULL || priv2 == NULL) { ret = MEMORY_E; @@ -6888,7 +6985,12 @@ void bench_dh(int useDeviceID) #endif /* clear for done cleanup */ - XMEMSET(dhKey, 0, sizeof(dhKey)); + XMEMSET(dhKey, 0, sizeof(DhKey) * BENCH_MAX_PENDING); +#if 0 + for (i = 0; i < BENCH_MAX_PENDING; i++) { + XMEMSET(dhKey[i], 0, sizeof(DhKey)); + } +#endif /* init keys */ for (i = 0; i < BENCH_MAX_PENDING; i++) { @@ -6999,9 +7101,18 @@ exit: useDeviceID, count, start, ret); /* cleanup */ +#ifdef WOLFSSL_SMALL_STACK + if (dhKey) { + for (i = 0; i < BENCH_MAX_PENDING; i++) { + wc_FreeDhKey(&dhKey[i]); + } + XFREE(dhKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + } +#else for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_FreeDhKey(&dhKey[i]); } +#endif WC_FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT); WC_FREE_VAR(pub2, HEAP_HINT); @@ -7169,16 +7280,29 @@ void bench_eccMakeKey(int useDeviceID, int curveId) int ret = 0, i, times, count, pending = 0; int deviceID; int keySize; +#ifdef WOLFSSL_SMALL_STACK + ecc_key *genKey; +#else ecc_key genKey[BENCH_MAX_PENDING]; +#endif char name[BENCH_ECC_NAME_SZ]; double start; const char**desc = bench_desc_words[lng_index]; +#ifdef WOLFSSL_SMALL_STACK + genKey = (ecc_key *)XMALLOC(sizeof(*genKey) * BENCH_MAX_PENDING, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (genKey == NULL) { + printf("bench_eccMakeKey malloc failed\n"); + return; + } +#endif + deviceID = useDeviceID ? devId : INVALID_DEVID; keySize = wc_ecc_get_curve_size_from_id(curveId); /* clear for done cleanup */ - XMEMSET(&genKey, 0, sizeof(genKey)); + XMEMSET(genKey, 0, sizeof(*genKey) * BENCH_MAX_PENDING); /* ECC Make Key */ bench_stats_start(&count, &start); @@ -7221,6 +7345,10 @@ exit: for (i = 0; i < BENCH_MAX_PENDING; i++) { wc_ecc_free(&genKey[i]); } + +#ifdef WOLFSSL_SMALL_STACK + XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); +#endif } @@ -7230,10 +7358,18 @@ void bench_ecc(int useDeviceID, int curveId) int deviceID; int keySize; char name[BENCH_ECC_NAME_SZ]; +#ifdef WOLFSSL_SMALL_STACK + ecc_key *genKey; +#else ecc_key genKey[BENCH_MAX_PENDING]; +#endif #ifdef HAVE_ECC_DHE +#ifdef WOLFSSL_SMALL_STACK + ecc_key *genKey2; +#else ecc_key genKey2[BENCH_MAX_PENDING]; #endif +#endif #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN) #ifdef HAVE_ECC_VERIFY @@ -7257,6 +7393,24 @@ void bench_ecc(int useDeviceID, int curveId) BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT); #endif +#ifdef WOLFSSL_SMALL_STACK + genKey = (ecc_key *)XMALLOC(sizeof(*genKey) * BENCH_MAX_PENDING, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (genKey == NULL) { + printf("bench_eccMakeKey malloc failed\n"); + return; + } +#ifdef HAVE_ECC_DHE + genKey2 = (ecc_key *)XMALLOC(sizeof(*genKey2) * BENCH_MAX_PENDING, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (genKey2 == NULL) { + XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + printf("bench_eccMakeKey malloc failed\n"); + return; + } +#endif +#endif + #ifdef HAVE_ECC_DHE WC_INIT_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT); @@ -7269,9 +7423,9 @@ void bench_ecc(int useDeviceID, int curveId) deviceID = useDeviceID ? devId : INVALID_DEVID; /* clear for done cleanup */ - XMEMSET(&genKey, 0, sizeof(genKey)); + XMEMSET(genKey, 0, sizeof(*genKey) * BENCH_MAX_PENDING); #ifdef HAVE_ECC_DHE - XMEMSET(&genKey2, 0, sizeof(genKey2)); + XMEMSET(genKey2, 0, sizeof(*genKey2) * BENCH_MAX_PENDING); #endif keySize = wc_ecc_get_curve_size_from_id(curveId); @@ -7446,6 +7600,13 @@ exit: #endif } +#ifdef WOLFSSL_SMALL_STACK + XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + #ifdef HAVE_ECC_DHE + XFREE(genKey2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + #endif +#endif + #ifdef HAVE_ECC_DHE WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT); #endif @@ -7468,58 +7629,83 @@ exit: #ifdef HAVE_ECC_ENCRYPT void bench_eccEncrypt(int curveId) { - ecc_key userA, userB; +#define BENCH_ECCENCRYPT_MSG_SIZE 48 +#define BENCH_ECCENCRYPT_OUT_SIZE (BENCH_ECCENCRYPT_MSG_SIZE + WC_SHA256_DIGEST_SIZE + (MAX_ECC_BITS+3)/4 + 2) + word32 outSz = BENCH_ECCENCRYPT_OUT_SIZE; +#ifdef WOLFSSL_SMALL_STACK + ecc_key *userA = NULL, *userB = NULL; + byte *msg = NULL; + byte *out = NULL; + char *name = NULL; +#else + ecc_key userA[1], userB[1]; + byte msg[BENCH_ECCENCRYPT_MSG_SIZE]; + byte out[BENCH_ECCENCRYPT_OUT_SIZE]; + char name[BENCH_ECC_NAME_SZ]; +#endif int keySize; - byte msg[48]; - byte out[sizeof(msg) + WC_SHA256_DIGEST_SIZE + (MAX_ECC_BITS+3)/4 + 2]; - word32 outSz = sizeof(out); word32 bench_plainSz = bench_size; int ret, i, count; double start; const char**desc = bench_desc_words[lng_index]; - char name[BENCH_ECC_NAME_SZ]; + +#ifdef WOLFSSL_SMALL_STACK + userA = (ecc_key *)XMALLOC(sizeof(*userA), + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + userB = (ecc_key *)XMALLOC(sizeof(*userB), + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + msg = (byte *)XMALLOC(BENCH_ECCENCRYPT_MSG_SIZE, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + out = (byte *)XMALLOC(outSz, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + name = (char *)XMALLOC(BENCH_ECC_NAME_SZ, + HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if ((! userA) || (! userB) || (! msg) || (! out) || (! name)) { + printf("bench_eccEncrypt malloc failed\n"); + goto exit; + } +#endif keySize = wc_ecc_get_curve_size_from_id(curveId); - ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId); + ret = wc_ecc_init_ex(userA, HEAP_HINT, devId); if (ret != 0) { printf("wc_ecc_encrypt make key A failed: %d\n", ret); - return; + goto exit; } - ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId); + ret = wc_ecc_init_ex(userB, HEAP_HINT, devId); if (ret != 0) { printf("wc_ecc_encrypt make key B failed: %d\n", ret); - wc_ecc_free(&userA); - return; + goto exit; } #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \ (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \ !defined(HAVE_SELFTEST) - ret = wc_ecc_set_rng(&userA, &gRng); + ret = wc_ecc_set_rng(userA, &gRng); if (ret != 0) { goto exit; } - ret = wc_ecc_set_rng(&userB, &gRng); + ret = wc_ecc_set_rng(userB, &gRng); if (ret != 0) { goto exit; } #endif - ret = wc_ecc_make_key_ex(&gRng, keySize, &userA, curveId); + ret = wc_ecc_make_key_ex(&gRng, keySize, userA, curveId); #ifdef WOLFSSL_ASYNC_CRYPT - ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) goto exit; - ret = wc_ecc_make_key_ex(&gRng, keySize, &userB, curveId); + ret = wc_ecc_make_key_ex(&gRng, keySize, userB, curveId); #ifdef WOLFSSL_ASYNC_CRYPT - ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE); + ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE); #endif if (ret != 0) goto exit; - for (i = 0; i < (int)sizeof(msg); i++) { + for (i = 0; i < BENCH_ECCENCRYPT_MSG_SIZE; i++) { msg[i] = (byte)i; } @@ -7527,7 +7713,7 @@ void bench_eccEncrypt(int curveId) do { for (i = 0; i < ntimes; i++) { /* encrypt msg to B */ - ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), + ret = wc_ecc_encrypt(userA, userB, msg, BENCH_ECCENCRYPT_MSG_SIZE, out, &outSz, NULL); if (ret != 0) { printf("wc_ecc_encrypt failed! %d\n", ret); @@ -7546,7 +7732,7 @@ exit_enc: do { for (i = 0; i < ntimes; i++) { /* decrypt msg from A */ - ret = wc_ecc_decrypt(&userB, &userA, out, outSz, bench_plain, + ret = wc_ecc_decrypt(userB, userA, out, outSz, bench_plain, &bench_plainSz, NULL); if (ret != 0) { printf("wc_ecc_decrypt failed! %d\n", ret); @@ -7561,8 +7747,25 @@ exit_dec: exit: /* cleanup */ - wc_ecc_free(&userB); - wc_ecc_free(&userA); +#ifdef WOLFSSL_SMALL_STACK + if (userA) { + wc_ecc_free(userA); + XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + } + if (userB) { + wc_ecc_free(userB); + XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + } + if (msg) + XFREE(msg, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (out) + XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (name) + XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); +#else + wc_ecc_free(userB); + wc_ecc_free(userA); +#endif } #endif #endif /* HAVE_ECC */ @@ -7928,7 +8131,7 @@ void bench_eccsiPairGen(void) const char**desc = bench_desc_words[lng_index]; mp_int ssk; ecc_point* pvt; - byte id[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte id[] = { 0x01, 0x23, 0x34, 0x45 }; int ret; (void)mp_init(&ssk); @@ -7966,7 +8169,7 @@ void bench_eccsiValidate(void) const char**desc = bench_desc_words[lng_index]; mp_int ssk; ecc_point* pvt; - byte id[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte id[] = { 0x01, 0x23, 0x34, 0x45 }; int valid; int ret; @@ -8006,8 +8209,8 @@ void bench_eccsi(void) const char**desc = bench_desc_words[lng_index]; mp_int ssk; ecc_point* pvt; - byte id[] = { 0x01, 0x23, 0x34, 0x45 }; - byte msg[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte id[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte msg[] = { 0x01, 0x23, 0x34, 0x45 }; byte hash[WC_SHA256_DIGEST_SIZE]; byte hashSz = (byte)sizeof(hash); byte sig[257]; @@ -8098,7 +8301,7 @@ void bench_sakkeRskGen(void) int i, count; const char**desc = bench_desc_words[lng_index]; ecc_point* rsk; - byte id[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte id[] = { 0x01, 0x23, 0x34, 0x45 }; int ret; rsk = wc_ecc_new_point(); @@ -8132,7 +8335,7 @@ void bench_sakkeValidate(void) int i, count; const char**desc = bench_desc_words[lng_index]; ecc_point* rsk; - byte id[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte id[] = { 0x01, 0x23, 0x34, 0x45 }; int valid; int ret; @@ -8168,8 +8371,9 @@ void bench_sakke(void) int i, count; const char**desc = bench_desc_words[lng_index]; ecc_point* rsk; - byte id[] = { 0x01, 0x23, 0x34, 0x45 }; - byte ssv[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte id[] = { 0x01, 0x23, 0x34, 0x45 }; + static const byte ssv_init[] = { 0x01, 0x23, 0x34, 0x45 }; + byte ssv[sizeof(ssv_init)]; byte derSSV[sizeof(ssv)]; byte auth[257]; word16 authSz = sizeof(auth); @@ -8179,6 +8383,8 @@ void bench_sakke(void) byte* iTable = NULL; word32 iTableLen = 0; + XMEMCPY(ssv, ssv_init, sizeof ssv); + rsk = wc_ecc_new_point(); (void)wc_InitSakkeKey_ex(&genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID); (void)wc_MakeSakkeKey(&genKey, &gRng); @@ -8910,8 +9116,17 @@ void bench_sphincsKeySign(byte level, byte optim) (cur_stime - start_stime) / (cur_utime - start_utime); if (stime_utime_ratio > .1) printf("%swarning, " - "excessive system time ratio for %s%s (%.3f%%).\n", - err_prefix, desc, desc_extra, stime_utime_ratio * 100.0); + "excessive system time ratio for %s%s (" FLT_FMT_PREC "%%).\n", + err_prefix, desc, desc_extra, FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0)); + } + +#elif defined(WOLFSSL_LINUXKM) + + double current_time(int reset) + { + (void)reset; + u64 ns = ktime_get_ns(); + return (double)ns / 1000000000.0; } #else @@ -8983,7 +9198,7 @@ static void print_alg(const char* str, int* line) { const char* const ident = " "; if (*line == 0) { - fputs(ident, stdout); + printf("%s", ident); *line = (int)XSTRLEN(ident); } printf(" %s", str);