• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.26
/src/lib/modes/aead/ocb/ocb.cpp
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

11
#include <botan/block_cipher.h>
12
#include <botan/internal/bit_ops.h>
13
#include <botan/internal/poly_dbl.h>
14

15
namespace Botan {
16

17
// Has to be in Botan namespace so unique_ptr can reference it
18
class L_computer final {
19
   public:
20
      explicit L_computer(const BlockCipher& cipher) :
243✔
21
            m_BS(cipher.block_size()), m_max_blocks(cipher.parallel_bytes() / m_BS) {
486✔
22
         m_L_star.resize(m_BS);
243✔
23
         cipher.encrypt(m_L_star);
243✔
24
         m_L_dollar = poly_double(star());
243✔
25
         m_L.push_back(poly_double(dollar()));
243✔
26

27
         while(m_L.size() < 8) {
1,944✔
28
            m_L.push_back(poly_double(m_L.back()));
3,402✔
29
         }
30

31
         m_offset_buf.resize(m_BS * m_max_blocks);
243✔
32
      }
243✔
33

34
      void init(const secure_vector<uint8_t>& offset) { m_offset = offset; }
9,445✔
35

36
      bool initialized() const { return m_offset.empty() == false; }
16,514✔
37

38
      const secure_vector<uint8_t>& star() const { return m_L_star; }
6,063✔
39

40
      const secure_vector<uint8_t>& dollar() const { return m_L_dollar; }
9,596✔
41

42
      const secure_vector<uint8_t>& offset() const { return m_offset; }
9,353✔
43

44
      const secure_vector<uint8_t>& get(size_t i) const {
71,714✔
45
         while(m_L.size() <= i) {
71,783✔
46
            m_L.push_back(poly_double(m_L.back()));
69✔
47
         }
48

49
         return m_L[i];
71,714✔
50
      }
51

52
      const uint8_t* compute_offsets(size_t block_index, size_t blocks) {
6,283✔
53
         BOTAN_ASSERT(blocks <= m_max_blocks, "OCB offsets");
6,283✔
54

55
         uint8_t* offsets = m_offset_buf.data();
6,283✔
56

57
         if(block_index % 4 == 0) {
6,283✔
58
            const secure_vector<uint8_t>& L0 = get(0);
5,829✔
59
            const secure_vector<uint8_t>& L1 = get(1);
5,829✔
60

61
            while(blocks >= 4) {
15,069✔
62
               // ntz(4*i+1) == 0
63
               // ntz(4*i+2) == 1
64
               // ntz(4*i+3) == 0
65
               block_index += 4;
3,411✔
66
               const size_t ntz4 = var_ctz32(static_cast<uint32_t>(block_index));
3,411✔
67

68
               xor_buf(offsets, m_offset.data(), L0.data(), m_BS);
3,411✔
69
               offsets += m_BS;
3,411✔
70

71
               xor_buf(offsets, offsets - m_BS, L1.data(), m_BS);
3,411✔
72
               offsets += m_BS;
3,411✔
73

74
               xor_buf(m_offset.data(), L1.data(), m_BS);
3,411✔
75
               copy_mem(offsets, m_offset.data(), m_BS);
3,411✔
76
               offsets += m_BS;
3,411✔
77

78
               xor_buf(m_offset.data(), get(ntz4).data(), m_BS);
3,411✔
79
               copy_mem(offsets, m_offset.data(), m_BS);
3,411✔
80
               offsets += m_BS;
3,411✔
81

82
               blocks -= 4;
3,411✔
83
            }
84
         }
85

86
         for(size_t i = 0; i != blocks; ++i) {  // could be done in parallel
15,901✔
87
            const size_t ntz = var_ctz32(static_cast<uint32_t>(block_index + i + 1));
9,618✔
88
            xor_buf(m_offset.data(), get(ntz).data(), m_BS);
9,618✔
89
            copy_mem(offsets, m_offset.data(), m_BS);
9,618✔
90
            offsets += m_BS;
9,618✔
91
         }
92

93
         return m_offset_buf.data();
6,283✔
94
      }
95

96
   private:
97
      static secure_vector<uint8_t> poly_double(const secure_vector<uint8_t>& in) {
2,256✔
98
         secure_vector<uint8_t> out(in.size());
2,256✔
99
         poly_double_n(out.data(), in.data(), out.size());
2,256✔
100
         return out;
2,256✔
101
      }
×
102

103
      const size_t m_BS, m_max_blocks;
104
      secure_vector<uint8_t> m_L_dollar, m_L_star;
105
      secure_vector<uint8_t> m_offset;
106
      mutable std::vector<secure_vector<uint8_t>> m_L;
107
      secure_vector<uint8_t> m_offset_buf;
108
};
109

110
namespace {
111

112
/*
113
* OCB's HASH
114
*/
115
secure_vector<uint8_t> ocb_hash(const L_computer& L, const BlockCipher& cipher, const uint8_t ad[], size_t ad_len) {
9,495✔
116
   const size_t BS = cipher.block_size();
9,495✔
117
   secure_vector<uint8_t> sum(BS);
9,495✔
118
   secure_vector<uint8_t> offset(BS);
9,495✔
119

120
   secure_vector<uint8_t> buf(BS);
9,495✔
121

122
   const size_t ad_blocks = (ad_len / BS);
9,495✔
123
   const size_t ad_remainder = (ad_len % BS);
9,495✔
124

125
   for(size_t i = 0; i != ad_blocks; ++i) {
56,522✔
126
      // this loop could run in parallel
127
      offset ^= L.get(var_ctz32(static_cast<uint32_t>(i + 1)));
94,054✔
128
      buf = offset;
47,027✔
129
      xor_buf(buf.data(), &ad[BS * i], BS);
47,027✔
130
      cipher.encrypt(buf);
47,027✔
131
      sum ^= buf;
47,027✔
132
   }
133

134
   if(ad_remainder) {
9,495✔
135
      offset ^= L.star();
5,865✔
136
      buf = offset;
5,865✔
137
      xor_buf(buf.data(), &ad[BS * ad_blocks], ad_remainder);
5,865✔
138
      buf[ad_remainder] ^= 0x80;
5,865✔
139
      cipher.encrypt(buf);
5,865✔
140
      sum ^= buf;
5,865✔
141
   }
142

143
   return sum;
9,495✔
144
}
18,990✔
145

146
}  // namespace
147

148
OCB_Mode::OCB_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size) :
335✔
149
      m_cipher(std::move(cipher)),
