Botan 3.14.1
Crypto and TLS for C&
p11_ecc_key.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 ECC
3* (C) 2016 Daniel Neus, Sirrix AG
4* (C) 2016 Philipp Weber, Sirrix AG
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/p11_ecc_key.h>
10
11#include <botan/pk_keys.h>
12
13#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
14
15 #include <botan/assert.h>
16 #include <botan/ber_dec.h>
17 #include <botan/internal/ec_key_data.h>
18 #include <botan/internal/p11_object_guard.h>
19 #include <botan/internal/workfactor.h>
20
21namespace Botan::PKCS11 {
22
23namespace {
24
25/// Converts a DER-encoded ANSI X9.62 ECPoint to EC_Point
26EC_AffinePoint decode_public_point(const EC_Group& group, std::span<const uint8_t> ec_point_data) {
27 std::vector<uint8_t> ec_point;
28 BER_Decoder(ec_point_data, BER_Decoder::Limits::DER()).decode(ec_point, ASN1_Type::OctetString).verify_end();
29 // Throws if invalid
30 return EC_AffinePoint(group, ec_point);
31}
32
33// Validate before creating the token object, so invalid input leaves nothing behind
34const EC_PrivateKeyImportProperties& check_ec_params(const EC_PrivateKeyImportProperties& props) {
35 const EC_Group group(props.ec_params());
36 BOTAN_UNUSED(group);
37 return props; // NOLINT(*-return-const-ref-from-parameter)
38}
39
40} // namespace
41
42EC_PublicKeyGenerationProperties::EC_PublicKeyGenerationProperties(const std::vector<uint8_t>& ec_params) :
43 PublicKeyProperties(KeyType::Ec), m_ec_params(ec_params) {
44 add_binary(AttributeType::EcParams, m_ec_params);
45}
46
47EC_PublicKeyImportProperties::EC_PublicKeyImportProperties(const std::vector<uint8_t>& ec_params,
48 const std::vector<uint8_t>& ec_point) :
49 PublicKeyProperties(KeyType::Ec), m_ec_params(ec_params), m_ec_point(ec_point) {
50 add_binary(AttributeType::EcParams, m_ec_params);
51 add_binary(AttributeType::EcPoint, m_ec_point);
52}
53
54PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session, ObjectHandle handle) : Object(session, handle) {
55 auto ec_parameters = get_attribute_value(AttributeType::EcParams);
56 auto pt_bytes = get_attribute_value(AttributeType::EcPoint);
57
58 EC_Group group(ec_parameters);
59 auto pt = decode_public_point(group, pt_bytes);
60 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
61}
62
63PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session, const EC_PublicKeyImportProperties& props) :
64 Object(session, props) {
65 Object_Creation_Guard guard(session, {handle()});
66 EC_Group group(props.ec_params());
67 auto pt = decode_public_point(group, props.ec_point());
68 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
69 guard.release();
70}
71
72EC_PrivateKeyImportProperties::EC_PrivateKeyImportProperties(const std::vector<uint8_t>& ec_params,
73 const BigInt& value) :
74 PrivateKeyProperties(KeyType::Ec), m_ec_params(ec_params), m_value(value) {
75 add_binary(AttributeType::EcParams, m_ec_params);
76 add_binary(AttributeType::Value, m_value.serialize());
77}
78
79PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session, ObjectHandle handle) :
80 Object(session, handle), m_domain_params(get_attribute_value(AttributeType::EcParams)) {}
81
82PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session, const EC_PrivateKeyImportProperties& props) :
83 Object(session, check_ec_params(props)), m_domain_params(EC_Group(props.ec_params())) {}
84
85PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session,
86 const std::vector<uint8_t>& ec_params,
87 const EC_PrivateKeyGenerationProperties& props) :
88 Object(session), m_domain_params(ec_params) {
89 EC_PublicKeyGenerationProperties pub_key_props(ec_params);
90 pub_key_props.set_verify(true);
91 pub_key_props.set_private(false);
92 pub_key_props.set_token(false); // don't create a persistent public key object
93
94 ObjectHandle pub_key_handle = CK_INVALID_HANDLE;
95 ObjectHandle priv_key_handle = CK_INVALID_HANDLE;
96 const Mechanism mechanism = {CKM_EC_KEY_PAIR_GEN, nullptr, 0};
97 session.module()->C_GenerateKeyPair(session.handle(),
98 &mechanism,
99 pub_key_props.data(),
100 checked_ulong_cast(pub_key_props.count()),
101 props.data(),
102 checked_ulong_cast(props.count()),
103 &pub_key_handle,
104 &priv_key_handle);
105
106 // The public key object is only needed temporarily
107 const Object_Creation_Guard destroy_public(session, {pub_key_handle});
108 Object_Creation_Guard guard(session, {priv_key_handle});
109
110 this->reset_handle(priv_key_handle);
111 const Object public_key(session, pub_key_handle);
112
113 auto pt_bytes = public_key.get_attribute_value(AttributeType::EcPoint);
114 m_public_key = decode_public_point(m_domain_params, pt_bytes);
115 guard.release();
116}
117
118size_t PKCS11_EC_PrivateKey::key_length() const {
119 return m_domain_params.get_order_bits();
120}
121
122std::vector<uint8_t> PKCS11_EC_PrivateKey::raw_public_key_bits() const {
123 // Matches the default of software EC keys, and public_value() for ECDH
124 return public_ec_point().serialize_uncompressed();
125}
126
127std::vector<uint8_t> PKCS11_EC_PrivateKey::public_key_bits() const {
128 return raw_public_key_bits();
129}
130
131size_t PKCS11_EC_PrivateKey::estimated_strength() const {
132 return ecp_work_factor(key_length());
133}
134
135bool PKCS11_EC_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
136 return true;
137}
138
139AlgorithmIdentifier PKCS11_EC_PrivateKey::algorithm_identifier() const {
140 return AlgorithmIdentifier(object_identifier(), domain().DER_encode());
141}
142} // namespace Botan::PKCS11
143
144#endif
#define BOTAN_UNUSED
Definition assert.h:151
static Limits DER()
Definition ber_dec.h:44
Common attributes of all public key objects.
Definition p11_object.h:305
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
AttributeType
Definition p11.h:50
CK_MECHANISM Mechanism
Definition p11.h:1207
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:1214
size_t ecp_work_factor(size_t bits)
#define CK_INVALID_HANDLE
Definition pkcs11.h:35
#define CKM_EC_KEY_PAIR_GEN
Definition pkcs11.h:861