Coverage Report

Created: 2025-03-09 06:52

/src/botan/src/lib/entropy/rdseed/rdseed.cpp
Line
Count
Source (jump to first uncovered line)
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/compiler.h>
12
#include <botan/rng.h>
13
#include <botan/internal/cpuid.h>
14
#include <botan/internal/target_info.h>
15
16
#if !defined(BOTAN_USE_GCC_INLINE_ASM)
17
   #include <immintrin.h>
18
#endif
19
20
namespace Botan {
21
22
namespace {
23
24
0
BOTAN_FUNC_ISA("rdseed") bool read_rdseed(secure_vector<uint32_t>& seed) {
25
   /*
26
   * RDSEED is not guaranteed to generate an output within any specific number
27
   * of attempts. However in testing on a Skylake system, with all hyperthreads
28
   * occupied in tight RDSEED loops, RDSEED will still usually succeed in under
29
   * 150 attempts. The maximum ever seen was 230 attempts until success. When
30
   * idle, RDSEED usually succeeds in 1 or 2 attempts.
31
   *
32
   * We set an upper bound of 1024 attempts, because it is possible that due
33
   * to firmware issue RDSEED is simply broken and never succeeds. We do not
34
   * want to loop forever in that case. If we exceed that limit, then we assume
35
   * the hardware is actually just broken, and stop the poll.
36
   */
37
0
   const size_t RDSEED_RETRIES = 1024;
38
39
0
   for(size_t i = 0; i != RDSEED_RETRIES; ++i) {
40
0
      uint32_t r = 0;
41
0
      int cf = 0;
42
43
0
#if defined(BOTAN_USE_GCC_INLINE_ASM)
44
0
      asm("rdseed %0; adcl $0,%1" : "=r"(r), "=r"(cf) : "0"(r), "1"(cf) : "cc");
45
#else
46
      cf = _rdseed32_step(&r);
47
#endif
48
49
0
      if(1 == cf) {
50
0
         seed.push_back(r);
51
0
         return true;
52
0
      }
53
54
      // Intel suggests pausing if RDSEED fails.
55
0
#if defined(BOTAN_USE_GCC_INLINE_ASM)
56
0
      asm volatile("pause");
57
#else
58
      _mm_pause();
59
#endif
60
0
   }
61
62
0
   return false;  // failed to produce an output after many attempts
63
0
}
64
65
}  // namespace
66
67
0
size_t Intel_Rdseed::poll(RandomNumberGenerator& rng) {
68
0
   const size_t RDSEED_BYTES = 1024;
69
0
   static_assert(RDSEED_BYTES % 4 == 0, "Bad RDSEED configuration");
70
71
0
   if(CPUID::has_rdseed()) {
72
0
      secure_vector<uint32_t> seed;
73
0
      seed.reserve(RDSEED_BYTES / 4);
74
75
0
      for(size_t p = 0; p != RDSEED_BYTES / 4; ++p) {
76
         /*
77
         If at any point we exceed our retry count, we stop the entire seed
78
         gathering process. This situation will only occur in situations of
79
         extremely high RDSEED utilization. If RDSEED is currently so highly
80
         contended, then the rest of the poll is likely to also face contention and
81
         it is better to quit now rather than (presumably) face very high retry
82
         times for the rest of the poll.
83
         */
84
0
         if(!read_rdseed(seed)) {
85
0
            break;
86
0
         }
87
0
      }
88
89
0
      if(!seed.empty()) {
90
0
         rng.add_entropy(reinterpret_cast<const uint8_t*>(seed.data()), seed.size() * sizeof(uint32_t));
91
0
      }
92
0
   }
93
94
   // RDSEED is used but not trusted
95
0
   return 0;
96
0
}
97
98
}  // namespace Botan