Botan 3.6.1
Crypto and TLS for C&
ecgdsa.cpp
Go to the documentation of this file.
1/*
2* ECGDSA (BSI-TR-03111, version 2.0)
3* (C) 2016 René Korthaus
4* (C) 2018,2024 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/ecgdsa.h>
10
11#include <botan/internal/keypair.h>
12#include <botan/internal/pk_ops_impl.h>
13
14namespace Botan {
15
16std::unique_ptr<Public_Key> ECGDSA_PrivateKey::public_key() const {
17 return std::make_unique<ECGDSA_PublicKey>(domain(), public_point());
18}
19
21 if(!EC_PrivateKey::check_key(rng, strong)) {
22 return false;
23 }
24
25 if(!strong) {
26 return true;
27 }
28
29 return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
30}
31
32namespace {
33
34/**
35* ECGDSA signature operation
36*/
37class ECGDSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
38 public:
39 ECGDSA_Signature_Operation(const ECGDSA_PrivateKey& ecgdsa, std::string_view emsa) :
40 PK_Ops::Signature_with_Hash(emsa), m_group(ecgdsa.domain()), m_x(ecgdsa._private_key()) {}
41
42 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) override;
43
44 size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
45
46 AlgorithmIdentifier algorithm_identifier() const override;
47
48 private:
49 const EC_Group m_group;
50 const EC_Scalar m_x;
51 std::vector<BigInt> m_ws;
52};
53
54AlgorithmIdentifier ECGDSA_Signature_Operation::algorithm_identifier() const {
55 const std::string full_name = "ECGDSA/" + hash_function();
56 const OID oid = OID::from_string(full_name);
57 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
58}
59
60std::vector<uint8_t> ECGDSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
61 const auto m = EC_Scalar::from_bytes_with_trunc(m_group, msg);
62
63 const auto k = EC_Scalar::random(m_group, rng);
64
65 const auto r = EC_Scalar::gk_x_mod_order(k, rng, m_ws);
66
67 const auto s = m_x * ((k * r) - m);
68
69 // With overwhelming probability, a bug rather than actual zero r/s
70 if(r.is_zero() || s.is_zero()) {
71 throw Internal_Error("During ECGDSA signature generated zero r/s");
72 }
73
74 return EC_Scalar::serialize_pair(r, s);
75}
76
77/**
78* ECGDSA verification operation
79*/
80class ECGDSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
81 public:
82 ECGDSA_Verification_Operation(const ECGDSA_PublicKey& ecgdsa, std::string_view padding) :
83 PK_Ops::Verification_with_Hash(padding), m_group(ecgdsa.domain()), m_gy_mul(ecgdsa._public_key()) {}
84
85 ECGDSA_Verification_Operation(const ECGDSA_PublicKey& ecgdsa, const AlgorithmIdentifier& alg_id) :
86 PK_Ops::Verification_with_Hash(alg_id, "ECGDSA"),
87 m_group(ecgdsa.domain()),
88 m_gy_mul(ecgdsa._public_key()) {}
89
90 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) override;
91
92 private:
93 const EC_Group m_group;
94 const EC_Group::Mul2Table m_gy_mul;
95};
96
97bool ECGDSA_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
98 if(auto rs = EC_Scalar::deserialize_pair(m_group, sig)) {
99 const auto& [r, s] = rs.value();
100
101 if(r.is_nonzero() && s.is_nonzero()) {
102 const auto m = EC_Scalar::from_bytes_with_trunc(m_group, msg);
103
104 const auto w = r.invert();
105
106 // Check if r == x_coord(g*w*m + y*w*s) % n
107 return m_gy_mul.mul2_vartime_x_mod_order_eq(r, w, m, s);
108 }
109 }
110
111 return false;
112}
113
114} // namespace
115
116std::unique_ptr<Private_Key> ECGDSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
117 return std::make_unique<ECGDSA_PrivateKey>(rng, domain());
118}
119
120std::unique_ptr<PK_Ops::Verification> ECGDSA_PublicKey::create_verification_op(std::string_view params,
121 std::string_view provider) const {
122 if(provider == "base" || provider.empty()) {
123 return std::make_unique<ECGDSA_Verification_Operation>(*this, params);
124 }
125 throw Provider_Not_Found(algo_name(), provider);
126}
127
128std::unique_ptr<PK_Ops::Verification> ECGDSA_PublicKey::create_x509_verification_op(
129 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
130 if(provider == "base" || provider.empty()) {
131 return std::make_unique<ECGDSA_Verification_Operation>(*this, signature_algorithm);
132 }
133
134 throw Provider_Not_Found(algo_name(), provider);
135}
136
137std::unique_ptr<PK_Ops::Signature> ECGDSA_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
138 std::string_view params,
139 std::string_view provider) const {
140 if(provider == "base" || provider.empty()) {
141 return std::make_unique<ECGDSA_Signature_Operation>(*this, params);
142 }
143 throw Provider_Not_Found(algo_name(), provider);
144}
145
146} // namespace Botan
std::unique_ptr< Public_Key > public_key() const override
Definition ecgdsa.cpp:16
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition ecgdsa.cpp:20
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition ecgdsa.cpp:137
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
Definition ecgdsa.cpp:120
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
Definition ecgdsa.cpp:128
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition ecgdsa.cpp:116
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
Definition ec_group.cpp:652
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:196
const EC_Group & domain() const
Definition ecc_key.cpp:59
const EC_Point & public_point() const
Definition ecc_key.cpp:64
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)
Definition keypair.cpp:49