@@ -507,40 +507,28 @@ static void SeedSlow(CSHA512& hasher) noexcept
507
507
}
508
508
509
509
/* * Extract entropy from rng, strengthen it, and feed it into hasher. */
510
- static void SeedStrengthen (CSHA512& hasher, RNGState& rng) noexcept
510
+ static void SeedStrengthen (CSHA512& hasher, RNGState& rng, int microseconds ) noexcept
511
511
{
512
- static std::atomic<int64_t > last_strengthen{0 };
513
- int64_t last_time = last_strengthen.load ();
514
- int64_t current_time = GetTimeMicros ();
515
- if (current_time > last_time + 60000000 ) { // Only run once a minute
516
- // Generate 32 bytes of entropy from the RNG, and a copy of the entropy already in hasher.
517
- unsigned char strengthen_seed[32 ];
518
- rng.MixExtract (strengthen_seed, sizeof (strengthen_seed), CSHA512 (hasher), false );
519
- // Strengthen it for 10ms (100ms on first run), and feed it into hasher.
520
- Strengthen (strengthen_seed, last_time == 0 ? 100000 : 10000 , hasher);
521
- last_strengthen = current_time;
522
- }
512
+ // Generate 32 bytes of entropy from the RNG, and a copy of the entropy already in hasher.
513
+ unsigned char strengthen_seed[32 ];
514
+ rng.MixExtract (strengthen_seed, sizeof (strengthen_seed), CSHA512 (hasher), false );
515
+ // Strengthen the seed, and feed it into hasher.
516
+ Strengthen (strengthen_seed, microseconds, hasher);
523
517
}
524
518
525
- static void SeedSleep (CSHA512& hasher, RNGState& rng)
519
+ static void SeedPeriodic (CSHA512& hasher, RNGState& rng)
526
520
{
527
521
// Everything that the 'fast' seeder includes
528
522
SeedFast (hasher);
529
523
530
524
// High-precision timestamp
531
525
SeedTimestamp (hasher);
532
526
533
- // Sleep for 1ms
534
- MilliSleep (1 );
535
-
536
- // High-precision timestamp after sleeping (as we commit to both the time before and after, this measures the delay)
537
- SeedTimestamp (hasher);
538
-
539
- // Dynamic environment data (performance monitoring, ...; once every 10 minutes)
527
+ // Dynamic environment data (performance monitoring, ...)
540
528
RandAddDynamicEnv (hasher);
541
529
542
- // Strengthen every minute
543
- SeedStrengthen (hasher, rng);
530
+ // Strengthen for 10 ms
531
+ SeedStrengthen (hasher, rng, 10000 );
544
532
}
545
533
546
534
static void SeedStartup (CSHA512& hasher, RNGState& rng) noexcept
@@ -551,20 +539,20 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
551
539
// Everything that the 'slow' seeder includes.
552
540
SeedSlow (hasher);
553
541
554
- // Dynamic environment data
542
+ // Dynamic environment data (performance monitoring, ...)
555
543
RandAddDynamicEnv (hasher);
556
544
557
545
// Static environment data
558
546
RandAddStaticEnv (hasher);
559
547
560
- // Strengthen
561
- SeedStrengthen (hasher, rng);
548
+ // Strengthen for 100 ms
549
+ SeedStrengthen (hasher, rng, 100000 );
562
550
}
563
551
564
552
enum class RNGLevel {
565
553
FAST, // !< Automatically called by GetRandBytes
566
554
SLOW, // !< Automatically called by GetStrongRandBytes
567
- SLEEP , // !< Called by RandAddSeedSleep ()
555
+ PERIODIC , // !< Called by RandAddPeriodic ()
568
556
};
569
557
570
558
static void ProcRand (unsigned char * out, int num, RNGLevel level)
@@ -582,8 +570,8 @@ static void ProcRand(unsigned char* out, int num, RNGLevel level)
582
570
case RNGLevel::SLOW:
583
571
SeedSlow (hasher);
584
572
break ;
585
- case RNGLevel::SLEEP :
586
- SeedSleep (hasher, rng);
573
+ case RNGLevel::PERIODIC :
574
+ SeedPeriodic (hasher, rng);
587
575
break ;
588
576
}
589
577
@@ -606,7 +594,7 @@ static void ProcRand(unsigned char* out, int num, RNGLevel level)
606
594
607
595
void GetRandBytes (unsigned char * buf, int num) noexcept { ProcRand (buf, num, RNGLevel::FAST); }
608
596
void GetStrongRandBytes (unsigned char * buf, int num) noexcept { ProcRand (buf, num, RNGLevel::SLOW); }
609
- void RandAddSeedSleep () { ProcRand (nullptr , 0 , RNGLevel::SLEEP ); }
597
+ void RandAddPeriodic () { ProcRand (nullptr , 0 , RNGLevel::PERIODIC ); }
610
598
611
599
bool g_mock_deterministic_tests{false };
612
600
0 commit comments