335✔
150
      m_checksum(m_cipher->parallel_bytes()),
670✔
151
      m_ad_hash(m_cipher->block_size()),
335✔
152
      m_tag_size(tag_size),
335✔
153
      m_block_size(m_cipher->block_size()),
335✔
154
      m_par_blocks(m_cipher->parallel_bytes() / m_block_size) {
1,005✔
155
   const size_t BS = block_size();
335✔
156

157
   /*
158
   * draft-krovetz-ocb-wide-d1 specifies OCB for several other block
159
   * sizes but only 128, 192, 256 and 512 bit are currently supported
160
   * by this implementation.
161
   */
162
   BOTAN_ARG_CHECK(BS == 16 || BS == 24 || BS == 32 || BS == 64, "Invalid block size for OCB");
335✔
163

164
   BOTAN_ARG_CHECK(m_tag_size % 4 == 0 && m_tag_size >= 8 && m_tag_size <= BS && m_tag_size <= 32,
335✔
165
                   "Invalid OCB tag length");
166
}
335✔
167

168
OCB_Mode::~OCB_Mode() = default;
1,977✔
169

170
void OCB_Mode::clear() {
92✔
171
   m_cipher->clear();
92✔
172
   m_L.reset();  // add clear here?
92✔
173
   reset();
92✔
174
}
92✔
175

176
void OCB_Mode::reset() {
410✔
177
   m_block_index = 0;
410✔
178
   zeroise(m_ad_hash);
410✔
179
   zeroise(m_checksum);
410✔
180
   m_last_nonce.clear();
410✔
181
   m_stretch.clear();
410✔
182
}
410✔
183

184
bool OCB_Mode::valid_nonce_length(size_t length) const {
9,491✔
185
   if(length == 0) {
9,491✔
186
      return false;
187
   }
188
   if(block_size() == 16) {
9,491✔
189
      return length < 16;
7,889✔
190
   } else {
191
      return length < (block_size() - 1);
1,602✔
192
   }
193
}
194

195
std::string OCB_Mode::name() const {
506✔
196
   return m_cipher->name() + "/OCB";  // include tag size?
1,012✔
197
}
198

199
size_t OCB_Mode::update_granularity() const {
1,254✔
200
   return block_size();
1,254✔
201
}
202

203
size_t OCB_Mode::ideal_granularity() const {
276✔
204
   return (m_par_blocks * block_size());
276✔
205
}
206

207
Key_Length_Specification OCB_Mode::key_spec() const {
243✔
208
   return m_cipher->key_spec();
243✔
209
}
210

