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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

84.0
/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
#if defined(BOTAN_HAS_AES)
10
   #include <botan/internal/loadstor.h>
11
#endif
12

13
#include <array>
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_AES)
18

19
void CTR_DRBG_AES256::clear() {
20
   const uint8_t zeros[32] = {0};
21
   m_cipher->set_key(zeros, 32);
22
   m_V0 = 0;
23
   m_V1 = 0;
24
}
25

26
void CTR_DRBG_AES256::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) {
27
   if(!input.empty()) {
28
      if(input.size() != 48) {
29
         throw Test_Error("CTR_DRBG(AES-256) assumes 48 byte input");
30
      }
31

32
      clear();
33
      update(input);
34
   }
35

36
   if(!output.empty()) {
37
      const size_t full_blocks = output.size() / 16;
38
      const size_t leftover_bytes = output.size() % 16;
39

40
      for(size_t i = 0; i != full_blocks; ++i) {
41
         incr_V_into(output.subspan(i * 16, 16));
42
      }
43

44
      m_cipher->encrypt_n(output.data(), output.data(), full_blocks);
45

46
      if(leftover_bytes > 0) {
47
         uint8_t block[16];
48
         incr_V_into(block);
49
         m_cipher->encrypt(block);
50
         Botan::copy_mem(output.subspan(full_blocks * 16).data(), block, leftover_bytes);
51
      }
52

53
      update({});
54
   }
55
}
56

57
CTR_DRBG_AES256::CTR_DRBG_AES256(std::span<const uint8_t> seed) {
58
   m_cipher = Botan::BlockCipher::create_or_throw("AES-256");
59
   add_entropy(seed);
60
}
61

62
void CTR_DRBG_AES256::incr_V_into(std::span<uint8_t> output) {
63
   BOTAN_ASSERT_NOMSG(output.size() == 16);
64

65
   m_V1 += 1;
66
   if(m_V1 == 0) {
67
      m_V0 += 1;
68
   }
69

70
   Botan::store_be<uint64_t>(output.data(), m_V0, m_V1);
71
}
72

73
void CTR_DRBG_AES256::update(std::span<const uint8_t> provided_data) {
74
   std::array<uint8_t, 3 * 16> temp = {0};
75

76
   std::span<uint8_t> t(temp);
77
   for(size_t i = 0; i != 3; ++i) {
78
      incr_V_into(t.subspan(16 * i, 16));
79
   }
80

81
   m_cipher->encrypt_n(temp.data(), temp.data(), 3);
82

83
   if(!provided_data.empty()) {
84
      BOTAN_ASSERT_NOMSG(provided_data.size() == temp.size());
85
      for(size_t i = 0; i != provided_data.size(); i++) {
86
         temp[i] ^= provided_data[i];
87
      }
88
   }
89

90
   m_cipher->set_key(temp.data(), 32);  // TODO: adapt after GH #3297
91

92
   m_V0 = Botan::load_be<uint64_t>(temp.data() + 32, 0);
93
   m_V1 = Botan::load_be<uint64_t>(temp.data() + 32, 1);
94
}
95

96
#endif
97

98
}  // 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