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

randombit / botan / 13230468750

09 Feb 2025 11:07PM UTC coverage: 91.651% (-0.008%) from 91.659%
13230468750

Pull #4647

github

web-flow
Merge 8b67be1f1 into 7deaa69bb
Pull Request #4647: Avoid using mem_ops.h or assert.h in public headers

94826 of 103464 relevant lines covered (91.65%)

11402585.19 hits per line

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

95.0
/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/divide.h>
13
#include <botan/internal/dl_scheme.h>
14
#include <botan/internal/keypair.h>
15
#include <botan/internal/pk_ops_impl.h>
16

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

21
namespace Botan {
22

23
std::optional<size_t> DSA_PublicKey::_signature_element_size_for_DER_encoding() const {
488✔
24
   return m_public_key->group().q_bytes();
488✔
25
}
26

27
size_t DSA_PublicKey::estimated_strength() const {
28✔
28
   return m_public_key->estimated_strength();
28✔
29
}
30

31
size_t DSA_PublicKey::key_length() const {
1✔
32
   return m_public_key->p_bits();
1✔
33
}
34

35
const BigInt& DSA_PublicKey::get_int_field(std::string_view field) const {
8✔
36
   return m_public_key->get_int_field(algo_name(), field);
8✔
37
}
38

39
AlgorithmIdentifier DSA_PublicKey::algorithm_identifier() const {
93✔
40
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_57));
186✔
41
}
42

43
std::vector<uint8_t> DSA_PublicKey::raw_public_key_bits() const {
1✔
44
   return m_public_key->public_key_as_bytes();
1✔
45
}
46

47
std::vector<uint8_t> DSA_PublicKey::public_key_bits() const {
68✔
48
   return m_public_key->DER_encode();
68✔
49
}
50

51
bool DSA_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
7✔
52
   return m_public_key->check_key(rng, strong);
7✔
53
}
54

55
std::unique_ptr<Private_Key> DSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
1✔
56
   return std::make_unique<DSA_PrivateKey>(rng, m_public_key->group());
2✔
57
}
58

59
DSA_PublicKey::DSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
234✔
60
   m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
234✔
61

62
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
121✔
63
}
234✔
64

65
DSA_PublicKey::DSA_PublicKey(const DL_Group& group, const BigInt& y) {
5✔
66
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
5✔
67

68
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
5✔
69
}
5✔
70

71
DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
21✔
72
   BOTAN_ARG_CHECK(group.has_q(), "Q parameter must be set for DSA");
21✔
73

74
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
21✔
75
   m_public_key = m_private_key->public_key();
21✔
76
}
21✔
77

78
DSA_PrivateKey::DSA_PrivateKey(const DL_Group& group, const BigInt& x) {
325✔
79
   BOTAN_ARG_CHECK(group.has_q(), "Q parameter must be set for DSA");
325✔
80

81
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
325✔
82
   m_public_key = m_private_key->public_key();
325✔
83
}
325✔
84

85
DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
316✔
86
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
316✔
87
   m_public_key = m_private_key->public_key();
57✔
88

89
   BOTAN_ARG_CHECK(m_private_key->group().has_q(), "Q parameter must be set for DSA");
57✔
90
}
316✔
91

92
bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
5✔
93
   if(!m_private_key->check_key(rng, strong)) {
5✔
94
      return false;
95
   }
96

97
   if(m_private_key->private_key() >= m_private_key->group().get_q()) {
5✔
98
      return false;
99
   }
100

101
   return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
5✔
102
}
103

104
secure_vector<uint8_t> DSA_PrivateKey::private_key_bits() const {
33✔
105
   return m_private_key->DER_encode();
33✔
106
}
107

108
secure_vector<uint8_t> DSA_PrivateKey::raw_private_key_bits() const {
×
109
   return m_private_key->raw_private_key_bits();
×
110
}
111

112
const BigInt& DSA_PrivateKey::get_int_field(std::string_view field) const {
10✔
113
   return m_private_key->get_int_field(algo_name(), field);
10✔
114
}
115

116
std::unique_ptr<Public_Key> DSA_PrivateKey::public_key() const {
336✔
117
   // can't use make_unique here due to private constructor
118
   return std::unique_ptr<DSA_PublicKey>(new DSA_PublicKey(m_public_key));
336✔
119
}
120

121
namespace {
122

123
/**
124
* Object that can create a DSA signature
125
*/
126
class DSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
×
127
   public:
128
      DSA_Signature_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
69✔
129
                              std::string_view emsa,
130
                              RandomNumberGenerator& rng) :
69✔
131
            PK_Ops::Signature_with_Hash(emsa), m_key(key) {
69✔
132
         m_b = BigInt::random_integer(rng, 2, m_key->group().get_q());
138✔
133
         m_b_inv = m_key->group().inverse_mod_q(m_b);
138✔
134
      }
69✔
135

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

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

140
      AlgorithmIdentifier algorithm_identifier() const override;
141

142
   private:
143
      std::shared_ptr<const DL_PrivateKey> m_key;
144
      BigInt m_b, m_b_inv;
145
};
146

147
AlgorithmIdentifier DSA_Signature_Operation::algorithm_identifier() const {
37✔
148
   const std::string full_name = "DSA/" + hash_function();
74✔
149
   const OID oid = OID::from_string(full_name);
37✔
150
   return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
37✔
151
}
37✔
152

