• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/lib/pubkey/ecdsa/ecdsa.cpp
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

22
namespace Botan {
23

24
namespace {
25

26
EC_Point recover_ecdsa_public_key(
20✔
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) {
20✔
29
      throw Invalid_Argument("ECDSA public key recovery only supported for prime order groups");
×
30
   }
31

32
   if(v >= 4) {
20✔
33
      throw Invalid_Argument("Unexpected v param for ECDSA public key recovery");
×
34
   }
35

36
   const BigInt& group_order = group.get_order();
20✔
37

38
   if(r <= 0 || r >= group_order || s <= 0 || s >= group_order) {
80✔
39
      throw Invalid_Argument("Out of range r/s cannot recover ECDSA public key");
×
40
   }
41

42
   const uint8_t y_odd = v % 2;
20✔
43
   const uint8_t add_order = v >> 1;
20✔
44
   const size_t p_bytes = group.get_p_bytes();
20✔
45

46
   try {
20✔
47
      const BigInt e = BigInt::from_bytes_with_max_bits(msg.data(), msg.size(), group.get_order_bits());
20✔
48
      const BigInt r_inv = group.inverse_mod_order(r);
20✔
49

50
      BigInt x = r + add_order * group_order;
20✔
51

52
      std::vector<uint8_t> X(p_bytes + 1);
20✔
53

54
      X[0] = 0x02 | y_odd;
20✔
55
      BigInt::encode_1363(&X[1], p_bytes, x);
20✔
56

57
      const EC_Point R = group.OS2ECP(X);
20✔
58

59
      if((R * group_order).is_zero() == false) {
20✔
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());
20✔
65
      const BigInt ne = group.mod_order(group_order - e);
20✔
66
      return r_inv * RG_mul.multi_exp(s, ne);
20✔
67
   } catch(...) {
100✔
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

76
ECDSA_PublicKey::ECDSA_PublicKey(
8✔
77
   const EC_Group& group, const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s, uint8_t v) :
8✔
78
      EC_PublicKey(group, recover_ecdsa_public_key(group, msg, r, s, v)) {}
8✔
79

80
uint8_t ECDSA_PublicKey::recovery_param(const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s) const {
8✔
81
   for(uint8_t v = 0; v != 4; ++v) {
12✔
82
      try {
12✔
83
         EC_Point R = recover_ecdsa_public_key(this->domain(), msg, r, s, v);
12✔
84

85
         if(R == this->public_point()) {
12✔
86
            return v;
8✔
87
         }
88
      } catch(Decoding_Error&) {
12✔
89
         // try the next v
90
      }
×
91
   }
92

93
   throw Internal_Error("Could not determine ECDSA recovery parameter");
×
94
}
95

96
std::unique_ptr<Public_Key> ECDSA_PrivateKey::public_key() const {
193✔
97
   return std::make_unique<ECDSA_PublicKey>(domain(), public_point());
193✔
98
}
99

100
bool ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
8✔
101
   if(!public_point().on_the_curve()) {
8✔
102
      return false;
103
   }
104

105
   if(!strong) {
8✔
106
      return true;
107
   }
108

109
   return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
7✔
110
}
111

112
namespace {
113

114
/**
115
* ECDSA signature operation
116
*/
117
class ECDSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
×
118
   public:
119
      ECDSA_Signature_Operation(const ECDSA_PrivateKey& ecdsa, std::string_view padding, RandomNumberGenerator& rng) :
396✔
120
            PK_Ops::Signature_with_Hash(padding), m_group(ecdsa.domain()), m_x(ecdsa.private_value()) {
396✔
121
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
122
         m_rfc6979 = std::make_unique<RFC6979_Nonce_Generator>(this->rfc6979_hash_function(), m_group.get_order(), m_x);
396✔
123
#endif
124

125
         m_b = m_group.random_scalar(rng);
792✔
126
         m_b_inv = m_group.inverse_mod_order(m_b);
792✔
127
      }
396✔
128

129
      size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
108✔
130

131
      secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len, RandomNumberGenerator& rng) override;
132

133
      AlgorithmIdentifier algorithm_identifier() const override;
134

135
   private:
136
      const EC_Group m_group;
137
      const BigInt& m_x;
138

139
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
140
      std::unique_ptr<RFC6979_Nonce_Generator> m_rfc6979;
141
#endif
142

143
      std::vector<BigInt> m_ws;
144

145
      BigInt m_b, m_b_inv;
146
};
147

148
AlgorithmIdentifier ECDSA_Signature_Operation::algorithm_identifier() const {
62✔
149
   const std::string full_name = "ECDSA/" + hash_function();
62✔
150
   const OID oid = OID::from_string(full_name);
62✔
151
   return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
62✔
152
}
62✔
153

154
secure_vector<uint8_t> ECDSA_Signature_Operation::raw_sign(const uint8_t msg[],
406✔
155
                                                           size_t msg_len,
156
                                                           RandomNumberGenerator& rng) {
157
   BigInt m = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
406✔
158

159
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
160
   const BigInt k = m_rfc6979->nonce_for(m);
406✔
161
#else
162
   const BigInt k = m_group.random_scalar(rng);
163
#endif
164

165
   const BigInt r = m_group.mod_order(m_group.blinded_base_point_multiply_x(k, rng, m_ws));
406✔
166

167
   const BigInt k_inv = m_group.inverse_mod_order(k);
406✔
168

169
   /*
170
   * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
171
   */
172
   m_b = m_group.square_mod_order(m_b);
406✔
173
   m_b_inv = m_group.square_mod_order(m_b_inv);
406✔
174

175
   m = m_group.multiply_mod_order(m_b, m_group.mod_order(m));
812✔
176
   const BigInt xr_m = m_group.mod_order(m_group.multiply_mod_order(m_x, m_b, r) + m);
812✔
177

178
   const BigInt s = m_group.multiply_mod_order(k_inv, xr_m, m_b_inv);
406✔
179

180
   // With overwhelming probability, a bug rather than actual zero r/s
181
   if(r.is_zero() || s.is_zero()) {
812✔
182
      throw Internal_Error("During ECDSA signature generated zero r/s");
×
183
   }
184

185
   return BigInt::encode_fixed_length_int_pair(r, s, m_group.get_order_bytes());
406✔
186
}
2,436✔
187

188
/**
189
* ECDSA verification operation
190
*/
191
class ECDSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
×
192
   public:
193
      ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, std::string_view padding) :
