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 "internal/bn_int.h"
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include "internal/evp_int.h"
22 return RSA_new_method(NULL);
25 const RSA_METHOD *RSA_get_method(const RSA *rsa)
30 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
33 * NB: The caller is specifically setting a method, so it's not up to us
34 * to deal with which ENGINE it comes from.
36 const RSA_METHOD *mtmp;
40 #ifndef OPENSSL_NO_ENGINE
41 ENGINE_finish(rsa->engine);
50 RSA *RSA_new_method(ENGINE *engine)
52 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
55 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
60 ret->lock = CRYPTO_THREAD_lock_new();
61 if (ret->lock == NULL) {
62 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
67 ret->meth = RSA_get_default_method();
68 #ifndef OPENSSL_NO_ENGINE
69 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
71 if (!ENGINE_init(engine)) {
72 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
77 ret->engine = ENGINE_get_default_RSA();
80 ret->meth = ENGINE_get_RSA(ret->engine);
81 if (ret->meth == NULL) {
82 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
88 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
89 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
93 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
94 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
105 void RSA_free(RSA *r)
112 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
113 REF_PRINT_COUNT("RSA", r);
116 REF_ASSERT_ISNT(i < 0);
118 if (r->meth != NULL && r->meth->finish != NULL)
120 #ifndef OPENSSL_NO_ENGINE
121 ENGINE_finish(r->engine);
124 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
126 CRYPTO_THREAD_lock_free(r->lock);
133 BN_clear_free(r->dmp1);
134 BN_clear_free(r->dmq1);
135 BN_clear_free(r->iqmp);
136 RSA_PSS_PARAMS_free(r->pss);
137 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
138 BN_BLINDING_free(r->blinding);
139 BN_BLINDING_free(r->mt_blinding);
140 OPENSSL_free(r->bignum_data);
144 int RSA_up_ref(RSA *r)
148 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
151 REF_PRINT_COUNT("RSA", r);
152 REF_ASSERT_ISNT(i < 2);
153 return i > 1 ? 1 : 0;
156 int RSA_set_ex_data(RSA *r, int idx, void *arg)
158 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
161 void *RSA_get_ex_data(const RSA *r, int idx)
163 return CRYPTO_get_ex_data(&r->ex_data, idx);
167 * Define a scaling constant for our fixed point arithmetic.
168 * This value must be a power of two because the base two logarithm code
169 * makes this assumption. The exponent must also be a multiple of three so
170 * that the scale factor has an exact cube root. Finally, the scale factor
171 * should not be so large that a multiplication of two scaled numbers
172 * overflows a 64 bit unsigned integer.
174 static const unsigned int scale = 1 << 18;
175 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
177 /* Define some constants, none exceed 32 bits */
178 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
179 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
180 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
181 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
184 * Multiply two scaled integers together and rescale the result.
186 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
188 return a * b / scale;
192 * Calculate the cube root of a 64 bit scaled integer.
193 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
194 * integer, this is not guaranteed after scaling, so this function has a
195 * 64 bit return. This uses the shifting nth root algorithm with some
196 * algebraic simplifications.
198 static uint64_t icbrt64(uint64_t x)
204 for (s = 63; s >= 0; s -= 3) {
206 b = 3 * r * (r + 1) + 1;
212 return r * cbrt_scale;
216 * Calculate the natural logarithm of a 64 bit scaled integer.
217 * This is done by calculating a base two logarithm and scaling.
218 * The maximum logarithm (base 2) is 64 and this reduces base e, so
219 * a 32 bit result should not overflow. The argument passed must be
220 * greater than unity so we don't need to handle negative results.
222 static uint32_t ilog_e(uint64_t v)
227 * Scale down the value into the range 1 .. 2.
229 * If fractional numbers need to be processed, another loop needs
230 * to go here that checks v < scale and if so multiplies it by 2 and
231 * reduces r by scale. This also means making r signed.
233 while (v >= 2 * scale) {
237 for (i = scale / 2; i != 0; i /= 2) {
239 if (v >= 2 * scale) {
244 r = (r * (uint64_t)scale) / log_e;
249 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
252 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
253 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
254 * The two cube roots are merged together here.
256 uint16_t rsa_compute_security_bits(int n)
262 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
276 * The first incorrect result (i.e. not accurate or off by one low) occurs
277 * for n = 699668. The true value here is 1200. Instead of using this n
278 * as the check threshold, the smallest n such that the correct result is
279 * 1200 is used instead.
286 x = n * (uint64_t)log_2;
288 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
293 int RSA_security_bits(const RSA *rsa)
295 int bits = BN_num_bits(rsa->n);
297 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
298 /* This ought to mean that we have private key at hand. */
299 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
301 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
304 return rsa_compute_security_bits(bits);
307 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
309 /* If the fields n and e in r are NULL, the corresponding input
310 * parameters MUST be non-NULL for n and e. d may be
311 * left NULL (in case only the public key is used).
313 if ((r->n == NULL && n == NULL)
314 || (r->e == NULL && e == NULL))
333 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
335 /* If the fields p and q in r are NULL, the corresponding input
336 * parameters MUST be non-NULL.
338 if ((r->p == NULL && p == NULL)
339 || (r->q == NULL && q == NULL))
354 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
356 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
357 * parameters MUST be non-NULL.
359 if ((r->dmp1 == NULL && dmp1 == NULL)
360 || (r->dmq1 == NULL && dmq1 == NULL)
361 || (r->iqmp == NULL && iqmp == NULL))
365 BN_clear_free(r->dmp1);
369 BN_clear_free(r->dmq1);
373 BN_clear_free(r->iqmp);
381 * Is it better to export RSA_PRIME_INFO structure
382 * and related functions to let user pass a triplet?
384 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
385 BIGNUM *coeffs[], int pnum)
387 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
388 RSA_PRIME_INFO *pinfo;
391 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
394 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
395 if (prime_infos == NULL)
398 if (r->prime_infos != NULL)
399 old = r->prime_infos;
401 for (i = 0; i < pnum; i++) {
402 pinfo = rsa_multip_info_new();
405 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
409 pinfo->r = primes[i];
411 pinfo->t = coeffs[i];
413 rsa_multip_info_free(pinfo);
416 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
419 r->prime_infos = prime_infos;
421 if (!rsa_multip_calc_product(r)) {
422 r->prime_infos = old;
428 * This is hard to deal with, since the old infos could
429 * also be set by this function and r, d, t should not
430 * be freed in that case. So currently, stay consistent
431 * with other *set0* functions: just free it...
433 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
436 r->version = RSA_ASN1_VERSION_MULTI;
440 /* r, d, t should not be freed */
441 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
445 void RSA_get0_key(const RSA *r,
446 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
456 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
464 int RSA_get_multi_prime_extra_count(const RSA *r)
468 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
474 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
477 RSA_PRIME_INFO *pinfo;
479 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
483 * return other primes
484 * it's caller's responsibility to allocate oth_primes[pnum]
486 for (i = 0; i < pnum; i++) {
487 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
488 primes[i] = pinfo->r;
494 void RSA_get0_crt_params(const RSA *r,
495 const BIGNUM **dmp1, const BIGNUM **dmq1,
506 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
507 const BIGNUM *coeffs[])
511 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
514 /* return other primes */
515 if (exps != NULL || coeffs != NULL) {
516 RSA_PRIME_INFO *pinfo;
519 /* it's the user's job to guarantee the buffer length */
520 for (i = 0; i < pnum; i++) {
521 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
525 coeffs[i] = pinfo->t;
532 const BIGNUM *RSA_get0_n(const RSA *r)
537 const BIGNUM *RSA_get0_e(const RSA *r)
542 const BIGNUM *RSA_get0_d(const RSA *r)
547 const BIGNUM *RSA_get0_p(const RSA *r)
552 const BIGNUM *RSA_get0_q(const RSA *r)
557 const BIGNUM *RSA_get0_dmp1(const RSA *r)
562 const BIGNUM *RSA_get0_dmq1(const RSA *r)
567 const BIGNUM *RSA_get0_iqmp(const RSA *r)
572 void RSA_clear_flags(RSA *r, int flags)
577 int RSA_test_flags(const RSA *r, int flags)
579 return r->flags & flags;
582 void RSA_set_flags(RSA *r, int flags)
587 int RSA_get_version(RSA *r)
589 /* { two-prime(0), multi(1) } */
593 ENGINE *RSA_get0_engine(const RSA *r)
598 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
600 /* If key type not RSA or RSA-PSS return error */
601 if (ctx != NULL && ctx->pmeth != NULL
602 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
603 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
605 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);