9#include <botan/internal/ocb.h>
11#include <botan/block_cipher.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/poly_dbl.h>
20class L_computer final {
22 explicit L_computer(
const BlockCipher& cipher) :
23 m_BS(cipher.block_size()), m_max_blocks(cipher.parallel_bytes() / m_BS) {
24 m_L_star.resize(m_BS);
25 cipher.encrypt(m_L_star);
26 m_L_dollar = poly_double(star());
38 m_L.push_back(poly_double(dollar()));
40 while(m_L.size() < 8) {
41 m_L.push_back(poly_double(m_L.back()));
44 m_offset_buf.resize(m_BS * m_max_blocks);
49 bool initialized()
const {
return m_offset.empty() ==
false; }
58 while(m_L.size() <= i) {
59 m_L.push_back(poly_double(m_L.back()));
65 const uint8_t* compute_offsets(
size_t block_index,
size_t blocks) {
68 uint8_t* offsets = m_offset_buf.data();
70 if(block_index % 4 == 0) {
79 const size_t ntz4 =
var_ctz32(
static_cast<uint32_t
>(block_index));
81 xor_buf(offsets, m_offset.data(), L0.data(), m_BS);
84 xor_buf(offsets, offsets - m_BS, L1.data(), m_BS);
87 xor_buf(m_offset.data(), L1.data(), m_BS);
88 copy_mem(offsets, m_offset.data(), m_BS);
91 xor_buf(m_offset.data(), get(ntz4).data(), m_BS);
92 copy_mem(offsets, m_offset.data(), m_BS);
99 for(
size_t i = 0; i != blocks; ++i) {
100 const size_t ntz =
var_ctz32(
static_cast<uint32_t
>(block_index + i + 1));
101 xor_buf(m_offset.data(), get(ntz).data(), m_BS);
102 copy_mem(offsets, m_offset.data(), m_BS);
106 return m_offset_buf.data();
116 const size_t m_BS, m_max_blocks;
119 mutable std::vector<secure_vector<uint8_t>> m_L;
129 const size_t BS = cipher.block_size();
135 const size_t ad_blocks = (ad_len / BS);
136 const size_t ad_remainder = (ad_len % BS);
138 for(
size_t i = 0; i != ad_blocks; ++i) {
140 offset ^= L.get(
var_ctz32(
static_cast<uint32_t
>(i + 1)));
142 xor_buf(buf.data(), &ad[BS * i], BS);
150 xor_buf(buf.data(), &ad[BS * ad_blocks], ad_remainder);
151 buf[ad_remainder] ^= 0x80;
167 m_par_blocks(
m_cipher->parallel_bytes() / m_block_size) {
175 BOTAN_ARG_CHECK(BS == 16 || BS == 24 || BS == 32 || BS == 64,
"Invalid block size for OCB");
177 BOTAN_ARG_CHECK(m_tag_size % 4 == 0 && m_tag_size >= 8 && m_tag_size <= BS && m_tag_size <= 32,
178 "Invalid OCB tag length");
193 m_last_nonce.clear();
225 return m_cipher->has_keying_material();
228void OCB_Mode::key_schedule(std::span<const uint8_t> key) {
234 BOTAN_ARG_CHECK(idx == 0,
"OCB: cannot handle non-zero index in set_associated_data_n");
242 BOTAN_ASSERT(BS == 16 || BS == 24 || BS == 32 || BS == 64,
"OCB block size is supported");
245 const size_t MASKLEN = (BS == 16 ? 6 : ((BS == 24) ? 7 : 8));
247 const uint8_t BOTTOM_MASK =
static_cast<uint8_t
>((
static_cast<uint16_t
>(1) << MASKLEN) - 1);
249 m_nonce_buf.resize(BS);
250 clear_mem(&m_nonce_buf[0], m_nonce_buf.size());
252 copy_mem(&m_nonce_buf[BS - nonce_len], nonce, nonce_len);
253 m_nonce_buf[0] =
static_cast<uint8_t
>(((
tag_size() * 8) % (BS * 8)) << (BS <= 16 ? 1 : 0));
255 m_nonce_buf[BS - nonce_len - 1] ^= 1;
257 const uint8_t bottom = m_nonce_buf[BS - 1] & BOTTOM_MASK;
258 m_nonce_buf[BS - 1] &= ~BOTTOM_MASK;
260 const bool need_new_stretch = (m_last_nonce != m_nonce_buf);
262 if(need_new_stretch) {
263 m_last_nonce = m_nonce_buf;
288 for(
size_t i = 0; i != BS / 2; ++i) {
289 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 1]);
291 }
else if(BS == 24) {
292 for(
size_t i = 0; i != 16; ++i) {
293 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 5]);
295 }
else if(BS == 32) {
296 for(
size_t i = 0; i != BS; ++i) {
297 m_nonce_buf.push_back(m_nonce_buf[i] ^ (m_nonce_buf[i] << 1) ^ (m_nonce_buf[i + 1] >> 7));
299 }
else if(BS == 64) {
300 for(
size_t i = 0; i != BS / 2; ++i) {
301 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 22]);
305 m_stretch = m_nonce_buf;
309 const size_t shift_bytes = bottom / 8;
310 const size_t shift_bits = bottom % 8;
312 BOTAN_ASSERT(m_stretch.size() >= BS + shift_bytes + 1,
"Size ok");
315 for(
size_t i = 0; i != BS; ++i) {
316 m_offset[i] = (m_stretch[i + shift_bytes] << shift_bits);
317 m_offset[i] |= (m_stretch[i + shift_bytes + 1] >> (8 - shift_bits));
323void OCB_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
325 throw Invalid_IV_Length(
name(), nonce_len);
330 m_L->init(update_nonce(nonce, nonce_len));
335void OCB_Encryption::encrypt(uint8_t buffer[],
size_t blocks) {
342 const size_t proc_blocks = std::min(blocks,
par_blocks());
343 const size_t proc_bytes = proc_blocks * BS;
349 xor_buf(buffer, offsets, proc_bytes);
350 m_cipher->encrypt_n(buffer, buffer, proc_blocks);
351 xor_buf(buffer, offsets, proc_bytes);
353 buffer += proc_bytes;
354 blocks -= proc_blocks;
359size_t OCB_Encryption::process_msg(uint8_t buf[],
size_t sz) {
372 const size_t sz = buffer.size() - offset;
373 uint8_t* buf = buffer.data() + offset;
378 const size_t final_full_blocks = sz / BS;
379 const size_t remainder_bytes = sz - (final_full_blocks * BS);
381 encrypt(buf, final_full_blocks);
384 if(remainder_bytes) {
385 BOTAN_ASSERT(remainder_bytes < BS,
"Only a partial block left");
386 uint8_t* remainder = &buf[sz - remainder_bytes];
396 xor_buf(remainder, pad.data(), remainder_bytes);
405 for(
size_t i = 0; i !=
m_checksum.size(); i += BS) {
409 xor_buf(mac.data(),
m_L->dollar().data(), BS);
413 buffer += std::make_pair(mac.data(),
tag_size());
419void OCB_Decryption::decrypt(uint8_t buffer[],
size_t blocks) {
426 const size_t proc_blocks = std::min(blocks,
par_blocks());
427 const size_t proc_bytes = proc_blocks * BS;
431 xor_buf(buffer, offsets, proc_bytes);
432 m_cipher->decrypt_n(buffer, buffer, proc_blocks);
433 xor_buf(buffer, offsets, proc_bytes);
437 buffer += proc_bytes;
438 blocks -= proc_blocks;
443size_t OCB_Decryption::process_msg(uint8_t buf[],
size_t sz) {
456 const size_t sz = buffer.size() - offset;
457 uint8_t* buf = buffer.data() + offset;
461 const size_t remaining = sz -
tag_size();
466 const size_t final_full_blocks = remaining / BS;
467 const size_t final_bytes = remaining - (final_full_blocks * BS);
469 decrypt(buf, final_full_blocks);
470 mac ^=
m_L->offset();
473 BOTAN_ASSERT(final_bytes < BS,
"Only a partial block left");
475 uint8_t* remainder = &buf[remaining - final_bytes];
480 xor_buf(remainder, pad.data(), final_bytes);
492 for(
size_t i = 0; i !=
m_checksum.size(); i += BS) {
496 mac ^=
m_L->dollar();
505 const uint8_t* included_tag = &buf[remaining];
508 throw Invalid_Authentication_Tag(
"OCB tag check failed");
512 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
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)