12,417✔
194
            PK_Ops::Verification_with_Hash(padding),
195
            m_group(ecdsa.domain()),
16,334✔
196
            m_gy_mul(m_group.get_base_point(), ecdsa.public_point()) {}
12,417✔
197

198
      ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, const AlgorithmIdentifier& alg_id) :
1,666✔
199
            PK_Ops::Verification_with_Hash(alg_id, "ECDSA", true),
200
            m_group(ecdsa.domain()),
3,310✔
201
            m_gy_mul(m_group.get_base_point(), ecdsa.public_point()) {}
1,666✔
202

203
      bool verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) override;
204

205
   private:
206
      const EC_Group m_group;
207
      const EC_Point_Multi_Point_Precompute m_gy_mul;
208
};
209

210
bool ECDSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) {
16,133✔
211
   if(sig_len != m_group.get_order_bytes() * 2) {
16,133✔
212
      return false;
213
   }
214

215
   const BigInt e = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
13,134✔
216

217
   const BigInt r(sig, sig_len / 2);
13,134✔
218
   const BigInt s(sig + sig_len / 2, sig_len / 2);
13,134✔
219

220
   // Cannot be negative here since we just decoded from binary
221
   if(r.is_zero() || s.is_zero()) {
38,957✔
222
      return false;
590✔
223
   }
224

225
   if(r >= m_group.get_order() || s >= m_group.get_order()) {
12,544✔
226
      return false;
660✔
227
   }
228

229
   const BigInt w = m_group.inverse_mod_order(s);
11,884✔
230

231
   const BigInt u1 = m_group.multiply_mod_order(m_group.mod_order(e), w);
11,884✔
232
   const BigInt u2 = m_group.multiply_mod_order(r, w);
11,884✔
233
   const EC_Point R = m_gy_mul.multi_exp(u1, u2);
11,884✔
234

235
   if(R.is_zero()) {
23,654✔
236
      return false;
237
   }
238

239
   const BigInt v = m_group.mod_order(R.get_affine_x());
11,773✔
240
   return (v == r);
11,773✔
241
}
91,187✔
242

243
}  // namespace
244

245
std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_verification_op(std::string_view params,
48,930✔
246
                                                                              std::string_view provider) const {
247
   if(provider == "base" || provider.empty()) {
61,108✔
248
      return std::make_unique<ECDSA_Verification_Operation>(*this, params);
12,417✔
249
   }
250

251
   throw Provider_Not_Found(algo_name(), provider);
73,026✔
252
}
253

254
std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_x509_verification_op(
1,666✔
255
   const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
256
   if(provider == "base" || provider.empty()) {
1,666✔
257
      return std::make_unique<ECDSA_Verification_Operation>(*this, signature_algorithm);
1,666✔
258
   }
259

260
   throw Provider_Not_Found(algo_name(), provider);
×
261
}
262

263
std::unique_ptr<PK_Ops::Signature> ECDSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
708✔
264
                                                                         std::string_view params,
265
                                                                         std::string_view provider) const {
266
   if(provider == "base" || provider.empty()) {
814✔
267
      return std::make_unique<ECDSA_Signature_Operation>(*this, params, rng);
396✔
268
   }
269

270
   throw Provider_Not_Found(algo_name(), provider);
624✔
271
}
272

273
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc