/src/botan/build/include/public/botan/sym_algo.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
16 | | namespace Botan { |
17 | | |
18 | | /** |
19 | | * Represents the length requirements on an algorithm key |
20 | | */ |
21 | | class BOTAN_PUBLIC_API(2, 0) Key_Length_Specification final { |
22 | | public: |
23 | | /** |
24 | | * Constructor for fixed length keys |
25 | | * @param keylen the supported key length |
26 | | */ |
27 | 0 | 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 | 1.27k | 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 | 1.27k | bool valid_keylength(size_t length) const { |
43 | 1.27k | return ((length >= m_min_keylen) && (length <= m_max_keylen) && (length % m_keylen_mod == 0)); |
44 | 1.27k | } |
45 | | |
46 | | /** |
47 | | * @return minimum key length in bytes |
48 | | */ |
49 | 0 | size_t minimum_keylength() const { return m_min_keylen; } |
50 | | |
51 | | /** |
52 | | * @return maximum key length in bytes |
53 | | */ |
54 | 0 | size_t maximum_keylength() const { return m_max_keylen; } |
55 | | |
56 | | /** |
57 | | * @return key length multiple in bytes |
58 | | */ |
59 | 0 | 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 | | */ |
66 | 0 | Key_Length_Specification multiple(size_t n) const { |
67 | 0 | return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod); |
68 | 0 | } |
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 | | */ |
77 | | class BOTAN_PUBLIC_API(2, 0) SymmetricAlgorithm { |
78 | | public: |
79 | 746 | 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 | | */ |
90 | | virtual Key_Length_Specification key_spec() const = 0; |
91 | | |
92 | | /** |
93 | | * @return maximum allowed key length |
94 | | */ |
95 | 0 | size_t maximum_keylength() const { return key_spec().maximum_keylength(); } |
96 | | |
97 | | /** |
98 | | * @return minimum allowed key length |
99 | | */ |
100 | 0 | 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 | 1.27k | 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 | 0 | 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 | 653 | 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 | 28.7k | void assert_key_material_set() const { assert_key_material_set(has_keying_material()); } |
140 | | |
141 | 28.7k | void assert_key_material_set(bool predicate) const { |
142 | 28.7k | if(!predicate) { |
143 | 0 | throw_key_not_set_error(); |
144 | 0 | } |
145 | 28.7k | } |
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 |