1 /* $OpenBSD: dsa_ossl.c,v 1.40 2018/11/06 07:02:33 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60 
61 #include <stdio.h>
62 
63 #include <openssl/asn1.h>
64 #include <openssl/bn.h>
65 #include <openssl/dsa.h>
66 #include <openssl/err.h>
67 #include <openssl/sha.h>
68 
69 #include "bn_lcl.h"
70 
71 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
72 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
73     BIGNUM **rp);
74 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
75     DSA *dsa);
76 static int dsa_init(DSA *dsa);
77 static int dsa_finish(DSA *dsa);
78 
79 static DSA_METHOD openssl_dsa_meth = {
80 	.name = "OpenSSL DSA method",
81 	.dsa_do_sign = dsa_do_sign,
82 	.dsa_sign_setup = dsa_sign_setup,
83 	.dsa_do_verify = dsa_do_verify,
84 	.init = dsa_init,
85 	.finish = dsa_finish,
86 };
87 
88 const DSA_METHOD *
89 DSA_OpenSSL(void)
90 {
91 	return &openssl_dsa_meth;
92 }
93 
94 static DSA_SIG *
95 dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
96 {
97 	BIGNUM b, bm, bxr, binv, m, *kinv = NULL, *r = NULL, *s = NULL;
98 	BN_CTX *ctx = NULL;
99 	int reason = ERR_R_BN_LIB;
100 	DSA_SIG *ret = NULL;
101 	int noredo = 0;
102 
103 	BN_init(&b);
104 	BN_init(&binv);
105 	BN_init(&bm);
106 	BN_init(&bxr);
107 	BN_init(&m);
108 
109 	if (!dsa->p || !dsa->q || !dsa->g) {
110 		reason = DSA_R_MISSING_PARAMETERS;
111 		goto err;
112 	}
113 
114 	s = BN_new();
115 	if (s == NULL)
116 		goto err;
117 	ctx = BN_CTX_new();
118 	if (ctx == NULL)
119 		goto err;
120 
121 	/*
122 	 * If the digest length is greater than N (the bit length of q), the
123 	 * leftmost N bits of the digest shall be used, see FIPS 186-3, 4.2.
124 	 * In this case the digest length is given in bytes.
125 	 */
126 	if (dlen > BN_num_bytes(dsa->q))
127 		dlen = BN_num_bytes(dsa->q);
128 	if (BN_bin2bn(dgst, dlen, &m) == NULL)
129 		goto err;
130 
131  redo:
132 	if (dsa->kinv == NULL || dsa->r == NULL) {
133 		if (!DSA_sign_setup(dsa, ctx, &kinv, &r))
134 			goto err;
135 	} else {
136 		kinv = dsa->kinv;
137 		dsa->kinv = NULL;
138 		r = dsa->r;
139 		dsa->r = NULL;
140 		noredo = 1;
141 	}
142 
143 	/*
144 	 * Compute:
145 	 *
146 	 *  s = inv(k)(m + xr) mod q
147 	 *
148 	 * In order to reduce the possibility of a side-channel attack, the
149 	 * following is calculated using a blinding value:
150 	 *
151 	 *  s = inv(k)inv(b)(bm + bxr) mod q
152 	 *
153 	 * Where b is a random value in the range [1, q).
154 	 */
155 	if (!bn_rand_interval(&b, BN_value_one(), dsa->q))
156 		goto err;
157 	if (BN_mod_inverse_ct(&binv, &b, dsa->q, ctx) == NULL)
158 		goto err;
159 
160 	if (!BN_mod_mul(&bxr, &b, dsa->priv_key, dsa->q, ctx))	/* bx */
161 		goto err;
162 	if (!BN_mod_mul(&bxr, &bxr, r, dsa->q, ctx))		/* bxr */
163 		goto err;
164 	if (!BN_mod_mul(&bm, &b, &m, dsa->q, ctx))		/* bm */
165 		goto err;
166 	if (!BN_mod_add(s, &bxr, &bm, dsa->q, ctx))		/* s = bm + bxr */
167 		goto err;
168 	if (!BN_mod_mul(s, s, &binv, dsa->q, ctx))		/* s = m + xr */
169 		goto err;
170 	if (!BN_mod_mul(s, s, kinv, dsa->q, ctx))
171 		goto err;
172 
173 	/*
174 	 * Redo if r or s is zero as required by FIPS 186-3: this is very
175 	 * unlikely.
176 	 */
177 	if (BN_is_zero(r) || BN_is_zero(s)) {
178 		if (noredo) {
179 			reason = DSA_R_NEED_NEW_SETUP_VALUES;
180 			goto err;
181 		}
182 		goto redo;
183 	}
184 
185 	if ((ret = DSA_SIG_new()) == NULL) {
186 		reason = ERR_R_MALLOC_FAILURE;
187 		goto err;
188 	}
189 	ret->r = r;
190 	ret->s = s;
191 
192  err:
193 	if (!ret) {
194 		DSAerror(reason);
195 		BN_free(r);
196 		BN_free(s);
197 	}
198 	BN_CTX_free(ctx);
199 	BN_clear_free(&b);
200 	BN_clear_free(&bm);
201 	BN_clear_free(&bxr);
202 	BN_clear_free(&binv);
203 	BN_clear_free(&m);
204 	BN_clear_free(kinv);
205 
206 	return ret;
207 }
208 
209 static int
210 dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
211 {
212 	BN_CTX *ctx;
213 	BIGNUM k, l, m, *kinv = NULL, *r = NULL;
214 	int q_bits, ret = 0;
215 
216 	if (!dsa->p || !dsa->q || !dsa->g) {
217 		DSAerror(DSA_R_MISSING_PARAMETERS);
218 		return 0;
219 	}
220 
221 	BN_init(&k);
222 	BN_init(&l);
223 	BN_init(&m);
224 
225 	if (ctx_in == NULL) {
226 		if ((ctx = BN_CTX_new()) == NULL)
227 			goto err;
228 	} else
229 		ctx = ctx_in;
230 
231 	if ((r = BN_new()) == NULL)
232 		goto err;
233 
234 	/* Preallocate space */
235 	q_bits = BN_num_bits(dsa->q);
236 	if (!BN_set_bit(&k, q_bits) ||
237 	    !BN_set_bit(&l, q_bits) ||
238 	    !BN_set_bit(&m, q_bits))
239 		goto err;
240 
241 	if (!bn_rand_interval(&k, BN_value_one(), dsa->q))
242 		goto err;
243 
244 	BN_set_flags(&k, BN_FLG_CONSTTIME);
245 
246 	if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
247 		if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
248 		    CRYPTO_LOCK_DSA, dsa->p, ctx))
249 			goto err;
250 	}
251 
252 	/* Compute r = (g^k mod p) mod q */
253 
254 	/*
255 	 * We do not want timing information to leak the length of k,
256 	 * so we compute G^k using an equivalent exponent of fixed
257 	 * bit-length.
258 	 *
259 	 * We unconditionally perform both of these additions to prevent a
260 	 * small timing information leakage.  We then choose the sum that is
261 	 * one bit longer than the modulus.
262 	 *
263 	 * TODO: revisit the BN_copy aiming for a memory access agnostic
264 	 * conditional copy.
265 	 */
266 
267 	if (!BN_add(&l, &k, dsa->q) ||
268 	    !BN_add(&m, &l, dsa->q) ||
269 	    !BN_copy(&k, BN_num_bits(&l) > q_bits ? &l : &m))
270 		goto err;
271 
272 	if (dsa->meth->bn_mod_exp != NULL) {
273 		if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, &k, dsa->p, ctx,
274 		    dsa->method_mont_p))
275 			goto err;
276 	} else {
277 		if (!BN_mod_exp_mont_ct(r, dsa->g, &k, dsa->p, ctx,
278 		    dsa->method_mont_p))
279 			goto err;
280 	}
281 
282 	if (!BN_mod_ct(r, r, dsa->q, ctx))
283 		goto err;
284 
285 	/* Compute  part of 's = inv(k) (m + xr) mod q' */
286 	if ((kinv = BN_mod_inverse_ct(NULL, &k, dsa->q, ctx)) == NULL)
287 		goto err;
288 
289 	BN_clear_free(*kinvp);
290 	*kinvp = kinv;
291 	kinv = NULL;
292 	BN_clear_free(*rp);
293 	*rp = r;
294 
295 	ret = 1;
296 
297  err:
298 	if (!ret) {
299 		DSAerror(ERR_R_BN_LIB);
300 		BN_clear_free(r);
301 	}
302 	if (ctx_in == NULL)
303 		BN_CTX_free(ctx);
304 	BN_clear_free(&k);
305 	BN_clear_free(&l);
306 	BN_clear_free(&m);
307 
308 	return ret;
309 }
310 
311 static int
312 dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa)
313 {
314 	BN_CTX *ctx;
315 	BIGNUM u1, u2, t1;
316 	BN_MONT_CTX *mont = NULL;
317 	int ret = -1, i;
318 
319 	if (!dsa->p || !dsa->q || !dsa->g) {
320 		DSAerror(DSA_R_MISSING_PARAMETERS);
321 		return -1;
322 	}
323 
324 	i = BN_num_bits(dsa->q);
325 	/* FIPS 186-3 allows only three different sizes for q. */
326 	if (i != 160 && i != 224 && i != 256) {
327 		DSAerror(DSA_R_BAD_Q_VALUE);
328 		return -1;
329 	}
330 
331 	if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
332 		DSAerror(DSA_R_MODULUS_TOO_LARGE);
333 		return -1;
334 	}
335 	BN_init(&u1);
336 	BN_init(&u2);
337 	BN_init(&t1);
338 
339 	if ((ctx = BN_CTX_new()) == NULL)
340 		goto err;
341 
342 	if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
343 	    BN_ucmp(sig->r, dsa->q) >= 0) {
344 		ret = 0;
345 		goto err;
346 	}
347 	if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
348 	    BN_ucmp(sig->s, dsa->q) >= 0) {
349 		ret = 0;
350 		goto err;
351 	}
352 
353 	/* Calculate w = inv(s) mod q, saving w in u2. */
354 	if ((BN_mod_inverse_ct(&u2, sig->s, dsa->q, ctx)) == NULL)
355 		goto err;
356 
357 	/*
358 	 * If the digest length is greater than the size of q use the
359 	 * BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-3, 4.2.
360 	 */
361 	if (dgst_len > (i >> 3))
362 		dgst_len = (i >> 3);
363 
364 	/* Save m in u1. */
365 	if (BN_bin2bn(dgst, dgst_len, &u1) == NULL)
366 		goto err;
367 
368 	/* u1 = m * w mod q */
369 	if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx))
370 		goto err;
371 
372 	/* u2 = r * w mod q */
373 	if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx))
374 		goto err;
375 
376 	if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
377 		mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
378 		    CRYPTO_LOCK_DSA, dsa->p, ctx);
379 		if (!mont)
380 			goto err;
381 	}
382 
383 	if (dsa->meth->dsa_mod_exp != NULL) {
384 		if (!dsa->meth->dsa_mod_exp(dsa, &t1, dsa->g, &u1, dsa->pub_key,
385 		    &u2, dsa->p, ctx, mont))
386 			goto err;
387 	} else {
388 		if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2,
389 		    dsa->p, ctx, mont))
390 			goto err;
391 	}
392 
393 	/* BN_copy(&u1,&t1); */
394 	/* let u1 = u1 mod q */
395 	if (!BN_mod_ct(&u1, &t1, dsa->q, ctx))
396 		goto err;
397 
398 	/* v is in u1 - if the signature is correct, it will be equal to r. */
399 	ret = BN_ucmp(&u1, sig->r) == 0;
400 
401  err:
402 	if (ret < 0)
403 		DSAerror(ERR_R_BN_LIB);
404 	BN_CTX_free(ctx);
405 	BN_free(&u1);
406 	BN_free(&u2);
407 	BN_free(&t1);
408 
409 	return ret;
410 }
411 
412 static int
413 dsa_init(DSA *dsa)
414 {
415 	dsa->flags |= DSA_FLAG_CACHE_MONT_P;
416 	return 1;
417 }
418 
419 static int
420 dsa_finish(DSA *dsa)
421 {
422 	BN_MONT_CTX_free(dsa->method_mont_p);
423 	return 1;
424 }
425 
426