/src/openssl/crypto/dsa/dsa_ossl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 <openssl/bn.h> |
13 | | #include <openssl/sha.h> |
14 | | #include "dsa_locl.h" |
15 | | #include <openssl/asn1.h> |
16 | | |
17 | | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); |
18 | | static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, |
19 | | BIGNUM **rp); |
20 | | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, |
21 | | BIGNUM **rp, const unsigned char *dgst, int dlen); |
22 | | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, |
23 | | DSA_SIG *sig, DSA *dsa); |
24 | | static int dsa_init(DSA *dsa); |
25 | | static int dsa_finish(DSA *dsa); |
26 | | |
27 | | static DSA_METHOD openssl_dsa_meth = { |
28 | | "OpenSSL DSA method", |
29 | | dsa_do_sign, |
30 | | dsa_sign_setup_no_digest, |
31 | | dsa_do_verify, |
32 | | NULL, /* dsa_mod_exp, */ |
33 | | NULL, /* dsa_bn_mod_exp, */ |
34 | | dsa_init, |
35 | | dsa_finish, |
36 | | DSA_FLAG_FIPS_METHOD, |
37 | | NULL, |
38 | | NULL, |
39 | | NULL |
40 | | }; |
41 | | |
42 | | static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth; |
43 | | |
44 | | void DSA_set_default_method(const DSA_METHOD *meth) |
45 | 0 | { |
46 | 0 | default_DSA_method = meth; |
47 | 0 | } |
48 | | |
49 | | const DSA_METHOD *DSA_get_default_method(void) |
50 | 0 | { |
51 | 0 | return default_DSA_method; |
52 | 0 | } |
53 | | |
54 | | const DSA_METHOD *DSA_OpenSSL(void) |
55 | 0 | { |
56 | 0 | return &openssl_dsa_meth; |
57 | 0 | } |
58 | | |
59 | | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) |
60 | 0 | { |
61 | 0 | BIGNUM *kinv = NULL; |
62 | 0 | BIGNUM *m, *blind, *blindm, *tmp; |
63 | 0 | BN_CTX *ctx = NULL; |
64 | 0 | int reason = ERR_R_BN_LIB; |
65 | 0 | DSA_SIG *ret = NULL; |
66 | 0 | int rv = 0; |
67 | 0 |
|
68 | 0 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
69 | 0 | reason = DSA_R_MISSING_PARAMETERS; |
70 | 0 | goto err; |
71 | 0 | } |
72 | 0 |
|
73 | 0 | ret = DSA_SIG_new(); |
74 | 0 | if (ret == NULL) |
75 | 0 | goto err; |
76 | 0 | ret->r = BN_new(); |
77 | 0 | ret->s = BN_new(); |
78 | 0 | if (ret->r == NULL || ret->s == NULL) |
79 | 0 | goto err; |
80 | 0 | |
81 | 0 | ctx = BN_CTX_new(); |
82 | 0 | if (ctx == NULL) |
83 | 0 | goto err; |
84 | 0 | m = BN_CTX_get(ctx); |
85 | 0 | blind = BN_CTX_get(ctx); |
86 | 0 | blindm = BN_CTX_get(ctx); |
87 | 0 | tmp = BN_CTX_get(ctx); |
88 | 0 | if (tmp == NULL) |
89 | 0 | goto err; |
90 | 0 | |
91 | 0 | redo: |
92 | 0 | if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen)) |
93 | 0 | goto err; |
94 | 0 | |
95 | 0 | if (dlen > BN_num_bytes(dsa->q)) |
96 | 0 | /* |
97 | 0 | * if the digest length is greater than the size of q use the |
98 | 0 | * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3, |
99 | 0 | * 4.2 |
100 | 0 | */ |
101 | 0 | dlen = BN_num_bytes(dsa->q); |
102 | 0 | if (BN_bin2bn(dgst, dlen, m) == NULL) |
103 | 0 | goto err; |
104 | 0 | |
105 | 0 | /* |
106 | 0 | * The normal signature calculation is: |
107 | 0 | * |
108 | 0 | * s := k^-1 * (m + r * priv_key) mod q |
109 | 0 | * |
110 | 0 | * We will blind this to protect against side channel attacks |
111 | 0 | * |
112 | 0 | * s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q |
113 | 0 | */ |
114 | 0 | |
115 | 0 | /* Generate a blinding value */ |
116 | 0 | do { |
117 | 0 | if (!BN_priv_rand(blind, BN_num_bits(dsa->q) - 1, |
118 | 0 | BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) |
119 | 0 | goto err; |
120 | 0 | } while (BN_is_zero(blind)); |
121 | 0 | BN_set_flags(blind, BN_FLG_CONSTTIME); |
122 | 0 | BN_set_flags(blindm, BN_FLG_CONSTTIME); |
123 | 0 | BN_set_flags(tmp, BN_FLG_CONSTTIME); |
124 | 0 |
|
125 | 0 | /* tmp := blind * priv_key * r mod q */ |
126 | 0 | if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->q, ctx)) |
127 | 0 | goto err; |
128 | 0 | if (!BN_mod_mul(tmp, tmp, ret->r, dsa->q, ctx)) |
129 | 0 | goto err; |
130 | 0 | |
131 | 0 | /* blindm := blind * m mod q */ |
132 | 0 | if (!BN_mod_mul(blindm, blind, m, dsa->q, ctx)) |
133 | 0 | goto err; |
134 | 0 | |
135 | 0 | /* s : = (blind * priv_key * r) + (blind * m) mod q */ |
136 | 0 | if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->q)) |
137 | 0 | goto err; |
138 | 0 | |
139 | 0 | /* s := s * k^-1 mod q */ |
140 | 0 | if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx)) |
141 | 0 | goto err; |
142 | 0 | |
143 | 0 | /* s:= s * blind^-1 mod q */ |
144 | 0 | if (BN_mod_inverse(blind, blind, dsa->q, ctx) == NULL) |
145 | 0 | goto err; |
146 | 0 | if (!BN_mod_mul(ret->s, ret->s, blind, dsa->q, ctx)) |
147 | 0 | goto err; |
148 | 0 | |
149 | 0 | /* |
150 | 0 | * Redo if r or s is zero as required by FIPS 186-3: this is very |
151 | 0 | * unlikely. |
152 | 0 | */ |
153 | 0 | if (BN_is_zero(ret->r) || BN_is_zero(ret->s)) |
154 | 0 | goto redo; |
155 | 0 | |
156 | 0 | rv = 1; |
157 | 0 |
|
158 | 0 | err: |
159 | 0 | if (rv == 0) { |
160 | 0 | DSAerr(DSA_F_DSA_DO_SIGN, reason); |
161 | 0 | DSA_SIG_free(ret); |
162 | 0 | ret = NULL; |
163 | 0 | } |
164 | 0 | BN_CTX_free(ctx); |
165 | 0 | BN_clear_free(kinv); |
166 | 0 | return ret; |
167 | 0 | } |
168 | | |
169 | | static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, |
170 | | BIGNUM **kinvp, BIGNUM **rp) |
171 | 0 | { |
172 | 0 | return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0); |
173 | 0 | } |
174 | | |
175 | | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, |
176 | | BIGNUM **kinvp, BIGNUM **rp, |
177 | | const unsigned char *dgst, int dlen) |
178 | 0 | { |
179 | 0 | BN_CTX *ctx = NULL; |
180 | 0 | BIGNUM *k, *kinv = NULL, *r = *rp; |
181 | 0 | BIGNUM *l, *m; |
182 | 0 | int ret = 0; |
183 | 0 | int q_bits; |
184 | 0 |
|
185 | 0 | if (!dsa->p || !dsa->q || !dsa->g) { |
186 | 0 | DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS); |
187 | 0 | return 0; |
188 | 0 | } |
189 | 0 |
|
190 | 0 | k = BN_new(); |
191 | 0 | l = BN_new(); |
192 | 0 | m = BN_new(); |
193 | 0 | if (k == NULL || l == NULL || m == NULL) |
194 | 0 | goto err; |
195 | 0 | |
196 | 0 | if (ctx_in == NULL) { |
197 | 0 | if ((ctx = BN_CTX_new()) == NULL) |
198 | 0 | goto err; |
199 | 0 | } else |
200 | 0 | ctx = ctx_in; |
201 | 0 |
|
202 | 0 | /* Preallocate space */ |
203 | 0 | q_bits = BN_num_bits(dsa->q); |
204 | 0 | if (!BN_set_bit(k, q_bits) |
205 | 0 | || !BN_set_bit(l, q_bits) |
206 | 0 | || !BN_set_bit(m, q_bits)) |
207 | 0 | goto err; |
208 | 0 | |
209 | 0 | /* Get random k */ |
210 | 0 | do { |
211 | 0 | if (dgst != NULL) { |
212 | 0 | /* |
213 | 0 | * We calculate k from SHA512(private_key + H(message) + random). |
214 | 0 | * This protects the private key from a weak PRNG. |
215 | 0 | */ |
216 | 0 | if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst, |
217 | 0 | dlen, ctx)) |
218 | 0 | goto err; |
219 | 0 | } else if (!BN_priv_rand_range(k, dsa->q)) |
220 | 0 | goto err; |
221 | 0 | } while (BN_is_zero(k)); |
222 | 0 |
|
223 | 0 | BN_set_flags(k, BN_FLG_CONSTTIME); |
224 | 0 |
|
225 | 0 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { |
226 | 0 | if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, |
227 | 0 | dsa->lock, dsa->p, ctx)) |
228 | 0 | goto err; |
229 | 0 | } |
230 | 0 | |
231 | 0 | /* Compute r = (g^k mod p) mod q */ |
232 | 0 | |
233 | 0 | /* |
234 | 0 | * We do not want timing information to leak the length of k, so we |
235 | 0 | * compute G^k using an equivalent scalar of fixed bit-length. |
236 | 0 | * |
237 | 0 | * We unconditionally perform both of these additions to prevent a |
238 | 0 | * small timing information leakage. We then choose the sum that is |
239 | 0 | * one bit longer than the modulus. |
240 | 0 | * |
241 | 0 | * TODO: revisit the BN_copy aiming for a memory access agnostic |
242 | 0 | * conditional copy. |
243 | 0 | */ |
244 | 0 | if (!BN_add(l, k, dsa->q) |
245 | 0 | || !BN_add(m, l, dsa->q) |
246 | 0 | || !BN_copy(k, BN_num_bits(l) > q_bits ? l : m)) |
247 | 0 | goto err; |
248 | 0 | |
249 | 0 | if ((dsa)->meth->bn_mod_exp != NULL) { |
250 | 0 | if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx, |
251 | 0 | dsa->method_mont_p)) |
252 | 0 | goto err; |
253 | 0 | } else { |
254 | 0 | if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p)) |
255 | 0 | goto err; |
256 | 0 | } |
257 | 0 | |
258 | 0 | if (!BN_mod(r, r, dsa->q, ctx)) |
259 | 0 | goto err; |
260 | 0 | |
261 | 0 | /* Compute part of 's = inv(k) (m + xr) mod q' */ |
262 | 0 | if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL) |
263 | 0 | goto err; |
264 | 0 | |
265 | 0 | BN_clear_free(*kinvp); |
266 | 0 | *kinvp = kinv; |
267 | 0 | kinv = NULL; |
268 | 0 | ret = 1; |
269 | 0 | err: |
270 | 0 | if (!ret) |
271 | 0 | DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB); |
272 | 0 | if (ctx != ctx_in) |
273 | 0 | BN_CTX_free(ctx); |
274 | 0 | BN_clear_free(k); |
275 | 0 | BN_clear_free(l); |
276 | 0 | BN_clear_free(m); |
277 | 0 | return ret; |
278 | 0 | } |
279 | | |
280 | | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, |
281 | | DSA_SIG *sig, DSA *dsa) |
282 | 0 | { |
283 | 0 | BN_CTX *ctx; |
284 | 0 | BIGNUM *u1, *u2, *t1; |
285 | 0 | BN_MONT_CTX *mont = NULL; |
286 | 0 | const BIGNUM *r, *s; |
287 | 0 | int ret = -1, i; |
288 | 0 | if (!dsa->p || !dsa->q || !dsa->g) { |
289 | 0 | DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS); |
290 | 0 | return -1; |
291 | 0 | } |
292 | 0 |
|
293 | 0 | i = BN_num_bits(dsa->q); |
294 | 0 | /* fips 186-3 allows only different sizes for q */ |
295 | 0 | if (i != 160 && i != 224 && i != 256) { |
296 | 0 | DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE); |
297 | 0 | return -1; |
298 | 0 | } |
299 | 0 |
|
300 | 0 | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) { |
301 | 0 | DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE); |
302 | 0 | return -1; |
303 | 0 | } |
304 | 0 | u1 = BN_new(); |
305 | 0 | u2 = BN_new(); |
306 | 0 | t1 = BN_new(); |
307 | 0 | ctx = BN_CTX_new(); |
308 | 0 | if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL) |
309 | 0 | goto err; |
310 | 0 | |
311 | 0 | DSA_SIG_get0(sig, &r, &s); |
312 | 0 |
|
313 | 0 | if (BN_is_zero(r) || BN_is_negative(r) || |
314 | 0 | BN_ucmp(r, dsa->q) >= 0) { |
315 | 0 | ret = 0; |
316 | 0 | goto err; |
317 | 0 | } |
318 | 0 | if (BN_is_zero(s) || BN_is_negative(s) || |
319 | 0 | BN_ucmp(s, dsa->q) >= 0) { |
320 | 0 | ret = 0; |
321 | 0 | goto err; |
322 | 0 | } |
323 | 0 | |
324 | 0 | /* |
325 | 0 | * Calculate W = inv(S) mod Q save W in u2 |
326 | 0 | */ |
327 | 0 | if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL) |
328 | 0 | goto err; |
329 | 0 | |
330 | 0 | /* save M in u1 */ |
331 | 0 | if (dgst_len > (i >> 3)) |
332 | 0 | /* |
333 | 0 | * if the digest length is greater than the size of q use the |
334 | 0 | * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3, |
335 | 0 | * 4.2 |
336 | 0 | */ |
337 | 0 | dgst_len = (i >> 3); |
338 | 0 | if (BN_bin2bn(dgst, dgst_len, u1) == NULL) |
339 | 0 | goto err; |
340 | 0 | |
341 | 0 | /* u1 = M * w mod q */ |
342 | 0 | if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx)) |
343 | 0 | goto err; |
344 | 0 | |
345 | 0 | /* u2 = r * w mod q */ |
346 | 0 | if (!BN_mod_mul(u2, r, u2, dsa->q, ctx)) |
347 | 0 | goto err; |
348 | 0 | |
349 | 0 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { |
350 | 0 | mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p, |
351 | 0 | dsa->lock, dsa->p, ctx); |
352 | 0 | if (!mont) |
353 | 0 | goto err; |
354 | 0 | } |
355 | 0 | |
356 | 0 | if (dsa->meth->dsa_mod_exp != NULL) { |
357 | 0 | if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2, |
358 | 0 | dsa->p, ctx, mont)) |
359 | 0 | goto err; |
360 | 0 | } else { |
361 | 0 | if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx, |
362 | 0 | mont)) |
363 | 0 | goto err; |
364 | 0 | } |
365 | 0 | |
366 | 0 | /* let u1 = u1 mod q */ |
367 | 0 | if (!BN_mod(u1, t1, dsa->q, ctx)) |
368 | 0 | goto err; |
369 | 0 | |
370 | 0 | /* |
371 | 0 | * V is now in u1. If the signature is correct, it will be equal to R. |
372 | 0 | */ |
373 | 0 | ret = (BN_ucmp(u1, r) == 0); |
374 | 0 |
|
375 | 0 | err: |
376 | 0 | if (ret < 0) |
377 | 0 | DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB); |
378 | 0 | BN_CTX_free(ctx); |
379 | 0 | BN_free(u1); |
380 | 0 | BN_free(u2); |
381 | 0 | BN_free(t1); |
382 | 0 | return ret; |
383 | 0 | } |
384 | | |
385 | | static int dsa_init(DSA *dsa) |
386 | 0 | { |
387 | 0 | dsa->flags |= DSA_FLAG_CACHE_MONT_P; |
388 | 0 | return 1; |
389 | 0 | } |
390 | | |
391 | | static int dsa_finish(DSA *dsa) |
392 | 0 | { |
393 | 0 | BN_MONT_CTX_free(dsa->method_mont_p); |
394 | 0 | return 1; |
395 | 0 | } |