Skip to content

Commit 63c00ae

Browse files
committed
add DRBG reseed boundary test
1 parent 3ce4742 commit 63c00ae

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

tests/api/test_random.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,67 @@ int test_wc_RNG_GenerateBlock_Reseed(void)
9494
return EXPECT_RESULT();
9595
}
9696

97+
int test_wc_RNG_ReseedBoundary(void)
98+
{
99+
EXPECT_DECLS;
100+
#if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK) && \
101+
!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
102+
WC_RNG rng;
103+
byte out[32];
104+
int drbgChecked = 0;
105+
106+
XMEMSET(&rng, 0, sizeof(WC_RNG));
107+
ExpectIntEQ(wc_InitRng(&rng), 0);
108+
109+
#ifndef NO_SHA256
110+
if (rng.drbgType == WC_DRBG_SHA256) {
111+
struct DRBG_internal* drbg = (struct DRBG_internal*)rng.drbg;
112+
if (drbg != NULL && rng.status == WC_DRBG_OK) {
113+
#ifdef WORD64_AVAILABLE
114+
word64 startCtr = drbg->reseedCtr;
115+
#else
116+
word32 startCtr = drbg->reseedCtr;
117+
#endif
118+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
119+
if (drbg->reseedCtr == startCtr + 1) {
120+
drbg->reseedCtr = WC_RESEED_INTERVAL - 1;
121+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
122+
ExpectTrue(drbg->reseedCtr == WC_RESEED_INTERVAL);
123+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
124+
ExpectTrue(drbg->reseedCtr == 2);
125+
drbgChecked = 1;
126+
}
127+
}
128+
}
129+
#endif
130+
#ifdef WOLFSSL_DRBG_SHA512
131+
if (!drbgChecked && rng.drbgType == WC_DRBG_SHA512) {
132+
struct DRBG_SHA512_internal* drbg =
133+
(struct DRBG_SHA512_internal*)rng.drbg512;
134+
if (drbg != NULL && rng.status == WC_DRBG_OK) {
135+
word64 startCtr = drbg->reseedCtr;
136+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
137+
if (drbg->reseedCtr == startCtr + 1) {
138+
drbg->reseedCtr = WC_RESEED_INTERVAL - 1;
139+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
140+
ExpectTrue(drbg->reseedCtr == WC_RESEED_INTERVAL);
141+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
142+
ExpectTrue(drbg->reseedCtr == 2);
143+
drbgChecked = 1;
144+
}
145+
}
146+
}
147+
#endif
148+
/* Ensure the test actually exercised a DRBG; a silently-skipped body
149+
* (e.g. due to an unrecognised drbgType) would otherwise pass with no
150+
* coverage. */
151+
ExpectIntEQ(drbgChecked, 1);
152+
153+
DoExpectIntEQ(wc_FreeRng(&rng), 0);
154+
#endif
155+
return EXPECT_RESULT();
156+
}
157+
97158
int test_wc_RNG_GenerateBlock(void)
98159
{
99160
EXPECT_DECLS;

tests/api/test_random.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
int test_wc_InitRng(void);
2828
int test_wc_RNG_GenerateBlock_Reseed(void);
29+
int test_wc_RNG_ReseedBoundary(void);
2930
int test_wc_RNG_GenerateBlock(void);
3031
int test_wc_RNG_GenerateByte(void);
3132
int test_wc_InitRngNonce(void);
@@ -40,6 +41,7 @@ int test_wc_RNG_HealthTest_SHA512(void);
4041
#define TEST_RANDOM_DECLS \
4142
TEST_DECL_GROUP("random", test_wc_InitRng), \
4243
TEST_DECL_GROUP("random", test_wc_RNG_GenerateBlock_Reseed), \
44+
TEST_DECL_GROUP("random", test_wc_RNG_ReseedBoundary), \
4345
TEST_DECL_GROUP("random", test_wc_RNG_GenerateBlock), \
4446
TEST_DECL_GROUP("random", test_wc_RNG_GenerateByte), \
4547
TEST_DECL_GROUP("random", test_wc_InitRngNonce), \

0 commit comments

Comments
 (0)