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