Botan 1.10.17

cascade.cpp
Go to the documentation of this file.
1 /*
2 * Block Cipher Cascade
3 * (C) 2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/cascade.h>
9
10 namespace Botan {
11
12 void Cascade_Cipher::encrypt_n(const byte in[], byte out[],
13 size_t blocks) const
14 {
15 size_t c1_blocks = blocks * (block_size() / cipher1->block_size());
16 size_t c2_blocks = blocks * (block_size() / cipher2->block_size());
17
18 cipher1->encrypt_n(in, out, c1_blocks);
19 cipher2->encrypt_n(out, out, c2_blocks);
20 }
21
22 void Cascade_Cipher::decrypt_n(const byte in[], byte out[],
23 size_t blocks) const
24 {
25 size_t c1_blocks = blocks * (block_size() / cipher1->block_size());
26 size_t c2_blocks = blocks * (block_size() / cipher2->block_size());
27
28 cipher2->decrypt_n(in, out, c2_blocks);
29 cipher1->decrypt_n(out, out, c1_blocks);
30 }
31
32 void Cascade_Cipher::key_schedule(const byte key[], size_t)
33 {
34 const byte* key2 = key + cipher1->maximum_keylength();
35
36 cipher1->set_key(key , cipher1->maximum_keylength());
37 cipher2->set_key(key2, cipher2->maximum_keylength());
38 }
39
40 void Cascade_Cipher::clear()
41 {
42 cipher1->clear();
43 cipher2->clear();
44 }
45
46 std::string Cascade_Cipher::name() const
47 {
48 return "Cascade(" + cipher1->name() + "," + cipher2->name() + ")";
49 }
50
51 BlockCipher* Cascade_Cipher::clone() const
52 {
53 return new Cascade_Cipher(cipher1->clone(),
54 cipher2->clone());
55 }
56
57 namespace {
58
59 size_t euclids_algorithm(size_t a, size_t b)
60 {
61 while(b != 0) // gcd
62 {
63 size_t t = b;
64 b = a % b;
65 a = t;
66 }
67
68 return a;
69 }
70
71 size_t block_size_for_cascade(size_t bs, size_t bs2)
72 {
73 if(bs == bs2)
74 return bs;
75
76 size_t gcd = euclids_algorithm(bs, bs2);
77
78 return (bs * bs2) / gcd;
79 }
80
81 }
82
83 Cascade_Cipher::Cascade_Cipher(BlockCipher* c1, BlockCipher* c2) :
84 cipher1(c1), cipher2(c2)
85 {
86 block = block_size_for_cascade(c1->block_size(), c2->block_size());
87
88 if(block_size() % c1->block_size() || block_size() % c2->block_size())
89 throw Internal_Error("Failure in " + name() + " constructor");
90 }
91
92 Cascade_Cipher::~Cascade_Cipher()
93 {
94 delete cipher1;
95 delete cipher2;
96 }
97
98 }
Botan::Cascade_Cipher::~Cascade_Cipher
~Cascade_Cipher()
Definition: cascade.cpp:92
Botan::Algorithm::clear
virtual void clear()=0
Botan::Cascade_Cipher::clone
BlockCipher * clone() const
Definition: cascade.cpp:51
Botan::gcd
BigInt gcd(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:168
Botan::BlockCipher::clone
virtual BlockCipher * clone() const =0
Botan::Internal_Error
Definition: exceptn.h:45
Botan::Cascade_Cipher::encrypt_n
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: cascade.cpp:12
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::SymmetricAlgorithm::maximum_keylength
size_t maximum_keylength() const
Definition: sym_algo.h:33
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::Cascade_Cipher::name
std::string name() const
Definition: cascade.cpp:46
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::Cascade_Cipher::Cascade_Cipher
Cascade_Cipher(BlockCipher *cipher1, BlockCipher *cipher2)
Definition: cascade.cpp:83
Botan::Cascade_Cipher::clear
void clear()
Definition: cascade.cpp:40
Botan::BlockCipher::encrypt_n
virtual void encrypt_n(const byte in[], byte out[], size_t blocks) const =0
Botan::BlockCipher::decrypt_n
virtual void decrypt_n(const byte in[], byte out[], size_t blocks) const =0
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::Cascade_Cipher::decrypt_n
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: cascade.cpp:22
Botan::Cascade_Cipher::block_size
size_t block_size() const
Definition: cascade.h:24
Botan::BlockCipher
Definition: block_cipher.h:18