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

randombit / botan / 20194419005

13 Dec 2025 12:36PM UTC coverage: 90.357% (-1.4%) from 91.76%
20194419005

push

github

web-flow
Merge pull request #5173 from randombit/jack/ecdsa-new-blinding

Use a new strategy for blinding in ECDSA signing

100950 of 111723 relevant lines covered (90.36%)

12770309.14 hits per line

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

87.74
/src/lib/pubkey/ecdsa/ecdsa.cpp
1
/*
2
* ECDSA implementation
3
* (C) 2007 Manuel Hartl, FlexSecure GmbH
4
*     2007 Falko Strenzke, FlexSecure GmbH
5
*     2008-2010,2015,2016,2018,2024 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/internal/keypair.h>
14
#include <botan/internal/pk_ops_impl.h>
15

16
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
17
   #include <botan/internal/rfc6979.h>
18
#endif
19

20
namespace Botan {
21

22
namespace {
23

24
EC_AffinePoint recover_ecdsa_public_key(
29✔
25
   const EC_Group& group, const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s, uint8_t v) {
26
   if(group.has_cofactor()) {
29✔
27
      throw Invalid_Argument("ECDSA public key recovery only supported for prime order groups");
×
28
   }
29

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

34
   const BigInt& group_order = group.get_order();
29✔
35

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

40
   const uint8_t y_odd = v % 2;
29✔
41
   const bool add_order = (v >> 1) == 0x01;
29✔
42
   const size_t p_bytes = group.get_p_bytes();
29✔
43

44
   BigInt x = r;
29✔
45

46
   if(add_order) {
29✔
47
      x += group_order;
×
48
   }
49

50
   if(x.bytes() <= p_bytes) {
29✔
51
      std::vector<uint8_t> X(p_bytes + 1);
29✔
52

53
      X[0] = 0x02 | y_odd;
29✔
54
      x.serialize_to(std::span{X}.subspan(1));
29✔
55

56
      if(auto R = EC_AffinePoint::deserialize(group, X)) {
29✔
57
         // Compute r_inv * (-eG + s*R)
58
         const auto ne = EC_Scalar::from_bytes_with_trunc(group, msg).negate();
29✔
59
         const auto ss = EC_Scalar::from_bigint(group, s);
29✔
60

61
         const auto r_inv = EC_Scalar::from_bigint(group, r).invert_vartime();
29✔
62

63
         const EC_Group::Mul2Table GR_mul(R.value());
29✔
64
         if(auto egsr = GR_mul.mul2_vartime(ne * r_inv, ss * r_inv)) {
29✔
65
            return egsr.value();
58✔
66
         }
×
67
      }
58✔
68
   }
×
69

70
   throw Decoding_Error("Failed to recover ECDSA public key from signature/msg pair");
×
71
}
58✔
72

73
}  // namespace
74

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

79
std::unique_ptr<Private_Key> ECDSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
28✔
80
   return std::make_unique<ECDSA_PrivateKey>(rng, domain());
56✔
81
}
82

83
uint8_t ECDSA_PublicKey::recovery_param(const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s) const {
12✔
84
   const auto this_key = this->_public_ec_point().serialize_compressed();
12✔
85

86
   for(uint8_t v = 0; v != 4; ++v) {
17✔
87
      try {
17✔
88
         const auto R = recover_ecdsa_public_key(this->domain(), msg, r, s, v);
17✔
89

90
         if(R.serialize_compressed() == this_key) {
34✔
91
            return v;
24✔
92
         }
93
      } catch(Decoding_Error&) {
17✔
94
         // try the next v
95
      }
×
96
   }
97

98
   throw Internal_Error("Could not determine ECDSA recovery parameter");
×
99
}
12✔
100

101
std::unique_ptr<Public_Key> ECDSA_PrivateKey::public_key() const {
487✔
102
   return std::make_unique<ECDSA_PublicKey>(domain(), _public_ec_point());
487✔
103
}
104

105
bool ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
32✔
106
   if(!EC_PrivateKey::check_key(rng, strong)) {
32✔
107
      return false;
108
   }
109

110
   if(!strong) {
32✔
111
      return true;
112
   }
113

114
   return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
31✔
115
}
116

117
namespace {
118

119
/**
120
* ECDSA signature operation
121
*/
122
class ECDSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
×
123
   public:
124
      ECDSA_Signature_Operation(const ECDSA_PrivateKey& ecdsa, std::string_view padding, RandomNumberGenerator& rng) :
2,183✔
125
            PK_Ops::Signature_with_Hash(padding),
126
            m_group(ecdsa.domain()),
2,183✔
127
            m_x(ecdsa._private_key()),
2,183✔
128
            m_b(EC_Scalar::random(m_group, rng)) {
4,366✔
129
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
130
         m_rfc6979 = std::make_unique<RFC6979_Nonce_Generator>(
6,549✔
131
            this->rfc6979_hash_function(), m_group.get_order_bits(), ecdsa._private_key());
4,366✔
132
#endif
133
      }
