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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

99.25
/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
         m_offset_buf.resize(m_BS * m_max_blocks);
243✔
31
      }
243✔
32

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

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

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

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

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

43
      const secure_vector<uint8_t>& get(size_t i) const {
72,303✔
44
         while(m_L.size() <= i)
72,372✔
45
            m_L.push_back(poly_double(m_L.back()));
69✔
46

47
         return m_L[i];
72,303✔
48
      }
49

50
      const uint8_t* compute_offsets(size_t block_index, size_t blocks) {
6,381✔
51
         BOTAN_ASSERT(blocks <= m_max_blocks, "OCB offsets");
6,381✔
52

53
         uint8_t* offsets = m_offset_buf.data();
6,381✔
54

55
         if(block_index % 4 == 0) {
6,381✔
56
            const secure_vector<uint8_t>& L0 = get(0);
5,927✔
57
            const secure_vector<uint8_t>& L1 = get(1);
5,927✔
58

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

66
               xor_buf(offsets, m_offset.data(), L0.data(), m_BS);
3,795✔
67
               offsets += m_BS;
3,795✔
68

69
               xor_buf(offsets, offsets - m_BS, L1.data(), m_BS);
3,795✔
70
               offsets += m_BS;
3,795✔
71

72
               xor_buf(m_offset.data(), L1.data(), m_BS);
3,795✔
73
               copy_mem(offsets, m_offset.data(), m_BS);
3,795✔
74
               offsets += m_BS;
3,795✔
75

76
               xor_buf(m_offset.data(), get(ntz4).data(), m_BS);
3,795✔
77
               copy_mem(offsets, m_offset.data(), m_BS);
3,795✔
78
               offsets += m_BS;
3,795✔
79

80
               blocks -= 4;
3,795✔
81
            }
82
         }
83

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

91
         return m_offset_buf.data();
6,381✔
92
      }
93

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

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

108
namespace {
109

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

118
   secure_vector<uint8_t> buf(BS);
9,499✔
119

120
   const size_t ad_blocks = (ad_len / BS);
9,499✔
121
   const size_t ad_remainder = (ad_len % BS);
9,499✔
122

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

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

141
   return sum;
9,499✔
142
}
18,998✔
143

144
}  // namespace
145

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

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

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

166
OCB_Mode::~OCB_Mode() = default;
1,977✔
167

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

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

182
bool OCB_Mode::valid_nonce_length(size_t length) const {
9,495✔
183
   if(length == 0)
9,495✔
184
      return false;
185
   if(block_size() == 16)
9,495✔
186
      return length < 16;
7,893✔
187
   else
188
      return length < (block_size() - 1);
1,602✔
189
}
190

191
std::string OCB_Mode::name() const {
506✔
192
   return m_cipher->name() + "/OCB";  // include tag size?
1,012✔
193
}
194

195
size_t OCB_Mode::update_granularity() const { return block_size(); }
1,254✔
196

197
size_t OCB_Mode::ideal_granularity() const { return (m_par_blocks * block_size()); }
276✔
198

199
Key_Length_Specification OCB_Mode::key_spec() const { return m_cipher->key_spec(); }
243✔
200

201
bool OCB_Mode::has_keying_material() const { return m_cipher->has_keying_material(); }
36,252✔
202

203
void OCB_Mode::key_schedule(const uint8_t key[], size_t length) {
243✔
204
   m_cipher->set_key(key, length);
243✔
205
   m_L = std::make_unique<L_computer>(*m_cipher);
243✔
206
}
243✔
207

208
void OCB_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
9,683✔
209
   BOTAN_ARG_CHECK(idx == 0, "OCB: cannot handle non-zero index in set_associated_data_n");
9,683✔
210
   assert_key_material_set();
9,683✔
211
   m_ad_hash = ocb_hash(*m_L, *m_cipher, ad.data(), ad.size());
9,499✔
212
}
9,499✔
213

