Botan 3.6.1
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) :
34 EC_PublicKey(), PKCS11_EC_PublicKey(session, handle) {}
35
36 /**
37 * Imports a ECDH public key
38 * @param session the session to use
39 * @param props the attributes of the public key
40 */
41 PKCS11_ECDH_PublicKey(Session& session, const EC_PublicKeyImportProperties& props) :
42 EC_PublicKey(), PKCS11_EC_PublicKey(session, props) {}
43
44 inline std::string algo_name() const override { return "ECDH"; }
45
46 /**
47 * @throws Not_Implemented
48 */
49 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator&) const final {
50 throw Not_Implemented("Cannot generate a new PKCS#11 ECDH keypair from this public key");
51 }
52
53 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
54
55 /// @return the exported ECDH public key
56 ECDH_PublicKey export_key() const;
57};
58
59/// Represents a PKCS#11 ECDH private key
60
63
64class BOTAN_PUBLIC_API(2, 0) PKCS11_ECDH_PrivateKey final : public virtual PKCS11_EC_PrivateKey,
65 public virtual PK_Key_Agreement_Key {
66 public:
67 /**
68 * Creates a PKCS11_ECDH_PrivateKey object from an existing PKCS#11 ECDH private key
69 * @param session the session to use
70 * @param handle the handle of the ECDH private key
71 */
72 PKCS11_ECDH_PrivateKey(Session& session, ObjectHandle handle) : PKCS11_EC_PrivateKey(session, handle) {}
73
74 /**
75 * Imports an ECDH private key
76 * @param session the session to use
77 * @param props the attributes of the private key
78 */
79 PKCS11_ECDH_PrivateKey(Session& session, const EC_PrivateKeyImportProperties& props) :
80 PKCS11_EC_PrivateKey(session, props) {}
81
82 /**
83 * Generates a PKCS#11 ECDH private key
84 * @param session the session to use
85 * @param ec_params DER-encoding of an ANSI X9.62 Parameters value
86 * @param props the attributes of the private key
87 * @note no persistent public key object will be created
88 */
89 PKCS11_ECDH_PrivateKey(Session& session,
90 const std::vector<uint8_t>& ec_params,
91 const EC_PrivateKeyGenerationProperties& props) :
92 PKCS11_EC_PrivateKey(session, ec_params, props) {}
93
94 inline std::string algo_name() const override { return "ECDH"; }
95
96 std::unique_ptr<Public_Key> public_key() const override;
97
98 inline std::vector<uint8_t> public_value() const override {
99 return public_point().encode(EC_Point_Format::Uncompressed);
100 }
101
102 /// @return the exported ECDH private key
103 ECDH_PrivateKey export_key() const;
104
105 secure_vector<uint8_t> private_key_bits() const override;
106
107 /**
108 * @throws Not_Implemented
109 */
110 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator&) const override {
111 throw Not_Implemented("Cannot generate a new PKCS#11 ECDH keypair from this private key");
112 }
113
114 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
115
116 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
117 std::string_view params,
118 std::string_view provider) const override;
119};
120
122
123using PKCS11_ECDH_KeyPair = std::pair<PKCS11_ECDH_PublicKey, PKCS11_ECDH_PrivateKey>;
124
125/**
126* PKCS#11 ECDH key pair generation
127* @param session the session that should be used for the key generation
128* @param pub_props the properties of the public key
129* @param priv_props the properties of the private key
130*/
132PKCS11_ECDH_KeyPair generate_ecdh_keypair(Session& session,
133 const EC_PublicKeyGenerationProperties& pub_props,
134 const EC_PrivateKeyGenerationProperties& priv_props);
135} // namespace Botan::PKCS11
136
137#endif
138#endif
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition compiler.h:191
#define BOTAN_DIAGNOSTIC_PUSH
Definition compiler.h:188
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition compiler.h:190
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31