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);
335 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
337 /* If the fields p and q in r are NULL, the corresponding input
338 * parameters MUST be non-NULL.
340 if ((r->p == NULL && p == NULL)
341 || (r->q == NULL && q == NULL))
347 BN_set_flags(r->p, BN_FLG_CONSTTIME);
352 BN_set_flags(r->q, BN_FLG_CONSTTIME);
358 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
360 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
361 * parameters MUST be non-NULL.
363 if ((r->dmp1 == NULL && dmp1 == NULL)
364 || (r->dmq1 == NULL && dmq1 == NULL)
365 || (r->iqmp == NULL && iqmp == NULL))
369 BN_clear_free(r->dmp1);
371 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
374 BN_clear_free(r->dmq1);
376 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
379 BN_clear_free(r->iqmp);
381 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
388 * Is it better to export RSA_PRIME_INFO structure
389 * and related functions to let user pass a triplet?
391 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
392 BIGNUM *coeffs[], int pnum)
394 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
395 RSA_PRIME_INFO *pinfo;
398 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
401 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
402 if (prime_infos == NULL)
405 if (r->prime_infos != NULL)
406 old = r->prime_infos;
408 for (i = 0; i < pnum; i++) {
409 pinfo = rsa_multip_info_new();
412 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
413 BN_clear_free(pinfo->r);
414 BN_clear_free(pinfo->d);
415 BN_clear_free(pinfo->t);
416 pinfo->r = primes[i];
418 pinfo->t = coeffs[i];
419 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
420 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
421 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
423 rsa_multip_info_free(pinfo);
426 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
429 r->prime_infos = prime_infos;
431 if (!rsa_multip_calc_product(r)) {
432 r->prime_infos = old;
438 * This is hard to deal with, since the old infos could
439 * also be set by this function and r, d, t should not
440 * be freed in that case. So currently, stay consistent
441 * with other *set0* functions: just free it...
443 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
446 r->version = RSA_ASN1_VERSION_MULTI;
450 /* r, d, t should not be freed */
451 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
455 void RSA_get0_key(const RSA *r,
456 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
466 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
474 int RSA_get_multi_prime_extra_count(const RSA *r)
478 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
484 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
487 RSA_PRIME_INFO *pinfo;
489 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
493 * return other primes
494 * it's caller's responsibility to allocate oth_primes[pnum]
496 for (i = 0; i < pnum; i++) {
497 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
498 primes[i] = pinfo->r;
504 void RSA_get0_crt_params(const RSA *r,
505 const BIGNUM **dmp1, const BIGNUM **dmq1,
516 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
517 const BIGNUM *coeffs[])
521 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
524 /* return other primes */
525 if (exps != NULL || coeffs != NULL) {
526 RSA_PRIME_INFO *pinfo;
529 /* it's the user's job to guarantee the buffer length */
530 for (i = 0; i < pnum; i++) {
531 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
535 coeffs[i] = pinfo->t;
542 const BIGNUM *RSA_get0_n(const RSA *r)
547 const BIGNUM *RSA_get0_e(const RSA *r)
552 const BIGNUM *RSA_get0_d(const RSA *r)
557 const BIGNUM *RSA_get0_p(const RSA *r)
562 const BIGNUM *RSA_get0_q(const RSA *r)
567 const BIGNUM *RSA_get0_dmp1(const RSA *r)
572 const BIGNUM *RSA_get0_dmq1(const RSA *r)
577 const BIGNUM *RSA_get0_iqmp(const RSA *r)
582 void RSA_clear_flags(RSA *r, int flags)
587 int RSA_test_flags(const RSA *r, int flags)
589 return r->flags & flags;
592 void RSA_set_flags(RSA *r, int flags)
597 int RSA_get_version(RSA *r)
599 /* { two-prime(0), multi(1) } */
603 ENGINE *RSA_get0_engine(const RSA *r)
608 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
610 /* If key type not RSA or RSA-PSS return error */
611 if (ctx != NULL && ctx->pmeth != NULL
612 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
613 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
615 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
618 DEFINE_STACK_OF(BIGNUM)
620 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
621 const STACK_OF(BIGNUM) *exps,
622 const STACK_OF(BIGNUM) *coeffs)
624 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
627 if (primes == NULL || exps == NULL || coeffs == NULL)
630 pnum = sk_BIGNUM_num(primes);
632 || pnum != sk_BIGNUM_num(exps)
633 || pnum != sk_BIGNUM_num(coeffs) + 1)
636 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
637 sk_BIGNUM_value(primes, 1))
638 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
639 sk_BIGNUM_value(exps, 1),
640 sk_BIGNUM_value(coeffs, 0)))
643 old_infos = r->prime_infos;
648 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
649 if (prime_infos == NULL)
652 for (i = 2; i < pnum; i++) {
653 BIGNUM *prime = sk_BIGNUM_value(primes, i);
654 BIGNUM *exp = sk_BIGNUM_value(exps, i);
655 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
656 RSA_PRIME_INFO *pinfo = NULL;
658 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
661 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
662 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
663 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
670 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
671 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
672 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
673 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
676 r->prime_infos = prime_infos;
678 if (!rsa_multip_calc_product(r)) {
679 r->prime_infos = old_infos;
684 if (old_infos != NULL) {
686 * This is hard to deal with, since the old infos could
687 * also be set by this function and r, d, t should not
688 * be freed in that case. So currently, stay consistent
689 * with other *set0* functions: just free it...
691 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
694 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
699 /* r, d, t should not be freed */
700 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
704 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
706 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
707 STACK_OF(BIGNUM_const) *exps,
708 STACK_OF(BIGNUM_const) *coeffs)
710 RSA_PRIME_INFO *pinfo;
716 pnum = RSA_get_multi_prime_extra_count(r);
718 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
719 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
720 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
721 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
722 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
723 for (i = 0; i < pnum; i++) {
724 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
725 sk_BIGNUM_const_push(primes, pinfo->r);
726 sk_BIGNUM_const_push(exps, pinfo->d);
727 sk_BIGNUM_const_push(coeffs, pinfo->t);