Coverage Report

Created: 2020-06-30 13:58

/src/botan/src/lib/stream/ctr/ctr.cpp
Line
Count
Source (jump to first uncovered line)
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/ctr.h>
9
#include <botan/exceptn.h>
10
#include <botan/loadstor.h>
11
#include <botan/internal/bit_ops.h>
12
13
namespace Botan {
14
15
CTR_BE::CTR_BE(BlockCipher* ciph) :
16
   m_cipher(ciph),
17
   m_block_size(m_cipher->block_size()),
18
   m_ctr_size(m_block_size),
19
   m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
20
   m_counter(m_cipher->parallel_bytes()),
21
   m_pad(m_counter.size()),
22
   m_pad_pos(0)
23
0
   {
24
0
   }
25
26
CTR_BE::CTR_BE(BlockCipher* cipher, size_t ctr_size) :
27
   m_cipher(cipher),
28
   m_block_size(m_cipher->block_size()),
29
   m_ctr_size(ctr_size),
30
   m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
31
   m_counter(m_cipher->parallel_bytes()),
32
   m_pad(m_counter.size()),
33
   m_pad_pos(0)
34
943
   {
35
943
   BOTAN_ARG_CHECK(m_ctr_size >= 4 && m_ctr_size <= m_block_size,
36
943
                   "Invalid CTR-BE counter size");
37
943
   }
38
39
void CTR_BE::clear()
40
0
   {
41
0
   m_cipher->clear();
42
0
   zeroise(m_pad);
43
0
   zeroise(m_counter);
44
0
   zap(m_iv);
45
0
   m_pad_pos = 0;
46
0
   }
47
48
size_t CTR_BE::default_iv_length() const
49
0
   {
50
0
   return m_block_size;
51
0
   }
52
53
bool CTR_BE::valid_iv_length(size_t iv_len) const
54
3.01k
   {
55
3.01k
   return (iv_len <= m_block_size);
56
3.01k
   }
57
58
Key_Length_Specification CTR_BE::key_spec() const
59
1.88k
   {
60
1.88k
   return m_cipher->key_spec();
61
1.88k
   }
62
63
CTR_BE* CTR_BE::clone() const
64
0
   {
65
0
   return new CTR_BE(m_cipher->clone(), m_ctr_size);
66
0
   }
67
68
void CTR_BE::key_schedule(const uint8_t key[], size_t key_len)
69
943
   {
70
943
   m_cipher->set_key(key, key_len);
71
943
72
943
   // Set a default all-zeros IV
73
943
   set_iv(nullptr, 0);
74
943
   }
75
76
std::string CTR_BE::name() const
77
0
   {
78
0
   if(m_ctr_size == m_block_size)
79
0
      return ("CTR-BE(" + m_cipher->name() + ")");
80
0
   else
81
0
      return ("CTR-BE(" + m_cipher->name() + "," + std::to_string(m_ctr_size) + ")");
82
0
83
0
   }
84
85
void CTR_BE::cipher(const uint8_t in[], uint8_t out[], size_t length)
86
3.19k
   {
87
3.19k
   verify_key_set(m_iv.empty() == false);
88
3.19k
89
3.19k
   const uint8_t* pad_bits = &m_pad[0];
90
3.19k
   const size_t pad_size = m_pad.size();
91
3.19k
92
3.19k
   if(m_pad_pos > 0)
93
1.12k
      {
94
1.12k
      const size_t avail = pad_size - m_pad_pos;
95
1.12k
      const size_t take = std::min(length, avail);
96
1.12k
      xor_buf(out, in, pad_bits + m_pad_pos, take);
97
1.12k
      length -= take;
98
1.12k
      in += take;
99
1.12k
      out += take;
100
1.12k
      m_pad_pos += take;
101
1.12k
102
1.12k
      if(take == avail)
103
211
         {
104
211
         add_counter(m_ctr_blocks);
105
211
         m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
106
211
         m_pad_pos = 0;
107
211
         }
108
1.12k
      }
109
3.19k
110
8.16k
   while(length >= pad_size)
111
4.97k
      {
112
4.97k
      xor_buf(out, in, pad_bits, pad_size);
113
4.97k
      length -= pad_size;
114
4.97k
      in += pad_size;
115
4.97k
      out += pad_size;
116
4.97k
117
4.97k
      add_counter(m_ctr_blocks);
118
4.97k
      m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
119
4.97k
      }
120
3.19k
121
3.19k
   xor_buf(out, in, pad_bits, length);
122
3.19k
   m_pad_pos += length;
123
3.19k
   }
124
125
void CTR_BE::set_iv(const uint8_t iv[], size_t iv_len)
126
3.01k
   {
127
3.01k
   if(!valid_iv_length(iv_len))
128
0
      throw Invalid_IV_Length(name(), iv_len);
129
3.01k
130
3.01k
   m_iv.resize(m_block_size);
131
3.01k
   zeroise(m_iv);
132
3.01k
   buffer_insert(m_iv, 0, iv, iv_len);
133
3.01k
134
3.01k
   seek(0);
135
3.01k
   }
136
137
void CTR_BE::add_counter(const uint64_t counter)
138
5.18k
   {
139
5.18k
   const size_t ctr_size = m_ctr_size;
140
5.18k
   const size_t ctr_blocks = m_ctr_blocks;
141
5.18k
   const size_t BS = m_block_size;
142
5.18k
143
5.18k
   if(ctr_size == 4)
144
5.18k
      {
145
5.18k
      const size_t off = (BS - 4);
146
5.18k
      const uint32_t low32 = static_cast<uint32_t>(counter + load_be<uint32_t>(&m_counter[off], 0));
147
5.18k
148
29.4k
      for(size_t i = 0; i != ctr_blocks; ++i)
149
24.2k
         {
150
24.2k
         store_be(uint32_t(low32 + i), &m_counter[i*BS+off]);
151
24.2k
         }
152
5.18k
      }
153
0
   else if(ctr_size == 8)
154
0
      {
155
0
      const size_t off = (BS - 8);
156
0
      const uint64_t low64 = counter + load_be<uint64_t>(&m_counter[off], 0);
157
0
158
0
      for(size_t i = 0; i != ctr_blocks; ++i)
159
0
         {
160
0
         store_be(uint64_t(low64 + i), &m_counter[i*BS+off]);
161
0
         }
162
0
      }
163
0
   else if(ctr_size == 16)
164
0
      {
165
0
      const size_t off = (BS - 16);
166
0
      uint64_t b0 = load_be<uint64_t>(&m_counter[off], 0);
167
0
      uint64_t b1 = load_be<uint64_t>(&m_counter[off], 1);
168
0
      b1 += counter;
169
0
      b0 += (b1 < counter) ? 1 : 0; // carry
170
0
171
0
      for(size_t i = 0; i != ctr_blocks; ++i)
172
0
         {
173
0
         store_be(b0, &m_counter[i*BS+off]);
174
0
         store_be(b1, &m_counter[i*BS+off+8]);
175
0
         b1 += 1;
176
0
         b0 += (b1 == 0); // carry
177
0
         }
178
0
      }
179
0
   else
180
0
      {
181
0
      for(size_t i = 0; i != ctr_blocks; ++i)
182
0
         {
183
0
         uint64_t local_counter = counter;
184
0
         uint16_t carry = static_cast<uint8_t>(local_counter);
185
0
         for(size_t j = 0; (carry || local_counter) && j != ctr_size; ++j)
186
0
            {
187
0
            const size_t off = i*BS + (BS-1-j);
188
0
            const uint16_t cnt = static_cast<uint16_t>(m_counter[off]) + carry;
189
0
            m_counter[off] = static_cast<uint8_t>(cnt);
190
0
            local_counter = (local_counter >> 8);
191
0
            carry = (cnt >> 8) + static_cast<uint8_t>(local_counter);
192
0
            }
193
0
         }
194
0
      }
195
5.18k
   }
196
197
void CTR_BE::seek(uint64_t offset)
198
3.01k
   {
199
3.01k
   verify_key_set(m_iv.empty() == false);
200
3.01k
201
3.01k
   const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
202
3.01k
203
3.01k
   zeroise(m_counter);
204
3.01k
   buffer_insert(m_counter, 0, m_iv);
205
3.01k
206
3.01k
   const size_t BS = m_block_size;
207
3.01k
208
3.01k
   // Set m_counter blocks to IV, IV + 1, ... IV + n
209
3.01k
210
3.01k
   if(m_ctr_size == 4 && BS >= 8)
211
3.01k
      {
212
3.01k
      const uint32_t low32 = load_be<uint32_t>(&m_counter[BS-4], 0);
213
3.01k
214
3.01k
      if(m_ctr_blocks >= 4 && is_power_of_2(m_ctr_blocks))
215
3.01k
         {
216
3.01k
         size_t written = 1;
217
11.9k
         while(written < m_ctr_blocks)
218
8.92k
            {
219
8.92k
            copy_mem(&m_counter[written*BS], &m_counter[0], BS*written);
220
8.92k
            written *= 2;
221
8.92k
            }
222
3.01k
         }
223
0
      else
224
0
         {
225
0
         for(size_t i = 1; i != m_ctr_blocks; ++i)
226
0
            {
227
0
            copy_mem(&m_counter[i*BS], &m_counter[0], BS - 4);
228
0
            }
229
0
         }
230
3.01k
231
29.4k
      for(size_t i = 1; i != m_ctr_blocks; ++i)
232
26.4k
         {
233
26.4k
         const uint32_t c = static_cast<uint32_t>(low32 + i);
234
26.4k
         store_be(c, &m_counter[(BS-4)+i*BS]);
235
26.4k
         }
236
3.01k
      }
237
0
   else
238
0
      {
239
0
      // do everything sequentially:
240
0
      for(size_t i = 1; i != m_ctr_blocks; ++i)
241
0
         {
242
0
         buffer_insert(m_counter, i*BS, &m_counter[(i-1)*BS], BS);
243
0
244
0
         for(size_t j = 0; j != m_ctr_size; ++j)
245
0
            if(++m_counter[i*BS + (BS - 1 - j)])
246
0
               break;
247
0
         }
248
0
      }
249
3.01k
250
3.01k
   if(base_counter > 0)
251
0
      add_counter(base_counter);
252
3.01k
253
3.01k
   m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
254
3.01k
   m_pad_pos = offset % m_counter.size();
255
3.01k
   }
256
}