• 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

94.39
/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/numthry.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
size_t DSA_PublicKey::message_part_size() const { return m_public_key->group().q_bytes(); }
442✔
24

25
size_t DSA_PublicKey::estimated_strength() const { return m_public_key->estimated_strength(); }
24✔
26

27
size_t DSA_PublicKey::key_length() const { return m_public_key->p_bits(); }
×
28

29
const BigInt& DSA_PublicKey::get_int_field(std::string_view field) const {
8✔
30
   return m_public_key->get_int_field(algo_name(), field);
8✔
31
}
32

33
AlgorithmIdentifier DSA_PublicKey::algorithm_identifier() const {
92✔
34
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_57));
184✔
35
}
36

37
std::vector<uint8_t> DSA_PublicKey::public_key_bits() const { return m_public_key->DER_encode(); }
65✔
38

39
bool DSA_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
7✔
40
   return m_public_key->check_key(rng, strong);
7✔
41
}
42

43
DSA_PublicKey::DSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
235✔
44
   m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
235✔
45

46
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
121✔
47
}
235✔
48

49
DSA_PublicKey::DSA_PublicKey(const DL_Group& group, const BigInt& y) {
4✔
50
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
4✔
51

52
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
4✔
53
}
4✔
54

55
DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
14✔
56
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
14✔
57
   m_public_key = m_private_key->public_key();
14✔
58
}
14✔
59

60
DSA_PrivateKey::DSA_PrivateKey(const DL_Group& group, const BigInt& x) {
325✔
61
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
325✔
62
   m_public_key = m_private_key->public_key();
325✔
63
}
325✔
64

65
DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
212✔
66
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
212✔
67
   m_public_key = m_private_key->public_key();
66✔
68
}
212✔
69

70
bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
5✔
71
   if(!m_private_key->check_key(rng, strong)) {
5✔
72
      return false;
73
   }
74

75
   if(m_private_key->private_key() >= m_private_key->group().get_q()) {
5✔
76
      return false;
77
   }
78

79
   return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
5✔
80
}
81

82
secure_vector<uint8_t> DSA_PrivateKey::private_key_bits() const { return m_private_key->DER_encode(); }
31✔
83

84
secure_vector<uint8_t> DSA_PrivateKey::raw_private_key_bits() const { return m_private_key->raw_private_key_bits(); }
×
85

86
const BigInt& DSA_PrivateKey::get_int_field(std::string_view field) const {
10✔
87
   return m_private_key->get_int_field(algo_name(), field);
10✔
88
}
89

90
std::unique_ptr<Public_Key> DSA_PrivateKey::public_key() const {
305✔
91
   // can't use make_unique here due to private constructor
92
   return std::unique_ptr<DSA_PublicKey>(new DSA_PublicKey(m_public_key));
610✔
93
}
94

95
namespace {
96

97
/**
98
* Object that can create a DSA signature
99
*/
100
class DSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
×
101
   public:
102
      DSA_Signature_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
68✔
103
                              std::string_view emsa,
104
                              RandomNumberGenerator& rng) :
68✔
105
            PK_Ops::Signature_with_Hash(emsa), m_key(key) {
68✔
106
         m_b = BigInt::random_integer(rng, 2, m_key->group().get_q());
136✔
107
         m_b_inv = m_key->group().inverse_mod_q(m_b);
136✔
108
      }
68✔
109

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

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

114
      AlgorithmIdentifier algorithm_identifier() const override;
115

116
   private:
117
      std::shared_ptr<const DL_PrivateKey> m_key;
118
      BigInt m_b, m_b_inv;
119
};
120

121
AlgorithmIdentifier DSA_Signature_Operation::algorithm_identifier() const {
37✔
122
   const std::string full_name = "DSA/" + hash_function();
37✔
123
   const OID oid = OID::from_string(full_name);
37✔
124
   return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
37✔
125
}
37✔
126

