Botan 3.3.0
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
13namespace Botan {
14
15void Cascade_Cipher::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
16 size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size());
17 size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size());
18
19 m_cipher1->encrypt_n(in, out, c1_blocks);
20 m_cipher2->encrypt_n(out, out, c2_blocks);
21}
22
23void Cascade_Cipher::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
24 size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size());
25 size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size());
26
27 m_cipher2->decrypt_n(in, out, c2_blocks);
28 m_cipher1->decrypt_n(out, out, c1_blocks);
29}
30
31void Cascade_Cipher::key_schedule(std::span<const uint8_t> key) {
32 BufferSlicer keys(key);
33
34 m_cipher1->set_key(keys.take(m_cipher1->maximum_keylength()));
35 m_cipher2->set_key(keys.take(m_cipher2->maximum_keylength()));
36}
37
39 m_cipher1->clear();
40 m_cipher2->clear();
41}
42
43std::string Cascade_Cipher::name() const {
44 return fmt("Cascade({},{})", m_cipher1->name(), m_cipher2->name());
45}
46
48 return m_cipher1->has_keying_material() && m_cipher2->has_keying_material();
49}
50
51std::unique_ptr<BlockCipher> Cascade_Cipher::new_object() const {
52 return std::make_unique<Cascade_Cipher>(m_cipher1->new_object(), m_cipher2->new_object());
53}
54
55namespace {
56
57size_t euclids_algorithm(size_t a, size_t b) {
58 while(b != 0) {
59 size_t t = b;
60 b = a % b;
61 a = t;
62 }
63
64 return a;
65}
66
67size_t block_size_for_cascade(size_t bs, size_t bs2) {
68 if(bs == bs2) {
69 return bs;
70 }
71
72 const size_t gcd = euclids_algorithm(bs, bs2);
73
74 return (bs * bs2) / gcd;
75}
76
77} // namespace
78
79Cascade_Cipher::Cascade_Cipher(std::unique_ptr<BlockCipher> cipher1, std::unique_ptr<BlockCipher> cipher2) :
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");
85}
86
87} // namespace Botan
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
bool has_keying_material() const override
Definition cascade.cpp:47
std::string name() const override
Definition cascade.cpp:43
Cascade_Cipher(std::unique_ptr< BlockCipher > cipher1, std::unique_ptr< BlockCipher > cipher2)
Definition cascade.cpp:79
std::unique_ptr< BlockCipher > new_object() const override
Definition cascade.cpp:51
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition cascade.cpp:15
void clear() override
Definition cascade.cpp:38
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition cascade.cpp:23
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 gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:193