8#include <botan/internal/cascade.h>
9#include <botan/internal/fmt.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);
26 size_t c1_blocks = blocks * (
block_size() / m_cipher1->block_size());
27 size_t c2_blocks = blocks * (
block_size() / m_cipher2->block_size());
29 m_cipher2->decrypt_n(in, out, c2_blocks);
30 m_cipher1->decrypt_n(out, out, c1_blocks);
33void Cascade_Cipher::key_schedule(
const uint8_t key[],
size_t )
35 const uint8_t* key2 = key + m_cipher1->maximum_keylength();
37 m_cipher1->set_key(key , m_cipher1->maximum_keylength());
38 m_cipher2->set_key(key2, m_cipher2->maximum_keylength());
49 return fmt(
"Cascade({},{})", m_cipher1->name(), m_cipher2->name());
54 return m_cipher1->has_keying_material() &&
55 m_cipher2->has_keying_material();
60 return std::make_unique<Cascade_Cipher>(m_cipher1->new_object(),
61 m_cipher2->new_object());
66size_t euclids_algorithm(
size_t a,
size_t b)
78size_t block_size_for_cascade(
size_t bs,
size_t bs2)
83 const size_t gcd = euclids_algorithm(bs, bs2);
85 return (bs * bs2) /
gcd;
91 std::unique_ptr<BlockCipher> cipher2) :
92 m_cipher1(
std::move(cipher1)),
93 m_cipher2(
std::move(cipher2)),
94 m_block_size(block_size_for_cascade(m_cipher1->block_size(), m_cipher2->block_size()))
96 BOTAN_ASSERT(m_block_size % m_cipher1->block_size() == 0 &&
97 m_block_size % m_cipher2->block_size() == 0,
98 "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)