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
11 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/lhash.h>
14 #include "internal/bn_int.h"
15 #include <openssl/engine.h>
18 static const RSA_METHOD *default_RSA_meth = NULL;
22 RSA *r = RSA_new_method(NULL);
27 void RSA_set_default_method(const RSA_METHOD *meth)
29 default_RSA_meth = meth;
32 const RSA_METHOD *RSA_get_default_method(void)
34 if (default_RSA_meth == NULL) {
36 default_RSA_meth = RSA_null_method();
38 default_RSA_meth = RSA_PKCS1_OpenSSL();
42 return default_RSA_meth;
45 const RSA_METHOD *RSA_get_method(const RSA *rsa)
50 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
53 * NB: The caller is specifically setting a method, so it's not up to us
54 * to deal with which ENGINE it comes from.
56 const RSA_METHOD *mtmp;
60 #ifndef OPENSSL_NO_ENGINE
61 ENGINE_finish(rsa->engine);
70 RSA *RSA_new_method(ENGINE *engine)
72 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
75 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
80 ret->lock = CRYPTO_THREAD_lock_new();
81 if (ret->lock == NULL) {
82 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
87 ret->meth = RSA_get_default_method();
88 #ifndef OPENSSL_NO_ENGINE
89 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
91 if (!ENGINE_init(engine)) {
92 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
97 ret->engine = ENGINE_get_default_RSA();
99 ret->meth = ENGINE_get_RSA(ret->engine);
100 if (ret->meth == NULL) {
101 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
107 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
108 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
112 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
113 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
124 void RSA_free(RSA *r)
131 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
132 REF_PRINT_COUNT("RSA", r);
135 REF_ASSERT_ISNT(i < 0);
139 #ifndef OPENSSL_NO_ENGINE
140 ENGINE_finish(r->engine);
143 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
145 CRYPTO_THREAD_lock_free(r->lock);
152 BN_clear_free(r->dmp1);
153 BN_clear_free(r->dmq1);
154 BN_clear_free(r->iqmp);
155 BN_BLINDING_free(r->blinding);
156 BN_BLINDING_free(r->mt_blinding);
157 OPENSSL_free(r->bignum_data);
161 int RSA_up_ref(RSA *r)
165 if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
168 REF_PRINT_COUNT("RSA", r);
169 REF_ASSERT_ISNT(i < 2);
170 return ((i > 1) ? 1 : 0);
173 int RSA_set_ex_data(RSA *r, int idx, void *arg)
175 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
178 void *RSA_get_ex_data(const RSA *r, int idx)
180 return (CRYPTO_get_ex_data(&r->ex_data, idx));
183 int RSA_security_bits(const RSA *rsa)
185 return BN_security_bits(BN_num_bits(rsa->n), -1);
188 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
190 /* If the fields n and e in r are NULL, the corresponding input
191 * parameters MUST be non-NULL for n and e. d may be
192 * left NULL (in case only the public key is used).
194 if ((r->n == NULL && n == NULL)
195 || (r->e == NULL && e == NULL))
214 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
216 /* If the fields p and q in r are NULL, the corresponding input
217 * parameters MUST be non-NULL.
219 if ((r->p == NULL && p == NULL)
220 || (r->q == NULL && q == NULL))
235 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
237 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
238 * parameters MUST be non-NULL.
240 if ((r->dmp1 == NULL && dmp1 == NULL)
241 || (r->dmq1 == NULL && dmq1 == NULL)
242 || (r->iqmp == NULL && iqmp == NULL))
261 void RSA_get0_key(const RSA *r,
262 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
272 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
280 void RSA_get0_crt_params(const RSA *r,
281 const BIGNUM **dmp1, const BIGNUM **dmq1,
292 void RSA_clear_flags(RSA *r, int flags)
297 int RSA_test_flags(const RSA *r, int flags)
299 return r->flags & flags;
302 void RSA_set_flags(RSA *r, int flags)
307 ENGINE *RSA_get0_engine(const RSA *r)