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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

98.63
/src/lib/rng/stateful_rng/stateful_rng.cpp
1
/*
2
* (C) 2016,2020 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/stateful_rng.h>
8

9
#include <botan/internal/loadstor.h>
10
#include <botan/internal/os_utils.h>
11

12
namespace Botan {
13

14
void Stateful_RNG::clear() {
6,662✔
15
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
6,662✔
16
   m_reseed_counter = 0;
6,662✔
17
   m_last_pid = 0;
6,662✔
18
   clear_state();
6,662✔
19
}
6,662✔
20

21
void Stateful_RNG::force_reseed() {
84✔
22
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
84✔
23
   m_reseed_counter = 0;
84✔
24
}
84✔
25

26
bool Stateful_RNG::is_seeded() const {
226,404✔
27
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
226,404✔
28
   return m_reseed_counter > 0;
226,404✔
29
}
226,404✔
30

31
void Stateful_RNG::initialize_with(std::span<const uint8_t> input) {
1,979✔
32
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
1,979✔
33

34
   clear();
1,979✔
35
   add_entropy(input);
1,979✔
36
}
1,979✔
37

38
void Stateful_RNG::generate_batched_output(std::span<uint8_t> output, std::span<const uint8_t> input) {
221,374✔
39
   BOTAN_ASSERT_NOMSG(!output.empty());
221,374✔
40

41
   const size_t max_per_request = max_number_of_bytes_per_request();
221,374✔
42

43
   if(max_per_request == 0)  // no limit
221,374✔
44
   {
45
      reseed_check();
42,577✔
46
      this->generate_output(output, input);
42,566✔
47
   } else {
48
      while(!output.empty()) {
357,625✔
49
         const size_t this_req = std::min(max_per_request, output.size());
178,842✔
50

51
         reseed_check();
178,842✔
52
         this->generate_output(output.subspan(0, this_req), input);
178,828✔
53

54
         // only include the input for the first iteration
55
         input = {};
178,828✔
56

57
         output = output.subspan(this_req);
178,828✔
58
      }
59
   }
60
}
221,349✔
61

62
void Stateful_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) {
239,357✔
63
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
239,357✔
64

65
   if(output.empty()) {
239,357✔
66
      // Special case for exclusively adding entropy to the stateful RNG.
67
      this->update(input);
17,983✔
68

69
      if(8 * input.size() >= security_level()) {
17,983✔
70
         reset_reseed_counter();
17,872✔
71
      }
72
   } else {
73
      generate_batched_output(output, input);
221,374✔
74
   }
75
}
239,332✔
76

77
size_t Stateful_RNG::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) {
9✔
78
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
9✔
79

80
   const size_t bits_collected = RandomNumberGenerator::reseed(srcs, poll_bits, poll_timeout);
9✔
81

82
   if(bits_collected >= security_level()) {
5✔
83
      reset_reseed_counter();
1✔
84
   }
85

86
   return bits_collected;
5✔
87
}
5✔
88

89
void Stateful_RNG::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits) {
145✔
90
   lock_guard_type<recursive_mutex_type> lock(m_mutex);
145✔
91

92
   RandomNumberGenerator::reseed_from_rng(rng, poll_bits);
145✔
93

94
   if(poll_bits >= security_level()) {
137✔
95
      reset_reseed_counter();
137✔
96
   }
97
}
137✔
98

99
void Stateful_RNG::reset_reseed_counter() {
18,010✔
100
   // Lock is held whenever this function is called
101
   m_reseed_counter = 1;
18,010✔
102
}
18,010✔
103

104
void Stateful_RNG::reseed_check() {
221,419✔
105
   // Lock is held whenever this function is called
106

107
   const uint32_t cur_pid = OS::get_process_id();
221,419✔
108

109
   const bool fork_detected = (m_last_pid > 0) && (cur_pid != m_last_pid);
221,419✔
110

111
   if(is_seeded() == false || fork_detected || (m_reseed_interval > 0 && m_reseed_counter >= m_reseed_interval)) {
221,419✔
112
      m_reseed_counter = 0;
156✔
113
      m_last_pid = cur_pid;
156✔
114

115
      if(m_underlying_rng) {
156✔
116
         reseed_from_rng(*m_underlying_rng, security_level());
140✔
117
      }
118

119
      if(m_entropy_sources) {
148✔
120
         reseed(*m_entropy_sources, security_level());
8✔
121
      }
122

123
      if(!is_seeded()) {
144✔
124
         if(fork_detected)
13✔
125
            throw Invalid_State("Detected use of fork but cannot reseed DRBG");
×
126
         else
127
            throw PRNG_Unseeded(name());
26✔
128
      }
129
   } else {
130
      BOTAN_ASSERT(m_reseed_counter != 0, "RNG is seeded");
221,263✔
131
      m_reseed_counter += 1;
221,263✔
132
   }
133
}
221,394✔
134

135
}
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