Botan 3.9.0
Crypto and TLS for C&
p11_ecdh.h
Go to the documentation of this file.
1/*
2* PKCS#11 ECDH
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_ECDH_H_
10#define BOTAN_P11_ECDH_H_
11
12#include <botan/p11.h>
13
14#if defined(BOTAN_HAS_ECDH)
15
16 #include <botan/ecdh.h>
17 #include <botan/p11_ecc_key.h>
18
19 #include <string>
20 #include <vector>
21
22namespace Botan::PKCS11 {
23class Session;
24
25/// Represents a PKCS#11 ECDH public key
26class BOTAN_PUBLIC_API(2, 0) PKCS11_ECDH_PublicKey : public PKCS11_EC_PublicKey {
27 public:
28 /**
29 * Create a PKCS11_ECDH_PublicKey object from an existing PKCS#11 ECDH public key
30 * @param session the session to use
31 * @param handle the handle of the ECDH public key
32 */
33 PKCS11_ECDH_PublicKey(Session& session, ObjectHandle handle) : PKCS11_EC_PublicKey(session, handle) {}
34
35 /**
36 * Imports a ECDH public key
37 * @param session the session to use
38 * @param props the attributes of the public key
39 */
40 PKCS11_ECDH_PublicKey(Session& session, const EC_PublicKeyImportProperties& props) :
41 PKCS11_EC_PublicKey(session, props) {}
42
43 inline std::string algo_name() const override { return "ECDH"; }
44
45 /**
46 * @throws Not_Implemented as this operation is not possible in PKCS11
47 */
48 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& /*rng*/) const final {
49 throw Not_Implemented("Cannot generate a new PKCS#11 ECDH keypair from this public key");
50 }
51
52 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
53
54 /// @return the exported ECDH public key
55 ECDH_PublicKey export_key() const;
56};
57
58/// Represents a PKCS#11 ECDH private key
59
62
63class BOTAN_PUBLIC_API(2, 0) PKCS11_ECDH_PrivateKey final : public virtual PKCS11_EC_PrivateKey,
64 public virtual PK_Key_Agreement_Key {
65 public:
66 /**
67 * Creates a PKCS11_ECDH_PrivateKey object from an existing PKCS#11 ECDH private key
68 * @param session the session to use
69 * @param handle the handle of the ECDH private key
70 */
71 PKCS11_ECDH_PrivateKey(Session& session, ObjectHandle handle) : PKCS11_EC_PrivateKey(session, handle) {}
72
73 /**
74 * Imports an ECDH private key
75 * @param session the session to use
76 * @param props the attributes of the private key
77 */
78 PKCS11_ECDH_PrivateKey(Session& session, const EC_PrivateKeyImportProperties& props) :
79 PKCS11_EC_PrivateKey(session, props) {}
80
81 /**
82 * Generates a PKCS#11 ECDH private key
83 * @param session the session to use
84 * @param ec_params DER-encoding of an ANSI X9.62 Parameters value
85 * @param props the attributes of the private key
86 * @note no persistent public key object will be created
87 */
88 PKCS11_ECDH_PrivateKey(Session& session,
89 const std::vector<uint8_t>& ec_params,
90 const EC_PrivateKeyGenerationProperties& props) :
91 PKCS11_EC_PrivateKey(session, ec_params, props) {}
92
93 inline std::string algo_name() const override { return "ECDH"; }
94
95 std::unique_ptr<Public_Key> public_key() const override;
96
97 inline std::vector<uint8_t> public_value() const override { return public_ec_point().serialize_uncompressed(); }
98
99 /// @return the exported ECDH private key
100 ECDH_PrivateKey export_key() const;
101
102 secure_vector<uint8_t> private_key_bits() const override;
103
104 /**
105 * @throws Not_Implemented as this operation is not possible in PKCS11
106 */
107 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& /*rng*/) const override {
108 throw Not_Implemented("Cannot generate a new PKCS#11 ECDH keypair from this private key");
109 }
110
111 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
112
113 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
114 std::string_view params,
115 std::string_view provider) const override;
116};
117
119
120using PKCS11_ECDH_KeyPair = std::pair<PKCS11_ECDH_PublicKey, PKCS11_ECDH_PrivateKey>;
121
122/**
123* PKCS#11 ECDH key pair generation
124* @param session the session that should be used for the key generation
125* @param pub_props the properties of the public key
126* @param priv_props the properties of the private key
127*/
129PKCS11_ECDH_KeyPair generate_ecdh_keypair(Session& session,
130 const EC_PublicKeyGenerationProperties& pub_props,
131 const EC_PrivateKeyGenerationProperties& priv_props);
132} // namespace Botan::PKCS11
133
134#endif
135#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