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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

93.17
/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) :
99,707✔
27
      m_cipher(std::move(cipher)),
99,707✔
28
      m_block_size(m_cipher->block_size()),
99,707✔
29
      m_ctr_size(ctr_size),
99,707✔
30
      m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
99,707✔
31
      m_counter(m_cipher->parallel_bytes()),
199,414✔
32
      m_pad(m_counter.size()),
99,707✔
33
      m_pad_pos(0) {
199,414✔
34
   BOTAN_ARG_CHECK(m_ctr_size >= 4 && m_ctr_size <= m_block_size, "Invalid CTR-BE counter size");
99,707✔
35
}
99,707✔
36

37
void CTR_BE::clear() {
2,217✔
38
   m_cipher->clear();
2,217✔
39
   zeroise(m_pad);
2,217✔
40
   zeroise(m_counter);
2,217✔
41
   zap(m_iv);
2,217✔
42
   m_pad_pos = 0;
2,217✔
43
}
2,217✔
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 {
233,195✔
50
   return (iv_len <= m_block_size);
233,195✔
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 {
290,117✔
58
   return m_cipher->key_spec();
290,117✔
59
}
60

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

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

69
void CTR_BE::key_schedule(const uint8_t key[], size_t key_len) {
104,308✔
70
   m_cipher->set_key(key, key_len);
104,308✔
71

72
   // Set a default all-zeros IV
73
   set_iv(nullptr, 0);
104,308✔
74
}
104,308✔
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,621✔
85
   assert_key_material_set();
38,621✔
86

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

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

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

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

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

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

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

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

130
   while(length >= m_pad.size()) {
289,583✔
131
      add_counter(m_ctr_blocks);
151,079✔
132
      m_cipher->encrypt_n(m_counter.data(), out, m_ctr_blocks);
151,079✔
133

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

138
   if(m_pad_pos == m_pad.size()) {
138,504✔
139
      add_counter(m_ctr_blocks);
103,237✔
140
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
103,237✔
141
      m_pad_pos = 0;
103,237✔
142
   }
143

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

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

154
   m_iv.resize(m_block_size);
229,895✔
155
   zeroise(m_iv);
229,895✔
156
   buffer_insert(m_iv, 0, iv, iv_len);
229,895✔
157

158
   seek(0);
229,895✔
159
}
228,762✔
160

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

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

170
      for(size_t i = 0; i != ctr_blocks; ++i) {
161,110✔
171
         store_be(uint32_t(low32 + i), &m_counter[i * BS + off]);
150,848✔
172
      }
173
   } else if(ctr_size == 8) {
258,199✔
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) {
253,311✔
181
      const size_t off = (BS - 16);
253,311✔
182
      uint64_t b0 = load_be<uint64_t>(&m_counter[off], 0);
253,311✔
183
      uint64_t b1 = load_be<uint64_t>(&m_counter[off], 1);
253,311✔
184
      b1 += counter;
253,311✔
185
      b0 += (b1 < counter) ? 1 : 0;  // carry
253,311✔
186

187
      for(size_t i = 0; i != ctr_blocks; ++i) {
4,306,287✔
188
         store_be(b0, &m_counter[i * BS + off]);
4,052,976✔
189
         store_be(b1, &m_counter[i * BS + off + 8]);
4,052,976✔
190
         b1 += 1;
4,052,976✔
191
         b0 += (b1 == 0);  // carry
4,052,976✔
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
}
268,461✔
207

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

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

213
   zeroise(m_counter);
230,362✔
214
   buffer_insert(m_counter, 0, m_iv);
230,362✔
215

216
   const size_t BS = m_block_size;
230,362✔
217

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

220
   if(m_ctr_size == 4 && BS >= 8) {
230,362✔
221
      const uint32_t low32 = load_be<uint32_t>(&m_counter[BS - 4], 0);
14,421✔
222

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

235
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
209,640✔
236
         const uint32_t c = static_cast<uint32_t>(low32 + i);
195,219✔
237
         store_be(c, &m_counter[(BS - 4) + i * BS]);
195,219✔
238
      }
239
   } else {
240
      // do everything sequentially:
241
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
3,408,784✔
242
         buffer_insert(m_counter, i * BS, &m_counter[(i - 1) * BS], BS);
3,192,843✔
243

244
         for(size_t j = 0; j != m_ctr_size; ++j) {
3,195,001✔
245
            if(++m_counter[i * BS + (BS - 1 - j)]) {
3,194,962✔
246
               break;
247
            }
248
         }
249
      }
250
   }
251

252
   if(base_counter > 0) {
230,362✔
253
      add_counter(base_counter);
156✔
254
   }
255

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