Botan  2.8.0
Crypto and TLS for C++11
ocb.cpp
Go to the documentation of this file.
1 /*
2 * OCB Mode
3 * (C) 2013,2017 Jack Lloyd
4 * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/ocb.h>
10 #include <botan/block_cipher.h>
11 #include <botan/internal/poly_dbl.h>
12 #include <botan/internal/bit_ops.h>
13 
14 namespace Botan {
15 
16 // Has to be in Botan namespace so unique_ptr can reference it
17 class L_computer final
18  {
19  public:
20  explicit L_computer(const BlockCipher& cipher) :
21  m_BS(cipher.block_size()),
22  m_max_blocks(cipher.parallel_bytes() / m_BS)
23  {
24  m_L_star.resize(m_BS);
25  cipher.encrypt(m_L_star);
26  m_L_dollar = poly_double(star());
27  m_L.push_back(poly_double(dollar()));
28 
29  while(m_L.size() < 8)
30  m_L.push_back(poly_double(m_L.back()));
31 
32  m_offset_buf.resize(m_BS * m_max_blocks);
33  }
34 
35  void init(const secure_vector<uint8_t>& offset)
36  {
37  m_offset = offset;
38  }
39 
40  bool initialized() const { return m_offset.empty() == false; }
41 
42  const secure_vector<uint8_t>& star() const { return m_L_star; }
43  const secure_vector<uint8_t>& dollar() const { return m_L_dollar; }
44  const secure_vector<uint8_t>& offset() const { return m_offset; }
45 
46  const secure_vector<uint8_t>& get(size_t i) const
47  {
48  while(m_L.size() <= i)
49  m_L.push_back(poly_double(m_L.back()));
50 
51  return m_L[i];
52  }
53 
54  const uint8_t*
55  compute_offsets(size_t block_index, size_t blocks)
56  {
57  BOTAN_ASSERT(blocks <= m_max_blocks, "OCB offsets");
58 
59  uint8_t* offsets = m_offset_buf.data();
60 
61  if(block_index % 4 == 0)
62  {
63  const secure_vector<uint8_t>& L0 = get(0);
64  const secure_vector<uint8_t>& L1 = get(1);
65 
66  while(blocks >= 4)
67  {
68  // ntz(4*i+1) == 0
69  // ntz(4*i+2) == 1
70  // ntz(4*i+3) == 0
71  block_index += 4;
72  const size_t ntz4 = ctz<uint32_t>(static_cast<uint32_t>(block_index));
73 
74  xor_buf(offsets, m_offset.data(), L0.data(), m_BS);
75  offsets += m_BS;
76 
77  xor_buf(offsets, offsets - m_BS, L1.data(), m_BS);
78  offsets += m_BS;
79 
80  xor_buf(m_offset.data(), L1.data(), m_BS);
81  copy_mem(offsets, m_offset.data(), m_BS);
82  offsets += m_BS;
83 
84  xor_buf(m_offset.data(), get(ntz4).data(), m_BS);
85  copy_mem(offsets, m_offset.data(), m_BS);
86  offsets += m_BS;
87 
88  blocks -= 4;
89  }
90  }
91 
92  for(size_t i = 0; i != blocks; ++i)
93  { // could be done in parallel
94  const size_t ntz = ctz<uint32_t>(static_cast<uint32_t>(block_index + i + 1));
95  xor_buf(m_offset.data(), get(ntz).data(), m_BS);
96  copy_mem(offsets, m_offset.data(), m_BS);
97  offsets += m_BS;
98  }
99 
100  return m_offset_buf.data();
101  }
102 
103  private:
104  secure_vector<uint8_t> poly_double(const secure_vector<uint8_t>& in) const
105  {
106  secure_vector<uint8_t> out(in.size());
107  poly_double_n(out.data(), in.data(), out.size());
108  return out;
109  }
110 
111  const size_t m_BS, m_max_blocks;
112  secure_vector<uint8_t> m_L_dollar, m_L_star;
113  secure_vector<uint8_t> m_offset;
114  mutable std::vector<secure_vector<uint8_t>> m_L;
115  secure_vector<uint8_t> m_offset_buf;
116  };
117 
118 namespace {
119 
120 /*
121 * OCB's HASH
122 */
123 secure_vector<uint8_t> ocb_hash(const L_computer& L,
124  const BlockCipher& cipher,
125  const uint8_t ad[], size_t ad_len)
126  {
127  const size_t BS = cipher.block_size();
128  secure_vector<uint8_t> sum(BS);
129  secure_vector<uint8_t> offset(BS);
130 
131  secure_vector<uint8_t> buf(BS);
132 
133  const size_t ad_blocks = (ad_len / BS);
134  const size_t ad_remainder = (ad_len % BS);
135 
136  for(size_t i = 0; i != ad_blocks; ++i)
137  {
138  // this loop could run in parallel
139  offset ^= L.get(ctz<uint32_t>(static_cast<uint32_t>(i+1)));
140  buf = offset;
141  xor_buf(buf.data(), &ad[BS*i], BS);
142  cipher.encrypt(buf);
143  sum ^= buf;
144  }
145 
146  if(ad_remainder)
147  {
148  offset ^= L.star();
149  buf = offset;
150  xor_buf(buf.data(), &ad[BS*ad_blocks], ad_remainder);
151  buf[ad_remainder] ^= 0x80;
152  cipher.encrypt(buf);
153  sum ^= buf;
154  }
155 
156  return sum;
157  }
158 
159 }
160 
161 OCB_Mode::OCB_Mode(BlockCipher* cipher, size_t tag_size) :
162  m_cipher(cipher),
163  m_checksum(m_cipher->parallel_bytes()),
164  m_ad_hash(m_cipher->block_size()),
165  m_tag_size(tag_size),
166  m_block_size(m_cipher->block_size()),
167  m_par_blocks(m_cipher->parallel_bytes() / m_block_size)
168  {
169  const size_t BS = block_size();
170 
171  /*
172  * draft-krovetz-ocb-wide-d1 specifies OCB for several other block
173  * sizes but only 128, 192, 256 and 512 bit are currently supported
174  * by this implementation.
175  */
176  BOTAN_ARG_CHECK(BS == 16 || BS == 24 || BS == 32 || BS == 64,
177  "Invalid block size for OCB");
178 
179  BOTAN_ARG_CHECK(m_tag_size % 4 == 0 &&
180  m_tag_size >= 8 && m_tag_size <= BS &&
181  m_tag_size <= 32,
182  "Invalid OCB tag length");
183  }
184 
185 OCB_Mode::~OCB_Mode() { /* for unique_ptr destructor */ }
186 
188  {
189  m_cipher->clear();
190  m_L.reset(); // add clear here?
191  reset();
192  }
193 
195  {
196  m_block_index = 0;
199  m_last_nonce.clear();
200  m_stretch.clear();
201  }
202 
203 bool OCB_Mode::valid_nonce_length(size_t length) const
204  {
205  if(length == 0)
206  return false;
207  if(block_size() == 16)
208  return length < 16;
209  else
210  return length < (block_size() - 1);
211  }
212 
213 std::string OCB_Mode::name() const
214  {
215  return m_cipher->name() + "/OCB"; // include tag size?
216  }
217 
219  {
220  return (m_par_blocks * block_size());
221  }
222 
224  {
225  return m_cipher->key_spec();
226  }
227 
228 void OCB_Mode::key_schedule(const uint8_t key[], size_t length)
229  {
230  m_cipher->set_key(key, length);
231  m_L.reset(new L_computer(*m_cipher));
232  }
233 
234 void OCB_Mode::set_associated_data(const uint8_t ad[], size_t ad_len)
235  {
236  verify_key_set(m_L != nullptr);
237  m_ad_hash = ocb_hash(*m_L, *m_cipher, ad, ad_len);
238  }
239 
241 OCB_Mode::update_nonce(const uint8_t nonce[], size_t nonce_len)
242  {
243  const size_t BS = block_size();
244 
245  BOTAN_ASSERT(BS == 16 || BS == 24 || BS == 32 || BS == 64,
246  "OCB block size is supported");
247 
248  const size_t MASKLEN = (BS == 16 ? 6 : ((BS == 24) ? 7 : 8));
249 
250  const uint8_t BOTTOM_MASK =
251  static_cast<uint8_t>((static_cast<uint16_t>(1) << MASKLEN) - 1);
252 
253  secure_vector<uint8_t> nonce_buf(BS);
254 
255  copy_mem(&nonce_buf[BS - nonce_len], nonce, nonce_len);
256  nonce_buf[0] = static_cast<uint8_t>(((tag_size()*8) % (BS*8)) << (BS <= 16 ? 1 : 0));
257 
258  nonce_buf[BS - nonce_len - 1] ^= 1;
259 
260  const uint8_t bottom = nonce_buf[BS-1] & BOTTOM_MASK;
261  nonce_buf[BS-1] &= ~BOTTOM_MASK;
262 
263  const bool need_new_stretch = (m_last_nonce != nonce_buf);
264 
265  if(need_new_stretch)
266  {
267  m_last_nonce = nonce_buf;
268 
269  m_cipher->encrypt(nonce_buf);
270 
271  /*
272  The loop bounds (BS vs BS/2) are derived from the relation
273  between the block size and the MASKLEN. Using the terminology
274  of draft-krovetz-ocb-wide, we have to derive enough bits in
275  ShiftedKtop to read up to BLOCKLEN+bottom bits from Stretch.
276 
277  +----------+---------+-------+---------+
278  | BLOCKLEN | RESIDUE | SHIFT | MASKLEN |
279  +----------+---------+-------+---------+
280  | 32 | 141 | 17 | 4 |
281  | 64 | 27 | 25 | 5 |
282  | 96 | 1601 | 33 | 6 |
283  | 128 | 135 | 8 | 6 |
284  | 192 | 135 | 40 | 7 |
285  | 256 | 1061 | 1 | 8 |
286  | 384 | 4109 | 80 | 8 |
287  | 512 | 293 | 176 | 8 |
288  | 1024 | 524355 | 352 | 9 |
289  +----------+---------+-------+---------+
290  */
291  if(BS == 16)
292  {
293  for(size_t i = 0; i != BS / 2; ++i)
294  nonce_buf.push_back(nonce_buf[i] ^ nonce_buf[i+1]);
295  }
296  else if(BS == 24)
297  {
298  for(size_t i = 0; i != 16; ++i)
299  nonce_buf.push_back(nonce_buf[i] ^ nonce_buf[i+5]);
300  }
301  else if(BS == 32)
302  {
303  for(size_t i = 0; i != BS; ++i)
304  nonce_buf.push_back(nonce_buf[i] ^ (nonce_buf[i] << 1) ^ (nonce_buf[i+1] >> 7));
305  }
306  else if(BS == 64)
307  {
308  for(size_t i = 0; i != BS / 2; ++i)
309  nonce_buf.push_back(nonce_buf[i] ^ nonce_buf[i+22]);
310  }
311 
312  m_stretch = nonce_buf;
313  }
314 
315  // now set the offset from stretch and bottom
316  const size_t shift_bytes = bottom / 8;
317  const size_t shift_bits = bottom % 8;
318 
319  BOTAN_ASSERT(m_stretch.size() >= BS + shift_bytes + 1, "Size ok");
320 
321  secure_vector<uint8_t> offset(BS);
322  for(size_t i = 0; i != BS; ++i)
323  {
324  offset[i] = (m_stretch[i+shift_bytes] << shift_bits);
325  offset[i] |= (m_stretch[i+shift_bytes+1] >> (8-shift_bits));
326  }
327 
328  return offset;
329  }
330 
331 void OCB_Mode::start_msg(const uint8_t nonce[], size_t nonce_len)
332  {
333  if(!valid_nonce_length(nonce_len))
334  throw Invalid_IV_Length(name(), nonce_len);
335 
336  verify_key_set(m_L != nullptr);
337 
338  m_L->init(update_nonce(nonce, nonce_len));
340  m_block_index = 0;
341  }
342 
343 void OCB_Encryption::encrypt(uint8_t buffer[], size_t blocks)
344  {
345  verify_key_set(m_L != nullptr);
346  BOTAN_STATE_CHECK(m_L->initialized());
347 
348  const size_t BS = block_size();
349 
350  while(blocks)
351  {
352  const size_t proc_blocks = std::min(blocks, par_blocks());
353  const size_t proc_bytes = proc_blocks * BS;
354 
355  const uint8_t* offsets = m_L->compute_offsets(m_block_index, proc_blocks);
356 
357  xor_buf(m_checksum.data(), buffer, proc_bytes);
358 
359  m_cipher->encrypt_n_xex(buffer, offsets, proc_blocks);
360 
361  buffer += proc_bytes;
362  blocks -= proc_blocks;
363  m_block_index += proc_blocks;
364  }
365  }
366 
367 size_t OCB_Encryption::process(uint8_t buf[], size_t sz)
368  {
369  BOTAN_ASSERT(sz % update_granularity() == 0, "Invalid OCB input size");
370  encrypt(buf, sz / block_size());
371  return sz;
372  }
373 
374 void OCB_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
375  {
376  verify_key_set(m_L != nullptr);
377 
378  const size_t BS = block_size();
379 
380  BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
381  const size_t sz = buffer.size() - offset;
382  uint8_t* buf = buffer.data() + offset;
383 
384  secure_vector<uint8_t> mac(BS);
385 
386  if(sz)
387  {
388  const size_t final_full_blocks = sz / BS;
389  const size_t remainder_bytes = sz - (final_full_blocks * BS);
390 
391  encrypt(buf, final_full_blocks);
392  mac = m_L->offset();
393 
394  if(remainder_bytes)
395  {
396  BOTAN_ASSERT(remainder_bytes < BS, "Only a partial block left");
397  uint8_t* remainder = &buf[sz - remainder_bytes];
398 
399  xor_buf(m_checksum.data(), remainder, remainder_bytes);
400  m_checksum[remainder_bytes] ^= 0x80;
401 
402  // Offset_*
403  mac ^= m_L->star();
404 
405  secure_vector<uint8_t> pad(BS);
406  m_cipher->encrypt(mac, pad);
407  xor_buf(remainder, pad.data(), remainder_bytes);
408  }
409  }
410  else
411  {
412  mac = m_L->offset();
413  }
414 
415  // now compute the tag
416 
417  // fold checksum
418  for(size_t i = 0; i != m_checksum.size(); i += BS)
419  {
420  xor_buf(mac.data(), m_checksum.data() + i, BS);
421  }
422 
423  xor_buf(mac.data(), m_L->dollar().data(), BS);
424  m_cipher->encrypt(mac);
425  xor_buf(mac.data(), m_ad_hash.data(), BS);
426 
427  buffer += std::make_pair(mac.data(), tag_size());
428 
430  m_block_index = 0;
431  }
432 
433 void OCB_Decryption::decrypt(uint8_t buffer[], size_t blocks)
434  {
435  verify_key_set(m_L != nullptr);
436  BOTAN_STATE_CHECK(m_L->initialized());
437 
438  const size_t BS = block_size();
439 
440  while(blocks)
441  {
442  const size_t proc_blocks = std::min(blocks, par_blocks());
443  const size_t proc_bytes = proc_blocks * BS;
444 
445  const uint8_t* offsets = m_L->compute_offsets(m_block_index, proc_blocks);
446 
447  m_cipher->decrypt_n_xex(buffer, offsets, proc_blocks);
448 
449  xor_buf(m_checksum.data(), buffer, proc_bytes);
450 
451  buffer += proc_bytes;
452  blocks -= proc_blocks;
453  m_block_index += proc_blocks;
454  }
455  }
456 
457 size_t OCB_Decryption::process(uint8_t buf[], size_t sz)
458  {
459  BOTAN_ASSERT(sz % update_granularity() == 0, "Invalid OCB input size");
460  decrypt(buf, sz / block_size());
461  return sz;
462  }
463 
464 void OCB_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
465  {
466  verify_key_set(m_L != nullptr);
467 
468  const size_t BS = block_size();
469 
470  BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
471  const size_t sz = buffer.size() - offset;
472  uint8_t* buf = buffer.data() + offset;
473 
474  BOTAN_ASSERT(sz >= tag_size(), "We have the tag");
475 
476  const size_t remaining = sz - tag_size();
477 
478  secure_vector<uint8_t> mac(BS);
479 
480  if(remaining)
481  {
482  const size_t final_full_blocks = remaining / BS;
483  const size_t final_bytes = remaining - (final_full_blocks * BS);
484 
485  decrypt(buf, final_full_blocks);
486  mac ^= m_L->offset();
487 
488  if(final_bytes)
489  {
490  BOTAN_ASSERT(final_bytes < BS, "Only a partial block left");
491 
492  uint8_t* remainder = &buf[remaining - final_bytes];
493 
494  mac ^= m_L->star();
495  secure_vector<uint8_t> pad(BS);
496  m_cipher->encrypt(mac, pad); // P_*
497  xor_buf(remainder, pad.data(), final_bytes);
498 
499  xor_buf(m_checksum.data(), remainder, final_bytes);
500  m_checksum[final_bytes] ^= 0x80;
501  }
502  }
503  else
504  mac = m_L->offset();
505 
506  // compute the mac
507 
508  // fold checksum
509  for(size_t i = 0; i != m_checksum.size(); i += BS)
510  {
511  xor_buf(mac.data(), m_checksum.data() + i, BS);
512  }
513 
514  mac ^= m_L->dollar();
515  m_cipher->encrypt(mac);
516  mac ^= m_ad_hash;
517 
518  // reset state
520  m_block_index = 0;
521 
522  // compare mac
523  const uint8_t* included_tag = &buf[remaining];
524 
525  if(!constant_time_compare(mac.data(), included_tag, tag_size()))
526  throw Integrity_Failure("OCB tag check failed");
527 
528  // remove tag from end of message
529  buffer.resize(remaining + offset);
530  }
531 
532 }
std::unique_ptr< BlockCipher > m_cipher
Definition: ocb.h:63
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: ocb.cpp:374
void verify_key_set(bool cond) const
Definition: sym_algo.h:89
bool valid_nonce_length(size_t) const override
Definition: ocb.cpp:203
void set_associated_data(const uint8_t ad[], size_t ad_len) override
Definition: ocb.cpp:234
int(* final)(unsigned char *, CTX *)
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.cpp:51
#define BOTAN_STATE_CHECK(expr)
Definition: assert.h:49
size_t tag_size() const override
Definition: ocb.h:44
void reset() override
Definition: ocb.cpp:194
size_t par_blocks() const
Definition: ocb.h:59
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: ocb.cpp:464
size_t update_granularity() const override
Definition: ocb.cpp:218
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
Definition: alg_id.cpp:13
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
std::unique_ptr< L_computer > m_L
Definition: ocb.h:64
size_t block_size() const
Definition: ocb.h:58
int(* init)(CTX *)
secure_vector< uint8_t > m_ad_hash
Definition: ocb.h:69
OCB_Mode(BlockCipher *cipher, size_t tag_size)
Definition: ocb.cpp:161
secure_vector< uint8_t > m_checksum
Definition: ocb.h:68
size_t process(uint8_t buf[], size_t size) override
Definition: ocb.cpp:367
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
size_t m_block_index
Definition: ocb.h:66
void clear() override
Definition: ocb.cpp:187
std::string name() const override
Definition: ocb.cpp:213
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
Definition: poly_dbl.cpp:63
size_t process(uint8_t buf[], size_t size) override
Definition: ocb.cpp:457
Key_Length_Specification key_spec() const override
Definition: ocb.cpp:223
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183