214
const secure_vector<uint8_t>& OCB_Mode::update_nonce(const uint8_t nonce[], size_t nonce_len) {
9,449✔
215
   const size_t BS = block_size();
9,449✔
216

217
   BOTAN_ASSERT(BS == 16 || BS == 24 || BS == 32 || BS == 64, "OCB block size is supported");
9,449✔
218

219
   const size_t MASKLEN = (BS == 16 ? 6 : ((BS == 24) ? 7 : 8));
9,449✔
220

221
   const uint8_t BOTTOM_MASK = static_cast<uint8_t>((static_cast<uint16_t>(1) << MASKLEN) - 1);
9,449✔
222

223
   m_nonce_buf.resize(BS);
9,449✔
224
   clear_mem(&m_nonce_buf[0], m_nonce_buf.size());
9,449✔
225

226
   copy_mem(&m_nonce_buf[BS - nonce_len], nonce, nonce_len);
9,449✔
227
   m_nonce_buf[0] = static_cast<uint8_t>(((tag_size() * 8) % (BS * 8)) << (BS <= 16 ? 1 : 0));
11,047✔
228

229
   m_nonce_buf[BS - nonce_len - 1] ^= 1;
9,449✔
230

231
   const uint8_t bottom = m_nonce_buf[BS - 1] & BOTTOM_MASK;
9,449✔
232
   m_nonce_buf[BS - 1] &= ~BOTTOM_MASK;
9,449✔
233

234
   const bool need_new_stretch = (m_last_nonce != m_nonce_buf);
9,449✔
235

236
   if(need_new_stretch) {
9,449✔
237
      m_last_nonce = m_nonce_buf;
681✔
238

239
      m_cipher->encrypt(m_nonce_buf);
681✔
240

241
      /*
242
      The loop bounds (BS vs BS/2) are derived from the relation
243
      between the block size and the MASKLEN. Using the terminology
244
      of draft-krovetz-ocb-wide, we have to derive enough bits in
245
      ShiftedKtop to read up to BLOCKLEN+bottom bits from Stretch.
246

247
                 +----------+---------+-------+---------+
248
                 | BLOCKLEN | RESIDUE | SHIFT | MASKLEN |
249
                 +----------+---------+-------+---------+
250
                 |       32 |     141 |    17 |    4    |
251
                 |       64 |      27 |    25 |    5    |
252
                 |       96 |    1601 |    33 |    6    |
253
                 |      128 |     135 |     8 |    6    |
254
                 |      192 |     135 |    40 |    7    |
255
                 |      256 |    1061 |     1 |    8    |
256
                 |      384 |    4109 |    80 |    8    |
257
                 |      512 |     293 |   176 |    8    |
258
                 |     1024 |  524355 |   352 |    9    |
259
                 +----------+---------+-------+---------+
260
      */
261
      if(BS == 16) {
681✔
262
         for(size_t i = 0; i != BS / 2; ++i)
5,517✔
263
            m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 1]);
4,904✔
264
      } else if(BS == 24) {
68✔
265
         for(size_t i = 0; i != 16; ++i)
408✔
266
            m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 5]);
384✔
267
      } else if(BS == 32) {
44✔
268
         for(size_t i = 0; i != BS; ++i)
1,089✔
269
            m_nonce_buf.push_back(m_nonce_buf[i] ^ (m_nonce_buf[i] << 1) ^ (m_nonce_buf[i + 1] >> 7));
1,056✔
270
      } else if(BS == 64) {
271
         for(size_t i = 0; i != BS / 2; ++i)
363✔
272
            m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 22]);
352✔
273
      }
274

275
      m_stretch = m_nonce_buf;
681✔
276
   }
277

278
   // now set the offset from stretch and bottom
279
   const size_t shift_bytes = bottom / 8;
9,449✔
280
   const size_t shift_bits = bottom % 8;
9,449✔
281

282
   BOTAN_ASSERT(m_stretch.size() >= BS + shift_bytes + 1, "Size ok");
9,449✔
283

284
   m_offset.resize(BS);
9,449✔
285
   for(size_t i = 0; i != BS; ++i) {
195,569✔
286
      m_offset[i] = (m_stretch[i + shift_bytes] << shift_bits);
186,120✔
287
      m_offset[i] |= (m_stretch[i + shift_bytes + 1] >> (8 - shift_bits));
186,120✔
288
   }
