Botan 3.13.0
Crypto and TLS for C&
sym_algo.h
Go to the documentation of this file.
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
15namespace Botan {
16
17class OctetString;
18
19/**
20* Represents the length requirements on an algorithm key
21*/
23 public:
24 /**
25 * Constructor for fixed length keys
26 * @param keylen the supported key length
27 */
28 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 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 bool valid_keylength(size_t length) const {
45 return ((length >= m_min_keylen) && (length <= m_max_keylen) && (length % m_keylen_mod == 0));
46 }
47
48 /**
49 * Return the smallest acceptable key length
50 * @return minimum key length in bytes
51 */
52 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 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 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 */
73 return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod);
74 }
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*/
84 public:
85 /**
86 * Default constructor
87 */
88 SymmetricAlgorithm() = default;
89
90 virtual ~SymmetricAlgorithm() = default;
91
92 /**
93 * Copy constructor
94 */
95 SymmetricAlgorithm(const SymmetricAlgorithm& other) = default;
96
97 /**
98 * Move constructor
99 */
101
102 /**
103 * Copy assignment
104 * @return reference to this
105 */
107
108 /**
109 * Move assignment
110 * @return reference to this
111 */
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 */
125
126 /**
127 * Return the largest acceptable key length
128 * @return maximum allowed key length
129 */
130 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 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 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 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 */
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 void assert_key_material_set(bool predicate) const {
187 if(!predicate) {
188 throw_key_not_set_error();
189 }
190 }
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
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Key_Length_Specification(size_t keylen)
Definition sym_algo.h:28
Key_Length_Specification(size_t min_k, size_t max_k, size_t k_mod=1)
Definition sym_algo.h:36
size_t maximum_keylength() const
Definition sym_algo.h:58
bool valid_keylength(size_t length) const
Definition sym_algo.h:44
size_t keylength_multiple() const
Definition sym_algo.h:64
Key_Length_Specification multiple(size_t n) const
Definition sym_algo.h:72
size_t minimum_keylength() const
Definition sym_algo.h:52
SymmetricAlgorithm & operator=(const SymmetricAlgorithm &other)=default
bool valid_keylength(size_t length) const
Definition sym_algo.h:143
virtual std::string name() const =0
virtual bool has_keying_material() const =0
SymmetricAlgorithm(const SymmetricAlgorithm &other)=default
size_t maximum_keylength() const
Definition sym_algo.h:130
size_t minimum_keylength() const
Definition sym_algo.h:136
SymmetricAlgorithm(SymmetricAlgorithm &&other)=default
void set_key(const uint8_t key[], size_t length)
Definition sym_algo.h:162
void assert_key_material_set() const
Definition sym_algo.h:180
virtual void clear()=0
SymmetricAlgorithm & operator=(SymmetricAlgorithm &&other)=default
virtual ~SymmetricAlgorithm()=default
virtual Key_Length_Specification key_spec() const =0
void assert_key_material_set(bool predicate) const
Definition sym_algo.h:186