/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/types.h> |
12 | | |
13 | | #include <span> |
14 | | #include <string> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | class OctetString; |
19 | | |
20 | | /** |
21 | | * Represents the length requirements on an algorithm key |
22 | | */ |
23 | | class BOTAN_PUBLIC_API(2, 0) Key_Length_Specification final { |
24 | | public: |
25 | | /** |
26 | | * Constructor for fixed length keys |
27 | | * @param keylen the supported key length |
28 | | */ |
29 | 711 | 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 | 21.8k | 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 | 22.5k | bool valid_keylength(size_t length) const { |
45 | 22.5k | return ((length >= m_min_keylen) && (length <= m_max_keylen) && (length % m_keylen_mod == 0)); |
46 | 22.5k | } |
47 | | |
48 | | /** |
49 | | * @return minimum key length in bytes |
50 | | */ |
51 | 0 | size_t minimum_keylength() const { return m_min_keylen; } |
52 | | |
53 | | /** |
54 | | * @return maximum key length in bytes |
55 | | */ |
56 | 0 | size_t maximum_keylength() const { return m_max_keylen; } |
57 | | |
58 | | /** |
59 | | * @return key length multiple in bytes |
60 | | */ |
61 | 0 | 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 | | */ |
68 | 0 | Key_Length_Specification multiple(size_t n) const { |
69 | 0 | return Key_Length_Specification(n * m_min_keylen, n * m_max_keylen, n * m_keylen_mod); |
70 | 0 | } |
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 | | */ |
79 | | class BOTAN_PUBLIC_API(2, 0) SymmetricAlgorithm { |
80 | | public: |
81 | 14.0k | 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 | | */ |
92 | | virtual Key_Length_Specification key_spec() const = 0; |
93 | | |
94 | | /** |
95 | | * @return maximum allowed key length |
96 | | */ |
97 | 0 | size_t maximum_keylength() const { return key_spec().maximum_keylength(); } |
98 | | |
99 | | /** |
100 | | * @return minimum allowed key length |
101 | | */ |
102 | 0 | 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 | 22.4k | 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 | 3.12k | 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: |
141 | 350k | void assert_key_material_set() const { assert_key_material_set(has_keying_material()); } |
142 | | |
143 | 350k | void assert_key_material_set(bool predicate) const { |
144 | 350k | if(!predicate) { |
145 | 0 | throw_key_not_set_error(); |
146 | 0 | } |
147 | 350k | } |
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 |