From 1a5064cf8ce61d7ca77939f3ccbc0ba73ce77d5b Mon Sep 17 00:00:00 2001 From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:57:00 -0700 Subject: [PATCH 1/2] add full support to wolfcrypt tests for random.c cryptocbs --- wolfcrypt/src/random.c | 42 ++++++++++++++++++++++++++++++++++++++ wolfcrypt/test/test.c | 2 +- wolfssl/wolfcrypt/random.h | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 2b5f927ea..ee8d9eb4e 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -1770,6 +1770,24 @@ WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap) } +WOLFSSL_ABI +WC_RNG* wc_rng_new_ex(byte* nonce, word32 nonceSz, void* heap, int devId) +{ + WC_RNG* rng; + + rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG); + if (rng) { + int error = _InitRng(rng, nonce, nonceSz, heap, devId) != 0; + if (error) { + XFREE(rng, heap, DYNAMIC_TYPE_RNG); + rng = NULL; + } + } + + return rng; +} + + WOLFSSL_ABI void wc_rng_free(WC_RNG* rng) { @@ -3777,6 +3795,28 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) #elif defined(NO_DEV_RANDOM) + /* Allow bare-metal targets to use cryptoCb as seed provider */ + #if defined(WOLF_CRYPTO_CB) + + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + int ret = WC_HW_E; + + #ifndef WOLF_CRYPTO_CB_FIND + if (os->devId != INVALID_DEVID) + #endif + { + ret = wc_CryptoCb_RandomSeed(os, output, sz); + if (ret == CRYPTOCB_UNAVAILABLE) { + ret = WC_HW_E; + } + } + + return ret; + } + + #else /* defined(WOLF_CRYPTO_CB)*/ + #error "you need to write an os specific wc_GenerateSeed() here" /* @@ -3786,6 +3826,8 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) } */ + #endif /* !defined(WOLF_CRYPTO_CB) */ + #else /* may block */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 0b1a93c59..9b78a9089 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -15282,7 +15282,7 @@ static wc_test_ret_t random_rng_test(void) { byte nonce[8] = { 0 }; /* Test dynamic RNG. */ - rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT); + rng = wc_rng_new_ex(nonce, (word32)sizeof(nonce), HEAP_HINT, devId); if (rng == NULL) return WC_TEST_RET_ENC_ERRNO; diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index 8cd599bdd..d50cad8d8 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -206,6 +206,7 @@ WOLFSSL_API int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz); WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap); +WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new_ex(byte* nonce, word32 nonceSz, void* heap, int devId); WOLFSSL_ABI WOLFSSL_API void wc_rng_free(WC_RNG* rng); From 55421a11b9e634013ad36d737ea14cf44f11e43c Mon Sep 17 00:00:00 2001 From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com> Date: Tue, 27 Feb 2024 10:11:21 -0700 Subject: [PATCH 2/2] review: removed WOLFSSL_ABI from and refactored args for wc_rng_new_ex, updated tests --- wolfcrypt/src/random.c | 24 +++++++++++++----------- wolfcrypt/test/test.c | 22 +++++++++++++++++++--- wolfssl/wolfcrypt/random.h | 6 ++++-- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index ee8d9eb4e..e74320872 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -1770,21 +1770,23 @@ WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap) } -WOLFSSL_ABI -WC_RNG* wc_rng_new_ex(byte* nonce, word32 nonceSz, void* heap, int devId) +int wc_rng_new_ex(WC_RNG **rng, byte* nonce, word32 nonceSz, + void* heap, int devId) { - WC_RNG* rng; + int ret; - rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG); - if (rng) { - int error = _InitRng(rng, nonce, nonceSz, heap, devId) != 0; - if (error) { - XFREE(rng, heap, DYNAMIC_TYPE_RNG); - rng = NULL; - } + *rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG); + if (*rng == NULL) { + return MEMORY_E; } - return rng; + ret = _InitRng(*rng, nonce, nonceSz, heap, devId); + if (ret != 0) { + XFREE(*rng, heap, DYNAMIC_TYPE_RNG); + *rng = NULL; + } + + return ret; } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 9b78a9089..82c01dab1 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -15281,14 +15281,30 @@ static wc_test_ret_t random_rng_test(void) #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) { byte nonce[8] = { 0 }; - /* Test dynamic RNG. */ - rng = wc_rng_new_ex(nonce, (word32)sizeof(nonce), HEAP_HINT, devId); + + /* Test dynamic RNG */ + rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT); if (rng == NULL) return WC_TEST_RET_ENC_ERRNO; ret = _rng_test(rng, WC_TEST_RET_ENC_NC); - wc_rng_free(rng); + rng = NULL; + + if (ret != 0) + return ret; + + /* Test dynamic RNG using extended API */ + ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce), + HEAP_HINT, devId); + if (ret != 0) + return WC_TEST_RET_ENC_EC(ret); + + ret = _rng_test(rng, WC_TEST_RET_ENC_NC); + wc_rng_free(rng); + + if (ret != 0) + return ret; } #endif diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index d50cad8d8..d4ab8e338 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -205,8 +205,10 @@ WOLFSSL_API int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz); #endif /* HAVE_WNR */ -WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap); -WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new_ex(byte* nonce, word32 nonceSz, void* heap, int devId); +WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, + void* heap); +WOLFSSL_API int wc_rng_new_ex(WC_RNG **rng, byte* nonce, word32 nonceSz, + void* heap, int devId); WOLFSSL_ABI WOLFSSL_API void wc_rng_free(WC_RNG* rng);