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