free NULL cleanup
[openssl.git] / crypto / rsa / rsa_eay.c
1 /* crypto/rsa/rsa_eay.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111
112 #include "cryptlib.h"
113 #include "internal/bn_int.h"
114 #include <openssl/rsa.h>
115 #include <openssl/rand.h>
116
117 #ifndef RSA_NULL
118
119 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
120                                   unsigned char *to, RSA *rsa, int padding);
121 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
122                                    unsigned char *to, RSA *rsa, int padding);
123 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
124                                   unsigned char *to, RSA *rsa, int padding);
125 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
126                                    unsigned char *to, RSA *rsa, int padding);
127 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
128                            BN_CTX *ctx);
129 static int RSA_eay_init(RSA *rsa);
130 static int RSA_eay_finish(RSA *rsa);
131 static RSA_METHOD rsa_pkcs1_eay_meth = {
132     "Eric Young's PKCS#1 RSA",
133     RSA_eay_public_encrypt,
134     RSA_eay_public_decrypt,     /* signature verification */
135     RSA_eay_private_encrypt,    /* signing */
136     RSA_eay_private_decrypt,
137     RSA_eay_mod_exp,
138     BN_mod_exp_mont,            /* XXX probably we should not use Montgomery
139                                  * if e == 3 */
140     RSA_eay_init,
141     RSA_eay_finish,
142     RSA_FLAG_FIPS_METHOD,       /* flags */
143     NULL,
144     0,                          /* rsa_sign */
145     0,                          /* rsa_verify */
146     NULL                        /* rsa_keygen */
147 };
148
149 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
150 {
151     return (&rsa_pkcs1_eay_meth);
152 }
153
154 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
155                                   unsigned char *to, RSA *rsa, int padding)
156 {
157     BIGNUM *f, *ret;
158     int i, j, k, num = 0, r = -1;
159     unsigned char *buf = NULL;
160     BN_CTX *ctx = NULL;
161
162     if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
163         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
164         return -1;
165     }
166
167     if (BN_ucmp(rsa->n, rsa->e) <= 0) {
168         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
169         return -1;
170     }
171
172     /* for large moduli, enforce exponent limit */
173     if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
174         if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
175             RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
176             return -1;
177         }
178     }
179
180     if ((ctx = BN_CTX_new()) == NULL)
181         goto err;
182     BN_CTX_start(ctx);
183     f = BN_CTX_get(ctx);
184     ret = BN_CTX_get(ctx);
185     num = BN_num_bytes(rsa->n);
186     buf = OPENSSL_malloc(num);
187     if (!f || !ret || !buf) {
188         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
189         goto err;
190     }
191
192     switch (padding) {
193     case RSA_PKCS1_PADDING:
194         i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
195         break;
196     case RSA_PKCS1_OAEP_PADDING:
197         i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
198         break;
199     case RSA_SSLV23_PADDING:
200         i = RSA_padding_add_SSLv23(buf, num, from, flen);
201         break;
202     case RSA_NO_PADDING:
203         i = RSA_padding_add_none(buf, num, from, flen);
204         break;
205     default:
206         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
207         goto err;
208     }
209     if (i <= 0)
210         goto err;
211
212     if (BN_bin2bn(buf, num, f) == NULL)
213         goto err;
214
215     if (BN_ucmp(f, rsa->n) >= 0) {
216         /* usually the padding functions would catch this */
217         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,
218                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
219         goto err;
220     }
221
222     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
223         if (!BN_MONT_CTX_set_locked
224             (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
225             goto err;
226
227     if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
228                                rsa->_method_mod_n))
229         goto err;
230
231     /*
232      * put in leading 0 bytes if the number is less than the length of the
233      * modulus
234      */
235     j = BN_num_bytes(ret);
236     i = BN_bn2bin(ret, &(to[num - j]));
237     for (k = 0; k < (num - i); k++)
238         to[k] = 0;
239
240     r = num;
241  err:
242     if (ctx != NULL) {
243         BN_CTX_end(ctx);
244         BN_CTX_free(ctx);
245     }
246     if (buf != NULL) {
247         OPENSSL_cleanse(buf, num);
248         OPENSSL_free(buf);
249     }
250     return (r);
251 }
252
253 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
254 {
255     BN_BLINDING *ret;
256     int got_write_lock = 0;
257     CRYPTO_THREADID cur;
258
259     CRYPTO_r_lock(CRYPTO_LOCK_RSA);
260
261     if (rsa->blinding == NULL) {
262         CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
263         CRYPTO_w_lock(CRYPTO_LOCK_RSA);
264         got_write_lock = 1;
265
266         if (rsa->blinding == NULL)
267             rsa->blinding = RSA_setup_blinding(rsa, ctx);
268     }
269
270     ret = rsa->blinding;
271     if (ret == NULL)
272         goto err;
273
274     CRYPTO_THREADID_current(&cur);
275     if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) {
276         /* rsa->blinding is ours! */
277
278         *local = 1;
279     } else {
280         /* resort to rsa->mt_blinding instead */
281
282         /*
283          * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
284          * BN_BLINDING is shared, meaning that accesses require locks, and
285          * that the blinding factor must be stored outside the BN_BLINDING
286          */
287         *local = 0;
288
289         if (rsa->mt_blinding == NULL) {
290             if (!got_write_lock) {
291                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
292                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
293                 got_write_lock = 1;
294             }
295
296             if (rsa->mt_blinding == NULL)
297                 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
298         }
299         ret = rsa->mt_blinding;
300     }
301
302  err:
303     if (got_write_lock)
304         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
305     else
306         CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
307     return ret;
308 }
309
310 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
311                                 BN_CTX *ctx)
312 {
313     if (unblind == NULL)
314         /*
315          * Local blinding: store the unblinding factor in BN_BLINDING.
316          */
317         return BN_BLINDING_convert_ex(f, NULL, b, ctx);
318     else {
319         /*
320          * Shared blinding: store the unblinding factor outside BN_BLINDING.
321          */
322         int ret;
323         CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
324         ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
325         CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
326         return ret;
327     }
328 }
329
330 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
331                                BN_CTX *ctx)
332 {
333     /*
334      * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
335      * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
336      * is shared between threads, unblind must be non-null:
337      * BN_BLINDING_invert_ex will then use the local unblinding factor, and
338      * will only read the modulus from BN_BLINDING. In both cases it's safe
339      * to access the blinding without a lock.
340      */
341     return BN_BLINDING_invert_ex(f, unblind, b, ctx);
342 }
343
344 /* signing */
345 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
346                                    unsigned char *to, RSA *rsa, int padding)
347 {
348     BIGNUM *f, *ret, *res;
349     int i, j, k, num = 0, r = -1;
350     unsigned char *buf = NULL;
351     BN_CTX *ctx = NULL;
352     int local_blinding = 0;
353     /*
354      * Used only if the blinding structure is shared. A non-NULL unblind
355      * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
356      * the unblinding factor outside the blinding structure.
357      */
358     BIGNUM *unblind = NULL;
359     BN_BLINDING *blinding = NULL;
360
361     if ((ctx = BN_CTX_new()) == NULL)
362         goto err;
363     BN_CTX_start(ctx);
364     f = BN_CTX_get(ctx);
365     ret = BN_CTX_get(ctx);
366     num = BN_num_bytes(rsa->n);
367     buf = OPENSSL_malloc(num);
368     if (!f || !ret || !buf) {
369         RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
370         goto err;
371     }
372
373     switch (padding) {
374     case RSA_PKCS1_PADDING:
375         i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
376         break;
377     case RSA_X931_PADDING:
378         i = RSA_padding_add_X931(buf, num, from, flen);
379         break;
380     case RSA_NO_PADDING:
381         i = RSA_padding_add_none(buf, num, from, flen);
382         break;
383     case RSA_SSLV23_PADDING:
384     default:
385         RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
386         goto err;
387     }
388     if (i <= 0)
389         goto err;
390
391     if (BN_bin2bn(buf, num, f) == NULL)
392         goto err;
393
394     if (BN_ucmp(f, rsa->n) >= 0) {
395         /* usually the padding functions would catch this */
396         RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
397                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
398         goto err;
399     }
400
401     if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
402         blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
403         if (blinding == NULL) {
404             RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
405             goto err;
406         }
407     }
408
409     if (blinding != NULL) {
410         if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
411             RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
412             goto err;
413         }
414         if (!rsa_blinding_convert(blinding, f, unblind, ctx))
415             goto err;
416     }
417
418     if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
419         ((rsa->p != NULL) &&
420          (rsa->q != NULL) &&
421          (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
422         if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
423             goto err;
424     } else {
425         BIGNUM *d = NULL, *local_d = NULL;
426
427         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
428             local_d = d = BN_new();
429             if (!d) {
430                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
431                 goto err;
432             }
433             BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
434         } else
435             d = rsa->d;
436
437         if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
438             if (!BN_MONT_CTX_set_locked
439                 (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {
440                 if (local_d)
441                     BN_free(local_d);
442                 goto err;
443             }
444
445         if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
446                                    rsa->_method_mod_n)) {
447             if (local_d)
448                 BN_free(local_d);
449             goto err;
450         }
451         if (local_d)
452             BN_free(local_d);
453     }
454
455     if (blinding)
456         if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
457             goto err;
458
459     if (padding == RSA_X931_PADDING) {
460         BN_sub(f, rsa->n, ret);
461         if (BN_cmp(ret, f) > 0)
462             res = f;
463         else
464             res = ret;
465     } else
466         res = ret;
467
468     /*
469      * put in leading 0 bytes if the number is less than the length of the
470      * modulus
471      */
472     j = BN_num_bytes(res);
473     i = BN_bn2bin(res, &(to[num - j]));
474     for (k = 0; k < (num - i); k++)
475         to[k] = 0;
476
477     r = num;
478  err:
479     if (ctx != NULL) {
480         BN_CTX_end(ctx);
481         BN_CTX_free(ctx);
482     }
483     if (buf != NULL) {
484         OPENSSL_cleanse(buf, num);
485         OPENSSL_free(buf);
486     }
487     return (r);
488 }
489
490 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
491                                    unsigned char *to, RSA *rsa, int padding)
492 {
493     BIGNUM *f, *ret;
494     int j, num = 0, r = -1;
495     unsigned char *p;
496     unsigned char *buf = NULL;
497     BN_CTX *ctx = NULL;
498     int local_blinding = 0;
499     /*
500      * Used only if the blinding structure is shared. A non-NULL unblind
501      * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
502      * the unblinding factor outside the blinding structure.
503      */
504     BIGNUM *unblind = NULL;
505     BN_BLINDING *blinding = NULL;
506
507     if ((ctx = BN_CTX_new()) == NULL)
508         goto err;
509     BN_CTX_start(ctx);
510     f = BN_CTX_get(ctx);
511     ret = BN_CTX_get(ctx);
512     num = BN_num_bytes(rsa->n);
513     buf = OPENSSL_malloc(num);
514     if (!f || !ret || !buf) {
515         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
516         goto err;
517     }
518
519     /*
520      * This check was for equality but PGP does evil things and chops off the
521      * top '0' bytes
522      */
523     if (flen > num) {
524         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
525                RSA_R_DATA_GREATER_THAN_MOD_LEN);
526         goto err;
527     }
528
529     /* make data into a big number */
530     if (BN_bin2bn(from, (int)flen, f) == NULL)
531         goto err;
532
533     if (BN_ucmp(f, rsa->n) >= 0) {
534         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
535                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
536         goto err;
537     }
538
539     if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
540         blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
541         if (blinding == NULL) {
542             RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
543             goto err;
544         }
545     }
546
547     if (blinding != NULL) {
548         if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
549             RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
550             goto err;
551         }
552         if (!rsa_blinding_convert(blinding, f, unblind, ctx))
553             goto err;
554     }
555
556     /* do the decrypt */
557     if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
558         ((rsa->p != NULL) &&
559          (rsa->q != NULL) &&
560          (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
561         if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
562             goto err;
563     } else {
564         BIGNUM *d = NULL, *local_d = NULL;
565
566         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
567             local_d = d = BN_new();
568             if (!d) {
569                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
570                 goto err;
571             }
572             BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
573         } else
574             d = rsa->d;
575
576         if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
577             if (!BN_MONT_CTX_set_locked
578                 (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {
579                 if (local_d)
580                     BN_free(local_d);
581                 goto err;
582             }
583         if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
584                                    rsa->_method_mod_n)) {
585             if (local_d)
586                 BN_free(local_d);
587             goto err;
588         }
589         if (local_d)
590             BN_free(local_d);
591     }
592
593     if (blinding)
594         if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
595             goto err;
596
597     p = buf;
598     j = BN_bn2bin(ret, p);      /* j is only used with no-padding mode */
599
600     switch (padding) {
601     case RSA_PKCS1_PADDING:
602         r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
603         break;
604     case RSA_PKCS1_OAEP_PADDING:
605         r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
606         break;
607     case RSA_SSLV23_PADDING:
608         r = RSA_padding_check_SSLv23(to, num, buf, j, num);
609         break;
610     case RSA_NO_PADDING:
611         r = RSA_padding_check_none(to, num, buf, j, num);
612         break;
613     default:
614         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
615         goto err;
616     }
617     if (r < 0)
618         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
619
620  err:
621     if (ctx != NULL) {
622         BN_CTX_end(ctx);
623         BN_CTX_free(ctx);
624     }
625     if (buf != NULL) {
626         OPENSSL_cleanse(buf, num);
627         OPENSSL_free(buf);
628     }
629     return (r);
630 }
631
632 /* signature verification */
633 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
634                                   unsigned char *to, RSA *rsa, int padding)
635 {
636     BIGNUM *f, *ret;
637     int i, num = 0, r = -1;
638     unsigned char *p;
639     unsigned char *buf = NULL;
640     BN_CTX *ctx = NULL;
641
642     if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
643         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
644         return -1;
645     }
646
647     if (BN_ucmp(rsa->n, rsa->e) <= 0) {
648         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
649         return -1;
650     }
651
652     /* for large moduli, enforce exponent limit */
653     if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
654         if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
655             RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
656             return -1;
657         }
658     }
659
660     if ((ctx = BN_CTX_new()) == NULL)
661         goto err;
662     BN_CTX_start(ctx);
663     f = BN_CTX_get(ctx);
664     ret = BN_CTX_get(ctx);
665     num = BN_num_bytes(rsa->n);
666     buf = OPENSSL_malloc(num);
667     if (!f || !ret || !buf) {
668         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
669         goto err;
670     }
671
672     /*
673      * This check was for equality but PGP does evil things and chops off the
674      * top '0' bytes
675      */
676     if (flen > num) {
677         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
678         goto err;
679     }
680
681     if (BN_bin2bn(from, flen, f) == NULL)
682         goto err;
683
684     if (BN_ucmp(f, rsa->n) >= 0) {
685         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,
686                RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
687         goto err;
688     }
689
690     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
691         if (!BN_MONT_CTX_set_locked
692             (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
693             goto err;
694
695     if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
696                                rsa->_method_mod_n))
697         goto err;
698
699     if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
700         if (!BN_sub(ret, rsa->n, ret))
701             goto err;
702
703     p = buf;
704     i = BN_bn2bin(ret, p);
705
706     switch (padding) {
707     case RSA_PKCS1_PADDING:
708         r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
709         break;
710     case RSA_X931_PADDING:
711         r = RSA_padding_check_X931(to, num, buf, i, num);
712         break;
713     case RSA_NO_PADDING:
714         r = RSA_padding_check_none(to, num, buf, i, num);
715         break;
716     default:
717         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
718         goto err;
719     }
720     if (r < 0)
721         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
722
723  err:
724     if (ctx != NULL) {
725         BN_CTX_end(ctx);
726         BN_CTX_free(ctx);
727     }
728     if (buf != NULL) {
729         OPENSSL_cleanse(buf, num);
730         OPENSSL_free(buf);
731     }
732     return (r);
733 }
734
735 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
736 {
737     BIGNUM *r1, *m1, *vrfy;
738     BIGNUM *local_dmp1, *local_dmq1, *local_c, *local_r1;
739     BIGNUM *dmp1, *dmq1, *c, *pr1;
740     int ret = 0;
741
742     local_dmp1 = BN_new();
743     local_dmq1 = BN_new();
744     local_c = BN_new();
745     local_r1 = BN_new();
746     if (!local_dmp1 || !local_dmq1 || !local_c || !local_r1)
747         goto err;
748
749     BN_CTX_start(ctx);
750     r1 = BN_CTX_get(ctx);
751     m1 = BN_CTX_get(ctx);
752     vrfy = BN_CTX_get(ctx);
753
754     {
755         BIGNUM *local_p = NULL, *local_q = NULL;
756         BIGNUM *p = NULL, *q = NULL;
757
758         /*
759          * Make sure BN_mod_inverse in Montgomery intialization uses the
760          * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
761          */
762         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
763             local_p = p = BN_new();
764             if (!p)
765                 goto err;
766             BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
767
768             local_q = q = BN_new();
769             if (!q) {
770                 BN_free(local_p);
771                 goto err;
772             }
773             BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
774         } else {
775             p = rsa->p;
776             q = rsa->q;
777         }
778
779         if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
780             if (!BN_MONT_CTX_set_locked
781                 (&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)
782                 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
783                                            CRYPTO_LOCK_RSA, q, ctx)) {
784                 if (local_p)
785                     BN_free(local_p);
786                 if (local_q)
787                     BN_free(local_q);
788                 goto err;
789             }
790         }
791         if (local_p)
792             BN_free(local_p);
793         if (local_q)
794             BN_free(local_q);
795     }
796
797     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
798         if (!BN_MONT_CTX_set_locked
799             (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
800             goto err;
801
802     /* compute I mod q */
803     if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
804         c = local_c;
805         BN_with_flags(c, I, BN_FLG_CONSTTIME);
806         if (!BN_mod(r1, c, rsa->q, ctx))
807             goto err;
808     } else {
809         if (!BN_mod(r1, I, rsa->q, ctx))
810             goto err;
811     }
812
813     /* compute r1^dmq1 mod q */
814     if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
815         dmq1 = local_dmq1;
816         BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
817     } else
818         dmq1 = rsa->dmq1;
819     if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q))
820         goto err;
821
822     /* compute I mod p */
823     if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
824         c = local_c;
825         BN_with_flags(c, I, BN_FLG_CONSTTIME);
826         if (!BN_mod(r1, c, rsa->p, ctx))
827             goto err;
828     } else {
829         if (!BN_mod(r1, I, rsa->p, ctx))
830             goto err;
831     }
832
833     /* compute r1^dmp1 mod p */
834     if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
835         dmp1 = local_dmp1;
836         BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
837     } else
838         dmp1 = rsa->dmp1;
839     if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p))
840         goto err;
841
842     if (!BN_sub(r0, r0, m1))
843         goto err;
844     /*
845      * This will help stop the size of r0 increasing, which does affect the
846      * multiply if it optimised for a power of 2 size
847      */
848     if (BN_is_negative(r0))
849         if (!BN_add(r0, r0, rsa->p))
850             goto err;
851
852     if (!BN_mul(r1, r0, rsa->iqmp, ctx))
853         goto err;
854
855     /* Turn BN_FLG_CONSTTIME flag on before division operation */
856     if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
857         pr1 = local_r1;
858         BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
859     } else
860         pr1 = r1;
861     if (!BN_mod(r0, pr1, rsa->p, ctx))
862         goto err;
863
864     /*
865      * If p < q it is occasionally possible for the correction of adding 'p'
866      * if r0 is negative above to leave the result still negative. This can
867      * break the private key operations: the following second correction
868      * should *always* correct this rare occurrence. This will *never* happen
869      * with OpenSSL generated keys because they ensure p > q [steve]
870      */
871     if (BN_is_negative(r0))
872         if (!BN_add(r0, r0, rsa->p))
873             goto err;
874     if (!BN_mul(r1, r0, rsa->q, ctx))
875         goto err;
876     if (!BN_add(r0, r1, m1))
877         goto err;
878
879     if (rsa->e && rsa->n) {
880         if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
881                                    rsa->_method_mod_n))
882             goto err;
883         /*
884          * If 'I' was greater than (or equal to) rsa->n, the operation will
885          * be equivalent to using 'I mod n'. However, the result of the
886          * verify will *always* be less than 'n' so we don't check for
887          * absolute equality, just congruency.
888          */
889         if (!BN_sub(vrfy, vrfy, I))
890             goto err;
891         if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
892             goto err;
893         if (BN_is_negative(vrfy))
894             if (!BN_add(vrfy, vrfy, rsa->n))
895                 goto err;
896         if (!BN_is_zero(vrfy)) {
897             /*
898              * 'I' and 'vrfy' aren't congruent mod n. Don't leak
899              * miscalculated CRT output, just do a raw (slower) mod_exp and
900              * return that instead.
901              */
902
903             BIGNUM *local_d = NULL;
904             BIGNUM *d = NULL;
905
906             if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
907                 local_d = d = BN_new();
908                 if (!d)
909                     goto err;
910                 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
911             } else
912                 d = rsa->d;
913             if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
914                                        rsa->_method_mod_n)) {
915                 if (local_d)
916                     BN_free(local_d);
917                 goto err;
918             }
919
920             if (local_d)
921                 BN_free(local_d);
922         }
923     }
924     ret = 1;
925  err:
926     if (local_dmp1)
927         BN_free(local_dmp1);
928     if (local_dmq1)
929         BN_free(local_dmq1);
930     if (local_c)
931         BN_free(local_c);
932     if (local_r1)
933         BN_free(local_r1);
934     BN_CTX_end(ctx);
935     return (ret);
936 }
937
938 static int RSA_eay_init(RSA *rsa)
939 {
940     rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
941     return (1);
942 }
943
944 static int RSA_eay_finish(RSA *rsa)
945 {
946     if (rsa->_method_mod_n != NULL)
947         BN_MONT_CTX_free(rsa->_method_mod_n);
948     if (rsa->_method_mod_p != NULL)
949         BN_MONT_CTX_free(rsa->_method_mod_p);
950     if (rsa->_method_mod_q != NULL)
951         BN_MONT_CTX_free(rsa->_method_mod_q);
952     return (1);
953 }
954
955 #endif