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