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 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112 #include <stdio.h>
113 #include "cryptlib.h"
114 #include <openssl/bn.h>
115 #include <openssl/rsa.h>
116 #include <openssl/rand.h>
117
118 #ifndef RSA_NULL
119
120 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
121 unsigned char *to, RSA *rsa,int padding);
122 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
123 unsigned char *to, RSA *rsa,int padding);
124 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
125 unsigned char *to, RSA *rsa,int padding);
126 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
127 unsigned char *to, RSA *rsa,int padding);
128 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
129 static int RSA_eay_init(RSA *rsa);
130 static int RSA_eay_finish(RSA *rsa);
131 static RSA_METHOD rsa_pkcs1_eay_meth={
132 "Eric Young's PKCS#1 RSA",
133 RSA_eay_public_encrypt,
134 RSA_eay_public_decrypt, /* signature verification */
135 RSA_eay_private_encrypt, /* signing */
136 RSA_eay_private_decrypt,
137 RSA_eay_mod_exp,
138 BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
139 RSA_eay_init,
140 RSA_eay_finish,
141 0, /* flags */
142 NULL,
143 0, /* rsa_sign */
144 0, /* rsa_verify */
145 NULL /* rsa_keygen */
146 };
147
RSA_PKCS1_SSLeay(void)148 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
149 {
150 return(&rsa_pkcs1_eay_meth);
151 }
152
153 /* Usage example;
154 * MONT_HELPER(rsa, bn_ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
155 */
156 #define MONT_HELPER(rsa, ctx, m, pre_cond, err_instr) \
157 if((pre_cond) && ((rsa)->_method_mod_##m == NULL) && \
158 !BN_MONT_CTX_set_locked(&((rsa)->_method_mod_##m), \
159 CRYPTO_LOCK_RSA, \
160 (rsa)->m, (ctx))) \
161 err_instr
162
RSA_eay_public_encrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)163 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
164 unsigned char *to, RSA *rsa, int padding)
165 {
166 BIGNUM *f,*ret;
167 int i,j,k,num=0,r= -1;
168 unsigned char *buf=NULL;
169 BN_CTX *ctx=NULL;
170
171 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
172 {
173 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
174 return -1;
175 }
176
177 if (BN_ucmp(rsa->n, rsa->e) <= 0)
178 {
179 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
180 return -1;
181 }
182
183 /* for large moduli, enforce exponent limit */
184 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
185 {
186 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
187 {
188 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
189 return -1;
190 }
191 }
192
193 if ((ctx=BN_CTX_new()) == NULL) goto err;
194 BN_CTX_start(ctx);
195 f = BN_CTX_get(ctx);
196 ret = BN_CTX_get(ctx);
197 num=BN_num_bytes(rsa->n);
198 buf = OPENSSL_malloc(num);
199 if (!f || !ret || !buf)
200 {
201 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
202 goto err;
203 }
204
205 switch (padding)
206 {
207 case RSA_PKCS1_PADDING:
208 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
209 break;
210 #ifndef OPENSSL_NO_SHA
211 case RSA_PKCS1_OAEP_PADDING:
212 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
213 break;
214 #endif
215 case RSA_SSLV23_PADDING:
216 i=RSA_padding_add_SSLv23(buf,num,from,flen);
217 break;
218 case RSA_NO_PADDING:
219 i=RSA_padding_add_none(buf,num,from,flen);
220 break;
221 default:
222 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
223 goto err;
224 }
225 if (i <= 0) goto err;
226
227 if (BN_bin2bn(buf,num,f) == NULL) goto err;
228
229 if (BN_ucmp(f, rsa->n) >= 0)
230 {
231 /* usually the padding functions would catch this */
232 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
233 goto err;
234 }
235
236 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
237
238 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
239 rsa->_method_mod_n)) goto err;
240
241 /* put in leading 0 bytes if the number is less than the
242 * length of the modulus */
243 j=BN_num_bytes(ret);
244 i=BN_bn2bin(ret,&(to[num-j]));
245 for (k=0; k<(num-i); k++)
246 to[k]=0;
247
248 r=num;
249 err:
250 if (ctx != NULL)
251 {
252 BN_CTX_end(ctx);
253 BN_CTX_free(ctx);
254 }
255 if (buf != NULL)
256 {
257 OPENSSL_cleanse(buf,num);
258 OPENSSL_free(buf);
259 }
260 return(r);
261 }
262
rsa_get_blinding(RSA * rsa,BIGNUM ** r,int * local,BN_CTX * ctx)263 static BN_BLINDING *rsa_get_blinding(RSA *rsa, BIGNUM **r, int *local, BN_CTX *ctx)
264 {
265 BN_BLINDING *ret;
266
267 if (rsa->blinding == NULL)
268 {
269 if (rsa->blinding == NULL)
270 {
271 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
272 if (rsa->blinding == NULL)
273 rsa->blinding = RSA_setup_blinding(rsa, ctx);
274 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
275 }
276 }
277
278 ret = rsa->blinding;
279 if (ret == NULL)
280 return NULL;
281
282 if (BN_BLINDING_get_thread_id(ret) != CRYPTO_thread_id())
283 {
284 *local = 0;
285 if (rsa->mt_blinding == NULL)
286 {
287 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
288 if (rsa->mt_blinding == NULL)
289 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
290 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
291 }
292 ret = rsa->mt_blinding;
293 }
294 else
295 *local = 1;
296
297 return ret;
298 }
299
rsa_blinding_convert(BN_BLINDING * b,int local,BIGNUM * f,BIGNUM * r,BN_CTX * ctx)300 static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f,
301 BIGNUM *r, BN_CTX *ctx)
302 {
303 if (local)
304 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
305 else
306 {
307 int ret;
308 CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING);
309 ret = BN_BLINDING_convert_ex(f, r, b, ctx);
310 CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING);
311 return ret;
312 }
313 }
314
rsa_blinding_invert(BN_BLINDING * b,int local,BIGNUM * f,BIGNUM * r,BN_CTX * ctx)315 static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f,
316 BIGNUM *r, BN_CTX *ctx)
317 {
318 if (local)
319 return BN_BLINDING_invert_ex(f, NULL, b, ctx);
320 else
321 {
322 int ret;
323 CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
324 ret = BN_BLINDING_invert_ex(f, r, b, ctx);
325 CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
326 return ret;
327 }
328 }
329
330 /* signing */
RSA_eay_private_encrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)331 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
332 unsigned char *to, RSA *rsa, int padding)
333 {
334 BIGNUM *f, *ret, *br, *res;
335 int i,j,k,num=0,r= -1;
336 unsigned char *buf=NULL;
337 BN_CTX *ctx=NULL;
338 int local_blinding = 0;
339 BN_BLINDING *blinding = NULL;
340
341 if ((ctx=BN_CTX_new()) == NULL) goto err;
342 BN_CTX_start(ctx);
343 f = BN_CTX_get(ctx);
344 br = BN_CTX_get(ctx);
345 ret = BN_CTX_get(ctx);
346 num = BN_num_bytes(rsa->n);
347 buf = OPENSSL_malloc(num);
348 if(!f || !ret || !buf)
349 {
350 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
351 goto err;
352 }
353
354 switch (padding)
355 {
356 case RSA_PKCS1_PADDING:
357 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
358 break;
359 case RSA_X931_PADDING:
360 i=RSA_padding_add_X931(buf,num,from,flen);
361 break;
362 case RSA_NO_PADDING:
363 i=RSA_padding_add_none(buf,num,from,flen);
364 break;
365 case RSA_SSLV23_PADDING:
366 default:
367 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
368 goto err;
369 }
370 if (i <= 0) goto err;
371
372 if (BN_bin2bn(buf,num,f) == NULL) goto err;
373
374 if (BN_ucmp(f, rsa->n) >= 0)
375 {
376 /* usually the padding functions would catch this */
377 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
378 goto err;
379 }
380
381 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
382 {
383 blinding = rsa_get_blinding(rsa, &br, &local_blinding, ctx);
384 if (blinding == NULL)
385 {
386 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
387 goto err;
388 }
389 }
390
391 if (blinding != NULL)
392 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
393 goto err;
394
395 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
396 ((rsa->p != NULL) &&
397 (rsa->q != NULL) &&
398 (rsa->dmp1 != NULL) &&
399 (rsa->dmq1 != NULL) &&
400 (rsa->iqmp != NULL)) )
401 {
402 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
403 }
404 else
405 {
406 BIGNUM local_d;
407 BIGNUM *d = NULL;
408
409 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
410 {
411 BN_init(&local_d);
412 d = &local_d;
413 BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME);
414 }
415 else
416 d = rsa->d;
417
418 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
419
420 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
421 rsa->_method_mod_n)) goto err;
422 }
423
424 if (blinding)
425 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
426 goto err;
427
428 if (padding == RSA_X931_PADDING)
429 {
430 BN_sub(f, rsa->n, ret);
431 if (BN_cmp(ret, f))
432 res = f;
433 else
434 res = ret;
435 }
436 else
437 res = ret;
438
439 /* put in leading 0 bytes if the number is less than the
440 * length of the modulus */
441 j=BN_num_bytes(res);
442 i=BN_bn2bin(res,&(to[num-j]));
443 for (k=0; k<(num-i); k++)
444 to[k]=0;
445
446 r=num;
447 err:
448 if (ctx != NULL)
449 {
450 BN_CTX_end(ctx);
451 BN_CTX_free(ctx);
452 }
453 if (buf != NULL)
454 {
455 OPENSSL_cleanse(buf,num);
456 OPENSSL_free(buf);
457 }
458 return(r);
459 }
460
RSA_eay_private_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)461 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
462 unsigned char *to, RSA *rsa, int padding)
463 {
464 BIGNUM *f, *ret, *br;
465 int j,num=0,r= -1;
466 unsigned char *p;
467 unsigned char *buf=NULL;
468 BN_CTX *ctx=NULL;
469 int local_blinding = 0;
470 BN_BLINDING *blinding = NULL;
471
472 if((ctx = BN_CTX_new()) == NULL) goto err;
473 BN_CTX_start(ctx);
474 f = BN_CTX_get(ctx);
475 br = BN_CTX_get(ctx);
476 ret = BN_CTX_get(ctx);
477 num = BN_num_bytes(rsa->n);
478 buf = OPENSSL_malloc(num);
479 if(!f || !ret || !buf)
480 {
481 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
482 goto err;
483 }
484
485 /* This check was for equality but PGP does evil things
486 * and chops off the top '0' bytes */
487 if (flen > num)
488 {
489 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
490 goto err;
491 }
492
493 /* make data into a big number */
494 if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
495
496 if (BN_ucmp(f, rsa->n) >= 0)
497 {
498 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
499 goto err;
500 }
501
502 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
503 {
504 blinding = rsa_get_blinding(rsa, &br, &local_blinding, ctx);
505 if (blinding == NULL)
506 {
507 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
508 goto err;
509 }
510 }
511
512 if (blinding != NULL)
513 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
514 goto err;
515
516 /* do the decrypt */
517 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
518 ((rsa->p != NULL) &&
519 (rsa->q != NULL) &&
520 (rsa->dmp1 != NULL) &&
521 (rsa->dmq1 != NULL) &&
522 (rsa->iqmp != NULL)) )
523 {
524 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
525 }
526 else
527 {
528 BIGNUM local_d;
529 BIGNUM *d = NULL;
530
531 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
532 {
533 d = &local_d;
534 BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME);
535 }
536 else
537 d = rsa->d;
538
539 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
540 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
541 rsa->_method_mod_n))
542 goto err;
543 }
544
545 if (blinding)
546 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
547 goto err;
548
549 p=buf;
550 j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
551
552 switch (padding)
553 {
554 case RSA_PKCS1_PADDING:
555 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
556 break;
557 #ifndef OPENSSL_NO_SHA
558 case RSA_PKCS1_OAEP_PADDING:
559 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
560 break;
561 #endif
562 case RSA_SSLV23_PADDING:
563 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
564 break;
565 case RSA_NO_PADDING:
566 r=RSA_padding_check_none(to,num,buf,j,num);
567 break;
568 default:
569 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
570 goto err;
571 }
572 if (r < 0)
573 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
574
575 err:
576 if (ctx != NULL)
577 {
578 BN_CTX_end(ctx);
579 BN_CTX_free(ctx);
580 }
581 if (buf != NULL)
582 {
583 OPENSSL_cleanse(buf,num);
584 OPENSSL_free(buf);
585 }
586 return(r);
587 }
588
589 /* signature verification */
RSA_eay_public_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)590 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
591 unsigned char *to, RSA *rsa, int padding)
592 {
593 BIGNUM *f,*ret;
594 int i,num=0,r= -1;
595 unsigned char *p;
596 unsigned char *buf=NULL;
597 BN_CTX *ctx=NULL;
598
599 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
600 {
601 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
602 return -1;
603 }
604
605 if (BN_ucmp(rsa->n, rsa->e) <= 0)
606 {
607 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
608 return -1;
609 }
610
611 /* for large moduli, enforce exponent limit */
612 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
613 {
614 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
615 {
616 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
617 return -1;
618 }
619 }
620
621 if((ctx = BN_CTX_new()) == NULL) goto err;
622 BN_CTX_start(ctx);
623 f = BN_CTX_get(ctx);
624 ret = BN_CTX_get(ctx);
625 num=BN_num_bytes(rsa->n);
626 buf = OPENSSL_malloc(num);
627 if(!f || !ret || !buf)
628 {
629 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
630 goto err;
631 }
632
633 /* This check was for equality but PGP does evil things
634 * and chops off the top '0' bytes */
635 if (flen > num)
636 {
637 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
638 goto err;
639 }
640
641 if (BN_bin2bn(from,flen,f) == NULL) goto err;
642
643 if (BN_ucmp(f, rsa->n) >= 0)
644 {
645 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
646 goto err;
647 }
648
649 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
650
651 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
652 rsa->_method_mod_n)) goto err;
653
654 if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
655 BN_sub(ret, rsa->n, ret);
656
657 p=buf;
658 i=BN_bn2bin(ret,p);
659
660 switch (padding)
661 {
662 case RSA_PKCS1_PADDING:
663 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
664 break;
665 case RSA_X931_PADDING:
666 r=RSA_padding_check_X931(to,num,buf,i,num);
667 break;
668 case RSA_NO_PADDING:
669 r=RSA_padding_check_none(to,num,buf,i,num);
670 break;
671 default:
672 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
673 goto err;
674 }
675 if (r < 0)
676 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
677
678 err:
679 if (ctx != NULL)
680 {
681 BN_CTX_end(ctx);
682 BN_CTX_free(ctx);
683 }
684 if (buf != NULL)
685 {
686 OPENSSL_cleanse(buf,num);
687 OPENSSL_free(buf);
688 }
689 return(r);
690 }
691
RSA_eay_mod_exp(BIGNUM * r0,const BIGNUM * I,RSA * rsa,BN_CTX * ctx)692 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
693 {
694 BIGNUM *r1,*m1,*vrfy;
695 BIGNUM local_dmp1, local_dmq1;
696 BIGNUM *dmp1, *dmq1;
697 int ret=0;
698
699 BN_CTX_start(ctx);
700 r1 = BN_CTX_get(ctx);
701 m1 = BN_CTX_get(ctx);
702 vrfy = BN_CTX_get(ctx);
703
704 MONT_HELPER(rsa, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
705 MONT_HELPER(rsa, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
706 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
707
708 if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
709 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
710 {
711 dmq1 = &local_dmq1;
712 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_EXP_CONSTTIME);
713 }
714 else
715 dmq1 = rsa->dmq1;
716 if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,
717 rsa->_method_mod_q)) goto err;
718
719 if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
720 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
721 {
722 dmp1 = &local_dmp1;
723 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_EXP_CONSTTIME);
724 }
725 else
726 dmp1 = rsa->dmp1;
727 if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,
728 rsa->_method_mod_p)) goto err;
729
730 if (!BN_sub(r0,r0,m1)) goto err;
731 /* This will help stop the size of r0 increasing, which does
732 * affect the multiply if it optimised for a power of 2 size */
733 if (BN_is_negative(r0))
734 if (!BN_add(r0,r0,rsa->p)) goto err;
735
736 if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
737 if (!BN_mod(r0,r1,rsa->p,ctx)) goto err;
738 /* If p < q it is occasionally possible for the correction of
739 * adding 'p' if r0 is negative above to leave the result still
740 * negative. This can break the private key operations: the following
741 * second correction should *always* correct this rare occurrence.
742 * This will *never* happen with OpenSSL generated keys because
743 * they ensure p > q [steve]
744 */
745 if (BN_is_negative(r0))
746 if (!BN_add(r0,r0,rsa->p)) goto err;
747 if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
748 if (!BN_add(r0,r1,m1)) goto err;
749
750 if (rsa->e && rsa->n)
751 {
752 if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
753 /* If 'I' was greater than (or equal to) rsa->n, the operation
754 * will be equivalent to using 'I mod n'. However, the result of
755 * the verify will *always* be less than 'n' so we don't check
756 * for absolute equality, just congruency. */
757 if (!BN_sub(vrfy, vrfy, I)) goto err;
758 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
759 if (BN_is_negative(vrfy))
760 if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
761 if (!BN_is_zero(vrfy))
762 {
763 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
764 * miscalculated CRT output, just do a raw (slower)
765 * mod_exp and return that instead. */
766
767 BIGNUM local_d;
768 BIGNUM *d = NULL;
769
770 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
771 {
772 d = &local_d;
773 BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME);
774 }
775 else
776 d = rsa->d;
777 if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,
778 rsa->_method_mod_n)) goto err;
779 }
780 }
781 ret=1;
782 err:
783 BN_CTX_end(ctx);
784 return(ret);
785 }
786
RSA_eay_init(RSA * rsa)787 static int RSA_eay_init(RSA *rsa)
788 {
789 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
790 return(1);
791 }
792
RSA_eay_finish(RSA * rsa)793 static int RSA_eay_finish(RSA *rsa)
794 {
795 if (rsa->_method_mod_n != NULL)
796 BN_MONT_CTX_free(rsa->_method_mod_n);
797 if (rsa->_method_mod_p != NULL)
798 BN_MONT_CTX_free(rsa->_method_mod_p);
799 if (rsa->_method_mod_q != NULL)
800 BN_MONT_CTX_free(rsa->_method_mod_q);
801 return(1);
802 }
803
804 #endif
805