1 /* crypto/rsa/rsa_eay.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
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);
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();
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,
101 RSA_METHOD *RSA_PKCS1_SSLeay()
103 return(&rsa_pkcs1_eay_meth);
106 static int RSA_eay_public_encrypt(flen, from, to, rsa, padding)
113 BIGNUM *f=NULL,*ret=NULL;
114 int i,j,k,num=0,r= -1;
115 unsigned char *buf=NULL;
118 if ((ctx=BN_CTX_new()) == NULL) goto err;
119 num=BN_num_bytes(rsa->n);
120 if ((buf=(unsigned char *)Malloc(num)) == NULL)
122 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
128 case RSA_PKCS1_PADDING:
129 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
131 case RSA_SSLV23_PADDING:
132 i=RSA_padding_add_SSLv23(buf,num,from,flen);
135 i=RSA_padding_add_none(buf,num,from,flen);
138 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
141 if (i <= 0) goto err;
143 if (((f=BN_new()) == NULL) || ((ret=BN_new()) == NULL)) goto err;
145 if (BN_bin2bn(buf,num,f) == NULL) goto err;
147 if ((rsa->method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
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;
154 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
155 rsa->method_mod_n)) goto err;
157 /* put in leading 0 bytes if the number is less than the
158 * length of the modulus */
160 i=BN_bn2bin(ret,&(to[num-j]));
161 for (k=0; k<(num-i); k++)
166 if (ctx != NULL) BN_CTX_free(ctx);
167 if (f != NULL) BN_free(f);
168 if (ret != NULL) BN_free(ret);
177 static int RSA_eay_private_encrypt(flen, from, to, rsa, padding)
184 BIGNUM *f=NULL,*ret=NULL;
185 int i,j,k,num=0,r= -1;
186 unsigned char *buf=NULL;
189 if ((ctx=BN_CTX_new()) == NULL) goto err;
190 num=BN_num_bytes(rsa->n);
191 if ((buf=(unsigned char *)Malloc(num)) == NULL)
193 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
199 case RSA_PKCS1_PADDING:
200 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
203 i=RSA_padding_add_none(buf,num,from,flen);
205 case RSA_SSLV23_PADDING:
207 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
210 if (i <= 0) goto err;
212 if (((f=BN_new()) == NULL) || ((ret=BN_new()) == NULL)) goto err;
213 if (BN_bin2bn(buf,num,f) == NULL) goto err;
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;
220 if ( (rsa->p != NULL) &&
222 (rsa->dmp1 != NULL) &&
223 (rsa->dmq1 != NULL) &&
225 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa)) goto err; }
228 if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx)) goto err;
231 if (rsa->flags & RSA_FLAG_BLINDING)
232 if (!BN_BLINDING_invert(ret,rsa->blinding,ctx)) goto err;
234 /* put in leading 0 bytes if the number is less than the
235 * length of the modulus */
237 i=BN_bn2bin(ret,&(to[num-j]));
238 for (k=0; k<(num-i); k++)
243 if (ctx != NULL) BN_CTX_free(ctx);
244 if (ret != NULL) BN_free(ret);
245 if (f != NULL) BN_free(f);
254 static int RSA_eay_private_decrypt(flen, from, to, rsa,padding)
261 BIGNUM *f=NULL,*ret=NULL;
264 unsigned char *buf=NULL;
268 if (ctx == NULL) goto err;
270 num=BN_num_bytes(rsa->n);
272 if ((buf=(unsigned char *)Malloc(num)) == NULL)
274 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
278 /* This check was for equallity but PGP does evil things
279 * and chops off the top '0' bytes */
282 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
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;
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;
296 if ( (rsa->p != NULL) &&
298 (rsa->dmp1 != NULL) &&
299 (rsa->dmq1 != NULL) &&
301 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa)) goto err; }
304 if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx))
308 if (rsa->flags & RSA_FLAG_BLINDING)
309 if (!BN_BLINDING_invert(ret,rsa->blinding,ctx)) goto err;
312 j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
316 case RSA_PKCS1_PADDING:
317 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j);
319 case RSA_SSLV23_PADDING:
320 r=RSA_padding_check_SSLv23(to,num,buf,j);
323 r=RSA_padding_check_none(to,num,buf,j);
326 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
330 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
333 if (ctx != NULL) BN_CTX_free(ctx);
334 if (f != NULL) BN_free(f);
335 if (ret != NULL) BN_free(ret);
344 static int RSA_eay_public_decrypt(flen, from, to, rsa, padding)
351 BIGNUM *f=NULL,*ret=NULL;
354 unsigned char *buf=NULL;
358 if (ctx == NULL) goto err;
360 num=BN_num_bytes(rsa->n);
361 buf=(unsigned char *)Malloc(num);
364 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
368 /* This check was for equallity but PGP does evil things
369 * and chops off the top '0' bytes */
372 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
376 /* make data into a big number */
377 if (((ret=BN_new()) == NULL) || ((f=BN_new()) == NULL)) goto err;
379 if (BN_bin2bn(from,flen,f) == NULL) goto err;
381 if ((rsa->method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
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;
388 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
389 rsa->method_mod_n)) goto err;
396 case RSA_PKCS1_PADDING:
397 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i);
400 r=RSA_padding_check_none(to,num,buf,i);
403 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
407 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
410 if (ctx != NULL) BN_CTX_free(ctx);
411 if (f != NULL) BN_free(f);
412 if (ret != NULL) BN_free(ret);
421 static int RSA_eay_mod_exp(r0, I, rsa)
426 BIGNUM *r1=NULL,*m1=NULL;
430 if ((ctx=BN_CTX_new()) == NULL) goto err;
433 if ((m1 == NULL) || (r1 == NULL)) goto err;
435 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
437 if (rsa->method_mod_p == NULL)
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))
445 if (rsa->method_mod_q == NULL)
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))
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;
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;
463 if (!BN_add(r1,r0,rsa->p)) goto err;
464 if (!BN_sub(r0,r1,m1)) goto err;
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;
473 if (m1 != NULL) BN_free(m1);
474 if (r1 != NULL) BN_free(r1);
479 static int RSA_eay_init(rsa)
482 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
486 static int RSA_eay_finish(rsa)
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);