Skip to content

Commit 363bd73

Browse files
committed
add DRBG reseed boundary test
1 parent 3ce4742 commit 363bd73

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

tests/api/test_random.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,63 @@ 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+
word32 startCtr = drbg->reseedCtr;
114+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
115+
if (drbg->reseedCtr == startCtr + 1) {
116+
drbg->reseedCtr = WC_RESEED_INTERVAL - 1;
117+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
118+
ExpectTrue(drbg->reseedCtr == WC_RESEED_INTERVAL);
119+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
120+
ExpectTrue(drbg->reseedCtr == 2);
121+
drbgChecked = 1;
122+
}
123+
}
124+
}
125+
#endif
126+
#ifdef WOLFSSL_DRBG_SHA512
127+
if (!drbgChecked && rng.drbgType == WC_DRBG_SHA512) {
128+
struct DRBG_SHA512_internal* drbg =
129+
(struct DRBG_SHA512_internal*)rng.drbg512;
130+
if (drbg != NULL && rng.status == WC_DRBG_OK) {
131+
word64 startCtr = drbg->reseedCtr;
132+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
133+
if (drbg->reseedCtr == startCtr + 1) {
134+
drbg->reseedCtr = WC_RESEED_INTERVAL - 1;
135+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
136+
ExpectTrue(drbg->reseedCtr == WC_RESEED_INTERVAL);
137+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, out, sizeof(out)), 0);
138+
ExpectTrue(drbg->reseedCtr == 2);
139+
drbgChecked = 1;
140+
}
141+
}
142+
}
143+
#endif
144+
/* Ensure the test actually exercised a DRBG; a silently-skipped body
145+
* (e.g. due to an unrecognised drbgType) would otherwise pass with no
146+
* coverage. */
147+
ExpectIntEQ(drbgChecked, 1);
148+
149+
DoExpectIntEQ(wc_FreeRng(&rng), 0);
150+
#endif
151+
return EXPECT_RESULT();
152+
}
153+
97154
int test_wc_RNG_GenerateBlock(void)
98155
{
99156
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)