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>
15#if defined(BOTAN_HAS_CTR_BE_AVX2) || defined(BOTAN_HAS_CTR_BE_SIMD32)
16 #include <botan/internal/cpuid.h>
22 m_cipher(std::move(
cipher)),
23 m_block_size(m_cipher->block_size()),
24 m_ctr_size(m_block_size),
25 m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
26 m_counter(m_cipher->parallel_bytes()),
27 m_pad(m_counter.size()),
31 m_cipher(std::move(
cipher)),
32 m_block_size(m_cipher->block_size()),
34 m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
35 m_counter(m_cipher->parallel_bytes()),
36 m_pad(m_counter.size()),
38 BOTAN_ARG_CHECK(m_ctr_size >= 4 && m_ctr_size <= m_block_size,
"Invalid CTR-BE counter size");
47 m_bytes_remaining = 0;
54 return m_bytes_remaining;
62 return (iv_len <= m_block_size);
70 return m_cipher->key_spec();
74 return std::make_unique<CTR_BE>(m_cipher->new_object(), m_ctr_size);
78 return m_cipher->has_keying_material();
81void CTR_BE::key_schedule(std::span<const uint8_t> key) {
82 m_cipher->set_key(key);
89 if(m_ctr_size == m_block_size) {
90 return fmt(
"CTR-BE({})", m_cipher->name());
92 return fmt(
"CTR-BE({},{})", m_cipher->name(), m_ctr_size);
96void CTR_BE::cipher_bytes(
const uint8_t in[], uint8_t out[],
size_t length) {
99 if(m_ctr_size <
sizeof(uint64_t)) {
100 if(length > m_bytes_remaining) {
101 throw Invalid_State(
fmt(
"CTR_BE with {}-byte counter has exhausted its keystream", m_ctr_size));
103 m_bytes_remaining -= length;
106 const uint8_t* pad_bits = m_pad.data();
107 const size_t pad_size = m_pad.size();
112 const size_t avail = pad_size - m_pad_pos;
113 const size_t take = std::min(length, avail);
114 xor_buf(out, in, pad_bits + m_pad_pos, take);
121 add_counter(m_ctr_blocks);
122 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
129 [[maybe_unused]]
const bool can_use_bs16_ctr4_fastpath = m_block_size == 16 && m_ctr_size == 4 && pad_size % 64 == 0;
131#if defined(BOTAN_HAS_CTR_BE_AVX2)
133 const size_t consumed = ctr_proc_bs16_ctr4_avx2(in, out, length);
140#if defined(BOTAN_HAS_CTR_BE_SIMD32)
142 const size_t consumed = ctr_proc_bs16_ctr4_simd32(in, out, length);
149 while(length >= pad_size) {
150 xor_buf(out, in, pad_bits, pad_size);
155 add_counter(m_ctr_blocks);
156 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
161 xor_buf(out, in, pad_bits, length);
166void CTR_BE::generate_keystream(uint8_t out[],
size_t length) {
169 if(m_ctr_size <
sizeof(uint64_t)) {
170 if(length > m_bytes_remaining) {
171 throw Invalid_State(
fmt(
"CTR_BE with {}-byte counter has exhausted its keystream", m_ctr_size));
173 m_bytes_remaining -= length;
176 const size_t avail = m_pad.size() - m_pad_pos;
177 const size_t take = std::min(length, avail);
178 copy_mem(out, &m_pad[m_pad_pos], take);
183 while(length >= m_pad.size()) {
184 add_counter(m_ctr_blocks);
185 m_cipher->encrypt_n(m_counter.data(), out, m_ctr_blocks);
187 length -= m_pad.size();
191 if(m_pad_pos == m_pad.size()) {
192 add_counter(m_ctr_blocks);
193 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
197 copy_mem(out, m_pad.data(), length);
202void CTR_BE::set_iv_bytes(
const uint8_t iv[],
size_t iv_len) {
204 throw Invalid_IV_Length(
name(), iv_len);
207 m_iv.resize(m_block_size);
214void CTR_BE::add_counter(
const uint64_t counter) {
215 const size_t ctr_size = m_ctr_size;
216 const size_t ctr_blocks = m_ctr_blocks;
217 const size_t BS = m_block_size;
220 const size_t off = (BS - 4);
221 const uint32_t low32 =
static_cast<uint32_t
>(counter +
load_be<uint32_t>(&m_counter[off], 0));
223 for(
size_t i = 0; i != ctr_blocks; ++i) {
224 store_be(uint32_t(low32 + i), &m_counter[i * BS + off]);
226 }
else if(ctr_size == 8) {
227 const size_t off = (BS - 8);
230 for(
size_t i = 0; i != ctr_blocks; ++i) {
231 store_be(uint64_t(low64 + i), &m_counter[i * BS + off]);
233 }
else if(ctr_size == 16) {
234 const size_t off = (BS - 16);
238 b0 += (b1 < counter) ? 1 : 0;
240 for(
size_t i = 0; i != ctr_blocks; ++i) {
241 store_be(b0, &m_counter[i * BS + off]);
242 store_be(b1, &m_counter[i * BS + off + 8]);
249 for(
size_t i = 0; i != ctr_blocks; ++i) {
250 uint64_t local_counter = counter;
251 uint16_t
carry =
static_cast<uint8_t
>(local_counter);
252 for(
size_t j = 0; (
carry > 0 || local_counter > 0) && j != ctr_size; ++j) {
253 const size_t off = i * BS + (BS - 1 - j);
254 const uint16_t cnt =
static_cast<uint16_t
>(m_counter[off]) +
carry;
255 m_counter[off] =
static_cast<uint8_t
>(cnt);
256 local_counter = (local_counter >> 8);
257 carry = (cnt >> 8) +
static_cast<uint8_t
>(local_counter);
266 if(m_ctr_size <
sizeof(uint64_t)) {
267 const uint64_t requested_block = offset / m_block_size;
268 const uint64_t max_blocks = uint64_t{1} << (8 * m_ctr_size);
269 if(requested_block >= max_blocks) {
270 throw Invalid_Argument(
fmt(
"CTR_BE::seek offset {} exceeds {}-byte counter range", offset, m_ctr_size));
273 m_bytes_remaining = max_blocks * m_block_size - offset;
276 const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
280 copy_mem(m_counter.data(), m_iv.data(), m_iv.size());
282 const size_t BS = m_block_size;
286 if(m_ctr_size == 4 && BS >= 8) {
291 while(written < m_ctr_blocks) {
292 copy_mem(&m_counter[written * BS], &m_counter[0], BS * written);
296 for(
size_t i = 1; i != m_ctr_blocks; ++i) {
297 copy_mem(&m_counter[i * BS], &m_counter[0], BS - 4);
301 for(
size_t i = 1; i != m_ctr_blocks; ++i) {
302 const uint32_t c =
static_cast<uint32_t
>(low32 + i);
303 store_be(c, &m_counter[(BS - 4) + i * BS]);
307 for(
size_t i = 1; i != m_ctr_blocks; ++i) {
308 copy_mem(&m_counter[i * BS], &m_counter[(i - 1) * BS], BS);
310 for(
size_t j = 0; j != m_ctr_size; ++j) {
311 uint8_t& c = m_counter[i * BS + (BS - 1 - j)];
320 if(base_counter > 0) {
321 add_counter(base_counter);
324 m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
325 m_pad_pos = offset % m_counter.size();
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
static bool has(CPUID::Feature feat)
size_t default_iv_length() const override
std::optional< uint64_t > remaining_keystream_bytes() 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 cipher(const uint8_t in[], uint8_t out[], size_t len)
void assert_key_material_set() const
BOTAN_FORCE_INLINE constexpr bool is_power_of_2(T arg)
void zeroise(std::vector< T, Alloc > &vec)
void zap(std::vector< T, Alloc > &vec)
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)
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 auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)