289

290
   return m_offset;
9,449✔
291
}
292

293
void OCB_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
9,449✔
294
   if(!valid_nonce_length(nonce_len))
9,449✔
295
      throw Invalid_IV_Length(name(), nonce_len);
×
296

297
   assert_key_material_set();
9,449✔
298

299
   m_L->init(update_nonce(nonce, nonce_len));
9,449✔
300
   zeroise(m_checksum);
9,449✔
301
   m_block_index = 0;
9,449✔
302
}
9,449✔
303

304
void OCB_Encryption::encrypt(uint8_t buffer[], size_t blocks) {
4,163✔
305
   assert_key_material_set();
4,163✔
306
   BOTAN_STATE_CHECK(m_L->initialized());
4,117✔
307

308
   const size_t BS = block_size();
4,071✔
309

310
   while(blocks) {
7,725✔
311
      const size_t proc_blocks = std::min(blocks, par_blocks());
3,654✔
312
      const size_t proc_bytes = proc_blocks * BS;
3,654✔
313

314
      const uint8_t* offsets = m_L->compute_offsets(m_block_index, proc_blocks);
3,654✔
315

316
      xor_buf(m_checksum.data(), buffer, proc_bytes);
3,654✔
317

318
      m_cipher->encrypt_n_xex(buffer, offsets, proc_blocks);
3,654✔
319

320
      buffer += proc_bytes;
3,654✔
321
      blocks -= proc_blocks;
3,654✔
322
      m_block_index += proc_blocks;
3,654✔
323
   }
324
}
4,071✔
325

326
size_t OCB_Encryption::process_msg(uint8_t buf[], size_t sz) {
467✔
327
   BOTAN_ARG_CHECK(sz % update_granularity() == 0, "Invalid OCB input size");
467✔
328
   encrypt(buf, sz / block_size());
467✔
329
   return sz;
375✔
330
}
331

332
void OCB_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
5,704✔
333
   assert_key_material_set();
5,704✔
334
   BOTAN_STATE_CHECK(m_L->initialized());
5,612✔
335

336
   const size_t BS = block_size();
5,566✔
337

338
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
5,566✔
339
   const size_t sz = buffer.size() - offset;
5,566✔
340
   uint8_t* buf = buffer.data() + offset;
5,566✔
341

342
   secure_vector<uint8_t> mac(BS);
5,566✔
343

344
   if(sz) {
5,566✔
345
      const size_t final_full_blocks = sz / BS;
3,696✔
346
      const size_t remainder_bytes = sz - (final_full_blocks * BS);
3,696✔
347

348
      encrypt(buf, final_full_blocks);
3,696✔
349
      mac = m_L->offset();
3,696✔
350

351
      if(remainder_bytes) {
3,696✔
352
         BOTAN_ASSERT(remainder_bytes < BS, "Only a partial block left");
3,493✔
353
         uint8_t* remainder = &buf[sz - remainder_bytes];
3,493✔
354

355
         xor_buf(m_checksum.data(), remainder, remainder_bytes);
3,493✔
356
         m_checksum[remainder_bytes] ^= 0x80;
3,493✔
357

358
         // Offset_*
359
         mac ^= m_L->star();
3,493✔
360

361
         secure_vector<uint8_t> pad(BS);
3,493✔
362
         m_cipher->encrypt(mac, pad);
3,493✔
363
         xor_buf(remainder, pad.data(), remainder_bytes);
3,493✔
364
      }
3,493✔
365
   } else {
366
      mac = m_L->offset();
1,870✔
367
   }
368

369
   // now compute the tag
370

371
   // fold checksum
372
   for(size_t i = 0; i != m_checksum.size(); i += BS) {
82,254✔
373
      xor_buf(mac.data(), m_checksum.data() + i, BS);
76,688✔
374
   }
375

376
   xor_buf(mac.data(), m_L->dollar().data(), BS);
5,566✔
377
   m_cipher->encrypt(mac);
5,566✔
378
   xor_buf(mac.data(), m_ad_hash.data(), BS);
5,566✔
379

380
   buffer += std::make_pair(mac.data(), tag_size());
5,566✔
381

