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

randombit / botan / 25650639339

10 May 2026 07:03PM UTC coverage: 89.326% (-0.002%) from 89.328%
25650639339

push

github

web-flow
Merge pull request #5592 from randombit/jack/bn-hardening

Various BigInt/mp related hardenings and bug fixes

107853 of 120741 relevant lines covered (89.33%)

11294230.95 hits per line

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

95.38
/src/lib/pubkey/dsa/dsa.cpp
1
/*
2
* DSA
3
* (C) 1999-2010,2014,2016,2023 Jack Lloyd
4
* (C) 2016 René Korthaus
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/dsa.h>
10

11
#include <botan/assert.h>
12
#include <botan/internal/buffer_stuffer.h>
13
#include <botan/internal/divide.h>
14
#include <botan/internal/dl_scheme.h>
15
#include <botan/internal/keypair.h>
16
#include <botan/internal/pk_ops_impl.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
void check_dsa_group(const DL_Group& group) {
460✔
27
   BOTAN_ARG_CHECK(group.has_q(), "Q parameter must be set for DSA");
460✔
28
   // All versions of FIPS 186 have required that Q be at least 160 bits
29
   BOTAN_ARG_CHECK(group.q_bits() >= 160, "DSA Q parameter must be at least 160 bits");
460✔
30
}
460✔
31

32
}  // namespace
33

34
std::optional<size_t> DSA_PublicKey::_signature_element_size_for_DER_encoding() const {
625✔
35
   return m_public_key->group().q_bytes();
625✔
36
}
37

38
size_t DSA_PublicKey::estimated_strength() const {
28✔
39
   return m_public_key->estimated_strength();
28✔
40
}
41

42
size_t DSA_PublicKey::key_length() const {
1✔
43
   return m_public_key->p_bits();
1✔
44
}
45

46
const BigInt& DSA_PublicKey::get_int_field(std::string_view field) const {
8✔
47
   return m_public_key->get_int_field(algo_name(), field);
8✔
48
}
49

50
AlgorithmIdentifier DSA_PublicKey::algorithm_identifier() const {
93✔
51
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_57));
186✔
52
}
53

54
std::vector<uint8_t> DSA_PublicKey::raw_public_key_bits() const {
1✔
55
   return m_public_key->public_key_as_bytes();
1✔
56
}
57

58
std::vector<uint8_t> DSA_PublicKey::public_key_bits() const {
68✔
59
   return m_public_key->DER_encode();
68✔
60
}
61

62
bool DSA_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
7✔
63
   return m_public_key->check_key(rng, strong);
7✔
64
}
65

66
std::unique_ptr<Private_Key> DSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
1✔
67
   return std::make_unique<DSA_PrivateKey>(rng, m_public_key->group());
2✔
68
}
69

70
DSA_PublicKey::DSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
80✔
71
   m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
80✔
72

73
   check_dsa_group(m_public_key->group());
80✔
74
}
80✔
75

76
DSA_PublicKey::DSA_PublicKey(const DL_Group& group, const BigInt& y) {
5✔
77
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
5✔
78

79
   check_dsa_group(m_public_key->group());
5✔
80
}
5✔
81

82
DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
31✔
83
   check_dsa_group(group);
31✔
84

85
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
31✔
86
   m_public_key = m_private_key->public_key();
31✔
87
}
31✔
88

89
DSA_PrivateKey::DSA_PrivateKey(const DL_Group& group, const BigInt& x) {
325✔
90
   check_dsa_group(group);
325✔
91

92
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
325✔
93
   m_public_key = m_private_key->public_key();
325✔
94
}
325✔
95

96
DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
28✔
97
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
28✔
98
   m_public_key = m_private_key->public_key();
19✔
99

100
   check_dsa_group(m_private_key->group());
19✔
101
}
28✔
102

103
bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
15✔
104
   if(!m_private_key->check_key(rng, strong)) {
15✔
105
      return false;
106
   }
107

108
   if(m_private_key->private_key() >= m_private_key->group().get_q()) {
15✔
109
      return false;
110
   }
111

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

115
secure_vector<uint8_t> DSA_PrivateKey::private_key_bits() const {
33✔
116
   return m_private_key->DER_encode();
33✔
117
}
118

119
secure_vector<uint8_t> DSA_PrivateKey::raw_private_key_bits() const {
×
120
   return m_private_key->raw_private_key_bits();
×
121
}
122

123
const BigInt& DSA_PrivateKey::get_int_field(std::string_view field) const {
10✔
124
   return m_private_key->get_int_field(algo_name(), field);
10✔
125
}
126

127
std::unique_ptr<Public_Key> DSA_PrivateKey::public_key() const {
347✔
128
   // can't use make_unique here due to private constructor
129
   return std::unique_ptr<DSA_PublicKey>(new DSA_PublicKey(m_public_key));
347✔
130
}
131

132
namespace {
133

134
/**
135
* Object that can create a DSA signature
136
*/
137
class DSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
×
138
   public:
