Botan: src/block/lion/lion.cpp Source File

Botan 1.10.16

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 SecureVector<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 SecureVector<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.copy(key, length / 2);
76 key2.copy(key + length / 2, length / 2);
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 to_string(BLOCK_SIZE) + ")";
87 }
88
89 /*
90 * Return a clone of this object
91 */
92 BlockCipher* Lion::clone() const
93 {
94 return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE);
95 }
96
97 /*
98 * Clear memory of sensitive data
99 */
100 void Lion::clear()
101 {
102 hash->clear();
103 cipher->clear();
104 zeroise(key1);
105 zeroise(key2);
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 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
Botan::Algorithm::clear
virtual void clear()=0
Botan::Lion::encrypt_n
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: lion.cpp:17
Botan::StreamCipher::cipher1
void cipher1(byte buf[], size_t len)
Definition: stream_cipher.h:34
std
Definition: secmem.h:422
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::HashFunction::clone
virtual HashFunction * clone() const =0
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:120
Botan::Lion::clear
void clear()
Definition: lion.cpp:100
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::SymmetricAlgorithm::valid_keylength
bool valid_keylength(size_t length) const
Definition: sym_algo.h:51
Botan::Lion::clone
BlockCipher * clone() const
Definition: lion.cpp:92
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::StreamCipher
Definition: stream_cipher.h:18
Botan::Lion::decrypt_n
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: lion.cpp:44
Botan::Buffered_Computation::update
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
Botan::StreamCipher::cipher
virtual void cipher(const byte in[], byte out[], size_t len)=0
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::Buffered_Computation::final
void final(byte out[])
Definition: buf_comp.h:80
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::Lion::name
std::string name() const
Definition: lion.cpp:82
Botan::HashFunction
Definition: hash.h:20
Botan::StreamCipher::clone
virtual StreamCipher * clone() const =0
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::SecureVector< byte >
Botan::Lion::Lion
Lion(HashFunction *hash, StreamCipher *cipher, size_t block_size)
Definition: lion.cpp:111
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
Botan::BlockCipher
Definition: block_cipher.h:18