Botan 3.6.1
Crypto and TLS for C&
cascade.cpp
Go to the documentation of this file.
1/*
2* Block Cipher Cascade
3* (C) 2010 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/cascade.h>
9
10#include <botan/internal/fmt.h>
11#include <botan/internal/stl_util.h>
12#include <numeric>
13
14namespace Botan {
15
16void Cascade_Cipher::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
17 size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size());
18 size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size());
19
20 m_cipher1->encrypt_n(in, out, c1_blocks);
21 m_cipher2->encrypt_n(out, out, c2_blocks);
22}
23
24void Cascade_Cipher::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
25 size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size());
26 size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size());
27
28 m_cipher2->decrypt_n(in, out, c2_blocks);
29 m_cipher1->decrypt_n(out, out, c1_blocks);
30}
31
32void Cascade_Cipher::key_schedule(std::span<const uint8_t> key) {
33 BufferSlicer keys(key);
34
35 m_cipher1->set_key(keys.take(m_cipher1->maximum_keylength()));
36 m_cipher2->set_key(keys.take(m_cipher2->maximum_keylength()));
37}
38
40 m_cipher1->clear();
41 m_cipher2->clear();
42}
43
44std::string Cascade_Cipher::name() const {
45 return fmt("Cascade({},{})", m_cipher1->name(), m_cipher2->name());
46}
47
49 return m_cipher1->has_keying_material() && m_cipher2->has_keying_material();
50}
51
52std::unique_ptr<BlockCipher> Cascade_Cipher::new_object() const {
53 return std::make_unique<Cascade_Cipher>(m_cipher1->new_object(), m_cipher2->new_object());
54}
55
56Cascade_Cipher::Cascade_Cipher(std::unique_ptr<BlockCipher> cipher1, std::unique_ptr<BlockCipher> cipher2) :
57 m_cipher1(std::move(cipher1)),
58 m_cipher2(std::move(cipher2)),
59 m_block_size(std::lcm(m_cipher1->block_size(), m_cipher2->block_size())) {
60 BOTAN_ASSERT(m_block_size % m_cipher1->block_size() == 0 && m_block_size % m_cipher2->block_size() == 0,
61 "Combined block size is a multiple of each ciphers block");
62}
63
64} // namespace Botan
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
bool has_keying_material() const override
Definition cascade.cpp:48
std::string name() const override
Definition cascade.cpp:44
Cascade_Cipher(std::unique_ptr< BlockCipher > cipher1, std::unique_ptr< BlockCipher > cipher2)
Definition cascade.cpp:56
std::unique_ptr< BlockCipher > new_object() const override
Definition cascade.cpp:52
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition cascade.cpp:16
void clear() override
Definition cascade.cpp:39
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition cascade.cpp:24
size_t block_size() const override
Definition cascade.h:23
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
BigInt lcm(const BigInt &a, const BigInt &b)
Definition numthry.cpp:270