2 * Copyright 1995-2017 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"
14 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
15 unsigned char *to, RSA *rsa, int padding);
16 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
17 unsigned char *to, RSA *rsa, int padding);
18 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
19 unsigned char *to, RSA *rsa, int padding);
20 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
21 unsigned char *to, RSA *rsa, int padding);
22 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
24 static int rsa_ossl_init(RSA *rsa);
25 static int rsa_ossl_finish(RSA *rsa);
26 static RSA_METHOD rsa_pkcs1_ossl_meth = {
28 rsa_ossl_public_encrypt,
29 rsa_ossl_public_decrypt, /* signature verification */
30 rsa_ossl_private_encrypt, /* signing */
31 rsa_ossl_private_decrypt,
33 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
37 RSA_FLAG_FIPS_METHOD, /* flags */
44 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
46 void RSA_set_default_method(const RSA_METHOD *meth)
48 default_RSA_meth = meth;
51 const RSA_METHOD *RSA_get_default_method(void)
53 return default_RSA_meth;
56 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
58 return &rsa_pkcs1_ossl_meth;
61 const RSA_METHOD *RSA_null_method(void)
66 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
67 unsigned char *to, RSA *rsa, int padding)
70 int i, j, k, num = 0, r = -1;
71 unsigned char *buf = NULL;
74 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
75 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
79 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
80 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
84 /* for large moduli, enforce exponent limit */
85 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
86 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
87 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
92 if ((ctx = BN_CTX_new()) == NULL)
96 ret = BN_CTX_get(ctx);
97 num = BN_num_bytes(rsa->n);
98 buf = OPENSSL_malloc(num);
99 if (ret == NULL || buf == NULL) {
100 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
105 case RSA_PKCS1_PADDING:
106 i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
108 case RSA_PKCS1_OAEP_PADDING:
109 i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
111 case RSA_SSLV23_PADDING:
112 i = RSA_padding_add_SSLv23(buf, num, from, flen);
115 i = RSA_padding_add_none(buf, num, from, flen);
118 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
124 if (BN_bin2bn(buf, num, f) == NULL)
127 if (BN_ucmp(f, rsa->n) >= 0) {
128 /* usually the padding functions would catch this */
129 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
130 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
134 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
135 if (!BN_MONT_CTX_set_locked
136 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
139 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
144 * put in leading 0 bytes if the number is less than the length of the
147 j = BN_num_bytes(ret);
148 i = BN_bn2bin(ret, &(to[num - j]));
149 for (k = 0; k < (num - i); k++)
157 OPENSSL_clear_free(buf, num);
161 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
165 CRYPTO_THREAD_write_lock(rsa->lock);
167 if (rsa->blinding == NULL) {
168 rsa->blinding = RSA_setup_blinding(rsa, ctx);
175 if (BN_BLINDING_is_current_thread(ret)) {
176 /* rsa->blinding is ours! */
180 /* resort to rsa->mt_blinding instead */
183 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
184 * BN_BLINDING is shared, meaning that accesses require locks, and
185 * that the blinding factor must be stored outside the BN_BLINDING
189 if (rsa->mt_blinding == NULL) {
190 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
192 ret = rsa->mt_blinding;
196 CRYPTO_THREAD_unlock(rsa->lock);
200 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
203 if (unblind == NULL) {
205 * Local blinding: store the unblinding factor in BN_BLINDING.
207 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
210 * Shared blinding: store the unblinding factor outside BN_BLINDING.
215 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
216 BN_BLINDING_unlock(b);
222 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
226 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
227 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
228 * is shared between threads, unblind must be non-null:
229 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
230 * will only read the modulus from BN_BLINDING. In both cases it's safe
231 * to access the blinding without a lock.
233 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
237 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
238 unsigned char *to, RSA *rsa, int padding)
240 BIGNUM *f, *ret, *res;
241 int i, j, k, num = 0, r = -1;
242 unsigned char *buf = NULL;
244 int local_blinding = 0;
246 * Used only if the blinding structure is shared. A non-NULL unblind
247 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
248 * the unblinding factor outside the blinding structure.
250 BIGNUM *unblind = NULL;
251 BN_BLINDING *blinding = NULL;
253 if ((ctx = BN_CTX_new()) == NULL)
257 ret = BN_CTX_get(ctx);
258 num = BN_num_bytes(rsa->n);
259 buf = OPENSSL_malloc(num);
260 if (ret == NULL || buf == NULL) {
261 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
266 case RSA_PKCS1_PADDING:
267 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
269 case RSA_X931_PADDING:
270 i = RSA_padding_add_X931(buf, num, from, flen);
273 i = RSA_padding_add_none(buf, num, from, flen);
275 case RSA_SSLV23_PADDING:
277 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
283 if (BN_bin2bn(buf, num, f) == NULL)
286 if (BN_ucmp(f, rsa->n) >= 0) {
287 /* usually the padding functions would catch this */
288 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
289 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
293 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
294 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
295 if (blinding == NULL) {
296 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
301 if (blinding != NULL) {
302 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
303 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
306 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
310 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
313 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
314 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
317 BIGNUM *d = BN_new();
319 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
322 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
324 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
325 if (!BN_MONT_CTX_set_locked
326 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
331 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
332 rsa->_method_mod_n)) {
336 /* We MUST free d before any further use of rsa->d */
341 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
344 if (padding == RSA_X931_PADDING) {
345 BN_sub(f, rsa->n, ret);
346 if (BN_cmp(ret, f) > 0)
355 * put in leading 0 bytes if the number is less than the length of the
358 j = BN_num_bytes(res);
359 i = BN_bn2bin(res, &(to[num - j]));
360 for (k = 0; k < (num - i); k++)
368 OPENSSL_clear_free(buf, num);
372 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
373 unsigned char *to, RSA *rsa, int padding)
376 int j, num = 0, r = -1;
378 unsigned char *buf = NULL;
380 int local_blinding = 0;
382 * Used only if the blinding structure is shared. A non-NULL unblind
383 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
384 * the unblinding factor outside the blinding structure.
386 BIGNUM *unblind = NULL;
387 BN_BLINDING *blinding = NULL;
389 if ((ctx = BN_CTX_new()) == NULL)
393 ret = BN_CTX_get(ctx);
394 num = BN_num_bytes(rsa->n);
395 buf = OPENSSL_malloc(num);
396 if (ret == NULL || buf == NULL) {
397 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
402 * This check was for equality but PGP does evil things and chops off the
406 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
407 RSA_R_DATA_GREATER_THAN_MOD_LEN);
411 /* make data into a big number */
412 if (BN_bin2bn(from, (int)flen, f) == NULL)
415 if (BN_ucmp(f, rsa->n) >= 0) {
416 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
417 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
421 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
422 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
423 if (blinding == NULL) {
424 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
429 if (blinding != NULL) {
430 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
431 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
434 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
439 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
442 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
443 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
446 BIGNUM *d = BN_new();
448 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
451 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
453 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
454 if (!BN_MONT_CTX_set_locked
455 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
459 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
460 rsa->_method_mod_n)) {
464 /* We MUST free d before any further use of rsa->d */
469 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
473 j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
476 case RSA_PKCS1_PADDING:
477 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
479 case RSA_PKCS1_OAEP_PADDING:
480 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
482 case RSA_SSLV23_PADDING:
483 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
486 r = RSA_padding_check_none(to, num, buf, j, num);
489 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
493 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
499 OPENSSL_clear_free(buf, num);
503 /* signature verification */
504 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
505 unsigned char *to, RSA *rsa, int padding)
508 int i, num = 0, r = -1;
510 unsigned char *buf = NULL;
513 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
514 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
518 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
519 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
523 /* for large moduli, enforce exponent limit */
524 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
525 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
526 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
531 if ((ctx = BN_CTX_new()) == NULL)
535 ret = BN_CTX_get(ctx);
536 num = BN_num_bytes(rsa->n);
537 buf = OPENSSL_malloc(num);
538 if (ret == NULL || buf == NULL) {
539 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
544 * This check was for equality but PGP does evil things and chops off the
548 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
552 if (BN_bin2bn(from, flen, f) == NULL)
555 if (BN_ucmp(f, rsa->n) >= 0) {
556 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
557 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
561 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
562 if (!BN_MONT_CTX_set_locked
563 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
566 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
570 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
571 if (!BN_sub(ret, rsa->n, ret))
575 i = BN_bn2bin(ret, p);
578 case RSA_PKCS1_PADDING:
579 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
581 case RSA_X931_PADDING:
582 r = RSA_padding_check_X931(to, num, buf, i, num);
585 r = RSA_padding_check_none(to, num, buf, i, num);
588 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
592 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
598 OPENSSL_clear_free(buf, num);
602 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
604 BIGNUM *r1, *m1, *vrfy;
609 r1 = BN_CTX_get(ctx);
610 m1 = BN_CTX_get(ctx);
611 vrfy = BN_CTX_get(ctx);
616 BIGNUM *p = BN_new(), *q = BN_new();
619 * Make sure BN_mod_inverse in Montgomery initialization uses the
620 * BN_FLG_CONSTTIME flag
622 if (p == NULL || q == NULL) {
627 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
628 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
630 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
631 if (!BN_MONT_CTX_set_locked
632 (&rsa->_method_mod_p, rsa->lock, p, ctx)
633 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
634 rsa->lock, q, ctx)) {
641 * We MUST free p and q before any further use of rsa->p and rsa->q
647 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
648 if (!BN_MONT_CTX_set_locked
649 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
652 /* compute I mod q */
654 BIGNUM *c = BN_new();
657 BN_with_flags(c, I, BN_FLG_CONSTTIME);
659 if (!BN_mod(r1, c, rsa->q, ctx)) {
665 BIGNUM *dmq1 = BN_new();
670 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
672 /* compute r1^dmq1 mod q */
673 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
674 rsa->_method_mod_q)) {
679 /* We MUST free dmq1 before any further use of rsa->dmq1 */
683 /* compute I mod p */
684 if (!BN_mod(r1, c, rsa->p, ctx)) {
688 /* We MUST free c before any further use of I */
693 BIGNUM *dmp1 = BN_new();
696 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
698 /* compute r1^dmp1 mod p */
699 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
700 rsa->_method_mod_p)) {
704 /* We MUST free dmp1 before any further use of rsa->dmp1 */
708 if (!BN_sub(r0, r0, m1))
711 * This will help stop the size of r0 increasing, which does affect the
712 * multiply if it optimised for a power of 2 size
714 if (BN_is_negative(r0))
715 if (!BN_add(r0, r0, rsa->p))
718 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
722 BIGNUM *pr1 = BN_new();
725 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
727 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
731 /* We MUST free pr1 before any further use of r1 */
736 * If p < q it is occasionally possible for the correction of adding 'p'
737 * if r0 is negative above to leave the result still negative. This can
738 * break the private key operations: the following second correction
739 * should *always* correct this rare occurrence. This will *never* happen
740 * with OpenSSL generated keys because they ensure p > q [steve]
742 if (BN_is_negative(r0))
743 if (!BN_add(r0, r0, rsa->p))
745 if (!BN_mul(r1, r0, rsa->q, ctx))
747 if (!BN_add(r0, r1, m1))
750 if (rsa->e && rsa->n) {
751 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
755 * If 'I' was greater than (or equal to) rsa->n, the operation will
756 * be equivalent to using 'I mod n'. However, the result of the
757 * verify will *always* be less than 'n' so we don't check for
758 * absolute equality, just congruency.
760 if (!BN_sub(vrfy, vrfy, I))
762 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
764 if (BN_is_negative(vrfy))
765 if (!BN_add(vrfy, vrfy, rsa->n))
767 if (!BN_is_zero(vrfy)) {
769 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
770 * miscalculated CRT output, just do a raw (slower) mod_exp and
771 * return that instead.
774 BIGNUM *d = BN_new();
777 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
779 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
780 rsa->_method_mod_n)) {
784 /* We MUST free d before any further use of rsa->d */
794 static int rsa_ossl_init(RSA *rsa)
796 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
800 static int rsa_ossl_finish(RSA *rsa)
802 BN_MONT_CTX_free(rsa->_method_mod_n);
803 BN_MONT_CTX_free(rsa->_method_mod_p);
804 BN_MONT_CTX_free(rsa->_method_mod_q);