xref: /dragonfly/crypto/libressl/crypto/rsa/rsa_eay.c (revision cca6fc52)
1 /* $OpenBSD: rsa_eay.c,v 1.51 2019/11/02 13:52:31 jsing 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_OpenSSL(void)
149 {
150 	return &rsa_pkcs1_eay_meth;
151 }
152 
153 const RSA_METHOD *
154 RSA_PKCS1_SSLeay(void)
155 {
156 	return &rsa_pkcs1_eay_meth;
157 }
158 
159 static int
160 RSA_eay_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
161     RSA *rsa, int padding)
162 {
163 	BIGNUM *f, *ret;
164 	int i, j, k, num = 0, r = -1;
165 	unsigned char *buf = NULL;
166 	BN_CTX *ctx = NULL;
167 
168 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
169 		RSAerror(RSA_R_MODULUS_TOO_LARGE);
170 		return -1;
171 	}
172 
173 	if (BN_ucmp(rsa->n, rsa->e) <= 0) {
174 		RSAerror(RSA_R_BAD_E_VALUE);
175 		return -1;
176 	}
177 
178 	/* for large moduli, enforce exponent limit */
179 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
180 		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
181 			RSAerror(RSA_R_BAD_E_VALUE);
182 			return -1;
183 		}
184 	}
185 
186 	if ((ctx = BN_CTX_new()) == NULL)
187 		goto err;
188 
189 	BN_CTX_start(ctx);
190 	f = BN_CTX_get(ctx);
191 	ret = BN_CTX_get(ctx);
192 	num = BN_num_bytes(rsa->n);
193 	buf = malloc(num);
194 
195 	if (f == NULL || ret == NULL || buf == NULL) {
196 		RSAerror(ERR_R_MALLOC_FAILURE);
197 		goto err;
198 	}
199 
200 	switch (padding) {
201 	case RSA_PKCS1_PADDING:
202 		i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
203 		break;
204 #ifndef OPENSSL_NO_SHA
205 	case RSA_PKCS1_OAEP_PADDING:
206 		i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
207 		break;
208 #endif
209 	case RSA_NO_PADDING:
210 		i = RSA_padding_add_none(buf, num, from, flen);
211 		break;
212 	default:
213 		RSAerror(RSA_R_UNKNOWN_PADDING_TYPE);
214 		goto err;
215 	}
216 	if (i <= 0)
217 		goto err;
218 
219 	if (BN_bin2bn(buf, num, f) == NULL)
220 		goto err;
221 
222 	if (BN_ucmp(f, rsa->n) >= 0) {
223 		/* usually the padding functions would catch this */
224 		RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
225 		goto err;
226 	}
227 
228 	if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
229 		if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
230 		    CRYPTO_LOCK_RSA, rsa->n, ctx))
231 			goto err;
232 
233 	if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
234 	    rsa->_method_mod_n))
235 		goto err;
236 
237 	/* put in leading 0 bytes if the number is less than the
238 	 * length of the modulus */
239 	j = BN_num_bytes(ret);
240 	i = BN_bn2bin(ret, &(to[num - j]));
241 	for (k = 0; k < num - i; k++)
242 		to[k] = 0;
243 
244 	r = num;
245 err:
246 	if (ctx != NULL) {
247 		BN_CTX_end(ctx);
248 		BN_CTX_free(ctx);
249 	}
250 	freezero(buf, num);
251 	return r;
252 }
253 
254 static BN_BLINDING *
255 rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
256 {
257 	BN_BLINDING *ret;
258 	int got_write_lock = 0;
259 	CRYPTO_THREADID cur;
260 
261 	CRYPTO_r_lock(CRYPTO_LOCK_RSA);
262 
263 	if (rsa->blinding == NULL) {
264 		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
265 		CRYPTO_w_lock(CRYPTO_LOCK_RSA);
266 		got_write_lock = 1;
267 
268 		if (rsa->blinding == NULL)
269 			rsa->blinding = RSA_setup_blinding(rsa, ctx);
270 	}
271 
272 	ret = rsa->blinding;
273 	if (ret == NULL)
274 		goto err;
275 
276 	CRYPTO_THREADID_current(&cur);
277 	if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) {
278 		/* rsa->blinding is ours! */
279 		*local = 1;
280 	} else {
281 		/* resort to rsa->mt_blinding instead */
282 		/*
283 		 * Instruct rsa_blinding_convert(), rsa_blinding_invert()
284 		 * that the BN_BLINDING is shared, meaning that accesses
285 		 * require locks, and that the blinding factor must be
286 		 * stored outside the BN_BLINDING
287 		 */
288 		*local = 0;
289 
290 		if (rsa->mt_blinding == NULL) {
291 			if (!got_write_lock) {
292 				CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
293 				CRYPTO_w_lock(CRYPTO_LOCK_RSA);
294 				got_write_lock = 1;
295 			}
296 
297 			if (rsa->mt_blinding == NULL)
298 				rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
299 		}
300 		ret = rsa->mt_blinding;
301 	}
302 
303 err:
304 	if (got_write_lock)
305 		CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
306 	else
307 		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
308 	return ret;
309 }
310 
311 static int
312 rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, BN_CTX *ctx)
313 {
314 	if (unblind == NULL)
315 		/*
316 		 * Local blinding: store the unblinding factor
317 		 * in BN_BLINDING.
318 		 */
319 		return BN_BLINDING_convert_ex(f, NULL, b, ctx);
320 	else {
321 		/*
322 		 * Shared blinding: store the unblinding factor
323 		 * outside BN_BLINDING.
324 		 */
325 		int ret;
326 		CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
327 		ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
328 		CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
329 		return ret;
330 	}
331 }
332 
333 static int
334 rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, BN_CTX *ctx)
335 {
336 	/*
337 	 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
338 	 * will use the unblinding factor stored in BN_BLINDING.
339 	 * If BN_BLINDING is shared between threads, unblind must be non-null:
340 	 * BN_BLINDING_invert_ex will then use the local unblinding factor,
341 	 * and will only read the modulus from BN_BLINDING.
342 	 * In both cases it's safe to access the blinding without a lock.
343 	 */
344 	return BN_BLINDING_invert_ex(f, unblind, b, ctx);
345 }
346 
347 /* signing */
348 static int
349 RSA_eay_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
350     RSA *rsa, int padding)
351 {
352 	BIGNUM *f, *ret, *res;
353 	int i, j, k, num = 0, r = -1;
354 	unsigned char *buf = NULL;
355 	BN_CTX *ctx = NULL;
356 	int local_blinding = 0;
357 	/*
358 	 * Used only if the blinding structure is shared. A non-NULL unblind
359 	 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
360 	 * the unblinding factor outside the blinding structure.
361 	 */
362 	BIGNUM *unblind = NULL;
363 	BN_BLINDING *blinding = NULL;
364 
365 	if ((ctx = BN_CTX_new()) == NULL)
366 		goto err;
367 
368 	BN_CTX_start(ctx);
369 	f = BN_CTX_get(ctx);
370 	ret = BN_CTX_get(ctx);
371 	num = BN_num_bytes(rsa->n);
372 	buf = malloc(num);
373 
374 	if (f == NULL || ret == NULL || buf == NULL) {
375 		RSAerror(ERR_R_MALLOC_FAILURE);
376 		goto err;
377 	}
378 
379 	switch (padding) {
380 	case RSA_PKCS1_PADDING:
381 		i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
382 		break;
383 	case RSA_X931_PADDING:
384 		i = RSA_padding_add_X931(buf, num, from, flen);
385 		break;
386 	case RSA_NO_PADDING:
387 		i = RSA_padding_add_none(buf, num, from, flen);
388 		break;
389 	default:
390 		RSAerror(RSA_R_UNKNOWN_PADDING_TYPE);
391 		goto err;
392 	}
393 	if (i <= 0)
394 		goto err;
395 
396 	if (BN_bin2bn(buf, num, f) == NULL)
397 		goto err;
398 
399 	if (BN_ucmp(f, rsa->n) >= 0) {
400 		/* usually the padding functions would catch this */
401 		RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
402 		goto err;
403 	}
404 
405 	if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
406 		blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
407 		if (blinding == NULL) {
408 			RSAerror(ERR_R_INTERNAL_ERROR);
409 			goto err;
410 		}
411 	}
412 
413 	if (blinding != NULL) {
414 		if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
415 			RSAerror(ERR_R_MALLOC_FAILURE);
416 			goto err;
417 		}
418 		if (!rsa_blinding_convert(blinding, f, unblind, ctx))
419 			goto err;
420 	}
421 
422 	if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
423 	    (rsa->p != NULL && rsa->q != NULL && rsa->dmp1 != NULL &&
424 	    rsa->dmq1 != NULL && rsa->iqmp != NULL)) {
425 		if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
426 			goto err;
427 	} else {
428 		BIGNUM d;
429 
430 		BN_init(&d);
431 		BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
432 
433 		if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
434 			if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
435 			    CRYPTO_LOCK_RSA, rsa->n, ctx))
436 				goto err;
437 
438 		if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx,
439 		    rsa->_method_mod_n)) {
440 			goto err;
441 		}
442 	}
443 
444 	if (blinding)
445 		if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
446 			goto err;
447 
448 	if (padding == RSA_X931_PADDING) {
449 		BN_sub(f, rsa->n, ret);
450 		if (BN_cmp(ret, f) > 0)
451 			res = f;
452 		else
453 			res = ret;
454 	} else
455 		res = ret;
456 
457 	/* put in leading 0 bytes if the number is less than the
458 	 * length of the modulus */
459 	j = BN_num_bytes(res);
460 	i = BN_bn2bin(res, &(to[num - j]));
461 	for (k = 0; k < num - i; k++)
462 		to[k] = 0;
463 
464 	r = num;
465 err:
466 	if (ctx != NULL) {
467 		BN_CTX_end(ctx);
468 		BN_CTX_free(ctx);
469 	}
470 	freezero(buf, num);
471 	return r;
472 }
473 
474 static int
475 RSA_eay_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
476     RSA *rsa, int padding)
477 {
478 	BIGNUM *f, *ret;
479 	int j, num = 0, r = -1;
480 	unsigned char *p;
481 	unsigned char *buf = NULL;
482 	BN_CTX *ctx = NULL;
483 	int local_blinding = 0;
484 	/*
485 	 * Used only if the blinding structure is shared. A non-NULL unblind
486 	 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
487 	 * the unblinding factor outside the blinding structure.
488 	 */
489 	BIGNUM *unblind = NULL;
490 	BN_BLINDING *blinding = NULL;
491 
492 	if ((ctx = BN_CTX_new()) == NULL)
493 		goto err;
494 
495 	BN_CTX_start(ctx);
496 	f = BN_CTX_get(ctx);
497 	ret = BN_CTX_get(ctx);
498 	num = BN_num_bytes(rsa->n);
499 	buf = malloc(num);
500 
501 	if (!f || !ret || !buf) {
502 		RSAerror(ERR_R_MALLOC_FAILURE);
503 		goto err;
504 	}
505 
506 	/* This check was for equality but PGP does evil things
507 	 * and chops off the top '0' bytes */
508 	if (flen > num) {
509 		RSAerror(RSA_R_DATA_GREATER_THAN_MOD_LEN);
510 		goto err;
511 	}
512 
513 	/* make data into a big number */
514 	if (BN_bin2bn(from, (int)flen, f) == NULL)
515 		goto err;
516 
517 	if (BN_ucmp(f, rsa->n) >= 0) {
518 		RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
519 		goto err;
520 	}
521 
522 	if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
523 		blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
524 		if (blinding == NULL) {
525 			RSAerror(ERR_R_INTERNAL_ERROR);
526 			goto err;
527 		}
528 	}
529 
530 	if (blinding != NULL) {
531 		if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
532 			RSAerror(ERR_R_MALLOC_FAILURE);
533 			goto err;
534 		}
535 		if (!rsa_blinding_convert(blinding, f, unblind, ctx))
536 			goto err;
537 	}
538 
539 	/* do the decrypt */
540 	if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
541 	    (rsa->p != NULL && rsa->q != NULL && rsa->dmp1 != NULL &&
542 	    rsa->dmq1 != NULL && rsa->iqmp != NULL)) {
543 		if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
544 			goto err;
545 	} else {
546 		BIGNUM d;
547 
548 		BN_init(&d);
549 		BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
550 
551 		if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
552 			if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
553 			    CRYPTO_LOCK_RSA, rsa->n, ctx))
554 				goto err;
555 
556 		if (!rsa->meth->bn_mod_exp(ret, f, &d, rsa->n, ctx,
557 		    rsa->_method_mod_n)) {
558 			goto err;
559 		}
560 	}
561 
562 	if (blinding)
563 		if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
564 			goto err;
565 
566 	p = buf;
567 	j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
568 
569 	switch (padding) {
570 	case RSA_PKCS1_PADDING:
571 		r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
572 		break;
573 #ifndef OPENSSL_NO_SHA
574 	case RSA_PKCS1_OAEP_PADDING:
575 		r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
576 		break;
577 #endif
578 	case RSA_NO_PADDING:
579 		r = RSA_padding_check_none(to, num, buf, j, num);
580 		break;
581 	default:
582 		RSAerror(RSA_R_UNKNOWN_PADDING_TYPE);
583 		goto err;
584 	}
585 	if (r < 0)
586 		RSAerror(RSA_R_PADDING_CHECK_FAILED);
587 
588 err:
589 	if (ctx != NULL) {
590 		BN_CTX_end(ctx);
591 		BN_CTX_free(ctx);
592 	}
593 	freezero(buf, num);
594 	return r;
595 }
596 
597 /* signature verification */
598 static int
599 RSA_eay_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
600     RSA *rsa, int padding)
601 {
602 	BIGNUM *f, *ret;
603 	int i, num = 0, r = -1;
604 	unsigned char *p;
605 	unsigned char *buf = NULL;
606 	BN_CTX *ctx = NULL;
607 
608 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
609 		RSAerror(RSA_R_MODULUS_TOO_LARGE);
610 		return -1;
611 	}
612 
613 	if (BN_ucmp(rsa->n, rsa->e) <= 0) {
614 		RSAerror(RSA_R_BAD_E_VALUE);
615 		return -1;
616 	}
617 
618 	/* for large moduli, enforce exponent limit */
619 	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
620 		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
621 			RSAerror(RSA_R_BAD_E_VALUE);
622 			return -1;
623 		}
624 	}
625 
626 	if ((ctx = BN_CTX_new()) == NULL)
627 		goto err;
628 
629 	BN_CTX_start(ctx);
630 	f = BN_CTX_get(ctx);
631 	ret = BN_CTX_get(ctx);
632 	num = BN_num_bytes(rsa->n);
633 	buf = malloc(num);
634 
635 	if (!f || !ret || !buf) {
636 		RSAerror(ERR_R_MALLOC_FAILURE);
637 		goto err;
638 	}
639 
640 	/* This check was for equality but PGP does evil things
641 	 * and chops off the top '0' bytes */
642 	if (flen > num) {
643 		RSAerror(RSA_R_DATA_GREATER_THAN_MOD_LEN);
644 		goto err;
645 	}
646 
647 	if (BN_bin2bn(from, flen, f) == NULL)
648 		goto err;
649 
650 	if (BN_ucmp(f, rsa->n) >= 0) {
651 		RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
652 		goto err;
653 	}
654 
655 	if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
656 		if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
657 		    CRYPTO_LOCK_RSA, rsa->n, ctx))
658 			goto err;
659 
660 	if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
661 	    rsa->_method_mod_n))
662 		goto err;
663 
664 	if (padding == RSA_X931_PADDING && (ret->d[0] & 0xf) != 12)
665 		if (!BN_sub(ret, rsa->n, ret))
666 			goto err;
667 
668 	p = buf;
669 	i = BN_bn2bin(ret, p);
670 
671 	switch (padding) {
672 	case RSA_PKCS1_PADDING:
673 		r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
674 		break;
675 	case RSA_X931_PADDING:
676 		r = RSA_padding_check_X931(to, num, buf, i, num);
677 		break;
678 	case RSA_NO_PADDING:
679 		r = RSA_padding_check_none(to, num, buf, i, num);
680 		break;
681 	default:
682 		RSAerror(RSA_R_UNKNOWN_PADDING_TYPE);
683 		goto err;
684 	}
685 	if (r < 0)
686 		RSAerror(RSA_R_PADDING_CHECK_FAILED);
687 
688 err:
689 	if (ctx != NULL) {
690 		BN_CTX_end(ctx);
691 		BN_CTX_free(ctx);
692 	}
693 	freezero(buf, num);
694 	return r;
695 }
696 
697 static int
698 RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
699 {
700 	BIGNUM *r1, *m1, *vrfy;
701 	BIGNUM dmp1, dmq1, c, pr1;
702 	int ret = 0;
703 
704 	BN_CTX_start(ctx);
705 	r1 = BN_CTX_get(ctx);
706 	m1 = BN_CTX_get(ctx);
707 	vrfy = BN_CTX_get(ctx);
708 	if (r1 == NULL || m1 == NULL || vrfy == NULL) {
709 		RSAerror(ERR_R_MALLOC_FAILURE);
710 		goto err;
711 	}
712 
713 	{
714 		BIGNUM p, q;
715 
716 		/*
717 		 * Make sure BN_mod_inverse in Montgomery intialization uses the
718 		 * BN_FLG_CONSTTIME flag
719 		 */
720 		BN_init(&p);
721 		BN_init(&q);
722 		BN_with_flags(&p, rsa->p, BN_FLG_CONSTTIME);
723 		BN_with_flags(&q, rsa->q, BN_FLG_CONSTTIME);
724 
725 		if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
726 			if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p,
727 			     CRYPTO_LOCK_RSA, &p, ctx) ||
728 			    !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
729 			     CRYPTO_LOCK_RSA, &q, ctx)) {
730 				goto err;
731 			}
732 		}
733 	}
734 
735 	if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
736 		if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
737 		    CRYPTO_LOCK_RSA, rsa->n, ctx))
738 			goto err;
739 
740 	/* compute I mod q */
741 	BN_init(&c);
742 	BN_with_flags(&c, I, BN_FLG_CONSTTIME);
743 
744 	if (!BN_mod_ct(r1, &c, rsa->q, ctx))
745 		goto err;
746 
747 	/* compute r1^dmq1 mod q */
748 	BN_init(&dmq1);
749 	BN_with_flags(&dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
750 
751 	if (!rsa->meth->bn_mod_exp(m1, r1, &dmq1, rsa->q, ctx,
752 	    rsa->_method_mod_q))
753 		goto err;
754 
755 	/* compute I mod p */
756 	BN_with_flags(&c, I, BN_FLG_CONSTTIME);
757 
758 	if (!BN_mod_ct(r1, &c, rsa->p, ctx))
759 		goto err;
760 
761 	/* compute r1^dmp1 mod p */
762 	BN_init(&dmp1);
763 	BN_with_flags(&dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
764 
765 	if (!rsa->meth->bn_mod_exp(r0, r1, &dmp1, rsa->p, ctx,
766 	    rsa->_method_mod_p))
767 		goto err;
768 
769 	if (!BN_sub(r0, r0, m1))
770 		goto err;
771 
772 	/*
773 	 * This will help stop the size of r0 increasing, which does
774 	 * affect the multiply if it optimised for a power of 2 size
775 	 */
776 	if (BN_is_negative(r0))
777 		if (!BN_add(r0, r0, rsa->p))
778 			goto err;
779 
780 	if (!BN_mul(r1, r0, rsa->iqmp, ctx))
781 		goto err;
782 
783 	/* Turn BN_FLG_CONSTTIME flag on before division operation */
784 	BN_init(&pr1);
785 	BN_with_flags(&pr1, r1, BN_FLG_CONSTTIME);
786 
787 	if (!BN_mod_ct(r0, &pr1, rsa->p, ctx))
788 		goto err;
789 
790 	/*
791 	 * If p < q it is occasionally possible for the correction of
792 	 * adding 'p' if r0 is negative above to leave the result still
793 	 * negative. This can break the private key operations: the following
794 	 * second correction should *always* correct this rare occurrence.
795 	 * This will *never* happen with OpenSSL generated keys because
796 	 * they ensure p > q [steve]
797 	 */
798 	if (BN_is_negative(r0))
799 		if (!BN_add(r0, r0, rsa->p))
800 			goto err;
801 	if (!BN_mul(r1, r0, rsa->q, ctx))
802 		goto err;
803 	if (!BN_add(r0, r1, m1))
804 		goto err;
805 
806 	if (rsa->e && rsa->n) {
807 		if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
808 		    rsa->_method_mod_n))
809 			goto err;
810 		/*
811 		 * If 'I' was greater than (or equal to) rsa->n, the operation
812 		 * will be equivalent to using 'I mod n'. However, the result of
813 		 * the verify will *always* be less than 'n' so we don't check
814 		 * for absolute equality, just congruency.
815 		 */
816 		if (!BN_sub(vrfy, vrfy, I))
817 			goto err;
818 		if (!BN_mod_ct(vrfy, vrfy, rsa->n, ctx))
819 			goto err;
820 		if (BN_is_negative(vrfy))
821 			if (!BN_add(vrfy, vrfy, rsa->n))
822 				goto err;
823 		if (!BN_is_zero(vrfy)) {
824 			/*
825 			 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
826 			 * miscalculated CRT output, just do a raw (slower)
827 			 * mod_exp and return that instead.
828 			 */
829 			BIGNUM d;
830 
831 			BN_init(&d);
832 			BN_with_flags(&d, rsa->d, BN_FLG_CONSTTIME);
833 
834 			if (!rsa->meth->bn_mod_exp(r0, I, &d, rsa->n, ctx,
835 			    rsa->_method_mod_n)) {
836 				goto err;
837 			}
838 		}
839 	}
840 	ret = 1;
841 err:
842 	BN_CTX_end(ctx);
843 	return ret;
844 }
845 
846 static int
847 RSA_eay_init(RSA *rsa)
848 {
849 	rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
850 	return 1;
851 }
852 
853 static int
854 RSA_eay_finish(RSA *rsa)
855 {
856 	BN_MONT_CTX_free(rsa->_method_mod_n);
857 	BN_MONT_CTX_free(rsa->_method_mod_p);
858 	BN_MONT_CTX_free(rsa->_method_mod_q);
859 
860 	return 1;
861 }
862