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

randombit / botan / 23109969988

15 Mar 2026 12:02PM UTC coverage: 89.727% (-0.003%) from 89.73%
23109969988

Pull #5445

github

web-flow
Merge 34a481fe4 into e9952d62f
Pull Request #5445: Remove redundant typename keywords

104206 of 116137 relevant lines covered (89.73%)

11479306.11 hits per line

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

90.3
/src/lib/stream/ctr/ctr.cpp
1
/*
2
* Counter mode
3
* (C) 1999-2011,2014 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/internal/ctr.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/loadstor.h>
14

15
namespace Botan {
16

17
CTR_BE::CTR_BE(std::unique_ptr<BlockCipher> cipher) :
1,023✔
18
      m_cipher(std::move(cipher)),
1,023✔
19
      m_block_size(m_cipher->block_size()),
1,023✔
20
      m_ctr_size(m_block_size),
1,023✔
21
      m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
1,023✔
22
      m_counter(m_cipher->parallel_bytes()),
2,046✔
23
      m_pad(m_counter.size()),
1,023✔
24
      m_pad_pos(0) {}
3,069✔
25

26
CTR_BE::CTR_BE(std::unique_ptr<BlockCipher> cipher, size_t ctr_size) :
98,094✔
27
      m_cipher(std::move(cipher)),
98,094✔
28
      m_block_size(m_cipher->block_size()),
98,094✔
29
      m_ctr_size(ctr_size),
98,094✔
30
      m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
98,094✔
31
      m_counter(m_cipher->parallel_bytes()),
196,188✔
32
      m_pad(m_counter.size()),
98,094✔
33
      m_pad_pos(0) {
196,188✔
34
   BOTAN_ARG_CHECK(m_ctr_size >= 4 && m_ctr_size <= m_block_size, "Invalid CTR-BE counter size");
98,094✔
35
}
98,094✔
36

37
void CTR_BE::clear() {
6,627✔
38
   m_cipher->clear();
6,627✔
39
   zeroise(m_pad);
6,627✔
40
   zeroise(m_counter);
6,627✔
41
   zap(m_iv);
6,627✔
42
   m_pad_pos = 0;
6,627✔
43
}
6,627✔
44

45
size_t CTR_BE::default_iv_length() const {
1,638✔
46
   return m_block_size;
1,638✔
47
}
48

49
bool CTR_BE::valid_iv_length(size_t iv_len) const {
316,774✔
50
   return (iv_len <= m_block_size);
316,774✔
51
}
52

53
size_t CTR_BE::buffer_size() const {
840✔
54
   return m_pad.size();
840✔
55
}
56

57
Key_Length_Specification CTR_BE::key_spec() const {
203,578✔
58
   return m_cipher->key_spec();
203,578✔
59
}
60

61
std::unique_ptr<StreamCipher> CTR_BE::new_object() const {
819✔
62
   return std::make_unique<CTR_BE>(m_cipher->new_object(), m_ctr_size);
819✔
63
}
64

65
bool CTR_BE::has_keying_material() const {
20,912,349✔
66
   return m_cipher->has_keying_material();
20,912,349✔
67
}
68

69
void CTR_BE::key_schedule(std::span<const uint8_t> key) {
102,198✔
70
   m_cipher->set_key(key);
102,198✔
71

72
   // Set a default all-zeros IV
73
   set_iv(nullptr, 0);
102,198✔
74
}
102,198✔
75

76
std::string CTR_BE::name() const {
7,467✔
77
   if(m_ctr_size == m_block_size) {
7,467✔
78
      return fmt("CTR-BE({})", m_cipher->name());
6,545✔
79
   } else {
80
      return fmt("CTR-BE({},{})", m_cipher->name(), m_ctr_size);
922✔
81
   }
82
}
83

84
void CTR_BE::cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) {
69,083✔
85
   assert_key_material_set();
69,083✔
86

87
   const uint8_t* pad_bits = m_pad.data();
66,853✔
88
   const size_t pad_size = m_pad.size();
66,853✔
89

90
   if(m_pad_pos > 0) {
66,853✔
91
      const size_t avail = pad_size - m_pad_pos;
47,824✔
92
      const size_t take = std::min(length, avail);
47,824✔
93
      xor_buf(out, in, pad_bits + m_pad_pos, take);
47,824✔
94
      length -= take;
47,824✔
95
      in += take;
47,824✔
96
      out += take;
47,824✔
97
      m_pad_pos += take;
47,824✔
98

99
      if(take == avail) {
47,824✔
100
         add_counter(m_ctr_blocks);
1,855✔
101
         m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
1,855✔
102
         m_pad_pos = 0;
1,855✔
103
      }
104
   }
105

106
   while(length >= pad_size) {
71,912✔
107
      xor_buf(out, in, pad_bits, pad_size);
5,059✔
108
      length -= pad_size;
5,059✔
109
      in += pad_size;
5,059✔
110
      out += pad_size;
5,059✔
111

112
      add_counter(m_ctr_blocks);
5,059✔
113
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
5,059✔
114
   }
115

116
   xor_buf(out, in, pad_bits, length);
66,853✔
117
   m_pad_pos += length;
66,853✔
118
}
66,853✔
119

120
void CTR_BE::generate_keystream(uint8_t out[], size_t length) {
20,613,533✔
121
   assert_key_material_set();
20,613,533✔
122

123
   const size_t avail = m_pad.size() - m_pad_pos;
20,613,533✔
124
   const size_t take = std::min(length, avail);
20,613,533✔
125
   copy_mem(out, &m_pad[m_pad_pos], take);
20,613,533✔
126
   length -= take;
20,613,533✔
127
   out += take;
20,613,533✔
128
   m_pad_pos += take;
20,613,533✔
129

130
   while(length >= m_pad.size()) {
20,613,533✔
131
      add_counter(m_ctr_blocks);
×
132
      m_cipher->encrypt_n(m_counter.data(), out, m_ctr_blocks);
×
133

134
      length -= m_pad.size();
×
135
      out += m_pad.size();
×
136
   }
137

138
   if(m_pad_pos == m_pad.size()) {
20,613,533✔
139
      add_counter(m_ctr_blocks);
84,905✔
140
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
84,905✔
141
      m_pad_pos = 0;
84,905✔
142
   }
143

144
   copy_mem(out, m_pad.data(), length);
20,613,533✔
145
   m_pad_pos += length;
20,613,533✔
146
   BOTAN_ASSERT_NOMSG(m_pad_pos < m_pad.size());
20,613,533✔
147
}
20,613,533✔
148

149
void CTR_BE::set_iv_bytes(const uint8_t iv[], size_t iv_len) {
219,914✔
150
   if(!valid_iv_length(iv_len)) {
219,914✔
151
      throw Invalid_IV_Length(name(), iv_len);
1,646✔
152
   }
153

154
   m_iv.resize(m_block_size);
219,091✔
155
   zeroise(m_iv);
219,091✔
156
   copy_mem(m_iv.data(), iv, iv_len);
219,091✔
157

158
   seek(0);
219,091✔
159
}
217,958✔
160

161
void CTR_BE::add_counter(const uint64_t counter) {
91,831✔
162
   const size_t ctr_size = m_ctr_size;
91,831✔
163
   const size_t ctr_blocks = m_ctr_blocks;
91,831✔
164
   const size_t BS = m_block_size;
91,831✔
165

166
   if(ctr_size == 4) {
91,831✔
167
      const size_t off = (BS - 4);
6,815✔
168
      const uint32_t low32 = static_cast<uint32_t>(counter + load_be<uint32_t>(&m_counter[off], 0));
6,815✔
169

170
      for(size_t i = 0; i != ctr_blocks; ++i) {
183,959✔
171
         store_be(uint32_t(low32 + i), &m_counter[i * BS + off]);
177,144✔
172
      }
173
   } else if(ctr_size == 8) {
85,016✔
174
      const size_t off = (BS - 8);
22✔
175
      const uint64_t low64 = counter + load_be<uint64_t>(&m_counter[off], 0);
22✔
176

177
      for(size_t i = 0; i != ctr_blocks; ++i) {
886✔
178
         store_be(uint64_t(low64 + i), &m_counter[i * BS + off]);
864✔
179
      }
180
   } else if(ctr_size == 16) {
84,994✔
181
      const size_t off = (BS - 16);
84,994✔
182
      uint64_t b0 = load_be<uint64_t>(&m_counter[off], 0);
84,994✔
183
      uint64_t b1 = load_be<uint64_t>(&m_counter[off], 1);
84,994✔
184
      b1 += counter;
84,994✔
185
      b0 += (b1 < counter) ? 1 : 0;  // carry
84,994✔
186

187
      for(size_t i = 0; i != ctr_blocks; ++i) {
2,748,018✔
188
         store_be(b0, &m_counter[i * BS + off]);
2,663,024✔
189
         store_be(b1, &m_counter[i * BS + off + 8]);
2,663,024✔
190
         b1 += 1;
2,663,024✔
191
         if(b1 == 0) {
2,663,024✔
192
            b0 += 1;  // carry
×
193
         }
194
      }
195
   } else {
196
      for(size_t i = 0; i != ctr_blocks; ++i) {
×
197
         uint64_t local_counter = counter;
×
198
         uint16_t carry = static_cast<uint8_t>(local_counter);
×
199
         for(size_t j = 0; (carry > 0 || local_counter > 0) && j != ctr_size; ++j) {
×
200
            const size_t off = i * BS + (BS - 1 - j);
×
201
            const uint16_t cnt = static_cast<uint16_t>(m_counter[off]) + carry;
×
202
            m_counter[off] = static_cast<uint8_t>(cnt);
×
203
            local_counter = (local_counter >> 8);
×
204
            carry = (cnt >> 8) + static_cast<uint8_t>(local_counter);
×
205
         }
206
      }
207
   }
208
}
91,831✔
209

210
void CTR_BE::seek(uint64_t offset) {
221,510✔
211
   assert_key_material_set();
221,510✔
212

213
   const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
219,558✔
214

215
   zeroise(m_counter);
219,558✔
216
   BOTAN_ASSERT_NOMSG(m_counter.size() >= m_iv.size());
219,558✔
217
   copy_mem(m_counter.data(), m_iv.data(), m_iv.size());
219,558✔
218

219
   const size_t BS = m_block_size;
219,558✔
220

221
   // Set m_counter blocks to IV, IV + 1, ... IV + n
222

223
   if(m_ctr_size == 4 && BS >= 8) {
219,558✔
224
      const uint32_t low32 = load_be<uint32_t>(&m_counter[BS - 4], 0);
16,839✔
225

226
      if(m_ctr_blocks >= 4 && is_power_of_2(m_ctr_blocks)) {
16,839✔
227
         size_t written = 1;
228
         while(written < m_ctr_blocks) {
99,486✔
229
            copy_mem(&m_counter[written * BS], &m_counter[0], BS * written);  // NOLINT(*container-data-pointer)
82,647✔
230
            written *= 2;
82,647✔
231
         }
232
      } else {
233
         for(size_t i = 1; i != m_ctr_blocks; ++i) {
×
234
            copy_mem(&m_counter[i * BS], &m_counter[0], BS - 4);  // NOLINT(*container-data-pointer)
×
235
         }
236
      }
237

238
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
524,360✔
239
         const uint32_t c = static_cast<uint32_t>(low32 + i);
507,521✔
240
         store_be(c, &m_counter[(BS - 4) + i * BS]);
507,521✔
241
      }
242
   } else {
243
      // do everything sequentially:
244
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
6,666,776✔
245
         copy_mem(&m_counter[i * BS], &m_counter[(i - 1) * BS], BS);
6,464,057✔
246

247
         for(size_t j = 0; j != m_ctr_size; ++j) {
6,467,815✔
248
            uint8_t& c = m_counter[i * BS + (BS - 1 - j)];
6,467,778✔
249
            c += 1;
6,467,778✔
250
            if(c > 0) {
6,467,778✔
251
               break;
252
            }
253
         }
254
      }
255
   }
256

257
   if(base_counter > 0) {
219,558✔
258
      add_counter(base_counter);
12✔
259
   }
260

261
   m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
219,558✔
262
   m_pad_pos = offset % m_counter.size();
219,558✔
263
}
219,558✔
264
}  // namespace Botan
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