diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index f77976be9..31b2e7bea 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -84,6 +84,7 @@ #ifdef HAVE_NTRU #include "libntruencrypt/ntru_crypto.h" #endif +#include #if defined(WOLFSSL_MDK_ARM) extern FILE * wolfSSL_fopen(const char *fname, const char *mode) ; @@ -174,6 +175,7 @@ void bench_ed25519KeySign(void); void bench_ntru(void); void bench_ntruKeyGen(void); #endif +void bench_rng(void); double current_time(int); @@ -290,6 +292,7 @@ int benchmark_test(void *args) } #endif + bench_rng(); #ifndef NO_AES #ifdef HAVE_AES_CBC bench_aes(0); @@ -427,6 +430,62 @@ enum BenchmarkBounds { static const char blockType[] = "megs"; /* used in printf output */ #endif +void bench_rng(void) +{ + int ret, i; + double start, total, persec; + int pos, len, remain; +#ifndef HAVE_LOCAL_RNG + WC_RNG rng; +#endif + +#ifndef HAVE_LOCAL_RNG + ret = wc_InitRng(&rng); + if (ret < 0) { + printf("InitRNG failed\n"); + return; + } +#endif + + start = current_time(1); + BEGIN_INTEL_CYCLES + + for(i = 0; i < numBlocks; i++) { + /* Split request to handle large RNG request */ + pos = 0; + remain = (int)sizeof(plain); + while (remain > 0) { + len = remain; + if (len > RNG_MAX_BLOCK_LEN) + len = RNG_MAX_BLOCK_LEN; + ret = wc_RNG_GenerateBlock(&rng, &plain[pos], len); + if (ret < 0) { + printf("wc_RNG_GenerateBlock failed %d\n", ret); + break; + } + remain -= len; + pos += len; + } + } + + END_INTEL_CYCLES + total = current_time(0) - start; + + persec = 1 / total * numBlocks; +#ifdef BENCH_EMBEDDED + /* since using kB, convert to MB/s */ + persec = persec / 1024; +#endif + printf("RNG %d %s took %5.3f seconds, %8.3f MB/s", numBlocks, + blockType, total, persec); + SHOW_INTEL_CYCLES + printf("\n"); + +#ifndef HAVE_LOCAL_RNG + wc_FreeRng(&rng); +#endif +} + #ifndef NO_AES diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 2b5f40bfc..124e18281 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -194,6 +194,11 @@ int wc_FreeRng(WC_RNG* rng) #define DRBG_FAILED 2 #define DRBG_CONT_FAILED 3 +/* Verify max gen block len */ +#if RNG_MAX_BLOCK_LEN > MAX_REQUEST_LEN + #error RNG_MAX_BLOCK_LEN is larger than NIST DBRG max request length +#endif + enum { drbgInitC = 0, @@ -533,7 +538,7 @@ int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz) { int ret; - if (rng == NULL || output == NULL || sz > MAX_REQUEST_LEN) + if (rng == NULL || output == NULL || sz > RNG_MAX_BLOCK_LEN) return BAD_FUNC_ARG; if (rng->status != DRBG_OK) diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index cfcc671a2..421203dd9 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -35,6 +35,9 @@ extern "C" { #endif +/* Maximum generate block length */ +#define RNG_MAX_BLOCK_LEN (0x10000) + #ifndef HAVE_FIPS /* avoid redefining structs and macros */ #if defined(WOLFSSL_FORCE_RC4_DRBG) && defined(NO_RC4) #error Cannot have WOLFSSL_FORCE_RC4_DRBG and NO_RC4 defined.