Botan 3.0.0
Crypto and TLS for C&
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/lion.h>
9#include <botan/internal/fmt.h>
10#include <botan/exceptn.h>
11
12namespace Botan {
13
14/*
15* Lion Encryption
16*/
17void Lion::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
18 {
20
21 const size_t LEFT_SIZE = left_size();
22 const size_t RIGHT_SIZE = right_size();
23
24 secure_vector<uint8_t> buffer_vec(LEFT_SIZE);
25 uint8_t* buffer = buffer_vec.data();
26
27 for(size_t i = 0; i != blocks; ++i)
28 {
29 xor_buf(buffer, in, m_key1.data(), LEFT_SIZE);
30 m_cipher->set_key(buffer, LEFT_SIZE);
31 m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
32
33 m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
34 m_hash->final(buffer);
35 xor_buf(out, in, buffer, LEFT_SIZE);
36
37 xor_buf(buffer, out, m_key2.data(), LEFT_SIZE);
38 m_cipher->set_key(buffer, LEFT_SIZE);
39 m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
40
41 in += m_block_size;
42 out += m_block_size;
43 }
44 }
45
46/*
47* Lion Decryption
48*/
49void Lion::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
50 {
52
53 const size_t LEFT_SIZE = left_size();
54 const size_t RIGHT_SIZE = right_size();
55
56 secure_vector<uint8_t> buffer_vec(LEFT_SIZE);
57 uint8_t* buffer = buffer_vec.data();
58
59 for(size_t i = 0; i != blocks; ++i)
60 {
61 xor_buf(buffer, in, m_key2.data(), LEFT_SIZE);
62 m_cipher->set_key(buffer, LEFT_SIZE);
63 m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
64
65 m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
66 m_hash->final(buffer);
67 xor_buf(out, in, buffer, LEFT_SIZE);
68
69 xor_buf(buffer, out, m_key1.data(), LEFT_SIZE);
70 m_cipher->set_key(buffer, LEFT_SIZE);
71 m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
72
73 in += m_block_size;
74 out += m_block_size;
75 }
76 }
77
79 {
80 return !m_key1.empty() && !m_key2.empty();
81 }
82
83/*
84* Lion Key Schedule
85*/
86void Lion::key_schedule(const uint8_t key[], size_t length)
87 {
88 clear();
89
90 const size_t half = length / 2;
91
92 m_key1.resize(left_size());
93 m_key2.resize(left_size());
94 clear_mem(m_key1.data(), m_key1.size());
95 clear_mem(m_key2.data(), m_key2.size());
96 copy_mem(m_key1.data(), key, half);
97 copy_mem(m_key2.data(), key + half, half);
98 }
99
100/*
101* Return the name of this type
102*/
103std::string Lion::name() const
104 {
105 return fmt("Lion({},{},{})", m_hash->name(), m_cipher->name(), block_size());
106 }
107
108std::unique_ptr<BlockCipher> Lion::new_object() const
109 {
110 return std::make_unique<Lion>(m_hash->new_object(), m_cipher->new_object(), block_size());
111 }
112
113/*
114* Clear memory of sensitive data
115*/
117 {
118 zap(m_key1);
119 zap(m_key2);
120 m_hash->clear();
121 m_cipher->clear();
122 }
123
124/*
125* Lion Constructor
126*/
127Lion::Lion(std::unique_ptr<HashFunction> hash,
128 std::unique_ptr<StreamCipher> cipher,
129 size_t bs) :
130 m_block_size(std::max<size_t>(2*hash->output_length() + 1, bs)),
131 m_hash(std::move(hash)),
132 m_cipher(std::move(cipher))
133 {
134 if(2*left_size() + 1 > m_block_size)
135 {
136 throw Invalid_Argument(fmt("Block size {} is too small for {}", m_block_size, name()));
137 }
138
139 if(!m_cipher->valid_keylength(left_size()))
140 {
141 throw Invalid_Argument(fmt("Lion does not support combining {} and {}",
142 m_cipher->name(), m_hash->name()));
143 }
144 }
145
146}
std::unique_ptr< BlockCipher > new_object() const override
Definition: lion.cpp:108
bool has_keying_material() const override
Definition: lion.cpp:78
void clear() override
Definition: lion.cpp:116
size_t block_size() const override
Definition: lion.h:31
Lion(std::unique_ptr< HashFunction > hash, std::unique_ptr< StreamCipher > cipher, size_t block_size)
Definition: lion.cpp:127
std::string name() const override
Definition: lion.cpp:103
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: lion.cpp:49
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition: lion.cpp:17
void assert_key_material_set() const
Definition: sym_algo.h:182
Definition: alg_id.cpp:12
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:129
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:126
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:255
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64
constexpr void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:115
Definition: bigint.h:1092