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