Botan 3.14.1
Crypto and TLS for C&
p11_ecc_key.h
Go to the documentation of this file.
1/*
2* PKCS#11 ECC
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_ECC_H_
10#define BOTAN_P11_ECC_H_
11
12#include <botan/p11_object.h>
13#include <botan/pk_keys.h>
14
15#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
16 #include <botan/asn1_obj.h>
17 #include <botan/ec_group.h>
18 #include <botan/ecc_key.h>
19 #include <vector>
20
21namespace Botan::PKCS11 {
22
23class Session;
24
25/// Properties for generating a PKCS#11 EC public key
26class BOTAN_PUBLIC_API(2, 0) EC_PublicKeyGenerationProperties final : public PublicKeyProperties {
27 public:
28 /// @param ec_params DER-encoding of an ANSI X9.62 Parameters value
29 BOTAN_FUTURE_EXPLICIT EC_PublicKeyGenerationProperties(const std::vector<uint8_t>& ec_params);
30
31 /// @return the DER-encoding of the ec parameters according to ANSI X9.62
32 inline const std::vector<uint8_t>& ec_params() const { return m_ec_params; }
33
34 private:
35 const std::vector<uint8_t> m_ec_params;
36};
37
38/// Properties for importing a PKCS#11 EC public key
39class BOTAN_PUBLIC_API(2, 0) EC_PublicKeyImportProperties final : public PublicKeyProperties {
40 public:
41 /**
42 * @param ec_params DER-encoding of an ANSI X9.62 Parameters value
43 * @param ec_point DER-encoding of ANSI X9.62 ECPoint value Q
44 */
45 EC_PublicKeyImportProperties(const std::vector<uint8_t>& ec_params, const std::vector<uint8_t>& ec_point);
46
47 /// @return the DER-encoding of the ec parameters according to ANSI X9.62
48 inline const std::vector<uint8_t>& ec_params() const { return m_ec_params; }
49
50 /// @return the DER-encoding of the ec public point according to ANSI X9.62
51 inline const std::vector<uint8_t>& ec_point() const { return m_ec_point; }
52
53 private:
54 const std::vector<uint8_t> m_ec_params;
55 const std::vector<uint8_t> m_ec_point;
56};
57
58/// Represents a PKCS#11 EC public key
59class BOTAN_PUBLIC_API(2, 0) PKCS11_EC_PublicKey : public virtual EC_PublicKey,
60 public Object {
61 public:
62 static const ObjectClass Class = ObjectClass::PublicKey;
63 static const KeyType Key_Type = KeyType::Ec;
64
65 /**
66 * Creates a PKCS11_EC_PublicKey object from an existing PKCS#11 EC public key
67 * @param session the session to use
68 * @param handle the handle of the ecc public key
69 */
70 PKCS11_EC_PublicKey(Session& session, ObjectHandle handle);
71
72 /**
73 * Imports an EC public key
74 * @param session the session to use
75 * @param props the attributes of the public key
76 */
77 PKCS11_EC_PublicKey(Session& session, const EC_PublicKeyImportProperties& props);
78};
79
80/// Properties for generating a PKCS#11 EC private key
81class BOTAN_PUBLIC_API(2, 0) EC_PrivateKeyGenerationProperties final : public PrivateKeyProperties {
82 public:
83 EC_PrivateKeyGenerationProperties() : PrivateKeyProperties(KeyType::Ec) {}
84};
85
86/// Properties for importing a PKCS#11 EC private key
87class BOTAN_PUBLIC_API(2, 0) EC_PrivateKeyImportProperties final : public PrivateKeyProperties {
88 public:
89 /**
90 * @param ec_params DER-encoding of an ANSI X9.62 Parameters value
91 * @param value ANSI X9.62 private value d
92 */
93 EC_PrivateKeyImportProperties(const std::vector<uint8_t>& ec_params, const BigInt& value);
94
95 /// @return the DER-encoding of the ec parameters according to ANSI X9.62
96 inline const std::vector<uint8_t>& ec_params() const { return m_ec_params; }
97
98 /// @return the value of the ec private key
99 inline const BigInt& value() const { return m_value; }
100
101 private:
102 const std::vector<uint8_t> m_ec_params;
103 const BigInt m_value;
104};
105
106// note: don't inherit from PKCS11_EC_PublicKey: a private key object IS NOT A public key object on a smartcard (-> two different objects)
107// note: don't inherit from EC_PublicKey: the public key can not be extracted from a PKCS11-EC-PrivateKey (its only attributes are CKA_EC_PARAMS and CKA_VALUE)
108/// Represents a PKCS#11 EC private key
109class BOTAN_PUBLIC_API(2, 0) PKCS11_EC_PrivateKey : public virtual Private_Key,
110 public Object {
111 public:
112 static const ObjectClass Class = ObjectClass::PrivateKey;
113 static const KeyType Key_Type = KeyType::Ec;
114
115 /**
116 * Creates a PKCS11_EC_PrivateKey object from an existing PKCS#11 EC private key
117 * @param session the session to use
118 * @param handle the handle of the EC private key
119 */
120 PKCS11_EC_PrivateKey(Session& session, ObjectHandle handle);
121
122 /**
123 * Imports an EC private key
124 * @param session the session to use
125 * @param props the attributes of the private key
126 */
127 PKCS11_EC_PrivateKey(Session& session, const EC_PrivateKeyImportProperties& props);
128
129 /**
130 * Generates a PKCS#11 EC private key
131 * @param session the session to use
132 * @param ec_params DER-encoding of an ANSI X9.62 Parameters value
133 * @param props the attributes of the private key
134 * @note no persistent public key object will be created
135 */
136 PKCS11_EC_PrivateKey(Session& session,
137 const std::vector<uint8_t>& ec_params,
138 const EC_PrivateKeyGenerationProperties& props);
139
140 /// @returns the domain of the EC private key
141 inline const EC_Group& domain() const { return m_domain_params; }
142
143 #if defined(BOTAN_HAS_LEGACY_EC_POINT)
144 /**
145 * Sets the associated public point of this private key
146 * @param point the public point
147 * @param point_encoding encoding of the point (default DER-encoded)
148 */
149 void set_public_point(const EC_Point& point, PublicPointEncoding point_encoding = PublicPointEncoding::Der) {
150 this->set_public_point(EC_AffinePoint(domain(), point), point_encoding);
151 }
152 #endif
153
154 /**
155 * Sets the associated public point of this private key
156 * @param point the public point
157 * @param point_encoding encoding of the point (default DER-encoded)
158 */
159 void set_public_point(const EC_AffinePoint& point,
160 PublicPointEncoding point_encoding = PublicPointEncoding::Der) {
161 m_public_key = point;
162 m_point_encoding = point_encoding;
163 }
164
165 /**
166 * Sets the public desired public point encoding of this private key, when it is passed to cryptoki functions.
167 * This could be either `PublicPointEncoding::Raw` or `PublicPointEncoding::Der`. By default this is set to `Der`,
168 * but some tokens might expect `Raw`-encoded public keys, e.g. when using this private key for key agreement.
169 */
170 void set_point_encoding(PublicPointEncoding point_encoding) { m_point_encoding = point_encoding; }
171
172 #if defined(BOTAN_HAS_LEGACY_EC_POINT)
173 /**
174 * Gets the public_point
175 * @note the public key must be set using `set_public_point`
176 * because it is not possible to infer the public key from a PKCS#11 EC private key
177 * @return the public point of the private key
178 * @throws Exception if the public point was not set using set_public_point()
179 */
180 EC_Point public_point() const { return this->public_ec_point().to_legacy_point(); }
181 #endif
182
183 /**
184 * Gets the elliptic curve point associated with the public key
185 *
186 * @note the public key must be set using `set_public_point` because it is
187 * not possible to infer the public key from a PKCS#11 EC private key
188 *
189 * @return the public point of the private key
190 * @throws Exception if the public point was not set using set_public_point()
191 */
192 EC_AffinePoint public_ec_point() const {
193 if(m_public_key) {
194 return m_public_key.value();
195 } else {
196 throw Invalid_State(
197 "Public point not set. Inferring the public key from a PKCS#11 ec private key is not possible.");
198 }
199 }
200
201 /// @return the encoding format for the public point when it is passed to cryptoki functions as an argument
202 PublicPointEncoding point_encoding() const { return m_point_encoding; }
203
204 // Private_Key methods
205
206 std::vector<uint8_t> raw_public_key_bits() const override;
207
208 std::vector<uint8_t> public_key_bits() const override;
209
210 std::size_t key_length() const override;
211
212 std::size_t estimated_strength() const override;
213
214 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
215
216 AlgorithmIdentifier algorithm_identifier() const override;
217
218 private:
219 EC_Group m_domain_params;
220 std::optional<EC_AffinePoint> m_public_key;
221 PublicPointEncoding m_point_encoding = PublicPointEncoding::Der;
222};
223} // namespace Botan::PKCS11
224
225#endif
226
227#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
Common attributes of all private keys.
Definition p11_object.h:350
Common attributes of all public key objects.
Definition p11_object.h:305
Represents a PKCS#11 session.
Definition p11_types.h:139
PublicPointEncoding
Definition p11.h:1184