9#include <botan/p11_ecc_key.h>
11#include <botan/pk_keys.h>
13#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
15 #include <botan/ber_dec.h>
16 #include <botan/internal/ec_key_data.h>
17 #include <botan/internal/workfactor.h>
24EC_AffinePoint decode_public_point(
const EC_Group& group, std::span<const uint8_t> ec_point_data) {
25 std::vector<uint8_t> ec_point;
28 return EC_AffinePoint(group, ec_point);
33EC_PublicKeyGenerationProperties::EC_PublicKeyGenerationProperties(
const std::vector<uint8_t>& ec_params) :
34 PublicKeyProperties(
KeyType::
Ec), m_ec_params(ec_params) {
35 add_binary(AttributeType::EcParams, m_ec_params);
38EC_PublicKeyImportProperties::EC_PublicKeyImportProperties(
const std::vector<uint8_t>& ec_params,
39 const std::vector<uint8_t>& ec_point) :
40 PublicKeyProperties(
KeyType::
Ec), m_ec_params(ec_params), m_ec_point(ec_point) {
41 add_binary(AttributeType::EcParams, m_ec_params);
42 add_binary(AttributeType::EcPoint, m_ec_point);
45PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session, ObjectHandle handle) : Object(session, handle) {
46 auto ec_parameters = get_attribute_value(AttributeType::EcParams);
47 auto pt_bytes = get_attribute_value(AttributeType::EcPoint);
49 EC_Group group(ec_parameters);
50 auto pt = decode_public_point(group, pt_bytes);
51 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
54PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session,
const EC_PublicKeyImportProperties& props) :
55 Object(session, props) {
56 EC_Group group(props.ec_params());
57 auto pt = decode_public_point(group, props.ec_point());
58 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
61EC_PrivateKeyImportProperties::EC_PrivateKeyImportProperties(
const std::vector<uint8_t>& ec_params,
62 const BigInt& value) :
63 PrivateKeyProperties(
KeyType::
Ec), m_ec_params(ec_params), m_value(value) {
64 add_binary(AttributeType::EcParams, m_ec_params);
65 add_binary(AttributeType::Value, m_value.serialize());
68PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session, ObjectHandle handle) : Object(session, handle) {
69 secure_vector<uint8_t> ec_parameters = get_attribute_value(AttributeType::EcParams);
70 m_domain_params = EC_Group(
unlock(ec_parameters));
73PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session,
const EC_PrivateKeyImportProperties& props) :
74 Object(session, props) {
75 m_domain_params = EC_Group(props.ec_params());
78PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session,
79 const std::vector<uint8_t>& ec_params,
80 const EC_PrivateKeyGenerationProperties& props) :
82 m_domain_params = EC_Group(ec_params);
84 EC_PublicKeyGenerationProperties pub_key_props(ec_params);
85 pub_key_props.set_verify(
true);
86 pub_key_props.set_private(
false);
87 pub_key_props.set_token(
false);
92 session.module()->C_GenerateKeyPair(session.handle(),
95 static_cast<Ulong>(pub_key_props.count()),
97 static_cast<Ulong>(props.count()),
101 this->reset_handle(priv_key_handle);
102 Object public_key(session, pub_key_handle);
104 auto pt_bytes = public_key.get_attribute_value(AttributeType::EcPoint);
105 m_public_key = decode_public_point(m_domain_params, pt_bytes).to_legacy_point();
108size_t PKCS11_EC_PrivateKey::key_length()
const {
109 return m_domain_params.get_order_bits();
112std::vector<uint8_t> PKCS11_EC_PrivateKey::raw_public_key_bits()
const {
113 return public_point().encode(EC_Point_Format::Compressed);
116std::vector<uint8_t> PKCS11_EC_PrivateKey::public_key_bits()
const {
117 return public_point().encode(EC_Point_Format::Compressed);
120size_t PKCS11_EC_PrivateKey::estimated_strength()
const {
124bool PKCS11_EC_PrivateKey::check_key(RandomNumberGenerator& ,
bool )
const {
125 return m_public_key.on_the_curve();
128AlgorithmIdentifier PKCS11_EC_PrivateKey::algorithm_identifier()
const {
129 return AlgorithmIdentifier(object_identifier(), domain().DER_encode());
CK_OBJECT_HANDLE ObjectHandle
size_t ecp_work_factor(size_t bits)
std::vector< T > unlock(const secure_vector< T > &in)
#define CK_INVALID_HANDLE
#define CKM_EC_KEY_PAIR_GEN