Botan  1.11.10
lion.cpp
Go to the documentation of this file.
1 /*
2 * Lion
3 * (C) 1999-2007,2014 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/lion.h>
9 #include <botan/internal/xor_buf.h>
10 #include <botan/parsing.h>
11 
12 namespace Botan {
13 
14 /*
15 * Lion Encryption
16 */
17 void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const
18  {
19  const size_t LEFT_SIZE = left_size();
20  const size_t RIGHT_SIZE = right_size();
21 
22  secure_vector<byte> buffer_vec(LEFT_SIZE);
23  byte* buffer = &buffer_vec[0];
24 
25  for(size_t i = 0; i != blocks; ++i)
26  {
27  xor_buf(buffer, in, &m_key1[0], LEFT_SIZE);
28  m_cipher->set_key(buffer, LEFT_SIZE);
29  m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
30 
31  m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
32  m_hash->final(buffer);
33  xor_buf(out, in, buffer, LEFT_SIZE);
34 
35  xor_buf(buffer, out, &m_key2[0], LEFT_SIZE);
36  m_cipher->set_key(buffer, LEFT_SIZE);
37  m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
38 
39  in += m_block_size;
40  out += m_block_size;
41  }
42  }
43 
44 /*
45 * Lion Decryption
46 */
47 void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const
48  {
49  const size_t LEFT_SIZE = left_size();
50  const size_t RIGHT_SIZE = right_size();
51 
52  secure_vector<byte> buffer_vec(LEFT_SIZE);
53  byte* buffer = &buffer_vec[0];
54 
55  for(size_t i = 0; i != blocks; ++i)
56  {
57  xor_buf(buffer, in, &m_key2[0], LEFT_SIZE);
58  m_cipher->set_key(buffer, LEFT_SIZE);
59  m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
60 
61  m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
62  m_hash->final(buffer);
63  xor_buf(out, in, buffer, LEFT_SIZE);
64 
65  xor_buf(buffer, out, &m_key1[0], LEFT_SIZE);
66  m_cipher->set_key(buffer, LEFT_SIZE);
67  m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
68 
69  in += m_block_size;
70  out += m_block_size;
71  }
72  }
73 
74 /*
75 * Lion Key Schedule
76 */
77 void Lion::key_schedule(const byte key[], size_t length)
78  {
79  clear();
80 
81  const size_t half = length / 2;
82  copy_mem(&m_key1[0], key, half);
83  copy_mem(&m_key2[0], key + half, half);
84  }
85 
86 /*
87 * Return the name of this type
88 */
89 std::string Lion::name() const
90  {
91  return "Lion(" + m_hash->name() + "," +
92  m_cipher->name() + "," +
93  std::to_string(block_size()) + ")";
94  }
95 
96 /*
97 * Return a clone of this object
98 */
100  {
101  return new Lion(m_hash->clone(), m_cipher->clone(), block_size());
102  }
103 
104 /*
105 * Clear memory of sensitive data
106 */
108  {
109  zeroise(m_key1);
110  zeroise(m_key2);
111  m_hash->clear();
112  m_cipher->clear();
113  }
114 
115 /*
116 * Lion Constructor
117 */
118 Lion::Lion(HashFunction* hash, StreamCipher* cipher, size_t block_size) :
119  m_block_size(std::max<size_t>(2*hash->output_length() + 1, block_size)),
120  m_hash(hash),
121  m_cipher(cipher)
122  {
123  if(2*left_size() + 1 > m_block_size)
124  throw Invalid_Argument(name() + ": Chosen block size is too small");
125 
126  if(!m_cipher->valid_keylength(left_size()))
127  throw Invalid_Argument(name() + ": This stream/hash combo is invalid");
128 
129  m_key1.resize(left_size());
130  m_key2.resize(left_size());
131  }
132 
133 }
void xor_buf(T out[], const T in[], size_t length)
Definition: xor_buf.h:23
BlockCipher * clone() const override
Definition: lion.cpp:99
void clear() override
Definition: lion.cpp:107
Definition: bigint.h:565
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:46
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:92
size_t block_size() const override
Definition: lion.h:31
void encrypt_n(const byte in[], byte out[], size_t blocks) const override
Definition: lion.cpp:17
uint8_t byte
Definition: types.h:30
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:39
Definition: buf_comp.h:15
void decrypt_n(const byte in[], byte out[], size_t blocks) const override
Definition: lion.cpp:47
Lion(HashFunction *hash, StreamCipher *cipher, size_t block_size)
Definition: lion.cpp:118
std::string name() const override
Definition: lion.cpp:89
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:166