Botan 3.9.0
Crypto and TLS for C&
p11_ecdsa.h
Go to the documentation of this file.
1/*
2* PKCS#11 ECDSA
3* (C) 2016 Daniel Neus, Sirrix AG
4* (C) 2016 Philipp Weber, Sirrix AG
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_P11_ECDSA_H_
10#define BOTAN_P11_ECDSA_H_
11
12#include <botan/p11.h>
13#include <botan/pk_keys.h>
14
15#if defined(BOTAN_HAS_ECDSA)
16
17 #include <botan/ecdsa.h>
18 #include <botan/p11_ecc_key.h>
19
20 #include <string>
21
22namespace Botan::PKCS11 {
23class Session;
24
25/// Represents a PKCS#11 ECDSA public key
26
29
30class BOTAN_PUBLIC_API(2, 0) PKCS11_ECDSA_PublicKey final : public PKCS11_EC_PublicKey,
31 public virtual ECDSA_PublicKey {
32 public:
33 /**
34 * Creates a PKCS11_ECDSA_PublicKey object from an existing PKCS#11 ECDSA public key
35 * @param session the session to use
36 * @param handle the handle of the ECDSA public key
37 */
38 PKCS11_ECDSA_PublicKey(Session& session, ObjectHandle handle) : PKCS11_EC_PublicKey(session, handle) {}
39
40 /**
41 * Imports an ECDSA public key
42 * @param session the session to use
43 * @param props the attributes of the public key
44 */
45 PKCS11_ECDSA_PublicKey(Session& session, const EC_PublicKeyImportProperties& props) :
46 PKCS11_EC_PublicKey(session, props) {}
47
48 inline std::string algo_name() const override { return "ECDSA"; }
49
50 /// @return the exported ECDSA public key
51 ECDSA_PublicKey export_key() const;
52
53 /**
54 * @throws Not_Implemented as this operation is not possible in PKCS11
55 */
56 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& /*rng*/) const final {
57 throw Not_Implemented("Cannot generate a new PKCS#11 ECDSA keypair from this public key");
58 }
59
60 std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
61 std::string_view provider) const override;
62};
63
65
66/// Represents a PKCS#11 ECDSA private key
67class BOTAN_PUBLIC_API(2, 0) PKCS11_ECDSA_PrivateKey final : public PKCS11_EC_PrivateKey {
68 public:
69 /**
70 * Creates a PKCS11_ECDSA_PrivateKey object from an existing PKCS#11 ECDSA private key
71 * @param session the session to use
72 * @param handle the handle of the ECDSA private key
73 */
74 PKCS11_ECDSA_PrivateKey(Session& session, ObjectHandle handle) : PKCS11_EC_PrivateKey(session, handle) {}
75
76 /**
77 * Imports a ECDSA private key
78 * @param session the session to use
79 * @param props the attributes of the private key
80 */
81 PKCS11_ECDSA_PrivateKey(Session& session, const EC_PrivateKeyImportProperties& props) :
82 PKCS11_EC_PrivateKey(session, props) {}
83
84 /**
85 * Generates a PKCS#11 ECDSA private key
86 * @param session the session to use
87 * @param ec_params DER-encoding of an ANSI X9.62 Parameters value
88 * @param props the attributes of the private key
89 * @note no persistent public key object will be created
90 */
91 PKCS11_ECDSA_PrivateKey(Session& session,
92 const std::vector<uint8_t>& ec_params,
93 const EC_PrivateKeyGenerationProperties& props) :
94 PKCS11_EC_PrivateKey(session, ec_params, props) {}
95
96 inline std::string algo_name() const override { return "ECDSA"; }
97
98 /**
99 * @throws Not_Implemented as this operation is not possible in PKCS11
100 */
101 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& /*rng*/) const override {
102 throw Not_Implemented("Cannot generate a new PKCS#11 ECDSA keypair from this private key");
103 }
104
105 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
106
107 std::optional<size_t> _signature_element_size_for_DER_encoding() const override {
108 return domain().get_order_bytes();
109 }
110
111 /// @return the exported ECDSA private key
112 ECDSA_PrivateKey export_key() const;
113
114 std::unique_ptr<Public_Key> public_key() const override;
115
116 secure_vector<uint8_t> private_key_bits() const override;
117
118 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
119
120 std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
121 std::string_view params,
122 std::string_view provider) const override;
123};
124
125using PKCS11_ECDSA_KeyPair = std::pair<PKCS11_ECDSA_PublicKey, PKCS11_ECDSA_PrivateKey>;
126
127/**
128* ECDSA key pair generation
129* @param session the session that should be used for the key generation
130* @param pub_props the properties of the public key
131* @param priv_props the properties of the private key
132*/
134PKCS11_ECDSA_KeyPair generate_ecdsa_keypair(Session& session,
135 const EC_PublicKeyGenerationProperties& pub_props,
136 const EC_PrivateKeyGenerationProperties& priv_props);
137} // namespace Botan::PKCS11
138
139#endif
140#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:122
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:119
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:121
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Represents a PKCS#11 session.
Definition p11_types.h:122