• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13262741994

11 Feb 2025 12:19PM UTC coverage: 91.656% (-0.003%) from 91.659%
13262741994

Pull #4647

github

web-flow
Merge 0b8e56724 into f372b5a9e
Pull Request #4647: Avoid using mem_ops.h or assert.h in public headers

94864 of 103500 relevant lines covered (91.66%)

11330304.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.1
/src/lib/block/lion/lion.cpp
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
void Lion::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
5✔
20
   assert_key_material_set();
5✔
21

22
   const size_t LEFT_SIZE = left_size();
3✔
23
   const size_t RIGHT_SIZE = right_size();
3✔
24

25
   secure_vector<uint8_t> buffer_vec(LEFT_SIZE);
3✔
26
   uint8_t* buffer = buffer_vec.data();
3✔
27

28
   for(size_t i = 0; i != blocks; ++i) {
6✔
29
      xor_buf(buffer, in, m_key1.data(), LEFT_SIZE);
3✔
30
      m_cipher->set_key(buffer, LEFT_SIZE);
3✔
31
      m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
3✔
32

33
      m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
3✔
34
      m_hash->final(buffer);
3✔
35
      xor_buf(out, in, buffer, LEFT_SIZE);
3✔
36

37
      xor_buf(buffer, out, m_key2.data(), LEFT_SIZE);
3✔
38
      m_cipher->set_key(buffer, LEFT_SIZE);
3✔
39
      m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
3✔
40

41
      in += m_block_size;
3✔
42
      out += m_block_size;
3✔
43
   }
44
}
3✔
45

46
/*
47
* Lion Decryption
48
*/
49
void Lion::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
4✔
50
   assert_key_material_set();
4✔
51

52
   const size_t LEFT_SIZE = left_size();
2✔
53
   const size_t RIGHT_SIZE = right_size();
2✔
54

55
   secure_vector<uint8_t> buffer_vec(LEFT_SIZE);
2✔
56
   uint8_t* buffer = buffer_vec.data();
2✔
57

58
   for(size_t i = 0; i != blocks; ++i) {
4✔
59
      xor_buf(buffer, in, m_key2.data(), LEFT_SIZE);
2✔
60
      m_cipher->set_key(buffer, LEFT_SIZE);
2✔
61
      m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
2✔
62

63
      m_hash->update(out + LEFT_SIZE, RIGHT_SIZE);
2✔
64
      m_hash->final(buffer);
2✔
65
      xor_buf(out, in, buffer, LEFT_SIZE);
2✔
66

67
      xor_buf(buffer, out, m_key1.data(), LEFT_SIZE);
2✔
68
      m_cipher->set_key(buffer, LEFT_SIZE);
2✔
69
      m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
2✔
70

71
      in += m_block_size;
2✔
72
      out += m_block_size;
2✔
73
   }
74
}
2✔
75

76
bool Lion::has_keying_material() const {
13✔
77
   return !m_key1.empty() && !m_key2.empty();
13✔
78
}
79

80
/*
81
* Lion Key Schedule
82
*/
83
void Lion::key_schedule(std::span<const uint8_t> key) {
3✔
84
   clear();
3✔
85

86
   const size_t half = key.size() / 2;
3✔
87

88
   m_key1.resize(left_size());
3✔
89
   m_key2.resize(left_size());
3✔
90
   clear_mem(m_key1.data(), m_key1.size());
3✔
91
   clear_mem(m_key2.data(), m_key2.size());
3✔
92
   copy_mem(m_key1.data(), key.data(), half);
3✔
93
   copy_mem(m_key2.data(), key.subspan(half, half).data(), half);
3✔
94
}
3✔
95

96
/*
97
* Return the name of this type
98
*/
99
std::string Lion::name() const {
7✔
100
   return fmt("Lion({},{},{})", m_hash->name(), m_cipher->name(), block_size());
7✔
101
}
102

103
std::unique_ptr<BlockCipher> Lion::new_object() const {
1✔
104
   return std::make_unique<Lion>(m_hash->new_object(), m_cipher->new_object(), block_size());
1✔
105
}
106

107
/*
108
* Clear memory of sensitive data
109
*/
110
void Lion::clear() {
5✔
111
   zap(m_key1);
5✔
112
   zap(m_key2);
5✔
113
   m_hash->clear();
5✔
114
   m_cipher->clear();
5✔
115
}
5✔
116

117
/*
118
* Lion Constructor
119
*/
120
Lion::Lion(std::unique_ptr<HashFunction> hash, std::unique_ptr<StreamCipher> cipher, size_t bs) :
3✔
121
      m_block_size(std::max<size_t>(2 * hash->output_length() + 1, bs)),
3✔
122
      m_hash(std::move(hash)),
3✔
123
      m_cipher(std::move(cipher)) {
6✔
124
   if(2 * left_size() + 1 > m_block_size) {
3✔
125
      throw Invalid_Argument(fmt("Block size {} is too small for {}", m_block_size, name()));
×
126
   }
127

128
   if(!m_cipher->valid_keylength(left_size())) {
3✔
129
      throw Invalid_Argument(fmt("Lion does not support combining {} and {}", m_cipher->name(), m_hash->name()));
×
130
   }
131
}
3✔
132

133
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc