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