xref: /openbsd-src/lib/libcrypto/rsa/rsa_eay.c (revision 3a88f7af64c9d83ff2897421820b88bbc08b8c3c)
1 /* $OpenBSD: rsa_eay.c,v 1.44 2017/01/21 09:38:59 beck 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  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 
112 #include <stdio.h>
113 #include <string.h>
114 
115 #include <openssl/opensslconf.h>
116 
117 #include <openssl/bn.h>
118 #include <openssl/err.h>
119 #include <openssl/rsa.h>
120 
121 #include "bn_lcl.h"
122 
123 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
124     unsigned char *to, RSA *rsa, int padding);
125 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
126     unsigned char *to, RSA *rsa, int padding);
127 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
128     unsigned char *to, RSA *rsa, int padding);
129 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
130     unsigned char *to, RSA *rsa, int padding);
131 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
132 static int RSA_eay_init(RSA *rsa);
133 static int RSA_eay_finish(RSA *rsa);
134 
135 static RSA_METHOD rsa_pkcs1_eay_meth = {
136 	.name = "Eric Young's PKCS#1 RSA",
137 	.rsa_pub_enc = RSA_eay_public_encrypt,
138 	.rsa_pub_dec = RSA_eay_public_decrypt, /* signature verification */
139 	.rsa_priv_enc = RSA_eay_private_encrypt, /* signing */
140 	.rsa_priv_dec = RSA_eay_private_decrypt,
141 	.rsa_mod_exp = RSA_eay_mod_exp,
142 	.bn_mod_exp = BN_mod_exp_mont_ct, /* XXX probably we should not use Montgomery if  e == 3 */
143 	.init = RSA_eay_init,
144 	.finish = RSA_eay_finish,
145 };
146 
147 const RSA_METHOD *
148 RSA_PKCS1_SSLeay(void)
149 {
150 	return &rsa_pkcs1_eay_meth;
151 }
152 
153 static int
154 RSA_eay_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
155     RSA *rsa, int padding)
156 {
157 	BIGNUM *f, *ret;
158 	int i, j, k, num = 0, r = -1;
159 	unsigned char *buf = NULL;
160 	BN_CTX *ctx = NULL;
161 
162 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
163 		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
164 		return -1;
165 	}
166 
167 	if (BN_ucmp(rsa->n, rsa->e) <= 0) {
168 		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
169 		return -1;
170 	}
171 
172 	/* for large moduli, enforce exponent limit */
173 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
174 		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
175 			RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
176 			return -1;
177 		}
178 	}
179 
180 	if ((ctx = BN_CTX_new()) == NULL)
181 		goto err;
182 
183 	BN_CTX_start(ctx);
184 	f = BN_CTX_get(ctx);
185 	ret = BN_CTX_get(ctx);
186 	num = BN_num_bytes(rsa->n);
187 	buf = malloc(num);
188 
189 	if (f == NULL || ret == NULL || buf == NULL) {
190 		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
191 		goto err;
192 	}
193 
194 	switch (padding) {
195 	case RSA_PKCS1_PADDING:
196 		i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
197 		break;
198 #ifndef OPENSSL_NO_SHA
199 	case RSA_PKCS1_OAEP_PADDING:
200 		i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
201 		break;
202 #endif
203 	case RSA_SSLV23_PADDING:
204 		i = RSA_padding_add_SSLv23(buf, num, from, flen);
205 		break;
206 	case RSA_NO_PADDING:
207 		i = RSA_padding_add_none(buf, num, from, flen);
208 		break;
209 	default:
210 		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,
211 		    RSA_R_UNKNOWN_PADDING_TYPE);
212 		goto err;
213 	}
214 	if (i <= 0)
215 		goto err;
216 
217 	if (BN_bin2bn(buf, num, f) == NULL)
218 		goto err;
219 
220 	if (BN_ucmp(f, rsa->n) >= 0) {
221 		/* usually the padding functions would catch this */
222 		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,
223 		    RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
224 		goto err;
225 	}
226 
227 	if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
228 		if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
229 		    CRYPTO_LOCK_RSA, rsa->n, ctx))
230 			goto err;
231 
232 	if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
233 	    rsa->_method_mod_n))
234 		goto err;
235 
236 	/* put in leading 0 bytes if the number is less than the
237 	 * length of the modulus */
238 	j = BN_num_bytes(ret);
239 	i = BN_bn2bin(ret, &(to[num - j]));
240 	for (k = 0; k < num - i; k++)
241 		to[k] = 0;
242 
243 	r = num;
244 err:
245 	if (ctx != NULL) {
246 		BN_CTX_end(ctx);
247 		BN_CTX_free(ctx);
248 	}
249 	if (buf != NULL) {
250 		explicit_bzero(buf, num);
251 		free(buf);
252 	}
253 	return r;
254 }
255 
256 static BN_BLINDING *
257 rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
258 {
259 	BN_BLINDING *ret;
260 	int got_write_lock = 0;
261 	CRYPTO_THREADID cur;
262 
263 	CRYPTO_r_lock(CRYPTO_LOCK_RSA);
264 
265 	if (rsa->blinding == NULL) {
266 		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
267 		CRYPTO_w_lock(CRYPTO_LOCK_RSA);
268 		got_write_lock = 1;
269 
270 		if (rsa->blinding == NULL)
271 			rsa->blinding = RSA_setup_blinding(rsa, ctx);
272 	}
273 
274 	ret = rsa->blinding;
275 	if (ret == NULL)
276 		goto err;
277 
278 	CRYPTO_THREADID_current(&cur);
279 	if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) {
280 		/* rsa->blinding is ours! */
281 		*local = 1;
282 	} else {
283 		/* resort to rsa->mt_blinding instead */
284 		/*
285 		 * Instruct rsa_blinding_convert(), rsa_blinding_invert()
286 		 * that the BN_BLINDING is shared, meaning that accesses
287 		 * require locks, and that the blinding factor must be
288 		 * stored outside the BN_BLINDING
289 		 */
290 		*local = 0;
291 
292 		if (rsa->mt_blinding == NULL) {
293 			if (!got_write_lock) {
294 				CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
295 				CRYPTO_w_lock(CRYPTO_LOCK_RSA);
296 				got_write_lock = 1;
297 			}
298 
299 			if (rsa->mt_blinding == NULL)
300 				rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
301 		}
302 		ret = rsa->mt_blinding;
303 	}
304 
305 err:
306 	if (got_write_lock)
307 		CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
308 	else
309 		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
310 	return ret;
311 }
312 
313 static int
314 rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, BN_CTX *ctx)
315 {
316 	if (unblind == NULL)
317 		/*
318 		 * Local blinding: store the unblinding factor
319 		 * in BN_BLINDING.
320 		 */
321 		return BN_BLINDING_convert_ex(f, NULL, b, ctx);
322 	else {
323 		/*
324 		 * Shared blinding: store the unblinding factor
325 		 * outside BN_BLINDING.
326 		 */
327 		int ret;
328 		CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
329 		ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
330 		CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
331 		return ret;
332 	}
333 }
334 
335 static int
336 rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, BN_CTX *ctx)
337 {
338 	/*
339 	 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
340 	 * will use the unblinding factor stored in BN_BLINDING.
341 	 * If BN_BLINDING is shared between threads, unblind must be non-null:
342 	 * BN_BLINDING_invert_ex will then use the local unblinding factor,
343 	 * and will only read the modulus from BN_BLINDING.
344 	 * In both cases it's safe to access the blinding without a lock.
345 	 */
346 	return BN_BLINDING_invert_ex(f, unblind, b, ctx);
347 }
348 
349 /* signing */
350 static int
351 RSA_eay_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
352     RSA *rsa, int padding)
353 {
354 	BIGNUM *f, *ret, *res;
355 	int i, j, k, num = 0, r = -1;
356 	unsigned char *buf = NULL;
357 	BN_CTX *ctx = NULL;
358 	int local_blinding = 0;
359 	/*
360 	 * Used only if the blinding structure is shared. A non-NULL unblind
361 	 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
362 	 * the unblinding factor outside the blinding structure.
363 	 */
364 	BIGNUM *unblind = NULL;
365 	BN_BLINDING *blinding = NULL;
366 
367 	if ((ctx = BN_CTX_new()) == NULL)
368 		goto err;
369 
370 	BN_CTX_start(ctx);
371 	f = BN_CTX_get(ctx);
372 	ret = BN_CTX_get(ctx);
373 	num = BN_num_bytes(rsa->n);
374 	buf = malloc(num);
375 
376 	if (f == NULL || ret == NULL || buf == NULL) {
377 		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
378 		goto err;
379 	}
380 
381 	switch (padding) {
382 	case RSA_PKCS1_PADDING:
383 		i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
384 		break;
385 	case RSA_X931_PADDING:
386 		i = RSA_padding_add_X931(buf, num, from, flen);
387 		break;
388 	case RSA_NO_PADDING:
389 		i = RSA_padding_add_none(buf, num, from, flen);
390 		break;
391 	case RSA_SSLV23_PADDING:
392 	default:
393 		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
394 		    RSA_R_UNKNOWN_PADDING_TYPE);
395 		goto err;
396 	}
397 	if (i <= 0)
398 		goto err;
399 
400 	if (BN_bin2bn(buf, num, f) == NULL)
401 		goto err;
402 
403 	if (BN_ucmp(f, rsa->n) >= 0) {
404 		/* usually the padding functions would catch this */
405 		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
406 		    RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
407 		goto err;
408 	}
409 
410 	if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
411 		blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
412 		if (blinding == NULL) {
413 			RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
414 			    ERR_R_INTERNAL_ERROR);
415 			goto err;
416 		}
417 	}
418 
419 	if (blinding != NULL) {
420 		if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
421 			RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
422 			    ERR_R_MALLOC_FAILURE);
423 			goto err;
424 		}
425 		if (!rsa_blinding_convert(blinding, f, unblind, ctx))
426 			goto err;
427 	}
428 
429 	if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
430 	    (rsa->p != NULL && rsa->q != NULL && rsa->dmp1 != NULL &&
431 	    rsa->dmq1 != NULL && rsa->iqmp != NULL)) {
432 		if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
433 			goto err;
434 	} else {
435 		BIGNUM d;
436 
437 		BN_init(&d);
438 		BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
439 
440 		if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
441 			if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
442 			    CRYPTO_LOCK_RSA, rsa->n, ctx))
443 				goto err;
444 
445 		if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx,
446 		    rsa->_method_mod_n)) {
447 			goto err;
448 		}
449 	}
450 
451 	if (blinding)
452 		if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
453 			goto err;
454 
455 	if (padding == RSA_X931_PADDING) {
456 		BN_sub(f, rsa->n, ret);
457 		if (BN_cmp(ret, f) > 0)
458 			res = f;
459 		else
460 			res = ret;
461 	} else
462 		res = ret;
463 
464 	/* put in leading 0 bytes if the number is less than the
465 	 * length of the modulus */
466 	j = BN_num_bytes(res);
467 	i = BN_bn2bin(res, &(to[num - j]));
468 	for (k = 0; k < num - i; k++)
469 		to[k] = 0;
470 
471 	r = num;
472 err:
473 	if (ctx != NULL) {
474 		BN_CTX_end(ctx);
475 		BN_CTX_free(ctx);
476 	}
477 	if (buf != NULL) {
478 		explicit_bzero(buf, num);
479 		free(buf);
480 	}
481 	return r;
482 }
483 
484 static int
485 RSA_eay_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
486     RSA *rsa, int padding)
487 {
488 	BIGNUM *f, *ret;
489 	int j, num = 0, r = -1;
490 	unsigned char *p;
491 	unsigned char *buf = NULL;
492 	BN_CTX *ctx = NULL;
493 	int local_blinding = 0;
494 	/*
495 	 * Used only if the blinding structure is shared. A non-NULL unblind
496 	 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
497 	 * the unblinding factor outside the blinding structure.
498 	 */
499 	BIGNUM *unblind = NULL;
500 	BN_BLINDING *blinding = NULL;
501 
502 	if ((ctx = BN_CTX_new()) == NULL)
503 		goto err;
504 
505 	BN_CTX_start(ctx);
506 	f = BN_CTX_get(ctx);
507 	ret = BN_CTX_get(ctx);
508 	num = BN_num_bytes(rsa->n);
509 	buf = malloc(num);
510 
511 	if (!f || !ret || !buf) {
512 		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
513 		goto err;
514 	}
515 
516 	/* This check was for equality but PGP does evil things
517 	 * and chops off the top '0' bytes */
518 	if (flen > num) {
519 		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
520 		    RSA_R_DATA_GREATER_THAN_MOD_LEN);
521 		goto err;
522 	}
523 
524 	/* make data into a big number */
525 	if (BN_bin2bn(from, (int)flen, f) == NULL)
526 		goto err;
527 
528 	if (BN_ucmp(f, rsa->n) >= 0) {
529 		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
530 		    RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
531 		goto err;
532 	}
533 
534 	if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
535 		blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
536 		if (blinding == NULL) {
537 			RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
538 			    ERR_R_INTERNAL_ERROR);
539 			goto err;
540 		}
541 	}
542 
543 	if (blinding != NULL) {
544 		if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
545 			RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
546 			    ERR_R_MALLOC_FAILURE);
547 			goto err;
548 		}
549 		if (!rsa_blinding_convert(blinding, f, unblind, ctx))
550 			goto err;
551 	}
552 
553 	/* do the decrypt */
554 	if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
555 	    (rsa->p != NULL && rsa->q != NULL && rsa->dmp1 != NULL &&
556 	    rsa->dmq1 != NULL && rsa->iqmp != NULL)) {
557 		if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
558 			goto err;
559 	} else {
560 		BIGNUM d;
561 
562 		BN_init(&d);
563 		BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
564 
565 		if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
566 			if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
567 			    CRYPTO_LOCK_RSA, rsa->n, ctx))
568 				goto err;
569 
570 		if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx,
571 		    rsa->_method_mod_n)) {
572 			goto err;
573 		}
574 	}
575 
576 	if (blinding)
577 		if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
578 			goto err;
579 
580 	p = buf;
581 	j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
582 
583 	switch (padding) {
584 	case RSA_PKCS1_PADDING:
585 		r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
586 		break;
587 #ifndef OPENSSL_NO_SHA
588 	case RSA_PKCS1_OAEP_PADDING:
589 		r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
590 		break;
591 #endif
592 	case RSA_SSLV23_PADDING:
593 		r = RSA_padding_check_SSLv23(to, num, buf, j, num);
594 		break;
595 	case RSA_NO_PADDING:
596 		r = RSA_padding_check_none(to, num, buf, j, num);
597 		break;
598 	default:
599 		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
600 		    RSA_R_UNKNOWN_PADDING_TYPE);
601 		goto err;
602 	}
603 	if (r < 0)
604 		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
605 		    RSA_R_PADDING_CHECK_FAILED);
606 
607 err:
608 	if (ctx != NULL) {
609 		BN_CTX_end(ctx);
610 		BN_CTX_free(ctx);
611 	}
612 	if (buf != NULL) {
613 		explicit_bzero(buf, num);
614 		free(buf);
615 	}
616 	return r;
617 }
618 
619 /* signature verification */
620 static int
621 RSA_eay_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
622     RSA *rsa, int padding)
623 {
624 	BIGNUM *f, *ret;
625 	int i, num = 0, r = -1;
626 	unsigned char *p;
627 	unsigned char *buf = NULL;
628 	BN_CTX *ctx = NULL;
629 
630 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
631 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
632 		return -1;
633 	}
634 
635 	if (BN_ucmp(rsa->n, rsa->e) <= 0) {
636 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
637 		return -1;
638 	}
639 
640 	/* for large moduli, enforce exponent limit */
641 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
642 		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
643 			RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
644 			return -1;
645 		}
646 	}
647 
648 	if ((ctx = BN_CTX_new()) == NULL)
649 		goto err;
650 
651 	BN_CTX_start(ctx);
652 	f = BN_CTX_get(ctx);
653 	ret = BN_CTX_get(ctx);
654 	num = BN_num_bytes(rsa->n);
655 	buf = malloc(num);
656 
657 	if (!f || !ret || !buf) {
658 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
659 		goto err;
660 	}
661 
662 	/* This check was for equality but PGP does evil things
663 	 * and chops off the top '0' bytes */
664 	if (flen > num) {
665 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,
666 		    RSA_R_DATA_GREATER_THAN_MOD_LEN);
667 		goto err;
668 	}
669 
670 	if (BN_bin2bn(from, flen, f) == NULL)
671 		goto err;
672 
673 	if (BN_ucmp(f, rsa->n) >= 0) {
674 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,
675 		    RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
676 		goto err;
677 	}
678 
679 	if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
680 		if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
681 		    CRYPTO_LOCK_RSA, rsa->n, ctx))
682 			goto err;
683 
684 	if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
685 	    rsa->_method_mod_n))
686 		goto err;
687 
688 	if (padding == RSA_X931_PADDING && (ret->d[0] & 0xf) != 12)
689 		if (!BN_sub(ret, rsa->n, ret))
690 			goto err;
691 
692 	p = buf;
693 	i = BN_bn2bin(ret, p);
694 
695 	switch (padding) {
696 	case RSA_PKCS1_PADDING:
697 		r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
698 		break;
699 	case RSA_X931_PADDING:
700 		r = RSA_padding_check_X931(to, num, buf, i, num);
701 		break;
702 	case RSA_NO_PADDING:
703 		r = RSA_padding_check_none(to, num, buf, i, num);
704 		break;
705 	default:
706 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,
707 		    RSA_R_UNKNOWN_PADDING_TYPE);
708 		goto err;
709 	}
710 	if (r < 0)
711 		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,
712 		    RSA_R_PADDING_CHECK_FAILED);
713 
714 err:
715 	if (ctx != NULL) {
716 		BN_CTX_end(ctx);
717 		BN_CTX_free(ctx);
718 	}
719 	if (buf != NULL) {
720 		explicit_bzero(buf, num);
721 		free(buf);
722 	}
723 	return r;
724 }
725 
726 static int
727 RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
728 {
729 	BIGNUM *r1, *m1, *vrfy;
730 	BIGNUM dmp1, dmq1, c, pr1;
731 	int ret = 0;
732 
733 	BN_CTX_start(ctx);
734 	r1 = BN_CTX_get(ctx);
735 	m1 = BN_CTX_get(ctx);
736 	vrfy = BN_CTX_get(ctx);
737 	if (r1 == NULL || m1 == NULL || vrfy == NULL) {
738 		RSAerr(RSA_F_RSA_EAY_MOD_EXP, ERR_R_MALLOC_FAILURE);
739 		goto err;
740 	}
741 
742 	{
743 		BIGNUM p, q;
744 
745 		/*
746 		 * Make sure BN_mod_inverse in Montgomery intialization uses the
747 		 * BN_FLG_CONSTTIME flag
748 		 */
749 		BN_init(&p);
750 		BN_init(&q);
751 		BN_with_flags(&p, rsa->p, BN_FLG_CONSTTIME);
752 		BN_with_flags(&q, rsa->q, BN_FLG_CONSTTIME);
753 
754 		if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
755 			if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p,
756 			     CRYPTO_LOCK_RSA, &p, ctx) ||
757 			    !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
758 			     CRYPTO_LOCK_RSA, &q, ctx)) {
759 				goto err;
760 			}
761 		}
762 	}
763 
764 	if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
765 		if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
766 		    CRYPTO_LOCK_RSA, rsa->n, ctx))
767 			goto err;
768 
769 	/* compute I mod q */
770 	BN_init(&c);
771 	BN_with_flags(&c, I, BN_FLG_CONSTTIME);
772 
773 	if (!BN_mod(r1, &c, rsa->q, ctx))
774 		goto err;
775 
776 	/* compute r1^dmq1 mod q */
777 	BN_init(&dmq1);
778 	BN_with_flags(&dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
779 
780 	if (!rsa->meth->bn_mod_exp(m1, r1, &dmq1, rsa->q, ctx,
781 	    rsa->_method_mod_q))
782 		goto err;
783 
784 	/* compute I mod p */
785 	BN_with_flags(&c, I, BN_FLG_CONSTTIME);
786 
787 	if (!BN_mod(r1, &c, rsa->p, ctx))
788 		goto err;
789 
790 	/* compute r1^dmp1 mod p */
791 	BN_init(&dmp1);
792 	BN_with_flags(&dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
793 
794 	if (!rsa->meth->bn_mod_exp(r0, r1, &dmp1, rsa->p, ctx,
795 	    rsa->_method_mod_p))
796 		goto err;
797 
798 	if (!BN_sub(r0, r0, m1))
799 		goto err;
800 
801 	/*
802 	 * This will help stop the size of r0 increasing, which does
803 	 * affect the multiply if it optimised for a power of 2 size
804 	 */
805 	if (BN_is_negative(r0))
806 		if (!BN_add(r0, r0, rsa->p))
807 			goto err;
808 
809 	if (!BN_mul(r1, r0, rsa->iqmp, ctx))
810 		goto err;
811 
812 	/* Turn BN_FLG_CONSTTIME flag on before division operation */
813 	BN_init(&pr1);
814 	BN_with_flags(&pr1, r1, BN_FLG_CONSTTIME);
815 
816 	if (!BN_mod(r0, &pr1, rsa->p, ctx))
817 		goto err;
818 
819 	/*
820 	 * If p < q it is occasionally possible for the correction of
821 	 * adding 'p' if r0 is negative above to leave the result still
822 	 * negative. This can break the private key operations: the following
823 	 * second correction should *always* correct this rare occurrence.
824 	 * This will *never* happen with OpenSSL generated keys because
825 	 * they ensure p > q [steve]
826 	 */
827 	if (BN_is_negative(r0))
828 		if (!BN_add(r0, r0, rsa->p))
829 			goto err;
830 	if (!BN_mul(r1, r0, rsa->q, ctx))
831 		goto err;
832 	if (!BN_add(r0, r1, m1))
833 		goto err;
834 
835 	if (rsa->e && rsa->n) {
836 		if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
837 		    rsa->_method_mod_n))
838 			goto err;
839 		/*
840 		 * If 'I' was greater than (or equal to) rsa->n, the operation
841 		 * will be equivalent to using 'I mod n'. However, the result of
842 		 * the verify will *always* be less than 'n' so we don't check
843 		 * for absolute equality, just congruency.
844 		 */
845 		if (!BN_sub(vrfy, vrfy, I))
846 			goto err;
847 		if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
848 			goto err;
849 		if (BN_is_negative(vrfy))
850 			if (!BN_add(vrfy, vrfy, rsa->n))
851 				goto err;
852 		if (!BN_is_zero(vrfy)) {
853 			/*
854 			 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
855 			 * miscalculated CRT output, just do a raw (slower)
856 			 * mod_exp and return that instead.
857 			 */
858 			BIGNUM d;
859 
860 			BN_init(&d);
861 			BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
862 
863 			if (!rsa->meth->bn_mod_exp(r0, I, &d, rsa->n, ctx,
864 			    rsa->_method_mod_n)) {
865 				goto err;
866 			}
867 		}
868 	}
869 	ret = 1;
870 err:
871 	BN_CTX_end(ctx);
872 	return ret;
873 }
874 
875 static int
876 RSA_eay_init(RSA *rsa)
877 {
878 	rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
879 	return 1;
880 }
881 
882 static int
883 RSA_eay_finish(RSA *rsa)
884 {
885 	BN_MONT_CTX_free(rsa->_method_mod_n);
886 	BN_MONT_CTX_free(rsa->_method_mod_p);
887 	BN_MONT_CTX_free(rsa->_method_mod_q);
888 
889 	return 1;
890 }
891