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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

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

47
bool CTR_BE::valid_iv_length(size_t iv_len) const { return (iv_len <= m_block_size); }
233,020✔
48

49
size_t CTR_BE::buffer_size() const { return m_pad.size(); }
844✔
50

51
Key_Length_Specification CTR_BE::key_spec() const { return m_cipher->key_spec(); }
289,745✔
52

53
std::unique_ptr<StreamCipher> CTR_BE::new_object() const {
1,740✔
54
   return std::make_unique<CTR_BE>(m_cipher->new_object(), m_ctr_size);
1,740✔
55
}
56

57
bool CTR_BE::has_keying_material() const { return m_cipher->has_keying_material(); }
417,429✔
58

59
void CTR_BE::key_schedule(const uint8_t key[], size_t key_len) {
104,184✔
60
   m_cipher->set_key(key, key_len);
104,184✔
61

62
   // Set a default all-zeros IV
63
   set_iv(nullptr, 0);
104,184✔
64
}
104,184✔
65

66
std::string CTR_BE::name() const {
7,539✔
67
   if(m_ctr_size == m_block_size)
7,539✔
68
      return fmt("CTR-BE({})", m_cipher->name());
6,653✔
69
   else
70
      return fmt("CTR-BE({},{})", m_cipher->name(), m_ctr_size);
886✔
71
}
72

73
void CTR_BE::cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) {
38,772✔
74
   assert_key_material_set();
38,772✔
75

76
   const uint8_t* pad_bits = &m_pad[0];
36,478✔
77
   const size_t pad_size = m_pad.size();
36,478✔
78

79
   if(m_pad_pos > 0) {
36,478✔
80
      const size_t avail = pad_size - m_pad_pos;
19,214✔
81
      const size_t take = std::min(length, avail);
19,214✔
82
      xor_buf(out, in, pad_bits + m_pad_pos, take);
19,214✔
83
      length -= take;
19,214✔
84
      in += take;
19,214✔
85
      out += take;
19,214✔
86
      m_pad_pos += take;
19,214✔
87

88
      if(take == avail) {
19,214✔
89
         add_counter(m_ctr_blocks);
1,667✔
90
         m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
1,667✔
91
         m_pad_pos = 0;
1,667✔
92
      }
93
   }
94

95
   while(length >= pad_size) {
49,266✔
96
      xor_buf(out, in, pad_bits, pad_size);
12,788✔
97
      length -= pad_size;
12,788✔
98
      in += pad_size;
12,788✔
99
      out += pad_size;
12,788✔
100

101
      add_counter(m_ctr_blocks);
12,788✔
102
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
12,788✔
103
   }
104

105
   xor_buf(out, in, pad_bits, length);
36,478✔
106
   m_pad_pos += length;
36,478✔
107
}
36,478✔
108

109
void CTR_BE::generate_keystream(uint8_t out[], size_t length) {
138,367✔
110
   assert_key_material_set();
138,367✔
111

112
   const size_t avail = m_pad.size() - m_pad_pos;
138,367✔
113
   const size_t take = std::min(length, avail);
138,367✔
114
   copy_mem(out, &m_pad[m_pad_pos], take);
138,367✔
115
   length -= take;
138,367✔
116
   out += take;
138,367✔
117
   m_pad_pos += take;
138,367✔
118

119
   while(length >= m_pad.size()) {
289,343✔
120
      add_counter(m_ctr_blocks);
150,976✔
121
      m_cipher->encrypt_n(m_counter.data(), out, m_ctr_blocks);
150,976✔
122

123
      length -= m_pad.size();
150,976✔
124
      out += m_pad.size();
150,976✔
125
   }
126

127
   if(m_pad_pos == m_pad.size()) {
138,367✔
128
      add_counter(m_ctr_blocks);
103,092✔
129
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
103,092✔
130
      m_pad_pos = 0;
103,092✔
131
   }
132

133
   copy_mem(out, &m_pad[0], length);
138,367✔
134
   m_pad_pos += length;
138,367✔
135
   BOTAN_ASSERT_NOMSG(m_pad_pos < m_pad.size());
138,367✔
136
}
138,367✔
137

138
void CTR_BE::set_iv_bytes(const uint8_t iv[], size_t iv_len) {
230,547✔
139
   if(!valid_iv_length(iv_len))
230,547✔
140
      throw Invalid_IV_Length(name(), iv_len);
1,654✔
141

142
   m_iv.resize(m_block_size);
229,720✔
143
   zeroise(m_iv);
229,720✔
144
   buffer_insert(m_iv, 0, iv, iv_len);
229,720✔
145

146
   seek(0);
229,720✔
147
}
228,587✔
148

