Coverage Report

Created: 2023-02-13 06:21

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