Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/sym_algo.h
Line
Count
Source
1
/*
2
* Symmetric Algorithm Base Class
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_SYMMETRIC_ALGORITHM_H_
9
#define BOTAN_SYMMETRIC_ALGORITHM_H_
10
11
#include <botan/types.h>
12
#include <span>
13
#include <string>
14
15
namespace Botan {
16
17
class OctetString;
18
19
/**
20
* Represents the length requirements on an algorithm key
21
*/
22
class BOTAN_PUBLIC_API(2, 0) Key_Length_Specification final {
23
   public:
24
      /**
25
      * Constructor for fixed length keys
26
      * @param keylen the supported key length
27
      */
28
193k
      explicit Key_Length_Specification(size_t keylen) : m_min_keylen(keylen), m_max_keylen(keylen), m_keylen_mod(1) {}
29
30
      /**
31
      * Constructor for variable length keys
32
      * @param min_k the smallest supported key length
33
      * @param max_k the largest supported key length
34
      * @param k_mod the number of bytes the key must be a multiple of
35
      */
36
      Key_Length_Specification(size_t min_k, size_t max_k, size_t k_mod = 1) :
37
771k
            m_min_keylen(min_k), m_max_keylen(max_k > 0 ? max_k : min_k), m_keylen_mod(k_mod) {}
38
39
      /**
40
      * Test if a key length is acceptable
41
      * @param length is a key length in bytes
42
      * @return true iff this length is a valid length for this algo
43
      */
44
964k
      bool valid_keylength(size_t length) const {
45
964k
         return ((length >= m_min_keylen) && (length <= m_max_keylen) && (length % m_keylen_mod == 0));
46
964k
      }
47
48
      /**
49
      * Return the smallest acceptable key length
50
      * @return minimum key length in bytes
51
      */
52
0
      size_t minimum_keylength() const { return m_min_keylen; }
53
54
      /**
55
      * Return the largest acceptable key length
56
      * @return maximum key length in bytes
57
      */
58
0
      size_t maximum_keylength() const { return m_max_keylen; }
59
60
      /**
61
      * Return the granularity of acceptable key lengths
62
      * @return key length multiple in bytes
63
      */
64
0
      size_t keylength_multiple() const { return m_keylen_mod; }
65
66
      /**
67
      * Scale all length requirements by a factor
68
      * Multiplies all length requirements with the given factor
69
      * @param n the multiplication factor
70
      * @return a key length specification multiplied by the factor
71
      */
72
0
      Key_Length_Specification multiple(size_t n) const {
73
0
         return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod);
74
0
      }
75
76
   private:
77
      size_t m_min_keylen, m_max_keylen, m_keylen_mod;
78
};
79
80
/**
81
* This class represents a symmetric algorithm object.
82
*/
83
class BOTAN_PUBLIC_API(2, 0) SymmetricAlgorithm {
84
   public:
85
      /**
86
      * Default constructor
87
      */
88
284k
      SymmetricAlgorithm() = default;
89
90
284k
      virtual ~SymmetricAlgorithm() = default;
91
92
      /**
93
      * Copy constructor
94
      */
95
0
      SymmetricAlgorithm(const SymmetricAlgorithm& other) = default;
96
97
      /**
98
      * Move constructor
99
      */
100
      SymmetricAlgorithm(SymmetricAlgorithm&& other) = default;
101
102
      /**
103
      * Copy assignment
104
      * @return reference to this
105
      */
106
      SymmetricAlgorithm& operator=(const SymmetricAlgorithm& other) = default;
107
108
      /**
109
      * Move assignment
110
      * @return reference to this
111
      */
112
      SymmetricAlgorithm& operator=(SymmetricAlgorithm&& other) = default;
113
114
      /**
115
      * Reset the internal state. This includes not just the key, but
116
      * any partial message that may have been in process.
117
      */
118
      virtual void clear() = 0;
119
120
      /**
121
      * Return the key lengths supported by this algorithm
122
      * @return object describing limits on key size
123
      */
124
      virtual Key_Length_Specification key_spec() const = 0;
125
126
      /**
127
      * Return the largest acceptable key length
128
      * @return maximum allowed key length
129
      */
130
0
      size_t maximum_keylength() const { return key_spec().maximum_keylength(); }
131
132
      /**
133
      * Return the smallest acceptable key length
134
      * @return minimum allowed key length
135
      */
136
0
      size_t minimum_keylength() const { return key_spec().minimum_keylength(); }
137
138
      /**
139
      * Check whether a given key length is valid for this algorithm.
140
      * @param length the key length to be checked.
141
      * @return true if the key length is valid.
142
      */
143
774k
      bool valid_keylength(size_t length) const { return key_spec().valid_keylength(length); }
144
145
      /**
146
      * Set the symmetric key of this object.
147
      * @param key the SymmetricKey to be set.
148
      */
149
      void set_key(const OctetString& key);
150
151
      /**
152
      * Set the symmetric key of this object.
153
      * @param key the contiguous byte range to be set.
154
      */
155
      void set_key(std::span<const uint8_t> key);
156
157
      /**
158
      * Set the symmetric key of this object.
159
      * @param key the to be set as a byte array.
160
      * @param length in bytes of key param
161
      */
162
82.6k
      void set_key(const uint8_t key[], size_t length) { set_key(std::span{key, length}); }
163
164
      /**
165
      * Return the name of this algorithm
166
      * @return the algorithm name
167
      */
168
      virtual std::string name() const = 0;
169
170
      /**
171
      * Test whether a key has been set on this object
172
      * @return true if a key has been set on this object
173
      */
174
      virtual bool has_keying_material() const = 0;
175
176
   protected:
177
      /**
178
      * Throw Key_Not_Set unless a key has been set on this object
179
      */
180
40.3M
      void assert_key_material_set() const { assert_key_material_set(has_keying_material()); }
181
182
      /**
183
      * Throw Key_Not_Set unless the predicate holds
184
      * @param predicate if false, a Key_Not_Set exception is thrown
185
      */
186
40.3M
      void assert_key_material_set(bool predicate) const {
187
40.3M
         if(!predicate) {
188
0
            throw_key_not_set_error();
189
0
         }
190
40.3M
      }
191
192
   private:
193
      void throw_key_not_set_error() const;
194
195
      /**
196
      * Run the key schedule
197
      * @param key the key
198
      */
199
      virtual void key_schedule(std::span<const uint8_t> key) = 0;
200
};
201
202
}  // namespace Botan
203
204
#endif