Botan 3.7.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/ber_dec.h>
16 #include <botan/internal/ec_key_data.h>
17 #include <botan/internal/workfactor.h>
18
19namespace Botan::PKCS11 {
20
21namespace {
22
23/// Converts a DER-encoded ANSI X9.62 ECPoint to EC_Point
24EC_AffinePoint decode_public_point(const EC_Group& group, std::span<const uint8_t> ec_point_data) {
25 std::vector<uint8_t> ec_point;
26 BER_Decoder(ec_point_data).decode(ec_point, ASN1_Type::OctetString);
27 // Throws if invalid
28 return EC_AffinePoint(group, ec_point);
29}
30
31} // namespace
32
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);
36}
37
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);
43}
44
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);
48
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));
52}
53
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));
59}
60
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());
66}
67
68PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session, ObjectHandle handle) :
69 Object(session, handle), m_domain_params(get_attribute_value(AttributeType::EcParams)) {}
70
71PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session, const EC_PrivateKeyImportProperties& props) :
72 Object(session, props), m_domain_params(EC_Group(props.ec_params())) {}
73
74PKCS11_EC_PrivateKey::PKCS11_EC_PrivateKey(Session& session,
75 const std::vector<uint8_t>& ec_params,
76 const EC_PrivateKeyGenerationProperties& props) :
77 Object(session), m_domain_params(ec_params) {
78 EC_PublicKeyGenerationProperties pub_key_props(ec_params);
79 pub_key_props.set_verify(true);
80 pub_key_props.set_private(false);
81 pub_key_props.set_token(false); // don't create a persistent public key object
82
83 ObjectHandle pub_key_handle = CK_INVALID_HANDLE;
84 ObjectHandle priv_key_handle = CK_INVALID_HANDLE;
85 Mechanism mechanism = {CKM_EC_KEY_PAIR_GEN, nullptr, 0};
86 session.module()->C_GenerateKeyPair(session.handle(),
87 &mechanism,
88 pub_key_props.data(),
89 static_cast<Ulong>(pub_key_props.count()),
90 props.data(),
91 static_cast<Ulong>(props.count()),
92 &pub_key_handle,
93 &priv_key_handle);
94
95 this->reset_handle(priv_key_handle);
96 Object public_key(session, pub_key_handle);
97
98 auto pt_bytes = public_key.get_attribute_value(AttributeType::EcPoint);
99 m_public_key = decode_public_point(m_domain_params, pt_bytes);
100}
101
102size_t PKCS11_EC_PrivateKey::key_length() const {
103 return m_domain_params.get_order_bits();
104}
105
106std::vector<uint8_t> PKCS11_EC_PrivateKey::raw_public_key_bits() const {
107 return public_ec_point().serialize_compressed();
108}
109
110std::vector<uint8_t> PKCS11_EC_PrivateKey::public_key_bits() const {
111 return raw_public_key_bits();
112}
113
114size_t PKCS11_EC_PrivateKey::estimated_strength() const {
115 return ecp_work_factor(key_length());
116}
117
118bool PKCS11_EC_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
119 return true;
120}
121
122AlgorithmIdentifier PKCS11_EC_PrivateKey::algorithm_identifier() const {
123 return AlgorithmIdentifier(object_identifier(), domain().DER_encode());
124}
125} // namespace Botan::PKCS11
126
127#endif
AttributeType
Definition p11.h:61
CK_MECHANISM Mechanism
Definition p11.h:819
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:826
CK_ULONG Ulong
Definition p11.h:816
size_t ecp_work_factor(size_t bits)
#define CK_INVALID_HANDLE
Definition pkcs11t.h:75
#define CKM_EC_KEY_PAIR_GEN
Definition pkcs11t.h:889