d8af92dc6ce1e5ffb97ee80b6c0415d394f515dc
[openssl.git] / crypto / rsa / rsa_ossl.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 "internal/cryptlib.h"
11 #include "internal/bn_int.h"
12 #include <openssl/rand.h>
13 #include "rsa_locl.h"
14
15 #ifndef RSA_NULL
16
17 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
18                                   unsigned char *to, RSA *rsa, int padding);
19 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
20                                    unsigned char *to, RSA *rsa, int padding);
21 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
22                                   unsigned char *to, RSA *rsa, int padding);
23 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
24                                    unsigned char *to, RSA *rsa, int padding);
25 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
26                            BN_CTX *ctx);
27 static int rsa_ossl_init(RSA *rsa);
28 static int rsa_ossl_finish(RSA *rsa);
29 static RSA_METHOD rsa_pkcs1_ossl_meth = {
30     "OpenSSL PKCS#1 RSA (from Eric Young)",
31     rsa_ossl_public_encrypt,
32     rsa_ossl_public_decrypt,     /* signature verification */
33     rsa_ossl_private_encrypt,    /* signing */
34     rsa_ossl_private_decrypt,
35     rsa_ossl_mod_exp,
36     BN_mod_exp_mont,            /* XXX probably we should not use Montgomery
37                                  * if e == 3 */
38     rsa_ossl_init,
39     rsa_ossl_finish,
40     RSA_FLAG_FIPS_METHOD,       /* flags */
41     NULL,
42     0,                          /* rsa_sign */
43     0,                          /* rsa_verify */
44     NULL                        /* rsa_keygen */
45 };
46
47 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
48 {
49     return &rsa_pkcs1_ossl_meth;
50 }
51
52 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
53                                   unsigned char *to, RSA *rsa, int padding)
54 {
55     BIGNUM *f, *ret;
56     int i, j, k, num = 0, r = -1;
57     unsigned char *buf = NULL;
58     BN_CTX *ctx = NULL;
59
60     if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
61         RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
62         return -1;
63     }
64
65     if (BN_ucmp(rsa->n, rsa->e) <= 0) {
66         RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
67         return -1;
68     }
69
70     /* for large moduli, enforce exponent limit */
71     if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
72         if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
73             RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
74             return -1;
75         }
76     }
77
78     if ((ctx = BN_CTX_new()) == NULL)
79         goto err;
80     BN_CTX_start(ctx);
81     f = BN_CTX_get(ctx);
82     ret = BN_CTX_get(ctx);
83     num = BN_num_bytes(rsa->n);
84     buf = OPENSSL_malloc(num);
85     if (f == NULL || ret == NULL || buf == NULL) {
86         RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
87         goto err;
88     }
89
90     switch (padding) {
91     case RSA_PKCS1_PADDING:
92         i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
93         break;
94     case RSA_PKCS1_OAEP_PADDING:
95         i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
96         break;
97     case RSA_SSLV23_PADDING:
98         i = RSA_padding_add_SSLv23(buf, num, from, flen);
99         break;
100     case RSA_NO_PADDING:
101         i = RSA_padding_add_none(buf, num, from, flen);
102         break;
103     default:
104         RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
105         goto err;
106     }
107     if (i <= 0)
108         goto err;
109
110     if (BN_bin2bn(buf, num, f) == NULL)
111         goto err;
112
113     if (BN_ucmp(f, rsa->n) >= 0) {
114         /* usually the padding functions would catch this */
115         RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
116                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
117         goto err;
118     }
119
120     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
121         if (!BN_MONT_CTX_set_locked
122             (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
123             goto err;
124
125     if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
126                                rsa->_method_mod_n))
127         goto err;
128
129     /*
130      * put in leading 0 bytes if the number is less than the length of the
131      * modulus
132      */
133     j = BN_num_bytes(ret);
134     i = BN_bn2bin(ret, &(to[num - j]));
135     for (k = 0; k < (num - i); k++)
136         to[k] = 0;
137
138     r = num;
139  err:
140     if (ctx != NULL)
141         BN_CTX_end(ctx);
142     BN_CTX_free(ctx);
143     OPENSSL_clear_free(buf, num);
144     return (r);
145 }
146
147 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
148 {
149     BN_BLINDING *ret;
150
151     CRYPTO_THREAD_write_lock(rsa->lock);
152
153     if (rsa->blinding == NULL) {
154         rsa->blinding = RSA_setup_blinding(rsa, ctx);
155     }
156
157     ret = rsa->blinding;
158     if (ret == NULL)
159         goto err;
160
161     if (BN_BLINDING_is_current_thread(ret)) {
162         /* rsa->blinding is ours! */
163
164         *local = 1;
165     } else {
166         /* resort to rsa->mt_blinding instead */
167
168         /*
169          * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
170          * BN_BLINDING is shared, meaning that accesses require locks, and
171          * that the blinding factor must be stored outside the BN_BLINDING
172          */
173         *local = 0;
174
175         if (rsa->mt_blinding == NULL) {
176             rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
177         }
178         ret = rsa->mt_blinding;
179     }
180
181  err:
182     CRYPTO_THREAD_unlock(rsa->lock);
183     return ret;
184 }
185
186 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
187                                 BN_CTX *ctx)
188 {
189     if (unblind == NULL)
190         /*
191          * Local blinding: store the unblinding factor in BN_BLINDING.
192          */
193         return BN_BLINDING_convert_ex(f, NULL, b, ctx);
194     else {
195         /*
196          * Shared blinding: store the unblinding factor outside BN_BLINDING.
197          */
198         int ret;
199
200         BN_BLINDING_lock(b);
201         ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
202         BN_BLINDING_unlock(b);
203
204         return ret;
205     }
206 }
207
208 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
209                                BN_CTX *ctx)
210 {
211     /*
212      * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
213      * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
214      * is shared between threads, unblind must be non-null:
215      * BN_BLINDING_invert_ex will then use the local unblinding factor, and
216      * will only read the modulus from BN_BLINDING. In both cases it's safe
217      * to access the blinding without a lock.
218      */
219     return BN_BLINDING_invert_ex(f, unblind, b, ctx);
220 }
221
222 /* signing */
223 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
224                                    unsigned char *to, RSA *rsa, int padding)
225 {
226     BIGNUM *f, *ret, *res;
227     int i, j, k, num = 0, r = -1;
228     unsigned char *buf = NULL;
229     BN_CTX *ctx = NULL;
230     int local_blinding = 0;
231     /*
232      * Used only if the blinding structure is shared. A non-NULL unblind
233      * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
234      * the unblinding factor outside the blinding structure.
235      */
236     BIGNUM *unblind = NULL;
237     BN_BLINDING *blinding = NULL;
238
239     if ((ctx = BN_CTX_new()) == NULL)
240         goto err;
241     BN_CTX_start(ctx);
242     f = BN_CTX_get(ctx);
243     ret = BN_CTX_get(ctx);
244     num = BN_num_bytes(rsa->n);
245     buf = OPENSSL_malloc(num);
246     if (f == NULL || ret == NULL || buf == NULL) {
247         RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
248         goto err;
249     }
250
251     switch (padding) {
252     case RSA_PKCS1_PADDING:
253         i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
254         break;
255     case RSA_X931_PADDING:
256         i = RSA_padding_add_X931(buf, num, from, flen);
257         break;
258     case RSA_NO_PADDING:
259         i = RSA_padding_add_none(buf, num, from, flen);
260         break;
261     case RSA_SSLV23_PADDING:
262     default:
263         RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
264         goto err;
265     }
266     if (i <= 0)
267         goto err;
268
269     if (BN_bin2bn(buf, num, f) == NULL)
270         goto err;
271
272     if (BN_ucmp(f, rsa->n) >= 0) {
273         /* usually the padding functions would catch this */
274         RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
275                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
276         goto err;
277     }
278
279     if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
280         blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
281         if (blinding == NULL) {
282             RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
283             goto err;
284         }
285     }
286
287     if (blinding != NULL) {
288         if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
289             RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
290             goto err;
291         }
292         if (!rsa_blinding_convert(blinding, f, unblind, ctx))
293             goto err;
294     }
295
296     if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
297         ((rsa->p != NULL) &&
298          (rsa->q != NULL) &&
299          (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
300         if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
301             goto err;
302     } else {
303         BIGNUM *d = BN_new();
304         if (d == NULL) {
305             RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
306             goto err;
307         }
308         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
309
310         if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
311             if (!BN_MONT_CTX_set_locked
312                 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
313                 BN_free(d);
314                 goto err;
315             }
316
317         if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
318                                    rsa->_method_mod_n)) {
319             BN_free(d);
320             goto err;
321         }
322         /* We MUST free d before any further use of rsa->d */
323         BN_free(d);
324     }
325
326     if (blinding)
327         if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
328             goto err;
329
330     if (padding == RSA_X931_PADDING) {
331         BN_sub(f, rsa->n, ret);
332         if (BN_cmp(ret, f) > 0)
333             res = f;
334         else
335             res = ret;
336     } else
337         res = ret;
338
339     /*
340      * put in leading 0 bytes if the number is less than the length of the
341      * modulus
342      */
343     j = BN_num_bytes(res);
344     i = BN_bn2bin(res, &(to[num - j]));
345     for (k = 0; k < (num - i); k++)
346         to[k] = 0;
347
348     r = num;
349  err:
350     if (ctx != NULL)
351         BN_CTX_end(ctx);
352     BN_CTX_free(ctx);
353     OPENSSL_clear_free(buf, num);
354     return (r);
355 }
356
357 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
358                                    unsigned char *to, RSA *rsa, int padding)
359 {
360     BIGNUM *f, *ret;
361     int j, num = 0, r = -1;
362     unsigned char *p;
363     unsigned char *buf = NULL;
364     BN_CTX *ctx = NULL;
365     int local_blinding = 0;
366     /*
367      * Used only if the blinding structure is shared. A non-NULL unblind
368      * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
369      * the unblinding factor outside the blinding structure.
370      */
371     BIGNUM *unblind = NULL;
372     BN_BLINDING *blinding = NULL;
373
374     if ((ctx = BN_CTX_new()) == NULL)
375         goto err;
376     BN_CTX_start(ctx);
377     f = BN_CTX_get(ctx);
378     ret = BN_CTX_get(ctx);
379     num = BN_num_bytes(rsa->n);
380     buf = OPENSSL_malloc(num);
381     if (f == NULL || ret == NULL || buf == NULL) {
382         RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
383         goto err;
384     }
385
386     /*
387      * This check was for equality but PGP does evil things and chops off the
388      * top '0' bytes
389      */
390     if (flen > num) {
391         RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
392                RSA_R_DATA_GREATER_THAN_MOD_LEN);
393         goto err;
394     }
395
396     /* make data into a big number */
397     if (BN_bin2bn(from, (int)flen, f) == NULL)
398         goto err;
399
400     if (BN_ucmp(f, rsa->n) >= 0) {
401         RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
402                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
403         goto err;
404     }
405
406     if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
407         blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
408         if (blinding == NULL) {
409             RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
410             goto err;
411         }
412     }
413
414     if (blinding != NULL) {
415         if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
416             RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
417             goto err;
418         }
419         if (!rsa_blinding_convert(blinding, f, unblind, ctx))
420             goto err;
421     }
422
423     /* do the decrypt */
424     if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
425         ((rsa->p != NULL) &&
426          (rsa->q != NULL) &&
427          (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
428         if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
429             goto err;
430     } else {
431         BIGNUM *d = BN_new();
432         if (d == NULL) {
433             RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
434             goto err;
435         }
436         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
437
438         if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
439             if (!BN_MONT_CTX_set_locked
440                 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
441                 BN_free(d);
442                 goto err;
443             }
444         if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
445                                    rsa->_method_mod_n)) {
446             BN_free(d);
447             goto err;
448         }
449         /* We MUST free d before any further use of rsa->d */
450         BN_free(d);
451     }
452
453     if (blinding)
454         if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
455             goto err;
456
457     p = buf;
458     j = BN_bn2bin(ret, p);      /* j is only used with no-padding mode */
459
460     switch (padding) {
461     case RSA_PKCS1_PADDING:
462         r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
463         break;
464     case RSA_PKCS1_OAEP_PADDING:
465         r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
466         break;
467     case RSA_SSLV23_PADDING:
468         r = RSA_padding_check_SSLv23(to, num, buf, j, num);
469         break;
470     case RSA_NO_PADDING:
471         r = RSA_padding_check_none(to, num, buf, j, num);
472         break;
473     default:
474         RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
475         goto err;
476     }
477     if (r < 0)
478         RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
479
480  err:
481     if (ctx != NULL)
482         BN_CTX_end(ctx);
483     BN_CTX_free(ctx);
484     OPENSSL_clear_free(buf, num);
485     return (r);
486 }
487
488 /* signature verification */
489 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
490                                   unsigned char *to, RSA *rsa, int padding)
491 {
492     BIGNUM *f, *ret;
493     int i, num = 0, r = -1;
494     unsigned char *p;
495     unsigned char *buf = NULL;
496     BN_CTX *ctx = NULL;
497
498     if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
499         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
500         return -1;
501     }
502
503     if (BN_ucmp(rsa->n, rsa->e) <= 0) {
504         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
505         return -1;
506     }
507
508     /* for large moduli, enforce exponent limit */
509     if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
510         if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
511             RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
512             return -1;
513         }
514     }
515
516     if ((ctx = BN_CTX_new()) == NULL)
517         goto err;
518     BN_CTX_start(ctx);
519     f = BN_CTX_get(ctx);
520     ret = BN_CTX_get(ctx);
521     num = BN_num_bytes(rsa->n);
522     buf = OPENSSL_malloc(num);
523     if (f == NULL || ret == NULL || buf == NULL) {
524         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
525         goto err;
526     }
527
528     /*
529      * This check was for equality but PGP does evil things and chops off the
530      * top '0' bytes
531      */
532     if (flen > num) {
533         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
534         goto err;
535     }
536
537     if (BN_bin2bn(from, flen, f) == NULL)
538         goto err;
539
540     if (BN_ucmp(f, rsa->n) >= 0) {
541         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
542                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
543         goto err;
544     }
545
546     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
547         if (!BN_MONT_CTX_set_locked
548             (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
549             goto err;
550
551     if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
552                                rsa->_method_mod_n))
553         goto err;
554
555     if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
556         if (!BN_sub(ret, rsa->n, ret))
557             goto err;
558
559     p = buf;
560     i = BN_bn2bin(ret, p);
561
562     switch (padding) {
563     case RSA_PKCS1_PADDING:
564         r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
565         break;
566     case RSA_X931_PADDING:
567         r = RSA_padding_check_X931(to, num, buf, i, num);
568         break;
569     case RSA_NO_PADDING:
570         r = RSA_padding_check_none(to, num, buf, i, num);
571         break;
572     default:
573         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
574         goto err;
575     }
576     if (r < 0)
577         RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
578
579  err:
580     if (ctx != NULL)
581         BN_CTX_end(ctx);
582     BN_CTX_free(ctx);
583     OPENSSL_clear_free(buf, num);
584     return (r);
585 }
586
587 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
588 {
589     BIGNUM *r1, *m1, *vrfy;
590     int ret = 0;
591
592     BN_CTX_start(ctx);
593
594     r1 = BN_CTX_get(ctx);
595     m1 = BN_CTX_get(ctx);
596     vrfy = BN_CTX_get(ctx);
597
598     {
599         BIGNUM *p = BN_new(), *q = BN_new();
600
601         /*
602          * Make sure BN_mod_inverse in Montgomery initialization uses the
603          * BN_FLG_CONSTTIME flag
604          */
605         if (p == NULL || q == NULL) {
606             BN_free(p);
607             BN_free(q);
608             goto err;
609         }
610         BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
611         BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
612
613         if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
614             if (!BN_MONT_CTX_set_locked
615                 (&rsa->_method_mod_p, rsa->lock, p, ctx)
616                 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
617                                            rsa->lock, q, ctx)) {
618                 BN_free(p);
619                 BN_free(q);
620                 goto err;
621             }
622         }
623         /*
624          * We MUST free p and q before any further use of rsa->p and rsa->q
625          */
626         BN_free(p);
627         BN_free(q);
628     }
629
630     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
631         if (!BN_MONT_CTX_set_locked
632             (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
633             goto err;
634
635     /* compute I mod q */
636     {
637         BIGNUM *c = BN_new();
638         if (c == NULL)
639             goto err;
640         BN_with_flags(c, I, BN_FLG_CONSTTIME);
641
642         if (!BN_mod(r1, c, rsa->q, ctx)) {
643             BN_free(c);
644             goto err;
645         }
646
647         {
648             BIGNUM *dmq1 = BN_new();
649             if (dmq1 == NULL) {
650                 BN_free(c);
651                 goto err;
652             }
653             BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
654
655             /* compute r1^dmq1 mod q */
656             if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
657                 rsa->_method_mod_q)) {
658                 BN_free(c);
659                 BN_free(dmq1);
660                 goto err;
661             }
662             /* We MUST free dmq1 before any further use of rsa->dmq1 */
663             BN_free(dmq1);
664         }
665
666         /* compute I mod p */
667         if (!BN_mod(r1, c, rsa->p, ctx)) {
668             BN_free(c);
669             goto err;
670         }
671         /* We MUST free c before any further use of I */
672         BN_free(c);
673     }
674
675     {
676         BIGNUM *dmp1 = BN_new();
677         if (dmp1 == NULL)
678             goto err;
679         BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
680
681         /* compute r1^dmp1 mod p */
682         if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
683                                    rsa->_method_mod_p)) {
684             BN_free(dmp1);
685             goto err;
686         }
687         /* We MUST free dmp1 before any further use of rsa->dmp1 */
688         BN_free(dmp1);
689     }
690
691     if (!BN_sub(r0, r0, m1))
692         goto err;
693     /*
694      * This will help stop the size of r0 increasing, which does affect the
695      * multiply if it optimised for a power of 2 size
696      */
697     if (BN_is_negative(r0))
698         if (!BN_add(r0, r0, rsa->p))
699             goto err;
700
701     if (!BN_mul(r1, r0, rsa->iqmp, ctx))
702         goto err;
703
704     {
705         BIGNUM *pr1 = BN_new();
706         if (pr1 == NULL)
707             goto err;
708         BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
709
710         if (!BN_mod(r0, pr1, rsa->p, ctx)) {
711             BN_free(pr1);
712             goto err;
713         }
714         /* We MUST free pr1 before any further use of r1 */
715         BN_free(pr1);
716     }
717
718     /*
719      * If p < q it is occasionally possible for the correction of adding 'p'
720      * if r0 is negative above to leave the result still negative. This can
721      * break the private key operations: the following second correction
722      * should *always* correct this rare occurrence. This will *never* happen
723      * with OpenSSL generated keys because they ensure p > q [steve]
724      */
725     if (BN_is_negative(r0))
726         if (!BN_add(r0, r0, rsa->p))
727             goto err;
728     if (!BN_mul(r1, r0, rsa->q, ctx))
729         goto err;
730     if (!BN_add(r0, r1, m1))
731         goto err;
732
733     if (rsa->e && rsa->n) {
734         if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
735                                    rsa->_method_mod_n))
736             goto err;
737         /*
738          * If 'I' was greater than (or equal to) rsa->n, the operation will
739          * be equivalent to using 'I mod n'. However, the result of the
740          * verify will *always* be less than 'n' so we don't check for
741          * absolute equality, just congruency.
742          */
743         if (!BN_sub(vrfy, vrfy, I))
744             goto err;
745         if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
746             goto err;
747         if (BN_is_negative(vrfy))
748             if (!BN_add(vrfy, vrfy, rsa->n))
749                 goto err;
750         if (!BN_is_zero(vrfy)) {
751             /*
752              * 'I' and 'vrfy' aren't congruent mod n. Don't leak
753              * miscalculated CRT output, just do a raw (slower) mod_exp and
754              * return that instead.
755              */
756
757             BIGNUM *d = BN_new();
758             if (d == NULL)
759                 goto err;
760             BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
761
762             if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
763                                        rsa->_method_mod_n)) {
764                 BN_free(d);
765                 goto err;
766             }
767             /* We MUST free d before any further use of rsa->d */
768             BN_free(d);
769         }
770     }
771     ret = 1;
772  err:
773     BN_CTX_end(ctx);
774     return (ret);
775 }
776
777 static int rsa_ossl_init(RSA *rsa)
778 {
779     rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
780     return (1);
781 }
782
783 static int rsa_ossl_finish(RSA *rsa)
784 {
785     BN_MONT_CTX_free(rsa->_method_mod_n);
786     BN_MONT_CTX_free(rsa->_method_mod_p);
787     BN_MONT_CTX_free(rsa->_method_mod_q);
788     return (1);
789 }
790
791 #endif