Botan 3.14.1
Crypto and TLS for C&
p11_ecdsa.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 ECDSA
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_ecdsa.h>
10
11#if defined(BOTAN_HAS_ECDSA)
12
13 #include <botan/assert.h>
14 #include <botan/p11_mechanism.h>
15 #include <botan/pk_ops.h>
16 #include <botan/pk_options_readers.h>
17 #include <botan/rng.h>
18 #include <botan/internal/keypair.h>
19 #include <botan/internal/p11_object_guard.h>
20 #include <botan/internal/p11_sig_stream.h>
21 #include <botan/internal/pk_options_impl.h>
22
23namespace Botan::PKCS11 {
24
25ECDSA_PublicKey PKCS11_ECDSA_PublicKey::export_key() const {
26 return ECDSA_PublicKey(domain(), public_ec_point());
27}
28
29bool PKCS11_ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
30 if(!strong) {
31 return true;
32 }
33
34 const ECDSA_PublicKey pubkey(domain(), public_ec_point());
35 return KeyPair::signature_consistency_check(rng, *this, pubkey, "SHA-256");
36}
37
38ECDSA_PrivateKey PKCS11_ECDSA_PrivateKey::export_key() const {
39 auto priv_key = get_attribute_value(AttributeType::Value);
40
41 Null_RNG rng;
42 return ECDSA_PrivateKey(rng, domain(), BigInt::from_bytes(priv_key));
43}
44
45secure_vector<uint8_t> PKCS11_ECDSA_PrivateKey::private_key_bits() const {
46 return export_key().private_key_bits();
47}
48
49std::unique_ptr<Public_Key> PKCS11_ECDSA_PrivateKey::public_key() const {
50 return std::make_unique<ECDSA_PublicKey>(domain(), public_ec_point());
51}
52
53namespace {
54
55/*
56* The mechanism hashes the input itself, unless the caller provides the
57* digest, in which case the plain CKM_ECDSA mechanism is used
58*/
59std::string p11_ecdsa_mechanism_hash(const PK_Signature_Options_Reader& options) {
60 if(options.using_externally_computed_prehash()) {
61 return "Raw";
62 }
63 return options.hash_function_name();
64}
65
66std::string p11_ecdsa_hash_name(const PK_Signature_Options_Reader& options, const MechanismWrapper& mechanism) {
67 if(options.using_externally_computed_prehash()) {
68 return externally_computed_prehash_name(options).value_or("Raw");
69 }
70
71 // Derived from the mechanism so that aliases accepted by
72 // create_ecdsa_mechanism are reported under their canonical name
73 switch(mechanism.mechanism_type()) {
75 return "SHA-1";
77 return "SHA-224";
79 return "SHA-256";
81 return "SHA-384";
83 return "SHA-512";
84 default:
85 return "Raw";
86 }
87}
88
89class PKCS11_ECDSA_Signature_Operation final : public PK_Ops::Signature {
90 public:
91 PKCS11_ECDSA_Signature_Operation(const PKCS11_ECDSA_PrivateKey& key, const PK_Signature_Options_Reader& options) :
92 PK_Ops::Signature(),
93 m_key(key),
94 m_order_bytes(key.domain().get_order_bytes()),
95 m_mechanism(MechanismWrapper::create_ecdsa_mechanism(p11_ecdsa_mechanism_hash(options))),
96 m_hash(p11_ecdsa_hash_name(options, m_mechanism)),
97 m_stream(Signature_Stream::Direction::Sign, m_key, m_mechanism) {}
98
99 void update(std::span<const uint8_t> input) override { m_stream.update(input); }
100
101 std::vector<uint8_t> sign(RandomNumberGenerator& /*rng*/) override {
102 auto signature = m_stream.sign();
103 if(signature.size() != signature_length()) {
104 throw PKCS11_Error("PKCS #11 module returned an ECDSA signature of unexpected length");
105 }
106 return signature;
107 }
108
109 size_t signature_length() const override { return 2 * m_order_bytes; }
110
111 AlgorithmIdentifier algorithm_identifier() const override;
112
113 std::string hash_function() const override { return m_hash; }
114
115 private:
116 const PKCS11_ECDSA_PrivateKey m_key;
117 const size_t m_order_bytes;
118 MechanismWrapper m_mechanism;
119 const std::string m_hash;
120 Signature_Stream m_stream;
121};
122
123AlgorithmIdentifier PKCS11_ECDSA_Signature_Operation::algorithm_identifier() const {
124 const std::string full_name = "ECDSA/" + hash_function();
125 const OID oid = OID::from_string(full_name);
126 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
127}
128
129class PKCS11_ECDSA_Verification_Operation final : public PK_Ops::Verification {
130 public:
131 PKCS11_ECDSA_Verification_Operation(const PKCS11_ECDSA_PublicKey& key,
132 const PK_Signature_Options_Reader& options) :
133 PK_Ops::Verification(),
134 m_key(key),
135 m_mechanism(MechanismWrapper::create_ecdsa_mechanism(p11_ecdsa_mechanism_hash(options))),
136 m_hash(p11_ecdsa_hash_name(options, m_mechanism)),
137 m_stream(Signature_Stream::Direction::Verify, m_key, m_mechanism) {}
138
139 void update(std::span<const uint8_t> input) override { m_stream.update(input); }
140
141 bool is_valid_signature(std::span<const uint8_t> sig) override { return m_stream.verify(sig); }
142
143 std::string hash_function() const override { return m_hash; }
144
145 private:
146 const PKCS11_ECDSA_PublicKey m_key;
147 MechanismWrapper m_mechanism;
148 const std::string m_hash;
149 Signature_Stream m_stream;
150};
151
152} // namespace
153
154std::unique_ptr<PK_Ops::Verification> PKCS11_ECDSA_PublicKey::_create_verification_op(
155 const PK_Signature_Options_Reader& options) const {
156 require_hardware_provider(options, algo_name(), "pkcs11");
157 return std::make_unique<PKCS11_ECDSA_Verification_Operation>(*this, options);
158}
159
160std::unique_ptr<PK_Ops::Signature> PKCS11_ECDSA_PrivateKey::_create_signature_op(
161 RandomNumberGenerator& rng, const PK_Signature_Options_Reader& options) const {
162 BOTAN_UNUSED(rng);
163 require_hardware_provider(options, algo_name(), "pkcs11");
164 return std::make_unique<PKCS11_ECDSA_Signature_Operation>(*this, options);
165}
166
167PKCS11_ECDSA_KeyPair generate_ecdsa_keypair(Session& session,
168 const EC_PublicKeyGenerationProperties& pub_props,
169 const EC_PrivateKeyGenerationProperties& priv_props) {
170 ObjectHandle pub_key_handle = 0;
171 ObjectHandle priv_key_handle = 0;
172
173 const Mechanism mechanism = {static_cast<CK_MECHANISM_TYPE>(MechanismType::EcKeyPairGen), nullptr, 0};
174
175 session.module()->C_GenerateKeyPair(session.handle(),
176 &mechanism,
177 pub_props.data(),
178 checked_ulong_cast(pub_props.count()),
179 priv_props.data(),
180 checked_ulong_cast(priv_props.count()),
181 &pub_key_handle,
182 &priv_key_handle);
183
184 Object_Creation_Guard guard(session, {pub_key_handle, priv_key_handle});
185 PKCS11_ECDSA_PublicKey public_key(session, pub_key_handle); // NOLINT(*-const-correctness) clang-tidy bug
186 PKCS11_ECDSA_PrivateKey private_key(session, priv_key_handle);
187 // The private key object does not include the public point. Rebind it to
188 // the private key's domain since unregistered explicit groups are decoded
189 // independently for the two token objects.
190 const EC_AffinePoint public_point(private_key.domain(), public_key.raw_public_key_bits());
191 private_key.set_public_point(public_point, private_key.point_encoding());
192 guard.release();
193 return std::make_pair(std::move(public_key), std::move(private_key));
194}
195
196} // namespace Botan::PKCS11
197
198#endif
#define BOTAN_UNUSED
Definition assert.h:151
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:84
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
Represents a PKCS#11 session.
Definition p11_types.h:139
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
CK_MECHANISM Mechanism
Definition p11.h:1207
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:1214
void require_hardware_provider(const OptionsT &options, std::string_view algo_name, std::string_view provider_name)
std::optional< std::string > externally_computed_prehash_name(const PK_Signature_Options_Reader &options)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
CK_ULONG CK_MECHANISM_TYPE
Definition pkcs11.h:59