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