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
10namespace Botan {
11
12void 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
22void 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
32void 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
41 {
42 cipher1->clear();
43 cipher2->clear();
44 }
45
46std::string Cascade_Cipher::name() const
47 {
48 return "Cascade(" + cipher1->name() + "," + cipher2->name() + ")";
49 }
50
52 {
53 return new Cascade_Cipher(cipher1->clone(),
54 cipher2->clone());
55 }
56
57namespace {
58
59size_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
71size_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
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
93 {
94 delete cipher1;
95 delete cipher2;
96 }
97
98}
virtual size_t block_size() const =0
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition cascade.cpp:12
Cascade_Cipher(BlockCipher *cipher1, BlockCipher *cipher2)
Definition cascade.cpp:83
std::string name() const
Definition cascade.cpp:46
BlockCipher * clone() const
Definition cascade.cpp:51
size_t block_size() const
Definition cascade.h:24
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition cascade.cpp:22
void set_key(const SymmetricKey &key)
Definition sym_algo.h:60
size_t maximum_keylength() const
Definition sym_algo.h:33
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:168
Internal_Error(const std::string &err)
Definition exceptn.h:47