153
std::vector<uint8_t> DSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
79✔
154
   const DL_Group& group = m_key->group();
79✔
155
   const BigInt& q = group.get_q();
79✔
156

157
   BigInt m = BigInt::from_bytes_with_max_bits(msg.data(), msg.size(), group.q_bits());
79✔
158

159
   if(m >= q) {
79✔
160
      m -= q;
29✔
161
   }
162

163
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
164
   BOTAN_UNUSED(rng);
79✔
165
   const BigInt k = generate_rfc6979_nonce(m_key->private_key(), q, m, this->rfc6979_hash_function());
79✔
166
#else
167
   const BigInt k = BigInt::random_integer(rng, 1, q);
168
#endif
169

170
   const BigInt k_inv = group.inverse_mod_q(group.mod_q(m_b * k)) * m_b;
79✔
171

172
   /*
173
   * It may not be strictly necessary for the reduction (g^k mod p) mod q to be
174
   * const time, since r is published as part of the signature, and deriving
175
   * anything useful about k from g^k mod p would seem to require computing a
176
   * discrete logarithm.
177
   *
178
   * However it only increases the cost of signatures by about 7-10%, and DSA is
179
   * only for legacy use anyway so we don't care about the performance so much.
180
   */
181
   const BigInt r = ct_modulo(group.power_g_p(k, group.q_bits()), group.get_q());
79✔
182

183
   /*
184
   * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
185
   */
186
   m_b = group.square_mod_q(m_b);
79✔
187
   m_b_inv = group.square_mod_q(m_b_inv);
79✔
188

189
   m = group.multiply_mod_q(m_b, m);
79✔
190
   const BigInt xr = group.multiply_mod_q(m_b, m_key->private_key(), r);
79✔
191

192
   const BigInt s = group.multiply_mod_q(m_b_inv, k_inv, group.mod_q(xr + m));
79✔
193

194
   // With overwhelming probability, a bug rather than actual zero r/s
195
   if(r.is_zero() || s.is_zero()) {
158✔
196
      throw Internal_Error("Computed zero r/s during DSA signature");
×
197
   }
198

199
   return unlock(BigInt::encode_fixed_length_int_pair(r, s, q.bytes()));
237✔
200
}
79✔
201

202
/**
203
* Object that can verify a DSA signature
204
*/
205
class DSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
×
206
   public:
207
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, std::string_view emsa) :
336✔
208
            PK_Ops::Verification_with_Hash(emsa), m_key(key) {}
336✔
209

210
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, const AlgorithmIdentifier& alg_id) :
80✔
211
            PK_Ops::Verification_with_Hash(alg_id, "DSA"), m_key(key) {}
80✔
212

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

215
   private:
216
      std::shared_ptr<const DL_PublicKey> m_key;
217
};
218

219
bool DSA_Verification_Operation::verify(std::span<const uint8_t> input, std::span<const uint8_t> sig) {
7,241✔
220
   const auto group = m_key->group();
7,241✔
221

222
   const BigInt& q = group.get_q();
7,241✔
223
   const size_t q_bytes = q.bytes();
7,241✔
224

225
   if(sig.size() != 2 * q_bytes) {
7,241✔
226
      return false;
227
   }
228

229
   BigInt r(sig.first(q_bytes));
7,241✔
230
   BigInt s(sig.last(q_bytes));
7,241✔
231

232
   if(r == 0 || r >= q || s == 0 || s >= q) {
27,901✔
233
      return false;
406✔
234
   }
235

236
   BigInt i = BigInt::from_bytes_with_max_bits(input.data(), input.size(), group.q_bits());
6,835✔
237
   if(i >= q) {
6,835✔
238
      i -= q;
1,297✔
239
   }
240

241
   s = group.inverse_mod_q(s);
6,835✔
242

243
   const BigInt sr = group.multiply_mod_q(s, r);
6,835✔
244
   const BigInt si = group.multiply_mod_q(s, i);
6,835✔
245

246
   s = group.multi_exponentiate(si, m_key->public_key(), sr);
6,835✔
247

248
   // s is too big for Barrett, and verification doesn't need to be const-time
249
   return (s % group.get_q() == r);
13,670✔
250
}
14,482✔
251

252
}  // namespace
253

254
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_verification_op(std::string_view params,
1,311✔
255
                                                                            std::string_view provider) const {
256
   if(provider == "base" || provider.empty()) {
1,637✔
257
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, params);
336✔
258
   }
259
   throw Provider_Not_Found(algo_name(), provider);
1,950✔
260
}
261

262
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_x509_verification_op(
80✔
263
   const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
264
   if(provider == "base" || provider.empty()) {
80✔
265
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, signature_algorithm);
80✔
266
   }
267

268
   throw Provider_Not_Found(algo_name(), provider);
×
269
}
270

271
std::unique_ptr<PK_Ops::Signature> DSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
132✔
272
                                                                       std::string_view params,
273
                                                                       std::string_view provider) const {
274
   if(provider == "base" || provider.empty()) {
154✔
275
      return std::make_unique<DSA_Signature_Operation>(this->m_private_key, params, rng);
69✔
276
   }
277
   throw Provider_Not_Found(algo_name(), provider);
126✔
278
}
279

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