/src/Botan-3.13.0/build/include/public/botan/pubkey.h
Line | Count | Source |
1 | | /* |
2 | | * Public Key Interface |
3 | | * (C) 1999-2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_PUBKEY_H_ |
9 | | #define BOTAN_PUBKEY_H_ |
10 | | |
11 | | #include <botan/pk_keys.h> |
12 | | #include <botan/pk_ops_fwd.h> |
13 | | #include <botan/symkey.h> |
14 | | #include <span> |
15 | | #include <string> |
16 | | #include <string_view> |
17 | | #include <utility> |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | | class RandomNumberGenerator; |
22 | | |
23 | | /** |
24 | | * Public Key Encryptor |
25 | | * This is the primary interface for public key encryption |
26 | | */ |
27 | | class BOTAN_PUBLIC_API(2, 0) PK_Encryptor { |
28 | | public: |
29 | | /** |
30 | | * Encrypt a message. |
31 | | * @param in the message as a byte array |
32 | | * @param length the length of the above byte array |
33 | | * @param rng the random number source to use |
34 | | * @return encrypted message |
35 | | */ |
36 | 41 | std::vector<uint8_t> encrypt(const uint8_t in[], size_t length, RandomNumberGenerator& rng) const { |
37 | 41 | return enc(in, length, rng); |
38 | 41 | } |
39 | | |
40 | | /** |
41 | | * Encrypt a message. |
42 | | * @param in the message |
43 | | * @param rng the random number source to use |
44 | | * @return encrypted message |
45 | | */ |
46 | 0 | std::vector<uint8_t> encrypt(std::span<const uint8_t> in, RandomNumberGenerator& rng) const { |
47 | 0 | return enc(in.data(), in.size(), rng); |
48 | 0 | } |
49 | | |
50 | | /** |
51 | | * Return the maximum allowed message size in bytes. |
52 | | * @return maximum message size in bytes |
53 | | */ |
54 | | virtual size_t maximum_input_size() const = 0; |
55 | | |
56 | | /** |
57 | | * Return an upper bound on the ciphertext length |
58 | | */ |
59 | | virtual size_t ciphertext_length(size_t ptext_len) const = 0; |
60 | | |
61 | 41 | PK_Encryptor() = default; |
62 | 41 | virtual ~PK_Encryptor() = default; |
63 | | |
64 | | PK_Encryptor(const PK_Encryptor&) = delete; |
65 | | PK_Encryptor& operator=(const PK_Encryptor&) = delete; |
66 | | |
67 | 0 | PK_Encryptor(PK_Encryptor&&) noexcept = default; |
68 | 0 | PK_Encryptor& operator=(PK_Encryptor&&) noexcept = default; |
69 | | |
70 | | private: |
71 | | virtual std::vector<uint8_t> enc(const uint8_t[], size_t, RandomNumberGenerator&) const = 0; |
72 | | }; |
73 | | |
74 | | /** |
75 | | * Public Key Decryptor |
76 | | */ |
77 | | class BOTAN_PUBLIC_API(2, 0) PK_Decryptor { |
78 | | public: |
79 | | /** |
80 | | * Decrypt a ciphertext, throwing an exception if the input |
81 | | * seems to be invalid (eg due to an accidental or malicious |
82 | | * error in the ciphertext). |
83 | | * |
84 | | * @param in the ciphertext as a byte array |
85 | | * @param length the length of the above byte array |
86 | | * @return decrypted message |
87 | | */ |
88 | | secure_vector<uint8_t> decrypt(const uint8_t in[], size_t length) const; |
89 | | |
90 | | /** |
91 | | * Same as above, but taking a vector |
92 | | * @param in the ciphertext |
93 | | * @return decrypted message |
94 | | */ |
95 | 0 | secure_vector<uint8_t> decrypt(std::span<const uint8_t> in) const { return decrypt(in.data(), in.size()); } |
96 | | |
97 | | /** |
98 | | * Decrypt a ciphertext. If the ciphertext is invalid (eg due to |
99 | | * invalid padding) or is not the expected length, instead |
100 | | * returns a random string of the expected length. Use to avoid |
101 | | * oracle attacks, especially against PKCS #1 v1.5 decryption. |
102 | | */ |
103 | | secure_vector<uint8_t> decrypt_or_random(const uint8_t in[], |
104 | | size_t length, |
105 | | size_t expected_pt_len, |
106 | | RandomNumberGenerator& rng) const; |
107 | | |
108 | | /** |
109 | | * Decrypt a ciphertext. If the ciphertext is invalid (eg due to |
110 | | * invalid padding) or is not the expected length, instead |
111 | | * returns a random string of the expected length. Use to avoid |
112 | | * oracle attacks, especially against PKCS #1 v1.5 decryption. |
113 | | * |
114 | | * Additionally checks (also in const time) that: |
115 | | * contents[required_content_offsets[i]] == required_content_bytes[i] |
116 | | * for 0 <= i < required_contents |
117 | | * |
118 | | * Used for example in TLS, which encodes the client version in |
119 | | * the content bytes: if there is any timing variation the version |
120 | | * check can be used as an oracle to recover the key. |
121 | | */ |
122 | | secure_vector<uint8_t> decrypt_or_random(const uint8_t in[], |
123 | | size_t length, |
124 | | size_t expected_pt_len, |
125 | | RandomNumberGenerator& rng, |
126 | | const uint8_t required_content_bytes[], |
127 | | const uint8_t required_content_offsets[], |
128 | | size_t required_contents) const; |
129 | | |
130 | | /** |
131 | | * Return an upper bound on the plaintext length for a particular |
132 | | * ciphertext input length |
133 | | */ |
134 | | virtual size_t plaintext_length(size_t ctext_len) const = 0; |
135 | | |
136 | | /** |
137 | | * Return an upper bound on the ciphertext length for a particular |
138 | | * plaintext input length. |
139 | | */ |
140 | | virtual size_t ciphertext_length(size_t ptext_len) const = 0; |
141 | | |
142 | 0 | PK_Decryptor() = default; |
143 | 0 | virtual ~PK_Decryptor() = default; |
144 | | |
145 | | PK_Decryptor(const PK_Decryptor&) = delete; |
146 | | PK_Decryptor& operator=(const PK_Decryptor&) = delete; |
147 | | |
148 | 0 | PK_Decryptor(PK_Decryptor&&) noexcept = default; |
149 | 0 | PK_Decryptor& operator=(PK_Decryptor&&) noexcept = default; |
150 | | |
151 | | private: |
152 | | virtual secure_vector<uint8_t> do_decrypt(uint8_t& valid_mask, const uint8_t in[], size_t in_len) const = 0; |
153 | | }; |
154 | | |
155 | | /** |
156 | | * Public Key Signer. Use the sign_message() functions for small |
157 | | * messages. Use multiple calls update() to process large messages and |
158 | | * generate the signature by finally calling signature(). |
159 | | */ |
160 | | class BOTAN_PUBLIC_API(2, 0) PK_Signer final { |
161 | | public: |
162 | | /** |
163 | | * Construct a PK Signer. |
164 | | * @param key the key to use inside this signer |
165 | | * @param rng the random generator to use |
166 | | * @param padding the padding/hash to use, eg "SHA-512" or "PSS(SHA-256)" |
167 | | * @param format the signature format to use |
168 | | * @param provider the provider to use |
169 | | */ |
170 | | PK_Signer(const Private_Key& key, |
171 | | RandomNumberGenerator& rng, |
172 | | std::string_view padding, |
173 | | Signature_Format format = Signature_Format::Standard, |
174 | | std::string_view provider = ""); |
175 | | |
176 | | ~PK_Signer(); |
177 | | |
178 | | PK_Signer(const PK_Signer&) = delete; |
179 | | PK_Signer& operator=(const PK_Signer&) = delete; |
180 | | |
181 | | PK_Signer(PK_Signer&&) noexcept; |
182 | | PK_Signer& operator=(PK_Signer&&) noexcept; |
183 | | |
184 | | /** |
185 | | * Sign a message all in one go |
186 | | * @param in the message to sign as a byte array |
187 | | * @param length the length of the above byte array |
188 | | * @param rng the rng to use |
189 | | * @return signature |
190 | | */ |
191 | 0 | std::vector<uint8_t> sign_message(const uint8_t in[], size_t length, RandomNumberGenerator& rng) { |
192 | 0 | this->update(in, length); |
193 | 0 | return this->signature(rng); |
194 | 0 | } |
195 | | |
196 | | /** |
197 | | * Sign a message. |
198 | | * @param in the message to sign |
199 | | * @param rng the rng to use |
200 | | * @return signature |
201 | | */ |
202 | 0 | std::vector<uint8_t> sign_message(std::span<const uint8_t> in, RandomNumberGenerator& rng) { |
203 | 0 | return sign_message(in.data(), in.size(), rng); |
204 | 0 | } |
205 | | |
206 | | /** |
207 | | * Add a message part (single byte). |
208 | | * @param in the byte to add |
209 | | */ |
210 | 0 | void update(uint8_t in) { update(&in, 1); } |
211 | | |
212 | | /** |
213 | | * Add a message part. |
214 | | * @param in the message part to add as a byte array |
215 | | * @param length the length of the above byte array |
216 | | */ |
217 | | void update(const uint8_t in[], size_t length); |
218 | | |
219 | | /** |
220 | | * Add a message part. |
221 | | * @param in the message part to add |
222 | | */ |
223 | 0 | void update(std::span<const uint8_t> in) { update(in.data(), in.size()); } |
224 | | |
225 | | /** |
226 | | * Add a message part. |
227 | | * @param in the message part to add |
228 | | */ |
229 | | void update(std::string_view in); |
230 | | |
231 | | /** |
232 | | * Get the signature of the so far processed message (provided by the |
233 | | * calls to update()). |
234 | | * @param rng the rng to use |
235 | | * @return signature of the total message |
236 | | */ |
237 | | std::vector<uint8_t> signature(RandomNumberGenerator& rng); |
238 | | |
239 | | /** |
240 | | * Set the output format of the signature. |
241 | | * @param format the signature format to use |
242 | | */ |
243 | 0 | void set_output_format(Signature_Format format) { m_sig_format = format; } |
244 | | |
245 | | /** |
246 | | * Return an upper bound on the length of the signatures this |
247 | | * PK_Signer will produce |
248 | | */ |
249 | | size_t signature_length() const; |
250 | | |
251 | | /** |
252 | | * Return an AlgorithmIdentifier appropriate for identifying the signature |
253 | | * method being generated by this PK_Signer. Throws an exception if this |
254 | | * is not available for the current signature scheme. |
255 | | */ |
256 | | AlgorithmIdentifier algorithm_identifier() const; |
257 | | |
258 | | /** |
259 | | * Return the hash function which is being used to create signatures. |
260 | | * This should never return an empty string however it may return a string |
261 | | * which does not map directly to a hash function, in particular if "Raw" |
262 | | * (unhashed) encoding is being used. |
263 | | */ |
264 | | std::string hash_function() const; |
265 | | |
266 | | private: |
267 | | std::unique_ptr<PK_Ops::Signature> m_op; |
268 | | Signature_Format m_sig_format; |
269 | | std::optional<size_t> m_sig_element_size; |
270 | | }; |
271 | | |
272 | | /** |
273 | | * Public Key Verifier. Use the verify_message() functions for small |
274 | | * messages. Use multiple calls update() to process large messages and |
275 | | * verify the signature by finally calling check_signature(). |
276 | | */ |
277 | | class BOTAN_PUBLIC_API(2, 0) PK_Verifier final { |
278 | | public: |
279 | | /** |
280 | | * Construct a PK Verifier. |
281 | | * @param pub_key the public key to verify against |
282 | | * @param padding the padding/hash to use (eg "SHA-512" or "PSS(SHA-256)") |
283 | | * @param format the signature format to use |
284 | | * @param provider the provider to use |
285 | | */ |
286 | | PK_Verifier(const Public_Key& pub_key, |
287 | | std::string_view padding, |
288 | | Signature_Format format = Signature_Format::Standard, |
289 | | std::string_view provider = ""); |
290 | | |
291 | | /** |
292 | | * Construct a PK Verifier (X.509 specific) |
293 | | * |
294 | | * This constructor will attempt to decode signature_format relative |
295 | | * to the public key provided. If they seem to be inconsistent or |
296 | | * otherwise unsupported, a Decoding_Error is thrown. |
297 | | * |
298 | | * @param pub_key the public key to verify against |
299 | | * @param signature_algorithm the supposed signature algorithm |
300 | | * @param provider the provider to use |
301 | | */ |
302 | | PK_Verifier(const Public_Key& pub_key, |
303 | | const AlgorithmIdentifier& signature_algorithm, |
304 | | std::string_view provider = ""); |
305 | | |
306 | | ~PK_Verifier(); |
307 | | |
308 | | PK_Verifier(const PK_Verifier&) = delete; |
309 | | PK_Verifier& operator=(const PK_Verifier&) = delete; |
310 | | |
311 | | PK_Verifier(PK_Verifier&&) noexcept; |
312 | | PK_Verifier& operator=(PK_Verifier&&) noexcept; |
313 | | |
314 | | /** |
315 | | * Verify a signature. |
316 | | * @param msg the message that the signature belongs to, as a byte array |
317 | | * @param msg_length the length of the above byte array msg |
318 | | * @param sig the signature as a byte array |
319 | | * @param sig_length the length of the above byte array sig |
320 | | * @return true if the signature is valid |
321 | | */ |
322 | | bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length); |
323 | | |
324 | | /** |
325 | | * Verify a signature. |
326 | | * @param msg the message that the signature belongs to |
327 | | * @param sig the signature |
328 | | * @return true if the signature is valid |
329 | | */ |
330 | 0 | bool verify_message(std::span<const uint8_t> msg, std::span<const uint8_t> sig) { |
331 | 0 | return verify_message(msg.data(), msg.size(), sig.data(), sig.size()); |
332 | 0 | } |
333 | | |
334 | | /** |
335 | | * Add a message part (single byte) of the message corresponding to the |
336 | | * signature to be verified. |
337 | | * @param in the byte to add |
338 | | */ |
339 | 0 | void update(uint8_t in) { update(&in, 1); } |
340 | | |
341 | | /** |
342 | | * Add a message part of the message corresponding to the |
343 | | * signature to be verified. |
344 | | * @param msg_part the new message part as a byte array |
345 | | * @param length the length of the above byte array |
346 | | */ |
347 | | void update(const uint8_t msg_part[], size_t length); |
348 | | |
349 | | /** |
350 | | * Add a message part of the message corresponding to the |
351 | | * signature to be verified. |
352 | | * @param in the new message part |
353 | | */ |
354 | 0 | void update(std::span<const uint8_t> in) { update(in.data(), in.size()); } |
355 | | |
356 | | /** |
357 | | * Add a message part of the message corresponding to the |
358 | | * signature to be verified. |
359 | | */ |
360 | | void update(std::string_view in); |
361 | | |
362 | | /** |
363 | | * Check the signature of the buffered message, i.e. the one build |
364 | | * by successive calls to update. |
365 | | * @param sig the signature to be verified as a byte array |
366 | | * @param length the length of the above byte array |
367 | | * @return true if the signature is valid, false otherwise |
368 | | */ |
369 | | bool check_signature(const uint8_t sig[], size_t length); |
370 | | |
371 | | /** |
372 | | * Check the signature of the buffered message, i.e. the one build |
373 | | * by successive calls to update. |
374 | | * @param sig the signature to be verified |
375 | | * @return true if the signature is valid, false otherwise |
376 | | */ |
377 | 0 | bool check_signature(std::span<const uint8_t> sig) { return check_signature(sig.data(), sig.size()); } |
378 | | |
379 | | /** |
380 | | * Set the format of the signatures fed to this verifier. |
381 | | * @param format the signature format to use |
382 | | */ |
383 | | BOTAN_DEPRECATED("Provide Signature_Format to the constructor") void set_input_format(Signature_Format format); |
384 | | |
385 | | /** |
386 | | * Return the hash function which is being used to verify signatures. |
387 | | * This should never return an empty string however it may return a string |
388 | | * which does not map directly to a hash function, in particular if "Raw" |
389 | | * (unhashed) encoding is being used. |
390 | | */ |
391 | | std::string hash_function() const; |
392 | | |
393 | | private: |
394 | | std::unique_ptr<PK_Ops::Verification> m_op; |
395 | | Signature_Format m_sig_format; |
396 | | std::optional<size_t> m_sig_element_size; |
397 | | }; |
398 | | |
399 | | /** |
400 | | * Object used for key agreement |
401 | | */ |
402 | | class BOTAN_PUBLIC_API(2, 0) PK_Key_Agreement final { |
403 | | public: |
404 | | /** |
405 | | * Construct a PK Key Agreement. |
406 | | * @param key the key to use |
407 | | * @param rng the random generator to use |
408 | | * @param kdf name of the KDF to use (or 'Raw' for no KDF) |
409 | | * @param provider the algo provider to use (or empty for default) |
410 | | */ |
411 | | PK_Key_Agreement(const Private_Key& key, |
412 | | RandomNumberGenerator& rng, |
413 | | std::string_view kdf, |
414 | | std::string_view provider = ""); |
415 | | |
416 | | ~PK_Key_Agreement(); |
417 | | |
418 | | PK_Key_Agreement(const PK_Key_Agreement&) = delete; |
419 | | PK_Key_Agreement& operator=(const PK_Key_Agreement&) = delete; |
420 | | |
421 | | PK_Key_Agreement(PK_Key_Agreement&&) noexcept; |
422 | | PK_Key_Agreement& operator=(PK_Key_Agreement&&) noexcept; |
423 | | |
424 | | /** |
425 | | * Perform Key Agreement Operation |
426 | | * @param key_len the desired key output size (ignored if "Raw" KDF is used) |
427 | | * @param peer_key the other parties key |
428 | | * @param salt extra derivation salt |
429 | | */ |
430 | | SymmetricKey derive_key(size_t key_len, std::span<const uint8_t> peer_key, std::span<const uint8_t> salt) const; |
431 | | |
432 | | /** |
433 | | * Perform Key Agreement Operation |
434 | | * @param key_len the desired key output size (ignored if "Raw" KDF is used) |
435 | | * @param peer_key the other parties key |
436 | | * @param peer_key_len the length of peer_key in bytes |
437 | | * @param salt extra derivation salt |
438 | | * @param salt_len the length of salt in bytes |
439 | | */ |
440 | | SymmetricKey derive_key( |
441 | 0 | size_t key_len, const uint8_t peer_key[], size_t peer_key_len, const uint8_t salt[], size_t salt_len) const { |
442 | 0 | return this->derive_key(key_len, {peer_key, peer_key_len}, {salt, salt_len}); |
443 | 0 | } |
444 | | |
445 | | /** |
446 | | * Perform Key Agreement Operation |
447 | | * @param key_len the desired key output size (ignored if "Raw" KDF is used) |
448 | | * @param peer_key the other parties key |
449 | | * @param salt extra derivation salt |
450 | | * @param salt_len the length of salt in bytes |
451 | | */ |
452 | | SymmetricKey derive_key(size_t key_len, |
453 | | std::span<const uint8_t> peer_key, |
454 | | const uint8_t salt[], |
455 | 0 | size_t salt_len) const { |
456 | 0 | return derive_key(key_len, peer_key.data(), peer_key.size(), salt, salt_len); |
457 | 0 | } |
458 | | |
459 | | /** |
460 | | * Perform Key Agreement Operation |
461 | | * @param key_len the desired key output size (ignored if "Raw" KDF is used) |
462 | | * @param peer_key the other parties key |
463 | | * @param peer_key_len the length of peer_key in bytes |
464 | | * @param salt extra derivation info |
465 | | */ |
466 | | SymmetricKey derive_key(size_t key_len, |
467 | | const uint8_t peer_key[], |
468 | | size_t peer_key_len, |
469 | | std::string_view salt = "") const; |
470 | | |
471 | | /** |
472 | | * Perform Key Agreement Operation |
473 | | * @param key_len the desired key output size (ignored if "Raw" KDF is used) |
474 | | * @param peer_key the other parties key |
475 | | * @param salt extra derivation info |
476 | | */ |
477 | | SymmetricKey derive_key(size_t key_len, std::span<const uint8_t> peer_key, std::string_view salt = "") const; |
478 | | |
479 | | /** |
480 | | * Return the underlying size of the value that is agreed. |
481 | | * If derive_key is called with a length of 0 with a "Raw" |
482 | | * KDF, it will return a value of this size. |
483 | | */ |
484 | | size_t agreed_value_size() const; |
485 | | |
486 | | private: |
487 | | std::unique_ptr<PK_Ops::Key_Agreement> m_op; |
488 | | }; |
489 | | |
490 | | /** |
491 | | * Encryption using a standard message recovery algorithm like RSA or |
492 | | * ElGamal, paired with an encoding scheme like OAEP. |
493 | | */ |
494 | | class BOTAN_PUBLIC_API(2, 0) PK_Encryptor_EME final : public PK_Encryptor { |
495 | | public: |
496 | | size_t maximum_input_size() const override; |
497 | | |
498 | | /** |
499 | | * Construct an instance. |
500 | | * @param key the key to use inside the encryptor |
501 | | * @param rng the RNG to use |
502 | | * @param padding the message encoding scheme to use (eg "OAEP(SHA-256)") |
503 | | * @param provider the provider to use |
504 | | */ |
505 | | PK_Encryptor_EME(const Public_Key& key, |
506 | | RandomNumberGenerator& rng, |
507 | | std::string_view padding, |
508 | | std::string_view provider = ""); |
509 | | |
510 | | ~PK_Encryptor_EME() override; |
511 | | |
512 | | PK_Encryptor_EME(const PK_Encryptor_EME&) = delete; |
513 | | PK_Encryptor_EME& operator=(const PK_Encryptor_EME&) = delete; |
514 | | |
515 | | PK_Encryptor_EME(PK_Encryptor_EME&&) noexcept; |
516 | | PK_Encryptor_EME& operator=(PK_Encryptor_EME&&) noexcept; |
517 | | |
518 | | /** |
519 | | * Return an upper bound on the ciphertext length for a particular |
520 | | * plaintext input length |
521 | | */ |
522 | | size_t ciphertext_length(size_t ptext_len) const override; |
523 | | |
524 | | private: |
525 | | std::vector<uint8_t> enc(const uint8_t ptext[], size_t len, RandomNumberGenerator& rng) const override; |
526 | | |
527 | | std::unique_ptr<PK_Ops::Encryption> m_op; |
528 | | }; |
529 | | |
530 | | /** |
531 | | * Decryption with a padding scheme. |
532 | | * |
533 | | * This is typically only used with RSA |
534 | | */ |
535 | | class BOTAN_PUBLIC_API(2, 0) PK_Decryptor_EME final : public PK_Decryptor { |
536 | | public: |
537 | | /** |
538 | | * Construct an instance. |
539 | | * @param key the key to use inside the decryptor |
540 | | * @param rng the random generator to use |
541 | | * @param padding the padding scheme to use |
542 | | * @param provider the provider to use |
543 | | */ |
544 | | PK_Decryptor_EME(const Private_Key& key, |
545 | | RandomNumberGenerator& rng, |
546 | | std::string_view padding, |
547 | | std::string_view provider = ""); |
548 | | |
549 | | size_t plaintext_length(size_t ctext_len) const override; |
550 | | |
551 | | size_t ciphertext_length(size_t ptext_len) const override; |
552 | | |
553 | | ~PK_Decryptor_EME() override; |
554 | | |
555 | | PK_Decryptor_EME(const PK_Decryptor_EME&) = delete; |
556 | | PK_Decryptor_EME& operator=(const PK_Decryptor_EME&) = delete; |
557 | | |
558 | | PK_Decryptor_EME(PK_Decryptor_EME&&) noexcept; |
559 | | PK_Decryptor_EME& operator=(PK_Decryptor_EME&&) noexcept; |
560 | | |
561 | | private: |
562 | | secure_vector<uint8_t> do_decrypt(uint8_t& valid_mask, const uint8_t in[], size_t in_len) const override; |
563 | | |
564 | | std::unique_ptr<PK_Ops::Decryption> m_op; |
565 | | }; |
566 | | |
567 | | /** |
568 | | * Result of a key encapsulation operation. |
569 | | */ |
570 | | class KEM_Encapsulation final { |
571 | | public: |
572 | | KEM_Encapsulation(std::vector<uint8_t> encapsulated_shared_key, secure_vector<uint8_t> shared_key) : |
573 | 0 | m_encapsulated_shared_key(std::move(encapsulated_shared_key)), m_shared_key(std::move(shared_key)) {} |
574 | | |
575 | | /** |
576 | | * @returns the encapsulated shared secret (encrypted with the public key) |
577 | | */ |
578 | 0 | const std::vector<uint8_t>& encapsulated_shared_key() const { return m_encapsulated_shared_key; } |
579 | | |
580 | | /** |
581 | | * @returns the plaintext shared secret |
582 | | */ |
583 | 0 | const secure_vector<uint8_t>& shared_key() const { return m_shared_key; } |
584 | | |
585 | | /** |
586 | | * @returns the pair (encapsulated key, key) extracted from @p kem |
587 | | */ |
588 | | static std::pair<std::vector<uint8_t>, secure_vector<uint8_t>> destructure( |
589 | 0 | KEM_Encapsulation&& kem) /* NOLINT(*param-not-moved*) */ { |
590 | 0 | return std::make_pair(std::exchange(kem.m_encapsulated_shared_key, {}), std::exchange(kem.m_shared_key, {})); |
591 | 0 | } |
592 | | |
593 | | private: |
594 | | friend class PK_KEM_Encryptor; |
595 | | |
596 | | KEM_Encapsulation(size_t encapsulated_size, size_t shared_key_size) : |
597 | 0 | m_encapsulated_shared_key(encapsulated_size), m_shared_key(shared_key_size) {} |
598 | | |
599 | | private: |
600 | | std::vector<uint8_t> m_encapsulated_shared_key; |
601 | | secure_vector<uint8_t> m_shared_key; |
602 | | }; |
603 | | |
604 | | /** |
605 | | * Public Key Key Encapsulation Mechanism Encryption. |
606 | | */ |
607 | | class BOTAN_PUBLIC_API(2, 0) PK_KEM_Encryptor final { |
608 | | public: |
609 | | /** |
610 | | * Construct an instance. |
611 | | * @param key the key to encrypt to |
612 | | * @param kem_param additional KEM parameters |
613 | | * @param provider the provider to use |
614 | | */ |
615 | | BOTAN_FUTURE_EXPLICIT PK_KEM_Encryptor(const Public_Key& key, |
616 | | std::string_view kem_param = "", |
617 | | std::string_view provider = ""); |
618 | | |
619 | | /** |
620 | | * Construct an instance. |
621 | | * @param key the key to encrypt to |
622 | | * @param rng the RNG to use |
623 | | * @param kem_param additional KEM parameters |
624 | | * @param provider the provider to use |
625 | | */ |
626 | | BOTAN_DEPRECATED("Use constructor that does not take RNG") |
627 | | PK_KEM_Encryptor(const Public_Key& key, |
628 | | RandomNumberGenerator& rng, |
629 | | std::string_view kem_param = "", |
630 | | std::string_view provider = ""); |
631 | | |
632 | | ~PK_KEM_Encryptor(); |
633 | | |
634 | | PK_KEM_Encryptor(const PK_KEM_Encryptor&) = delete; |
635 | | PK_KEM_Encryptor& operator=(const PK_KEM_Encryptor&) = delete; |
636 | | |
637 | | PK_KEM_Encryptor(PK_KEM_Encryptor&&) noexcept; |
638 | | PK_KEM_Encryptor& operator=(PK_KEM_Encryptor&&) noexcept; |
639 | | |
640 | | /** |
641 | | * Return the length of the shared key returned by this KEM |
642 | | * |
643 | | * If this KEM was used with a KDF, then it will always return |
644 | | * exactly the desired key length, because the output of the KEM |
645 | | * will be hashed by the KDF. |
646 | | * |
647 | | * However if the KEM was used with "Raw" kdf, to request the |
648 | | * algorithmic output of the KEM directly, then the desired key |
649 | | * length will be ignored and a bytestring that depends on the |
650 | | * algorithm is returned |
651 | | * |
652 | | * @param desired_shared_key_len is the requested length |
653 | | */ |
654 | | size_t shared_key_length(size_t desired_shared_key_len) const; |
655 | | |
656 | | /** |
657 | | * Return the length in bytes of encapsulated keys returned by this KEM |
658 | | */ |
659 | | size_t encapsulated_key_length() const; |
660 | | |
661 | | /** |
662 | | * Generate a shared key for data encryption. |
663 | | * |
664 | | * @param rng the RNG to use |
665 | | * @param desired_shared_key_len desired size of the shared key in bytes for the KDF |
666 | | * (ignored if no KDF is used) |
667 | | * @param salt a salt value used in the KDF |
668 | | * (ignored if no KDF is used) |
669 | | * |
670 | | * @returns a struct with both the shared secret and its encapsulation |
671 | | */ |
672 | | KEM_Encapsulation encrypt(RandomNumberGenerator& rng, |
673 | | size_t desired_shared_key_len = 32, |
674 | 0 | std::span<const uint8_t> salt = {}) { |
675 | 0 | std::vector<uint8_t> encapsulated_shared_key(encapsulated_key_length()); |
676 | 0 | secure_vector<uint8_t> shared_key(shared_key_length(desired_shared_key_len)); |
677 | |
|
678 | 0 | encrypt(std::span{encapsulated_shared_key}, std::span{shared_key}, rng, desired_shared_key_len, salt); |
679 | 0 | return KEM_Encapsulation(std::move(encapsulated_shared_key), std::move(shared_key)); |
680 | 0 | } |
681 | | |
682 | | /** |
683 | | * Generate a shared key for data encryption. |
684 | | * @param out_encapsulated_key the generated encapsulated key |
685 | | * @param out_shared_key the generated shared key |
686 | | * @param rng the RNG to use |
687 | | * @param desired_shared_key_len desired size of the shared key in bytes |
688 | | * (ignored if no KDF is used) |
689 | | * @param salt a salt value used in the KDF |
690 | | * (ignored if no KDF is used) |
691 | | */ |
692 | | void encrypt(secure_vector<uint8_t>& out_encapsulated_key, |
693 | | secure_vector<uint8_t>& out_shared_key, |
694 | | RandomNumberGenerator& rng, |
695 | | size_t desired_shared_key_len = 32, |
696 | 0 | std::span<const uint8_t> salt = {}) { |
697 | 0 | out_encapsulated_key.resize(encapsulated_key_length()); |
698 | 0 | out_shared_key.resize(shared_key_length(desired_shared_key_len)); |
699 | 0 | encrypt(std::span{out_encapsulated_key}, std::span{out_shared_key}, rng, desired_shared_key_len, salt); |
700 | 0 | } |
701 | | |
702 | | /** |
703 | | * Generate a shared key for data encryption. |
704 | | * @param out_encapsulated_key the generated encapsulated key |
705 | | * @param out_shared_key the generated shared key |
706 | | * @param rng the RNG to use |
707 | | * @param desired_shared_key_len desired size of the shared key in bytes |
708 | | * (ignored if no KDF is used) |
709 | | * @param salt a salt value used in the KDF |
710 | | * (ignored if no KDF is used) |
711 | | */ |
712 | | void encrypt(std::span<uint8_t> out_encapsulated_key, |
713 | | std::span<uint8_t> out_shared_key, |
714 | | RandomNumberGenerator& rng, |
715 | | size_t desired_shared_key_len = 32, |
716 | | std::span<const uint8_t> salt = {}); |
717 | | |
718 | | BOTAN_DEPRECATED("use overload with salt as std::span<>") |
719 | | void encrypt(secure_vector<uint8_t>& out_encapsulated_key, |
720 | | secure_vector<uint8_t>& out_shared_key, |
721 | | size_t desired_shared_key_len, |
722 | | RandomNumberGenerator& rng, |
723 | | const uint8_t salt[], |
724 | 0 | size_t salt_len) { |
725 | 0 | this->encrypt(out_encapsulated_key, out_shared_key, rng, desired_shared_key_len, {salt, salt_len}); |
726 | 0 | } |
727 | | |
728 | | BOTAN_DEPRECATED("use overload where rng comes after the out-parameters") |
729 | | void encrypt(secure_vector<uint8_t>& out_encapsulated_key, |
730 | | secure_vector<uint8_t>& out_shared_key, |
731 | | size_t desired_shared_key_len, |
732 | | RandomNumberGenerator& rng, |
733 | 0 | std::span<const uint8_t> salt = {}) { |
734 | 0 | out_encapsulated_key.resize(encapsulated_key_length()); |
735 | 0 | out_shared_key.resize(shared_key_length(desired_shared_key_len)); |
736 | 0 | encrypt(out_encapsulated_key, out_shared_key, rng, desired_shared_key_len, salt); |
737 | 0 | } |
738 | | |
739 | | private: |
740 | | std::unique_ptr<PK_Ops::KEM_Encryption> m_op; |
741 | | }; |
742 | | |
743 | | /** |
744 | | * Public Key Key Encapsulation Mechanism Decryption. |
745 | | */ |
746 | | class BOTAN_PUBLIC_API(2, 0) PK_KEM_Decryptor final { |
747 | | public: |
748 | | /** |
749 | | * Construct an instance. |
750 | | * @param key the key to use inside the decryptor |
751 | | * @param rng the RNG to use |
752 | | * @param kem_param additional KEM parameters |
753 | | * @param provider the provider to use |
754 | | */ |
755 | | PK_KEM_Decryptor(const Private_Key& key, |
756 | | RandomNumberGenerator& rng, |
757 | | std::string_view kem_param = "", |
758 | | std::string_view provider = ""); |
759 | | |
760 | | ~PK_KEM_Decryptor(); |
761 | | PK_KEM_Decryptor(const PK_KEM_Decryptor&) = delete; |
762 | | PK_KEM_Decryptor& operator=(const PK_KEM_Decryptor&) = delete; |
763 | | |
764 | | PK_KEM_Decryptor(PK_KEM_Decryptor&&) noexcept; |
765 | | PK_KEM_Decryptor& operator=(PK_KEM_Decryptor&&) noexcept; |
766 | | |
767 | | /** |
768 | | * Return the length of the shared key returned by this KEM |
769 | | * |
770 | | * If this KEM was used with a KDF, then it will always return |
771 | | * exactly the desired key length, because the output of the KEM |
772 | | * will be hashed by the KDF. |
773 | | * |
774 | | * However if the KEM was used with "Raw" kdf, to request the |
775 | | * algorithmic output of the KEM directly, then the desired key |
776 | | * length will be ignored and a bytestring that depends on the |
777 | | * algorithm is returned |
778 | | * |
779 | | * @param desired_shared_key_len is the requested length. |
780 | | */ |
781 | | size_t shared_key_length(size_t desired_shared_key_len) const; |
782 | | |
783 | | /** |
784 | | * Return the length of the encapsulated key expected by this KEM |
785 | | */ |
786 | | size_t encapsulated_key_length() const; |
787 | | |
788 | | /** |
789 | | * Decrypts the shared key for data encryption. |
790 | | * |
791 | | * @param out_shared_key the generated shared key |
792 | | * @param encap_key the encapsulated key |
793 | | * @param desired_shared_key_len desired size of the shared key in bytes |
794 | | * (ignored if no KDF is used) |
795 | | * @param salt a salt value used in the KDF |
796 | | * (ignored if no KDF is used) |
797 | | */ |
798 | | void decrypt(std::span<uint8_t> out_shared_key, |
799 | | std::span<const uint8_t> encap_key, |
800 | | size_t desired_shared_key_len = 32, |
801 | | std::span<const uint8_t> salt = {}); |
802 | | |
803 | | /** |
804 | | * Decrypts the shared key for data encryption. |
805 | | * |
806 | | * @param encap_key the encapsulated key |
807 | | * @param encap_key_len size of the encapsulated key in bytes |
808 | | * @param desired_shared_key_len desired size of the shared key in bytes |
809 | | * (ignored if no KDF is used) |
810 | | * @param salt a salt value used in the KDF |
811 | | * (ignored if no KDF is used) |
812 | | * @param salt_len size of the salt value in bytes |
813 | | * (ignored if no KDF is used) |
814 | | * |
815 | | * @return the shared data encryption key |
816 | | */ |
817 | | secure_vector<uint8_t> decrypt(const uint8_t encap_key[], |
818 | | size_t encap_key_len, |
819 | | size_t desired_shared_key_len, |
820 | | const uint8_t salt[] = nullptr, |
821 | 0 | size_t salt_len = 0) { |
822 | 0 | secure_vector<uint8_t> shared_key(shared_key_length(desired_shared_key_len)); |
823 | 0 | decrypt(shared_key, {encap_key, encap_key_len}, desired_shared_key_len, {salt, salt_len}); |
824 | 0 | return shared_key; |
825 | 0 | } |
826 | | |
827 | | /** |
828 | | * Decrypts the shared key for data encryption. |
829 | | * |
830 | | * @param encap_key the encapsulated key |
831 | | * @param desired_shared_key_len desired size of the shared key in bytes |
832 | | * (ignored if no KDF is used) |
833 | | * @param salt a salt value used in the KDF |
834 | | * (ignored if no KDF is used) |
835 | | * |
836 | | * @return the shared data encryption key |
837 | | */ |
838 | | secure_vector<uint8_t> decrypt(std::span<const uint8_t> encap_key, |
839 | | size_t desired_shared_key_len = 32, |
840 | 0 | std::span<const uint8_t> salt = {}) { |
841 | 0 | secure_vector<uint8_t> shared_key(shared_key_length(desired_shared_key_len)); |
842 | 0 | decrypt(shared_key, encap_key, desired_shared_key_len, salt); |
843 | 0 | return shared_key; |
844 | 0 | } |
845 | | |
846 | | private: |
847 | | std::unique_ptr<PK_Ops::KEM_Decryption> m_op; |
848 | | }; |
849 | | |
850 | | } // namespace Botan |
851 | | |
852 | | #endif |