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