Support multi-prime RSA (RFC 8017)
[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 "internal/refcount.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     }
79     if (ret->engine) {
80         ret->meth = ENGINE_get_RSA(ret->engine);
81         if (ret->meth == NULL) {
82             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
83             goto err;
84         }
85     }
86 #endif
87
88     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
89     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
90         goto err;
91     }
92
93     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
94         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
95         goto err;
96     }
97
98     return ret;
99
100 err:
101     RSA_free(ret);
102     return NULL;
103 }
104
105 void RSA_free(RSA *r)
106 {
107     int i;
108
109     if (r == NULL)
110         return;
111
112     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
113     REF_PRINT_COUNT("RSA", r);
114     if (i > 0)
115         return;
116     REF_ASSERT_ISNT(i < 0);
117
118     if (r->meth->finish)
119         r->meth->finish(r);
120 #ifndef OPENSSL_NO_ENGINE
121     ENGINE_finish(r->engine);
122 #endif
123
124     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
125
126     CRYPTO_THREAD_lock_free(r->lock);
127
128     BN_clear_free(r->n);
129     BN_clear_free(r->e);
130     BN_clear_free(r->d);
131     BN_clear_free(r->p);
132     BN_clear_free(r->q);
133     BN_clear_free(r->dmp1);
134     BN_clear_free(r->dmq1);
135     BN_clear_free(r->iqmp);
136     RSA_PSS_PARAMS_free(r->pss);
137     sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
138     BN_BLINDING_free(r->blinding);
139     BN_BLINDING_free(r->mt_blinding);
140     OPENSSL_free(r->bignum_data);
141     OPENSSL_free(r);
142 }
143
144 int RSA_up_ref(RSA *r)
145 {
146     int i;
147
148     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
149         return 0;
150
151     REF_PRINT_COUNT("RSA", r);
152     REF_ASSERT_ISNT(i < 2);
153     return i > 1 ? 1 : 0;
154 }
155
156 int RSA_set_ex_data(RSA *r, int idx, void *arg)
157 {
158     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
159 }
160
161 void *RSA_get_ex_data(const RSA *r, int idx)
162 {
163     return CRYPTO_get_ex_data(&r->ex_data, idx);
164 }
165
166 int RSA_security_bits(const RSA *rsa)
167 {
168     return BN_security_bits(BN_num_bits(rsa->n), -1);
169 }
170
171 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
172 {
173     /* If the fields n and e in r are NULL, the corresponding input
174      * parameters MUST be non-NULL for n and e.  d may be
175      * left NULL (in case only the public key is used).
176      */
177     if ((r->n == NULL && n == NULL)
178         || (r->e == NULL && e == NULL))
179         return 0;
180
181     if (n != NULL) {
182         BN_free(r->n);
183         r->n = n;
184     }
185     if (e != NULL) {
186         BN_free(r->e);
187         r->e = e;
188     }
189     if (d != NULL) {
190         BN_free(r->d);
191         r->d = d;
192     }
193
194     return 1;
195 }
196
197 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
198 {
199     /* If the fields p and q in r are NULL, the corresponding input
200      * parameters MUST be non-NULL.
201      */
202     if ((r->p == NULL && p == NULL)
203         || (r->q == NULL && q == NULL))
204         return 0;
205
206     if (p != NULL) {
207         BN_free(r->p);
208         r->p = p;
209     }
210     if (q != NULL) {
211         BN_free(r->q);
212         r->q = q;
213     }
214
215     return 1;
216 }
217
218 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
219 {
220     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
221      * parameters MUST be non-NULL.
222      */
223     if ((r->dmp1 == NULL && dmp1 == NULL)
224         || (r->dmq1 == NULL && dmq1 == NULL)
225         || (r->iqmp == NULL && iqmp == NULL))
226         return 0;
227
228     if (dmp1 != NULL) {
229         BN_free(r->dmp1);
230         r->dmp1 = dmp1;
231     }
232     if (dmq1 != NULL) {
233         BN_free(r->dmq1);
234         r->dmq1 = dmq1;
235     }
236     if (iqmp != NULL) {
237         BN_free(r->iqmp);
238         r->iqmp = iqmp;
239     }
240
241     return 1;
242 }
243
244 /*
245  * Is it better to export RSA_PRIME_INFO structure
246  * and related functions to let user pass a triplet?
247  */
248 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
249                                 BIGNUM *coeffs[], int pnum)
250 {
251     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
252     RSA_PRIME_INFO *pinfo;
253     int i;
254
255     if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
256         return 0;
257
258     prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
259     if (prime_infos == NULL)
260         return 0;
261
262     if (r->prime_infos != NULL)
263         old = r->prime_infos;
264
265     for (i = 0; i < pnum; i++) {
266         pinfo = rsa_multip_info_new();
267         if (pinfo == NULL)
268             goto err;
269         if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
270             BN_free(pinfo->r);
271             BN_free(pinfo->d);
272             BN_free(pinfo->t);
273             pinfo->r = primes[i];
274             pinfo->d = exps[i];
275             pinfo->t = coeffs[i];
276         } else {
277             rsa_multip_info_free(pinfo);
278             goto err;
279         }
280         (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
281     }
282
283     r->prime_infos = prime_infos;
284
285     if (!rsa_multip_calc_product(r)) {
286         r->prime_infos = old;
287         goto err;
288     }
289
290     if (old != NULL) {
291         /*
292          * This is hard to deal with, since the old infos could
293          * also be set by this function and r, d, t should not
294          * be freed in that case. So currently, stay consistent
295          * with other *set0* functions: just free it...
296          */
297         sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
298     }
299
300     r->version = RSA_ASN1_VERSION_MULTI;
301
302     return 1;
303  err:
304     /* r, d, t should not be freed */
305     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
306     return 0;
307 }
308
309 void RSA_get0_key(const RSA *r,
310                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
311 {
312     if (n != NULL)
313         *n = r->n;
314     if (e != NULL)
315         *e = r->e;
316     if (d != NULL)
317         *d = r->d;
318 }
319
320 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
321 {
322     if (p != NULL)
323         *p = r->p;
324     if (q != NULL)
325         *q = r->q;
326 }
327
328 int RSA_get_multi_prime_extra_count(const RSA *r)
329 {
330     int pnum;
331
332     pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
333     if (pnum <= 0)
334         pnum = 0;
335     return pnum;
336 }
337
338 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
339 {
340     int pnum, i;
341     RSA_PRIME_INFO *pinfo;
342
343     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
344         return 0;
345
346     /*
347      * return other primes
348      * it's caller's responsibility to allocate oth_primes[pnum]
349      */
350     for (i = 0; i < pnum; i++) {
351         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
352         primes[i] = pinfo->r;
353     }
354
355     return 1;
356 }
357
358 void RSA_get0_crt_params(const RSA *r,
359                          const BIGNUM **dmp1, const BIGNUM **dmq1,
360                          const BIGNUM **iqmp)
361 {
362     if (dmp1 != NULL)
363         *dmp1 = r->dmp1;
364     if (dmq1 != NULL)
365         *dmq1 = r->dmq1;
366     if (iqmp != NULL)
367         *iqmp = r->iqmp;
368 }
369
370 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
371                                     const BIGNUM *coeffs[])
372 {
373     int pnum;
374
375     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
376         return 0;
377
378     /* return other primes */
379     if (exps != NULL || coeffs != NULL) {
380         RSA_PRIME_INFO *pinfo;
381         int i;
382
383         /* it's the user's job to guarantee the buffer length */
384         for (i = 0; i < pnum; i++) {
385             pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
386             if (exps != NULL)
387                 exps[i] = pinfo->d;
388             if (coeffs != NULL)
389                 coeffs[i] = pinfo->t;
390         }
391     }
392
393     return 1;
394 }
395
396 void RSA_clear_flags(RSA *r, int flags)
397 {
398     r->flags &= ~flags;
399 }
400
401 int RSA_test_flags(const RSA *r, int flags)
402 {
403     return r->flags & flags;
404 }
405
406 void RSA_set_flags(RSA *r, int flags)
407 {
408     r->flags |= flags;
409 }
410
411 int RSA_get_version(RSA *r)
412 {
413     /* { two-prime(0), multi(1) } */
414     return r->version;
415 }
416
417 ENGINE *RSA_get0_engine(const RSA *r)
418 {
419     return r->engine;
420 }
421
422 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
423 {
424     /* If key type not RSA or RSA-PSS return error */
425     if (ctx != NULL && ctx->pmeth != NULL
426         && ctx->pmeth->pkey_id != EVP_PKEY_RSA
427         && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
428         return -1;
429      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
430 }