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