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 = BN_new();
305 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
308 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
310 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
311 if (!BN_MONT_CTX_set_locked
312 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
317 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
318 rsa->_method_mod_n)) {
322 /* We MUST free d before any further use of rsa->d */
327 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
330 if (padding == RSA_X931_PADDING) {
331 BN_sub(f, rsa->n, ret);
332 if (BN_cmp(ret, f) > 0)
340 * put in leading 0 bytes if the number is less than the length of the
343 j = BN_num_bytes(res);
344 i = BN_bn2bin(res, &(to[num - j]));
345 for (k = 0; k < (num - i); k++)
353 OPENSSL_clear_free(buf, num);
357 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
358 unsigned char *to, RSA *rsa, int padding)
361 int j, num = 0, r = -1;
363 unsigned char *buf = NULL;
365 int local_blinding = 0;
367 * Used only if the blinding structure is shared. A non-NULL unblind
368 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
369 * the unblinding factor outside the blinding structure.
371 BIGNUM *unblind = NULL;
372 BN_BLINDING *blinding = NULL;
374 if ((ctx = BN_CTX_new()) == NULL)
378 ret = BN_CTX_get(ctx);
379 num = BN_num_bytes(rsa->n);
380 buf = OPENSSL_malloc(num);
381 if (f == NULL || ret == NULL || buf == NULL) {
382 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
387 * This check was for equality but PGP does evil things and chops off the
391 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
392 RSA_R_DATA_GREATER_THAN_MOD_LEN);
396 /* make data into a big number */
397 if (BN_bin2bn(from, (int)flen, f) == NULL)
400 if (BN_ucmp(f, rsa->n) >= 0) {
401 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
402 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
406 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
407 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
408 if (blinding == NULL) {
409 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
414 if (blinding != NULL) {
415 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
416 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
419 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
424 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
427 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
428 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
431 BIGNUM *d = BN_new();
433 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
436 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
438 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
439 if (!BN_MONT_CTX_set_locked
440 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
444 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
445 rsa->_method_mod_n)) {
449 /* We MUST free d before any further use of rsa->d */
454 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
458 j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
461 case RSA_PKCS1_PADDING:
462 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
464 case RSA_PKCS1_OAEP_PADDING:
465 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
467 case RSA_SSLV23_PADDING:
468 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
471 r = RSA_padding_check_none(to, num, buf, j, num);
474 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
478 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
484 OPENSSL_clear_free(buf, num);
488 /* signature verification */
489 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
490 unsigned char *to, RSA *rsa, int padding)
493 int i, num = 0, r = -1;
495 unsigned char *buf = NULL;
498 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
499 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
503 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
504 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
508 /* for large moduli, enforce exponent limit */
509 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
510 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
511 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
516 if ((ctx = BN_CTX_new()) == NULL)
520 ret = BN_CTX_get(ctx);
521 num = BN_num_bytes(rsa->n);
522 buf = OPENSSL_malloc(num);
523 if (f == NULL || ret == NULL || buf == NULL) {
524 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
529 * This check was for equality but PGP does evil things and chops off the
533 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
537 if (BN_bin2bn(from, flen, f) == NULL)
540 if (BN_ucmp(f, rsa->n) >= 0) {
541 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
542 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
546 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
547 if (!BN_MONT_CTX_set_locked
548 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
551 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
555 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
556 if (!BN_sub(ret, rsa->n, ret))
560 i = BN_bn2bin(ret, p);
563 case RSA_PKCS1_PADDING:
564 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
566 case RSA_X931_PADDING:
567 r = RSA_padding_check_X931(to, num, buf, i, num);
570 r = RSA_padding_check_none(to, num, buf, i, num);
573 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
577 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
583 OPENSSL_clear_free(buf, num);
587 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
589 BIGNUM *r1, *m1, *vrfy;
594 r1 = BN_CTX_get(ctx);
595 m1 = BN_CTX_get(ctx);
596 vrfy = BN_CTX_get(ctx);
599 BIGNUM *p = BN_new(), *q = BN_new();
602 * Make sure BN_mod_inverse in Montgomery initialization uses the
603 * BN_FLG_CONSTTIME flag
605 if (p == NULL || q == NULL) {
610 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
611 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
613 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
614 if (!BN_MONT_CTX_set_locked
615 (&rsa->_method_mod_p, rsa->lock, p, ctx)
616 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
617 rsa->lock, q, ctx)) {
624 * We MUST free p and q before any further use of rsa->p and rsa->q
630 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
631 if (!BN_MONT_CTX_set_locked
632 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
635 /* compute I mod q */
637 BIGNUM *c = BN_new();
640 BN_with_flags(c, I, BN_FLG_CONSTTIME);
642 if (!BN_mod(r1, c, rsa->q, ctx)) {
648 BIGNUM *dmq1 = BN_new();
653 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
655 /* compute r1^dmq1 mod q */
656 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
657 rsa->_method_mod_q)) {
662 /* We MUST free dmq1 before any further use of rsa->dmq1 */
666 /* compute I mod p */
667 if (!BN_mod(r1, c, rsa->p, ctx)) {
671 /* We MUST free c before any further use of I */
676 BIGNUM *dmp1 = BN_new();
679 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
681 /* compute r1^dmp1 mod p */
682 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
683 rsa->_method_mod_p)) {
687 /* We MUST free dmp1 before any further use of rsa->dmp1 */
691 if (!BN_sub(r0, r0, m1))
694 * This will help stop the size of r0 increasing, which does affect the
695 * multiply if it optimised for a power of 2 size
697 if (BN_is_negative(r0))
698 if (!BN_add(r0, r0, rsa->p))
701 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
705 BIGNUM *pr1 = BN_new();
708 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
710 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
714 /* We MUST free pr1 before any further use of r1 */
719 * If p < q it is occasionally possible for the correction of adding 'p'
720 * if r0 is negative above to leave the result still negative. This can
721 * break the private key operations: the following second correction
722 * should *always* correct this rare occurrence. This will *never* happen
723 * with OpenSSL generated keys because they ensure p > q [steve]
725 if (BN_is_negative(r0))
726 if (!BN_add(r0, r0, rsa->p))
728 if (!BN_mul(r1, r0, rsa->q, ctx))
730 if (!BN_add(r0, r1, m1))
733 if (rsa->e && rsa->n) {
734 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
738 * If 'I' was greater than (or equal to) rsa->n, the operation will
739 * be equivalent to using 'I mod n'. However, the result of the
740 * verify will *always* be less than 'n' so we don't check for
741 * absolute equality, just congruency.
743 if (!BN_sub(vrfy, vrfy, I))
745 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
747 if (BN_is_negative(vrfy))
748 if (!BN_add(vrfy, vrfy, rsa->n))
750 if (!BN_is_zero(vrfy)) {
752 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
753 * miscalculated CRT output, just do a raw (slower) mod_exp and
754 * return that instead.
757 BIGNUM *d = BN_new();
760 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
762 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
763 rsa->_method_mod_n)) {
767 /* We MUST free d before any further use of rsa->d */
777 static int rsa_ossl_init(RSA *rsa)
779 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
783 static int rsa_ossl_finish(RSA *rsa)
785 BN_MONT_CTX_free(rsa->_method_mod_n);
786 BN_MONT_CTX_free(rsa->_method_mod_p);
787 BN_MONT_CTX_free(rsa->_method_mod_q);