Botan 3.8.1
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 ? 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 virtual ~SymmetricAlgorithm() = default;
82
83 /**
84 * Reset the internal state. This includes not just the key, but
85 * any partial message that may have been in process.
86 */
87 virtual void clear() = 0;
88
89 /**
90 * @return object describing limits on key size
91 */
93
94 /**
95 * @return maximum allowed key length
96 */
97 size_t maximum_keylength() const { return key_spec().maximum_keylength(); }
98
99 /**
100 * @return minimum allowed key length
101 */
102 size_t minimum_keylength() const { return key_spec().minimum_keylength(); }
103
104 /**
105 * Check whether a given key length is valid for this algorithm.
106 * @param length the key length to be checked.
107 * @return true if the key length is valid.
108 */
109 bool valid_keylength(size_t length) const { return key_spec().valid_keylength(length); }
110
111 /**
112 * Set the symmetric key of this object.
113 * @param key the SymmetricKey to be set.
114 */
115 void set_key(const OctetString& key);
116
117 /**
118 * Set the symmetric key of this object.
119 * @param key the contiguous byte range to be set.
120 */
121 void set_key(std::span<const uint8_t> key);
122
123 /**
124 * Set the symmetric key of this object.
125 * @param key the to be set as a byte array.
126 * @param length in bytes of key param
127 */
128 void set_key(const uint8_t key[], size_t length) { set_key(std::span{key, length}); }
129
130 /**
131 * @return the algorithm name
132 */
133 virtual std::string name() const = 0;
134
135 /**
136 * @return true if a key has been set on this object
137 */
138 virtual bool has_keying_material() const = 0;
139
140 protected:
142
143 void assert_key_material_set(bool predicate) const {
144 if(!predicate) {
145 throw_key_not_set_error();
146 }
147 }
148
149 private:
150 void throw_key_not_set_error() const;
151
152 /**
153 * Run the key schedule
154 * @param key the key
155 */
156 virtual void key_schedule(std::span<const uint8_t> key) = 0;
157};
158
159} // namespace Botan
160
161#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:19
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
bool valid_keylength(size_t length) const
Definition sym_algo.h:109
virtual std::string name() const =0
virtual bool has_keying_material() const =0
size_t maximum_keylength() const
Definition sym_algo.h:97
size_t minimum_keylength() const
Definition sym_algo.h:102
void set_key(const uint8_t key[], size_t length)
Definition sym_algo.h:128
void assert_key_material_set() const
Definition sym_algo.h:141
virtual void clear()=0
virtual ~SymmetricAlgorithm()=default
virtual Key_Length_Specification key_spec() const =0
void assert_key_material_set(bool predicate) const
Definition sym_algo.h:143