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