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