Skip to content

Commit 88beaec

Browse files
committed
Entropy - fix off by ones in continuous testing
rep_cnt is count of contiguous bytes with same value. First ever sample must set count to 1. Wasn't filling the cache up completely. Off by one in check for initial fill.
1 parent ecb8cb7 commit 88beaec

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

wolfcrypt/src/random.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@ static int Entropy_HealthTest_Repetition(byte noise)
12251225
if (!rep_have_prev) {
12261226
rep_prev_noise = noise;
12271227
rep_have_prev = 1;
1228+
rep_cnt = 1;
12281229
}
12291230
/* Check whether this sample matches last. */
12301231
else if (noise == rep_prev_noise) {
@@ -1308,7 +1309,7 @@ static int Entropy_HealthTest_Proportion(byte noise)
13081309
{
13091310
int ret = 0;
13101311

1311-
/* Need at least 512-1 samples to test with. */
1312+
/* Need at 511 samples to test with - keep adding while we have less. */
13121313
if (prop_total < PROP_WINDOW_SIZE - 1) {
13131314
/* Store sample at last position in circular queue. */
13141315
prop_samples[prop_last++] = noise;
@@ -1318,19 +1319,18 @@ static int Entropy_HealthTest_Proportion(byte noise)
13181319
prop_total++;
13191320
}
13201321
else {
1322+
/* We have 511 samples in queue. */
13211323
/* Get first value in queue - value to test. */
13221324
byte val = (byte)prop_samples[prop_first];
1323-
/* Store new sample in queue. */
1325+
1326+
/* Store new sample at end of queue. */
13241327
prop_samples[prop_last] = noise;
1325-
/* Update first index now that we have removed in from the queue. */
1326-
prop_first = (prop_first + 1) % PROP_WINDOW_SIZE;
13271328
/* Update last index now that we have added new sample to queue. */
13281329
prop_last = (prop_last + 1) % PROP_WINDOW_SIZE;
1329-
/* Removed sample from queue - remove count. */
1330-
prop_cnt[val]--;
13311330
/* Added sample to queue - add count. */
13321331
prop_cnt[noise]++;
1333-
/* Check whether removed value has too many repetitions in queue. */
1332+
1333+
/* Check whether first value has too many repetitions in queue. */
13341334
if (prop_cnt[val] >= PROP_CUTOFF) {
13351335
#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
13361336
fprintf(stderr, "PROPORTION FAILED: %d %d\n", val, prop_cnt[val]);
@@ -1339,6 +1339,13 @@ static int Entropy_HealthTest_Proportion(byte noise)
13391339
/* Error code returned. */
13401340
ret = ENTROPY_APT_E;
13411341
}
1342+
else {
1343+
/* Return to 511 samples in queue. */
1344+
/* Update first index to remove first sample from the queue. */
1345+
prop_first = (prop_first + 1) % PROP_WINDOW_SIZE;
1346+
/* Removed first sample from queue - remove count. */
1347+
prop_cnt[val]--;
1348+
}
13421349
}
13431350

13441351
return ret;

0 commit comments

Comments
 (0)