9#include <botan/p11_ecdh.h>
11#if defined(BOTAN_HAS_ECDH)
13 #include <botan/der_enc.h>
14 #include <botan/pk_ops.h>
15 #include <botan/rng.h>
16 #include <botan/internal/p11_mechanism.h>
20ECDH_PublicKey PKCS11_ECDH_PublicKey::export_key()
const {
21 return ECDH_PublicKey(domain(), public_point());
24ECDH_PrivateKey PKCS11_ECDH_PrivateKey::export_key()
const {
31std::unique_ptr<Public_Key> PKCS11_ECDH_PrivateKey::public_key()
const {
32 return std::make_unique<ECDH_PublicKey>(domain(), public_point());
35secure_vector<uint8_t> PKCS11_ECDH_PrivateKey::private_key_bits()
const {
36 return export_key().private_key_bits();
40class PKCS11_ECDH_KA_Operation
final :
public PK_Ops::Key_Agreement {
42 PKCS11_ECDH_KA_Operation(
const PKCS11_EC_PrivateKey& key, std::string_view params) :
43 PK_Ops::Key_Agreement(), m_key(key), m_mechanism(MechanismWrapper::create_ecdh_mechanism(params)) {}
45 size_t agreed_value_size()
const override {
return m_key.domain().get_p_bytes(); }
49 secure_vector<uint8_t> agree(
size_t key_len,
50 const uint8_t other_key[],
53 size_t salt_len)
override {
54 std::vector<uint8_t> der_encoded_other_key;
55 if(m_key.point_encoding() == PublicPointEncoding::Der) {
56 DER_Encoder(der_encoded_other_key).encode(other_key, other_key_len, ASN1_Type::OctetString);
57 m_mechanism.set_ecdh_other_key(der_encoded_other_key.data(), der_encoded_other_key.size());
59 m_mechanism.set_ecdh_other_key(other_key, other_key_len);
62 if(salt !=
nullptr && salt_len > 0) {
63 m_mechanism.set_ecdh_salt(salt, salt_len);
67 AttributeContainer attributes;
68 attributes.add_bool(AttributeType::Sensitive,
false);
69 attributes.add_bool(AttributeType::Extractable,
true);
70 attributes.add_numeric(AttributeType::Class,
static_cast<CK_OBJECT_CLASS>(ObjectClass::SecretKey));
71 attributes.add_numeric(AttributeType::KeyType,
static_cast<CK_KEY_TYPE>(KeyType::GenericSecret));
72 attributes.add_numeric(AttributeType::ValueLen,
static_cast<CK_ULONG>(key_len));
73 m_key.module()->C_DeriveKey(m_key.session().handle(),
77 static_cast<Ulong>(attributes.count()),
80 Object secret_object(m_key.session(), secret_handle);
81 secure_vector<uint8_t> secret = secret_object.get_attribute_value(AttributeType::Value);
82 if(secret.size() < key_len) {
83 throw PKCS11_Error(
"ECDH key derivation secret length is too short");
85 secret.resize(key_len);
90 const PKCS11_EC_PrivateKey& m_key;
91 MechanismWrapper m_mechanism;
96std::unique_ptr<PK_Ops::Key_Agreement> PKCS11_ECDH_PrivateKey::create_key_agreement_op(
97 RandomNumberGenerator& , std::string_view params, std::string_view )
const {
98 return std::make_unique<PKCS11_ECDH_KA_Operation>(*
this, params);
101PKCS11_ECDH_KeyPair generate_ecdh_keypair(Session& session,
102 const EC_PublicKeyGenerationProperties& pub_props,
103 const EC_PrivateKeyGenerationProperties& priv_props) {
109 session.module()->C_GenerateKeyPair(session.handle(),
112 static_cast<Ulong>(pub_props.count()),
114 static_cast<Ulong>(priv_props.count()),
118 return std::make_pair(PKCS11_ECDH_PublicKey(session, pub_key_handle),
119 PKCS11_ECDH_PrivateKey(session, priv_key_handle));
static BigInt decode(const uint8_t buf[], size_t length)
int(* final)(unsigned char *, CTX *)
CK_OBJECT_HANDLE ObjectHandle
unsigned long int CK_ULONG
CK_ULONG CK_MECHANISM_TYPE