]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_lib.c
PSS EVP_PKEY method
[thirdparty/openssl.git] / crypto / rsa / rsa_lib.c
CommitLineData
2039c421
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
ec577822 11#include <openssl/crypto.h>
b39fc560 12#include "internal/cryptlib.h"
ec577822 13#include <openssl/lhash.h>
18125f7f 14#include "internal/bn_int.h"
3c27208f 15#include <openssl/engine.h>
9862e9aa 16#include "rsa_locl.h"
d02b48c6 17
0f113f3e 18static const RSA_METHOD *default_RSA_meth = NULL;
d02b48c6 19
6b691a5c 20RSA *RSA_new(void)
0f113f3e
MC
21{
22 RSA *r = RSA_new_method(NULL);
c554155b 23
0f113f3e
MC
24 return r;
25}
d02b48c6 26
cb78486d 27void RSA_set_default_method(const RSA_METHOD *meth)
0f113f3e
MC
28{
29 default_RSA_meth = meth;
30}
d02b48c6 31
cb78486d 32const RSA_METHOD *RSA_get_default_method(void)
0f113f3e
MC
33{
34 if (default_RSA_meth == NULL) {
deb4d50e 35#ifdef RSA_NULL
0f113f3e 36 default_RSA_meth = RSA_null_method();
deb4d50e 37#else
b0700d2c 38 default_RSA_meth = RSA_PKCS1_OpenSSL();
deb4d50e 39#endif
0f113f3e 40 }
deb4d50e 41
0f113f3e
MC
42 return default_RSA_meth;
43}
ce8b2574 44
29c1f061 45const RSA_METHOD *RSA_get_method(const RSA *rsa)
0f113f3e
MC
46{
47 return rsa->meth;
48}
cb78486d
GT
49
50int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
0f113f3e
MC
51{
52 /*
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.
55 */
56 const RSA_METHOD *mtmp;
57 mtmp = rsa->meth;
58 if (mtmp->finish)
59 mtmp->finish(rsa);
0b13e9f0 60#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
61 ENGINE_finish(rsa->engine);
62 rsa->engine = NULL;
0b13e9f0 63#endif
0f113f3e
MC
64 rsa->meth = meth;
65 if (meth->init)
66 meth->init(rsa);
67 return 1;
68}
ce8b2574 69
5270e702 70RSA *RSA_new_method(ENGINE *engine)
0f113f3e 71{
11ed851d 72 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
d02b48c6 73
0f113f3e
MC
74 if (ret == NULL) {
75 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
76 return NULL;
77 }
d02b48c6 78
11ed851d
F
79 ret->references = 1;
80 ret->lock = CRYPTO_THREAD_lock_new();
81 if (ret->lock == NULL) {
82 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
83 OPENSSL_free(ret);
84 return NULL;
85 }
86
0f113f3e 87 ret->meth = RSA_get_default_method();
0b13e9f0 88#ifndef OPENSSL_NO_ENGINE
11ed851d 89 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
0f113f3e
MC
90 if (engine) {
91 if (!ENGINE_init(engine)) {
92 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
11ed851d 93 goto err;
0f113f3e
MC
94 }
95 ret->engine = engine;
96 } else
97 ret->engine = ENGINE_get_default_RSA();
98 if (ret->engine) {
99 ret->meth = ENGINE_get_RSA(ret->engine);
7c96dbcd 100 if (ret->meth == NULL) {
0f113f3e 101 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
11ed851d 102 goto err;
0f113f3e
MC
103 }
104 }
0b13e9f0 105#endif
0c9de428 106
0f113f3e
MC
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)) {
11ed851d 109 goto err;
d188a536
AG
110 }
111
112 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
11ed851d
F
113 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
114 goto err;
0f113f3e 115 }
d188a536
AG
116
117 return ret;
11ed851d
F
118
119err:
120 RSA_free(ret);
121 return NULL;
0f113f3e 122}
d02b48c6 123
6b691a5c 124void RSA_free(RSA *r)
0f113f3e
MC
125{
126 int i;
d02b48c6 127
0f113f3e
MC
128 if (r == NULL)
129 return;
d02b48c6 130
2f545ae4 131 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 132 REF_PRINT_COUNT("RSA", r);
0f113f3e
MC
133 if (i > 0)
134 return;
f3f1cf84 135 REF_ASSERT_ISNT(i < 0);
d02b48c6 136
0f113f3e
MC
137 if (r->meth->finish)
138 r->meth->finish(r);
0b13e9f0 139#ifndef OPENSSL_NO_ENGINE
412bafdc 140 ENGINE_finish(r->engine);
0b13e9f0 141#endif
d02b48c6 142
0f113f3e 143 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
7abe8305 144
d188a536
AG
145 CRYPTO_THREAD_lock_free(r->lock);
146
23a1d5e9
RS
147 BN_clear_free(r->n);
148 BN_clear_free(r->e);
149 BN_clear_free(r->d);
150 BN_clear_free(r->p);
151 BN_clear_free(r->q);
152 BN_clear_free(r->dmp1);
153 BN_clear_free(r->dmq1);
154 BN_clear_free(r->iqmp);
d771441d 155 RSA_PSS_PARAMS_free(r->pss);
23a1d5e9
RS
156 BN_BLINDING_free(r->blinding);
157 BN_BLINDING_free(r->mt_blinding);
4c42ebd2 158 OPENSSL_free(r->bignum_data);
0f113f3e
MC
159 OPENSSL_free(r);
160}
d02b48c6 161
6ac4e8bd 162int RSA_up_ref(RSA *r)
0f113f3e 163{
d188a536
AG
164 int i;
165
2f545ae4 166 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 167 return 0;
f3f1cf84
RS
168
169 REF_PRINT_COUNT("RSA", r);
170 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
171 return ((i > 1) ? 1 : 0);
172}
5cbc2e8b 173
dd9d233e 174int RSA_set_ex_data(RSA *r, int idx, void *arg)
0f113f3e
MC
175{
176 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
177}
58964a49 178
29c1f061 179void *RSA_get_ex_data(const RSA *r, int idx)
0f113f3e
MC
180{
181 return (CRYPTO_get_ex_data(&r->ex_data, idx));
182}
58964a49 183
2514fa79 184int RSA_security_bits(const RSA *rsa)
0f113f3e
MC
185{
186 return BN_security_bits(BN_num_bits(rsa->n), -1);
187}
9862e9aa
RL
188
189int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
190{
fd809cfd 191 /* If the fields n and e in r are NULL, the corresponding input
1da12e34
RL
192 * parameters MUST be non-NULL for n and e. d may be
193 * left NULL (in case only the public key is used).
1da12e34 194 */
b84e1226
MC
195 if ((r->n == NULL && n == NULL)
196 || (r->e == NULL && e == NULL))
9862e9aa
RL
197 return 0;
198
1da12e34
RL
199 if (n != NULL) {
200 BN_free(r->n);
201 r->n = n;
202 }
203 if (e != NULL) {
204 BN_free(r->e);
205 r->e = e;
206 }
207 if (d != NULL) {
208 BN_free(r->d);
209 r->d = d;
210 }
9862e9aa
RL
211
212 return 1;
213}
214
215int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
216{
fd809cfd 217 /* If the fields p and q in r are NULL, the corresponding input
1da12e34 218 * parameters MUST be non-NULL.
1da12e34 219 */
b84e1226
MC
220 if ((r->p == NULL && p == NULL)
221 || (r->q == NULL && q == NULL))
9862e9aa
RL
222 return 0;
223
1da12e34
RL
224 if (p != NULL) {
225 BN_free(r->p);
226 r->p = p;
227 }
228 if (q != NULL) {
229 BN_free(r->q);
230 r->q = q;
231 }
9862e9aa
RL
232
233 return 1;
234}
235
236int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
237{
fd809cfd 238 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
1da12e34 239 * parameters MUST be non-NULL.
1da12e34 240 */
b84e1226
MC
241 if ((r->dmp1 == NULL && dmp1 == NULL)
242 || (r->dmq1 == NULL && dmq1 == NULL)
243 || (r->iqmp == NULL && iqmp == NULL))
9862e9aa
RL
244 return 0;
245
1da12e34
RL
246 if (dmp1 != NULL) {
247 BN_free(r->dmp1);
248 r->dmp1 = dmp1;
249 }
250 if (dmq1 != NULL) {
251 BN_free(r->dmq1);
252 r->dmq1 = dmq1;
253 }
254 if (iqmp != NULL) {
255 BN_free(r->iqmp);
256 r->iqmp = iqmp;
257 }
9862e9aa
RL
258
259 return 1;
260}
261
fd809cfd
RL
262void RSA_get0_key(const RSA *r,
263 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
9862e9aa
RL
264{
265 if (n != NULL)
266 *n = r->n;
267 if (e != NULL)
268 *e = r->e;
269 if (d != NULL)
270 *d = r->d;
271}
272
fd809cfd 273void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
9862e9aa
RL
274{
275 if (p != NULL)
276 *p = r->p;
277 if (q != NULL)
278 *q = r->q;
279}
280
281void RSA_get0_crt_params(const RSA *r,
fd809cfd
RL
282 const BIGNUM **dmp1, const BIGNUM **dmq1,
283 const BIGNUM **iqmp)
9862e9aa
RL
284{
285 if (dmp1 != NULL)
286 *dmp1 = r->dmp1;
287 if (dmq1 != NULL)
288 *dmq1 = r->dmq1;
289 if (iqmp != NULL)
290 *iqmp = r->iqmp;
291}
292
293void RSA_clear_flags(RSA *r, int flags)
294{
295 r->flags &= ~flags;
296}
297
298int RSA_test_flags(const RSA *r, int flags)
299{
300 return r->flags & flags;
301}
302
303void RSA_set_flags(RSA *r, int flags)
304{
305 r->flags |= flags;
306}
307
e0685d24 308ENGINE *RSA_get0_engine(const RSA *r)
9862e9aa
RL
309{
310 return r->engine;
311}