2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include "internal/bn_int.h"
12 #include <openssl/rand.h>
17 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
18 unsigned char *to, RSA *rsa, int padding);
19 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
20 unsigned char *to, RSA *rsa, int padding);
21 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
22 unsigned char *to, RSA *rsa, int padding);
23 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
24 unsigned char *to, RSA *rsa, int padding);
25 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
27 static int rsa_ossl_init(RSA *rsa);
28 static int rsa_ossl_finish(RSA *rsa);
29 static RSA_METHOD rsa_pkcs1_ossl_meth = {
30 "OpenSSL PKCS#1 RSA (from Eric Young)",
31 rsa_ossl_public_encrypt,
32 rsa_ossl_public_decrypt, /* signature verification */
33 rsa_ossl_private_encrypt, /* signing */
34 rsa_ossl_private_decrypt,
36 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
40 RSA_FLAG_FIPS_METHOD, /* flags */
47 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
49 return &rsa_pkcs1_ossl_meth;
52 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
53 unsigned char *to, RSA *rsa, int padding)
56 int i, j, k, num = 0, r = -1;
57 unsigned char *buf = NULL;
60 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
61 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
65 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
66 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
70 /* for large moduli, enforce exponent limit */
71 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
72 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
73 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
78 if ((ctx = BN_CTX_new()) == NULL)
82 ret = BN_CTX_get(ctx);
83 num = BN_num_bytes(rsa->n);
84 buf = OPENSSL_malloc(num);
85 if (f == NULL || ret == NULL || buf == NULL) {
86 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
91 case RSA_PKCS1_PADDING:
92 i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
94 case RSA_PKCS1_OAEP_PADDING:
95 i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
97 case RSA_SSLV23_PADDING:
98 i = RSA_padding_add_SSLv23(buf, num, from, flen);
101 i = RSA_padding_add_none(buf, num, from, flen);
104 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
110 if (BN_bin2bn(buf, num, f) == NULL)
113 if (BN_ucmp(f, rsa->n) >= 0) {
114 /* usually the padding functions would catch this */
115 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
116 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
120 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
121 if (!BN_MONT_CTX_set_locked
122 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
125 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
130 * put in leading 0 bytes if the number is less than the length of the
133 j = BN_num_bytes(ret);
134 i = BN_bn2bin(ret, &(to[num - j]));
135 for (k = 0; k < (num - i); k++)
143 OPENSSL_clear_free(buf, num);
147 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
151 CRYPTO_THREAD_write_lock(rsa->lock);
153 if (rsa->blinding == NULL) {
154 rsa->blinding = RSA_setup_blinding(rsa, ctx);
161 if (BN_BLINDING_is_current_thread(ret)) {
162 /* rsa->blinding is ours! */
166 /* resort to rsa->mt_blinding instead */
169 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
170 * BN_BLINDING is shared, meaning that accesses require locks, and
171 * that the blinding factor must be stored outside the BN_BLINDING
175 if (rsa->mt_blinding == NULL) {
176 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
178 ret = rsa->mt_blinding;
182 CRYPTO_THREAD_unlock(rsa->lock);
186 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
191 * Local blinding: store the unblinding factor in BN_BLINDING.
193 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
196 * Shared blinding: store the unblinding factor outside BN_BLINDING.
201 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
202 BN_BLINDING_unlock(b);
208 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
212 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
213 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
214 * is shared between threads, unblind must be non-null:
215 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
216 * will only read the modulus from BN_BLINDING. In both cases it's safe
217 * to access the blinding without a lock.
219 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
223 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
224 unsigned char *to, RSA *rsa, int padding)
226 BIGNUM *f, *ret, *res;
227 int i, j, k, num = 0, r = -1;
228 unsigned char *buf = NULL;
230 int local_blinding = 0;
232 * Used only if the blinding structure is shared. A non-NULL unblind
233 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
234 * the unblinding factor outside the blinding structure.
236 BIGNUM *unblind = NULL;
237 BN_BLINDING *blinding = NULL;
239 if ((ctx = BN_CTX_new()) == NULL)
243 ret = BN_CTX_get(ctx);
244 num = BN_num_bytes(rsa->n);
245 buf = OPENSSL_malloc(num);
246 if (f == NULL || ret == NULL || buf == NULL) {
247 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
252 case RSA_PKCS1_PADDING:
253 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
255 case RSA_X931_PADDING:
256 i = RSA_padding_add_X931(buf, num, from, flen);
259 i = RSA_padding_add_none(buf, num, from, flen);
261 case RSA_SSLV23_PADDING:
263 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
269 if (BN_bin2bn(buf, num, f) == NULL)
272 if (BN_ucmp(f, rsa->n) >= 0) {
273 /* usually the padding functions would catch this */
274 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
275 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
279 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
280 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
281 if (blinding == NULL) {
282 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
287 if (blinding != NULL) {
288 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
289 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
292 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
296 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
299 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
300 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
303 BIGNUM *d = NULL, *local_d = NULL;
305 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
306 local_d = d = BN_new();
308 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
311 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
316 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
317 if (!BN_MONT_CTX_set_locked
318 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
323 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
324 rsa->_method_mod_n)) {
328 /* We MUST free local_d before any further use of rsa->d */
333 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
336 if (padding == RSA_X931_PADDING) {
337 BN_sub(f, rsa->n, ret);
338 if (BN_cmp(ret, f) > 0)
346 * put in leading 0 bytes if the number is less than the length of the
349 j = BN_num_bytes(res);
350 i = BN_bn2bin(res, &(to[num - j]));
351 for (k = 0; k < (num - i); k++)
359 OPENSSL_clear_free(buf, num);
363 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
364 unsigned char *to, RSA *rsa, int padding)
367 int j, num = 0, r = -1;
369 unsigned char *buf = NULL;
371 int local_blinding = 0;
373 * Used only if the blinding structure is shared. A non-NULL unblind
374 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
375 * the unblinding factor outside the blinding structure.
377 BIGNUM *unblind = NULL;
378 BN_BLINDING *blinding = NULL;
380 if ((ctx = BN_CTX_new()) == NULL)
384 ret = BN_CTX_get(ctx);
385 num = BN_num_bytes(rsa->n);
386 buf = OPENSSL_malloc(num);
387 if (f == NULL || ret == NULL || buf == NULL) {
388 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
393 * This check was for equality but PGP does evil things and chops off the
397 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
398 RSA_R_DATA_GREATER_THAN_MOD_LEN);
402 /* make data into a big number */
403 if (BN_bin2bn(from, (int)flen, f) == NULL)
406 if (BN_ucmp(f, rsa->n) >= 0) {
407 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
408 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
412 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
413 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
414 if (blinding == NULL) {
415 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
420 if (blinding != NULL) {
421 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
422 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
425 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
430 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
433 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
434 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
437 BIGNUM *d = NULL, *local_d = NULL;
439 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
440 local_d = d = BN_new();
442 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
445 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
450 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
451 if (!BN_MONT_CTX_set_locked
452 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
456 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
457 rsa->_method_mod_n)) {
461 /* We MUST free local_d before any further use of rsa->d */
466 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
470 j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
473 case RSA_PKCS1_PADDING:
474 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
476 case RSA_PKCS1_OAEP_PADDING:
477 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
479 case RSA_SSLV23_PADDING:
480 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
483 r = RSA_padding_check_none(to, num, buf, j, num);
486 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
490 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
496 OPENSSL_clear_free(buf, num);
500 /* signature verification */
501 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
502 unsigned char *to, RSA *rsa, int padding)
505 int i, num = 0, r = -1;
507 unsigned char *buf = NULL;
510 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
511 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
515 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
516 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
520 /* for large moduli, enforce exponent limit */
521 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
522 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
523 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
528 if ((ctx = BN_CTX_new()) == NULL)
532 ret = BN_CTX_get(ctx);
533 num = BN_num_bytes(rsa->n);
534 buf = OPENSSL_malloc(num);
535 if (f == NULL || ret == NULL || buf == NULL) {
536 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
541 * This check was for equality but PGP does evil things and chops off the
545 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
549 if (BN_bin2bn(from, flen, f) == NULL)
552 if (BN_ucmp(f, rsa->n) >= 0) {
553 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
554 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
558 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
559 if (!BN_MONT_CTX_set_locked
560 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
563 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
567 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
568 if (!BN_sub(ret, rsa->n, ret))
572 i = BN_bn2bin(ret, p);
575 case RSA_PKCS1_PADDING:
576 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
578 case RSA_X931_PADDING:
579 r = RSA_padding_check_X931(to, num, buf, i, num);
582 r = RSA_padding_check_none(to, num, buf, i, num);
585 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
589 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
595 OPENSSL_clear_free(buf, num);
599 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
601 BIGNUM *r1, *m1, *vrfy;
606 r1 = BN_CTX_get(ctx);
607 m1 = BN_CTX_get(ctx);
608 vrfy = BN_CTX_get(ctx);
611 BIGNUM *local_p = NULL, *local_q = NULL;
612 BIGNUM *p = NULL, *q = NULL;
615 * Make sure BN_mod_inverse in Montgomery initialization uses the
616 * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
618 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
619 local_p = p = BN_new();
622 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
624 local_q = q = BN_new();
629 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
635 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
636 if (!BN_MONT_CTX_set_locked
637 (&rsa->_method_mod_p, rsa->lock, p, ctx)
638 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
639 rsa->lock, q, ctx)) {
646 * We MUST free local_p and local_q before any further use of rsa->p and
653 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
654 if (!BN_MONT_CTX_set_locked
655 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
658 /* compute I mod q */
660 BIGNUM *local_c = NULL;
662 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
666 BN_with_flags(local_c, I, BN_FLG_CONSTTIME);
671 if (!BN_mod(r1, c, rsa->q, ctx)) {
677 BIGNUM *local_dmq1 = NULL, *dmq1;
678 /* compute r1^dmq1 mod q */
679 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
680 dmq1 = local_dmq1 = BN_new();
681 if (local_dmq1 == NULL) {
685 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
689 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
690 rsa->_method_mod_q)) {
695 /* We MUST free local_dmq1 before any further use of rsa->dmq1 */
699 /* compute I mod p */
700 if (!BN_mod(r1, c, rsa->p, ctx)) {
704 /* We MUST free local_c before any further use of I */
709 BIGNUM *local_dmp1 = NULL, *dmp1;
710 /* compute r1^dmp1 mod p */
711 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
712 dmp1 = local_dmp1 = BN_new();
713 if (local_dmp1 == NULL)
715 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
719 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
720 rsa->_method_mod_p)) {
724 /* We MUST free local_dmp1 before any further use of rsa->dmp1 */
728 if (!BN_sub(r0, r0, m1))
731 * This will help stop the size of r0 increasing, which does affect the
732 * multiply if it optimised for a power of 2 size
734 if (BN_is_negative(r0))
735 if (!BN_add(r0, r0, rsa->p))
738 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
742 BIGNUM *local_r1 = NULL, *pr1;
743 /* Turn BN_FLG_CONSTTIME flag on before division operation */
744 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
745 pr1 = local_r1 = BN_new();
746 if (local_r1 == NULL)
748 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
752 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
756 /* We MUST free local_r1 before any further use of r1 */
761 * If p < q it is occasionally possible for the correction of adding 'p'
762 * if r0 is negative above to leave the result still negative. This can
763 * break the private key operations: the following second correction
764 * should *always* correct this rare occurrence. This will *never* happen
765 * with OpenSSL generated keys because they ensure p > q [steve]
767 if (BN_is_negative(r0))
768 if (!BN_add(r0, r0, rsa->p))
770 if (!BN_mul(r1, r0, rsa->q, ctx))
772 if (!BN_add(r0, r1, m1))
775 if (rsa->e && rsa->n) {
776 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
780 * If 'I' was greater than (or equal to) rsa->n, the operation will
781 * be equivalent to using 'I mod n'. However, the result of the
782 * verify will *always* be less than 'n' so we don't check for
783 * absolute equality, just congruency.
785 if (!BN_sub(vrfy, vrfy, I))
787 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
789 if (BN_is_negative(vrfy))
790 if (!BN_add(vrfy, vrfy, rsa->n))
792 if (!BN_is_zero(vrfy)) {
794 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
795 * miscalculated CRT output, just do a raw (slower) mod_exp and
796 * return that instead.
799 BIGNUM *local_d = NULL;
802 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
803 local_d = d = BN_new();
806 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
810 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
811 rsa->_method_mod_n)) {
815 /* We MUST free local_d before any further use of rsa->d */
825 static int rsa_ossl_init(RSA *rsa)
827 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
831 static int rsa_ossl_finish(RSA *rsa)
833 BN_MONT_CTX_free(rsa->_method_mod_n);
834 BN_MONT_CTX_free(rsa->_method_mod_p);
835 BN_MONT_CTX_free(rsa->_method_mod_q);