@@ -1129,6 +1129,16 @@ static word64 Entropy_GetSample(void)
11291129 word64 now ;
11301130 word64 ret ;
11311131
1132+ #ifdef HAVE_FIPS
1133+ /* First sample must be disregard when in FIPS. */
1134+ if (entropy_last_time == 0 ) {
1135+ /* Get sample which triggers CAST in FIPS mode. */
1136+ Entropy_MemUse ();
1137+ /* Start entropy time after CASTs. */
1138+ entropy_last_time = Entropy_TimeHiRes ();
1139+ }
1140+ #endif
1141+
11321142 /* Use memory such that it will take an unpredictable amount of time. */
11331143 Entropy_MemUse ();
11341144
@@ -1230,6 +1240,7 @@ static int Entropy_HealthTest_Repetition(byte noise)
12301240 if (!rep_have_prev ) {
12311241 rep_prev_noise = noise ;
12321242 rep_have_prev = 1 ;
1243+ rep_cnt = 1 ;
12331244 }
12341245 /* Check whether this sample matches last. */
12351246 else if (noise == rep_prev_noise ) {
@@ -1263,7 +1274,7 @@ static int Entropy_HealthTest_Repetition(byte noise)
12631274/* SP800-90b 4.4.2 - Adaptive Proportion Test
12641275 * Note 10
12651276 * C = 1 + CRITBINOM(W, power(2,( -H)),1-alpha)
1266- * alpa = 2^-30 = POWER(2,-30), H = 1, W = 512
1277+ * alpha = 2^-30 = POWER(2,-30), H = 1, W = 512
12671278 * C = 1 + CRITBINOM(512, 0.5, 1-POWER(2,-30)) = 1 + 324 = 325
12681279 */
12691280#define PROP_CUTOFF 325
@@ -1313,8 +1324,9 @@ static int Entropy_HealthTest_Proportion(byte noise)
13131324{
13141325 int ret = 0 ;
13151326
1316- /* Need at least 512-1 samples to test with. */
1317- if (prop_total < PROP_WINDOW_SIZE - 1 ) {
1327+ /* Need minimum samples in queue to test with - keep adding while we have
1328+ * less. */
1329+ if (prop_total < PROP_CUTOFF - 1 ) {
13181330 /* Store sample at last position in circular queue. */
13191331 prop_samples [prop_last ++ ] = noise ;
13201332 /* Update count of seen value based on new sample. */
@@ -1323,27 +1335,32 @@ static int Entropy_HealthTest_Proportion(byte noise)
13231335 prop_total ++ ;
13241336 }
13251337 else {
1326- /* Get first value in queue - value to test. */
1327- byte val = (byte )prop_samples [prop_first ];
1328- /* Store new sample in queue. */
1338+ /* We have at least a minimum set of samples in queue. */
1339+ /* Store new sample at end of queue. */
13291340 prop_samples [prop_last ] = noise ;
1330- /* Update first index now that we have removed in from the queue. */
1331- prop_first = (prop_first + 1 ) % PROP_WINDOW_SIZE ;
13321341 /* Update last index now that we have added new sample to queue. */
13331342 prop_last = (prop_last + 1 ) % PROP_WINDOW_SIZE ;
1334- /* Removed sample from queue - remove count. */
1335- prop_cnt [val ]-- ;
13361343 /* Added sample to queue - add count. */
13371344 prop_cnt [noise ]++ ;
1338- /* Check whether removed value has too many repetitions in queue. */
1339- if (prop_cnt [val ] >= PROP_CUTOFF ) {
1345+
1346+ /* Check whether first value has too many repetitions in queue. */
1347+ if (prop_cnt [noise ] >= PROP_CUTOFF ) {
13401348 #ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
1341- fprintf (stderr , "PROPORTION FAILED: %d %d\n" , val , prop_cnt [val ]);
1349+ fprintf (stderr , "PROPORTION FAILED: %d %d\n" , val , prop_cnt [noise ]);
13421350 #endif
13431351 Entropy_HealthTest_Proportion_Reset ();
13441352 /* Error code returned. */
13451353 ret = ENTROPY_APT_E ;
13461354 }
1355+ else if (prop_total == PROP_WINDOW_SIZE ) {
1356+ /* Return to 511 samples in queue. */
1357+ /* Get first value in queue - value to test. */
1358+ byte val = (byte )prop_samples [prop_first ];
1359+ /* Update first index to remove first sample from the queue. */
1360+ prop_first = (prop_first + 1 ) % PROP_WINDOW_SIZE ;
1361+ /* Removed first sample from queue - remove count. */
1362+ prop_cnt [val ]-- ;
1363+ }
13471364 }
13481365
13491366 return ret ;
@@ -1376,6 +1393,10 @@ static int Entropy_HealthTest_Startup(void)
13761393#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
13771394 fprintf (stderr , "STARTUP HEALTH TEST\n" );
13781395#endif
1396+
1397+ /* Reset cached values before testing. */
1398+ Entropy_HealthTest_Reset ();
1399+
13791400 /* Fill initial sample buffer with noise. */
13801401 Entropy_GetNoise (initial , ENTROPY_INITIAL_COUNT );
13811402 /* Health check initial noise. */
@@ -1542,8 +1563,6 @@ int wc_Entropy_OnDemandTest(void)
15421563 }
15431564
15441565 if (ret == 0 ) {
1545- /* Reset health test state for startup test. */
1546- Entropy_HealthTest_Reset ();
15471566 /* Perform startup tests. */
15481567 ret = Entropy_HealthTest_Startup ();
15491568 }
0 commit comments