Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,26 @@ WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap)
}


int wc_rng_new_ex(WC_RNG **rng, byte* nonce, word32 nonceSz,
void* heap, int devId)
{
int ret;

*rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG);
if (*rng == NULL) {
return MEMORY_E;
}

ret = _InitRng(*rng, nonce, nonceSz, heap, devId);
if (ret != 0) {
XFREE(*rng, heap, DYNAMIC_TYPE_RNG);
*rng = NULL;
}

return ret;
}


WOLFSSL_ABI
void wc_rng_free(WC_RNG* rng)
{
Expand Down Expand Up @@ -3777,6 +3797,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"

/*
Expand All @@ -3786,6 +3828,8 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
}
*/

#endif /* !defined(WOLF_CRYPTO_CB) */

#else

/* may block */
Expand Down
18 changes: 17 additions & 1 deletion wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

/* 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

Expand Down
5 changes: 4 additions & 1 deletion wolfssl/wolfcrypt/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +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(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);


Expand Down