Botan 3.6.1
Crypto and TLS for C&
pk_keys.h
Go to the documentation of this file.
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
20namespace Botan {
21
22class BigInt;
23class 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*/
31enum class Signature_Format {
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*/
51
52class 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*/
60 public:
61 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*/
119class 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 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 */
147
148 /**
149 * @return binary public key bits, with no additional encoding
150 *
151 * For key agreements this is an alias for PK_Key_Agreement_Key::public_value.
152 *
153 * Note: some algorithms (for example RSA) do not have an obvious encoding
154 * for this value due to having many different values, and thus throw
155 * Not_Implemented when invoking this method.
156 */
157 virtual std::vector<uint8_t> raw_public_key_bits() const = 0;
158
159 /**
160 * @return BER encoded public key bits
161 */
162 virtual std::vector<uint8_t> public_key_bits() const = 0;
163
164 /**
165 * @return X.509 subject key encoding for this key object
166 */
167 std::vector<uint8_t> subject_public_key() const;
168
169 /**
170 * @return Hash of the subject public key
171 */
172 std::string fingerprint_public(std::string_view alg = "SHA-256") const;
173
174 // Internal or non-public declarations follow
175
176 /**
177 * Returns more than 1 if the output of this algorithm
178 * (ciphertext, signature) should be treated as more than one
179 * value. This is used for algorithms like DSA and ECDSA, where
180 * the (r,s) output pair can be encoded as either a plain binary
181 * list or a TLV tagged DER encoding depending on the protocol.
182 *
183 * This function is public but applications should have few
184 * reasons to ever call this.
185 *
186 * @return number of message parts
187 */
188 virtual size_t message_parts() const { return 1; }
189
190 /**
191 * Returns how large each of the message parts refered to
192 * by message_parts() is
193 *
194 * This function is public but applications should have few
195 * reasons to ever call this.
196 *
197 * @return size of the message parts in bits
198 */
199 virtual size_t message_part_size() const { return 0; }
200
202 return (this->message_parts() >= 2) ? Signature_Format::DerSequence : Signature_Format::Standard;
203 }
204
205 /**
206 * This is an internal library function exposed on key types.
207 * In almost all cases applications should use wrappers in pubkey.h
208 *
209 * Return an encryption operation for this key/params or throw
210 *
211 * @param rng a random number generator. The PK_Op may maintain a
212 * reference to the RNG and use it many times. The rng must outlive
213 * any operations which reference it.
214 * @param params additional parameters
215 * @param provider the provider to use
216 */
217 virtual std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
218 std::string_view params,
219 std::string_view provider) const;
220
221 /**
222 * This is an internal library function exposed on key types.
223 * In almost all cases applications should use wrappers in pubkey.h
224 *
225 * Return a KEM encryption operation for this key/params or throw
226 *
227 * @param params additional parameters
228 * @param provider the provider to use
229 */
230 virtual std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_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 key/params or throw
238 * @param params additional parameters
239 * @param provider the provider to use
240 */
241 virtual std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
242 std::string_view provider) const;
243
244 /**
245 * This is an internal library function exposed on key types.
246 * In all cases applications should use wrappers in pubkey.h
247 *
248 * Return a verification operation for this combination of key and
249 * signature algorithm or throw.
250 *
251 * @param signature_algorithm is the X.509 algorithm identifier encoding the padding
252 * scheme and hash hash function used in the signature if applicable.
253 *
254 * @param provider the provider to use
255 */
256 virtual std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(
257 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const;
258};
259
260/**
261* Private Key Base Class
262*/
263class BOTAN_PUBLIC_API(2, 0) Private_Key : public virtual Public_Key {
264 public:
265 /**
266 * @return BER encoded private key bits
267 */
269
270 /**
271 * @return binary private key bits, with no additional encoding
272 *
273 * Note: some algorithms (for example RSA) do not have an obvious encoding
274 * for this value due to having many different values, and thus not implement
275 * this function. The default implementation throws Not_Implemented
276 */
277 virtual secure_vector<uint8_t> raw_private_key_bits() const;
278
279 /**
280 * Allocate a new object for the public key associated with this
281 * private key.
282 *
283 * @return public key
284 */
285 virtual std::unique_ptr<Public_Key> public_key() const = 0;
286
287 /**
288 * @return PKCS #8 private key encoding for this key object
289 */
290 secure_vector<uint8_t> private_key_info() const;
291
292 /**
293 * @return PKCS #8 AlgorithmIdentifier for this key
294 * Might be different from the X.509 identifier, but normally is not
295 */
296 virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const { return algorithm_identifier(); }
297
298 /**
299 * Indicates if this key is stateful, ie that performing a private
300 * key operation requires updating the key storage.
301 */
302 virtual bool stateful_operation() const { return false; }
303
304 /**
305 * @brief Retrieves the number of remaining operations if this is a stateful private key.
306 *
307 * @returns the number of remaining operations or std::nullopt if not applicable.
308 */
309 virtual std::optional<uint64_t> remaining_operations() const { return std::nullopt; }
310
311 // Internal or non-public declarations follow
312
313 /**
314 * @return Hash of the PKCS #8 encoding for this key object
315 */
316 std::string fingerprint_private(std::string_view alg) const;
317
318 /**
319 * This is an internal library function exposed on key types.
320 * In all cases applications should use wrappers in pubkey.h
321 *
322 * Return an decryption operation for this key/params or throw
323 *
324 * @param rng a random number generator. The PK_Op may maintain a
325 * reference to the RNG and use it many times. The rng must outlive
326 * any operations which reference it.
327 * @param params additional parameters
328 * @param provider the provider to use
329 *
330 */
331 virtual std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
332 std::string_view params,
333 std::string_view provider) const;
334
335 /**
336 * This is an internal library function exposed on key types.
337 * In all cases applications should use wrappers in pubkey.h
338 *
339 * Return a KEM decryption operation for this key/params or throw
340 *
341 * @param rng a random number generator. The PK_Op may maintain a
342 * reference to the RNG and use it many times. The rng must outlive
343 * any operations which reference it.
344 * @param params additional parameters
345 * @param provider the provider to use
346 */
347 virtual std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
348 std::string_view params,
349 std::string_view provider) const;
350
351 /**
352 * This is an internal library function exposed on key types.
353 * In all cases applications should use wrappers in pubkey.h
354 *
355 * Return a signature operation for this key/params or throw
356 *
357 * @param rng a random number generator. The PK_Op may maintain a
358 * reference to the RNG and use it many times. The rng must outlive
359 * any operations which reference it.
360 * @param params additional parameters
361 * @param provider the provider to use
362 */
363 virtual std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
364 std::string_view params,
365 std::string_view provider) const;
366
367 /**
368 * This is an internal library function exposed on key types.
369 * In all cases applications should use wrappers in pubkey.h
370 *
371 * Return a key agreement operation for this key/params or throw
372 *
373 * @param rng a random number generator. The PK_Op may maintain a
374 * reference to the RNG and use it many times. The rng must outlive
375 * any operations which reference it.
376 * @param params additional parameters
377 * @param provider the provider to use
378 */
379 virtual std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
380 std::string_view params,
381 std::string_view provider) const;
382};
383
384/**
385* PK Secret Value Derivation Key
386*/
388 public:
389 /*
390 * @return public component of this key
391 */
392 virtual std::vector<uint8_t> public_value() const = 0;
393};
394
395std::string BOTAN_PUBLIC_API(2, 4) create_hex_fingerprint(const uint8_t bits[], size_t len, std::string_view hash_name);
396
397inline std::string create_hex_fingerprint(std::span<const uint8_t> vec, std::string_view hash_name) {
398 return create_hex_fingerprint(vec.data(), vec.size(), hash_name);
399}
400
401} // namespace Botan
402
403#endif
virtual std::string algo_name() const =0
virtual ~Asymmetric_Key()=default
virtual bool supports_operation(PublicKeyOperation op) const =0
virtual std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const =0
virtual size_t estimated_strength() const =0
virtual std::vector< uint8_t > public_value() const =0
virtual std::unique_ptr< Public_Key > public_key() const =0
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition pk_keys.h:296
virtual std::optional< uint64_t > remaining_operations() const
Retrieves the number of remaining operations if this is a stateful private key.
Definition pk_keys.h:309
virtual bool stateful_operation() const
Definition pk_keys.h:302
virtual secure_vector< uint8_t > private_key_bits() const =0
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::vector< uint8_t > public_key_bits() const =0
virtual Signature_Format default_x509_signature_format() const
Definition pk_keys.h:201
virtual bool check_key(RandomNumberGenerator &rng, bool strong) const =0
virtual std::vector< uint8_t > raw_public_key_bits() const =0
virtual size_t key_length() const =0
virtual size_t message_part_size() const
Definition pk_keys.h:199
virtual size_t message_parts() const
Definition pk_keys.h:188
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
PublicKeyOperation
Definition pk_keys.h:45
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, std::string_view hash_name)
Definition pk_keys.cpp:30
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
Signature_Format
Definition pk_keys.h:31