/src/Botan-3.4.0/build/include/public/botan/pk_keys.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * PK Key Types |
3 | | * (C) 1999-2007,2018 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_PK_KEYS_H_ |
9 | | #define BOTAN_PK_KEYS_H_ |
10 | | |
11 | | #include <botan/asn1_obj.h> |
12 | | #include <botan/pk_ops_fwd.h> |
13 | | #include <botan/secmem.h> |
14 | | |
15 | | #include <optional> |
16 | | #include <span> |
17 | | #include <string> |
18 | | #include <string_view> |
19 | | |
20 | | namespace Botan { |
21 | | |
22 | | class BigInt; |
23 | | class RandomNumberGenerator; |
24 | | |
25 | | /** |
26 | | * Enumeration specifying the signature format. |
27 | | * |
28 | | * This is mostly used for requesting DER encoding of ECDSA signatures; |
29 | | * most other algorithms only support "standard". |
30 | | */ |
31 | | enum class Signature_Format { |
32 | | Standard, |
33 | | DerSequence, |
34 | | |
35 | | IEEE_1363 BOTAN_DEPRECATED("Use Standard") = Standard, |
36 | | DER_SEQUENCE BOTAN_DEPRECATED("Use DerSequence") = DerSequence, |
37 | | }; |
38 | | |
39 | | /** |
40 | | * Enumeration of possible operations a public key could be used for. |
41 | | * |
42 | | * It is possible to query if a key supports a particular operation |
43 | | * type using Asymmetric_Key::supports_operation() |
44 | | */ |
45 | | enum class PublicKeyOperation { |
46 | | Encryption, |
47 | | Signature, |
48 | | KeyEncapsulation, |
49 | | KeyAgreement, |
50 | | }; |
51 | | |
52 | | class Private_Key; |
53 | | |
54 | | /** |
55 | | * An interface for objects that are keys in public key algorithms |
56 | | * |
57 | | * This is derived for both public and private keys |
58 | | */ |
59 | | class BOTAN_PUBLIC_API(3, 0) Asymmetric_Key { |
60 | | public: |
61 | 201k | virtual ~Asymmetric_Key() = default; |
62 | | |
63 | | /** |
64 | | * Get the name of the underlying public key scheme. |
65 | | * @return name of the public key scheme |
66 | | */ |
67 | | virtual std::string algo_name() const = 0; |
68 | | |
69 | | /** |
70 | | * Return the estimated strength of the underlying key against |
71 | | * the best currently known attack. Note that this ignores anything |
72 | | * but pure attacks against the key itself and do not take into |
73 | | * account padding schemes, usage mistakes, etc which might reduce |
74 | | * the strength. However it does suffice to provide an upper bound. |
75 | | * |
76 | | * @return estimated strength in bits |
77 | | */ |
78 | | virtual size_t estimated_strength() const = 0; |
79 | | |
80 | | /** |
81 | | * Get the OID of the underlying public key scheme. |
82 | | * @return OID of the public key scheme |
83 | | */ |
84 | | virtual OID object_identifier() const; |
85 | | |
86 | | /** |
87 | | * Access an algorithm specific field |
88 | | * |
89 | | * If the field is not known for this algorithm, an Invalid_Argument is |
90 | | * thrown. The interpretation of the result requires knowledge of which |
91 | | * algorithm is involved. For instance for RSA "p" represents one of the |
92 | | * secret primes, while for DSA "p" is the public prime. |
93 | | * |
94 | | * Some algorithms may not implement this method at all. |
95 | | * |
96 | | * This is primarily used to implement the FFI botan_pubkey_get_field |
97 | | * and botan_privkey_get_field functions. |
98 | | */ |
99 | | virtual const BigInt& get_int_field(std::string_view field) const; |
100 | | |
101 | | /** |
102 | | * Return true if this key could be used for the specified type |
103 | | * of operation. |
104 | | */ |
105 | | virtual bool supports_operation(PublicKeyOperation op) const = 0; |
106 | | |
107 | | /** |
108 | | * Generate another (cryptographically independent) key pair using the |
109 | | * same algorithm parameters as this key. This is most useful for algorithms |
110 | | * that support PublicKeyOperation::KeyAgreement to generate a fitting ephemeral |
111 | | * key pair. For other key types it might throw Not_Implemented. |
112 | | */ |
113 | | virtual std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const = 0; |
114 | | }; |
115 | | |
116 | | /* |
117 | | * Public Key Base Class. |
118 | | */ |
119 | | class BOTAN_PUBLIC_API(2, 0) Public_Key : public virtual Asymmetric_Key { |
120 | | public: |
121 | | /** |
122 | | * Return an integer value best approximating the length of the |
123 | | * primary security parameter. For example for RSA this will be |
124 | | * the size of the modulus, for ECDSA the size of the ECC group, |
125 | | * and for McEliece the size of the code will be returned. |
126 | | */ |
127 | | virtual size_t key_length() const = 0; |
128 | | |
129 | | /** |
130 | | * Deprecated version of object_identifier |
131 | | */ |
132 | 0 | BOTAN_DEPRECATED("Use object_identifier") OID get_oid() const { return this->object_identifier(); } |
133 | | |
134 | | /* |
135 | | * Test the key values for consistency. |
136 | | * @param rng rng to use |
137 | | * @param strong whether to perform strong and lengthy version |
138 | | * of the test |
139 | | * @return true if the test is passed |
140 | | */ |
141 | | virtual bool check_key(RandomNumberGenerator& rng, bool strong) const = 0; |
142 | | |
143 | | /** |
144 | | * @return X.509 AlgorithmIdentifier for this key |
145 | | */ |
146 | | virtual AlgorithmIdentifier algorithm_identifier() const = 0; |
147 | | |
148 | | /** |
149 | | * @return BER encoded public key bits |
150 | | */ |
151 | | virtual std::vector<uint8_t> public_key_bits() const = 0; |
152 | | |
153 | | /** |
154 | | * @return X.509 subject key encoding for this key object |
155 | | */ |
156 | | std::vector<uint8_t> subject_public_key() const; |
157 | | |
158 | | /** |
159 | | * @return Hash of the subject public key |
160 | | */ |
161 | | std::string fingerprint_public(std::string_view alg = "SHA-256") const; |
162 | | |
163 | | // Internal or non-public declarations follow |
164 | | |
165 | | /** |
166 | | * Returns more than 1 if the output of this algorithm |
167 | | * (ciphertext, signature) should be treated as more than one |
168 | | * value. This is used for algorithms like DSA and ECDSA, where |
169 | | * the (r,s) output pair can be encoded as either a plain binary |
170 | | * list or a TLV tagged DER encoding depending on the protocol. |
171 | | * |
172 | | * This function is public but applications should have few |
173 | | * reasons to ever call this. |
174 | | * |
175 | | * @return number of message parts |
176 | | */ |
177 | 113k | virtual size_t message_parts() const { return 1; } |
178 | | |
179 | | /** |
180 | | * Returns how large each of the message parts refered to |
181 | | * by message_parts() is |
182 | | * |
183 | | * This function is public but applications should have few |
184 | | * reasons to ever call this. |
185 | | * |
186 | | * @return size of the message parts in bits |
187 | | */ |
188 | 113k | virtual size_t message_part_size() const { return 0; } |
189 | | |
190 | 0 | virtual Signature_Format default_x509_signature_format() const { |
191 | 0 | return (this->message_parts() >= 2) ? Signature_Format::DerSequence : Signature_Format::Standard; |
192 | 0 | } |
193 | | |
194 | | /** |
195 | | * This is an internal library function exposed on key types. |
196 | | * In almost all cases applications should use wrappers in pubkey.h |
197 | | * |
198 | | * Return an encryption operation for this key/params or throw |
199 | | * |
200 | | * @param rng a random number generator. The PK_Op may maintain a |
201 | | * reference to the RNG and use it many times. The rng must outlive |
202 | | * any operations which reference it. |
203 | | * @param params additional parameters |
204 | | * @param provider the provider to use |
205 | | */ |
206 | | virtual std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng, |
207 | | std::string_view params, |
208 | | std::string_view provider) const; |
209 | | |
210 | | /** |
211 | | * This is an internal library function exposed on key types. |
212 | | * In almost all cases applications should use wrappers in pubkey.h |
213 | | * |
214 | | * Return a KEM encryption operation for this key/params or throw |
215 | | * |
216 | | * @param params additional parameters |
217 | | * @param provider the provider to use |
218 | | */ |
219 | | virtual std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params, |
220 | | std::string_view provider) const; |
221 | | |
222 | | /** |
223 | | * This is an internal library function exposed on key types. |
224 | | * In all cases applications should use wrappers in pubkey.h |
225 | | * |
226 | | * Return a verification operation for this key/params or throw |
227 | | * @param params additional parameters |
228 | | * @param provider the provider to use |
229 | | */ |
230 | | virtual std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params, |
231 | | std::string_view provider) const; |
232 | | |
233 | | /** |
234 | | * This is an internal library function exposed on key types. |
235 | | * In all cases applications should use wrappers in pubkey.h |
236 | | * |
237 | | * Return a verification operation for this combination of key and |
238 | | * signature algorithm or throw. |
239 | | * |
240 | | * @param signature_algorithm is the X.509 algorithm identifier encoding the padding |
241 | | * scheme and hash hash function used in the signature if applicable. |
242 | | * |
243 | | * @param provider the provider to use |
244 | | */ |
245 | | virtual std::unique_ptr<PK_Ops::Verification> create_x509_verification_op( |
246 | | const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const; |
247 | | }; |
248 | | |
249 | | /** |
250 | | * Private Key Base Class |
251 | | */ |
252 | | class BOTAN_PUBLIC_API(2, 0) Private_Key : public virtual Public_Key { |
253 | | public: |
254 | | /** |
255 | | * @return BER encoded private key bits |
256 | | */ |
257 | | virtual secure_vector<uint8_t> private_key_bits() const = 0; |
258 | | |
259 | | /** |
260 | | * @return binary private key bits, with no additional encoding |
261 | | * |
262 | | * Note: some algorithms (for example RSA) do not have an obvious encoding |
263 | | * for this value due to having many different values, and thus not implement |
264 | | * this function. The default implementation throws Not_Implemented |
265 | | */ |
266 | | virtual secure_vector<uint8_t> raw_private_key_bits() const; |
267 | | |
268 | | /** |
269 | | * Allocate a new object for the public key associated with this |
270 | | * private key. |
271 | | * |
272 | | * @return public key |
273 | | */ |
274 | | virtual std::unique_ptr<Public_Key> public_key() const = 0; |
275 | | |
276 | | /** |
277 | | * @return PKCS #8 private key encoding for this key object |
278 | | */ |
279 | | secure_vector<uint8_t> private_key_info() const; |
280 | | |
281 | | /** |
282 | | * @return PKCS #8 AlgorithmIdentifier for this key |
283 | | * Might be different from the X.509 identifier, but normally is not |
284 | | */ |
285 | 0 | virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const { return algorithm_identifier(); } |
286 | | |
287 | | /** |
288 | | * Indicates if this key is stateful, ie that performing a private |
289 | | * key operation requires updating the key storage. |
290 | | */ |
291 | 0 | virtual bool stateful_operation() const { return false; } |
292 | | |
293 | | /** |
294 | | * @brief Retrieves the number of remaining operations if this is a stateful private key. |
295 | | * |
296 | | * @returns the number of remaining operations or std::nullopt if not applicable. |
297 | | */ |
298 | 0 | virtual std::optional<uint64_t> remaining_operations() const { return std::nullopt; } |
299 | | |
300 | | // Internal or non-public declarations follow |
301 | | |
302 | | /** |
303 | | * @return Hash of the PKCS #8 encoding for this key object |
304 | | */ |
305 | | std::string fingerprint_private(std::string_view alg) const; |
306 | | |
307 | | /** |
308 | | * This is an internal library function exposed on key types. |
309 | | * In all cases applications should use wrappers in pubkey.h |
310 | | * |
311 | | * Return an decryption operation for this key/params or throw |
312 | | * |
313 | | * @param rng a random number generator. The PK_Op may maintain a |
314 | | * reference to the RNG and use it many times. The rng must outlive |
315 | | * any operations which reference it. |
316 | | * @param params additional parameters |
317 | | * @param provider the provider to use |
318 | | * |
319 | | */ |
320 | | virtual std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng, |
321 | | std::string_view params, |
322 | | std::string_view provider) const; |
323 | | |
324 | | /** |
325 | | * This is an internal library function exposed on key types. |
326 | | * In all cases applications should use wrappers in pubkey.h |
327 | | * |
328 | | * Return a KEM decryption operation for this key/params or throw |
329 | | * |
330 | | * @param rng a random number generator. The PK_Op may maintain a |
331 | | * reference to the RNG and use it many times. The rng must outlive |
332 | | * any operations which reference it. |
333 | | * @param params additional parameters |
334 | | * @param provider the provider to use |
335 | | */ |
336 | | virtual std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng, |
337 | | std::string_view params, |
338 | | std::string_view provider) const; |
339 | | |
340 | | /** |
341 | | * This is an internal library function exposed on key types. |
342 | | * In all cases applications should use wrappers in pubkey.h |
343 | | * |
344 | | * Return a signature operation for this key/params or throw |
345 | | * |
346 | | * @param rng a random number generator. The PK_Op may maintain a |
347 | | * reference to the RNG and use it many times. The rng must outlive |
348 | | * any operations which reference it. |
349 | | * @param params additional parameters |
350 | | * @param provider the provider to use |
351 | | */ |
352 | | virtual std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng, |
353 | | std::string_view params, |
354 | | std::string_view provider) const; |
355 | | |
356 | | /** |
357 | | * This is an internal library function exposed on key types. |
358 | | * In all cases applications should use wrappers in pubkey.h |
359 | | * |
360 | | * Return a key agreement operation for this key/params or throw |
361 | | * |
362 | | * @param rng a random number generator. The PK_Op may maintain a |
363 | | * reference to the RNG and use it many times. The rng must outlive |
364 | | * any operations which reference it. |
365 | | * @param params additional parameters |
366 | | * @param provider the provider to use |
367 | | */ |
368 | | virtual std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng, |
369 | | std::string_view params, |
370 | | std::string_view provider) const; |
371 | | }; |
372 | | |
373 | | /** |
374 | | * PK Secret Value Derivation Key |
375 | | */ |
376 | | class BOTAN_PUBLIC_API(2, 0) PK_Key_Agreement_Key : public virtual Private_Key { |
377 | | public: |
378 | | /* |
379 | | * @return public component of this key |
380 | | */ |
381 | | virtual std::vector<uint8_t> public_value() const = 0; |
382 | | }; |
383 | | |
384 | | std::string BOTAN_PUBLIC_API(2, 4) create_hex_fingerprint(const uint8_t bits[], size_t len, std::string_view hash_name); |
385 | | |
386 | 0 | inline std::string create_hex_fingerprint(std::span<const uint8_t> vec, std::string_view hash_name) { |
387 | 0 | return create_hex_fingerprint(vec.data(), vec.size(), hash_name); |
388 | 0 | } |
389 | | |
390 | | } // namespace Botan |
391 | | |
392 | | #endif |