139
      DSA_Signature_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
90✔
140
                              std::string_view hash_fn,
141
                              RandomNumberGenerator& rng) :
90✔
142
            PK_Ops::Signature_with_Hash(hash_fn), m_key(key) {
90✔
143
         m_b = BigInt::random_integer(rng, BigInt::from_s32(2), m_key->group().get_q());
180✔
144
         m_b_inv = m_key->group().inverse_mod_q(m_b);
180✔
145
      }
90✔
146

147
      size_t signature_length() const override { return 2 * m_key->group().q_bytes(); }
23✔
148

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

151
      AlgorithmIdentifier algorithm_identifier() const override;
152

153
   private:
154
      std::shared_ptr<const DL_PrivateKey> m_key;
155
      BigInt m_b, m_b_inv;
156
};
157

158
AlgorithmIdentifier DSA_Signature_Operation::algorithm_identifier() const {
37✔
159
   const std::string full_name = "DSA/" + hash_function();
74✔
160
   const OID oid = OID::from_string(full_name);
37✔
161
   return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
37✔
162
}
37✔
163

164
std::vector<uint8_t> DSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
100✔
165
   const DL_Group& group = m_key->group();
100✔
166
   const BigInt& q = group.get_q();
100✔
167

168
   BigInt m = BigInt::from_bytes_with_max_bits(msg.data(), msg.size(), group.q_bits());
100✔
169

170
   if(m >= q) {
100✔
171
      m -= q;
44✔
172
   }
173

174
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
175
   BOTAN_UNUSED(rng);
100✔
176
   const BigInt k = generate_rfc6979_nonce(m_key->private_key(), q, m, this->rfc6979_hash_function());
100✔
177
#else
178
   const BigInt k = BigInt::random_integer(rng, 1, q);
179
#endif
180

181
   const BigInt k_inv = group.multiply_mod_q(group.inverse_mod_q(group.mod_q(m_b * k)), m_b);
100✔
182

183
   /*
184
   * It may not be strictly necessary for the reduction (g^k mod p) mod q to be
185
   * const time, since r is published as part of the signature, and deriving
186
   * anything useful about k from g^k mod p would seem to require computing a
187
   * discrete logarithm.
188
   *
189
   * However it only increases the cost of signatures by about 7-10%, and DSA is
190
   * only for legacy use anyway so we don't care about the performance so much.
191
   */
192
   const BigInt r = ct_modulo(group.power_g_p(k, group.q_bits()), group.get_q());
100✔
193

194
   /*
195
   * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
196
   */
197
   m_b = group.square_mod_q(m_b);
100✔
198
   m_b_inv = group.square_mod_q(m_b_inv);
100✔
199

200
   m = group.multiply_mod_q(m_b, m);
100✔
201
   const BigInt xr = group.multiply_mod_q(m_b, m_key->private_key(), r);
100✔
202

203
   const BigInt s = group.multiply_mod_q(m_b_inv, k_inv, group.mod_q(xr + m));
