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/workfactor.h>
24 return group.OS2ECP(ec_point);
28EC_PublicKeyGenerationProperties::EC_PublicKeyGenerationProperties(
const std::vector<uint8_t>& ec_params) :
29 PublicKeyProperties(
KeyType::
Ec), m_ec_params(ec_params) {
30 add_binary(AttributeType::EcParams, m_ec_params);
33EC_PublicKeyImportProperties::EC_PublicKeyImportProperties(
const std::vector<uint8_t>& ec_params,
34 const std::vector<uint8_t>& ec_point) :
35 PublicKeyProperties(
KeyType::
Ec), m_ec_params(ec_params), m_ec_point(ec_point) {
36 add_binary(AttributeType::EcParams, m_ec_params);
37 add_binary(AttributeType::EcPoint, m_ec_point);
40PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session, ObjectHandle handle) : Object(session, handle) {
41 secure_vector<uint8_t> ec_parameters = get_attribute_value(AttributeType::EcParams);
42 m_domain_params = EC_Group(
unlock(ec_parameters));
43 m_public_key = decode_public_point(get_attribute_value(AttributeType::EcPoint), m_domain_params);
44 m_domain_encoding = EC_Group_Encoding::Explicit;
47PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session,
const EC_PublicKeyImportProperties& props) :
48 Object(session, props) {
49 m_domain_params = EC_Group(props.ec_params());
51 secure_vector<uint8_t> ec_point;
52 BER_Decoder(props.ec_point()).decode(ec_point, ASN1_Type::OctetString);
53 m_public_key = m_domain_params.OS2ECP(ec_point);
54 m_domain_encoding = EC_Group_Encoding::Explicit;
57EC_PrivateKeyImportProperties::EC_PrivateKeyImportProperties(
const std::vector<uint8_t>& ec_params,
58 const BigInt& value) :
59 PrivateKeyProperties(
KeyType::
Ec), m_ec_params(ec_params), m_value(value) {
60 add_binary(AttributeType::EcParams, m_ec_params);
61 add_binary(AttributeType::Value, m_value.serialize());
64PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session, ObjectHandle handle) :
65 Object(session, handle), m_domain_params(), m_public_key() {
66 secure_vector<uint8_t> ec_parameters = get_attribute_value(AttributeType::EcParams);
67 m_domain_params = EC_Group(
unlock(ec_parameters));
70PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session,
const EC_PrivateKeyImportProperties& props) :
71 Object(session, props) {
72 m_domain_params = EC_Group(props.ec_params());
75PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session,
76 const std::vector<uint8_t>& ec_params,
77 const EC_PrivateKeyGenerationProperties& props) :
79 m_domain_params = EC_Group(ec_params);
81 EC_PublicKeyGenerationProperties pub_key_props(ec_params);
82 pub_key_props.set_verify(
true);
83 pub_key_props.set_private(
false);
84 pub_key_props.set_token(
false);
89 session.module()->C_GenerateKeyPair(session.handle(),
92 static_cast<Ulong>(pub_key_props.count()),
94 static_cast<Ulong>(props.count()),
98 this->reset_handle(priv_key_handle);
100 Object public_key(session, pub_key_handle);
101 m_public_key = decode_public_point(public_key.get_attribute_value(AttributeType::EcPoint), m_domain_params);
104size_t PKCS11_EC_PrivateKey::key_length()
const {
105 return m_domain_params.get_order_bits();
108std::vector<uint8_t> PKCS11_EC_PrivateKey::raw_public_key_bits()
const {
109 return public_point().encode(EC_Point_Format::Compressed);
112std::vector<uint8_t> PKCS11_EC_PrivateKey::public_key_bits()
const {
113 return public_point().encode(EC_Point_Format::Compressed);
116size_t PKCS11_EC_PrivateKey::estimated_strength()
const {
120bool PKCS11_EC_PrivateKey::check_key(RandomNumberGenerator& ,
bool )
const {
121 return m_public_key.on_the_curve();
124AlgorithmIdentifier PKCS11_EC_PrivateKey::algorithm_identifier()
const {
125 return AlgorithmIdentifier(object_identifier(), domain().DER_encode(EC_Group_Encoding::Explicit));
CK_OBJECT_HANDLE ObjectHandle
size_t ecp_work_factor(size_t bits)
std::vector< T > unlock(const secure_vector< T > &in)
std::vector< T, secure_allocator< T > > secure_vector
#define CK_INVALID_HANDLE
#define CKM_EC_KEY_PAIR_GEN