Coverage Report

Created: 2026-06-30 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/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
13
#include <span>
14
#include <string>
15
16
namespace Botan {
17
18
class OctetString;
19
20
/**
21
* Represents the length requirements on an algorithm key
22
*/
23
class BOTAN_PUBLIC_API(2, 0) Key_Length_Specification final {
24
   public:
25
      /**
26
      * Constructor for fixed length keys
27
      * @param keylen the supported key length
28
      */
29
0
      explicit Key_Length_Specification(size_t keylen) : m_min_keylen(keylen), m_max_keylen(keylen), m_keylen_mod(1) {}
30
31
      /**
32
      * Constructor for variable length keys
33
      * @param min_k the smallest supported key length
34
      * @param max_k the largest supported key length
35
      * @param k_mod the number of bytes the key must be a multiple of
36
      */
37
      Key_Length_Specification(size_t min_k, size_t max_k, size_t k_mod = 1) :
38
0
            m_min_keylen(min_k), m_max_keylen(max_k > 0 ? max_k : min_k), m_keylen_mod(k_mod) {}
39
40
      /**
41
      * @param length is a key length in bytes
42
      * @return true iff this length is a valid length for this algo
43
      */
44
0
      bool valid_keylength(size_t length) const {
45
0
         return ((length >= m_min_keylen) && (length <= m_max_keylen) && (length % m_keylen_mod == 0));
46
0
      }
47
48
      /**
49
      * @return minimum key length in bytes
50
      */
51
0
      size_t minimum_keylength() const { return m_min_keylen; }
52
53
      /**
54
      * @return maximum key length in bytes
55
      */
56
0
      size_t maximum_keylength() const { return m_max_keylen; }
57
58
      /**
59
      * @return key length multiple in bytes
60
      */
61
0
      size_t keylength_multiple() const { return m_keylen_mod; }
62
63
      /*
64
      * Multiplies all length requirements with the given factor
65
      * @param n the multiplication factor
66
      * @return a key length specification multiplied by the factor
67
      */
68
0
      Key_Length_Specification multiple(size_t n) const {
69
0
         return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod);
70
0
      }
71
72
   private:
73
      size_t m_min_keylen, m_max_keylen, m_keylen_mod;
74
};
75
76
/**
77
* This class represents a symmetric algorithm object.
78
*/
79
class BOTAN_PUBLIC_API(2, 0) SymmetricAlgorithm {
80
   public:
81
0
      SymmetricAlgorithm() = default;
82
0
      virtual ~SymmetricAlgorithm() = default;
83
0
      SymmetricAlgorithm(const SymmetricAlgorithm& other) = default;
84
      SymmetricAlgorithm(SymmetricAlgorithm&& other) = default;
85
      SymmetricAlgorithm& operator=(const SymmetricAlgorithm& other) = default;
86
      SymmetricAlgorithm& operator=(SymmetricAlgorithm&& other) = default;
87
88
      /**
89
      * Reset the internal state. This includes not just the key, but
90
      * any partial message that may have been in process.
91
      */
92
      virtual void clear() = 0;
93
94
      /**
95
      * @return object describing limits on key size
96
      */
97
      virtual Key_Length_Specification key_spec() const = 0;
98
99
      /**
100
      * @return maximum allowed key length
101
      */
102
0
      size_t maximum_keylength() const { return key_spec().maximum_keylength(); }
103
104
      /**
105
      * @return minimum allowed key length
106
      */
107
0
      size_t minimum_keylength() const { return key_spec().minimum_keylength(); }
108
109
      /**
110
      * Check whether a given key length is valid for this algorithm.
111
      * @param length the key length to be checked.
112
      * @return true if the key length is valid.
113
      */
114
0
      bool valid_keylength(size_t length) const { return key_spec().valid_keylength(length); }
115
116
      /**
117
      * Set the symmetric key of this object.
118
      * @param key the SymmetricKey to be set.
119
      */
120
      void set_key(const OctetString& key);
121
122
      /**
123
      * Set the symmetric key of this object.
124
      * @param key the contiguous byte range to be set.
125
      */
126
      void set_key(std::span<const uint8_t> key);
127
128
      /**
129
      * Set the symmetric key of this object.
130
      * @param key the to be set as a byte array.
131
      * @param length in bytes of key param
132
      */
133
0
      void set_key(const uint8_t key[], size_t length) { set_key(std::span{key, length}); }
134
135
      /**
136
      * @return the algorithm name
137
      */
138
      virtual std::string name() const = 0;
139
140
      /**
141
      * @return true if a key has been set on this object
142
      */
143
      virtual bool has_keying_material() const = 0;
144
145
   protected:
146
0
      void assert_key_material_set() const { assert_key_material_set(has_keying_material()); }
147
148
0
      void assert_key_material_set(bool predicate) const {
149
0
         if(!predicate) {
150
0
            throw_key_not_set_error();
151
0
         }
152
0
      }
153
154
   private:
155
      void throw_key_not_set_error() const;
156
157
      /**
158
      * Run the key schedule
159
      * @param key the key
160
      */
161
      virtual void key_schedule(std::span<const uint8_t> key) = 0;
162
};
163
164
}  // namespace Botan
165
166
#endif