/src/Botan-3.13.0/build/include/public/botan/kdf.h
Line | Count | Source |
1 | | /* |
2 | | * Key Derivation Function interfaces |
3 | | * (C) 1999-2007 Jack Lloyd |
4 | | * (C) 2024 René Meusel - Rohde & Schwarz Cybersecurity |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_KDF_BASE_H_ |
10 | | #define BOTAN_KDF_BASE_H_ |
11 | | |
12 | | #include <botan/concepts.h> |
13 | | #include <botan/secmem.h> |
14 | | #include <array> |
15 | | #include <memory> |
16 | | #include <span> |
17 | | #include <string> |
18 | | #include <string_view> |
19 | | |
20 | | namespace Botan { |
21 | | |
22 | | /** |
23 | | * Key Derivation Function |
24 | | */ |
25 | | class BOTAN_PUBLIC_API(2, 0) KDF /* NOLINT(*-special-member-functions*) */ { |
26 | | public: |
27 | 20.3k | virtual ~KDF() = default; |
28 | | |
29 | | /** |
30 | | * Create an instance based on a name |
31 | | * If provider is empty then best available is chosen. |
32 | | * @param algo_spec algorithm name |
33 | | * @param provider provider implementation to choose |
34 | | * @return a null pointer if the algo/provider combination cannot be found |
35 | | */ |
36 | | static std::unique_ptr<KDF> create(std::string_view algo_spec, std::string_view provider = ""); |
37 | | |
38 | | /** |
39 | | * Create an instance based on a name, or throw if the |
40 | | * algo/provider combination cannot be found. If provider is |
41 | | * empty then best available is chosen. |
42 | | */ |
43 | | static std::unique_ptr<KDF> create_or_throw(std::string_view algo_spec, std::string_view provider = ""); |
44 | | |
45 | | /** |
46 | | * List the providers available for a given KDF |
47 | | * @return list of available providers for this algorithm, empty if not available |
48 | | */ |
49 | | static std::vector<std::string> providers(std::string_view algo_spec); |
50 | | |
51 | | /** |
52 | | * Return the name of this KDF |
53 | | * @return KDF name |
54 | | */ |
55 | | virtual std::string name() const = 0; |
56 | | |
57 | | /** |
58 | | * Derive a key |
59 | | * @param key buffer holding the derived key, must be of length key_len |
60 | | * @param key_len the desired output length in bytes |
61 | | * @param secret the secret input |
62 | | * @param secret_len size of secret in bytes |
63 | | * @param salt a diversifier |
64 | | * @param salt_len size of salt in bytes |
65 | | * @param label purpose for the derived keying material |
66 | | * @param label_len size of label in bytes |
67 | | */ |
68 | | BOTAN_DEPRECATED("Use KDF::derive_key") |
69 | | void kdf(uint8_t key[], |
70 | | size_t key_len, |
71 | | const uint8_t secret[], |
72 | | size_t secret_len, |
73 | | const uint8_t salt[], |
74 | | size_t salt_len, |
75 | | const uint8_t label[], |
76 | 6.79k | size_t label_len) const { |
77 | 6.79k | derive_key({key, key_len}, {secret, secret_len}, {salt, salt_len}, {label, label_len}); |
78 | 6.79k | } |
79 | | |
80 | | /** |
81 | | * Derive a key |
82 | | * @param key_len the desired output length in bytes |
83 | | * @param secret the secret input |
84 | | * @param secret_len size of secret in bytes |
85 | | * @param salt a diversifier |
86 | | * @param salt_len size of salt in bytes |
87 | | * @param label purpose for the derived keying material |
88 | | * @param label_len size of label in bytes |
89 | | * @return the derived key |
90 | | */ |
91 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
92 | | BOTAN_DEPRECATED("Use std::span or std::string_view overloads") |
93 | | T derive_key(size_t key_len, |
94 | | const uint8_t secret[], |
95 | | size_t secret_len, |
96 | | const uint8_t salt[], |
97 | | size_t salt_len, |
98 | | const uint8_t label[] = nullptr, |
99 | | size_t label_len = 0) const { |
100 | | return derive_key<T>(key_len, {secret, secret_len}, {salt, salt_len}, {label, label_len}); |
101 | | } |
102 | | |
103 | | /** |
104 | | * Derive a key |
105 | | * @param key_len the desired output length in bytes |
106 | | * @param secret the secret input |
107 | | * @param salt a diversifier |
108 | | * @param label purpose for the derived keying material |
109 | | * @return the derived key |
110 | | */ |
111 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
112 | | T derive_key(size_t key_len, |
113 | | std::span<const uint8_t> secret, |
114 | | std::string_view salt = "", |
115 | 0 | std::string_view label = "") const { |
116 | 0 | return derive_key<T>(key_len, secret, _as_span(salt), _as_span(label)); |
117 | 0 | } |
118 | | |
119 | | /** |
120 | | * Derive a key |
121 | | * @param key the output buffer for the to-be-derived key |
122 | | * @param secret the secret input |
123 | | * @param salt a diversifier |
124 | | * @param label purpose for the derived keying material |
125 | | */ |
126 | | void derive_key(std::span<uint8_t> key, |
127 | | std::span<const uint8_t> secret, |
128 | | std::span<const uint8_t> salt, |
129 | 20.3k | std::span<const uint8_t> label) const { |
130 | 20.3k | perform_kdf(key, secret, salt, label); |
131 | 20.3k | } |
132 | | |
133 | | /** |
134 | | * Derive a key |
135 | | * @param key_len the desired output length in bytes |
136 | | * @param secret the secret input |
137 | | * @param salt a diversifier |
138 | | * @param label purpose for the derived keying material |
139 | | * @return the derived key |
140 | | */ |
141 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
142 | | T derive_key(size_t key_len, |
143 | | std::span<const uint8_t> secret, |
144 | | std::span<const uint8_t> salt, |
145 | 0 | std::span<const uint8_t> label) const { |
146 | 0 | T key(key_len); |
147 | 0 | perform_kdf(key, secret, salt, label); |
148 | 0 | return key; |
149 | 0 | } |
150 | | |
151 | | /** |
152 | | * Derive a key |
153 | | * @param key_len the desired output length in bytes |
154 | | * @param secret the secret input |
155 | | * @param salt a diversifier |
156 | | * @param salt_len size of salt in bytes |
157 | | * @param label purpose for the derived keying material |
158 | | * @return the derived key |
159 | | */ |
160 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
161 | | BOTAN_DEPRECATED("Use std::span or std::string_view overloads") |
162 | | T derive_key(size_t key_len, |
163 | | std::span<const uint8_t> secret, |
164 | | const uint8_t salt[], |
165 | | size_t salt_len, |
166 | 0 | std::string_view label = "") const { |
167 | 0 | return derive_key<T>(key_len, secret, {salt, salt_len}, _as_span(label)); |
168 | 0 | } |
169 | | |
170 | | /** |
171 | | * Derive a key |
172 | | * @param key_len the desired output length in bytes |
173 | | * @param secret the secret input |
174 | | * @param secret_len size of secret in bytes |
175 | | * @param salt a diversifier |
176 | | * @param label purpose for the derived keying material |
177 | | * @return the derived key |
178 | | */ |
179 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
180 | | BOTAN_DEPRECATED("Use std::span or std::string_view overloads") |
181 | | T derive_key(size_t key_len, |
182 | | const uint8_t secret[], |
183 | | size_t secret_len, |
184 | | std::string_view salt = "", |
185 | | std::string_view label = "") const { |
186 | | return derive_key<T>(key_len, {secret, secret_len}, _as_span(salt), _as_span(label)); |
187 | | } |
188 | | |
189 | | /** |
190 | | * Derive a key |
191 | | * @tparam key_len the desired output length in bytes |
192 | | * @param secret the secret input |
193 | | * @param salt a diversifier |
194 | | * @param label purpose for the derived keying material |
195 | | * @return the derived key |
196 | | */ |
197 | | template <size_t key_len> |
198 | | std::array<uint8_t, key_len> derive_key(std::span<const uint8_t> secret, |
199 | | std::span<const uint8_t> salt = {}, |
200 | | std::span<const uint8_t> label = {}) { |
201 | | std::array<uint8_t, key_len> key{}; |
202 | | perform_kdf(key, secret, salt, label); |
203 | | return key; |
204 | | } |
205 | | |
206 | | /** |
207 | | * Derive a key |
208 | | * @tparam key_len the desired output length in bytes |
209 | | * @param secret the secret input |
210 | | * @param salt a diversifier |
211 | | * @param label purpose for the derived keying material |
212 | | * @return the derived key |
213 | | */ |
214 | | template <size_t key_len> |
215 | | std::array<uint8_t, key_len> derive_key(std::span<const uint8_t> secret, |
216 | | std::span<const uint8_t> salt = {}, |
217 | | std::string_view label = "") { |
218 | | return derive_key<key_len>(secret, salt, _as_span(label)); |
219 | | } |
220 | | |
221 | | /** |
222 | | * Derive a key |
223 | | * @tparam key_len the desired output length in bytes |
224 | | * @param secret the secret input |
225 | | * @param salt a diversifier |
226 | | * @param label purpose for the derived keying material |
227 | | * @return the derived key |
228 | | */ |
229 | | template <size_t key_len> |
230 | | std::array<uint8_t, key_len> derive_key(std::span<const uint8_t> secret, |
231 | | std::string_view salt = "", |
232 | | std::string_view label = "") { |
233 | | return derive_key<key_len>(secret, _as_span(salt), _as_span(label)); |
234 | | } |
235 | | |
236 | | /** |
237 | | * Create a new uninitialized object of the same type |
238 | | * @return new object representing the same algorithm as *this |
239 | | */ |
240 | | virtual std::unique_ptr<KDF> new_object() const = 0; |
241 | | |
242 | | /** |
243 | | * Create a new uninitialized object of the same type |
244 | | * @return new object representing the same algorithm as *this |
245 | | */ |
246 | 0 | KDF* clone() const { return this->new_object().release(); } |
247 | | |
248 | | protected: |
249 | | /** |
250 | | * Internal customization point for subclasses |
251 | | * |
252 | | * The byte size of the @p key span is the number of bytes to be produced |
253 | | * by the concrete key derivation function. |
254 | | * |
255 | | * @param key the output buffer for the to-be-derived key |
256 | | * @param secret the secret input |
257 | | * @param salt a diversifier |
258 | | * @param label purpose for the derived keying material |
259 | | */ |
260 | | virtual void perform_kdf(std::span<uint8_t> key, |
261 | | std::span<const uint8_t> secret, |
262 | | std::span<const uint8_t> salt, |
263 | | std::span<const uint8_t> label) const = 0; |
264 | | |
265 | | private: |
266 | | static std::span<const uint8_t> _as_span(std::string_view s); |
267 | | }; |
268 | | |
269 | | /** |
270 | | * Factory method for KDF (key derivation function) |
271 | | * @param algo_spec the name of the KDF to create |
272 | | * @return pointer to newly allocated object of that type |
273 | | * |
274 | | * Prefer KDF::create |
275 | | */ |
276 | | BOTAN_DEPRECATED("Use KDF::create") |
277 | | |
278 | 0 | inline KDF* get_kdf(std::string_view algo_spec) { |
279 | 0 | if(algo_spec == "Raw") { |
280 | 0 | return nullptr; |
281 | 0 | } |
282 | 0 |
|
283 | 0 | return KDF::create_or_throw(algo_spec).release(); |
284 | 0 | } |
285 | | |
286 | | } // namespace Botan |
287 | | |
288 | | #endif |