9 #include <botan/p11_ecdh.h>
11 #if defined(BOTAN_HAS_ECDH)
13 #include <botan/internal/p11_mechanism.h>
14 #include <botan/ber_dec.h>
15 #include <botan/der_enc.h>
16 #include <botan/pk_ops.h>
17 #include <botan/rng.h>
23 ECDH_PublicKey PKCS11_ECDH_PublicKey::export_key()
const
25 return ECDH_PublicKey(domain(), public_point());
28 ECDH_PrivateKey PKCS11_ECDH_PrivateKey::export_key()
const
36 secure_vector<uint8_t> PKCS11_ECDH_PrivateKey::private_key_bits()
const
38 return export_key().private_key_bits();
42 class PKCS11_ECDH_KA_Operation :
public PK_Ops::Key_Agreement
45 PKCS11_ECDH_KA_Operation(
const PKCS11_EC_PrivateKey& key,
const std::string& params)
46 : PK_Ops::Key_Agreement(),
m_key(key), m_mechanism(MechanismWrapper::create_ecdh_mechanism(params))
52 secure_vector<uint8_t> agree(
size_t key_len,
const uint8_t other_key[],
size_t other_key_len,
const uint8_t salt[],
53 size_t salt_len)
override
55 std::vector<uint8_t> der_encoded_other_key;
58 der_encoded_other_key = DER_Encoder().encode(other_key, other_key_len,
OCTET_STRING).get_contents_unlocked();
59 m_mechanism.set_ecdh_other_key(der_encoded_other_key.data(), der_encoded_other_key.size());
63 m_mechanism.set_ecdh_other_key(other_key, other_key_len);
66 if(salt !=
nullptr && salt_len > 0)
68 m_mechanism.set_ecdh_salt(salt, salt_len);
72 AttributeContainer attributes;
78 m_key.module()->C_DeriveKey(
m_key.session().handle(), m_mechanism.data(),
m_key.handle(), attributes.data(),
79 attributes.count(), &secret_handle);
81 Object secret_object(
m_key.session(), secret_handle);
83 if(secret.size() < key_len)
85 throw PKCS11_Error(
"ECDH key derivation secret length is too short");
87 secret.resize(key_len);
92 const PKCS11_EC_PrivateKey&
m_key;
93 MechanismWrapper m_mechanism;
98 std::unique_ptr<PK_Ops::Key_Agreement>
99 PKCS11_ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator&,
100 const std::string& params,
101 const std::string& )
const
103 return std::unique_ptr<PK_Ops::Key_Agreement>(
new PKCS11_ECDH_KA_Operation(*
this, params));
106 PKCS11_ECDH_KeyPair generate_ecdh_keypair(Session& session,
const EC_PublicKeyGenerationProperties& pub_props,
107 const EC_PrivateKeyGenerationProperties& priv_props)
114 session.module()->C_GenerateKeyPair(session.handle(), &mechanism,
115 pub_props.data(), pub_props.count(), priv_props.data(), priv_props.count(),
116 &pub_key_handle, &priv_key_handle);
118 return std::make_pair(PKCS11_ECDH_PublicKey(session, pub_key_handle), PKCS11_ECDH_PrivateKey(session, priv_key_handle));
CK_ULONG CK_MECHANISM_TYPE
CK_OBJECT_HANDLE ObjectHandle
const TPM_PrivateKey & m_key
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)