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

randombit / botan / 22061970273

16 Feb 2026 12:03PM UTC coverage: 90.044% (+0.007%) from 90.037%
22061970273

push

github

web-flow
Merge pull request #5345 from randombit/jack/min-test-rng-h

Minimize includes into test_rng.h

102336 of 113651 relevant lines covered (90.04%)

11395171.59 hits per line

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

92.65
/src/tests/test_rngs.cpp
1
/*
2
* (C) 2023 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "test_rng.h"
8

9
#include <botan/hex.h>
10
#include <array>
11

12
#if defined(BOTAN_HAS_AES)
13
   #include <botan/block_cipher.h>
14
   #include <botan/internal/loadstor.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
uint8_t Fixed_Output_RNG::random() {
103,620✔
20
   if(m_buf_pos >= m_buf.size()) {
103,620✔
21
      if(m_fallback.has_value()) {
1✔
22
         return m_fallback.value()->next_byte();
×
23
      } else {
24
         throw Test_Error("Fixed output RNG ran out of bytes, test bug?");
1✔
25
      }
26
   }
27

28
   const uint8_t out = m_buf[m_buf_pos];
103,619✔
29
   m_buf_pos += 1;
103,619✔
30
   return out;
103,619✔
31
}
32

33
Fixed_Output_RNG::Fixed_Output_RNG(const std::string& in_str) {
13✔
34
   std::vector<uint8_t> in = Botan::hex_decode(in_str);
13✔
35
   m_buf.insert(m_buf.end(), in.begin(), in.end());
13✔
36
}
13✔
37

38
Fixed_Output_RNG::Fixed_Output_RNG(RandomNumberGenerator& rng, size_t len) {
601✔
39
   std::vector<uint8_t> output;
601✔
40
   rng.random_vec(output, len);
601✔
41
   m_buf.insert(m_buf.end(), output.begin(), output.end());
601✔
42
}
601✔
43

44
#if defined(BOTAN_HAS_AES)
45

46
CTR_DRBG_AES256::~CTR_DRBG_AES256() = default;
3,810✔
47

48
void CTR_DRBG_AES256::clear() {
1,917✔
49
   const uint8_t zeros[32] = {0};
1,917✔
50
   m_cipher->set_key(zeros, 32);
×
51
   m_V0 = 0;
1,917✔
52
   m_V1 = 0;
1,917✔
53
}
×
54

55
void CTR_DRBG_AES256::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) {
6,883✔
56
   if(!input.empty()) {
6,883✔
57
      if(input.size() != 48) {
1,917✔
58
         throw Test_Error("CTR_DRBG(AES-256) assumes 48 byte input");
×
59
      }
60

61
      clear();
1,917✔
62
      update(input);
1,917✔
63
   }
64

65
   if(!output.empty()) {
6,883✔
66
      const size_t full_blocks = output.size() / 16;
4,966✔
67
      const size_t leftover_bytes = output.size() % 16;
4,966✔
68

69
      for(size_t i = 0; i != full_blocks; ++i) {
52,788✔
70
         incr_V_into(output.subspan(i * 16, 16));
47,822✔
71
      }
72

73
      m_cipher->encrypt_n(output.data(), output.data(), full_blocks);
4,966✔
74

75
      if(leftover_bytes > 0) {
4,966✔
76
         uint8_t block[16];
566✔
77
         incr_V_into(block);
566✔
78
         m_cipher->encrypt(block);
566✔
79
         Botan::copy_mem(output.subspan(full_blocks * 16).data(), block, leftover_bytes);
566✔
80
      }
81

82
      update({});
4,966✔
83
   }
84
}
6,883✔
85

86
CTR_DRBG_AES256::CTR_DRBG_AES256(std::span<const uint8_t> seed) :
1,917✔
87
      m_cipher(Botan::BlockCipher::create_or_throw("AES-256")) {
1,917✔
88
   add_entropy(seed);
1,917✔
89
}
1,917✔
90

91
void CTR_DRBG_AES256::incr_V_into(std::span<uint8_t> output) {
69,037✔
92
   BOTAN_ASSERT_NOMSG(output.size() == 16);
69,037✔
93

94
   m_V1 += 1;
69,037✔
95
   if(m_V1 == 0) {
69,037✔
96
      m_V0 += 1;
×
97
   }
98

99
   Botan::store_be<uint64_t>(output.data(), m_V0, m_V1);
69,037✔
100
}
69,037✔
101

102
void CTR_DRBG_AES256::update(std::span<const uint8_t> provided_data) {
6,883✔
103
   std::array<uint8_t, 3 * 16> temp = {0};
6,883✔
104

105
   const std::span<uint8_t> t(temp);
6,883✔
106
   for(size_t i = 0; i != 3; ++i) {
27,532✔
107
      incr_V_into(t.subspan(16 * i, 16));
20,649✔
108
   }
109

110
   m_cipher->encrypt_n(temp.data(), temp.data(), 3);
6,883✔
111

112
   if(!provided_data.empty()) {
6,883✔
113
      BOTAN_ASSERT_NOMSG(provided_data.size() == temp.size());
1,917✔
114
      for(size_t i = 0; i != provided_data.size(); i++) {
93,933✔
115
         temp[i] ^= provided_data[i];
92,016✔
116
      }
117
   }
118

119
   m_cipher->set_key(std::span(temp).first(32));
6,883✔
120

121
   m_V0 = Botan::load_be<uint64_t>(temp.data() + 32, 0);
6,883✔
122
   m_V1 = Botan::load_be<uint64_t>(temp.data() + 32, 1);
6,883✔
123
}
6,883✔
124

125
#endif
126

127
}  // namespace Botan_Tests
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

© 2026 Coveralls, Inc