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

randombit / botan / 6513140094

13 Oct 2023 09:12PM UTC coverage: 91.704% (-0.003%) from 91.707%
6513140094

push

github

web-flow
Merge pull request #3757 from randombit/jack/remove-buffer-insert

Remove use of buffer_insert from filters, MACs, stream ciphers

80025 of 87264 relevant lines covered (91.7%)

8532652.1 hits per line

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

93.21
/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,149✔
18
      m_cipher(std::move(cipher)),
1,149✔
19
      m_block_size(m_cipher->block_size()),
1,149✔
20
      m_ctr_size(m_block_size),
1,149✔
21
      m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
1,149✔
22
      m_counter(m_cipher->parallel_bytes()),
2,298✔
23
      m_pad(m_counter.size()),
1,149✔
24
      m_pad_pos(0) {}
2,298✔
25

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

37
void CTR_BE::clear() {
17,430✔
38
   m_cipher->clear();
17,430✔
39
   zeroise(m_pad);
17,430✔
40
   zeroise(m_counter);
17,430✔
41
   zap(m_iv);
17,430✔
42
   m_pad_pos = 0;
17,430✔
43
}
17,430✔
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 {
346,300✔
50
   return (iv_len <= m_block_size);
346,300✔
51
}
52

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

57
Key_Length_Specification CTR_BE::key_spec() const {
223,744✔
58
   return m_cipher->key_spec();
223,744✔
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 {
1,805,937✔
66
   return m_cipher->has_keying_material();
1,805,937✔
67
}
68

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

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

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

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

87
   const uint8_t* pad_bits = &m_pad[0];
36,423✔
88
   const size_t pad_size = m_pad.size();
36,423✔
89

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

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

106
   while(length >= pad_size) {
48,885✔
107
      xor_buf(out, in, pad_bits, pad_size);
12,462✔
108
      length -= pad_size;
12,462✔
109
      in += pad_size;
12,462✔
110
      out += pad_size;
12,462✔
111

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

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

120
void CTR_BE::generate_keystream(uint8_t out[], size_t length) {
1,518,571✔
121
   assert_key_material_set();
1,518,571✔
122

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

130
   while(length >= m_pad.size()) {
1,669,558✔
131
      add_counter(m_ctr_blocks);
150,987✔
132
      m_cipher->encrypt_n(m_counter.data(), out, m_ctr_blocks);
150,987✔
133

134
      length -= m_pad.size();
150,987✔
135
      out += m_pad.size();
150,987✔
136
   }
137

138
   if(m_pad_pos == m_pad.size()) {
1,518,571✔
139
      add_counter(m_ctr_blocks);
94,251✔
140
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
94,251✔
141
      m_pad_pos = 0;
94,251✔
142
   }
143

144
   copy_mem(out, &m_pad[0], length);
1,518,571✔
145
   m_pad_pos += length;
1,518,571✔
146
   BOTAN_ASSERT_NOMSG(m_pad_pos < m_pad.size());
1,518,571✔
147
}
1,518,571✔
148

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

154
   m_iv.resize(m_block_size);
238,079✔
155
   zeroise(m_iv);
238,079✔
156
   copy_mem(&m_iv[0], iv, iv_len);
238,079✔
157

158
   seek(0);
238,079✔
159
}
236,946✔
160

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

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

170
      for(size_t i = 0; i != ctr_blocks; ++i) {
164,682✔
171
         store_be(uint32_t(low32 + i), &m_counter[i * BS + off]);
154,296✔
172
      }
173
   } else if(ctr_size == 8) {
249,121✔
174
      const size_t off = (BS - 8);
4,888✔
175
      const uint64_t low64 = counter + load_be<uint64_t>(&m_counter[off], 0);
4,888✔
176

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

187
      for(size_t i = 0; i != ctr_blocks; ++i) {
4,151,961✔
188
         store_be(b0, &m_counter[i * BS + off]);
3,907,728✔
189
         store_be(b1, &m_counter[i * BS + off + 8]);
3,907,728✔
190
         b1 += 1;
3,907,728✔
191
         b0 += (b1 == 0);  // carry
3,907,728✔
192
      }
193
   } else {
194
      for(size_t i = 0; i != ctr_blocks; ++i) {
×
195
         uint64_t local_counter = counter;
×
196
         uint16_t carry = static_cast<uint8_t>(local_counter);
×
197
         for(size_t j = 0; (carry || local_counter) && j != ctr_size; ++j) {
×
198
            const size_t off = i * BS + (BS - 1 - j);
×
199
            const uint16_t cnt = static_cast<uint16_t>(m_counter[off]) + carry;
×
200
            m_counter[off] = static_cast<uint8_t>(cnt);
×
201
            local_counter = (local_counter >> 8);
×
202
            carry = (cnt >> 8) + static_cast<uint8_t>(local_counter);
×
203
         }
204
      }
205
   }
206
}
259,507✔
207

208
void CTR_BE::seek(uint64_t offset) {
240,498✔
209
   assert_key_material_set();
240,498✔
210

211
   const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
238,546✔
212

213
   zeroise(m_counter);
238,546✔
214
   BOTAN_ASSERT_NOMSG(m_counter.size() >= m_iv.size());
238,546✔
215
   copy_mem(&m_counter[0], &m_iv[0], m_iv.size());
238,546✔
216

217
   const size_t BS = m_block_size;
238,546✔
218

219
   // Set m_counter blocks to IV, IV + 1, ... IV + n
220

221
   if(m_ctr_size == 4 && BS >= 8) {
238,546✔
222
      const uint32_t low32 = load_be<uint32_t>(&m_counter[BS - 4], 0);
14,498✔
223

224
      if(m_ctr_blocks >= 4 && is_power_of_2(m_ctr_blocks)) {
14,498✔
225
         size_t written = 1;
226
         while(written < m_ctr_blocks) {
69,677✔
227
            copy_mem(&m_counter[written * BS], &m_counter[0], BS * written);
55,179✔
228
            written *= 2;
55,179✔
229
         }
230
      } else {
231
         for(size_t i = 1; i != m_ctr_blocks; ++i) {
×
232
            copy_mem(&m_counter[i * BS], &m_counter[0], BS - 4);
×
233
         }
234
      }
235

236
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
210,920✔
237
         const uint32_t c = static_cast<uint32_t>(low32 + i);
196,422✔
238
         store_be(c, &m_counter[(BS - 4) + i * BS]);
196,422✔
239
      }
240
   } else {
241
      // do everything sequentially:
242
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
3,538,496✔
243
         copy_mem(&m_counter[i * BS], &m_counter[(i - 1) * BS], BS);
3,314,448✔
244

245
         for(size_t j = 0; j != m_ctr_size; ++j) {
3,316,683✔
246
            if(++m_counter[i * BS + (BS - 1 - j)]) {
3,316,644✔
247
               break;
248
            }
249
         }
250
      }
251
   }
252

253
   if(base_counter > 0) {
238,546✔
254
      add_counter(base_counter);
156✔
255
   }
256

257
   m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
238,546✔
258
   m_pad_pos = offset % m_counter.size();
238,546✔
259
}
238,546✔
260
}  // 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

© 2025 Coveralls, Inc