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

randombit / botan / 6642806734

25 Oct 2023 03:40PM UTC coverage: 91.687% (+0.01%) from 91.677%
6642806734

push

github

web-flow
Merge pull request #3770 from Rohde-Schwarz/feature/pubkey_create_another

Feature: `AsymmetricKey::generate_another()`

80170 of 87439 relevant lines covered (91.69%)

8623809.04 hits per line

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

89.09
/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
std::unique_ptr<Private_Key> ECDSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
4✔
81
   return std::make_unique<ECDSA_PrivateKey>(rng, domain());
8✔
82
}
83

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

89
         if(R == this->public_point()) {
12✔
90
            return v;
8✔
91
         }
92
      } catch(Decoding_Error&) {
12✔
93
         // try the next v
94
      }
×
95
   }
96

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

100
std::unique_ptr<Public_Key> ECDSA_PrivateKey::public_key() const {
200✔
101
   return std::make_unique<ECDSA_PublicKey>(domain(), public_point());
200✔
102
}
103

104
bool ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
8✔
105
   if(!public_point().on_the_curve()) {
8✔
106
      return false;
107
   }
108

109
   if(!strong) {
8✔
110
      return true;
111
   }
112

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

116
namespace {
117

118
/**
119
* ECDSA signature operation
120
*/
121
class 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) :
394✔
124
            PK_Ops::Signature_with_Hash(padding), m_group(ecdsa.domain()), m_x(ecdsa.private_value()) {
394✔
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);
394✔
127
#endif
128

129
         m_b = m_group.random_scalar(rng);
788✔
130
         m_b_inv = m_group.inverse_mod_order(m_b);
788✔
131
      }
394✔
132

133
      size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
108✔
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

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

158
secure_vector<uint8_t> ECDSA_Signature_Operation::raw_sign(const uint8_t msg[],
404✔
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());
404✔
162

163
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
164
   const BigInt k = m_rfc6979->nonce_for(m);
404✔
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));
404✔
170

171
   const BigInt k_inv = m_group.inverse_mod_order(k);
404✔
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);
404✔
177
   m_b_inv = m_group.square_mod_order(m_b_inv);
404✔
178

179
   m = m_group.multiply_mod_order(m_b, m_group.mod_order(m));
808✔
180
   const BigInt xr_m = m_group.mod_order(m_group.multiply_mod_order(m_x, m_b, r) + m);
808✔
181

182
   const BigInt s = m_group.multiply_mod_order(k_inv, xr_m, m_b_inv);
404✔
183

184
   // With overwhelming probability, a bug rather than actual zero r/s
185
   if(r.is_zero() || s.is_zero()) {
808✔
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());
404✔
190
}
2,424✔
191

192
/**
193
* ECDSA verification operation
194
*/
195
class ECDSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
×
196
   public:
197
      ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, std::string_view padding) :
12,415✔
198
            PK_Ops::Verification_with_Hash(padding),
199
            m_group(ecdsa.domain()),
16,330✔
200
            m_gy_mul(m_group.get_base_point(), ecdsa.public_point()) {}
12,415✔
201

202
      ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa, const AlgorithmIdentifier& alg_id) :
2,119✔
203
            PK_Ops::Verification_with_Hash(alg_id, "ECDSA", true),
204
            m_group(ecdsa.domain()),
4,216✔
205
            m_gy_mul(m_group.get_base_point(), ecdsa.public_point()) {}
2,119✔
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

214
bool ECDSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) {
16,582✔
215
   if(sig_len != m_group.get_order_bytes() * 2) {
16,582✔
216
      return false;
217
   }
218

219
   const BigInt e = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
13,584✔
220

221
   const BigInt r(sig, sig_len / 2);
13,584✔
222
   const BigInt s(sig + sig_len / 2, sig_len / 2);
13,584✔
223

224
   // Cannot be negative here since we just decoded from binary
225
   if(r.is_zero() || s.is_zero()) {
40,307✔
226
      return false;
590✔
227
   }
228

229
   if(r >= m_group.get_order() || s >= m_group.get_order()) {
12,994✔
230
      return false;
664✔
231
   }
232

233
   const BigInt w = m_group.inverse_mod_order(s);
12,330✔
234

235
   const BigInt u1 = m_group.multiply_mod_order(m_group.mod_order(e), w);
12,330✔
236
   const BigInt u2 = m_group.multiply_mod_order(r, w);
12,330✔
237
   const EC_Point R = m_gy_mul.multi_exp(u1, u2);
12,330✔
238

239
   if(R.is_zero()) {
24,546✔
240
      return false;
241
   }
242

243
   const BigInt v = m_group.mod_order(R.get_affine_x());
12,219✔
244
   return (v == r);
12,219✔
245
}
94,324✔
246

247
}  // namespace
248

249
std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_verification_op(std::string_view params,
48,928✔
250
                                                                              std::string_view provider) const {
251
   if(provider == "base" || provider.empty()) {
61,107✔
252
      return std::make_unique<ECDSA_Verification_Operation>(*this, params);
12,415✔
253
   }
254

255
   throw Provider_Not_Found(algo_name(), provider);
73,026✔
256
}
257

258
std::unique_ptr<PK_Ops::Verification> ECDSA_PublicKey::create_x509_verification_op(
2,119✔
259
   const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
260
   if(provider == "base" || provider.empty()) {
2,119✔
261
      return std::make_unique<ECDSA_Verification_Operation>(*this, signature_algorithm);
2,119✔
262
   }
263

264
   throw Provider_Not_Found(algo_name(), provider);
×
265
}
266

267
std::unique_ptr<PK_Ops::Signature> ECDSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
706✔
268
                                                                         std::string_view params,
269
                                                                         std::string_view provider) const {
270
   if(provider == "base" || provider.empty()) {
813✔
271
      return std::make_unique<ECDSA_Signature_Operation>(*this, params, rng);
394✔
272
   }
273

274
   throw Provider_Not_Found(algo_name(), provider);
624✔
275
}
276

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