Botan 3.9.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
13#include <span>
14#include <string>
15
16namespace Botan {
17
18class OctetString;
19
20/**
21* Represents the length requirements on an algorithm key
22*/
24 public:
25 /**
26 * Constructor for fixed length keys
27 * @param keylen the supported key length
28 */
29 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 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 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 minimum key length in bytes
50 */
51 size_t minimum_keylength() const { return m_min_keylen; }
52
53 /**
54 * @return maximum key length in bytes
55 */
56 size_t maximum_keylength() const { return m_max_keylen; }
57
58 /**
59 * @return key length multiple in bytes
60 */
61 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 */
69 return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod);
70 }
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*/
80 public:
81 SymmetricAlgorithm() = default;
82 virtual ~SymmetricAlgorithm() = default;
83 SymmetricAlgorithm(const 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 */
98
99 /**
100 * @return maximum allowed key length
101 */
102 size_t maximum_keylength() const { return key_spec().maximum_keylength(); }
103
104 /**
105 * @return minimum allowed key length
106 */
107 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 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 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:
147
148 void assert_key_material_set(bool predicate) const {
149 if(!predicate) {
150 throw_key_not_set_error();
151 }
152 }
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
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Key_Length_Specification(size_t keylen)
Definition sym_algo.h:29
Key_Length_Specification(size_t min_k, size_t max_k, size_t k_mod=1)
Definition sym_algo.h:37
size_t maximum_keylength() const
Definition sym_algo.h:56
bool valid_keylength(size_t length) const
Definition sym_algo.h:44
size_t keylength_multiple() const
Definition sym_algo.h:61
Key_Length_Specification multiple(size_t n) const
Definition sym_algo.h:68
size_t minimum_keylength() const
Definition sym_algo.h:51
SymmetricAlgorithm & operator=(const SymmetricAlgorithm &other)=default
bool valid_keylength(size_t length) const
Definition sym_algo.h:114
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:102
size_t minimum_keylength() const
Definition sym_algo.h:107
SymmetricAlgorithm(SymmetricAlgorithm &&other)=default
void set_key(const uint8_t key[], size_t length)
Definition sym_algo.h:133
void assert_key_material_set() const
Definition sym_algo.h:146
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:148