9#include <botan/internal/ocb.h>
11#include <botan/block_cipher.h>
12#include <botan/internal/bit_ops.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/poly_dbl.h>
19class L_computer
final {
21 explicit L_computer(
const BlockCipher& cipher) :
22 m_BS(cipher.block_size()), m_max_blocks(cipher.parallel_bytes() / m_BS) {
23 m_L_star.resize(m_BS);
24 cipher.encrypt(m_L_star);
25 m_L_dollar = poly_double(star());
37 m_L.push_back(poly_double(dollar()));
39 while(m_L.size() < 8) {
40 m_L.push_back(poly_double(m_L.back()));
43 m_offset_buf.resize(m_BS * m_max_blocks);
48 bool initialized()
const {
return m_offset.empty() ==
false; }
57 while(m_L.size() <= i) {
58 m_L.push_back(poly_double(m_L.back()));
64 const uint8_t* compute_offsets(
size_t block_index,
size_t blocks) {
67 uint8_t* offsets = m_offset_buf.data();
69 if(block_index % 4 == 0) {
78 const size_t ntz4 =
var_ctz32(
static_cast<uint32_t
>(block_index));
80 xor_buf(offsets, m_offset.data(), L0.data(), m_BS);
83 xor_buf(offsets, offsets - m_BS, L1.data(), m_BS);
86 xor_buf(m_offset.data(), L1.data(), m_BS);
87 copy_mem(offsets, m_offset.data(), m_BS);
90 xor_buf(m_offset.data(), get(ntz4).data(), m_BS);
91 copy_mem(offsets, m_offset.data(), m_BS);
98 for(
size_t i = 0; i != blocks; ++i) {
99 const size_t ntz =
var_ctz32(
static_cast<uint32_t
>(block_index + i + 1));
100 xor_buf(m_offset.data(), get(ntz).data(), m_BS);
101 copy_mem(offsets, m_offset.data(), m_BS);
105 return m_offset_buf.data();
115 const size_t m_BS, m_max_blocks;
118 mutable std::vector<secure_vector<uint8_t>> m_L;
127secure_vector<uint8_t> ocb_hash(
const L_computer& L,
const BlockCipher& cipher,
const uint8_t ad[],
size_t ad_len) {
128 const size_t BS = cipher.block_size();
134 const size_t ad_blocks = (ad_len / BS);
135 const size_t ad_remainder = (ad_len % BS);
137 for(
size_t i = 0; i != ad_blocks; ++i) {
139 offset ^= L.get(
var_ctz32(
static_cast<uint32_t
>(i + 1)));
141 xor_buf(buf.data(), &ad[BS * i], BS);
149 xor_buf(buf.data(), &ad[BS * ad_blocks], ad_remainder);
150 buf[ad_remainder] ^= 0x80;
161 m_cipher(std::move(cipher)),
162 m_checksum(m_cipher->parallel_bytes()),
163 m_ad_hash(m_cipher->block_size()),
164 m_tag_size(tag_size),
165 m_block_size(m_cipher->block_size()),
166 m_par_blocks(m_cipher->parallel_bytes() / m_block_size) {
174 BOTAN_ARG_CHECK(BS == 16 || BS == 24 || BS == 32 || BS == 64,
"Invalid block size for OCB");
176 BOTAN_ARG_CHECK(m_tag_size % 4 == 0 && m_tag_size >= 8 && m_tag_size <= BS && m_tag_size <= 32,
177 "Invalid OCB tag length");
192 m_last_nonce.clear();
224 return m_cipher->has_keying_material();
227void OCB_Mode::key_schedule(std::span<const uint8_t> key) {
233 BOTAN_ARG_CHECK(idx == 0,
"OCB: cannot handle non-zero index in set_associated_data_n");
241 BOTAN_ASSERT(BS == 16 || BS == 24 || BS == 32 || BS == 64,
"OCB block size is supported");
244 const size_t MASKLEN = (BS == 16 ? 6 : ((BS == 24) ? 7 : 8));
246 const uint8_t BOTTOM_MASK =
static_cast<uint8_t
>((
static_cast<uint16_t
>(1) << MASKLEN) - 1);
248 m_nonce_buf.resize(BS);
249 clear_mem(&m_nonce_buf[0], m_nonce_buf.size());
251 copy_mem(&m_nonce_buf[BS - nonce_len], nonce, nonce_len);
252 m_nonce_buf[0] =
static_cast<uint8_t
>(((
tag_size() * 8) % (BS * 8)) << (BS <= 16 ? 1 : 0));
254 m_nonce_buf[BS - nonce_len - 1] ^= 1;
256 const uint8_t bottom = m_nonce_buf[BS - 1] & BOTTOM_MASK;
257 m_nonce_buf[BS - 1] &= ~BOTTOM_MASK;
259 const bool need_new_stretch = (m_last_nonce != m_nonce_buf);
261 if(need_new_stretch) {
262 m_last_nonce = m_nonce_buf;
287 for(
size_t i = 0; i != BS / 2; ++i) {
288 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 1]);
290 }
else if(BS == 24) {
291 for(
size_t i = 0; i != 16; ++i) {
292 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 5]);
294 }
else if(BS == 32) {
295 for(
size_t i = 0; i != BS; ++i) {
296 m_nonce_buf.push_back(m_nonce_buf[i] ^ (m_nonce_buf[i] << 1) ^ (m_nonce_buf[i + 1] >> 7));
298 }
else if(BS == 64) {
299 for(
size_t i = 0; i != BS / 2; ++i) {
300 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 22]);
304 m_stretch = m_nonce_buf;
308 const size_t shift_bytes = bottom / 8;
309 const size_t shift_bits = bottom % 8;
311 BOTAN_ASSERT(m_stretch.size() >= BS + shift_bytes + 1,
"Size ok");
314 for(
size_t i = 0; i != BS; ++i) {
315 m_offset[i] = (m_stretch[i + shift_bytes] << shift_bits);
316 m_offset[i] |= (m_stretch[i + shift_bytes + 1] >> (8 - shift_bits));
322void OCB_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
324 throw Invalid_IV_Length(
name(), nonce_len);
329 m_L->init(update_nonce(nonce, nonce_len));
334void OCB_Encryption::encrypt(uint8_t buffer[],
size_t blocks) {
341 const size_t proc_blocks = std::min(blocks,
par_blocks());
342 const size_t proc_bytes = proc_blocks * BS;
348 m_cipher->encrypt_n_xex(buffer, offsets, proc_blocks);
350 buffer += proc_bytes;
351 blocks -= proc_blocks;
356size_t OCB_Encryption::process_msg(uint8_t buf[],
size_t sz) {
369 const size_t sz = buffer.size() - offset;
370 uint8_t* buf = buffer.data() + offset;
375 const size_t final_full_blocks = sz / BS;
376 const size_t remainder_bytes = sz - (final_full_blocks * BS);
378 encrypt(buf, final_full_blocks);
381 if(remainder_bytes) {
382 BOTAN_ASSERT(remainder_bytes < BS,
"Only a partial block left");
383 uint8_t* remainder = &buf[sz - remainder_bytes];
393 xor_buf(remainder, pad.data(), remainder_bytes);
402 for(
size_t i = 0; i !=
m_checksum.size(); i += BS) {
406 xor_buf(mac.data(),
m_L->dollar().data(), BS);
410 buffer += std::make_pair(mac.data(),
tag_size());
416void OCB_Decryption::decrypt(uint8_t buffer[],
size_t blocks) {
423 const size_t proc_blocks = std::min(blocks,
par_blocks());
424 const size_t proc_bytes = proc_blocks * BS;
428 m_cipher->decrypt_n_xex(buffer, offsets, proc_blocks);
432 buffer += proc_bytes;
433 blocks -= proc_blocks;
438size_t OCB_Decryption::process_msg(uint8_t buf[],
size_t sz) {
451 const size_t sz = buffer.size() - offset;
452 uint8_t* buf = buffer.data() + offset;
456 const size_t remaining = sz -
tag_size();
461 const size_t final_full_blocks = remaining / BS;
462 const size_t final_bytes = remaining - (final_full_blocks * BS);
464 decrypt(buf, final_full_blocks);
465 mac ^=
m_L->offset();
468 BOTAN_ASSERT(final_bytes < BS,
"Only a partial block left");
470 uint8_t* remainder = &buf[remaining - final_bytes];
475 xor_buf(remainder, pad.data(), final_bytes);
487 for(
size_t i = 0; i !=
m_checksum.size(); i += BS) {
491 mac ^=
m_L->dollar();
500 const uint8_t* included_tag = &buf[remaining];
503 throw Invalid_Authentication_Tag(
"OCB tag check failed");
507 buffer.resize(remaining + offset);
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
std::string name() const override final
size_t block_size() const
size_t par_blocks() const
size_t tag_size() const override final
bool has_keying_material() const override final
size_t ideal_granularity() const override final
bool valid_nonce_length(size_t) const override final
void reset() override final
secure_vector< uint8_t > m_checksum
std::unique_ptr< BlockCipher > m_cipher
secure_vector< uint8_t > m_ad_hash
void clear() override final
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override final
size_t update_granularity() const override final
Key_Length_Specification key_spec() const override final
OCB_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
std::unique_ptr< L_computer > m_L
void assert_key_material_set() const
int(* final)(unsigned char *, CTX *)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
void zeroise(std::vector< T, Alloc > &vec)
constexpr size_t var_ctz32(uint32_t n)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr void clear_mem(T *ptr, size_t n)