Botan 3.11.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 * @param length is a key length in bytes
41 * @return true iff this length is a valid length for this algo
42 */
43 bool valid_keylength(size_t length) const {
44 return ((length >= m_min_keylen) && (length <= m_max_keylen) && (length % m_keylen_mod == 0));
45 }
46
47 /**
48 * @return minimum key length in bytes
49 */
50 size_t minimum_keylength() const { return m_min_keylen; }
51
52 /**
53 * @return maximum key length in bytes
54 */
55 size_t maximum_keylength() const { return m_max_keylen; }
56
57 /**
58 * @return key length multiple in bytes
59 */
60 size_t keylength_multiple() const { return m_keylen_mod; }
61
62 /*
63 * Multiplies all length requirements with the given factor
64 * @param n the multiplication factor
65 * @return a key length specification multiplied by the factor
66 */
68 return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod);
69 }
70
71 private:
72 size_t m_min_keylen, m_max_keylen, m_keylen_mod;
73};
74
75/**
76* This class represents a symmetric algorithm object.
77*/
79 public:
80 SymmetricAlgorithm() = default;
81 virtual ~SymmetricAlgorithm() = default;
82 SymmetricAlgorithm(const SymmetricAlgorithm& other) = default;
86
87 /**
88 * Reset the internal state. This includes not just the key, but
89 * any partial message that may have been in process.
90 */
91 virtual void clear() = 0;
92
93 /**
94 * @return object describing limits on key size
95 */
97
98 /**
99 * @return maximum allowed key length
100 */
101 size_t maximum_keylength() const { return key_spec().maximum_keylength(); }
102
103 /**
104 * @return minimum allowed key length
105 */
106 size_t minimum_keylength() const { return key_spec().minimum_keylength(); }
107
108 /**
109 * Check whether a given key length is valid for this algorithm.
110 * @param length the key length to be checked.
111 * @return true if the key length is valid.
112 */
113 bool valid_keylength(size_t length) const { return key_spec().valid_keylength(length); }
114
115 /**
116 * Set the symmetric key of this object.
117 * @param key the SymmetricKey to be set.
118 */
119 void set_key(const OctetString& key);
120
121 /**
122 * Set the symmetric key of this object.
123 * @param key the contiguous byte range to be set.
124 */
125 void set_key(std::span<const uint8_t> key);
126
127 /**
128 * Set the symmetric key of this object.
129 * @param key the to be set as a byte array.
130 * @param length in bytes of key param
131 */
132 void set_key(const uint8_t key[], size_t length) { set_key(std::span{key, length}); }
133
134 /**
135 * @return the algorithm name
136 */
137 virtual std::string name() const = 0;
138
139 /**
140 * @return true if a key has been set on this object
141 */
142 virtual bool has_keying_material() const = 0;
143
144 protected:
146
147 void assert_key_material_set(bool predicate) const {
148 if(!predicate) {
149 throw_key_not_set_error();
150 }
151 }
152
153 private:
154 void throw_key_not_set_error() const;
155
156 /**
157 * Run the key schedule
158 * @param key the key
159 */
160 virtual void key_schedule(std::span<const uint8_t> key) = 0;
161};
162
163} // namespace Botan
164
165#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:55
bool valid_keylength(size_t length) const
Definition sym_algo.h:43
size_t keylength_multiple() const
Definition sym_algo.h:60
Key_Length_Specification multiple(size_t n) const
Definition sym_algo.h:67
size_t minimum_keylength() const
Definition sym_algo.h:50
SymmetricAlgorithm & operator=(const SymmetricAlgorithm &other)=default
bool valid_keylength(size_t length) const
Definition sym_algo.h:113
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:101
size_t minimum_keylength() const
Definition sym_algo.h:106
SymmetricAlgorithm(SymmetricAlgorithm &&other)=default
void set_key(const uint8_t key[], size_t length)
Definition sym_algo.h:132
void assert_key_material_set() const
Definition sym_algo.h:145
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:147