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.]
61 #include <openssl/bn.h>
62 #include <openssl/rsa.h>
63 #include <openssl/rand.h>
67 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
68 unsigned char *to, RSA *rsa,int padding);
69 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
70 unsigned char *to, RSA *rsa,int padding);
71 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
72 unsigned char *to, RSA *rsa,int padding);
73 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
74 unsigned char *to, RSA *rsa,int padding);
75 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
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, /* signature verification */
82 RSA_eay_private_encrypt, /* signing */
83 RSA_eay_private_decrypt,
85 BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
95 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
97 return(&rsa_pkcs1_eay_meth);
100 /* Static helper to reduce oodles of code duplication. As a slight
101 * optimisation, the "MONT_HELPER() macro must be used as front-end to this
102 * function, to prevent unnecessary function calls - there is an initial test
103 * that is performed by the macro-generated code. */
104 static int rsa_eay_mont_helper(BN_MONT_CTX **ptr, const BIGNUM *modulus, BN_CTX *ctx)
106 BN_MONT_CTX *bn_mont_ctx;
107 if((bn_mont_ctx = BN_MONT_CTX_new()) == NULL)
109 if(!BN_MONT_CTX_set(bn_mont_ctx, modulus, ctx))
111 BN_MONT_CTX_free(bn_mont_ctx);
114 if (*ptr == NULL) /* other thread may have finished first */
116 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
117 if (*ptr == NULL) /* check again in the lock to stop races */
122 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
125 BN_MONT_CTX_free(bn_mont_ctx);
129 * MONT_HELPER(rsa, bn_ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
131 #define MONT_HELPER(rsa, ctx, m, pre_cond, err_instr) \
132 if((pre_cond) && ((rsa)->_method_mod_##m == NULL) && \
133 !rsa_eay_mont_helper(&((rsa)->_method_mod_##m), \
137 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
138 unsigned char *to, RSA *rsa, int padding)
141 int i,j,k,num=0,r= -1;
142 unsigned char *buf=NULL;
145 if ((ctx=BN_CTX_new()) == NULL) goto err;
148 ret = BN_CTX_get(ctx);
149 num=BN_num_bytes(rsa->n);
150 buf = OPENSSL_malloc(num);
151 if (!f || !ret || !buf)
153 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
159 case RSA_PKCS1_PADDING:
160 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
162 #ifndef OPENSSL_NO_SHA
163 case RSA_PKCS1_OAEP_PADDING:
164 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
167 case RSA_SSLV23_PADDING:
168 i=RSA_padding_add_SSLv23(buf,num,from,flen);
171 i=RSA_padding_add_none(buf,num,from,flen);
174 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
177 if (i <= 0) goto err;
179 if (BN_bin2bn(buf,num,f) == NULL) goto err;
181 if (BN_ucmp(f, rsa->n) >= 0)
183 /* usually the padding functions would catch this */
184 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
188 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
190 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
191 rsa->_method_mod_n)) goto err;
193 /* put in leading 0 bytes if the number is less than the
194 * length of the modulus */
196 i=BN_bn2bin(ret,&(to[num-j]));
197 for (k=0; k<(num-i); k++)
209 OPENSSL_cleanse(buf,num);
215 static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
218 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
219 /* Check again inside the lock - the macro's check is racey */
220 if(rsa->blinding == NULL)
221 ret = RSA_blinding_on(rsa, ctx);
222 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
226 #define BLINDING_HELPER(rsa, ctx, err_instr) \
228 if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \
229 ((rsa)->blinding == NULL) && \
230 !rsa_eay_blinding(rsa, ctx)) \
234 static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx)
237 BN_BLINDING *ret = NULL;
239 /* added in OpenSSL 0.9.6j and 0.9.7b */
241 /* NB: similar code appears in RSA_blinding_on (rsa_lib.c);
242 * this should be placed in a new function of its own, but for reasons
243 * of binary compatibility can't */
247 if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
249 /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
250 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0);
251 if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
255 if (!BN_rand_range(A,rsa->n)) goto err;
257 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
259 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
261 ret = BN_BLINDING_new(A,Ai,rsa->n);
269 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
270 unsigned char *to, RSA *rsa, int padding)
273 int i,j,k,num=0,r= -1;
274 unsigned char *buf=NULL;
276 int local_blinding = 0;
277 BN_BLINDING *blinding = NULL;
279 if ((ctx=BN_CTX_new()) == NULL) goto err;
282 ret = BN_CTX_get(ctx);
283 num=BN_num_bytes(rsa->n);
284 buf = OPENSSL_malloc(num);
285 if(!f || !ret || !buf)
287 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
293 case RSA_PKCS1_PADDING:
294 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
297 i=RSA_padding_add_none(buf,num,from,flen);
299 case RSA_SSLV23_PADDING:
301 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
304 if (i <= 0) goto err;
306 if (BN_bin2bn(buf,num,f) == NULL) goto err;
308 if (BN_ucmp(f, rsa->n) >= 0)
310 /* usually the padding functions would catch this */
311 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
315 BLINDING_HELPER(rsa, ctx, goto err;);
316 blinding = rsa->blinding;
318 /* Now unless blinding is disabled, 'blinding' is non-NULL.
319 * But the BN_BLINDING object may be owned by some other thread
320 * (we don't want to keep it constant and we don't want to use
321 * lots of locking to avoid race conditions, so only a single
322 * thread can use it; other threads have to use local blinding
324 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
326 if (blinding == NULL)
328 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
333 if (blinding != NULL)
335 if (blinding->thread_id != CRYPTO_thread_id())
337 /* we need a local one-time blinding factor */
339 blinding = setup_blinding(rsa, ctx);
340 if (blinding == NULL)
347 if (!BN_BLINDING_convert(f, blinding, ctx)) goto err;
349 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
352 (rsa->dmp1 != NULL) &&
353 (rsa->dmq1 != NULL) &&
354 (rsa->iqmp != NULL)) )
355 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa,ctx)) goto err; }
358 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
359 if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx,
360 rsa->_method_mod_n)) goto err;
364 if (!BN_BLINDING_invert(ret, blinding, ctx)) goto err;
366 /* put in leading 0 bytes if the number is less than the
367 * length of the modulus */
369 i=BN_bn2bin(ret,&(to[num-j]));
370 for (k=0; k<(num-i); k++)
381 BN_BLINDING_free(blinding);
384 OPENSSL_cleanse(buf,num);
390 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
391 unsigned char *to, RSA *rsa, int padding)
396 unsigned char *buf=NULL;
398 int local_blinding = 0;
399 BN_BLINDING *blinding = NULL;
401 if((ctx = BN_CTX_new()) == NULL) goto err;
404 ret = BN_CTX_get(ctx);
405 num=BN_num_bytes(rsa->n);
406 buf = OPENSSL_malloc(num);
407 if(!f || !ret || !buf)
409 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
413 /* This check was for equality but PGP does evil things
414 * and chops off the top '0' bytes */
417 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
421 /* make data into a big number */
422 if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
424 if (BN_ucmp(f, rsa->n) >= 0)
426 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
430 BLINDING_HELPER(rsa, ctx, goto err;);
431 blinding = rsa->blinding;
433 /* Now unless blinding is disabled, 'blinding' is non-NULL.
434 * But the BN_BLINDING object may be owned by some other thread
435 * (we don't want to keep it constant and we don't want to use
436 * lots of locking to avoid race conditions, so only a single
437 * thread can use it; other threads have to use local blinding
439 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
441 if (blinding == NULL)
443 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
448 if (blinding != NULL)
450 if (blinding->thread_id != CRYPTO_thread_id())
452 /* we need a local one-time blinding factor */
454 blinding = setup_blinding(rsa, ctx);
455 if (blinding == NULL)
462 if (!BN_BLINDING_convert(f, blinding, ctx)) goto err;
465 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
468 (rsa->dmp1 != NULL) &&
469 (rsa->dmq1 != NULL) &&
470 (rsa->iqmp != NULL)) )
471 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa,ctx)) goto err; }
474 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
475 if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx,
481 if (!BN_BLINDING_invert(ret, blinding, ctx)) goto err;
484 j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
488 case RSA_PKCS1_PADDING:
489 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
491 #ifndef OPENSSL_NO_SHA
492 case RSA_PKCS1_OAEP_PADDING:
493 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
496 case RSA_SSLV23_PADDING:
497 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
500 r=RSA_padding_check_none(to,num,buf,j,num);
503 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
507 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
516 BN_BLINDING_free(blinding);
519 OPENSSL_cleanse(buf,num);
525 /* signature verification */
526 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
527 unsigned char *to, RSA *rsa, int padding)
532 unsigned char *buf=NULL;
535 if((ctx = BN_CTX_new()) == NULL) goto err;
538 ret = BN_CTX_get(ctx);
539 num=BN_num_bytes(rsa->n);
540 buf = OPENSSL_malloc(num);
541 if(!f || !ret || !buf)
543 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
547 /* This check was for equality but PGP does evil things
548 * and chops off the top '0' bytes */
551 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
555 if (BN_bin2bn(from,flen,f) == NULL) goto err;
557 if (BN_ucmp(f, rsa->n) >= 0)
559 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
563 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
565 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
566 rsa->_method_mod_n)) goto err;
573 case RSA_PKCS1_PADDING:
574 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
577 r=RSA_padding_check_none(to,num,buf,i,num);
580 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
584 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
594 OPENSSL_cleanse(buf,num);
600 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
602 BIGNUM *r1,*m1,*vrfy;
606 r1 = BN_CTX_get(ctx);
607 m1 = BN_CTX_get(ctx);
608 vrfy = BN_CTX_get(ctx);
610 MONT_HELPER(rsa, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
611 MONT_HELPER(rsa, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
612 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
614 if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
615 if (!rsa->meth->bn_mod_exp(m1,r1,rsa->dmq1,rsa->q,ctx,
616 rsa->_method_mod_q)) goto err;
618 if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
619 if (!rsa->meth->bn_mod_exp(r0,r1,rsa->dmp1,rsa->p,ctx,
620 rsa->_method_mod_p)) goto err;
622 if (!BN_sub(r0,r0,m1)) goto err;
623 /* This will help stop the size of r0 increasing, which does
624 * affect the multiply if it optimised for a power of 2 size */
625 if (BN_is_negative(r0))
626 if (!BN_add(r0,r0,rsa->p)) goto err;
628 if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
629 if (!BN_mod(r0,r1,rsa->p,ctx)) goto err;
630 /* If p < q it is occasionally possible for the correction of
631 * adding 'p' if r0 is negative above to leave the result still
632 * negative. This can break the private key operations: the following
633 * second correction should *always* correct this rare occurrence.
634 * This will *never* happen with OpenSSL generated keys because
635 * they ensure p > q [steve]
637 if (BN_is_negative(r0))
638 if (!BN_add(r0,r0,rsa->p)) goto err;
639 if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
640 if (!BN_add(r0,r1,m1)) goto err;
642 if (rsa->e && rsa->n)
644 if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
645 /* If 'I' was greater than (or equal to) rsa->n, the operation
646 * will be equivalent to using 'I mod n'. However, the result of
647 * the verify will *always* be less than 'n' so we don't check
648 * for absolute equality, just congruency. */
649 if (!BN_sub(vrfy, vrfy, I)) goto err;
650 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
651 if (BN_is_negative(vrfy))
652 if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
653 if (!BN_is_zero(vrfy))
654 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
655 * miscalculated CRT output, just do a raw (slower)
656 * mod_exp and return that instead. */
657 if (!rsa->meth->bn_mod_exp(r0,I,rsa->d,rsa->n,ctx,
658 rsa->_method_mod_n)) goto err;
666 static int RSA_eay_init(RSA *rsa)
668 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
672 static int RSA_eay_finish(RSA *rsa)
674 if (rsa->_method_mod_n != NULL)
675 BN_MONT_CTX_free(rsa->_method_mod_n);
676 if (rsa->_method_mod_p != NULL)
677 BN_MONT_CTX_free(rsa->_method_mod_p);
678 if (rsa->_method_mod_q != NULL)
679 BN_MONT_CTX_free(rsa->_method_mod_q);