]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_ossl.c
Fix Typos
[thirdparty/openssl.git] / crypto / dsa / dsa_ossl.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "internal/bn_int.h"
13 #include <openssl/bn.h>
14 #include <openssl/sha.h>
15 #include "dsa_locl.h"
16 #include <openssl/asn1.h>
17
18 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
19 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
20 BIGNUM **rp);
21 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
22 BIGNUM **rp, const unsigned char *dgst, int dlen);
23 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
24 DSA_SIG *sig, DSA *dsa);
25 static int dsa_init(DSA *dsa);
26 static int dsa_finish(DSA *dsa);
27 static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
28 BN_CTX *ctx);
29
30 static DSA_METHOD openssl_dsa_meth = {
31 "OpenSSL DSA method",
32 dsa_do_sign,
33 dsa_sign_setup_no_digest,
34 dsa_do_verify,
35 NULL, /* dsa_mod_exp, */
36 NULL, /* dsa_bn_mod_exp, */
37 dsa_init,
38 dsa_finish,
39 DSA_FLAG_FIPS_METHOD,
40 NULL,
41 NULL,
42 NULL
43 };
44
45 static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth;
46
47 void DSA_set_default_method(const DSA_METHOD *meth)
48 {
49 default_DSA_method = meth;
50 }
51
52 const DSA_METHOD *DSA_get_default_method(void)
53 {
54 return default_DSA_method;
55 }
56
57 const DSA_METHOD *DSA_OpenSSL(void)
58 {
59 return &openssl_dsa_meth;
60 }
61
62 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
63 {
64 BIGNUM *kinv = NULL;
65 BIGNUM *m, *blind, *blindm, *tmp;
66 BN_CTX *ctx = NULL;
67 int reason = ERR_R_BN_LIB;
68 DSA_SIG *ret = NULL;
69 int rv = 0;
70
71 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
72 reason = DSA_R_MISSING_PARAMETERS;
73 goto err;
74 }
75
76 ret = DSA_SIG_new();
77 if (ret == NULL)
78 goto err;
79 ret->r = BN_new();
80 ret->s = BN_new();
81 if (ret->r == NULL || ret->s == NULL)
82 goto err;
83
84 ctx = BN_CTX_new();
85 if (ctx == NULL)
86 goto err;
87 m = BN_CTX_get(ctx);
88 blind = BN_CTX_get(ctx);
89 blindm = BN_CTX_get(ctx);
90 tmp = BN_CTX_get(ctx);
91 if (tmp == NULL)
92 goto err;
93
94 redo:
95 if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))
96 goto err;
97
98 if (dlen > BN_num_bytes(dsa->q))
99 /*
100 * if the digest length is greater than the size of q use the
101 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
102 * 4.2
103 */
104 dlen = BN_num_bytes(dsa->q);
105 if (BN_bin2bn(dgst, dlen, m) == NULL)
106 goto err;
107
108 /*
109 * The normal signature calculation is:
110 *
111 * s := k^-1 * (m + r * priv_key) mod q
112 *
113 * We will blind this to protect against side channel attacks
114 *
115 * s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
116 */
117
118 /* Generate a blinding value */
119 do {
120 if (!BN_priv_rand(blind, BN_num_bits(dsa->q) - 1,
121 BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
122 goto err;
123 } while (BN_is_zero(blind));
124 BN_set_flags(blind, BN_FLG_CONSTTIME);
125 BN_set_flags(blindm, BN_FLG_CONSTTIME);
126 BN_set_flags(tmp, BN_FLG_CONSTTIME);
127
128 /* tmp := blind * priv_key * r mod q */
129 if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->q, ctx))
130 goto err;
131 if (!BN_mod_mul(tmp, tmp, ret->r, dsa->q, ctx))
132 goto err;
133
134 /* blindm := blind * m mod q */
135 if (!BN_mod_mul(blindm, blind, m, dsa->q, ctx))
136 goto err;
137
138 /* s : = (blind * priv_key * r) + (blind * m) mod q */
139 if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->q))
140 goto err;
141
142 /* s := s * k^-1 mod q */
143 if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))
144 goto err;
145
146 /* s:= s * blind^-1 mod q */
147 if (BN_mod_inverse(blind, blind, dsa->q, ctx) == NULL)
148 goto err;
149 if (!BN_mod_mul(ret->s, ret->s, blind, dsa->q, ctx))
150 goto err;
151
152 /*
153 * Redo if r or s is zero as required by FIPS 186-3: this is very
154 * unlikely.
155 */
156 if (BN_is_zero(ret->r) || BN_is_zero(ret->s))
157 goto redo;
158
159 rv = 1;
160
161 err:
162 if (rv == 0) {
163 DSAerr(DSA_F_DSA_DO_SIGN, reason);
164 DSA_SIG_free(ret);
165 ret = NULL;
166 }
167 BN_CTX_free(ctx);
168 BN_clear_free(kinv);
169 return ret;
170 }
171
172 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
173 BIGNUM **kinvp, BIGNUM **rp)
174 {
175 return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
176 }
177
178 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
179 BIGNUM **kinvp, BIGNUM **rp,
180 const unsigned char *dgst, int dlen)
181 {
182 BN_CTX *ctx = NULL;
183 BIGNUM *k, *kinv = NULL, *r = *rp;
184 BIGNUM *l;
185 int ret = 0;
186 int q_bits, q_words;
187
188 if (!dsa->p || !dsa->q || !dsa->g) {
189 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
190 return 0;
191 }
192
193 /* Reject obviously invalid parameters */
194 if (BN_is_zero(dsa->p) || BN_is_zero(dsa->q) || BN_is_zero(dsa->g)) {
195 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_INVALID_PARAMETERS);
196 return 0;
197 }
198
199 k = BN_new();
200 l = BN_new();
201 if (k == NULL || l == NULL)
202 goto err;
203
204 if (ctx_in == NULL) {
205 if ((ctx = BN_CTX_new()) == NULL)
206 goto err;
207 } else
208 ctx = ctx_in;
209
210 /* Preallocate space */
211 q_bits = BN_num_bits(dsa->q);
212 q_words = bn_get_top(dsa->q);
213 if (!bn_wexpand(k, q_words + 2)
214 || !bn_wexpand(l, q_words + 2))
215 goto err;
216
217 /* Get random k */
218 do {
219 if (dgst != NULL) {
220 /*
221 * We calculate k from SHA512(private_key + H(message) + random).
222 * This protects the private key from a weak PRNG.
223 */
224 if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
225 dlen, ctx))
226 goto err;
227 } else if (!BN_priv_rand_range(k, dsa->q))
228 goto err;
229 } while (BN_is_zero(k));
230
231 BN_set_flags(k, BN_FLG_CONSTTIME);
232 BN_set_flags(l, BN_FLG_CONSTTIME);
233
234 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
235 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
236 dsa->lock, dsa->p, ctx))
237 goto err;
238 }
239
240 /* Compute r = (g^k mod p) mod q */
241
242 /*
243 * We do not want timing information to leak the length of k, so we
244 * compute G^k using an equivalent scalar of fixed bit-length.
245 *
246 * We unconditionally perform both of these additions to prevent a
247 * small timing information leakage. We then choose the sum that is
248 * one bit longer than the modulus.
249 *
250 * There are some concerns about the efficacy of doing this. More
251 * specifically refer to the discussion starting with:
252 * https://github.com/openssl/openssl/pull/7486#discussion_r228323705
253 * The fix is to rework BN so these gymnastics aren't required.
254 */
255 if (!BN_add(l, k, dsa->q)
256 || !BN_add(k, l, dsa->q))
257 goto err;
258
259 BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);
260
261 if ((dsa)->meth->bn_mod_exp != NULL) {
262 if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,
263 dsa->method_mont_p))
264 goto err;
265 } else {
266 if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))
267 goto err;
268 }
269
270 if (!BN_mod(r, r, dsa->q, ctx))
271 goto err;
272
273 /* Compute part of 's = inv(k) (m + xr) mod q' */
274 if ((kinv = dsa_mod_inverse_fermat(k, dsa->q, ctx)) == NULL)
275 goto err;
276
277 BN_clear_free(*kinvp);
278 *kinvp = kinv;
279 kinv = NULL;
280 ret = 1;
281 err:
282 if (!ret)
283 DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
284 if (ctx != ctx_in)
285 BN_CTX_free(ctx);
286 BN_clear_free(k);
287 BN_clear_free(l);
288 return ret;
289 }
290
291 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
292 DSA_SIG *sig, DSA *dsa)
293 {
294 BN_CTX *ctx;
295 BIGNUM *u1, *u2, *t1;
296 BN_MONT_CTX *mont = NULL;
297 const BIGNUM *r, *s;
298 int ret = -1, i;
299 if (!dsa->p || !dsa->q || !dsa->g) {
300 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
301 return -1;
302 }
303
304 i = BN_num_bits(dsa->q);
305 /* fips 186-3 allows only different sizes for q */
306 if (i != 160 && i != 224 && i != 256) {
307 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
308 return -1;
309 }
310
311 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
312 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
313 return -1;
314 }
315 u1 = BN_new();
316 u2 = BN_new();
317 t1 = BN_new();
318 ctx = BN_CTX_new();
319 if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
320 goto err;
321
322 DSA_SIG_get0(sig, &r, &s);
323
324 if (BN_is_zero(r) || BN_is_negative(r) ||
325 BN_ucmp(r, dsa->q) >= 0) {
326 ret = 0;
327 goto err;
328 }
329 if (BN_is_zero(s) || BN_is_negative(s) ||
330 BN_ucmp(s, dsa->q) >= 0) {
331 ret = 0;
332 goto err;
333 }
334
335 /*
336 * Calculate W = inv(S) mod Q save W in u2
337 */
338 if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
339 goto err;
340
341 /* save M in u1 */
342 if (dgst_len > (i >> 3))
343 /*
344 * if the digest length is greater than the size of q use the
345 * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
346 * 4.2
347 */
348 dgst_len = (i >> 3);
349 if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
350 goto err;
351
352 /* u1 = M * w mod q */
353 if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
354 goto err;
355
356 /* u2 = r * w mod q */
357 if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
358 goto err;
359
360 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
361 mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
362 dsa->lock, dsa->p, ctx);
363 if (!mont)
364 goto err;
365 }
366
367 if (dsa->meth->dsa_mod_exp != NULL) {
368 if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
369 dsa->p, ctx, mont))
370 goto err;
371 } else {
372 if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
373 mont))
374 goto err;
375 }
376
377 /* let u1 = u1 mod q */
378 if (!BN_mod(u1, t1, dsa->q, ctx))
379 goto err;
380
381 /*
382 * V is now in u1. If the signature is correct, it will be equal to R.
383 */
384 ret = (BN_ucmp(u1, r) == 0);
385
386 err:
387 if (ret < 0)
388 DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
389 BN_CTX_free(ctx);
390 BN_free(u1);
391 BN_free(u2);
392 BN_free(t1);
393 return ret;
394 }
395
396 static int dsa_init(DSA *dsa)
397 {
398 dsa->flags |= DSA_FLAG_CACHE_MONT_P;
399 return 1;
400 }
401
402 static int dsa_finish(DSA *dsa)
403 {
404 BN_MONT_CTX_free(dsa->method_mont_p);
405 return 1;
406 }
407
408 /*
409 * Compute the inverse of k modulo q.
410 * Since q is prime, Fermat's Little Theorem applies, which reduces this to
411 * mod-exp operation. Both the exponent and modulus are public information
412 * so a mod-exp that doesn't leak the base is sufficient. A newly allocated
413 * BIGNUM is returned which the caller must free.
414 */
415 static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
416 BN_CTX *ctx)
417 {
418 BIGNUM *res = NULL;
419 BIGNUM *r, *e;
420
421 if ((r = BN_new()) == NULL)
422 return NULL;
423
424 BN_CTX_start(ctx);
425 if ((e = BN_CTX_get(ctx)) != NULL
426 && BN_set_word(r, 2)
427 && BN_sub(e, q, r)
428 && BN_mod_exp_mont(r, k, e, q, ctx, NULL))
429 res = r;
430 else
431 BN_free(r);
432 BN_CTX_end(ctx);
433 return res;
434 }