@@ -1124,6 +1124,16 @@ static word64 Entropy_GetSample(void)
11241124 word64 now ;
11251125 word64 ret ;
11261126
1127+ #ifdef HAVE_FIPS
1128+ /* First sample must be disregard when in FIPS. */
1129+ if (entropy_last_time == 0 ) {
1130+ /* Get sample which triggers CAST in FIPS mode. */
1131+ Entropy_MemUse ();
1132+ /* Start entropy time after CASTs. */
1133+ entropy_last_time = Entropy_TimeHiRes ();
1134+ }
1135+ #endif
1136+
11271137 /* Use memory such that it will take an unpredictable amount of time. */
11281138 Entropy_MemUse ();
11291139
@@ -1225,6 +1235,7 @@ static int Entropy_HealthTest_Repetition(byte noise)
12251235 if (!rep_have_prev ) {
12261236 rep_prev_noise = noise ;
12271237 rep_have_prev = 1 ;
1238+ rep_cnt = 1 ;
12281239 }
12291240 /* Check whether this sample matches last. */
12301241 else if (noise == rep_prev_noise ) {
@@ -1308,7 +1319,8 @@ static int Entropy_HealthTest_Proportion(byte noise)
13081319{
13091320 int ret = 0 ;
13101321
1311- /* Need at least 512-1 samples to test with. */
1322+ /* Need 511 samples in queue to test with - keep adding while we have less.
1323+ */
13121324 if (prop_total < PROP_WINDOW_SIZE - 1 ) {
13131325 /* Store sample at last position in circular queue. */
13141326 prop_samples [prop_last ++ ] = noise ;
@@ -1318,19 +1330,18 @@ static int Entropy_HealthTest_Proportion(byte noise)
13181330 prop_total ++ ;
13191331 }
13201332 else {
1333+ /* We have 511 samples in queue. */
13211334 /* Get first value in queue - value to test. */
13221335 byte val = (byte )prop_samples [prop_first ];
1323- /* Store new sample in queue. */
1336+
1337+ /* Store new sample at end of queue - 512 samples in queue. */
13241338 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 ;
13271339 /* Update last index now that we have added new sample to queue. */
13281340 prop_last = (prop_last + 1 ) % PROP_WINDOW_SIZE ;
1329- /* Removed sample from queue - remove count. */
1330- prop_cnt [val ]-- ;
13311341 /* Added sample to queue - add count. */
13321342 prop_cnt [noise ]++ ;
1333- /* Check whether removed value has too many repetitions in queue. */
1343+
1344+ /* Check whether first value has too many repetitions in queue. */
13341345 if (prop_cnt [val ] >= PROP_CUTOFF ) {
13351346 #ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
13361347 fprintf (stderr , "PROPORTION FAILED: %d %d\n" , val , prop_cnt [val ]);
@@ -1339,6 +1350,13 @@ static int Entropy_HealthTest_Proportion(byte noise)
13391350 /* Error code returned. */
13401351 ret = ENTROPY_APT_E ;
13411352 }
1353+ else {
1354+ /* Return to 511 samples in queue. */
1355+ /* Update first index to remove first sample from the queue. */
1356+ prop_first = (prop_first + 1 ) % PROP_WINDOW_SIZE ;
1357+ /* Removed first sample from queue - remove count. */
1358+ prop_cnt [val ]-- ;
1359+ }
13421360 }
13431361
13441362 return ret ;
0 commit comments