9#include <botan/p11_ecdh.h>
11#if defined(BOTAN_HAS_ECDH)
13 #include <botan/der_enc.h>
14 #include <botan/ec_apoint.h>
15 #include <botan/p11_mechanism.h>
16 #include <botan/pk_ops.h>
17 #include <botan/pk_options_readers.h>
18 #include <botan/rng.h>
19 #include <botan/internal/p11_object_guard.h>
20 #include <botan/internal/pk_options_impl.h>
21 #include <botan/internal/scoped_cleanup.h>
23 #if defined(BOTAN_HAS_LEGACY_EC_POINT)
24 #include <botan/ec_point.h>
29ECDH_PublicKey PKCS11_ECDH_PublicKey::export_key()
const {
30 return ECDH_PublicKey(domain(), public_ec_point());
33ECDH_PrivateKey PKCS11_ECDH_PrivateKey::export_key()
const {
40std::unique_ptr<Public_Key> PKCS11_ECDH_PrivateKey::public_key()
const {
41 return std::make_unique<ECDH_PublicKey>(domain(), public_ec_point());
45 return export_key().private_key_bits();
49class PKCS11_ECDH_KA_Operation final :
public PK_Ops::Key_Agreement {
51 PKCS11_ECDH_KA_Operation(
const PKCS11_ECDH_PrivateKey& key, std::string_view params) :
52 PK_Ops::Key_Agreement(), m_key(key), m_mechanism(MechanismWrapper::create_ecdh_mechanism(params)) {}
54 size_t agreed_value_size()
const override {
return m_key.domain().get_p_bytes(); }
58 secure_vector<uint8_t> agree(
size_t key_len,
59 std::span<const uint8_t> other_key,
60 std::span<const uint8_t> salt)
override {
61 const auto peer_point = EC_AffinePoint::deserialize(m_key.domain(), other_key);
63 throw Decoding_Error(
"ECDH - Invalid elliptic curve point: not on curve");
65 if(peer_point->is_identity()) {
66 throw Decoding_Error(
"ECDH - Invalid elliptic curve point: identity");
75 if(m_key.domain().has_cofactor()) {
76 #if defined(BOTAN_HAS_LEGACY_EC_POINT)
77 if(!(m_key.domain().get_order() * peer_point->to_legacy_point()).is_zero()) {
78 throw Decoding_Error(
"ECDH - Invalid elliptic curve point: not in the prime order subgroup");
81 throw Not_Implemented(
"PKCS#11 ECDH with a cofactor is not available in this build configuration");
85 std::vector<uint8_t> der_encoded_other_key;
86 if(m_key.point_encoding() == PublicPointEncoding::Der) {
87 DER_Encoder(der_encoded_other_key).encode(other_key.data(), other_key.size(), ASN1_Type::OctetString);
88 m_mechanism.set_ecdh_other_key(der_encoded_other_key.data(), der_encoded_other_key.size());
90 m_mechanism.set_ecdh_other_key(other_key.data(), other_key.size());
93 const bool raw_kdf = (m_mechanism.ecdh_kdf() == KeyDerivation::Null);
95 if(raw_kdf && !salt.empty()) {
96 throw Invalid_Argument(
"PK_Key_Agreement::derive_key requires a KDF to use a salt");
100 m_mechanism.set_ecdh_salt(
nullptr, 0);
102 m_mechanism.set_ecdh_salt(salt.data(), salt.size());
105 const size_t out_len = raw_kdf ? agreed_value_size() : key_len;
108 AttributeContainer attributes;
109 attributes.add_bool(AttributeType::Sensitive,
false);
110 attributes.add_bool(AttributeType::Extractable,
true);
111 attributes.add_numeric(AttributeType::Class,
static_cast<CK_OBJECT_CLASS>(ObjectClass::SecretKey));
112 attributes.add_numeric(AttributeType::KeyType,
static_cast<CK_KEY_TYPE>(KeyType::GenericSecret));
114 m_key.module()->C_DeriveKey(m_key.session().handle(),
121 const Object secret_object(m_key.session(), secret_handle);
122 const auto destroy_secret = scoped_cleanup([&]()
noexcept {
124 secret_object.destroy();
128 secure_vector<uint8_t> secret = secret_object.get_attribute_value(AttributeType::Value);
129 if(secret.size() < out_len) {
130 throw PKCS11_Error(
"ECDH key derivation secret length is too short");
132 secret.resize(out_len);
137 PKCS11_ECDH_PrivateKey m_key;
138 MechanismWrapper m_mechanism;
143std::unique_ptr<PK_Ops::Key_Agreement> PKCS11_ECDH_PrivateKey::_create_key_agreement_op(
144 RandomNumberGenerator& ,
const PK_Key_Agreement_Options_Reader& options)
const {
159 const std::string params = [&]() -> std::string {
160 if(options.using_kdf()) {
161 const std::string& kdf = options.kdf().value();
163 throw Invalid_Argument(
"PKCS#11 ECDH does not accept Raw as a KDF; request the raw shared key instead");
167 if(options.using_raw_shared_key()) {
170 throw Invalid_Argument(
171 "Key agreement does not produce a uniform shared key, so a KDF must be specified (or the raw shared key "
172 "explicitly requested)");
174 return std::make_unique<PKCS11_ECDH_KA_Operation>(*
this, params);
177PKCS11_ECDH_KeyPair generate_ecdh_keypair(
Session& session,
178 const EC_PublicKeyGenerationProperties& pub_props,
179 const EC_PrivateKeyGenerationProperties& priv_props) {
185 session.module()->C_GenerateKeyPair(session.handle(),
195 PKCS11_ECDH_PublicKey public_key(session, pub_key_handle);
196 PKCS11_ECDH_PrivateKey private_key(session, priv_key_handle);
200 const EC_AffinePoint public_point(private_key.domain(), public_key.raw_public_key_bits());
201 private_key.set_public_point(public_point, private_key.point_encoding());
203 return std::make_pair(std::move(public_key), std::move(private_key));
static BigInt from_bytes(std::span< const uint8_t > bytes)
Represents a PKCS#11 session.
Ulong checked_ulong_cast(size_t v)
CK_OBJECT_HANDLE ObjectHandle
void require_hardware_provider(const OptionsT &options, std::string_view algo_name, std::string_view provider_name)
std::vector< T, secure_allocator< T > > secure_vector
CK_ULONG CK_MECHANISM_TYPE