Coverage Report

Created: 2025-03-09 06:52

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