Botan 3.13.0
Crypto and TLS for C&
dsa.cpp
Go to the documentation of this file.
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
22namespace Botan {
23
24namespace {
25
26void check_dsa_group(const DL_Group& group) {
27 BOTAN_ARG_CHECK(group.has_q(), "Q parameter must be set for DSA");
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");
30}
31
32} // namespace
33
35 return m_public_key->group().q_bytes();
36}
37
39 return m_public_key->estimated_strength();
40}
41
43 return m_public_key->p_bits();
44}
45
46const BigInt& DSA_PublicKey::get_int_field(std::string_view field) const {
47 return m_public_key->get_int_field(algo_name(), field);
48}
49
53
54std::vector<uint8_t> DSA_PublicKey::raw_public_key_bits() const {
55 return m_public_key->public_key_as_bytes();
56}
57
58std::vector<uint8_t> DSA_PublicKey::public_key_bits() const {
59 return m_public_key->DER_encode();
60}
61
62bool DSA_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
63 return m_public_key->check_key(rng, strong);
64}
65
66std::unique_ptr<Private_Key> DSA_PublicKey::generate_another(RandomNumberGenerator& rng) const {
67 return std::make_unique<DSA_PrivateKey>(rng, m_public_key->group());
68}
69
70DSA_PublicKey::DSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
71 m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
72
73 check_dsa_group(m_public_key->group());
74}
75
77 m_public_key = std::make_shared<DL_PublicKey>(group, y);
78
79 check_dsa_group(m_public_key->group());
80}
81
83 check_dsa_group(group);
84
85 m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
86 m_public_key = m_private_key->public_key();
87}
88
90 check_dsa_group(group);
91
92 m_private_key = std::make_shared<DL_PrivateKey>(group, x);
93 m_public_key = m_private_key->public_key();
94}
95
96DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
97 m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_57);
98 m_public_key = m_private_key->public_key();
99
100 check_dsa_group(m_private_key->group());
101}
102
104 if(!m_private_key->check_key(rng, strong)) {
105 return false;
106 }
107
108 if(m_private_key->private_key() >= m_private_key->group().get_q()) {
109 return false;
110 }
111
112 return KeyPair::signature_consistency_check(rng, *this, "SHA-256");
113}
114
116 return m_private_key->DER_encode();
117}
118
120 return m_private_key->raw_private_key_bits();
121}
122
123const BigInt& DSA_PrivateKey::get_int_field(std::string_view field) const {
124 return m_private_key->get_int_field(algo_name(), field);
125}
126
127std::unique_ptr<Public_Key> DSA_PrivateKey::public_key() const {
128 // can't use make_unique here due to private constructor
129 return std::unique_ptr<DSA_PublicKey>(new DSA_PublicKey(m_public_key));
130}
131
132namespace {
133
134/**
135* Object that can create a DSA signature
136*/
137class DSA_Signature_Operation final : public PK_Ops::Signature_with_Hash {
138 public:
139 DSA_Signature_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
140 std::string_view hash_fn,
142 PK_Ops::Signature_with_Hash(hash_fn), m_key(key) {
143 m_b = BigInt::random_integer(rng, BigInt::from_s32(2), m_key->group().get_q());
144 m_b_inv = m_key->group().inverse_mod_q(m_b);
145 }
146
147 size_t signature_length() const override { return 2 * m_key->group().q_bytes(); }
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
158AlgorithmIdentifier DSA_Signature_Operation::algorithm_identifier() const {
159 const std::string full_name = "DSA/" + hash_function();
160 const OID oid = OID::from_string(full_name);
161 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
162}
163
164std::vector<uint8_t> DSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
165 const DL_Group& group = m_key->group();
166 const BigInt& q = group.get_q();
167
168 BigInt m = BigInt::from_bytes_with_max_bits(msg.data(), msg.size(), group.q_bits());
169
170 if(m >= q) {
171 m -= q;
172 }
173
174#if defined(BOTAN_HAS_RFC6979_GENERATOR)
175 BOTAN_UNUSED(rng);
176 const BigInt k = generate_rfc6979_nonce(m_key->private_key(), q, m, this->rfc6979_hash_function());
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);
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());
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);
198 m_b_inv = group.square_mod_q(m_b_inv);
199
200 m = group.multiply_mod_q(m_b, m);
201 const BigInt xr = group.multiply_mod_q(m_b, m_key->private_key(), r);
202
203 const BigInt s = group.multiply_mod_q(m_b_inv, k_inv, group.mod_q(xr + m));
204
205 // With overwhelming probability, a bug rather than actual zero r/s
206 if(r.is_zero() || s.is_zero()) {
207 throw Internal_Error("Computed zero r/s during DSA signature");
208 }
209
210 const size_t q_bytes = q.bytes();
211 std::vector<uint8_t> sig(2 * q_bytes);
212 BufferStuffer stuffer(sig);
213 r.serialize_to(stuffer.next(q_bytes));
214 s.serialize_to(stuffer.next(q_bytes));
215 return sig;
216}
217
218/**
219* Object that can verify a DSA signature
220*/
221class 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) :
224 PK_Ops::Verification_with_Hash(hash_fn), m_key(key) {}
225
226 DSA_Verification_Operation(const std::shared_ptr<const DL_PublicKey>& key, const AlgorithmIdentifier& alg_id) :
227 PK_Ops::Verification_with_Hash(alg_id, "DSA"), m_key(key) {}
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
235bool DSA_Verification_Operation::verify(std::span<const uint8_t> input, std::span<const uint8_t> sig) {
236 const auto group = m_key->group();
237
238 const BigInt& q = group.get_q();
239 const size_t q_bytes = q.bytes();
240
241 if(sig.size() != 2 * q_bytes) {
242 return false;
243 }
244
245 const BigInt r(sig.first(q_bytes));
246 BigInt s(sig.last(q_bytes));
247
248 if(r == 0 || r >= q || s == 0 || s >= q) {
249 return false;
250 }
251
252 BigInt i = BigInt::from_bytes_with_max_bits(input.data(), input.size(), group.q_bits());
253 if(i >= q) {
254 i -= q;
255 }
256
257 s = group.inverse_mod_q(s);
258
259 // Since we already checked for s == 0 above this shouldn't happen
260 if(s.is_zero()) {
261 return false;
262 }
263
264 const BigInt sr = group.multiply_mod_q(s, r);
265 const BigInt si = group.multiply_mod_q(s, i);
266
267 s = group.multi_exponentiate(si, m_key->public_key(), sr);
268
269 // s is too big for Barrett, and verification doesn't need to be const-time
270 return (s % group.get_q() == r);
271}
272
273} // namespace
274
275std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_verification_op(std::string_view params,
276 std::string_view provider) const {
277 if(provider == "base" || provider.empty()) {
278 return std::make_unique<DSA_Verification_Operation>(this->m_public_key, params);
279 }
280 throw Provider_Not_Found(algo_name(), provider);
281}
282
283std::unique_ptr<PK_Ops::Verification> DSA_PublicKey::create_x509_verification_op(
284 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
285 if(provider == "base" || provider.empty()) {
286 return std::make_unique<DSA_Verification_Operation>(this->m_public_key, signature_algorithm);
287 }
288
289 throw Provider_Not_Found(algo_name(), provider);
290}
291
292std::unique_ptr<PK_Ops::Signature> DSA_PrivateKey::create_signature_op(RandomNumberGenerator& rng,
293 std::string_view params,
294 std::string_view provider) const {
295 if(provider == "base" || provider.empty()) {
296 return std::make_unique<DSA_Signature_Operation>(this->m_private_key, params, rng);
297 }
298 throw Provider_Not_Found(algo_name(), provider);
299}
300
301} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
virtual OID object_identifier() const
Definition pk_keys.cpp:22
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:44
static BigInt from_s32(int32_t n)
Definition bigint.cpp:42
size_t bytes() const
Definition bigint.cpp:294
secure_vector< uint8_t > private_key_bits() const override
Definition dsa.cpp:115
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition dsa.cpp:103
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition dsa.cpp:292
secure_vector< uint8_t > raw_private_key_bits() const override
Definition dsa.cpp:119
std::unique_ptr< Public_Key > public_key() const override
Definition dsa.cpp:127
const BigInt & get_int_field(std::string_view field) const override
Definition dsa.cpp:123
std::optional< size_t > _signature_element_size_for_DER_encoding() const override
Definition dsa.cpp:34
std::vector< uint8_t > raw_public_key_bits() const override
Definition dsa.cpp:54
DSA_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition dsa.cpp:70
size_t estimated_strength() const override
Definition dsa.cpp:38
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
Definition dsa.cpp:283
friend class DSA_PrivateKey
Definition dsa.h:69
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition dsa.cpp:62
std::vector< uint8_t > public_key_bits() const override
Definition dsa.cpp:58
AlgorithmIdentifier algorithm_identifier() const override
Definition dsa.cpp:50
const BigInt & get_int_field(std::string_view field) const override
Definition dsa.cpp:46
std::string algo_name() const override
Definition dsa.h:44
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition dsa.cpp:66
size_t key_length() const override
Definition dsa.cpp:42
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
Definition dsa.cpp:275
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49
BigInt generate_rfc6979_nonce(const BigInt &x, const BigInt &q, const BigInt &h, std::string_view hash)
Definition rfc6979.h:56
BigInt ct_modulo(const BigInt &x, const BigInt &y)
Definition divide.cpp:198
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128