Coverage Report

Created: 2024-02-25 06:16

/src/botan/src/lib/pubkey/dsa/dsa.cpp
Line
Count
Source (jump to first uncovered line)
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
185
size_t DSA_PublicKey::message_part_size() const {
24
185
   return m_public_key->group().q_bytes();
25
185
}
26
27
0
size_t DSA_PublicKey::estimated_strength() const {
28
0
   return m_public_key->estimated_strength();
29
0
}
30
31
0
size_t DSA_PublicKey::key_length() const {
32
0
   return m_public_key->p_bits();
33
0
}
34
35
0
const BigInt& DSA_PublicKey::get_int_field(std::string_view field) const {
36
0
   return m_public_key->get_int_field(algo_name(), field);
37
0
}
38
39
0
AlgorithmIdentifier DSA_PublicKey::algorithm_identifier() const {
40
0
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_57));
41
0
}
42
43
0
std::vector<uint8_t> DSA_PublicKey::public_key_bits() const {
44
0
   return m_public_key->DER_encode();
45
0
}
46
47
0
bool DSA_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
48
0
   return m_public_key->check_key(rng, strong);
49
0
}
50
51
0
std::unique_ptr<Private_Key> DSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
52
0
   return std::make_unique<DSA_PrivateKey>(rng, m_public_key->group());
53
0
}
54
55
0
DSA_PublicKey::DSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
56
0
   m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
57
58
0
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
59
0
}
Unexecuted instantiation: Botan::DSA_PublicKey::DSA_PublicKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
Unexecuted instantiation: Botan::DSA_PublicKey::DSA_PublicKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
60
61
210
DSA_PublicKey::DSA_PublicKey(const DL_Group& group, const BigInt& y) {
62
210
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
63
64
210
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
65
210
}
Unexecuted instantiation: Botan::DSA_PublicKey::DSA_PublicKey(Botan::DL_Group const&, Botan::BigInt const&)
Botan::DSA_PublicKey::DSA_PublicKey(Botan::DL_Group const&, Botan::BigInt const&)
Line
Count
Source
61
210
DSA_PublicKey::DSA_PublicKey(const DL_Group& group, const BigInt& y) {
62
210
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
63
64
210
   BOTAN_ARG_CHECK(m_public_key->group().has_q(), "Q parameter must be set for DSA");
65
210
}
66
67
0
DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
68
0
   BOTAN_ARG_CHECK(group.has_q(), "Q parameter must be set for DSA");
69
70
0
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
71
0
   m_public_key = m_private_key->public_key();
72
0
}
Unexecuted instantiation: Botan::DSA_PrivateKey::DSA_PrivateKey(Botan::RandomNumberGenerator&, Botan::DL_Group const&)
Unexecuted instantiation: Botan::DSA_PrivateKey::DSA_PrivateKey(Botan::RandomNumberGenerator&, Botan::DL_Group const&)
73
74
0
DSA_PrivateKey::DSA_PrivateKey(const DL_Group& group, const BigInt& x) {
75
0
   BOTAN_ARG_CHECK(group.has_q(), "Q parameter must be set for DSA");
76
77
0
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
78
0
   m_public_key = m_private_key->public_key();
79
0
}
Unexecuted instantiation: Botan::DSA_PrivateKey::DSA_PrivateKey(Botan::DL_Group const&, Botan::BigInt const&)
Unexecuted instantiation: Botan::DSA_PrivateKey::DSA_PrivateKey(Botan::DL_Group const&, Botan::BigInt const&)
80
81
0
DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
82
0
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
83
0
   m_public_key = m_private_key->public_key();
84
85
0
   BOTAN_ARG_CHECK(m_private_key->group().has_q(), "Q parameter must be set for DSA");
86
0
}
Unexecuted instantiation: Botan::DSA_PrivateKey::DSA_PrivateKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
Unexecuted instantiation: Botan::DSA_PrivateKey::DSA_PrivateKey(Botan::AlgorithmIdentifier const&, std::__1::span<unsigned char const, 18446744073709551615ul>)
87
88
0
bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
89
0
   if(!m_private_key->check_key(rng, strong)) {
90
0
      return false;
91
0
   }
92
93
0
   if(m_private_key->private_key() >= m_private_key->group().get_q()) {
94
0
      return false;
95
0
   }
96
97
0
   return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
98
0
}
99
100
0
secure_vector<uint8_t> DSA_PrivateKey::private_key_bits() const {
101
0
   return m_private_key->DER_encode();
102
0
}
103
104
0
secure_vector<uint8_t> DSA_PrivateKey::raw_private_key_bits() const {
105
0
   return m_private_key->raw_private_key_bits();
106
0
}
107
108
0
const BigInt& DSA_PrivateKey::get_int_field(std::string_view field) const {
109
0
   return m_private_key->get_int_field(algo_name(), field);
110
0
}
111
112
0
std::unique_ptr<Public_Key> DSA_PrivateKey::public_key() const {
113
   // can't use make_unique here due to private constructor
114
0
   return std::unique_ptr<DSA_PublicKey>(new DSA_PublicKey(m_public_key));
115
0
}
116
117
namespace {
118
119
/**
120
* Object that can create a DSA signature
121
*/
122
class DSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
123
   public:
124
      DSA_Signature_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
125
                              std::string_view emsa,
126
                              RandomNumberGenerator& rng) :
127
0
            PK_Ops::Signature_with_Hash(emsa), m_key(key) {
128
0
         m_b = BigInt::random_integer(rng, 2, m_key->group().get_q());
129
0
         m_b_inv = m_key->group().inverse_mod_q(m_b);
130
0
      }
131
132
0
      size_t signature_length() const override { return 2 * m_key->group().q_bytes(); }
