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