Skip to content

Commit af31fbc

Browse files
authored
Merge pull request #7271 from bigbrett/cryptocb-random-wctestfix
add full support to wolfcrypt tests for random.c cryptocbs
2 parents e64a26d + 55421a1 commit af31fbc

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

wolfcrypt/src/random.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,26 @@ WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap)
17701770
}
17711771

17721772

1773+
int wc_rng_new_ex(WC_RNG **rng, byte* nonce, word32 nonceSz,
1774+
void* heap, int devId)
1775+
{
1776+
int ret;
1777+
1778+
*rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG);
1779+
if (*rng == NULL) {
1780+
return MEMORY_E;
1781+
}
1782+
1783+
ret = _InitRng(*rng, nonce, nonceSz, heap, devId);
1784+
if (ret != 0) {
1785+
XFREE(*rng, heap, DYNAMIC_TYPE_RNG);
1786+
*rng = NULL;
1787+
}
1788+
1789+
return ret;
1790+
}
1791+
1792+
17731793
WOLFSSL_ABI
17741794
void wc_rng_free(WC_RNG* rng)
17751795
{
@@ -3777,6 +3797,28 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
37773797

37783798
#elif defined(NO_DEV_RANDOM)
37793799

3800+
/* Allow bare-metal targets to use cryptoCb as seed provider */
3801+
#if defined(WOLF_CRYPTO_CB)
3802+
3803+
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3804+
{
3805+
int ret = WC_HW_E;
3806+
3807+
#ifndef WOLF_CRYPTO_CB_FIND
3808+
if (os->devId != INVALID_DEVID)
3809+
#endif
3810+
{
3811+
ret = wc_CryptoCb_RandomSeed(os, output, sz);
3812+
if (ret == CRYPTOCB_UNAVAILABLE) {
3813+
ret = WC_HW_E;
3814+
}
3815+
}
3816+
3817+
return ret;
3818+
}
3819+
3820+
#else /* defined(WOLF_CRYPTO_CB)*/
3821+
37803822
#error "you need to write an os specific wc_GenerateSeed() here"
37813823

37823824
/*
@@ -3786,6 +3828,8 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
37863828
}
37873829
*/
37883830

3831+
#endif /* !defined(WOLF_CRYPTO_CB) */
3832+
37893833
#else
37903834

37913835
/* may block */

wolfcrypt/test/test.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15281,14 +15281,30 @@ static wc_test_ret_t random_rng_test(void)
1528115281
#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
1528215282
{
1528315283
byte nonce[8] = { 0 };
15284-
/* Test dynamic RNG. */
15284+
15285+
/* Test dynamic RNG */
1528515286
rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
1528615287
if (rng == NULL)
1528715288
return WC_TEST_RET_ENC_ERRNO;
1528815289

1528915290
ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
15291+
wc_rng_free(rng);
15292+
rng = NULL;
15293+
15294+
if (ret != 0)
15295+
return ret;
15296+
15297+
/* Test dynamic RNG using extended API */
15298+
ret = wc_rng_new_ex(&rng, nonce, (word32)sizeof(nonce),
15299+
HEAP_HINT, devId);
15300+
if (ret != 0)
15301+
return WC_TEST_RET_ENC_EC(ret);
1529015302

15303+
ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
1529115304
wc_rng_free(rng);
15305+
15306+
if (ret != 0)
15307+
return ret;
1529215308
}
1529315309
#endif
1529415310

wolfssl/wolfcrypt/random.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ WOLFSSL_API int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
205205
#endif /* HAVE_WNR */
206206

207207

208-
WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap);
208+
WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz,
209+
void* heap);
210+
WOLFSSL_API int wc_rng_new_ex(WC_RNG **rng, byte* nonce, word32 nonceSz,
211+
void* heap, int devId);
209212
WOLFSSL_ABI WOLFSSL_API void wc_rng_free(WC_RNG* rng);
210213

211214

0 commit comments

Comments
 (0)