100✔
204

205
   // With overwhelming probability, a bug rather than actual zero r/s
206
   if(r.is_zero() || s.is_zero()) {
200✔
207
      throw Internal_Error("Computed zero r/s during DSA signature");
×
208
   }
209

210
   const size_t q_bytes = q.bytes();
100✔
211
   std::vector<uint8_t> sig(2 * q_bytes);
100✔
212
   BufferStuffer stuffer(sig);
100✔
213
   r.serialize_to(stuffer.next(q_bytes));
100✔
214
   s.serialize_to(stuffer.next(q_bytes));
100✔
215
   return sig;
100✔
216
}
100✔
217

218
/**
219
* Object that can verify a DSA signature
220
*/
221
class DSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
×
222
   public:
223
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, std::string_view hash_fn) :
357✔
224
            PK_Ops::Verification_with_Hash(hash_fn), m_key(key) {}
357✔
225

226
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, const AlgorithmIdentifier& alg_id) :
39✔
227
            PK_Ops::Verification_with_Hash(alg_id, "DSA"), m_key(key) {}
39✔
228

229
      bool verify(std::span<const uint8_t> input, std::span<const uint8_t> sig) override;
230

231
   private:
232
      std::shared_ptr<const DL_PublicKey> m_key;
233
};
234

235
bool DSA_Verification_Operation::verify(std::span<const uint8_t> input, std::span<const uint8_t> sig) {
16,379✔
236
   const auto group = m_key->group();
16,379✔
237

238
   const BigInt& q = group.get_q();
16,379✔
239
   const size_t q_bytes = q.bytes();
16,379✔
240

241
   if(sig.size() != 2 * q_bytes) {
16,379✔
242
      return false;
243
   }
244

245
   const BigInt r(sig.first(q_bytes));
15,079✔
246
   BigInt s(sig.last(q_bytes));
15,079✔
247

248
   if(r == 0 || r >= q || s == 0 || s >= q) {
59,271✔
249
      return false;
388✔
250
   }
251

252
   BigInt i = BigInt::from_bytes_with_max_bits(input.data(), input.size(), group.q_bits());
14,691✔
253
   if(i >= q) {
14,691✔
254
      i -= q;
2,795✔
255
   }
256

257
   s = group.inverse_mod_q(s);
14,691✔
258

259
   // Since we already checked for s == 0 above this shouldn't happen
260
   if(s.is_zero()) {
29,382✔
261
      return false;
262
   }
263

264
   const BigInt sr = group.multiply_mod_q(s, r);
14,691✔
265
   const BigInt si = group.multiply_mod_q(s, i);
14,691✔
266

267
   s = group.multi_exponentiate(si, m_key->public_key(), sr);
14,691✔
268

269
   // s is too big for Barrett, and verification doesn't need to be const-time
270
   return (s % group.get_q() == r);
29,382✔
271
}
31,458✔
272

273
}  // namespace
274

275
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_verification_op(std::string_view params,
1,332✔
276
                                                                            std::string_view provider) const {
277
   if(provider == "base" || provider.empty()) {
1,658✔
278
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, params);
357✔
279
   }
280
   throw Provider_Not_Found(algo_name(), provider);
1,950✔
281
}
282

283
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_x509_verification_op(
39✔
284
   const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
285
   if(provider == "base" || provider.empty()) {
39✔
286
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, signature_algorithm);
39✔
287
   }
288

289
   throw Provider_Not_Found(algo_name(), provider);
×
290
}
291

292
std::unique_ptr<PK_Ops::Signature> DSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
153✔
293
                                                                       std::string_view params,
294
                                                                       std::string_view provider) const {
295
   if(provider == "base" || provider.empty()) {
175✔
296
      return std::make_unique<DSA_Signature_Operation>(this->m_private_key, params, rng);
90✔
297
   }
298
   throw Provider_Not_Found(algo_name(), provider);
126✔
299
}
300

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