Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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