8#include <botan/internal/cascade.h>
10#include <botan/internal/buffer_slicer.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/int_utils.h>
19size_t cascade_block_size(
size_t b1,
size_t b2) {
21 return mul_or_throw(b1 / std::gcd(b1, b2), b2,
"Cascade combined block size is too large");
27 const size_t c1_blocks = blocks * (
block_size() / m_cipher1->block_size());
28 const size_t c2_blocks = blocks * (
block_size() / m_cipher2->block_size());
30 m_cipher1->encrypt_n(in, out, c1_blocks);
31 m_cipher2->encrypt_n(out, out, c2_blocks);
35 const size_t c1_blocks = blocks * (
block_size() / m_cipher1->block_size());
36 const size_t c2_blocks = blocks * (
block_size() / m_cipher2->block_size());
38 m_cipher2->decrypt_n(in, out, c2_blocks);
39 m_cipher1->decrypt_n(out, out, c1_blocks);
42void Cascade_Cipher::key_schedule(std::span<const uint8_t> key) {
45 m_cipher1->set_key(keys.take(m_cipher1->maximum_keylength()));
46 m_cipher2->set_key(keys.take(m_cipher2->maximum_keylength()));
55 return fmt(
"Cascade({},{})", m_cipher1->name(), m_cipher2->name());
59 return m_cipher1->has_keying_material() && m_cipher2->has_keying_material();
63 return std::make_unique<Cascade_Cipher>(m_cipher1->new_object(), m_cipher2->new_object());
67 m_cipher1(std::move(cipher1)),
68 m_cipher2(std::move(cipher2)),
71 BOTAN_ASSERT(m_block_size % m_cipher1->block_size() == 0 && m_block_size % m_cipher2->block_size() == 0,
72 "Combined block size is a multiple of each ciphers block");
#define BOTAN_ASSERT(expr, assertion_made)
bool has_keying_material() const override
std::string name() const override
Cascade_Cipher(std::unique_ptr< BlockCipher > cipher1, std::unique_ptr< BlockCipher > cipher2)
std::unique_ptr< BlockCipher > new_object() const override
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
size_t block_size() const override
constexpr T mul_or_throw(T a, T b, std::string_view msg)
std::string fmt(std::string_view format, const T &... args)