133
134
      secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len, RandomNumberGenerator& rng) override;
135
136
      AlgorithmIdentifier algorithm_identifier() const override;
137
138
   private:
139
      std::shared_ptr<const DL_PrivateKey> m_key;
140
      BigInt m_b, m_b_inv;
141
};
142
143
0
AlgorithmIdentifier DSA_Signature_Operation::algorithm_identifier() const {
144
0
   const std::string full_name = "DSA/" + hash_function();
145
0
   const OID oid = OID::from_string(full_name);
146
0
   return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
147
0
}
148
149
secure_vector<uint8_t> DSA_Signature_Operation::raw_sign(const uint8_t msg[],
150
                                                         size_t msg_len,
151
0
                                                         RandomNumberGenerator& rng) {
152
0
   const DL_Group& group = m_key->group();
153
0
   const BigInt& q = group.get_q();
154
155
0
   BigInt m = BigInt::from_bytes_with_max_bits(msg, msg_len, group.q_bits());
156
157
0
   if(m >= q) {
158
0
      m -= q;
159
0
   }
160
161
0
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
162
0
   BOTAN_UNUSED(rng);
163
0
   const BigInt k = generate_rfc6979_nonce(m_key->private_key(), q, m, this->rfc6979_hash_function());
164
#else
165
   const BigInt k = BigInt::random_integer(rng, 1, q);
166
#endif
167
168
0
   const BigInt k_inv = group.inverse_mod_q(k);
169
170
   /*
171
   * It may not be strictly necessary for the reduction (g^k mod p) mod q to be
172
   * const time, since r is published as part of the signature, and deriving
173
   * anything useful about k from g^k mod p would seem to require computing a
174
   * discrete logarithm.
175
   *
176
   * However it only increases the cost of signatures by about 7-10%, and DSA is
177
   * only for legacy use anyway so we don't care about the performance so much.
178
   */
179
0
   const BigInt r = ct_modulo(group.power_g_p(k, group.q_bits()), group.get_q());
180
181
   /*
182
   * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
183
   */
184
0
   m_b = group.square_mod_q(m_b);
185
0
   m_b_inv = group.square_mod_q(m_b_inv);
186
187
0
   m = group.multiply_mod_q(m_b, m);
188
0
   const BigInt xr = group.multiply_mod_q(m_b, m_key->private_key(), r);
189
190
0
   const BigInt s = group.multiply_mod_q(m_b_inv, k_inv, group.mod_q(xr + m));
191
192
   // With overwhelming probability, a bug rather than actual zero r/s
193
0
   if(r.is_zero() || s.is_zero()) {
194
0
      throw Internal_Error("Computed zero r/s during DSA signature");
195
0
   }
196
197
0
   return BigInt::encode_fixed_length_int_pair(r, s, q.bytes());
198
0
}
199
200
/**
201
* Object that can verify a DSA signature
202
*/
203
class DSA_Verification_Operation final : public PK_Ops::Verification_with_Hash {
204
   public:
205
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, std::string_view emsa) :
206
185
            PK_Ops::Verification_with_Hash(emsa), m_key(key) {}
207
208
      DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, const AlgorithmIdentifier& alg_id) :
209
0
            PK_Ops::Verification_with_Hash(alg_id, "DSA"), m_key(key) {}
210
211
      bool verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) override;
212
213
   private:
214
      std::shared_ptr<const DL_PublicKey> m_key;
215
};
216
217
185
bool DSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[], size_t sig_len) {
218
185
   const auto group = m_key->group();
219
220
185
   const BigInt& q = group.get_q();
221
185
   const size_t q_bytes = q.bytes();
222
223
185
   if(sig_len != 2 * q_bytes) {
224
0
      return false;
225
0
   }
226
227
185
   BigInt r(sig, q_bytes);
228
185
   BigInt s(sig + q_bytes, q_bytes);
229
185
   BigInt i = BigInt::from_bytes_with_max_bits(msg, msg_len, group.q_bits());
230
185
   if(i >= q) {
231
32
      i -= q;
232
32
   }
233
234
185
   if(r <= 0 || r >= q || s <= 0 || s >= q) {
235
118
      return false;
236
118
   }
237
238
67
   s = inverse_mod(s, q);
239
240
67
   const BigInt sr = group.multiply_mod_q(s, r);
241
67
   const BigInt si = group.multiply_mod_q(s, i);
242
243
67
   s = group.multi_exponentiate(si, m_key->public_key(), sr);
244
245
   // s is too big for Barrett, and verification doesn't need to be const-time
246
67
   return (s % group.get_q() == r);
247
185
}
248
249
}  // namespace
250
251
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_verification_op(std::string_view params,
252
185
                                                                            std::string_view provider) const {
253
185
   if(provider == "base" || provider.empty()) {
254
185
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, params);
255
185
   }
256
0
   throw Provider_Not_Found(algo_name(), provider);
257
185
}
258
259
std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_x509_verification_op(
260
0
   const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
261
0
   if(provider == "base" || provider.empty()) {
262
0
      return std::make_unique<DSA_Verification_Operation>(this->m_public_key, signature_algorithm);
263
0
   }
264
265
0
   throw Provider_Not_Found(algo_name(), provider);
266
0
}
267
268
std::unique_ptr<PK_Ops::Signature> DSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
269
                                                                       std::string_view params,
270
0
                                                                       std::string_view provider) const {
271
0
   if(provider == "base" || provider.empty()) {
272
0
      return std::make_unique<DSA_Signature_Operation>(this->m_private_key, params, rng);
273
0
   }
274
0
   throw Provider_Not_Found(algo_name(), provider);
275
0
}
276
277
}  // namespace Botan