11#include <botan/ecdsa.h>
13#include <botan/ec_group.h>
14#include <botan/pk_options_readers.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/keypair.h>
17#include <botan/internal/pk_ops_impl.h>
18#include <botan/internal/pk_options_impl.h>
20#if defined(BOTAN_HAS_RFC6979_GENERATOR)
21 #include <botan/internal/rfc6979.h>
29 const EC_Group& group,
const std::vector<uint8_t>& msg,
const BigInt& r,
const BigInt& s, uint8_t v) {
30 if(group.has_cofactor()) {
31 throw Invalid_Argument(
"ECDSA public key recovery only supported for prime order groups");
38 const BigInt& group_order = group.get_order();
40 if(r <= 0 || r >= group_order || s <= 0 || s >= group_order) {
44 const uint8_t y_odd = v % 2;
45 const bool add_order = (v >> 1) == 0x01;
46 const size_t p_bytes = group.get_p_bytes();
54 if(x.bytes() <= p_bytes) {
55 std::vector<uint8_t> X(p_bytes + 1);
58 x.serialize_to(std::span{X}.subspan(1));
68 if(
auto egsr = GR_mul.mul2_vartime(ne * r_inv, ss * r_inv)) {
74 throw Decoding_Error(
"Failed to recover ECDSA public key from signature/msg pair");
80 const EC_Group& group,
const std::vector<uint8_t>& msg,
const BigInt& r,
const BigInt& s, uint8_t v) :
81 EC_PublicKey(group, recover_ecdsa_public_key(group, msg, r, s, v)) {}
88 return std::make_unique<ECDSA_PrivateKey>(rng,
domain());
94 for(uint8_t v = 0; v != 4; ++v) {
96 const auto R = recover_ecdsa_public_key(this->
domain(), msg, r, s, v);
98 if(R.serialize_compressed() == this_key) {
106 throw Internal_Error(
"Could not determine ECDSA recovery parameter");
135 PK_Ops::Signature_with_Hash(options),
136 m_group(ecdsa.domain()),
137 m_x(ecdsa._private_key()),
139#if defined(BOTAN_HAS_RFC6979_GENERATOR)
141 m_rfc6979 = std::make_unique<RFC6979_Nonce_Generator>(
142 this->rfc6979_hash_function(), m_group.get_order_bits(), ecdsa._private_key());
146 throw Not_Implemented(
"Deterministic ECDSA signatures require RFC 6979 support");
151 size_t signature_length()
const override {
return 2 * m_group.get_order_bytes(); }
153 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng)
override;
155 AlgorithmIdentifier algorithm_identifier()
const override;
158 const EC_Group m_group;
161#if defined(BOTAN_HAS_RFC6979_GENERATOR)
162 std::unique_ptr<RFC6979_Nonce_Generator> m_rfc6979;
169 const std::string full_name =
"ECDSA/" + hash_function();
170 const OID oid = OID::from_string(full_name);
171 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
174std::vector<uint8_t> ECDSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
175 const auto m = EC_Scalar::from_bytes_with_trunc(m_group, msg);
177 const auto scope = CT::scoped_poison(m_x, m_b);
179#if defined(BOTAN_HAS_RFC6979_GENERATOR)
180 const auto k = m_rfc6979 ? m_rfc6979->nonce_for(m_group, m) : EC_Scalar::random(m_group, rng);
182 const auto k = EC_Scalar::random(m_group, rng);
207 const auto r = EC_Scalar::gk_x_mod_order(k, rng);
209 const auto k_inv = (m_b * k).invert();
211 const auto xr_m = ((m_x * m_b) * r) + (m * m_b);
213 const auto s = (k_inv * xr_m);
218 CT::unpoison_all(r, s);
221 if(r.is_zero() || s.is_zero()) {
222 throw Internal_Error(
"During ECDSA signature generated zero r/s");
225 return EC_Scalar::serialize_pair(r, s);
231class ECDSA_Verification_Operation final :
public PK_Ops::Verification_with_Hash {
233 ECDSA_Verification_Operation(
const ECDSA_PublicKey& ecdsa,
const PK_Signature_Options_Reader& options) :
234 PK_Ops::Verification_with_Hash(options), m_group(ecdsa.domain()), m_gy_mul(ecdsa.public_ec_point()) {}
236 ECDSA_Verification_Operation(
const ECDSA_PublicKey& ecdsa,
const AlgorithmIdentifier& alg_id) :
249 PK_Ops::Verification_with_Hash(alg_id,
"ECDSA", true),
250 m_group(ecdsa.domain()),
251 m_gy_mul(ecdsa.public_ec_point()) {}
253 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig)
override;
256 const EC_Group m_group;
257 const EC_Group::Mul2Table m_gy_mul;
260bool ECDSA_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
261 if(
auto rs = EC_Scalar::deserialize_pair(m_group, sig)) {
262 const auto& [r, s] = rs.value();
264 if(r.is_nonzero() && s.is_nonzero()) {
265 const auto m = EC_Scalar::from_bytes_with_trunc(m_group, msg);
267 const auto w = s.invert_vartime();
283 return std::make_unique<ECDSA_Verification_Operation>(*
this, options);
288 if(provider ==
"base" || provider.empty()) {
289 return std::make_unique<ECDSA_Verification_Operation>(*
this, signature_algorithm);
299 return std::make_unique<ECDSA_Signature_Operation>(*
this, options, rng);
std::unique_ptr< Public_Key > public_key() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
std::string algo_name() const override
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
ECDSA_PublicKey()=default
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const override
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
uint8_t recovery_param(const std::vector< uint8_t > &msg, const BigInt &r, const BigInt &s) const
std::optional< size_t > _signature_element_size_for_DER_encoding() const override
static std::optional< EC_AffinePoint > deserialize_compressed(const EC_Group &group, std::span< const uint8_t > bytes)
T serialize_compressed() const
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
size_t get_order_bytes() const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
const EC_Group & domain() const
const EC_AffinePoint & public_ec_point() const
EC_PublicKey(const EC_PublicKey &other)=default
static EC_Scalar from_bigint(const EC_Group &group, const BigInt &bn)
EC_Scalar invert_vartime() const
static EC_Scalar from_bytes_with_trunc(const EC_Group &group, std::span< const uint8_t > bytes)
bool using_deterministic_signature() const
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
void require_software_provider(const OptionsT &options, std::string_view algo_name)