211
bool OCB_Mode::has_keying_material() const {
36,236✔
212
   return m_cipher->has_keying_material();
36,236✔
213
}
214

215
void OCB_Mode::key_schedule(const uint8_t key[], size_t length) {
243✔
216
   m_cipher->set_key(key, length);
243✔
217
   m_L = std::make_unique<L_computer>(*m_cipher);
243✔
218
}
243✔
219

220
void OCB_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
9,679✔
221
   BOTAN_ARG_CHECK(idx == 0, "OCB: cannot handle non-zero index in set_associated_data_n");
9,679✔
222
   assert_key_material_set();
9,679✔
223
   m_ad_hash = ocb_hash(*m_L, *m_cipher, ad.data(), ad.size());
9,495✔
224
}
9,495✔
225

226
const secure_vector<uint8_t>& OCB_Mode::update_nonce(const uint8_t nonce[], size_t nonce_len) {
9,445✔
227
   const size_t BS = block_size();
9,445✔
228

229
   BOTAN_ASSERT(BS == 16 || BS == 24 || BS == 32 || BS == 64, "OCB block size is supported");
9,445✔
230

231
   const size_t MASKLEN = (BS == 16 ? 6 : ((BS == 24) ? 7 : 8));
9,445✔
232

233
   const uint8_t BOTTOM_MASK = static_cast<uint8_t>((static_cast<uint16_t>(1) << MASKLEN) - 1);
9,445✔
234

235
   m_nonce_buf.resize(BS);
9,445✔
236
   clear_mem(&m_nonce_buf[0], m_nonce_buf.size());
9,445✔
237

238
   copy_mem(&m_nonce_buf[BS - nonce_len], nonce, nonce_len);
9,445✔
239
   m_nonce_buf[0] = static_cast<uint8_t>(((tag_size() * 8) % (BS * 8)) << (BS <= 16 ? 1 : 0));
11,043✔
240

241
   m_nonce_buf[BS - nonce_len - 1] ^= 1;
9,445✔
242

243
   const uint8_t bottom = m_nonce_buf[BS - 1] & BOTTOM_MASK;
9,445✔
244
   m_nonce_buf[BS - 1] &= ~BOTTOM_MASK;
9,445✔
245

246
   const bool need_new_stretch = (m_last_nonce != m_nonce_buf);
9,445✔
247

248
   if(need_new_stretch) {
9,445✔
249
      m_last_nonce = m_nonce_buf;
681✔
250

251
      m_cipher->encrypt(m_nonce_buf);
681✔
252

253
      /*
254
      The loop bounds (BS vs BS/2) are derived from the relation
255
      between the block size and the MASKLEN. Using the terminology
256
      of draft-krovetz-ocb-wide, we have to derive enough bits in
257
      ShiftedKtop to read up to BLOCKLEN+bottom bits from Stretch.
258

259
                 +----------+---------+-------+---------+
260
                 | BLOCKLEN | RESIDUE | SHIFT | MASKLEN |
261
                 +----------+---------+-------+---------+
262
                 |       32 |     141 |    17 |    4    |
263
                 |       64 |      27 |    25 |    5    |
264
                 |       96 |    1601 |    33 |    6    |
265
                 |      128 |     135 |     8 |    6    |
266
                 |      192 |     135 |    40 |    7    |
267
                 |      256 |    1061 |     1 |    8    |
268
                 |      384 |    4109 |    80 |    8    |
269
                 |      512 |     293 |   176 |    8    |
270
                 |     1024 |  524355 |   352 |    9    |
271
                 +----------+---------+-------+---------+
272
      */
273
      if(BS == 16) {
681✔
274
         for(size_t i = 0; i != BS / 2; ++i) {
5,517✔
275
            m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 1]);
4,904✔
276
         }
277
      } else if(BS == 24) {
68✔
278
         for(size_t i = 0; i != 16; ++i) {
408✔
279
            m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 5]);
384✔
280
         }
281
      } else if(BS == 32) {
44✔
282
         for(size_t i = 0; i != BS; ++i) {
1,089✔
283
            m_nonce_buf.push_back(m_nonce_buf[i] ^ (m_nonce_buf[i] << 1) ^ (m_nonce_buf[i + 1] >> 7));
1,056✔
284
         }
285
      } else if(BS == 64) {
286
         for(size_t i = 0; i != BS / 2; ++i) {
363✔
287
            m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 22]);
352✔
288
         }
289
      }
290

291
      m_stretch = m_nonce_buf;
681✔
292
   }
293

