Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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