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"
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 (f == NULL || 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,
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 (f == NULL || 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)
354 * put in leading 0 bytes if the number is less than the length of the
357 j = BN_num_bytes(res);
358 i = BN_bn2bin(res, &(to[num - j]));
359 for (k = 0; k < (num - i); k++)
367 OPENSSL_clear_free(buf, num);
371 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
372 unsigned char *to, RSA *rsa, int padding)
375 int j, num = 0, r = -1;
377 unsigned char *buf = NULL;
379 int local_blinding = 0;
381 * Used only if the blinding structure is shared. A non-NULL unblind
382 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
383 * the unblinding factor outside the blinding structure.
385 BIGNUM *unblind = NULL;
386 BN_BLINDING *blinding = NULL;
388 if ((ctx = BN_CTX_new()) == NULL)
392 ret = BN_CTX_get(ctx);
393 num = BN_num_bytes(rsa->n);
394 buf = OPENSSL_malloc(num);
395 if (f == NULL || ret == NULL || buf == NULL) {
396 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
401 * This check was for equality but PGP does evil things and chops off the
405 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
406 RSA_R_DATA_GREATER_THAN_MOD_LEN);
410 /* make data into a big number */
411 if (BN_bin2bn(from, (int)flen, f) == NULL)
414 if (BN_ucmp(f, rsa->n) >= 0) {
415 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
416 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
420 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
421 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
422 if (blinding == NULL) {
423 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
428 if (blinding != NULL) {
429 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
430 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
433 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
438 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
441 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
442 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
445 BIGNUM *d = BN_new();
447 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
450 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
452 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
453 if (!BN_MONT_CTX_set_locked
454 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
458 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
459 rsa->_method_mod_n)) {
463 /* We MUST free d before any further use of rsa->d */
468 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
472 j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
475 case RSA_PKCS1_PADDING:
476 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
478 case RSA_PKCS1_OAEP_PADDING:
479 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
481 case RSA_SSLV23_PADDING:
482 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
485 r = RSA_padding_check_none(to, num, buf, j, num);
488 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
492 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
498 OPENSSL_clear_free(buf, num);
502 /* signature verification */
503 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
504 unsigned char *to, RSA *rsa, int padding)
507 int i, num = 0, r = -1;
509 unsigned char *buf = NULL;
512 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
513 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
517 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
518 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
522 /* for large moduli, enforce exponent limit */
523 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
524 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
525 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
530 if ((ctx = BN_CTX_new()) == NULL)
534 ret = BN_CTX_get(ctx);
535 num = BN_num_bytes(rsa->n);
536 buf = OPENSSL_malloc(num);
537 if (f == NULL || ret == NULL || buf == NULL) {
538 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
543 * This check was for equality but PGP does evil things and chops off the
547 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
551 if (BN_bin2bn(from, flen, f) == NULL)
554 if (BN_ucmp(f, rsa->n) >= 0) {
555 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
556 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
560 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
561 if (!BN_MONT_CTX_set_locked
562 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
565 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
569 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
570 if (!BN_sub(ret, rsa->n, ret))
574 i = BN_bn2bin(ret, p);
577 case RSA_PKCS1_PADDING:
578 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
580 case RSA_X931_PADDING:
581 r = RSA_padding_check_X931(to, num, buf, i, num);
584 r = RSA_padding_check_none(to, num, buf, i, num);
587 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
591 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
597 OPENSSL_clear_free(buf, num);
601 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
603 BIGNUM *r1, *m1, *vrfy;
608 r1 = BN_CTX_get(ctx);
609 m1 = BN_CTX_get(ctx);
610 vrfy = BN_CTX_get(ctx);
613 BIGNUM *p = BN_new(), *q = BN_new();
616 * Make sure BN_mod_inverse in Montgomery initialization uses the
617 * BN_FLG_CONSTTIME flag
619 if (p == NULL || q == NULL) {
624 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
625 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
627 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
628 if (!BN_MONT_CTX_set_locked
629 (&rsa->_method_mod_p, rsa->lock, p, ctx)
630 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
631 rsa->lock, q, ctx)) {
638 * We MUST free p and q before any further use of rsa->p and rsa->q
644 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
645 if (!BN_MONT_CTX_set_locked
646 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
649 /* compute I mod q */
651 BIGNUM *c = BN_new();
654 BN_with_flags(c, I, BN_FLG_CONSTTIME);
656 if (!BN_mod(r1, c, rsa->q, ctx)) {
662 BIGNUM *dmq1 = BN_new();
667 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
669 /* compute r1^dmq1 mod q */
670 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
671 rsa->_method_mod_q)) {
676 /* We MUST free dmq1 before any further use of rsa->dmq1 */
680 /* compute I mod p */
681 if (!BN_mod(r1, c, rsa->p, ctx)) {
685 /* We MUST free c before any further use of I */
690 BIGNUM *dmp1 = BN_new();
693 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
695 /* compute r1^dmp1 mod p */
696 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
697 rsa->_method_mod_p)) {
701 /* We MUST free dmp1 before any further use of rsa->dmp1 */
705 if (!BN_sub(r0, r0, m1))
708 * This will help stop the size of r0 increasing, which does affect the
709 * multiply if it optimised for a power of 2 size
711 if (BN_is_negative(r0))
712 if (!BN_add(r0, r0, rsa->p))
715 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
719 BIGNUM *pr1 = BN_new();
722 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
724 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
728 /* We MUST free pr1 before any further use of r1 */
733 * If p < q it is occasionally possible for the correction of adding 'p'
734 * if r0 is negative above to leave the result still negative. This can
735 * break the private key operations: the following second correction
736 * should *always* correct this rare occurrence. This will *never* happen
737 * with OpenSSL generated keys because they ensure p > q [steve]
739 if (BN_is_negative(r0))
740 if (!BN_add(r0, r0, rsa->p))
742 if (!BN_mul(r1, r0, rsa->q, ctx))
744 if (!BN_add(r0, r1, m1))
747 if (rsa->e && rsa->n) {
748 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
752 * If 'I' was greater than (or equal to) rsa->n, the operation will
753 * be equivalent to using 'I mod n'. However, the result of the
754 * verify will *always* be less than 'n' so we don't check for
755 * absolute equality, just congruency.
757 if (!BN_sub(vrfy, vrfy, I))
759 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
761 if (BN_is_negative(vrfy))
762 if (!BN_add(vrfy, vrfy, rsa->n))
764 if (!BN_is_zero(vrfy)) {
766 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
767 * miscalculated CRT output, just do a raw (slower) mod_exp and
768 * return that instead.
771 BIGNUM *d = BN_new();
774 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
776 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
777 rsa->_method_mod_n)) {
781 /* We MUST free d before any further use of rsa->d */
791 static int rsa_ossl_init(RSA *rsa)
793 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
797 static int rsa_ossl_finish(RSA *rsa)
799 BN_MONT_CTX_free(rsa->_method_mod_n);
800 BN_MONT_CTX_free(rsa->_method_mod_p);
801 BN_MONT_CTX_free(rsa->_method_mod_q);