294
   // now set the offset from stretch and bottom
295
   const size_t shift_bytes = bottom / 8;
9,445✔
296
   const size_t shift_bits = bottom % 8;
9,445✔
297

298
   BOTAN_ASSERT(m_stretch.size() >= BS + shift_bytes + 1, "Size ok");
9,445✔
299

300
   m_offset.resize(BS);
9,445✔
301
   for(size_t i = 0; i != BS; ++i) {
195,501✔
302
      m_offset[i] = (m_stretch[i + shift_bytes] << shift_bits);
186,056✔
303
      m_offset[i] |= (m_stretch[i + shift_bytes + 1] >> (8 - shift_bits));
186,056✔
304
   }
305

306
   return m_offset;
9,445✔
307
}
308

309
void OCB_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
9,445✔
310
   if(!valid_nonce_length(nonce_len)) {
9,445✔
311
      throw Invalid_IV_Length(name(), nonce_len);
×
312
   }
313

314
   assert_key_material_set();
9,445✔
315

316
   m_L->init(update_nonce(nonce, nonce_len));
9,445✔
317
   zeroise(m_checksum);
9,445✔
318
   m_block_index = 0;
9,445✔
319
}
9,445✔
320

321
void OCB_Encryption::encrypt(uint8_t buffer[], size_t blocks) {
4,161✔
322
   assert_key_material_set();
4,161✔
323
   BOTAN_STATE_CHECK(m_L->initialized());
4,115✔
324

325
   const size_t BS = block_size();
4,069✔
326

327
   while(blocks) {
7,674✔
328
      const size_t proc_blocks = std::min(blocks, par_blocks());
3,605✔
329
      const size_t proc_bytes = proc_blocks * BS;
3,605✔
330

331
      const uint8_t* offsets = m_L->compute_offsets(m_block_index, proc_blocks);
3,605✔
332

333
      xor_buf(m_checksum.data(), buffer, proc_bytes);
3,605✔
334

335
      m_cipher->encrypt_n_xex(buffer, offsets, proc_blocks);
3,605✔
336

337
      buffer += proc_bytes;
3,605✔
338
      blocks -= proc_blocks;
3,605✔
339
      m_block_index += proc_blocks;
3,605✔
340
   }
341
}
4,069✔
342

343
size_t OCB_Encryption::process_msg(uint8_t buf[], size_t sz) {
467✔
344
   BOTAN_ARG_CHECK(sz % update_granularity() == 0, "Invalid OCB input size");
467✔
345
   encrypt(buf, sz / block_size());
467✔
346
   return sz;
375✔
347
}
348

349
void OCB_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
5,702✔
350
   assert_key_material_set();
5,702✔
351
   BOTAN_STATE_CHECK(m_L->initialized());
5,610✔
352

353
   const size_t BS = block_size();
5,564✔
354

355
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
5,564✔
356
   const size_t sz = buffer.size() - offset;
5,564✔
357
   uint8_t* buf = buffer.data() + offset;
5,564✔
358

359
   secure_vector<uint8_t> mac(BS);
5,564✔
360

361
   if(sz) {
5,564✔
362
      const size_t final_full_blocks = sz / BS;
3,694✔
363
      const size_t remainder_bytes = sz - (final_full_blocks * BS);
3,694✔
364

365
      encrypt(buf, final_full_blocks);
3,694✔
366
      mac = m_L->offset();
3,694✔
367

368
      if(remainder_bytes) {
3,694✔
369
         BOTAN_ASSERT(remainder_bytes < BS, "Only a partial block left");
3,492✔
370
         uint8_t* remainder = &buf[sz - remainder_bytes];
3,492✔
371

372
         xor_buf(m_checksum.data(), remainder, remainder_bytes);
3,492✔
373
         m_checksum[remainder_bytes] ^= 0x80;
3,492✔
374

375
         // Offset_*
376
         mac ^= m_L->star();
3,492✔
377

378
         secure_vector<uint8_t> pad(BS);
3,492✔
379
         m_cipher->encrypt(mac, pad);
3,492✔
380
         xor_buf(remainder, pad.data(), remainder_bytes);
3,492✔
381
      }
3,492✔
382
   } else {
383
      mac = m_L->offset();
1,870✔
384
   }
385

386
   // now compute the tag
387

388
   // fold checksum
389
   for(size_t i = 0; i != m_checksum.size(); i += BS) {
82,220✔
390
      xor_buf(mac.data(), m_checksum.data() + i, BS);
76,656✔
391
   }
392

