8#include <botan/internal/cascade.h>
10#include <botan/internal/fmt.h>
11#include <botan/internal/stl_util.h>
16 size_t c1_blocks = blocks * (
block_size() / m_cipher1->block_size());
17 size_t c2_blocks = blocks * (
block_size() / m_cipher2->block_size());
19 m_cipher1->encrypt_n(in, out, c1_blocks);
20 m_cipher2->encrypt_n(out, out, c2_blocks);
24 size_t c1_blocks = blocks * (
block_size() / m_cipher1->block_size());
25 size_t c2_blocks = blocks * (
block_size() / m_cipher2->block_size());
27 m_cipher2->decrypt_n(in, out, c2_blocks);
28 m_cipher1->decrypt_n(out, out, c1_blocks);
31void Cascade_Cipher::key_schedule(std::span<const uint8_t> key) {
34 m_cipher1->set_key(keys.take(m_cipher1->maximum_keylength()));
35 m_cipher2->set_key(keys.take(m_cipher2->maximum_keylength()));
44 return fmt(
"Cascade({},{})", m_cipher1->name(), m_cipher2->name());
48 return m_cipher1->has_keying_material() && m_cipher2->has_keying_material();
52 return std::make_unique<Cascade_Cipher>(m_cipher1->new_object(), m_cipher2->new_object());
57size_t euclids_algorithm(
size_t a,
size_t b) {
67size_t block_size_for_cascade(
size_t bs,
size_t bs2) {
72 const size_t gcd = euclids_algorithm(bs, bs2);
74 return (bs * bs2) /
gcd;
80 m_cipher1(std::move(cipher1)),
81 m_cipher2(std::move(cipher2)),
82 m_block_size(block_size_for_cascade(m_cipher1->block_size(), m_cipher2->block_size())) {
83 BOTAN_ASSERT(m_block_size % m_cipher1->block_size() == 0 && m_block_size % m_cipher2->block_size() == 0,
84 "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
std::string fmt(std::string_view format, const T &... args)
BigInt gcd(const BigInt &a, const BigInt &b)