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