149
void CTR_BE::add_counter(const uint64_t counter) {
268,679✔
150
   const size_t ctr_size = m_ctr_size;
268,679✔
151
   const size_t ctr_blocks = m_ctr_blocks;
268,679✔
152
   const size_t BS = m_block_size;
268,679✔
153

154
   if(ctr_size == 4) {
268,679✔
155
      const size_t off = (BS - 4);
10,728✔
156
      const uint32_t low32 = static_cast<uint32_t>(counter + load_be<uint32_t>(&m_counter[off], 0));
10,728✔
157

158
      for(size_t i = 0; i != ctr_blocks; ++i) {
168,168✔
159
         store_be(uint32_t(low32 + i), &m_counter[i * BS + off]);
157,440✔
160
      }
161
   } else if(ctr_size == 8) {
257,951✔
162
      const size_t off = (BS - 8);
4,888✔
163
      const uint64_t low64 = counter + load_be<uint64_t>(&m_counter[off], 0);
4,888✔
164

165
      for(size_t i = 0; i != ctr_blocks; ++i) {
25,400✔
166
         store_be(uint64_t(low64 + i), &m_counter[i * BS + off]);
20,512✔
167
      }
168
   } else if(ctr_size == 16) {
253,063✔
169
      const size_t off = (BS - 16);
253,063✔
170
      uint64_t b0 = load_be<uint64_t>(&m_counter[off], 0);
253,063✔
171
      uint64_t b1 = load_be<uint64_t>(&m_counter[off], 1);
253,063✔
172
      b1 += counter;
253,063✔
173
      b0 += (b1 < counter) ? 1 : 0;  // carry
253,063✔
174

175
      for(size_t i = 0; i != ctr_blocks; ++i) {
4,302,071✔
176
         store_be(b0, &m_counter[i * BS + off]);
4,049,008✔
177
         store_be(b1, &m_counter[i * BS + off + 8]);
4,049,008✔
178
         b1 += 1;
4,049,008✔
179
         b0 += (b1 == 0);  // carry
4,049,008✔
180
      }
181
   } else {
182
      for(size_t i = 0; i != ctr_blocks; ++i) {
×
183
         uint64_t local_counter = counter;
×
184
         uint16_t carry = static_cast<uint8_t>(local_counter);
×
185
         for(size_t j = 0; (carry || local_counter) && j != ctr_size; ++j) {
×
186
            const size_t off = i * BS + (BS - 1 - j);
×
187
            const uint16_t cnt = static_cast<uint16_t>(m_counter[off]) + carry;
×
188
            m_counter[off] = static_cast<uint8_t>(cnt);
×
189
            local_counter = (local_counter >> 8);
×
190
            carry = (cnt >> 8) + static_cast<uint8_t>(local_counter);
×
191
         }
192
      }
193
   }
194
}
268,679✔
195

196
void CTR_BE::seek(uint64_t offset) {
232,139✔
197
   assert_key_material_set();
232,139✔
198

199
   const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
230,187✔
200

201
   zeroise(m_counter);
230,187✔
202
   buffer_insert(m_counter, 0, m_iv);
230,187✔
203

204
   const size_t BS = m_block_size;
230,187✔
205

206
   // Set m_counter blocks to IV, IV + 1, ... IV + n
207

208
   if(m_ctr_size == 4 && BS >= 8) {
230,187✔
209
      const uint32_t low32 = load_be<uint32_t>(&m_counter[BS - 4], 0);
14,497✔
210

211
      if(m_ctr_blocks >= 4 && is_power_of_2(m_ctr_blocks)) {
14,497✔
212
         size_t written = 1;
213
         while(written < m_ctr_blocks) {
69,656✔
214
            copy_mem(&m_counter[written * BS], &m_counter[0], BS * written);
55,159✔
215
            written *= 2;
55,159✔
216
         }
217
      } else {
218
         for(size_t i = 1; i != m_ctr_blocks; ++i) {
×
219
            copy_mem(&m_counter[i * BS], &m_counter[0], BS - 4);
×
220
         }
221
      }
222

223
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
210,808✔
224
         const uint32_t c = static_cast<uint32_t>(low32 + i);
196,311✔
225
         store_be(c, &m_counter[(BS - 4) + i * BS]);
196,311✔
226
      }
227
   } else {
228
      // do everything sequentially:
229
      for(size_t i = 1; i != m_ctr_blocks; ++i) {
3,404,768✔
230
         buffer_insert(m_counter, i * BS, &m_counter[(i - 1) * BS], BS);
3,189,078✔
231

232
         for(size_t j = 0; j != m_ctr_size; ++j)
3,191,243✔
233
            if(++m_counter[i * BS + (BS - 1 - j)])
3,191,204✔
234
               break;
235
      }
236
   }
237

238
   if(base_counter > 0)
230,187✔
239
      add_counter(base_counter);
156✔
240

241
   m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
230,187✔
242
   m_pad_pos = offset % m_counter.size();
230,187✔
243
}
230,187✔
244
}
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