From 1b0e5f4806bc75ceb3d2eb1f6beffe8507fa0624 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 17 Mar 2022 14:42:23 -0700 Subject: [PATCH] Allow disabling DRBG with KCAPI. Add KCAPI `/dev/hwrng` support. --- configure.ac | 4 ++-- wolfcrypt/src/random.c | 29 +++++++++++++++++++++++++++-- wolfssl/wolfcrypt/random.h | 5 +++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 72e394fec..15e753b2c 100644 --- a/configure.ac +++ b/configure.ac @@ -3888,8 +3888,8 @@ if test "x$ENABLED_HASHDRBG" = "xyes" then AM_CFLAGS="$AM_CFLAGS -DHAVE_HASHDRBG" else - # turn on Hash DRBG if FIPS is on - if test "x$ENABLED_FIPS" = "xyes" + # turn on Hash DRBG if FIPS is on (don't force on for KCAPI) + if test "x$ENABLED_FIPS" = "xyes" && test "x$ENABLED_KCAPI" = "xno" then AM_CFLAGS="$AM_CFLAGS -DHAVE_HASHDRBG" ENABLED_HASHDRBG=yes diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index d2bb66329..14e3ca202 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2786,8 +2786,33 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) return 0; } #endif - - /* End wc_GenerateSeed */ + +#if defined(CUSTOM_RAND_GENERATE_BLOCK) && defined(WOLFSSL_KCAPI) +#include +int wc_hwrng_generate_block(byte *output, word32 sz) +{ + int fd; + int len; + int ret = 0; + fd = open("/dev/hwrng", O_RDONLY); + if (fd == -1) + return OPEN_RAN_E; + while(sz) + { + len = (int)read(fd, output, sz); + if (len == -1) + { + ret = READ_RAN_E; + break; + } + sz -= len; + output += len; + } + close(fd); + return ret; +} +#endif + #endif /* WC_NO_RNG */ #endif /* HAVE_FIPS */ diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index 963596dfb..ff4ef0de5 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -99,6 +99,11 @@ * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc * extern int myRngFunc(byte* output, word32 sz); */ + #if defined(CUSTOM_RAND_GENERATE_BLOCK) && defined(WOLFSSL_KCAPI) + #undef CUSTOM_RAND_GENERATE_BLOCK + #define CUSTOM_RAND_GENERATE_BLOCK wc_hwrng_generate_block + WOLFSSL_LOCAL int wc_hwrng_generate_block(byte *output, word32 sz); + #endif #elif defined(HAVE_HASHDRBG) #ifdef NO_SHA256 #error "Hash DRBG requires SHA-256."