Fix coding style in crypto/rsa directory
[openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
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"
18 #include "rsa_locl.h"
19
20 RSA *RSA_new(void)
21 {
22     return RSA_new_method(NULL);
23 }
24
25 const RSA_METHOD *RSA_get_method(const RSA *rsa)
26 {
27     return rsa->meth;
28 }
29
30 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
31 {
32     /*
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.
35      */
36     const RSA_METHOD *mtmp;
37     mtmp = rsa->meth;
38     if (mtmp->finish)
39         mtmp->finish(rsa);
40 #ifndef OPENSSL_NO_ENGINE
41     ENGINE_finish(rsa->engine);
42     rsa->engine = NULL;
43 #endif
44     rsa->meth = meth;
45     if (meth->init)
46         meth->init(rsa);
47     return 1;
48 }
49
50 RSA *RSA_new_method(ENGINE *engine)
51 {
52     RSA *ret = OPENSSL_zalloc(sizeof(*ret));
53
54     if (ret == NULL) {
55         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
56         return NULL;
57     }
58
59     ret->references = 1;
60     ret->lock = CRYPTO_THREAD_lock_new();
61     if (ret->lock == NULL) {
62         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
63         OPENSSL_free(ret);
64         return NULL;
65     }
66
67     ret->meth = RSA_get_default_method();
68 #ifndef OPENSSL_NO_ENGINE
69     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
70     if (engine) {
71         if (!ENGINE_init(engine)) {
72             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
73             goto err;
74         }
75         ret->engine = engine;
76     } else
77         ret->engine = ENGINE_get_default_RSA();
78     if (ret->engine) {
79         ret->meth = ENGINE_get_RSA(ret->engine);
80         if (ret->meth == NULL) {
81             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
82             goto err;
83         }
84     }
85 #endif
86
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)) {
89         goto err;
90     }
91
92     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
93         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
94         goto err;
95     }
96
97     return ret;
98
99 err:
100     RSA_free(ret);
101     return NULL;
102 }
103
104 void RSA_free(RSA *r)
105 {
106     int i;
107
108     if (r == NULL)
109         return;
110
111     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
112     REF_PRINT_COUNT("RSA", r);
113     if (i > 0)
114         return;
115     REF_ASSERT_ISNT(i < 0);
116
117     if (r->meth->finish)
118         r->meth->finish(r);
119 #ifndef OPENSSL_NO_ENGINE
120     ENGINE_finish(r->engine);
121 #endif
122
123     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
124
125     CRYPTO_THREAD_lock_free(r->lock);
126
127     BN_clear_free(r->n);
128     BN_clear_free(r->e);
129     BN_clear_free(r->d);
130     BN_clear_free(r->p);
131     BN_clear_free(r->q);
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);
139     OPENSSL_free(r);
140 }
141
142 int RSA_up_ref(RSA *r)
143 {
144     int i;
145
146     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
147         return 0;
148
149     REF_PRINT_COUNT("RSA", r);
150     REF_ASSERT_ISNT(i < 2);
151     return i > 1 ? 1 : 0;
152 }
153
154 int RSA_set_ex_data(RSA *r, int idx, void *arg)
155 {
156     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
157 }
158
159 void *RSA_get_ex_data(const RSA *r, int idx)
160 {
161     return CRYPTO_get_ex_data(&r->ex_data, idx);
162 }
163
164 int RSA_security_bits(const RSA *rsa)
165 {
166     return BN_security_bits(BN_num_bits(rsa->n), -1);
167 }
168
169 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
170 {
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).
174      */
175     if ((r->n == NULL && n == NULL)
176         || (r->e == NULL && e == NULL))
177         return 0;
178
179     if (n != NULL) {
180         BN_free(r->n);
181         r->n = n;
182     }
183     if (e != NULL) {
184         BN_free(r->e);
185         r->e = e;
186     }
187     if (d != NULL) {
188         BN_free(r->d);
189         r->d = d;
190     }
191
192     return 1;
193 }
194
195 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
196 {
197     /* If the fields p and q in r are NULL, the corresponding input
198      * parameters MUST be non-NULL.
199      */
200     if ((r->p == NULL && p == NULL)
201         || (r->q == NULL && q == NULL))
202         return 0;
203
204     if (p != NULL) {
205         BN_free(r->p);
206         r->p = p;
207     }
208     if (q != NULL) {
209         BN_free(r->q);
210         r->q = q;
211     }
212
213     return 1;
214 }
215
216 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
217 {
218     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
219      * parameters MUST be non-NULL.
220      */
221     if ((r->dmp1 == NULL && dmp1 == NULL)
222         || (r->dmq1 == NULL && dmq1 == NULL)
223         || (r->iqmp == NULL && iqmp == NULL))
224         return 0;
225
226     if (dmp1 != NULL) {
227         BN_free(r->dmp1);
228         r->dmp1 = dmp1;
229     }
230     if (dmq1 != NULL) {
231         BN_free(r->dmq1);
232         r->dmq1 = dmq1;
233     }
234     if (iqmp != NULL) {
235         BN_free(r->iqmp);
236         r->iqmp = iqmp;
237     }
238
239     return 1;
240 }
241
242 void RSA_get0_key(const RSA *r,
243                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
244 {
245     if (n != NULL)
246         *n = r->n;
247     if (e != NULL)
248         *e = r->e;
249     if (d != NULL)
250         *d = r->d;
251 }
252
253 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
254 {
255     if (p != NULL)
256         *p = r->p;
257     if (q != NULL)
258         *q = r->q;
259 }
260
261 void RSA_get0_crt_params(const RSA *r,
262                          const BIGNUM **dmp1, const BIGNUM **dmq1,
263                          const BIGNUM **iqmp)
264 {
265     if (dmp1 != NULL)
266         *dmp1 = r->dmp1;
267     if (dmq1 != NULL)
268         *dmq1 = r->dmq1;
269     if (iqmp != NULL)
270         *iqmp = r->iqmp;
271 }
272
273 void RSA_clear_flags(RSA *r, int flags)
274 {
275     r->flags &= ~flags;
276 }
277
278 int RSA_test_flags(const RSA *r, int flags)
279 {
280     return r->flags & flags;
281 }
282
283 void RSA_set_flags(RSA *r, int flags)
284 {
285     r->flags |= flags;
286 }
287
288 ENGINE *RSA_get0_engine(const RSA *r)
289 {
290     return r->engine;
291 }
292
293 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
294 {
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)
299         return -1;
300      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
301 }