Botan  1.11.30
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 
14  {
15  if(spec.arg_count_between(2, 3))
16  {
17  std::unique_ptr<HashFunction> hash(HashFunction::create(spec.arg(0)));
18  std::unique_ptr<StreamCipher> stream(StreamCipher::create(spec.arg(1)));
19 
20  if(hash && stream)
21  {
22  const size_t block_size = spec.arg_as_integer(2, 1024);
23  return new Lion(hash.release(), stream.release(), block_size);
24  }
25  }
26  return nullptr;
27  }
28 
29 /*
30 * Lion Encryption
31 */
32 void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const
33  {
34  const size_t LEFT_SIZE = left_size();
35  const size_t RIGHT_SIZE = right_size();
36 
37  secure_vector<byte> buffer_vec(LEFT_SIZE);
38  byte* buffer = buffer_vec.data();
39 
40  for(size_t i = 0; i != blocks; ++i)
41  {
42  xor_buf(buffer, in, m_key1.data(), LEFT_SIZE);
43  m_cipher->set_key(buffer, LEFT_SIZE);
44  m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
45 
46  m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
47  m_hash->final(buffer);
48  xor_buf(out, in, buffer, LEFT_SIZE);
49 
50  xor_buf(buffer, out, m_key2.data(), LEFT_SIZE);
51  m_cipher->set_key(buffer, LEFT_SIZE);
52  m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
53 
54  in += m_block_size;
55  out += m_block_size;
56  }
57  }
58 
59 /*
60 * Lion Decryption
61 */
62 void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const
63  {
64  const size_t LEFT_SIZE = left_size();
65  const size_t RIGHT_SIZE = right_size();
66 
67  secure_vector<byte> buffer_vec(LEFT_SIZE);
68  byte* buffer = buffer_vec.data();
69 
70  for(size_t i = 0; i != blocks; ++i)
71  {
72  xor_buf(buffer, in, m_key2.data(), LEFT_SIZE);
73  m_cipher->set_key(buffer, LEFT_SIZE);
74  m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
75 
76  m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
77  m_hash->final(buffer);
78  xor_buf(out, in, buffer, LEFT_SIZE);
79 
80  xor_buf(buffer, out, m_key1.data(), LEFT_SIZE);
81  m_cipher->set_key(buffer, LEFT_SIZE);
82  m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
83 
84  in += m_block_size;
85  out += m_block_size;
86  }
87  }
88 
89 /*
90 * Lion Key Schedule
91 */
92 void Lion::key_schedule(const byte key[], size_t length)
93  {
94  clear();
95 
96  const size_t half = length / 2;
97  copy_mem(m_key1.data(), key, half);
98  copy_mem(m_key2.data(), key + half, half);
99  }
100 
101 /*
102 * Return the name of this type
103 */
104 std::string Lion::name() const
105  {
106  return "Lion(" + m_hash->name() + "," +
107  m_cipher->name() + "," +
108  std::to_string(block_size()) + ")";
109  }
110 
111 /*
112 * Return a clone of this object
113 */
115  {
116  return new Lion(m_hash->clone(), m_cipher->clone(), block_size());
117  }
118 
119 /*
120 * Clear memory of sensitive data
121 */
123  {
124  zeroise(m_key1);
125  zeroise(m_key2);
126  m_hash->clear();
127  m_cipher->clear();
128  }
129 
130 /*
131 * Lion Constructor
132 */
133 Lion::Lion(HashFunction* hash, StreamCipher* cipher, size_t bs) :
134  m_block_size(std::max<size_t>(2*hash->output_length() + 1, bs)),
135  m_hash(hash),
136  m_cipher(cipher)
137  {
138  if(2*left_size() + 1 > m_block_size)
139  throw Invalid_Argument(name() + ": Chosen block size is too small");
140 
141  if(!m_cipher->valid_keylength(left_size()))
142  throw Invalid_Argument(name() + ": This stream/hash combo is invalid");
143 
144  m_key1.resize(left_size());
145  m_key2.resize(left_size());
146  }
147 
148 }
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:90
std::string arg(size_t i) const
Definition: scan_name.cpp:153
BlockCipher * clone() const override
Definition: lion.cpp:114
void clear() override
Definition: lion.cpp:122
Definition: bigint.h:610
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:96
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:32
static Lion * make(const Spec &)
Definition: lion.cpp:13
bool arg_count_between(size_t lower, size_t upper) const
Definition: scan_name.h:71
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:98
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:168
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:43
Definition: alg_id.cpp:13
T max(T a, T b)
Definition: ct_utils.h:173
static std::unique_ptr< StreamCipher > create(const std::string &algo_spec, const std::string &provider="")
void decrypt_n(const byte in[], byte out[], size_t blocks) const override
Definition: lion.cpp:62
Lion(HashFunction *hash, StreamCipher *cipher, size_t block_size)
Definition: lion.cpp:133
std::string name() const override
Definition: lion.cpp:104
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:186
std::uint8_t byte
Definition: types.h:31