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