382
   zeroise(m_checksum);
5,566✔
383
   m_block_index = 0;
5,566✔
384
}
5,566✔
385

386
void OCB_Decryption::decrypt(uint8_t buffer[], size_t blocks) {
3,002✔
387
   assert_key_material_set();
3,002✔
388
   BOTAN_STATE_CHECK(m_L->initialized());
2,956✔
389

390
   const size_t BS = block_size();
2,910✔
391

392
   while(blocks) {
5,637✔
393
      const size_t proc_blocks = std::min(blocks, par_blocks());
2,727✔
394
      const size_t proc_bytes = proc_blocks * BS;
2,727✔
395

396
      const uint8_t* offsets = m_L->compute_offsets(m_block_index, proc_blocks);
2,727✔
397

398
      m_cipher->decrypt_n_xex(buffer, offsets, proc_blocks);
2,727✔
399

400
      xor_buf(m_checksum.data(), buffer, proc_bytes);
2,727✔
401

402
      buffer += proc_bytes;
2,727✔
403
      blocks -= proc_blocks;
2,727✔
404
      m_block_index += proc_blocks;
2,727✔
405
   }
406
}
2,910✔
407

408
size_t OCB_Decryption::process_msg(uint8_t buf[], size_t sz) {
479✔
409
   BOTAN_ARG_CHECK(sz % update_granularity() == 0, "Invalid OCB input size");
479✔
410
   decrypt(buf, sz / block_size());
479✔
411
   return sz;
387✔
412
}
413

414
void OCB_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
3,929✔
415
   assert_key_material_set();
3,929✔
416
   BOTAN_STATE_CHECK(m_L->initialized());
3,837✔
417

418
   const size_t BS = block_size();
3,791✔
419

420
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
3,791✔
421
   const size_t sz = buffer.size() - offset;
3,791✔
422
   uint8_t* buf = buffer.data() + offset;
3,791✔
423

424
   BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag");
3,791✔
425

426
   const size_t remaining = sz - tag_size();
3,791✔
427

428
   secure_vector<uint8_t> mac(BS);
3,791✔
429

430
   if(remaining) {
3,791✔
431
      const size_t final_full_blocks = remaining / BS;
2,523✔
432
      const size_t final_bytes = remaining - (final_full_blocks * BS);
2,523✔
433

434
      decrypt(buf, final_full_blocks);
2,523✔
435
      mac ^= m_L->offset();
2,523✔
436

437
      if(final_bytes) {
2,523✔
438
         BOTAN_ASSERT(final_bytes < BS, "Only a partial block left");
2,328✔
439

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

442
         mac ^= m_L->star();
2,328✔
443
         secure_vector<uint8_t> pad(BS);
2,466✔
444
         m_cipher->encrypt(mac, pad);  // P_*
2,328✔
445
         xor_buf(remainder, pad.data(), final_bytes);
2,328✔
446

447
         xor_buf(m_checksum.data(), remainder, final_bytes);
2,328✔
448
         m_checksum[final_bytes] ^= 0x80;
2,328✔
449
      }
2,328✔
450
   } else
451
      mac = m_L->offset();
1,268✔
452

453
   // compute the mac
454

455
   // fold checksum
456
   for(size_t i = 0; i != m_checksum.size(); i += BS) {
64,539✔
457
      xor_buf(mac.data(), m_checksum.data() + i, BS);
60,748✔
458
   }
459

460
   mac ^= m_L->dollar();
3,791✔
461
   m_cipher->encrypt(mac);
3,791✔
462
   mac ^= m_ad_hash;
3,791✔
463

464
   // reset state
465
   zeroise(m_checksum);
3,791✔
466
   m_block_index = 0;
3,791✔
467

468
   // compare mac
469
   const uint8_t* included_tag = &buf[remaining];
3,791✔
470

471
   if(!constant_time_compare(mac.data(), included_tag, tag_size()))
3,791✔
472
      throw Invalid_Authentication_Tag("OCB tag check failed");
138✔
473

474
   // remove tag from end of message
475
   buffer.resize(remaining + offset);
3,653✔
476
}
3,653✔
477

478
}  // 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