9#include <botan/p11_ecdsa.h>
11#if defined(BOTAN_HAS_ECDSA)
13#include <botan/internal/p11_mechanism.h>
14#include <botan/pk_ops.h>
15#include <botan/keypair.h>
21ECDSA_PublicKey PKCS11_ECDSA_PublicKey::export_key()
const
23 return ECDSA_PublicKey(domain(), public_point());
26bool PKCS11_ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng,
bool strong)
const
28 if(!public_point().on_the_curve())
38 ECDSA_PublicKey pubkey(domain(), public_point());
42ECDSA_PrivateKey PKCS11_ECDSA_PrivateKey::export_key()
const
50secure_vector<uint8_t> PKCS11_ECDSA_PrivateKey::private_key_bits()
const
52 return export_key().private_key_bits();
57class PKCS11_ECDSA_Signature_Operation
final :
public PK_Ops::Signature
60 PKCS11_ECDSA_Signature_Operation(
const PKCS11_EC_PrivateKey& key,
const std::string& emsa)
61 : PK_Ops::Signature(), m_key(key), m_order(key.domain().get_order()), m_mechanism(MechanismWrapper::create_ecdsa_mechanism(emsa))
64 void update(
const uint8_t msg[],
size_t msg_len)
override
69 m_key.module()->C_SignInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
71 m_first_message = secure_vector<uint8_t>(msg, msg + msg_len);
75 if(!m_first_message.empty())
78 m_key.module()->C_SignUpdate(m_key.session().handle(), m_first_message);
79 m_first_message.clear();
82 m_key.module()->C_SignUpdate(m_key.session().handle(),
const_cast<Byte*
>(msg),
static_cast<Ulong>(msg_len));
85 secure_vector<uint8_t> sign(RandomNumberGenerator&)
override
87 secure_vector<uint8_t> signature;
88 if(!m_first_message.empty())
91 m_key.module()->C_Sign(m_key.session().handle(), m_first_message, signature);
92 m_first_message.clear();
97 m_key.module()->C_SignFinal(m_key.session().handle(), signature);
99 m_initialized =
false;
103 size_t signature_length()
const override {
return 2*m_order.bytes(); }
106 const PKCS11_EC_PrivateKey& m_key;
107 const BigInt& m_order;
108 MechanismWrapper m_mechanism;
109 secure_vector<uint8_t> m_first_message;
110 bool m_initialized =
false;
114class PKCS11_ECDSA_Verification_Operation
final :
public PK_Ops::Verification
117 PKCS11_ECDSA_Verification_Operation(
const PKCS11_EC_PublicKey& key,
const std::string& emsa)
118 : PK_Ops::Verification(), m_key(key), m_order(key.domain().get_order()), m_mechanism(MechanismWrapper::create_ecdsa_mechanism(emsa))
121 void update(
const uint8_t msg[],
size_t msg_len)
override
126 m_key.module()->C_VerifyInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
127 m_initialized =
true;
128 m_first_message = secure_vector<uint8_t>(msg, msg + msg_len);
132 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(),
const_cast<Byte*
>(msg),
static_cast<Ulong>(msg_len));
142 bool is_valid_signature(
const uint8_t sig[],
size_t sig_len)
override
144 ReturnValue return_value = ReturnValue::SignatureInvalid;
145 if(!m_first_message.empty())
148 m_key.module()->C_Verify(m_key.session().handle(),
149 m_first_message.data(),
static_cast<Ulong>(m_first_message.size()),
150 const_cast<Byte*
>(sig),
static_cast<Ulong>(sig_len),
152 m_first_message.clear();
157 m_key.module()->C_VerifyFinal(m_key.session().handle(),
const_cast<Byte*
>(sig),
static_cast<Ulong>(sig_len), &return_value);
159 m_initialized =
false;
160 if(return_value != ReturnValue::OK && return_value != ReturnValue::SignatureInvalid)
162 throw PKCS11_ReturnError(return_value);
164 return return_value == ReturnValue::OK;
168 const PKCS11_EC_PublicKey& m_key;
169 const BigInt& m_order;
170 MechanismWrapper m_mechanism;
171 secure_vector<uint8_t> m_first_message;
172 bool m_initialized =
false;
177std::unique_ptr<PK_Ops::Verification>
178PKCS11_ECDSA_PublicKey::create_verification_op(
const std::string& params,
179 const std::string& )
const
181 return std::unique_ptr<PK_Ops::Verification>(
new PKCS11_ECDSA_Verification_Operation(*
this, params));
184std::unique_ptr<PK_Ops::Signature>
185PKCS11_ECDSA_PrivateKey::create_signature_op(RandomNumberGenerator& ,
186 const std::string& params,
187 const std::string& )
const
189 return std::unique_ptr<PK_Ops::Signature>(
new PKCS11_ECDSA_Signature_Operation(*
this, params));
192PKCS11_ECDSA_KeyPair generate_ecdsa_keypair(Session& session,
const EC_PublicKeyGenerationProperties& pub_props,
193 const EC_PrivateKeyGenerationProperties& priv_props)
200 session.module()->C_GenerateKeyPair(session.handle(), &mechanism,
201 pub_props.data(),
static_cast<Ulong>(pub_props.count()),
202 priv_props.data(),
static_cast<Ulong>(priv_props.count()),
203 &pub_key_handle, &priv_key_handle);
205 return std::make_pair(PKCS11_ECDSA_PublicKey(session, pub_key_handle), PKCS11_ECDSA_PrivateKey(session,
static BigInt decode(const uint8_t buf[], size_t length)
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, const std::string &padding)
CK_OBJECT_HANDLE ObjectHandle
CK_ULONG CK_MECHANISM_TYPE