2,183✔
134

135
      size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
127✔
136

137
      std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) override;
138

139
      AlgorithmIdentifier algorithm_identifier() const override;
140

141
   private:
142
      const EC_Group m_group;
143
      const EC_Scalar m_x;
144

145
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
146
      std::unique_ptr<RFC6979_Nonce_Generator> m_rfc6979;
147
#endif
148

149
      EC_Scalar m_b;
150
};
151

152
AlgorithmIdentifier ECDSA_Signature_Operation::algorithm_identifier() const {
1,643✔
153
   const std::string full_name = "ECDSA/" + hash_function();
3,286✔
154
   const OID oid = OID::from_string(full_name);
1,643✔
155
   return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
1,643✔
156
}
1,643✔
157

158
std::vector<uint8_t> ECDSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
5,947✔
159
   const auto m = EC_Scalar::from_bytes_with_trunc(m_group, msg);
5,947✔
160

161
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
162
   const auto k = m_rfc6979->nonce_for(m_group, m);
5,947✔
163
#else
164
   const auto k = EC_Scalar::random(m_group, rng);
165
#endif
166

167
   const auto r = EC_Scalar::gk_x_mod_order(k, rng);
5,947✔
168

169
   /*
170
   * Blind the inputs
171
   *
172
   * Here we are computing (x*r+m)/k
173
   *
174
   * Instead have a random b and compute (k*b)^-1
175
   *
176
   * Then compute (x*r+m) as (x*r*b + m*b)
177
   *
178
   * Finally (x*r*b + m*b)/(k*b) = (x*r+m)/k
179
   *
180
   * This effectively blinds both the inversion as well as the various scalar
181
   * multiplications. All of these operations should be constant-time anyway but
182
   * blinding is very cheap and may help if either the compiler introduces
183
   * variable-time behavior, or for the case of EM/power side channel attacks [1].
184
   *
185
   * [1] But note that such attacks are currently outside of Botan's threat model.
186
   *
187
   */
188
   const auto k_inv = (m_b * k).invert();
5,947✔
189

190
   const auto xr_m = ((m_x * m_b) * r) + (m * m_b);
5,947✔
191

192
   const auto s = (k_inv * xr_m);
5,947✔
193

194
   // Generate the next blinding value via modular squaring
195
   m_b.square_self();
5,947✔
196

197
   // With overwhelming probability, a bug rather than actual zero r/s
198
   if(r.is_zero() || s.is_zero()) {
5,947✔
199
      throw Internal_Error("During ECDSA signature generated zero r/s");
×
200
   }
201

202
   return EC_Scalar::serialize_pair(r, s);
11,894✔
203
}
5,947✔
204

205
/**
206
* ECDSA verification operation
207
*/
208
class ECDSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
×
209
   public:
210
      ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, std::string_view padding) :
12,783✔
211
            PK_Ops::Verification_with_Hash(padding), m_group(ecdsa.domain()), m_gy_mul(ecdsa._public_ec_point()) {}
12,783✔
212

213
      ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, const AlgorithmIdentifier& alg_id) :
4,456✔
214
            PK_Ops::Verification_with_Hash(alg_id, "ECDSA", true),
215
            m_group(ecdsa.domain()),
4,456✔
216
            m_gy_mul(ecdsa._public_ec_point()) {}
8,912✔
217

218
      bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) override;
219

220
   private:
221
      const EC_Group m_group;
222
      const EC_Group::Mul2Table m_gy_mul;
223
};
224

225
bool ECDSA_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
28,599✔
226
   if(auto rs = EC_Scalar::deserialize_pair(m_group, sig)) {
28,599✔
227
      const auto& [r, s] = rs.value();
24,161✔
228

229
      if(r.is_nonzero() && s.is_nonzero()) {
48,312✔
230
         const auto m = EC_Scalar::from_bytes_with_trunc(m_group, msg);
24,142✔
231

232
         const auto w = s.invert_vartime();
24,142✔
233

234
         // Check if r == x_coord(g*w*m + y*w*r) % n
235
         return m_gy_mul.mul2_vartime_x_mod_order_eq(r, w, m, r);
24,142✔
236
      }
24,142✔
237
   }
24,142✔
238

239
   return false;
4,457✔
240
}
241

242
}  // namespace
243

244
std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_verification_op(std::string_view params,
49,485✔
245
                                                                              std::string_view provider) const {
246
   if(provider == "base" || provider.empty()) {
61,731✔
247
      return std::make_unique<ECDSA_Verification_Operation>(*this, params);
12,783✔
248
   }
249

250
   throw Provider_Not_Found(algo_name(), provider);
73,404✔
251
}
252

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

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

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

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

272
}  // 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