2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include "internal/cryptlib.h"
17 #include "crypto/bn.h"
18 #include "rsa_local.h"
19 #include "internal/constant_time.h"
20 #include <openssl/evp.h>
21 #include <openssl/sha.h>
22 #include <openssl/hmac.h>
24 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
25 unsigned char *to, RSA *rsa, int padding);
26 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
27 unsigned char *to, RSA *rsa, int padding);
28 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
29 unsigned char *to, RSA *rsa, int padding);
30 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
31 unsigned char *to, RSA *rsa, int padding);
32 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
34 static int rsa_ossl_init(RSA *rsa);
35 static int rsa_ossl_finish(RSA *rsa);
37 static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
39 static RSA_METHOD rsa_pkcs1_ossl_meth = {
41 rsa_ossl_public_encrypt,
42 rsa_ossl_public_decrypt, /* signature verification */
43 rsa_ossl_private_encrypt, /* signing */
44 rsa_ossl_private_decrypt,
45 rsa_ossl_s390x_mod_exp,
49 RSA_FLAG_FIPS_METHOD, /* flags */
53 NULL, /* rsa_keygen */
54 NULL /* rsa_multi_prime_keygen */
57 static RSA_METHOD rsa_pkcs1_ossl_meth = {
59 rsa_ossl_public_encrypt,
60 rsa_ossl_public_decrypt, /* signature verification */
61 rsa_ossl_private_encrypt, /* signing */
62 rsa_ossl_private_decrypt,
64 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
68 RSA_FLAG_FIPS_METHOD, /* flags */
72 NULL, /* rsa_keygen */
73 NULL /* rsa_multi_prime_keygen */
77 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
79 void RSA_set_default_method(const RSA_METHOD *meth)
81 default_RSA_meth = meth;
84 const RSA_METHOD *RSA_get_default_method(void)
86 return default_RSA_meth;
89 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
91 return &rsa_pkcs1_ossl_meth;
94 const RSA_METHOD *RSA_null_method(void)
99 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
100 unsigned char *to, RSA *rsa, int padding)
103 int i, num = 0, r = -1;
104 unsigned char *buf = NULL;
107 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
108 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
112 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
113 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
117 /* for large moduli, enforce exponent limit */
118 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
119 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
120 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
125 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
129 ret = BN_CTX_get(ctx);
130 num = BN_num_bytes(rsa->n);
131 buf = OPENSSL_malloc(num);
132 if (ret == NULL || buf == NULL)
136 case RSA_PKCS1_PADDING:
137 i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
140 case RSA_PKCS1_OAEP_PADDING:
141 i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
146 i = RSA_padding_add_none(buf, num, from, flen);
149 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
155 if (BN_bin2bn(buf, num, f) == NULL)
158 if (BN_ucmp(f, rsa->n) >= 0) {
159 /* usually the padding functions would catch this */
160 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
164 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
165 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
169 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
174 * BN_bn2binpad puts in leading 0 bytes if the number is less than
175 * the length of the modulus.
177 r = BN_bn2binpad(ret, to, num);
181 OPENSSL_clear_free(buf, num);
185 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
189 if (!CRYPTO_THREAD_write_lock(rsa->lock))
192 if (rsa->blinding == NULL) {
193 rsa->blinding = RSA_setup_blinding(rsa, ctx);
200 if (BN_BLINDING_is_current_thread(ret)) {
201 /* rsa->blinding is ours! */
205 /* resort to rsa->mt_blinding instead */
208 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
209 * BN_BLINDING is shared, meaning that accesses require locks, and
210 * that the blinding factor must be stored outside the BN_BLINDING
214 if (rsa->mt_blinding == NULL) {
215 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
217 ret = rsa->mt_blinding;
221 CRYPTO_THREAD_unlock(rsa->lock);
225 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
228 if (unblind == NULL) {
230 * Local blinding: store the unblinding factor in BN_BLINDING.
232 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
235 * Shared blinding: store the unblinding factor outside BN_BLINDING.
239 if (!BN_BLINDING_lock(b))
242 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
243 BN_BLINDING_unlock(b);
249 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
253 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
254 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
255 * is shared between threads, unblind must be non-null:
256 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
257 * will only read the modulus from BN_BLINDING. In both cases it's safe
258 * to access the blinding without a lock.
260 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
264 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
265 unsigned char *to, RSA *rsa, int padding)
267 BIGNUM *f, *ret, *res;
268 int i, num = 0, r = -1;
269 unsigned char *buf = NULL;
271 int local_blinding = 0;
273 * Used only if the blinding structure is shared. A non-NULL unblind
274 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
275 * the unblinding factor outside the blinding structure.
277 BIGNUM *unblind = NULL;
278 BN_BLINDING *blinding = NULL;
280 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
284 ret = BN_CTX_get(ctx);
285 num = BN_num_bytes(rsa->n);
286 buf = OPENSSL_malloc(num);
287 if (ret == NULL || buf == NULL)
291 case RSA_PKCS1_PADDING:
292 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
294 case RSA_X931_PADDING:
295 i = RSA_padding_add_X931(buf, num, from, flen);
298 i = RSA_padding_add_none(buf, num, from, flen);
301 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
307 if (BN_bin2bn(buf, num, f) == NULL)
310 if (BN_ucmp(f, rsa->n) >= 0) {
311 /* usually the padding functions would catch this */
312 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
316 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
317 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
321 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
322 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
323 if (blinding == NULL) {
324 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
329 if (blinding != NULL) {
330 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
331 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
334 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
338 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
339 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
342 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
343 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
346 BIGNUM *d = BN_new();
348 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
351 if (rsa->d == NULL) {
352 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
356 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
358 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
359 rsa->_method_mod_n)) {
363 /* We MUST free d before any further use of rsa->d */
368 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
371 if (padding == RSA_X931_PADDING) {
372 if (!BN_sub(f, rsa->n, ret))
374 if (BN_cmp(ret, f) > 0)
383 * BN_bn2binpad puts in leading 0 bytes if the number is less than
384 * the length of the modulus.
386 r = BN_bn2binpad(res, to, num);
390 OPENSSL_clear_free(buf, num);
394 static int derive_kdk(int flen, const unsigned char *from, RSA *rsa,
395 unsigned char *buf, int num, unsigned char *kdk)
398 HMAC_CTX *hmac = NULL;
400 unsigned int md_len = SHA256_DIGEST_LENGTH;
401 unsigned char d_hash[SHA256_DIGEST_LENGTH] = {0};
403 * because we use d as a handle to rsa->d we need to keep it local and
404 * free before any further use of rsa->d
406 BIGNUM *d = BN_new();
409 ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
412 if (rsa->d == NULL) {
413 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
417 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
418 if (BN_bn2binpad(d, buf, num) < 0) {
419 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
426 * we use hardcoded hash so that migrating between versions that use
427 * different hash doesn't provide a Bleichenbacher oracle:
428 * if the attacker can see that different versions return different
429 * messages for the same ciphertext, they'll know that the message is
430 * syntethically generated, which means that the padding check failed
432 md = EVP_MD_fetch(rsa->libctx, "sha256", NULL);
434 ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED);
438 if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) {
439 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
443 hmac = HMAC_CTX_new();
445 ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
449 if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) {
450 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
455 memset(buf, 0, num - flen);
456 if (HMAC_Update(hmac, buf, num - flen) <= 0) {
457 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
461 if (HMAC_Update(hmac, from, flen) <= 0) {
462 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
466 md_len = SHA256_DIGEST_LENGTH;
467 if (HMAC_Final(hmac, kdk, &md_len) <= 0) {
468 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
479 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
480 unsigned char *to, RSA *rsa, int padding)
483 int j, num = 0, r = -1;
484 unsigned char *buf = NULL;
485 unsigned char kdk[SHA256_DIGEST_LENGTH] = {0};
487 int local_blinding = 0;
489 * Used only if the blinding structure is shared. A non-NULL unblind
490 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
491 * the unblinding factor outside the blinding structure.
493 BIGNUM *unblind = NULL;
494 BN_BLINDING *blinding = NULL;
497 * we need the value of the private exponent to perform implicit rejection
499 if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
500 padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
502 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
506 ret = BN_CTX_get(ctx);
508 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
511 num = BN_num_bytes(rsa->n);
512 buf = OPENSSL_malloc(num);
517 * This check was for equality but PGP does evil things and chops off the
521 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
526 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
530 /* make data into a big number */
531 if (BN_bin2bn(from, (int)flen, f) == NULL)
534 if (BN_ucmp(f, rsa->n) >= 0) {
535 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
539 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
540 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
541 if (blinding == NULL) {
542 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
547 if (blinding != NULL) {
548 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
549 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
552 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
557 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
558 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
561 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
562 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
565 BIGNUM *d = BN_new();
567 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
570 if (rsa->d == NULL) {
571 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
575 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
577 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
578 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
583 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
584 rsa->_method_mod_n)) {
588 /* We MUST free d before any further use of rsa->d */
593 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
597 * derive the Key Derivation Key from private exponent and public
600 if (padding == RSA_PKCS1_PADDING) {
601 if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
605 j = BN_bn2binpad(ret, buf, num);
610 case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
611 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
613 case RSA_PKCS1_PADDING:
614 r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
616 case RSA_PKCS1_OAEP_PADDING:
617 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
620 memcpy(to, buf, (r = j));
623 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
628 * This trick doesn't work in the FIPS provider because libcrypto manages
629 * the error stack. Instead we opt not to put an error on the stack at all
630 * in case of padding failure in the FIPS provider.
632 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
633 err_clear_last_constant_time(1 & ~constant_time_msb(r));
639 OPENSSL_clear_free(buf, num);
643 /* signature verification */
644 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
645 unsigned char *to, RSA *rsa, int padding)
648 int i, num = 0, r = -1;
649 unsigned char *buf = NULL;
652 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
653 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
657 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
658 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
662 /* for large moduli, enforce exponent limit */
663 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
664 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
665 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
670 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
674 ret = BN_CTX_get(ctx);
676 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
679 num = BN_num_bytes(rsa->n);
680 buf = OPENSSL_malloc(num);
685 * This check was for equality but PGP does evil things and chops off the
689 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
693 if (BN_bin2bn(from, flen, f) == NULL)
696 if (BN_ucmp(f, rsa->n) >= 0) {
697 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
701 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
702 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
706 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
710 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
711 if (!BN_sub(ret, rsa->n, ret))
714 i = BN_bn2binpad(ret, buf, num);
719 case RSA_PKCS1_PADDING:
720 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
722 case RSA_X931_PADDING:
723 r = RSA_padding_check_X931(to, num, buf, i, num);
726 memcpy(to, buf, (r = i));
729 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
733 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
738 OPENSSL_clear_free(buf, num);
742 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
744 BIGNUM *r1, *m1, *vrfy;
745 int ret = 0, smooth = 0;
747 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
748 int i, ex_primes = 0;
749 RSA_PRIME_INFO *pinfo;
754 r1 = BN_CTX_get(ctx);
756 r2 = BN_CTX_get(ctx);
758 m1 = BN_CTX_get(ctx);
759 vrfy = BN_CTX_get(ctx);
764 if (rsa->version == RSA_ASN1_VERSION_MULTI
765 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
766 || ex_primes > RSA_MAX_PRIME_NUM - 2))
770 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
771 BIGNUM *factor = BN_new();
777 * Make sure BN_mod_inverse in Montgomery initialization uses the
778 * BN_FLG_CONSTTIME flag
780 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
781 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
783 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
784 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
790 for (i = 0; i < ex_primes; i++) {
791 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
792 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
793 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
800 * We MUST free |factor| before any further use of the prime factors
804 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
808 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
811 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
812 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
818 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
819 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
820 * to limb width. So that at the very least if |I| is fully reduced,
821 * i.e. less than p*q, we can count on from-to round to perform
822 * below modulo operations on |I|. Unlike BN_mod it's constant time.
824 if (/* m1 = I moq q */
825 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
826 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
828 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
829 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
831 * Use parallel exponentiations optimization if possible,
832 * otherwise fallback to two sequential exponentiations:
836 || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
838 r1, r1, rsa->dmp1, rsa->p,
841 /* r1 = (r1 - m1) mod p */
843 * bn_mod_sub_fixed_top is not regular modular subtraction,
844 * it can tolerate subtrahend to be larger than modulus, but
845 * not bit-wise wider. This makes up for uncommon q>p case,
846 * when |m1| can be larger than |rsa->p|.
848 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
850 /* r1 = r1 * iqmp mod p */
851 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
852 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
854 /* r0 = r1 * q + m1 */
855 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
856 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
862 /* compute I mod q */
864 BIGNUM *c = BN_new();
867 BN_with_flags(c, I, BN_FLG_CONSTTIME);
869 if (!BN_mod(r1, c, rsa->q, ctx)) {
875 BIGNUM *dmq1 = BN_new();
880 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
882 /* compute r1^dmq1 mod q */
883 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
884 rsa->_method_mod_q)) {
889 /* We MUST free dmq1 before any further use of rsa->dmq1 */
893 /* compute I mod p */
894 if (!BN_mod(r1, c, rsa->p, ctx)) {
898 /* We MUST free c before any further use of I */
903 BIGNUM *dmp1 = BN_new();
906 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
908 /* compute r1^dmp1 mod p */
909 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
910 rsa->_method_mod_p)) {
914 /* We MUST free dmp1 before any further use of rsa->dmp1 */
920 BIGNUM *di = BN_new(), *cc = BN_new();
922 if (cc == NULL || di == NULL) {
928 for (i = 0; i < ex_primes; i++) {
930 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
936 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
938 /* prepare c and d_i */
939 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
940 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
942 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
947 /* compute r1 ^ d_i mod r_i */
948 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
960 if (!BN_sub(r0, r0, m1))
963 * This will help stop the size of r0 increasing, which does affect the
964 * multiply if it optimised for a power of 2 size
966 if (BN_is_negative(r0))
967 if (!BN_add(r0, r0, rsa->p))
970 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
974 BIGNUM *pr1 = BN_new();
977 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
979 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
983 /* We MUST free pr1 before any further use of r1 */
988 * If p < q it is occasionally possible for the correction of adding 'p'
989 * if r0 is negative above to leave the result still negative. This can
990 * break the private key operations: the following second correction
991 * should *always* correct this rare occurrence. This will *never* happen
992 * with OpenSSL generated keys because they ensure p > q [steve]
994 if (BN_is_negative(r0))
995 if (!BN_add(r0, r0, rsa->p))
997 if (!BN_mul(r1, r0, rsa->q, ctx))
999 if (!BN_add(r0, r1, m1))
1003 /* add m_i to m in multi-prime case */
1004 if (ex_primes > 0) {
1005 BIGNUM *pr2 = BN_new();
1010 for (i = 0; i < ex_primes; i++) {
1011 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1012 if (!BN_sub(r1, m[i], r0)) {
1017 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1022 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1024 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1029 if (BN_is_negative(r1))
1030 if (!BN_add(r1, r1, pinfo->r)) {
1034 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1038 if (!BN_add(r0, r0, r1)) {
1048 if (rsa->e && rsa->n) {
1049 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1050 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1051 rsa->_method_mod_n))
1055 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1056 rsa->_method_mod_n))
1060 * If 'I' was greater than (or equal to) rsa->n, the operation will
1061 * be equivalent to using 'I mod n'. However, the result of the
1062 * verify will *always* be less than 'n' so we don't check for
1063 * absolute equality, just congruency.
1065 if (!BN_sub(vrfy, vrfy, I))
1067 if (BN_is_zero(vrfy)) {
1070 goto err; /* not actually error */
1072 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1074 if (BN_is_negative(vrfy))
1075 if (!BN_add(vrfy, vrfy, rsa->n))
1077 if (!BN_is_zero(vrfy)) {
1079 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1080 * miscalculated CRT output, just do a raw (slower) mod_exp and
1081 * return that instead.
1084 BIGNUM *d = BN_new();
1087 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1089 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1090 rsa->_method_mod_n)) {
1094 /* We MUST free d before any further use of rsa->d */
1099 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1100 * saves the day is that correction is highly unlike, and private key
1101 * operations are customarily performed on blinded message. Which means
1102 * that attacker won't observe correlation with chosen plaintext.
1103 * Secondly, remaining code would still handle it in same computational
1104 * time and even conceal memory access pattern around corrected top.
1113 static int rsa_ossl_init(RSA *rsa)
1115 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1119 static int rsa_ossl_finish(RSA *rsa)
1123 RSA_PRIME_INFO *pinfo;
1125 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1126 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1127 BN_MONT_CTX_free(pinfo->m);
1131 BN_MONT_CTX_free(rsa->_method_mod_n);
1132 BN_MONT_CTX_free(rsa->_method_mod_p);
1133 BN_MONT_CTX_free(rsa->_method_mod_q);
1137 #ifdef S390X_MOD_EXP
1138 static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1141 if (rsa->version != RSA_ASN1_VERSION_MULTI) {
1142 if (s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp) == 1)
1145 return rsa_ossl_mod_exp(r0, i, rsa, ctx);