2 * Copyright 1995-2018 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 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include "internal/refcount.h"
14 #include "crypto/bn.h"
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include "crypto/evp.h"
18 #include "crypto/rsa.h"
19 #include "rsa_local.h"
23 return RSA_new_method(NULL);
26 const RSA_METHOD *RSA_get_method(const RSA *rsa)
31 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
34 * NB: The caller is specifically setting a method, so it's not up to us
35 * to deal with which ENGINE it comes from.
37 const RSA_METHOD *mtmp;
41 #ifndef OPENSSL_NO_ENGINE
42 ENGINE_finish(rsa->engine);
51 RSA *RSA_new_method(ENGINE *engine)
53 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
56 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
61 ret->lock = CRYPTO_THREAD_lock_new();
62 if (ret->lock == NULL) {
63 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
68 ret->meth = RSA_get_default_method();
69 #ifndef OPENSSL_NO_ENGINE
70 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
72 if (!ENGINE_init(engine)) {
73 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
78 ret->engine = ENGINE_get_default_RSA();
81 ret->meth = ENGINE_get_RSA(ret->engine);
82 if (ret->meth == NULL) {
83 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
89 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
90 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
94 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
95 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
106 void RSA_free(RSA *r)
113 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
114 REF_PRINT_COUNT("RSA", r);
117 REF_ASSERT_ISNT(i < 0);
119 if (r->meth != NULL && r->meth->finish != NULL)
121 #ifndef OPENSSL_NO_ENGINE
122 ENGINE_finish(r->engine);
125 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
127 CRYPTO_THREAD_lock_free(r->lock);
134 BN_clear_free(r->dmp1);
135 BN_clear_free(r->dmq1);
136 BN_clear_free(r->iqmp);
137 RSA_PSS_PARAMS_free(r->pss);
138 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
139 BN_BLINDING_free(r->blinding);
140 BN_BLINDING_free(r->mt_blinding);
141 OPENSSL_free(r->bignum_data);
145 int RSA_up_ref(RSA *r)
149 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
152 REF_PRINT_COUNT("RSA", r);
153 REF_ASSERT_ISNT(i < 2);
154 return i > 1 ? 1 : 0;
157 int RSA_set_ex_data(RSA *r, int idx, void *arg)
159 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
162 void *RSA_get_ex_data(const RSA *r, int idx)
164 return CRYPTO_get_ex_data(&r->ex_data, idx);
168 * Define a scaling constant for our fixed point arithmetic.
169 * This value must be a power of two because the base two logarithm code
170 * makes this assumption. The exponent must also be a multiple of three so
171 * that the scale factor has an exact cube root. Finally, the scale factor
172 * should not be so large that a multiplication of two scaled numbers
173 * overflows a 64 bit unsigned integer.
175 static const unsigned int scale = 1 << 18;
176 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
178 /* Define some constants, none exceed 32 bits */
179 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
180 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
181 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
182 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
185 * Multiply two scaled integers together and rescale the result.
187 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
189 return a * b / scale;
193 * Calculate the cube root of a 64 bit scaled integer.
194 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
195 * integer, this is not guaranteed after scaling, so this function has a
196 * 64 bit return. This uses the shifting nth root algorithm with some
197 * algebraic simplifications.
199 static uint64_t icbrt64(uint64_t x)
205 for (s = 63; s >= 0; s -= 3) {
207 b = 3 * r * (r + 1) + 1;
213 return r * cbrt_scale;
217 * Calculate the natural logarithm of a 64 bit scaled integer.
218 * This is done by calculating a base two logarithm and scaling.
219 * The maximum logarithm (base 2) is 64 and this reduces base e, so
220 * a 32 bit result should not overflow. The argument passed must be
221 * greater than unity so we don't need to handle negative results.
223 static uint32_t ilog_e(uint64_t v)
228 * Scale down the value into the range 1 .. 2.
230 * If fractional numbers need to be processed, another loop needs
231 * to go here that checks v < scale and if so multiplies it by 2 and
232 * reduces r by scale. This also means making r signed.
234 while (v >= 2 * scale) {
238 for (i = scale / 2; i != 0; i /= 2) {
240 if (v >= 2 * scale) {
245 r = (r * (uint64_t)scale) / log_e;
250 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
253 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
254 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
255 * The two cube roots are merged together here.
257 uint16_t rsa_compute_security_bits(int n)
263 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
277 * The first incorrect result (i.e. not accurate or off by one low) occurs
278 * for n = 699668. The true value here is 1200. Instead of using this n
279 * as the check threshold, the smallest n such that the correct result is
280 * 1200 is used instead.
287 x = n * (uint64_t)log_2;
289 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
294 int RSA_security_bits(const RSA *rsa)
296 int bits = BN_num_bits(rsa->n);
298 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
299 /* This ought to mean that we have private key at hand. */
300 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
302 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
305 return rsa_compute_security_bits(bits);
308 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
310 /* If the fields n and e in r are NULL, the corresponding input
311 * parameters MUST be non-NULL for n and e. d may be
312 * left NULL (in case only the public key is used).
314 if ((r->n == NULL && n == NULL)
315 || (r->e == NULL && e == NULL))
329 BN_set_flags(r->d, BN_FLG_CONSTTIME);
336 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
338 /* If the fields p and q in r are NULL, the corresponding input
339 * parameters MUST be non-NULL.
341 if ((r->p == NULL && p == NULL)
342 || (r->q == NULL && q == NULL))
348 BN_set_flags(r->p, BN_FLG_CONSTTIME);
353 BN_set_flags(r->q, BN_FLG_CONSTTIME);
360 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
362 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
363 * parameters MUST be non-NULL.
365 if ((r->dmp1 == NULL && dmp1 == NULL)
366 || (r->dmq1 == NULL && dmq1 == NULL)
367 || (r->iqmp == NULL && iqmp == NULL))
371 BN_clear_free(r->dmp1);
373 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
376 BN_clear_free(r->dmq1);
378 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
381 BN_clear_free(r->iqmp);
383 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
391 * Is it better to export RSA_PRIME_INFO structure
392 * and related functions to let user pass a triplet?
394 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
395 BIGNUM *coeffs[], int pnum)
397 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
398 RSA_PRIME_INFO *pinfo;
401 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
404 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
405 if (prime_infos == NULL)
408 if (r->prime_infos != NULL)
409 old = r->prime_infos;
411 for (i = 0; i < pnum; i++) {
412 pinfo = rsa_multip_info_new();
415 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
416 BN_clear_free(pinfo->r);
417 BN_clear_free(pinfo->d);
418 BN_clear_free(pinfo->t);
419 pinfo->r = primes[i];
421 pinfo->t = coeffs[i];
422 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
423 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
424 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
426 rsa_multip_info_free(pinfo);
429 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
432 r->prime_infos = prime_infos;
434 if (!rsa_multip_calc_product(r)) {
435 r->prime_infos = old;
441 * This is hard to deal with, since the old infos could
442 * also be set by this function and r, d, t should not
443 * be freed in that case. So currently, stay consistent
444 * with other *set0* functions: just free it...
446 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
449 r->version = RSA_ASN1_VERSION_MULTI;
454 /* r, d, t should not be freed */
455 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
459 void RSA_get0_key(const RSA *r,
460 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
470 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
478 int RSA_get_multi_prime_extra_count(const RSA *r)
482 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
488 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
491 RSA_PRIME_INFO *pinfo;
493 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
497 * return other primes
498 * it's caller's responsibility to allocate oth_primes[pnum]
500 for (i = 0; i < pnum; i++) {
501 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
502 primes[i] = pinfo->r;
508 void RSA_get0_crt_params(const RSA *r,
509 const BIGNUM **dmp1, const BIGNUM **dmq1,
520 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
521 const BIGNUM *coeffs[])
525 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
528 /* return other primes */
529 if (exps != NULL || coeffs != NULL) {
530 RSA_PRIME_INFO *pinfo;
533 /* it's the user's job to guarantee the buffer length */
534 for (i = 0; i < pnum; i++) {
535 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
539 coeffs[i] = pinfo->t;
546 const BIGNUM *RSA_get0_n(const RSA *r)
551 const BIGNUM *RSA_get0_e(const RSA *r)
556 const BIGNUM *RSA_get0_d(const RSA *r)
561 const BIGNUM *RSA_get0_p(const RSA *r)
566 const BIGNUM *RSA_get0_q(const RSA *r)
571 const BIGNUM *RSA_get0_dmp1(const RSA *r)
576 const BIGNUM *RSA_get0_dmq1(const RSA *r)
581 const BIGNUM *RSA_get0_iqmp(const RSA *r)
586 void RSA_clear_flags(RSA *r, int flags)
591 int RSA_test_flags(const RSA *r, int flags)
593 return r->flags & flags;
596 void RSA_set_flags(RSA *r, int flags)
601 int RSA_get_version(RSA *r)
603 /* { two-prime(0), multi(1) } */
607 ENGINE *RSA_get0_engine(const RSA *r)
612 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
614 /* If key type not RSA or RSA-PSS return error */
615 if (ctx != NULL && ctx->pmeth != NULL
616 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
617 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
619 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
622 DEFINE_STACK_OF(BIGNUM)
624 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
625 const STACK_OF(BIGNUM) *exps,
626 const STACK_OF(BIGNUM) *coeffs)
628 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
631 if (primes == NULL || exps == NULL || coeffs == NULL)
634 pnum = sk_BIGNUM_num(primes);
636 || pnum != sk_BIGNUM_num(exps)
637 || pnum != sk_BIGNUM_num(coeffs) + 1)
640 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
641 sk_BIGNUM_value(primes, 1))
642 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
643 sk_BIGNUM_value(exps, 1),
644 sk_BIGNUM_value(coeffs, 0)))
647 old_infos = r->prime_infos;
652 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
653 if (prime_infos == NULL)
656 for (i = 2; i < pnum; i++) {
657 BIGNUM *prime = sk_BIGNUM_value(primes, i);
658 BIGNUM *exp = sk_BIGNUM_value(exps, i);
659 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
660 RSA_PRIME_INFO *pinfo = NULL;
662 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
665 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
666 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
667 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
674 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
675 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
676 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
677 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
680 r->prime_infos = prime_infos;
682 if (!rsa_multip_calc_product(r)) {
683 r->prime_infos = old_infos;
688 if (old_infos != NULL) {
690 * This is hard to deal with, since the old infos could
691 * also be set by this function and r, d, t should not
692 * be freed in that case. So currently, stay consistent
693 * with other *set0* functions: just free it...
695 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
698 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
703 /* r, d, t should not be freed */
704 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
708 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
710 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
711 STACK_OF(BIGNUM_const) *exps,
712 STACK_OF(BIGNUM_const) *coeffs)
714 RSA_PRIME_INFO *pinfo;
720 pnum = RSA_get_multi_prime_extra_count(r);
722 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
723 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
724 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
725 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
726 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
727 for (i = 0; i < pnum; i++) {
728 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
729 sk_BIGNUM_const_push(primes, pinfo->r);
730 sk_BIGNUM_const_push(exps, pinfo->d);
731 sk_BIGNUM_const_push(coeffs, pinfo->t);