Botan  1.11.4
lion.cpp
Go to the documentation of this file.
1 /*
2 * Lion
3 * (C) 1999-2007 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  secure_vector<byte> buffer_vec(LEFT_SIZE);
20  byte* buffer = &buffer_vec[0];
21 
22  for(size_t i = 0; i != blocks; ++i)
23  {
24  xor_buf(buffer, in, &key1[0], LEFT_SIZE);
25  cipher->set_key(buffer, LEFT_SIZE);
26  cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
27 
28  hash->update(out + LEFT_SIZE, RIGHT_SIZE);
29  hash->final(buffer);
30  xor_buf(out, in, buffer, LEFT_SIZE);
31 
32  xor_buf(buffer, out, &key2[0], LEFT_SIZE);
33  cipher->set_key(buffer, LEFT_SIZE);
34  cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
35 
36  in += BLOCK_SIZE;
37  out += BLOCK_SIZE;
38  }
39  }
40 
41 /*
42 * Lion Decryption
43 */
44 void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const
45  {
46  secure_vector<byte> buffer_vec(LEFT_SIZE);
47  byte* buffer = &buffer_vec[0];
48 
49  for(size_t i = 0; i != blocks; ++i)
50  {
51  xor_buf(buffer, in, &key2[0], LEFT_SIZE);
52  cipher->set_key(buffer, LEFT_SIZE);
53  cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
54 
55  hash->update(out + LEFT_SIZE, RIGHT_SIZE);
56  hash->final(buffer);
57  xor_buf(out, in, buffer, LEFT_SIZE);
58 
59  xor_buf(buffer, out, &key1[0], LEFT_SIZE);
60  cipher->set_key(buffer, LEFT_SIZE);
61  cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
62 
63  in += BLOCK_SIZE;
64  out += BLOCK_SIZE;
65  }
66  }
67 
68 /*
69 * Lion Key Schedule
70 */
71 void Lion::key_schedule(const byte key[], size_t length)
72  {
73  clear();
74 
75  key1.assign(key, key + (length / 2));
76  key2.assign(key + (length / 2), key + length);
77  }
78 
79 /*
80 * Return the name of this type
81 */
82 std::string Lion::name() const
83  {
84  return "Lion(" + hash->name() + "," +
85  cipher->name() + "," +
86  std::to_string(BLOCK_SIZE) + ")";
87  }
88 
89 /*
90 * Return a clone of this object
91 */
93  {
94  return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE);
95  }
96 
97 /*
98 * Clear memory of sensitive data
99 */
101  {
102  zap(key1);
103  zap(key2);
104  hash->clear();
105  cipher->clear();
106  }
107 
108 /*
109 * Lion Constructor
110 */
111 Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, size_t block_len) :
112  BLOCK_SIZE(std::max<size_t>(2*hash_in->output_length() + 1, block_len)),
113  LEFT_SIZE(hash_in->output_length()),
114  RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE),
115  hash(hash_in),
116  cipher(sc_in)
117  {
118  if(2*LEFT_SIZE + 1 > BLOCK_SIZE)
119  throw Invalid_Argument(name() + ": Chosen block size is too small");
120 
121  if(!cipher->valid_keylength(LEFT_SIZE))
122  throw Invalid_Argument(name() + ": This stream/hash combo is invalid");
123 
124  key1.resize(LEFT_SIZE);
125  key2.resize(LEFT_SIZE);
126  }
127 
128 }