]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_lib.c
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>
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();
79 ret
->meth
= ENGINE_get_RSA(ret
->engine
);
80 if (ret
->meth
== NULL
) {
81 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
87 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
88 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
)) {
92 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
93 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_INIT_FAIL
);
104 void RSA_free(RSA
*r
)
111 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
112 REF_PRINT_COUNT("RSA", r
);
115 REF_ASSERT_ISNT(i
< 0);
119 #ifndef OPENSSL_NO_ENGINE
120 ENGINE_finish(r
->engine
);
123 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, r
, &r
->ex_data
);
125 CRYPTO_THREAD_lock_free(r
->lock
);
132 BN_clear_free(r
->dmp1
);
133 BN_clear_free(r
->dmq1
);
134 BN_clear_free(r
->iqmp
);
135 RSA_PSS_PARAMS_free(r
->pss
);
136 BN_BLINDING_free(r
->blinding
);
137 BN_BLINDING_free(r
->mt_blinding
);
138 OPENSSL_free(r
->bignum_data
);
142 int RSA_up_ref(RSA
*r
)
146 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
149 REF_PRINT_COUNT("RSA", r
);
150 REF_ASSERT_ISNT(i
< 2);
151 return ((i
> 1) ? 1 : 0);
154 int RSA_set_ex_data(RSA
*r
, int idx
, void *arg
)
156 return (CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
));
159 void *RSA_get_ex_data(const RSA
*r
, int idx
)
161 return (CRYPTO_get_ex_data(&r
->ex_data
, idx
));
164 int RSA_security_bits(const RSA
*rsa
)
166 return BN_security_bits(BN_num_bits(rsa
->n
), -1);
169 int RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
171 /* If the fields n and e in r are NULL, the corresponding input
172 * parameters MUST be non-NULL for n and e. d may be
173 * left NULL (in case only the public key is used).
175 if ((r
->n
== NULL
&& n
== NULL
)
176 || (r
->e
== NULL
&& e
== NULL
))
195 int RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
197 /* If the fields p and q in r are NULL, the corresponding input
198 * parameters MUST be non-NULL.
200 if ((r
->p
== NULL
&& p
== NULL
)
201 || (r
->q
== NULL
&& q
== NULL
))
216 int RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
218 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
219 * parameters MUST be non-NULL.
221 if ((r
->dmp1
== NULL
&& dmp1
== NULL
)
222 || (r
->dmq1
== NULL
&& dmq1
== NULL
)
223 || (r
->iqmp
== NULL
&& iqmp
== NULL
))
242 void RSA_get0_key(const RSA
*r
,
243 const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
253 void RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
261 void RSA_get0_crt_params(const RSA
*r
,
262 const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
273 void RSA_clear_flags(RSA
*r
, int flags
)
278 int RSA_test_flags(const RSA
*r
, int flags
)
280 return r
->flags
& flags
;
283 void RSA_set_flags(RSA
*r
, int flags
)
288 ENGINE
*RSA_get0_engine(const RSA
*r
)
293 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, int p1
, void *p2
)
295 /* If key type not RSA or RSA-PSS return error */
296 if (ctx
!= NULL
&& ctx
->pmeth
!= NULL
297 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
298 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
300 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, p1
, p2
);