9#include <botan/internal/ocb.h>
11#include <botan/block_cipher.h>
12#include <botan/exceptn.h>
13#include <botan/mem_ops.h>
14#include <botan/internal/bit_ops.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/poly_dbl.h>
21class L_computer final {
23 explicit L_computer(
const BlockCipher& cipher) :
24 m_BS(cipher.block_size()), m_max_blocks(cipher.parallel_bytes() / m_BS) {
25 m_L_star.resize(m_BS);
26 cipher.encrypt(m_L_star);
27 m_L_dollar = poly_double(star());
39 m_L.push_back(poly_double(dollar()));
41 while(m_L.size() < 8) {
42 m_L.push_back(poly_double(m_L.back()));
45 m_offset_buf.resize(m_BS * m_max_blocks);
50 bool initialized()
const {
return !m_offset.empty(); }
59 while(m_L.size() <= i) {
60 m_L.push_back(poly_double(m_L.back()));
66 const uint8_t* compute_offsets(
size_t block_index,
size_t blocks) {
69 uint8_t* offsets = m_offset_buf.data();
71 if(block_index % 4 == 0) {
80 const size_t ntz4 =
var_ctz32(
static_cast<uint32_t
>(block_index));
82 xor_buf(offsets, m_offset.data(), L0.data(), m_BS);
85 xor_buf(offsets, offsets - m_BS, L1.data(), m_BS);
88 xor_buf(m_offset.data(), L1.data(), m_BS);
89 copy_mem(offsets, m_offset.data(), m_BS);
92 xor_buf(m_offset.data(), get(ntz4).data(), m_BS);
93 copy_mem(offsets, m_offset.data(), m_BS);
100 for(
size_t i = 0; i != blocks; ++i) {
101 const size_t ntz =
var_ctz32(
static_cast<uint32_t
>(block_index + i + 1));
102 xor_buf(m_offset.data(), get(ntz).data(), m_BS);
103 copy_mem(offsets, m_offset.data(), m_BS);
107 return m_offset_buf.data();
117 const size_t m_BS, m_max_blocks;
120 mutable std::vector<secure_vector<uint8_t>> m_L;
130 const size_t BS = cipher.block_size();
136 const size_t ad_blocks = (ad_len / BS);
137 const size_t ad_remainder = (ad_len % BS);
139 for(
size_t i = 0; i != ad_blocks; ++i) {
141 offset ^= L.get(
var_ctz32(
static_cast<uint32_t
>(i + 1)));
143 xor_buf(buf.data(), &ad[BS * i], BS);
148 if(ad_remainder > 0) {
151 xor_buf(buf.data(), &ad[BS * ad_blocks], ad_remainder);
152 buf[ad_remainder] ^= 0x80;
168 m_par_blocks(
m_cipher->parallel_bytes() / m_block_size) {
176 BOTAN_ARG_CHECK(BS == 16 || BS == 24 || BS == 32 || BS == 64,
"Invalid block size for OCB");
178 BOTAN_ARG_CHECK(m_tag_size % 4 == 0 && m_tag_size >= 8 && m_tag_size <= BS && m_tag_size <= 32,
179 "Invalid OCB tag length");
194 m_last_nonce.clear();
226 return m_cipher->has_keying_material();
229void OCB_Mode::key_schedule(std::span<const uint8_t> key) {
235 BOTAN_ARG_CHECK(idx == 0,
"OCB: cannot handle non-zero index in set_associated_data_n");
243 BOTAN_ASSERT(BS == 16 || BS == 24 || BS == 32 || BS == 64,
"OCB block size is supported");
246 const size_t MASKLEN = (BS == 16 ? 6 : ((BS == 24) ? 7 : 8));
248 const uint8_t BOTTOM_MASK =
static_cast<uint8_t
>((
static_cast<uint16_t
>(1) << MASKLEN) - 1);
250 m_nonce_buf.resize(BS);
251 clear_mem(m_nonce_buf.data(), m_nonce_buf.size());
253 copy_mem(&m_nonce_buf[BS - nonce_len], nonce, nonce_len);
254 m_nonce_buf[0] =
static_cast<uint8_t
>(((
tag_size() * 8) % (BS * 8)) << (BS <= 16 ? 1 : 0));
256 m_nonce_buf[BS - nonce_len - 1] ^= 1;
258 const uint8_t bottom = m_nonce_buf[BS - 1] & BOTTOM_MASK;
259 m_nonce_buf[BS - 1] &= ~BOTTOM_MASK;
261 const bool need_new_stretch = (m_last_nonce != m_nonce_buf);
263 if(need_new_stretch) {
264 m_last_nonce = m_nonce_buf;
289 for(
size_t i = 0; i != BS / 2; ++i) {
290 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 1]);
292 }
else if(BS == 24) {
293 for(
size_t i = 0; i != 16; ++i) {
294 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 5]);
296 }
else if(BS == 32) {
297 for(
size_t i = 0; i != BS; ++i) {
298 m_nonce_buf.push_back(m_nonce_buf[i] ^ (m_nonce_buf[i] << 1) ^ (m_nonce_buf[i + 1] >> 7));
300 }
else if(BS == 64) {
301 for(
size_t i = 0; i != BS / 2; ++i) {
302 m_nonce_buf.push_back(m_nonce_buf[i] ^ m_nonce_buf[i + 22]);
306 m_stretch = m_nonce_buf;
310 const size_t shift_bytes = bottom / 8;
311 const size_t shift_bits = bottom % 8;
313 BOTAN_ASSERT(m_stretch.size() >= BS + shift_bytes + 1,
"Size ok");
316 for(
size_t i = 0; i != BS; ++i) {
317 m_offset[i] = (m_stretch[i + shift_bytes] << shift_bits);
318 m_offset[i] |= (m_stretch[i + shift_bytes + 1] >> (8 - shift_bits));
324void OCB_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
326 throw Invalid_IV_Length(
name(), nonce_len);
331 m_L->init(update_nonce(nonce, nonce_len));
336void OCB_Encryption::encrypt(uint8_t buffer[],
size_t blocks) {
343 const size_t proc_blocks = std::min(blocks,
par_blocks());
344 const size_t proc_bytes = proc_blocks * BS;
350 xor_buf(buffer, offsets, proc_bytes);
351 m_cipher->encrypt_n(buffer, buffer, proc_blocks);
352 xor_buf(buffer, offsets, proc_bytes);
354 buffer += proc_bytes;
355 blocks -= proc_blocks;
360size_t OCB_Encryption::process_msg(uint8_t buf[],
size_t sz) {
373 const size_t sz = buffer.size() - offset;
374 uint8_t* buf = buffer.data() + offset;
379 const size_t final_full_blocks = sz / BS;
380 const size_t remainder_bytes = sz - (final_full_blocks * BS);
382 encrypt(buf, final_full_blocks);
385 if(remainder_bytes > 0) {
386 BOTAN_ASSERT(remainder_bytes < BS,
"Only a partial block left");
387 uint8_t* remainder = &buf[sz - remainder_bytes];
397 xor_buf(remainder, pad.data(), remainder_bytes);
406 for(
size_t i = 0; i !=
m_checksum.size(); i += BS) {
410 xor_buf(mac.data(),
m_L->dollar().data(), BS);
414 buffer += std::make_pair(mac.data(),
tag_size());
420void OCB_Decryption::decrypt(uint8_t buffer[],
size_t blocks) {
427 const size_t proc_blocks = std::min(blocks,
par_blocks());
428 const size_t proc_bytes = proc_blocks * BS;
432 xor_buf(buffer, offsets, proc_bytes);
433 m_cipher->decrypt_n(buffer, buffer, proc_blocks);
434 xor_buf(buffer, offsets, proc_bytes);
438 buffer += proc_bytes;
439 blocks -= proc_blocks;
444size_t OCB_Decryption::process_msg(uint8_t buf[],
size_t sz) {
457 const size_t sz = buffer.size() - offset;
458 uint8_t* buf = buffer.data() + offset;
462 const size_t remaining = sz -
tag_size();
467 const size_t final_full_blocks = remaining / BS;
468 const size_t final_bytes = remaining - (final_full_blocks * BS);
470 decrypt(buf, final_full_blocks);
471 mac ^=
m_L->offset();
473 if(final_bytes > 0) {
474 BOTAN_ASSERT(final_bytes < BS,
"Only a partial block left");
476 uint8_t* remainder = &buf[remaining - final_bytes];
481 xor_buf(remainder, pad.data(), final_bytes);
493 for(
size_t i = 0; i !=
m_checksum.size(); i += BS) {
497 mac ^=
m_L->dollar();
506 const uint8_t* included_tag = &buf[remaining];
509 throw Invalid_Authentication_Tag(
"OCB tag check failed");
513 buffer.resize(remaining + offset);
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
size_t block_size() const
size_t par_blocks() const
Key_Length_Specification key_spec() const final
size_t tag_size() const final
secure_vector< uint8_t > m_checksum
std::string name() const final
bool valid_nonce_length(size_t length) const final
std::unique_ptr< BlockCipher > m_cipher
secure_vector< uint8_t > m_ad_hash
size_t ideal_granularity() const final
bool has_keying_material() const final
OCB_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) final
size_t update_granularity() const final
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 void copy_mem(T *out, const T *in, size_t n)
BOTAN_FORCE_INLINE 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 clear_mem(T *ptr, size_t n)