9#include <botan/p11_ecdsa.h>
11#if defined(BOTAN_HAS_ECDSA)
13 #include <botan/pk_ops.h>
14 #include <botan/rng.h>
15 #include <botan/internal/keypair.h>
16 #include <botan/internal/p11_mechanism.h>
20ECDSA_PublicKey PKCS11_ECDSA_PublicKey::export_key()
const {
21 return ECDSA_PublicKey(domain(), public_point());
24bool PKCS11_ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng,
bool strong)
const {
25 if(!public_point().on_the_curve()) {
33 ECDSA_PublicKey pubkey(domain(), public_point());
37ECDSA_PrivateKey PKCS11_ECDSA_PrivateKey::export_key()
const {
45 return export_key().private_key_bits();
48std::unique_ptr<Public_Key> PKCS11_ECDSA_PrivateKey::public_key()
const {
49 return std::make_unique<ECDSA_PublicKey>(domain(), public_point());
54class PKCS11_ECDSA_Signature_Operation
final :
public PK_Ops::Signature {
56 PKCS11_ECDSA_Signature_Operation(
const PKCS11_ECDSA_PrivateKey& key, std::string_view hash) :
59 m_order_bytes(key.domain().get_order_bytes()),
60 m_mechanism(MechanismWrapper::create_ecdsa_mechanism(hash)),
63 void update(std::span<const uint8_t> input)
override {
66 m_key.module()->C_SignInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
68 m_first_message.assign(input.begin(), input.end());
72 if(!m_first_message.empty()) {
74 m_key.module()->C_SignUpdate(m_key.session().handle(), m_first_message);
75 m_first_message.clear();
78 m_key.module()->C_SignUpdate(m_key.session().handle(), input.data(),
static_cast<Ulong>(input.size()));
81 std::vector<uint8_t> sign(RandomNumberGenerator& )
override {
82 std::vector<uint8_t> signature;
83 if(!m_first_message.empty()) {
85 m_key.module()->C_Sign(m_key.session().handle(), m_first_message, signature);
86 m_first_message.clear();
89 m_key.module()->C_SignFinal(m_key.session().handle(), signature);
91 m_initialized =
false;
95 size_t signature_length()
const override {
return 2 * m_order_bytes; }
97 AlgorithmIdentifier algorithm_identifier()
const override;
99 std::string hash_function()
const override {
return m_hash; }
102 const PKCS11_ECDSA_PrivateKey m_key;
103 const size_t m_order_bytes;
104 MechanismWrapper m_mechanism;
105 const std::string m_hash;
106 secure_vector<uint8_t> m_first_message;
107 bool m_initialized =
false;
110AlgorithmIdentifier PKCS11_ECDSA_Signature_Operation::algorithm_identifier()
const {
111 const std::string full_name =
"ECDSA/" + hash_function();
116class PKCS11_ECDSA_Verification_Operation
final :
public PK_Ops::Verification {
118 PKCS11_ECDSA_Verification_Operation(
const PKCS11_ECDSA_PublicKey& key, std::string_view hash) :
119 PK_Ops::Verification(),
121 m_mechanism(MechanismWrapper::create_ecdsa_mechanism(hash)),
124 void update(std::span<const uint8_t> input)
override {
127 m_key.module()->C_VerifyInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
128 m_initialized =
true;
129 m_first_message.assign(input.begin(), input.end());
133 if(!m_first_message.empty()) {
135 m_key.module()->C_VerifyUpdate(m_key.session().handle(), m_first_message);
136 m_first_message.clear();
139 m_key.module()->C_VerifyUpdate(m_key.session().handle(), input.data(),
static_cast<Ulong>(input.size()));
142 bool is_valid_signature(std::span<const uint8_t> sig)
override {
143 ReturnValue return_value = ReturnValue::SignatureInvalid;
144 if(!m_first_message.empty()) {
146 m_key.module()->C_Verify(m_key.session().handle(),
147 m_first_message.data(),
148 static_cast<Ulong>(m_first_message.size()),
150 static_cast<Ulong>(sig.size()),
152 m_first_message.clear();
155 m_key.module()->C_VerifyFinal(
156 m_key.session().handle(), sig.data(),
static_cast<Ulong>(sig.size()), &return_value);
158 m_initialized =
false;
159 if(return_value != ReturnValue::OK && return_value != ReturnValue::SignatureInvalid) {
160 throw PKCS11_ReturnError(return_value);
162 return return_value == ReturnValue::OK;
165 std::string hash_function()
const override {
return m_hash; }
168 const PKCS11_ECDSA_PublicKey m_key;
169 MechanismWrapper m_mechanism;
170 const std::string m_hash;
171 secure_vector<uint8_t> m_first_message;
172 bool m_initialized =
false;
177std::unique_ptr<PK_Ops::Verification> PKCS11_ECDSA_PublicKey::create_verification_op(
178 std::string_view params, std::string_view )
const {
179 return std::make_unique<PKCS11_ECDSA_Verification_Operation>(*
this, params);
182std::unique_ptr<PK_Ops::Signature> PKCS11_ECDSA_PrivateKey::create_signature_op(RandomNumberGenerator& ,
183 std::string_view params,
184 std::string_view )
const {
185 return std::make_unique<PKCS11_ECDSA_Signature_Operation>(*
this, params);
188PKCS11_ECDSA_KeyPair generate_ecdsa_keypair(Session& session,
189 const EC_PublicKeyGenerationProperties& pub_props,
190 const EC_PrivateKeyGenerationProperties& priv_props) {
196 session.module()->C_GenerateKeyPair(session.handle(),
199 static_cast<Ulong>(pub_props.count()),
201 static_cast<Ulong>(priv_props.count()),
205 return std::make_pair(PKCS11_ECDSA_PublicKey(session, pub_key_handle),
206 PKCS11_ECDSA_PrivateKey(session, priv_key_handle));
static BigInt from_bytes(std::span< const uint8_t > bytes)
static OID from_string(std::string_view str)
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
CK_OBJECT_HANDLE ObjectHandle
std::vector< T, secure_allocator< T > > secure_vector
CK_ULONG CK_MECHANISM_TYPE