Botan  2.4.0
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 
10 namespace Botan {
11 
12 /*
13 * Lion Encryption
14 */
15 void Lion::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
16  {
17  verify_key_set(m_key1.empty() == false);
18 
19  const size_t LEFT_SIZE = left_size();
20  const size_t RIGHT_SIZE = right_size();
21 
22  secure_vector<uint8_t> buffer_vec(LEFT_SIZE);
23  uint8_t* buffer = buffer_vec.data();
24 
25  for(size_t i = 0; i != blocks; ++i)
26  {
27  xor_buf(buffer, in, m_key1.data(), 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.data(), 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 uint8_t in[], uint8_t out[], size_t blocks) const
48  {
49  verify_key_set(m_key1.empty() == false);
50 
51  const size_t LEFT_SIZE = left_size();
52  const size_t RIGHT_SIZE = right_size();
53 
54  secure_vector<uint8_t> buffer_vec(LEFT_SIZE);
55  uint8_t* buffer = buffer_vec.data();
56 
57  for(size_t i = 0; i != blocks; ++i)
58  {
59  xor_buf(buffer, in, m_key2.data(), LEFT_SIZE);
60  m_cipher->set_key(buffer, LEFT_SIZE);
61  m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
62 
63  m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
64  m_hash->final(buffer);
65  xor_buf(out, in, buffer, LEFT_SIZE);
66 
67  xor_buf(buffer, out, m_key1.data(), LEFT_SIZE);
68  m_cipher->set_key(buffer, LEFT_SIZE);
69  m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
70 
71  in += m_block_size;
72  out += m_block_size;
73  }
74  }
75 
76 /*
77 * Lion Key Schedule
78 */
79 void Lion::key_schedule(const uint8_t key[], size_t length)
80  {
81  clear();
82 
83  const size_t half = length / 2;
84 
85  m_key1.resize(left_size());
86  m_key2.resize(left_size());
87  clear_mem(m_key1.data(), m_key1.size());
88  clear_mem(m_key2.data(), m_key2.size());
89  copy_mem(m_key1.data(), key, half);
90  copy_mem(m_key2.data(), key + half, half);
91  }
92 
93 /*
94 * Return the name of this type
95 */
96 std::string Lion::name() const
97  {
98  return "Lion(" + m_hash->name() + "," +
99  m_cipher->name() + "," +
100  std::to_string(block_size()) + ")";
101  }
102 
103 /*
104 * Return a clone of this object
105 */
107  {
108  return new Lion(m_hash->clone(), m_cipher->clone(), block_size());
109  }
110 
111 /*
112 * Clear memory of sensitive data
113 */
115  {
116  zap(m_key1);
117  zap(m_key2);
118  m_hash->clear();
119  m_cipher->clear();
120  }
121 
122 /*
123 * Lion Constructor
124 */
125 Lion::Lion(HashFunction* hash, StreamCipher* cipher, size_t bs) :
126  m_block_size(std::max<size_t>(2*hash->output_length() + 1, bs)),
127  m_hash(hash),
128  m_cipher(cipher)
129  {
130  if(2*left_size() + 1 > m_block_size)
131  throw Invalid_Argument(name() + ": Chosen block size is too small");
132 
133  if(!m_cipher->valid_keylength(left_size()))
134  throw Invalid_Argument(name() + ": This stream/hash combo is invalid");
135  }
136 
137 }
BlockCipher * clone() const override
Definition: lion.cpp:106
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: lion.cpp:15
void clear() override
Definition: lion.cpp:114
void verify_key_set(bool cond) const
Definition: sym_algo.h:95
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:191
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:86
Definition: bigint.h:635
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
size_t block_size() const override
Definition: lion.h:31
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:163
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:97
Definition: alg_id.cpp:13
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: lion.cpp:47
Lion(HashFunction *hash, StreamCipher *cipher, size_t block_size)
Definition: lion.cpp:125
MechanismType hash
std::string name() const override
Definition: lion.cpp:96