Botan 3.4.0
Crypto and TLS for C&
ecdsa.cpp
Go to the documentation of this file.
1/*
2* ECDSA implemenation
3* (C) 2007 Manuel Hartl, FlexSecure GmbH
4* 2007 Falko Strenzke, FlexSecure GmbH
5* 2008-2010,2015,2016,2018 Jack Lloyd
6* 2016 René Korthaus
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#include <botan/ecdsa.h>
12
13#include <botan/reducer.h>
14#include <botan/internal/keypair.h>
15#include <botan/internal/pk_ops_impl.h>
16#include <botan/internal/point_mul.h>
17
18#if defined(BOTAN_HAS_RFC6979_GENERATOR)
19 #include <botan/internal/rfc6979.h>
20#endif
21
22namespace Botan {
23
24namespace {
25
26EC_Point recover_ecdsa_public_key(
27 const EC_Group& group, const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s, uint8_t v) {
28 if(group.get_cofactor() != 1) {
29 throw Invalid_Argument("ECDSA public key recovery only supported for prime order groups");
30 }
31
32 if(v >= 4) {
33 throw Invalid_Argument("Unexpected v param for ECDSA public key recovery");
34 }
35
36 const BigInt& group_order = group.get_order();
37
38 if(r <= 0 || r >= group_order || s <= 0 || s >= group_order) {
39 throw Invalid_Argument("Out of range r/s cannot recover ECDSA public key");
40 }
41
42 const uint8_t y_odd = v % 2;
43 const uint8_t add_order = v >> 1;
44 const size_t p_bytes = group.get_p_bytes();
45
46 try {
47 const BigInt e = BigInt::from_bytes_with_max_bits(msg.data(), msg.size(), group.get_order_bits());
48 const BigInt r_inv = group.inverse_mod_order(r);
49
50 BigInt x = r + add_order * group_order;
51
52 std::vector<uint8_t> X(p_bytes + 1);
53
54 X[0] = 0x02 | y_odd;
55 BigInt::encode_1363(&X[1], p_bytes, x);
56
57 const EC_Point R = group.OS2ECP(X);
58
59 if((R * group_order).is_zero() == false) {
60 throw Decoding_Error("Unable to recover ECDSA public key");
61 }
62
63 // Compute r_inv * (s*R - eG)
64 EC_Point_Multi_Point_Precompute RG_mul(R, group.get_base_point());
65 const BigInt ne = group.mod_order(group_order - e);
66 return r_inv * RG_mul.multi_exp(s, ne);
67 } catch(...) {
68 // continue on and throw
69 }
70
71 throw Decoding_Error("Failed to recover ECDSA public key from signature/msg pair");
72}
73
74} // namespace
75
77 const EC_Group& group, const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s, uint8_t v) :
78 EC_PublicKey(group, recover_ecdsa_public_key(group, msg, r, s, v)) {}
79
80std::unique_ptr<Private_Key> ECDSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
81 return std::make_unique<ECDSA_PrivateKey>(rng, domain());
82}
83
84uint8_t ECDSA_PublicKey::recovery_param(const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s) const {
85 for(uint8_t v = 0; v != 4; ++v) {
86 try {
87 EC_Point R = recover_ecdsa_public_key(this->domain(), msg, r, s, v);
88
89 if(R == this->public_point()) {
90 return v;
91 }
92 } catch(Decoding_Error&) {
93 // try the next v
94 }
95 }
96
97 throw Internal_Error("Could not determine ECDSA recovery parameter");
98}
99
100std::unique_ptr<Public_Key> ECDSA_PrivateKey::public_key() const {
101 return std::make_unique<ECDSA_PublicKey>(domain(), public_point());
102}
103
105 if(!EC_PrivateKey::check_key(rng, strong)) {
106 return false;
107 }
108
109 if(!strong) {
110 return true;
111 }
112
113 return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
114}
115
116namespace {
117
118/**
119* ECDSA signature operation
120*/
121class ECDSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
122 public:
123 ECDSA_Signature_Operation(const ECDSA_PrivateKey& ecdsa, std::string_view padding, RandomNumberGenerator& rng) :
124 PK_Ops::Signature_with_Hash(padding), m_group(ecdsa.domain()), m_x(ecdsa.private_value()) {
125#if defined(BOTAN_HAS_RFC6979_GENERATOR)
126 m_rfc6979 = std::make_unique<RFC6979_Nonce_Generator>(this->rfc6979_hash_function(), m_group.get_order(), m_x);
127#endif
128
129 m_b = m_group.random_scalar(rng);
130 m_b_inv = m_group.inverse_mod_order(m_b);
131 }
132
133 size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
134
135 secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len, RandomNumberGenerator& rng) override;
136
137 AlgorithmIdentifier algorithm_identifier() const override;
138
139 private:
140 const EC_Group m_group;
141 const BigInt m_x;
142
143#if defined(BOTAN_HAS_RFC6979_GENERATOR)
144 std::unique_ptr<RFC6979_Nonce_Generator> m_rfc6979;
145#endif
146
147 std::vector<BigInt> m_ws;
148
149 BigInt m_b, m_b_inv;
150};
151
152AlgorithmIdentifier ECDSA_Signature_Operation::algorithm_identifier() const {
153 const std::string full_name = "ECDSA/" + hash_function();
154 const OID oid = OID::from_string(full_name);
155 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
156}
157
158secure_vector<uint8_t> ECDSA_Signature_Operation::raw_sign(const uint8_t msg[],
159 size_t msg_len,
160 RandomNumberGenerator& rng) {
161 BigInt m = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
162
163#if defined(BOTAN_HAS_RFC6979_GENERATOR)
164 const BigInt k = m_rfc6979->nonce_for(m);
165#else
166 const BigInt k = m_group.random_scalar(rng);
167#endif
168
169 const BigInt r = m_group.mod_order(m_group.blinded_base_point_multiply_x(k, rng, m_ws));
170
171 const BigInt k_inv = m_group.inverse_mod_order(k);
172
173 /*
174 * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
175 */
176 m_b = m_group.square_mod_order(m_b);
177 m_b_inv = m_group.square_mod_order(m_b_inv);
178
179 m = m_group.multiply_mod_order(m_b, m_group.mod_order(m));
180 const BigInt xr_m = m_group.mod_order(m_group.multiply_mod_order(m_x, m_b, r) + m);
181
182 const BigInt s = m_group.multiply_mod_order(k_inv, xr_m, m_b_inv);
183
184 // With overwhelming probability, a bug rather than actual zero r/s
185 if(r.is_zero() || s.is_zero()) {
186 throw Internal_Error("During ECDSA signature generated zero r/s");
187 }
188
189 return BigInt::encode_fixed_length_int_pair(r, s, m_group.get_order_bytes());
190}
191
192/**
193* ECDSA verification operation
194*/
195class ECDSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
196 public:
197 ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, std::string_view padding) :
198 PK_Ops::Verification_with_Hash(padding),
199 m_group(ecdsa.domain()),
200 m_gy_mul(m_group.get_base_point(), ecdsa.public_point()) {}
201
202 ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, const AlgorithmIdentifier& alg_id) :
203 PK_Ops::Verification_with_Hash(alg_id, "ECDSA", true),
204 m_group(ecdsa.domain()),
205 m_gy_mul(m_group.get_base_point(), ecdsa.public_point()) {}
206
207 bool verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) override;
208
209 private:
210 const EC_Group m_group;
211 const EC_Point_Multi_Point_Precompute m_gy_mul;
212};
213
214bool ECDSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) {
215 if(sig_len != m_group.get_order_bytes() * 2) {
216 return false;
217 }
218
219 const BigInt e = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
220
221 const BigInt r(sig, sig_len / 2);
222 const BigInt s(sig + sig_len / 2, sig_len / 2);
223
224 // Cannot be negative here since we just decoded from binary
225 if(r.is_zero() || s.is_zero()) {
226 return false;
227 }
228
229 if(r >= m_group.get_order() || s >= m_group.get_order()) {
230 return false;
231 }
232
233 const BigInt w = m_group.inverse_mod_order(s);
234
235 const BigInt u1 = m_group.multiply_mod_order(m_group.mod_order(e), w);
236 const BigInt u2 = m_group.multiply_mod_order(r, w);
237 const EC_Point R = m_gy_mul.multi_exp(u1, u2);
238
239 if(R.is_zero()) {
240 return false;
241 }
242
243 const BigInt v = m_group.mod_order(R.get_affine_x());
244 return (v == r);
245}
246
247} // namespace
248
249std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_verification_op(std::string_view params,
250 std::string_view provider) const {
251 if(provider == "base" || provider.empty()) {
252 return std::make_unique<ECDSA_Verification_Operation>(*this, params);
253 }
254
255 throw Provider_Not_Found(algo_name(), provider);
256}
257
258std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_x509_verification_op(
259 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
260 if(provider == "base" || provider.empty()) {
261 return std::make_unique<ECDSA_Verification_Operation>(*this, signature_algorithm);
262 }
263
264 throw Provider_Not_Found(algo_name(), provider);
265}
266
267std::unique_ptr<PK_Ops::Signature> ECDSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
268 std::string_view params,
269 std::string_view provider) const {
270 if(provider == "base" || provider.empty()) {
271 return std::make_unique<ECDSA_Signature_Operation>(*this, params, rng);
272 }
273
274 throw Provider_Not_Found(algo_name(), provider);
275}
276
277} // namespace Botan
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:105
static BigInt from_bytes_with_max_bits(const uint8_t buf[], size_t length, size_t max_bits)
Definition bigint.cpp:103
std::unique_ptr< Public_Key > public_key() const override
Definition ecdsa.cpp:100
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition ecdsa.cpp:267
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition ecdsa.cpp:104
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
Definition ecdsa.cpp:258
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const override
Definition ecdsa.cpp:80
uint8_t recovery_param(const std::vector< uint8_t > &msg, const BigInt &r, const BigInt &s) const
Definition ecdsa.cpp:84
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
Definition ecdsa.cpp:249
BigInt blinded_base_point_multiply_x(const BigInt &k, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
Definition ec_group.cpp:581
BigInt mod_order(const BigInt &x) const
Definition ec_group.cpp:524
BigInt multiply_mod_order(const BigInt &x, const BigInt &y) const
Definition ec_group.cpp:532
const BigInt & get_order() const
Definition ec_group.cpp:508
BigInt square_mod_order(const BigInt &x) const
Definition ec_group.cpp:528
BigInt inverse_mod_order(const BigInt &x) const
Definition ec_group.cpp:540
BigInt random_scalar(RandomNumberGenerator &rng) const
Definition ec_group.cpp:592
size_t get_order_bits() const
Definition ec_group.cpp:484
size_t get_order_bytes() const
Definition ec_group.cpp:488
EC_Point multi_exp(const BigInt &k1, const BigInt &k2) const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:161
const EC_Group & domain() const
Definition ecc_key.h:54
const EC_Point & public_point() const
Definition ecc_key.h:40
int(* final)(unsigned char *, CTX *)
FE_25519 X
Definition ge.cpp:25
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49