8#include <botan/internal/ctr.h>
10#include <botan/exceptn.h>
11#include <botan/internal/bit_ops.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/loadstor.h>
18 m_cipher(std::move(cipher)),
19 m_block_size(m_cipher->block_size()),
20 m_ctr_size(m_block_size),
21 m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
22 m_counter(m_cipher->parallel_bytes()),
23 m_pad(m_counter.size()),
27 m_cipher(std::move(cipher)),
28 m_block_size(m_cipher->block_size()),
30 m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
31 m_counter(m_cipher->parallel_bytes()),
32 m_pad(m_counter.size()),
34 BOTAN_ARG_CHECK(m_ctr_size >= 4 && m_ctr_size <= m_block_size,
"Invalid CTR-BE counter size");
50 return (iv_len <= m_block_size);
58 return m_cipher->key_spec();
62 return std::make_unique<CTR_BE>(m_cipher->new_object(), m_ctr_size);
66 return m_cipher->has_keying_material();
69void CTR_BE::key_schedule(std::span<const uint8_t> key) {
70 m_cipher->set_key(key);
77 if(m_ctr_size == m_block_size) {
78 return fmt(
"CTR-BE({})", m_cipher->name());
80 return fmt(
"CTR-BE({},{})", m_cipher->name(), m_ctr_size);
84void CTR_BE::cipher_bytes(
const uint8_t in[], uint8_t out[],
size_t length) {
87 const uint8_t* pad_bits = &m_pad[0];
88 const size_t pad_size = m_pad.size();
91 const size_t avail = pad_size - m_pad_pos;
92 const size_t take = std::min(length, avail);
93 xor_buf(out, in, pad_bits + m_pad_pos, take);
100 add_counter(m_ctr_blocks);
101 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
106 while(length >= pad_size) {
107 xor_buf(out, in, pad_bits, pad_size);
112 add_counter(m_ctr_blocks);
113 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
116 xor_buf(out, in, pad_bits, length);
120void CTR_BE::generate_keystream(uint8_t out[],
size_t length) {
123 const size_t avail = m_pad.size() - m_pad_pos;
124 const size_t take = std::min(length, avail);
125 copy_mem(out, &m_pad[m_pad_pos], take);
130 while(length >= m_pad.size()) {
131 add_counter(m_ctr_blocks);
132 m_cipher->encrypt_n(m_counter.data(), out, m_ctr_blocks);
134 length -= m_pad.size();
138 if(m_pad_pos == m_pad.size()) {
139 add_counter(m_ctr_blocks);
140 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
149void CTR_BE::set_iv_bytes(
const uint8_t iv[],
size_t iv_len) {
151 throw Invalid_IV_Length(
name(), iv_len);
154 m_iv.resize(m_block_size);
161void CTR_BE::add_counter(
const uint64_t counter) {
162 const size_t ctr_size = m_ctr_size;
163 const size_t ctr_blocks = m_ctr_blocks;
164 const size_t BS = m_block_size;
167 const size_t off = (BS - 4);
168 const uint32_t low32 =
static_cast<uint32_t
>(counter +
load_be<uint32_t>(&m_counter[off], 0));
170 for(
size_t i = 0; i != ctr_blocks; ++i) {
171 store_be(uint32_t(low32 + i), &m_counter[i * BS + off]);
173 }
else if(ctr_size == 8) {
174 const size_t off = (BS - 8);
177 for(
size_t i = 0; i != ctr_blocks; ++i) {
178 store_be(uint64_t(low64 + i), &m_counter[i * BS + off]);
180 }
else if(ctr_size == 16) {
181 const size_t off = (BS - 16);
185 b0 += (b1 < counter) ? 1 : 0;
187 for(
size_t i = 0; i != ctr_blocks; ++i) {
188 store_be(b0, &m_counter[i * BS + off]);
189 store_be(b1, &m_counter[i * BS + off + 8]);
194 for(
size_t i = 0; i != ctr_blocks; ++i) {
195 uint64_t local_counter = counter;
196 uint16_t
carry =
static_cast<uint8_t
>(local_counter);
197 for(
size_t j = 0; (
carry || local_counter) && j != ctr_size; ++j) {
198 const size_t off = i * BS + (BS - 1 - j);
199 const uint16_t cnt =
static_cast<uint16_t
>(m_counter[off]) +
carry;
200 m_counter[off] =
static_cast<uint8_t
>(cnt);
201 local_counter = (local_counter >> 8);
202 carry = (cnt >> 8) +
static_cast<uint8_t
>(local_counter);
211 const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
215 copy_mem(&m_counter[0], &m_iv[0], m_iv.size());
217 const size_t BS = m_block_size;
221 if(m_ctr_size == 4 && BS >= 8) {
226 while(written < m_ctr_blocks) {
227 copy_mem(&m_counter[written * BS], &m_counter[0], BS * written);
231 for(
size_t i = 1; i != m_ctr_blocks; ++i) {
232 copy_mem(&m_counter[i * BS], &m_counter[0], BS - 4);
236 for(
size_t i = 1; i != m_ctr_blocks; ++i) {
237 const uint32_t c =
static_cast<uint32_t
>(low32 + i);
238 store_be(c, &m_counter[(BS - 4) + i * BS]);
242 for(
size_t i = 1; i != m_ctr_blocks; ++i) {
243 copy_mem(&m_counter[i * BS], &m_counter[(i - 1) * BS], BS);
245 for(
size_t j = 0; j != m_ctr_size; ++j) {
246 if(++m_counter[i * BS + (BS - 1 - j)]) {
253 if(base_counter > 0) {
254 add_counter(base_counter);
257 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
258 m_pad_pos = offset % m_counter.size();
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
size_t default_iv_length() const override
bool has_keying_material() const override
size_t buffer_size() const override
Key_Length_Specification key_spec() const override
bool valid_iv_length(size_t iv_len) const override
void seek(uint64_t offset) override
std::unique_ptr< StreamCipher > new_object() const override
std::string name() const override
CTR_BE(std::unique_ptr< BlockCipher > cipher)
void set_iv(const uint8_t iv[], size_t iv_len)
void assert_key_material_set() const
void zeroise(std::vector< T, Alloc > &vec)
constexpr bool is_power_of_2(T arg)
void zap(std::vector< T, Alloc > &vec)
std::string fmt(std::string_view format, const T &... args)
void carry(int64_t &h0, int64_t &h1)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)