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