Botan 3.0.0-alpha0
Crypto and TLS for C&
dsa.cpp
Go to the documentation of this file.
1/*
2* DSA
3* (C) 1999-2010,2014,2016 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#include <botan/internal/keypair.h>
11#include <botan/reducer.h>
12#include <botan/rng.h>
13#include <botan/internal/divide.h>
14#include <botan/internal/pk_ops_impl.h>
15
16#if defined(BOTAN_HAS_RFC6979_GENERATOR)
17 #include <botan/internal/emsa.h>
18 #include <botan/internal/rfc6979.h>
19#endif
20
21namespace Botan {
22
23/*
24* DSA_PublicKey Constructor
25*/
26
28 const std::vector<uint8_t>& key_bits) :
30 {
31 BOTAN_ARG_CHECK(group_q().bytes() > 0, "Q parameter must be set for DSA");
32 }
33
34
36 {
37 m_group = grp;
38 m_y = y1;
39
40 BOTAN_ARG_CHECK(grp.q_bytes() > 0, "Q parameter must be set for DSA");
41 }
42
43/*
44* Create a DSA private key
45*/
47 const DL_Group& grp,
48 const BigInt& x_arg)
49 {
50 m_group = grp;
51 BOTAN_ARG_CHECK(x_arg.is_positive(), "x must be positive");
52
53 if(x_arg == 0)
55 else
56 {
57 BOTAN_ARG_CHECK(m_x < m_group.get_q(), "x must not be larger than q");
58 m_x = x_arg;
59 }
60
62 }
63
65 const secure_vector<uint8_t>& key_bits) :
67 {
68 BOTAN_ARG_CHECK(m_x > 0, "x must be greater than zero");
69 BOTAN_ARG_CHECK(m_x < m_group.get_q(), "x must not be larger than q");
71 }
72
73/*
74* Check Private DSA Parameters
75*/
77 {
78 if(!DL_Scheme_PrivateKey::check_key(rng, strong) || m_x >= group_q())
79 return false;
80
81 if(!strong)
82 return true;
83
84 return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-256)");
85 }
86
87std::unique_ptr<Public_Key> DSA_PrivateKey::public_key() const
88 {
89 return std::make_unique<DSA_PublicKey>(get_group(), get_y());
90 }
91
92namespace {
93
94/**
95* Object that can create a DSA signature
96*/
97class DSA_Signature_Operation final : public PK_Ops::Signature_with_EMSA
98 {
99 public:
100 DSA_Signature_Operation(const DSA_PrivateKey& dsa,
101 const std::string& emsa,
103 PK_Ops::Signature_with_EMSA(emsa),
104 m_group(dsa.get_group()),
105 m_x(dsa.get_x())
106 {
107 m_b = BigInt::random_integer(rng, 2, dsa.group_q());
108 m_b_inv = m_group.inverse_mod_q(m_b);
109 }
110
111 size_t signature_length() const override { return 2*m_group.q_bytes(); }
112 size_t max_input_bits() const override { return m_group.q_bits(); }
113
114 secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len,
115 RandomNumberGenerator& rng) override;
116 private:
117 const DL_Group m_group;
118 const BigInt& m_x;
119 BigInt m_b, m_b_inv;
120 };
121
122secure_vector<uint8_t>
123DSA_Signature_Operation::raw_sign(const uint8_t msg[], size_t msg_len,
124 RandomNumberGenerator& rng)
125 {
126 const BigInt& q = m_group.get_q();
127
128 BigInt m = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.q_bits());
129
130 while(m >= q)
131 m -= q;
132
133#if defined(BOTAN_HAS_RFC6979_GENERATOR)
134 BOTAN_UNUSED(rng);
135 const BigInt k = generate_rfc6979_nonce(m_x, q, m, this->hash_for_signature());
136#else
137 const BigInt k = BigInt::random_integer(rng, 1, q);
138#endif
139
140 const BigInt k_inv = m_group.inverse_mod_q(k);
141
142 /*
143 * It may not be strictly necessary for the reduction (g^k mod p) mod q to be
144 * const time, since r is published as part of the signature, and deriving
145 * anything useful about k from g^k mod p would seem to require computing a
146 * discrete logarithm.
147 *
148 * However it only increases the cost of signatures by about 7-10%, and DSA is
149 * only for legacy use anyway so we don't care about the performance so much.
150 */
151 const BigInt r = ct_modulo(m_group.power_g_p(k, m_group.q_bits()), m_group.get_q());
152
153 /*
154 * Blind the input message and compute x*r+m as (x*r*b + m*b)/b
155 */
156 m_b = m_group.square_mod_q(m_b);
157 m_b_inv = m_group.square_mod_q(m_b_inv);
158
159 m = m_group.multiply_mod_q(m_b, m);
160 const BigInt xr = m_group.multiply_mod_q(m_b, m_x, r);
161
162 const BigInt s = m_group.multiply_mod_q(m_b_inv, k_inv, m_group.mod_q(xr+m));
163
164 // With overwhelming probability, a bug rather than actual zero r/s
165 if(r.is_zero() || s.is_zero())
166 throw Internal_Error("Computed zero r/s during DSA signature");
167
168 return BigInt::encode_fixed_length_int_pair(r, s, q.bytes());
169 }
170
171/**
172* Object that can verify a DSA signature
173*/
174class DSA_Verification_Operation final : public PK_Ops::Verification_with_EMSA
175 {
176 public:
177 DSA_Verification_Operation(const DSA_PublicKey& dsa,
178 const std::string& emsa) :
179 PK_Ops::Verification_with_EMSA(emsa),
180 m_group(dsa.get_group()),
181 m_y(dsa.get_y())
182 {
183 }
184
185 size_t max_input_bits() const override { return m_group.q_bits(); }
186
187 bool with_recovery() const override { return false; }
188
189 bool verify(const uint8_t msg[], size_t msg_len,
190 const uint8_t sig[], size_t sig_len) override;
191 private:
192 const DL_Group m_group;
193 const BigInt& m_y;
194 };
195
196bool DSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len,
197 const uint8_t sig[], size_t sig_len)
198 {
199 const BigInt& q = m_group.get_q();
200 const size_t q_bytes = q.bytes();
201
202 if(sig_len != 2*q_bytes || msg_len > q_bytes)
203 return false;
204
205 BigInt r(sig, q_bytes);
206 BigInt s(sig + q_bytes, q_bytes);
207 BigInt i = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.q_bits());
208
209 if(r <= 0 || r >= q || s <= 0 || s >= q)
210 return false;
211
212 s = inverse_mod(s, q);
213
214 const BigInt sr = m_group.multiply_mod_q(s, r);
215 const BigInt si = m_group.multiply_mod_q(s, i);
216
217 s = m_group.multi_exponentiate(si, m_y, sr);
218
219 // s is too big for Barrett, and verification doesn't need to be const-time
220 return (s % m_group.get_q() == r);
221 }
222
223}
224
225std::unique_ptr<PK_Ops::Verification>
227 const std::string& provider) const
228 {
229 if(provider == "base" || provider.empty())
230 return std::make_unique<DSA_Verification_Operation>(*this, params);
231 throw Provider_Not_Found(algo_name(), provider);
232 }
233
234std::unique_ptr<PK_Ops::Signature>
236 const std::string& params,
237 const std::string& provider) const
238 {
239 if(provider == "base" || provider.empty())
240 return std::make_unique<DSA_Signature_Operation>(*this, params, rng);
241 throw Provider_Not_Found(algo_name(), provider);
242 }
243
244}
#define BOTAN_UNUSED(...)
Definition: assert.h:141
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:36
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:45
static secure_vector< uint8_t > encode_fixed_length_int_pair(const BigInt &n1, const BigInt &n2, size_t bytes)
Definition: big_code.cpp:128
static BigInt from_bytes_with_max_bits(const uint8_t buf[], size_t length, size_t max_bits)
Definition: bigint.cpp:110
size_t bytes() const
Definition: bigint.cpp:294
bool is_positive() const
Definition: bigint.h:547
BigInt power_g_p(const BigInt &x) const
Definition: dl_group.cpp:571
BigInt multi_exponentiate(const BigInt &x, const BigInt &y, const BigInt &z) const
Definition: dl_group.cpp:566
BigInt square_mod_q(const BigInt &x) const
Definition: dl_group.cpp:560
BigInt inverse_mod_q(const BigInt &x) const
Definition: dl_group.cpp:535
size_t q_bytes() const
Definition: dl_group.cpp:503
BigInt multiply_mod_q(const BigInt &x, const BigInt &y) const
Definition: dl_group.cpp:548
size_t q_bits() const
Definition: dl_group.cpp:497
BigInt mod_q(const BigInt &x) const
Definition: dl_group.cpp:542
const BigInt & get_q() const
Definition: dl_group.cpp:477
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: dl_algo.cpp:78
const DL_Group & get_group() const
Definition: dl_algo.h:38
const BigInt & group_q() const
Definition: dl_algo.h:55
const BigInt & get_y() const
Definition: dl_algo.h:43
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition: dsa.cpp:76
DSA_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: dsa.cpp:64
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: dsa.cpp:235
std::unique_ptr< Public_Key > public_key() const override
Definition: dsa.cpp:87
std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const override
Definition: dsa.cpp:226
std::string algo_name() const override
Definition: dsa.h:21
int(* final)(unsigned char *, CTX *)
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:47
Definition: alg_id.cpp:13
DL_Group_Format
Definition: dl_group.h:27
BigInt generate_rfc6979_nonce(const BigInt &x, const BigInt &q, const BigInt &h, const std::string &hash)
Definition: rfc6979.cpp:49
BigInt ct_modulo(const BigInt &x, const BigInt &y)
Definition: divide.cpp:124
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: mod_inv.cpp:177
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65