1 /* crypto/rsa/rsa_enc.c */
2 /* Copyright (C) 1995-1997 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);
76 static int RSA_eay_public_encrypt();
77 static int RSA_eay_private_encrypt();
78 static int RSA_eay_public_decrypt();
79 static int RSA_eay_private_decrypt();
80 static int RSA_eay_mod_exp();
83 static RSA_METHOD rsa_pkcs1_eay_meth={
84 "Eric Young's PKCS#1 RSA",
85 RSA_eay_public_encrypt,
86 RSA_eay_public_decrypt,
87 RSA_eay_private_encrypt,
88 RSA_eay_private_decrypt,
95 RSA_METHOD *RSA_PKCS1_SSLeay()
97 return(&rsa_pkcs1_eay_meth);
100 static int RSA_eay_public_encrypt(flen, from, to, rsa, padding)
107 BIGNUM *f=NULL,*ret=NULL;
108 int i,j,k,num=0,r= -1;
110 unsigned char *buf=NULL;
113 if ( (padding != RSA_PKCS1_PADDING) &&
114 (padding != RSA_SSLV23_PADDING))
116 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
121 if (ctx == NULL) goto err;
123 num=BN_num_bytes(rsa->n);
126 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
130 buf=(unsigned char *)Malloc(num);
133 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
136 p=(unsigned char *)buf;
139 *(p++)=2; /* Public Key BT (Block Type) */
141 /* pad out with non-zero random data */
150 } while (*p == '\0');
154 if (padding == RSA_SSLV23_PADDING)
155 memset(&(p[-8]),3,8);
159 memcpy(p,from,(unsigned int)flen);
163 if ((f == NULL) || (ret == NULL)) goto err;
165 if (BN_bin2bn(buf,num,f) == NULL) goto err;
166 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx)) goto err;
168 /* put in leading 0 bytes if the number is less than the
169 * length of the modulus */
171 i=BN_bn2bin(ret,&(to[num-j]));
172 for (k=0; k<(num-i); k++)
177 if (ctx != NULL) BN_CTX_free(ctx);
178 if (f != NULL) BN_free(f);
179 if (ret != NULL) BN_free(ret);
188 static int RSA_eay_private_encrypt(flen, from, to, rsa, padding)
195 BIGNUM *f=NULL,*ret=NULL;
196 int i,j,k,num=0,r= -1;
198 unsigned char *buf=NULL;
201 if (padding != RSA_PKCS1_PADDING)
203 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
208 if (ctx == NULL) goto err;
210 num=BN_num_bytes(rsa->n);
213 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
216 buf=(unsigned char *)Malloc(num);
219 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
225 *(p++)=1; /* Private Key BT (Block Type) */
227 /* padd out with 0xff data */
232 memcpy(p,from,(unsigned int)flen);
235 if ((ret == NULL) || (f == NULL)) goto err;
236 if (BN_bin2bn(buf,num,f) == NULL) goto err;
237 if ( (rsa->p != NULL) &&
239 (rsa->dmp1 != NULL) &&
240 (rsa->dmq1 != NULL) &&
242 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa)) goto err; }
244 { if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx)) goto err; }
249 /* put in leading 0 bytes if the number is less than the
250 * length of the modulus */
252 i=BN_bn2bin(ret,&(to[num-j]));
253 for (k=0; k<(num-i); k++)
258 if (ctx != NULL) BN_CTX_free(ctx);
259 if (ret != NULL) BN_free(ret);
260 if (f != NULL) BN_free(f);
269 static int RSA_eay_private_decrypt(flen, from, to, rsa,padding)
276 BIGNUM *f=NULL,*ret=NULL;
279 unsigned char *buf=NULL;
282 if ((padding != RSA_PKCS1_PADDING) && (padding != RSA_SSLV23_PADDING))
284 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
289 if (ctx == NULL) goto err;
291 num=BN_num_bytes(rsa->n);
293 buf=(unsigned char *)Malloc(num);
296 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
300 /* This check was for equallity but PGP does evil things
301 * and chops off the top '0' bytes */
304 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
308 /* make data into a big number */
311 if ((ret == NULL) || (f == NULL)) goto err;
312 if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
314 if ( (rsa->p != NULL) &&
316 (rsa->dmp1 != NULL) &&
317 (rsa->dmq1 != NULL) &&
319 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa)) goto err; }
321 { if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx)) goto err; }
329 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_BLOCK_TYPE_IS_NOT_02);
333 /* scan over padding data */
334 j=num-2; /* one for type and one for the prepended 0. */
336 if (*(p++) == 0) break;
340 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_NULL_BEFORE_BLOCK_MISSING);
346 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_BAD_PAD_BYTE_COUNT);
356 fprintf(stderr,"\n");
357 for (z=0; z<8; z++) fprintf(stderr,"%02X",q[z]);
358 fprintf(stderr,"\n");
362 if (padding == RSA_SSLV23_PADDING)
367 /* -9 because we have jumped the '\0' */
376 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_SSLV3_ROLLBACK_ATTACK);
381 /* skip over the '\0' */
386 memcpy(to,p,(unsigned int)j);
389 if (ctx != NULL) BN_CTX_free(ctx);
390 if (f != NULL) BN_free(f);
391 if (ret != NULL) BN_free(ret);
400 static int RSA_eay_public_decrypt(flen, from, to, rsa, padding)
407 BIGNUM *f=NULL,*ret=NULL;
410 unsigned char *buf=NULL;
413 if (padding != RSA_PKCS1_PADDING)
415 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
420 if (ctx == NULL) goto err;
422 num=BN_num_bytes(rsa->n);
423 buf=(unsigned char *)Malloc(num);
426 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
430 /* This check was for equallity but PGP does evil things
431 * and chops off the top '0' bytes */
434 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
438 /* make data into a big number */
441 if ((f == NULL) || (ret == NULL)) goto err;
443 if (BN_bin2bn(from,flen,f) == NULL) goto err;
445 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx)) goto err;
453 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_BLOCK_TYPE_IS_NOT_01);
457 /* scan over padding data */
458 j=num-2; /* one for type and one for the prepended 0. */
461 if (*p != 0xff) /* should decrypt to 0xff */
466 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_BAD_FIXED_HEADER_DECRYPT);
474 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_NULL_BEFORE_BLOCK_MISSING);
479 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_BAD_PAD_BYTE_COUNT);
483 /* skip over the '\0' */
488 memcpy(to,p,(unsigned int)j);
491 if (ctx != NULL) BN_CTX_free(ctx);
492 if (f != NULL) BN_free(f);
493 if (ret != NULL) BN_free(ret);
502 static int RSA_eay_mod_exp(r0, I, rsa)
507 BIGNUM *r1=NULL,*m1=NULL;
511 if ((ctx=BN_CTX_new()) == NULL) goto err;
514 if ((m1 == NULL) || (r1 == NULL)) goto err;
516 if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
517 if (!rsa->meth->bn_mod_exp(m1,r1,rsa->dmq1,rsa->q,ctx)) goto err;
519 if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
520 if (!rsa->meth->bn_mod_exp(r0,r1,rsa->dmp1,rsa->p,ctx)) goto err;
522 if (!BN_add(r1,r0,rsa->p)) goto err;
523 if (!BN_sub(r0,r1,m1)) goto err;
525 if (!BN_mul(r1,r0,rsa->iqmp)) goto err;
526 if (!BN_mod(r0,r1,rsa->p,ctx)) goto err;
527 if (!BN_mul(r1,r0,rsa->q)) goto err;
528 if (!BN_add(r0,r1,m1)) goto err;
532 if (m1 != NULL) BN_free(m1);
533 if (r1 != NULL) BN_free(r1);