0ccb3ce02fbcb2cfeff79cd061c8a12041a22a08
[openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2  * Copyright 1995-2016 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/rand.h>
16 #include <openssl/engine.h>
17 #include "rsa_locl.h"
18
19 static const RSA_METHOD *default_RSA_meth = NULL;
20
21 RSA *RSA_new(void)
22 {
23     RSA *r = RSA_new_method(NULL);
24
25     return r;
26 }
27
28 void RSA_set_default_method(const RSA_METHOD *meth)
29 {
30     default_RSA_meth = meth;
31 }
32
33 const RSA_METHOD *RSA_get_default_method(void)
34 {
35     if (default_RSA_meth == NULL) {
36 #ifdef RSA_NULL
37         default_RSA_meth = RSA_null_method();
38 #else
39         default_RSA_meth = RSA_PKCS1_OpenSSL();
40 #endif
41     }
42
43     return default_RSA_meth;
44 }
45
46 const RSA_METHOD *RSA_get_method(const RSA *rsa)
47 {
48     return rsa->meth;
49 }
50
51 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
52 {
53     /*
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.
56      */
57     const RSA_METHOD *mtmp;
58     mtmp = rsa->meth;
59     if (mtmp->finish)
60         mtmp->finish(rsa);
61 #ifndef OPENSSL_NO_ENGINE
62     ENGINE_finish(rsa->engine);
63     rsa->engine = NULL;
64 #endif
65     rsa->meth = meth;
66     if (meth->init)
67         meth->init(rsa);
68     return 1;
69 }
70
71 RSA *RSA_new_method(ENGINE *engine)
72 {
73     RSA *ret;
74
75     ret = OPENSSL_zalloc(sizeof(*ret));
76     if (ret == NULL) {
77         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
78         return NULL;
79     }
80
81     ret->meth = RSA_get_default_method();
82 #ifndef OPENSSL_NO_ENGINE
83     if (engine) {
84         if (!ENGINE_init(engine)) {
85             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
86             OPENSSL_free(ret);
87             return NULL;
88         }
89         ret->engine = engine;
90     } else
91         ret->engine = ENGINE_get_default_RSA();
92     if (ret->engine) {
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);
97             OPENSSL_free(ret);
98             return NULL;
99         }
100     }
101 #endif
102
103     ret->references = 1;
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);
108 #endif
109         OPENSSL_free(ret);
110         return NULL;
111     }
112
113     ret->lock = CRYPTO_THREAD_lock_new();
114     if (ret->lock == NULL) {
115 #ifndef OPENSSL_NO_ENGINE
116         ENGINE_finish(ret->engine);
117 #endif
118         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
119         OPENSSL_free(ret);
120         return NULL;
121     }
122
123     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
124         RSA_free(ret);
125         ret = NULL;
126     }
127
128     return ret;
129 }
130
131 void RSA_free(RSA *r)
132 {
133     int i;
134
135     if (r == NULL)
136         return;
137
138     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
139     REF_PRINT_COUNT("RSA", r);
140     if (i > 0)
141         return;
142     REF_ASSERT_ISNT(i < 0);
143
144     if (r->meth->finish)
145         r->meth->finish(r);
146 #ifndef OPENSSL_NO_ENGINE
147     ENGINE_finish(r->engine);
148 #endif
149
150     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
151
152     CRYPTO_THREAD_lock_free(r->lock);
153
154     BN_clear_free(r->n);
155     BN_clear_free(r->e);
156     BN_clear_free(r->d);
157     BN_clear_free(r->p);
158     BN_clear_free(r->q);
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);
165     OPENSSL_free(r);
166 }
167
168 int RSA_up_ref(RSA *r)
169 {
170     int i;
171
172     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
173         return 0;
174
175     REF_PRINT_COUNT("RSA", r);
176     REF_ASSERT_ISNT(i < 2);
177     return ((i > 1) ? 1 : 0);
178 }
179
180 int RSA_set_ex_data(RSA *r, int idx, void *arg)
181 {
182     return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
183 }
184
185 void *RSA_get_ex_data(const RSA *r, int idx)
186 {
187     return (CRYPTO_get_ex_data(&r->ex_data, idx));
188 }
189
190 int RSA_memory_lock(RSA *r)
191 {
192     int i, j, k, off;
193     char *p;
194     BIGNUM *bn, **t[6], *b;
195     BN_ULONG *ul;
196
197     if (r->d == NULL)
198         return (1);
199     t[0] = &r->d;
200     t[1] = &r->p;
201     t[2] = &r->q;
202     t[3] = &r->dmp1;
203     t[4] = &r->dmq1;
204     t[5] = &r->iqmp;
205     k = bn_sizeof_BIGNUM() * 6;
206     off = k / sizeof(BN_ULONG) + 1;
207     j = 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);
212         return (0);
213     }
214     memset(p, 0, sizeof(*p) * (off + j));
215     bn = (BIGNUM *)p;
216     ul = (BN_ULONG *)&(p[off]);
217     for (i = 0; i < 6; i++) {
218         b = *(t[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));
223         ul += bn_get_top(b);
224         BN_clear_free(b);
225     }
226
227     /* I should fix this so it can still be done */
228     r->flags &= ~(RSA_FLAG_CACHE_PRIVATE | RSA_FLAG_CACHE_PUBLIC);
229
230     r->bignum_data = p;
231     return (1);
232 }
233
234 int RSA_security_bits(const RSA *rsa)
235 {
236     return BN_security_bits(BN_num_bits(rsa->n), -1);
237 }
238
239 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
240 {
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).
244      *
245      * It is an error to give the results from get0 on r
246      * as input parameters.
247      */
248     if (n == r->n || e == r->e
249         || (r->d != NULL && d == r->d))
250         return 0;
251
252     if (n != NULL) {
253         BN_free(r->n);
254         r->n = n;
255     }
256     if (e != NULL) {
257         BN_free(r->e);
258         r->e = e;
259     }
260     if (d != NULL) {
261         BN_free(r->d);
262         r->d = d;
263     }
264
265     return 1;
266 }
267
268 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
269 {
270     /* If the fields in r are NULL, the corresponding input
271      * parameters MUST be non-NULL.
272      *
273      * It is an error to give the results from get0 on r
274      * as input parameters.
275      */
276     if (p == r->p || q == r->q)
277         return 0;
278
279     if (p != NULL) {
280         BN_free(r->p);
281         r->p = p;
282     }
283     if (q != NULL) {
284         BN_free(r->q);
285         r->q = q;
286     }
287
288     return 1;
289 }
290
291 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
292 {
293     /* If the fields in r are NULL, the corresponding input
294      * parameters MUST be non-NULL.
295      *
296      * It is an error to give the results from get0 on r
297      * as input parameters.
298      */
299     if (dmp1 == r->dmp1 || dmq1 == r->dmq1 || iqmp == r->iqmp)
300         return 0;
301
302     if (dmp1 != NULL) {
303         BN_free(r->dmp1);
304         r->dmp1 = dmp1;
305     }
306     if (dmq1 != NULL) {
307         BN_free(r->dmq1);
308         r->dmq1 = dmq1;
309     }
310     if (iqmp != NULL) {
311         BN_free(r->iqmp);
312         r->iqmp = iqmp;
313     }
314
315     return 1;
316 }
317
318 void RSA_get0_key(const RSA *r, BIGNUM **n, BIGNUM **e, BIGNUM **d)
319 {
320     if (n != NULL)
321         *n = r->n;
322     if (e != NULL)
323         *e = r->e;
324     if (d != NULL)
325         *d = r->d;
326 }
327
328 void RSA_get0_factors(const RSA *r, BIGNUM **p, BIGNUM **q)
329 {
330     if (p != NULL)
331         *p = r->p;
332     if (q != NULL)
333         *q = r->q;
334 }
335
336 void RSA_get0_crt_params(const RSA *r,
337                          BIGNUM **dmp1, BIGNUM **dmq1, BIGNUM **iqmp)
338 {
339     if (dmp1 != NULL)
340         *dmp1 = r->dmp1;
341     if (dmq1 != NULL)
342         *dmq1 = r->dmq1;
343     if (iqmp != NULL)
344         *iqmp = r->iqmp;
345 }
346
347 void RSA_clear_flags(RSA *r, int flags)
348 {
349     r->flags &= ~flags;
350 }
351
352 int RSA_test_flags(const RSA *r, int flags)
353 {
354     return r->flags & flags;
355 }
356
357 void RSA_set_flags(RSA *r, int flags)
358 {
359     r->flags |= flags;
360 }
361
362 ENGINE *RSA_get0_engine(RSA *r)
363 {
364     return r->engine;
365 }