Botan  2.8.0
Crypto and TLS for C++11
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/cascade.h>
9 
10 namespace Botan {
11 
12 void Cascade_Cipher::encrypt_n(const uint8_t in[], uint8_t out[],
13  size_t blocks) const
14  {
15  size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size());
16  size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size());
17 
18  m_cipher1->encrypt_n(in, out, c1_blocks);
19  m_cipher2->encrypt_n(out, out, c2_blocks);
20  }
21 
22 void Cascade_Cipher::decrypt_n(const uint8_t in[], uint8_t out[],
23  size_t blocks) const
24  {
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 
32 void Cascade_Cipher::key_schedule(const uint8_t key[], size_t)
33  {
34  const uint8_t* key2 = key + m_cipher1->maximum_keylength();
35 
36  m_cipher1->set_key(key , m_cipher1->maximum_keylength());
37  m_cipher2->set_key(key2, m_cipher2->maximum_keylength());
38  }
39 
41  {
42  m_cipher1->clear();
43  m_cipher2->clear();
44  }
45 
46 std::string Cascade_Cipher::name() const
47  {
48  return "Cascade(" + m_cipher1->name() + "," + m_cipher2->name() + ")";
49  }
50 
52  {
53  return new Cascade_Cipher(m_cipher1->clone(),
54  m_cipher2->clone());
55  }
56 
57 namespace {
58 
59 size_t euclids_algorithm(size_t a, size_t b)
60  {
61  while(b != 0)
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  const size_t gcd = euclids_algorithm(bs, bs2);
77 
78  return (bs * bs2) / gcd;
79  }
80 
81 }
82 
84  m_cipher1(c1), m_cipher2(c2)
85  {
86  m_block = block_size_for_cascade(c1->block_size(), c2->block_size());
87 
88  BOTAN_ASSERT(m_block % c1->block_size() == 0 &&
89  m_block % c2->block_size() == 0,
90  "Combined block size is a multiple of each ciphers block");
91  }
92 
93 }
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: cascade.cpp:22
BigInt gcd(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:51
std::string name() const override
Definition: cascade.cpp:46
BlockCipher * clone() const override
Definition: cascade.cpp:51
void clear() override
Definition: cascade.cpp:40
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
size_t block_size() const override
Definition: cascade.h:24
Definition: alg_id.cpp:13
Cascade_Cipher(BlockCipher *cipher1, BlockCipher *cipher2)
Definition: cascade.cpp:83
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: cascade.cpp:12
virtual size_t block_size() const =0