@@ -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 ) {
@@ -1258,7 +1269,7 @@ static int Entropy_HealthTest_Repetition(byte noise)
12581269/* SP800-90b 4.4.2 - Adaptive Proportion Test
12591270 * Note 10
12601271 * C = 1 + CRITBINOM(W, power(2,( -H)),1-alpha)
1261- * alpa = 2^-30 = POWER(2,-30), H = 1, W = 512
1272+ * alpha = 2^-30 = POWER(2,-30), H = 1, W = 512
12621273 * C = 1 + CRITBINOM(512, 0.5, 1-POWER(2,-30)) = 1 + 324 = 325
12631274 */
12641275#define PROP_CUTOFF 325
@@ -1308,8 +1319,9 @@ static int Entropy_HealthTest_Proportion(byte noise)
13081319{
13091320 int ret = 0 ;
13101321
1311- /* Need at least 512-1 samples to test with. */
1312- if (prop_total < PROP_WINDOW_SIZE - 1 ) {
1322+ /* Need minimum samples in queue to test with - keep adding while we have
1323+ * less. */
1324+ if (prop_total < PROP_CUTOFF - 1 ) {
13131325 /* Store sample at last position in circular queue. */
13141326 prop_samples [prop_last ++ ] = noise ;
13151327 /* Update count of seen value based on new sample. */
@@ -1318,27 +1330,32 @@ static int Entropy_HealthTest_Proportion(byte noise)
13181330 prop_total ++ ;
13191331 }
13201332 else {
1321- /* Get first value in queue - value to test. */
1322- byte val = (byte )prop_samples [prop_first ];
1323- /* Store new sample in queue. */
1333+ /* We have at least a minimum set of samples in queue. */
1334+ /* Store new sample at end of queue. */
13241335 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 ;
13271336 /* Update last index now that we have added new sample to queue. */
13281337 prop_last = (prop_last + 1 ) % PROP_WINDOW_SIZE ;
1329- /* Removed sample from queue - remove count. */
1330- prop_cnt [val ]-- ;
13311338 /* Added sample to queue - add count. */
13321339 prop_cnt [noise ]++ ;
1333- /* Check whether removed value has too many repetitions in queue. */
1334- if (prop_cnt [val ] >= PROP_CUTOFF ) {
1340+
1341+ /* Check whether first value has too many repetitions in queue. */
1342+ if (prop_cnt [noise ] >= PROP_CUTOFF ) {
13351343 #ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
1336- fprintf (stderr , "PROPORTION FAILED: %d %d\n" , val , prop_cnt [val ]);
1344+ fprintf (stderr , "PROPORTION FAILED: %d %d\n" , val , prop_cnt [noise ]);
13371345 #endif
13381346 Entropy_HealthTest_Proportion_Reset ();
13391347 /* Error code returned. */
13401348 ret = ENTROPY_APT_E ;
13411349 }
1350+ else if (prop_total == PROP_WINDOW_SIZE ) {
1351+ /* Return to 511 samples in queue. */
1352+ /* Get first value in queue - value to test. */
1353+ byte val = (byte )prop_samples [prop_first ];
1354+ /* Update first index to remove first sample from the queue. */
1355+ prop_first = (prop_first + 1 ) % PROP_WINDOW_SIZE ;
1356+ /* Removed first sample from queue - remove count. */
1357+ prop_cnt [val ]-- ;
1358+ }
13421359 }
13431360
13441361 return ret ;
@@ -1371,6 +1388,10 @@ static int Entropy_HealthTest_Startup(void)
13711388#ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE
13721389 fprintf (stderr , "STARTUP HEALTH TEST\n" );
13731390#endif
1391+
1392+ /* Reset cached values before testing. */
1393+ Entropy_HealthTest_Reset ();
1394+
13741395 /* Fill initial sample buffer with noise. */
13751396 Entropy_GetNoise (initial , ENTROPY_INITIAL_COUNT );
13761397 /* Health check initial noise. */
@@ -1537,8 +1558,6 @@ int wc_Entropy_OnDemandTest(void)
15371558 }
15381559
15391560 if (ret == 0 ) {
1540- /* Reset health test state for startup test. */
1541- Entropy_HealthTest_Reset ();
15421561 /* Perform startup tests. */
15431562 ret = Entropy_HealthTest_Startup ();
15441563 }
0 commit comments