393
   xor_buf(mac.data(), m_L->dollar().data(), BS);
5,564✔
394
   m_cipher->encrypt(mac);
5,564✔
395
   xor_buf(mac.data(), m_ad_hash.data(), BS);
5,564✔
396

397
   buffer += std::make_pair(mac.data(), tag_size());
5,564✔
398

399
   zeroise(m_checksum);
5,564✔
400
   m_block_index = 0;
5,564✔
401
}
5,564✔
402

403
void OCB_Decryption::decrypt(uint8_t buffer[], size_t blocks) {
3,000✔
404
   assert_key_material_set();
3,000✔
405
   BOTAN_STATE_CHECK(m_L->initialized());
2,954✔
406

407
   const size_t BS = block_size();
2,908✔
408

409
   while(blocks) {
5,586✔
410
      const size_t proc_blocks = std::min(blocks, par_blocks());
2,678✔
411
      const size_t proc_bytes = proc_blocks * BS;
2,678✔
412

413
      const uint8_t* offsets = m_L->compute_offsets(m_block_index, proc_blocks);
2,678✔
414

415
      m_cipher->decrypt_n_xex(buffer, offsets, proc_blocks);
2,678✔
416

417
      xor_buf(m_checksum.data(), buffer, proc_bytes);
2,678✔
418

419
      buffer += proc_bytes;
2,678✔
420
      blocks -= proc_blocks;
2,678✔
421
      m_block_index += proc_blocks;
2,678✔
422
   }
423
}
2,908✔
424

425
size_t OCB_Decryption::process_msg(uint8_t buf[], size_t sz) {
479✔
426
   BOTAN_ARG_CHECK(sz % update_granularity() == 0, "Invalid OCB input size");
479✔
427
   decrypt(buf, sz / block_size());
479✔
428
   return sz;
387✔
429
}
430

431
void OCB_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
3,927✔
432
   assert_key_material_set();
3,927✔
433
   BOTAN_STATE_CHECK(m_L->initialized());
3,835✔
434

435
   const size_t BS = block_size();
3,789✔
436

437
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
3,789✔
438
   const size_t sz = buffer.size() - offset;
3,789✔
439
   uint8_t* buf = buffer.data() + offset;
3,789✔
440

441
   BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag");
3,789✔
442

443
   const size_t remaining = sz - tag_size();
3,789✔
444

445
   secure_vector<uint8_t> mac(BS);
3,789✔
446

447
   if(remaining) {
3,789✔
448
      const size_t final_full_blocks = remaining / BS;
2,521✔
449
      const size_t final_bytes = remaining - (final_full_blocks * BS);
2,521✔
450

451
      decrypt(buf, final_full_blocks);
2,521✔
452
      mac ^= m_L->offset();
2,521✔
453

454
      if(final_bytes) {
2,521✔
455
         BOTAN_ASSERT(final_bytes < BS, "Only a partial block left");
2,328✔
456

457
         uint8_t* remainder = &buf[remaining - final_bytes];
2,328✔
458

459
         mac ^= m_L->star();
2,328✔
460
         secure_vector<uint8_t> pad(BS);
2,466✔
461
         m_cipher->encrypt(mac, pad);  // P_*
2,328✔
462
         xor_buf(remainder, pad.data(), final_bytes);
2,328✔
463

464
         xor_buf(m_checksum.data(), remainder, final_bytes);
2,328✔
465
         m_checksum[final_bytes] ^= 0x80;
2,328✔
466
      }
2,328✔
467
   } else {
468
      mac = m_L->offset();
1,268✔
469
   }
470

471
   // compute the mac
472

473
   // fold checksum
474
   for(size_t i = 0; i != m_checksum.size(); i += BS) {
64,505✔
475
      xor_buf(mac.data(), m_checksum.data() + i, BS);
60,716✔
476
   }
477

478
   mac ^= m_L->dollar();
3,789✔
479
   m_cipher->encrypt(mac);
3,789✔
480
   mac ^= m_ad_hash;
3,789✔
481

482
   // reset state
483
   zeroise(m_checksum);
3,789✔
484
   m_block_index = 0;
3,789✔
485

486
   // compare mac
487
   const uint8_t* included_tag = &buf[remaining];
3,789✔
488

489
   if(!constant_time_compare(mac.data(), included_tag, tag_size())) {
3,789✔
490
      throw Invalid_Authentication_Tag("OCB tag check failed");
138✔
491
   }
492

493
   // remove tag from end of message
494
   buffer.resize(remaining + offset);
3,651✔
495
}
3,651✔
496

497
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc