• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5000459109

17 May 2023 07:56AM UTC coverage: 91.688% (-0.04%) from 91.723%
5000459109

push

github

77594 of 84628 relevant lines covered (91.69%)

12048253.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.74
/src/lib/entropy/rdseed/rdseed.cpp
1
/*
2
* Entropy Source Using Intel's rdseed instruction
3
* (C) 2015 Daniel Neus
4
* (C) 2015,2019 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/internal/rdseed.h>
10

11
#include <botan/internal/cpuid.h>
12

13
#include <immintrin.h>
14

15
namespace Botan {
16

17
namespace {
18

19
BOTAN_FUNC_ISA("rdseed")
20
bool read_rdseed(secure_vector<uint32_t>& seed)
×
21
   {
22
   /*
23
   * RDSEED is not guaranteed to generate an output within any specific number
24
   * of attempts. However in testing on a Skylake system, with all hyperthreads
25
   * occupied in tight RDSEED loops, RDSEED will still usually succeed in under
26
   * 150 attempts. The maximum ever seen was 230 attempts until success. When
27
   * idle, RDSEED usually succeeds in 1 or 2 attempts.
28
   *
29
   * We set an upper bound of 1024 attempts, because it is possible that due
30
   * to firmware issue RDSEED is simply broken and never succeeds. We do not
31
   * want to loop forever in that case. If we exceed that limit, then we assume
32
   * the hardware is actually just broken, and stop the poll.
33
   */
34
   const size_t RDSEED_RETRIES = 1024;
×
35

36
   for(size_t i = 0; i != RDSEED_RETRIES; ++i)
×
37
      {
38
      uint32_t r = 0;
×
39
      int cf = 0;
×
40

41
#if defined(BOTAN_USE_GCC_INLINE_ASM)
42
      asm("rdseed %0; adcl $0,%1" :
×
43
          "=r" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
44
#else
45
      cf = _rdseed32_step(&r);
46
#endif
47

48
      if(1 == cf)
×
49
         {
50
         seed.push_back(r);
×
51
         return true;
×
52
         }
53

54
      // Intel suggests pausing if RDSEED fails.
55
      _mm_pause();
×
56
      }
57

58
   return false; // failed to produce an output after many attempts
59
   }
60

61
}
62

63
size_t Intel_Rdseed::poll(RandomNumberGenerator& rng)
4✔
64
   {
65
   const size_t RDSEED_BYTES = 1024;
4✔
66
   static_assert(RDSEED_BYTES % 4 == 0, "Bad RDSEED configuration");
4✔
67

68
   if(CPUID::has_rdseed())
4✔
69
      {
70
      secure_vector<uint32_t> seed;
×
71
      seed.reserve(RDSEED_BYTES / 4);
×
72

73
      for(size_t p = 0; p != RDSEED_BYTES / 4; ++p)
×
74
         {
75
         /*
76
         If at any point we exceed our retry count, we stop the entire seed
77
         gathering process. This situation will only occur in situations of
78
         extremely high RDSEED utilization. If RDSEED is currently so highly
79
         contended, then the rest of the poll is likely to also face contention and
80
         it is better to quit now rather than (presumably) face very high retry
81
         times for the rest of the poll.
82
         */
83
         if(!read_rdseed(seed))
×
84
            break;
85
         }
86

87
      if(!seed.empty())
×
88
         {
89
         rng.add_entropy(reinterpret_cast<const uint8_t*>(seed.data()),
×
90
                         seed.size() * sizeof(uint32_t));
×
91
         }
92
      }
×
93

94
   // RDSEED is used but not trusted
95
   return 0;
4✔
96
   }
97

98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc