Obtain lock CRYPTO_LOCK_RSA before creating BN_MONT_CTX
[oweals/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 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/bn.h>
62 #include <openssl/rsa.h>
63 #include <openssl/rand.h>
64
65 #ifndef RSA_NULL
66
67 static int RSA_eay_public_encrypt(int flen, unsigned char *from,
68                 unsigned char *to, RSA *rsa,int padding);
69 static int RSA_eay_private_encrypt(int flen, unsigned char *from,
70                 unsigned char *to, RSA *rsa,int padding);
71 static int RSA_eay_public_decrypt(int flen, unsigned char *from,
72                 unsigned char *to, RSA *rsa,int padding);
73 static int RSA_eay_private_decrypt(int flen, unsigned char *from,
74                 unsigned char *to, RSA *rsa,int padding);
75 static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *i, RSA *rsa);
76 static int RSA_eay_init(RSA *rsa);
77 static int RSA_eay_finish(RSA *rsa);
78 static RSA_METHOD rsa_pkcs1_eay_meth={
79         "Eric Young's PKCS#1 RSA",
80         RSA_eay_public_encrypt,
81         RSA_eay_public_decrypt,
82         RSA_eay_private_encrypt,
83         RSA_eay_private_decrypt,
84         RSA_eay_mod_exp,
85         BN_mod_exp_mont,
86         RSA_eay_init,
87         RSA_eay_finish,
88         0,
89         NULL,
90         };
91
92 RSA_METHOD *RSA_PKCS1_SSLeay(void)
93         {
94         return(&rsa_pkcs1_eay_meth);
95         }
96
97 static int RSA_eay_public_encrypt(int flen, unsigned char *from,
98              unsigned char *to, RSA *rsa, int padding)
99         {
100         BIGNUM f,ret;
101         int i,j,k,num=0,r= -1;
102         unsigned char *buf=NULL;
103         BN_CTX *ctx=NULL;
104
105         BN_init(&f);
106         BN_init(&ret);
107         if ((ctx=BN_CTX_new()) == NULL) goto err;
108         num=BN_num_bytes(rsa->n);
109         if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
110                 {
111                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
112                 goto err;
113                 }
114
115         switch (padding)
116                 {
117         case RSA_PKCS1_PADDING:
118                 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
119                 break;
120 #ifndef NO_SHA
121         case RSA_PKCS1_OAEP_PADDING:
122                 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
123                 break;
124 #endif
125         case RSA_SSLV23_PADDING:
126                 i=RSA_padding_add_SSLv23(buf,num,from,flen);
127                 break;
128         case RSA_NO_PADDING:
129                 i=RSA_padding_add_none(buf,num,from,flen);
130                 break;
131         default:
132                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
133                 goto err;
134                 }
135         if (i <= 0) goto err;
136
137         if (BN_bin2bn(buf,num,&f) == NULL) goto err;
138         
139         if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
140                 {
141                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
142                 if (rsa->_method_mod_n == NULL)
143                         {
144                         BN_MONT_CTX* bn_mont_ctx;
145                         if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
146                                 {
147                                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
148                                 goto err;
149                                 }
150                         if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
151                                 {
152                                 BN_MONT_CTX_free(bn_mont_ctx);
153                                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
154                                 goto err;
155                                 }
156                         rsa->_method_mod_n = bn_mont_ctx;
157                         }
158                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
159                 }
160
161         if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
162                 rsa->_method_mod_n)) goto err;
163
164         /* put in leading 0 bytes if the number is less than the
165          * length of the modulus */
166         j=BN_num_bytes(&ret);
167         i=BN_bn2bin(&ret,&(to[num-j]));
168         for (k=0; k<(num-i); k++)
169                 to[k]=0;
170
171         r=num;
172 err:
173         if (ctx != NULL) BN_CTX_free(ctx);
174         BN_clear_free(&f);
175         BN_clear_free(&ret);
176         if (buf != NULL) 
177                 {
178                 memset(buf,0,num);
179                 OPENSSL_free(buf);
180                 }
181         return(r);
182         }
183
184 static int RSA_eay_private_encrypt(int flen, unsigned char *from,
185              unsigned char *to, RSA *rsa, int padding)
186         {
187         BIGNUM f,ret;
188         int i,j,k,num=0,r= -1;
189         unsigned char *buf=NULL;
190         BN_CTX *ctx=NULL;
191
192         BN_init(&f);
193         BN_init(&ret);
194
195         if ((ctx=BN_CTX_new()) == NULL) goto err;
196         num=BN_num_bytes(rsa->n);
197         if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
198                 {
199                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
200                 goto err;
201                 }
202
203         switch (padding)
204                 {
205         case RSA_PKCS1_PADDING:
206                 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
207                 break;
208         case RSA_NO_PADDING:
209                 i=RSA_padding_add_none(buf,num,from,flen);
210                 break;
211         case RSA_SSLV23_PADDING:
212         default:
213                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
214                 goto err;
215                 }
216         if (i <= 0) goto err;
217
218         if (BN_bin2bn(buf,num,&f) == NULL) goto err;
219
220         if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
221                 RSA_blinding_on(rsa,ctx);
222         if (rsa->flags & RSA_FLAG_BLINDING)
223                 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
224
225         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
226                 ((rsa->p != NULL) &&
227                 (rsa->q != NULL) &&
228                 (rsa->dmp1 != NULL) &&
229                 (rsa->dmq1 != NULL) &&
230                 (rsa->iqmp != NULL)) )
231                 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
232         else
233                 {
234                 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
235                 }
236
237         if (rsa->flags & RSA_FLAG_BLINDING)
238                 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
239
240         /* put in leading 0 bytes if the number is less than the
241          * length of the modulus */
242         j=BN_num_bytes(&ret);
243         i=BN_bn2bin(&ret,&(to[num-j]));
244         for (k=0; k<(num-i); k++)
245                 to[k]=0;
246
247         r=num;
248 err:
249         if (ctx != NULL) BN_CTX_free(ctx);
250         BN_clear_free(&ret);
251         BN_clear_free(&f);
252         if (buf != NULL)
253                 {
254                 memset(buf,0,num);
255                 OPENSSL_free(buf);
256                 }
257         return(r);
258         }
259
260 static int RSA_eay_private_decrypt(int flen, unsigned char *from,
261              unsigned char *to, RSA *rsa, int padding)
262         {
263         BIGNUM f,ret;
264         int j,num=0,r= -1;
265         unsigned char *p;
266         unsigned char *buf=NULL;
267         BN_CTX *ctx=NULL;
268
269         BN_init(&f);
270         BN_init(&ret);
271         ctx=BN_CTX_new();
272         if (ctx == NULL) goto err;
273
274         num=BN_num_bytes(rsa->n);
275
276         if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
277                 {
278                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
279                 goto err;
280                 }
281
282         /* This check was for equality but PGP does evil things
283          * and chops off the top '0' bytes */
284         if (flen > num)
285                 {
286                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
287                 goto err;
288                 }
289
290         /* make data into a big number */
291         if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;
292
293         if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
294                 RSA_blinding_on(rsa,ctx);
295         if (rsa->flags & RSA_FLAG_BLINDING)
296                 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
297
298         /* do the decrypt */
299         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
300                 ((rsa->p != NULL) &&
301                 (rsa->q != NULL) &&
302                 (rsa->dmp1 != NULL) &&
303                 (rsa->dmq1 != NULL) &&
304                 (rsa->iqmp != NULL)) )
305                 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
306         else
307                 {
308                 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))
309                         goto err;
310                 }
311
312         if (rsa->flags & RSA_FLAG_BLINDING)
313                 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
314
315         p=buf;
316         j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
317
318         switch (padding)
319                 {
320         case RSA_PKCS1_PADDING:
321                 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
322                 break;
323 #ifndef NO_SHA
324         case RSA_PKCS1_OAEP_PADDING:
325                 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
326                 break;
327 #endif
328         case RSA_SSLV23_PADDING:
329                 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
330                 break;
331         case RSA_NO_PADDING:
332                 r=RSA_padding_check_none(to,num,buf,j,num);
333                 break;
334         default:
335                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
336                 goto err;
337                 }
338         if (r < 0)
339                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
340
341 err:
342         if (ctx != NULL) BN_CTX_free(ctx);
343         BN_clear_free(&f);
344         BN_clear_free(&ret);
345         if (buf != NULL)
346                 {
347                 memset(buf,0,num);
348                 OPENSSL_free(buf);
349                 }
350         return(r);
351         }
352
353 static int RSA_eay_public_decrypt(int flen, unsigned char *from,
354              unsigned char *to, RSA *rsa, int padding)
355         {
356         BIGNUM f,ret;
357         int i,num=0,r= -1;
358         unsigned char *p;
359         unsigned char *buf=NULL;
360         BN_CTX *ctx=NULL;
361
362         BN_init(&f);
363         BN_init(&ret);
364         ctx=BN_CTX_new();
365         if (ctx == NULL) goto err;
366
367         num=BN_num_bytes(rsa->n);
368         buf=(unsigned char *)OPENSSL_malloc(num);
369         if (buf == NULL)
370                 {
371                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
372                 goto err;
373                 }
374
375         /* This check was for equality but PGP does evil things
376          * and chops off the top '0' bytes */
377         if (flen > num)
378                 {
379                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
380                 goto err;
381                 }
382
383         if (BN_bin2bn(from,flen,&f) == NULL) goto err;
384         /* do the decrypt */
385         if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
386                 {
387                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
388                 if (rsa->_method_mod_n == NULL)
389                         {
390                         BN_MONT_CTX* bn_mont_ctx;
391                         if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
392                                 {
393                                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
394                                 goto err;
395                                 }
396                         if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
397                                 {
398                                 BN_MONT_CTX_free(bn_mont_ctx);
399                                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
400                                 goto err;
401                                 }
402                         rsa->_method_mod_n = bn_mont_ctx;
403                         }
404                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
405                 }
406
407         if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
408                 rsa->_method_mod_n)) goto err;
409
410         p=buf;
411         i=BN_bn2bin(&ret,p);
412
413         switch (padding)
414                 {
415         case RSA_PKCS1_PADDING:
416                 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
417                 break;
418         case RSA_NO_PADDING:
419                 r=RSA_padding_check_none(to,num,buf,i,num);
420                 break;
421         default:
422                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
423                 goto err;
424                 }
425         if (r < 0)
426                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
427
428 err:
429         if (ctx != NULL) BN_CTX_free(ctx);
430         BN_clear_free(&f);
431         BN_clear_free(&ret);
432         if (buf != NULL)
433                 {
434                 memset(buf,0,num);
435                 OPENSSL_free(buf);
436                 }
437         return(r);
438         }
439
440 static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
441         {
442         BIGNUM r1,m1;
443         int ret=0;
444         BN_CTX *ctx;
445
446         if ((ctx=BN_CTX_new()) == NULL) goto err;
447         BN_init(&m1);
448         BN_init(&r1);
449
450         if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
451                 {
452                 if (rsa->_method_mod_p == NULL)
453                         {
454                         CRYPTO_w_lock(CRYPTO_LOCK_RSA);
455                         if (rsa->_method_mod_p == NULL)
456                                 {
457                                 BN_MONT_CTX* bn_mont_ctx;
458                                 if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
459                                         {
460                                         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
461                                         goto err;
462                                         }
463                                 if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->p,ctx))
464                                         {
465                                         BN_MONT_CTX_free(bn_mont_ctx);
466                                         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
467                                         goto err;
468                                         }
469                                 rsa->_method_mod_p = bn_mont_ctx;
470                                 }
471                                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
472                         }
473                 if (rsa->_method_mod_q == NULL)
474                         {
475                         CRYPTO_w_lock(CRYPTO_LOCK_RSA);
476                         if (rsa->_method_mod_q == NULL)
477                                 {
478                                 BN_MONT_CTX* bn_mont_ctx;
479                                 if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
480                                         {
481                                         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
482                                         goto err;
483                                         }
484                                 if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->q,ctx))
485                                         {
486                                         BN_MONT_CTX_free(bn_mont_ctx);
487                                         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
488                                         goto err;
489                                         }
490                                 rsa->_method_mod_q = bn_mont_ctx;
491                                 }
492                         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
493                         }
494                 }
495
496         if (!BN_mod(&r1,I,rsa->q,ctx)) goto err;
497         if (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,
498                 rsa->_method_mod_q)) goto err;
499
500         if (!BN_mod(&r1,I,rsa->p,ctx)) goto err;
501         if (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,
502                 rsa->_method_mod_p)) goto err;
503
504         if (!BN_sub(r0,r0,&m1)) goto err;
505         /* This will help stop the size of r0 increasing, which does
506          * affect the multiply if it optimised for a power of 2 size */
507         if (r0->neg)
508                 if (!BN_add(r0,r0,rsa->p)) goto err;
509
510         if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
511         if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
512         /* If p < q it is occasionally possible for the correction of
513          * adding 'p' if r0 is negative above to leave the result still
514          * negative. This can break the private key operations: the following
515          * second correction should *always* correct this rare occurrence.
516          * This will *never* happen with OpenSSL generated keys because
517          * they ensure p > q [steve]
518          */
519         if (r0->neg)
520                 if (!BN_add(r0,r0,rsa->p)) goto err;
521         if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
522         if (!BN_add(r0,&r1,&m1)) goto err;
523
524         ret=1;
525 err:
526         BN_clear_free(&m1);
527         BN_clear_free(&r1);
528         BN_CTX_free(ctx);
529         return(ret);
530         }
531
532 static int RSA_eay_init(RSA *rsa)
533         {
534         rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
535         return(1);
536         }
537
538 static int RSA_eay_finish(RSA *rsa)
539         {
540         if (rsa->_method_mod_n != NULL)
541                 BN_MONT_CTX_free(rsa->_method_mod_n);
542         if (rsa->_method_mod_p != NULL)
543                 BN_MONT_CTX_free(rsa->_method_mod_p);
544         if (rsa->_method_mod_q != NULL)
545                 BN_MONT_CTX_free(rsa->_method_mod_q);
546         return(1);
547         }
548
549 #endif