127
secure_vector<uint8_t> DSA_Signature_Operation::raw_sign(const uint8_t msg[],
77✔
128
                                                         size_t msg_len,
129
                                                         RandomNumberGenerator& rng) {
130
   const DL_Group& group = m_key->group();
77✔
131
   const BigInt& q = group.get_q();
77✔
132

133
   BigInt m = BigInt::from_bytes_with_max_bits(msg, msg_len, group.q_bits());
77✔
134

135
   if(m >= q) {
77✔
136
      m -= q;
25✔
137
   }
138

139
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
140
   BOTAN_UNUSED(rng);
77✔
141
   const BigInt k = generate_rfc6979_nonce(m_key->private_key(), q, m, this->rfc6979_hash_function());
77✔
142
#else
143
   const BigInt k = BigInt::random_integer(rng, 1, q);
144
#endif
145

146
   const BigInt k_inv = group.inverse_mod_q(k);
77✔
147

148
   /*
149
   * It may not be strictly necessary for the reduction (g^k mod p) mod q to be
150
   * const time, since r is published as part of the signature, and deriving
151
   * anything useful about k from g^k mod p would seem to require computing a
152
   * discrete logarithm.
153
   *
154
   * However it only increases the cost of signatures by about 7-10%, and DSA is
155
   * only for legacy use anyway so we don't care about the performance so much.
156
   */
157
   const BigInt r = ct_modulo(group.power_g_p(k, group.q_bits()), group.get_q());
77✔
158

159
   /*
160
   * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
161
   */
162
   m_b = group.square_mod_q(m_b);
77✔
163
   m_b_inv = group.square_mod_q(m_b_inv);
77✔
164

165
   m = group.multiply_mod_q(m_b, m);
77✔
166
   const BigInt xr = group.multiply_mod_q(m_b, m_key->private_key(), r);
77✔
167

168
   const BigInt s = group.multiply_mod_q(m_b_inv, k_inv, group.mod_q(xr + m));
154✔
169

170
   // With overwhelming probability, a bug rather than actual zero r/s
171
   if(r.is_zero() || s.is_zero()) {
154✔
172
      throw Internal_Error("Computed zero r/s during DSA signature");
×
173
   }
174

175
   return BigInt::encode_fixed_length_int_pair(r, s, q.bytes());
77✔
176
}
462✔
177

178
/**
179
* Object that can verify a DSA signature
180
*/
181
class DSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
×
182
   public:
183
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, std::string_view emsa) :
335✔
184
            PK_Ops::Verification_with_Hash(emsa), m_key(key) {}
335✔
185

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

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

191
   private:
192
      std::shared_ptr<const DL_PublicKey> m_key;
193
};
194

195
bool DSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) {
7,236✔
196
   const auto group = m_key->group();
7,236✔
197

198
   const BigInt& q = group.get_q();
7,236✔
199
   const size_t q_bytes = q.bytes();
7,236✔
200

201
   if(sig_len != 2 * q_bytes) {
7,236✔
202
      return false;
203
   }
204

205
   BigInt r(sig, q_bytes);
7,236✔
206
   BigInt s(sig + q_bytes, q_bytes);
7,236✔
207
   BigInt i = BigInt::from_bytes_with_max_bits(msg, msg_len, group.q_bits());
7,236✔
208
   if(i >= q) {
7,236✔
209
      i -= q;
1,370✔
210
   }
211

212
   if(r <= 0 || r >= q || s <= 0 || s >= q) {
27,889✔
213
      return false;
397✔
214
   }
215

216
   s = inverse_mod(s, q);
6,839✔
217

218
   const BigInt sr = group.multiply_mod_q(s, r);
6,839✔
219
   const BigInt si = group.multiply_mod_q(s, i);
6,839✔
220

221
   s = group.multi_exponentiate(si, m_key->public_key(), sr);
6,839✔
222

223
   // s is too big for Barrett, and verification doesn't need to be const-time
224
   return (s % group.get_q() == r);
20,517✔
225
}
43,019✔
226

227
}  // namespace
228

229
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_verification_op(std::string_view params,
1,310✔
230
                                                                            std::string_view provider) const {
231
   if(provider == "base" || provider.empty()) {
1,635✔
232
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, params);
335✔
233
   }
234
   throw Provider_Not_Found(algo_name(), provider);
1,950✔
235
}
236

237
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_x509_verification_op(
80✔
238
   const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
239
   if(provider == "base" || provider.empty()) {
80✔
240
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, signature_algorithm);
80✔
241
   }
242

243
   throw Provider_Not_Found(algo_name(), provider);
×
244
}
245

246
std::unique_ptr<PK_Ops::Signature> DSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
131✔
247
                                                                       std::string_view params,
248
                                                                       std::string_view provider) const {
249
   if(provider == "base" || provider.empty()) {
152✔
250
      return std::make_unique<DSA_Signature_Operation>(this->m_private_key, params, rng);
68✔
251
   }
252
   throw Provider_Not_Found(algo_name(), provider);
126✔
253
}
254

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