Coverage Report

Created: 2020-02-14 15:38

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