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