]>
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/rand.h>
16 #include <openssl/engine.h>
19 static const RSA_METHOD
*default_RSA_meth
= NULL
;
23 RSA
*r
= RSA_new_method(NULL
);
28 void RSA_set_default_method(const RSA_METHOD
*meth
)
30 default_RSA_meth
= meth
;
33 const RSA_METHOD
*RSA_get_default_method(void)
35 if (default_RSA_meth
== NULL
) {
37 default_RSA_meth
= RSA_null_method();
39 default_RSA_meth
= RSA_PKCS1_OpenSSL();
43 return default_RSA_meth
;
46 const RSA_METHOD
*RSA_get_method(const RSA
*rsa
)
51 int RSA_set_method(RSA
*rsa
, const RSA_METHOD
*meth
)
54 * NB: The caller is specifically setting a method, so it's not up to us
55 * to deal with which ENGINE it comes from.
57 const RSA_METHOD
*mtmp
;
61 #ifndef OPENSSL_NO_ENGINE
62 ENGINE_finish(rsa
->engine
);
71 RSA
*RSA_new_method(ENGINE
*engine
)
75 ret
= OPENSSL_zalloc(sizeof(*ret
));
77 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
81 ret
->meth
= RSA_get_default_method();
82 #ifndef OPENSSL_NO_ENGINE
84 if (!ENGINE_init(engine
)) {
85 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
91 ret
->engine
= ENGINE_get_default_RSA();
93 ret
->meth
= ENGINE_get_RSA(ret
->engine
);
94 if (ret
->meth
== NULL
) {
95 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
96 ENGINE_finish(ret
->engine
);
104 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
105 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
)) {
106 #ifndef OPENSSL_NO_ENGINE
107 ENGINE_finish(ret
->engine
);
113 ret
->lock
= CRYPTO_THREAD_lock_new();
114 if (ret
->lock
== NULL
) {
115 #ifndef OPENSSL_NO_ENGINE
116 ENGINE_finish(ret
->engine
);
118 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
);
123 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
131 void RSA_free(RSA
*r
)
138 CRYPTO_atomic_add(&r
->references
, -1, &i
, r
->lock
);
139 REF_PRINT_COUNT("RSA", r
);
142 REF_ASSERT_ISNT(i
< 0);
146 #ifndef OPENSSL_NO_ENGINE
147 ENGINE_finish(r
->engine
);
150 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, r
, &r
->ex_data
);
152 CRYPTO_THREAD_lock_free(r
->lock
);
159 BN_clear_free(r
->dmp1
);
160 BN_clear_free(r
->dmq1
);
161 BN_clear_free(r
->iqmp
);
162 BN_BLINDING_free(r
->blinding
);
163 BN_BLINDING_free(r
->mt_blinding
);
164 OPENSSL_free(r
->bignum_data
);
168 int RSA_up_ref(RSA
*r
)
172 if (CRYPTO_atomic_add(&r
->references
, 1, &i
, r
->lock
) <= 0)
175 REF_PRINT_COUNT("RSA", r
);
176 REF_ASSERT_ISNT(i
< 2);
177 return ((i
> 1) ? 1 : 0);
180 int RSA_set_ex_data(RSA
*r
, int idx
, void *arg
)
182 return (CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
));
185 void *RSA_get_ex_data(const RSA
*r
, int idx
)
187 return (CRYPTO_get_ex_data(&r
->ex_data
, idx
));
190 int RSA_memory_lock(RSA
*r
)
194 BIGNUM
*bn
, **t
[6], *b
;
205 k
= bn_sizeof_BIGNUM() * 6;
206 off
= k
/ sizeof(BN_ULONG
) + 1;
208 for (i
= 0; i
< 6; i
++)
209 j
+= bn_get_top(*t
[i
]);
210 if ((p
= OPENSSL_malloc((off
+ j
) * sizeof(*p
))) == NULL
) {
211 RSAerr(RSA_F_RSA_MEMORY_LOCK
, ERR_R_MALLOC_FAILURE
);
214 memset(p
, 0, sizeof(*p
) * (off
+ j
));
216 ul
= (BN_ULONG
*)&(p
[off
]);
217 for (i
= 0; i
< 6; i
++) {
219 *(t
[i
]) = bn_array_el(bn
, i
);
220 memcpy(bn_array_el(bn
, i
), b
, bn_sizeof_BIGNUM());
221 memcpy(ul
, bn_get_words(b
), sizeof(*ul
) * bn_get_top(b
));
222 bn_set_static_words(bn_array_el(bn
, i
), ul
, bn_get_top(b
));
227 /* I should fix this so it can still be done */
228 r
->flags
&= ~(RSA_FLAG_CACHE_PRIVATE
| RSA_FLAG_CACHE_PUBLIC
);
234 int RSA_security_bits(const RSA
*rsa
)
236 return BN_security_bits(BN_num_bits(rsa
->n
), -1);
239 int RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
241 /* If the fields in r are NULL, the corresponding input
242 * parameters MUST be non-NULL for n and e. d may be
243 * left NULL (in case only the public key is used).
245 * It is an error to give the results from get0 on r
246 * as input parameters.
248 if (n
== r
->n
|| e
== r
->e
249 || (r
->d
!= NULL
&& d
== r
->d
))
268 int RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
270 /* If the fields in r are NULL, the corresponding input
271 * parameters MUST be non-NULL.
273 * It is an error to give the results from get0 on r
274 * as input parameters.
276 if (p
== r
->p
|| q
== r
->q
)
291 int RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
293 /* If the fields in r are NULL, the corresponding input
294 * parameters MUST be non-NULL.
296 * It is an error to give the results from get0 on r
297 * as input parameters.
299 if (dmp1
== r
->dmp1
|| dmq1
== r
->dmq1
|| iqmp
== r
->iqmp
)
318 void RSA_get0_key(const RSA
*r
, BIGNUM
**n
, BIGNUM
**e
, BIGNUM
**d
)
328 void RSA_get0_factors(const RSA
*r
, BIGNUM
**p
, BIGNUM
**q
)
336 void RSA_get0_crt_params(const RSA
*r
,
337 BIGNUM
**dmp1
, BIGNUM
**dmq1
, BIGNUM
**iqmp
)
347 void RSA_clear_flags(RSA
*r
, int flags
)
352 int RSA_test_flags(const RSA
*r
, int flags
)
354 return r
->flags
& flags
;
357 void RSA_set_flags(RSA
*r
, int flags
)
362 ENGINE
*RSA_get0_engine(RSA
*r
)