sl@0: /* crypto/rsa/rsa_gen.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: sl@0: /* NB: these functions have been "upgraded", the deprecated versions (which are sl@0: * compatibility wrappers using these functions) are in rsa_depr.c. sl@0: * - Geoff sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: sl@0: static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb); sl@0: sl@0: /* NB: this wrapper would normally be placed in rsa_lib.c and the static sl@0: * implementation would probably be in rsa_eay.c. Nonetheless, is kept here so sl@0: * that we don't introduce a new linker dependency. Eg. any application that sl@0: * wasn't previously linking object code related to key-generation won't have to sl@0: * now just because key-generation is part of RSA_METHOD. */ sl@0: EXPORT_C int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) sl@0: { sl@0: if(rsa->meth->rsa_keygen) sl@0: return rsa->meth->rsa_keygen(rsa, bits, e_value, cb); sl@0: return rsa_builtin_keygen(rsa, bits, e_value, cb); sl@0: } sl@0: sl@0: static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) sl@0: { sl@0: BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp; sl@0: BIGNUM local_r0,local_d,local_p; sl@0: BIGNUM *pr0,*d,*p; sl@0: int bitsp,bitsq,ok= -1,n=0; sl@0: BN_CTX *ctx=NULL; sl@0: sl@0: ctx=BN_CTX_new(); sl@0: if (ctx == NULL) goto err; sl@0: BN_CTX_start(ctx); sl@0: r0 = BN_CTX_get(ctx); sl@0: r1 = BN_CTX_get(ctx); sl@0: r2 = BN_CTX_get(ctx); sl@0: r3 = BN_CTX_get(ctx); sl@0: if (r3 == NULL) goto err; sl@0: sl@0: bitsp=(bits+1)/2; sl@0: bitsq=bits-bitsp; sl@0: sl@0: /* We need the RSA components non-NULL */ sl@0: if(!rsa->n && ((rsa->n=BN_new()) == NULL)) goto err; sl@0: if(!rsa->d && ((rsa->d=BN_new()) == NULL)) goto err; sl@0: if(!rsa->e && ((rsa->e=BN_new()) == NULL)) goto err; sl@0: if(!rsa->p && ((rsa->p=BN_new()) == NULL)) goto err; sl@0: if(!rsa->q && ((rsa->q=BN_new()) == NULL)) goto err; sl@0: if(!rsa->dmp1 && ((rsa->dmp1=BN_new()) == NULL)) goto err; sl@0: if(!rsa->dmq1 && ((rsa->dmq1=BN_new()) == NULL)) goto err; sl@0: if(!rsa->iqmp && ((rsa->iqmp=BN_new()) == NULL)) goto err; sl@0: sl@0: BN_copy(rsa->e, e_value); sl@0: sl@0: /* generate p and q */ sl@0: for (;;) sl@0: { sl@0: if(!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb)) sl@0: goto err; sl@0: if (!BN_sub(r2,rsa->p,BN_value_one())) goto err; sl@0: if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; sl@0: if (BN_is_one(r1)) break; sl@0: if(!BN_GENCB_call(cb, 2, n++)) sl@0: goto err; sl@0: } sl@0: if(!BN_GENCB_call(cb, 3, 0)) sl@0: goto err; sl@0: for (;;) sl@0: { sl@0: /* When generating ridiculously small keys, we can get stuck sl@0: * continually regenerating the same prime values. Check for sl@0: * this and bail if it happens 3 times. */ sl@0: unsigned int degenerate = 0; sl@0: do sl@0: { sl@0: if(!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb)) sl@0: goto err; sl@0: } while((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3)); sl@0: if(degenerate == 3) sl@0: { sl@0: ok = 0; /* we set our own err */ sl@0: RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,RSA_R_KEY_SIZE_TOO_SMALL); sl@0: goto err; sl@0: } sl@0: if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; sl@0: if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; sl@0: if (BN_is_one(r1)) sl@0: break; sl@0: if(!BN_GENCB_call(cb, 2, n++)) sl@0: goto err; sl@0: } sl@0: if(!BN_GENCB_call(cb, 3, 1)) sl@0: goto err; sl@0: if (BN_cmp(rsa->p,rsa->q) < 0) sl@0: { sl@0: tmp=rsa->p; sl@0: rsa->p=rsa->q; sl@0: rsa->q=tmp; sl@0: } sl@0: sl@0: /* calculate n */ sl@0: if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err; sl@0: sl@0: /* calculate d */ sl@0: if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */ sl@0: if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */ sl@0: if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: pr0 = &local_r0; sl@0: BN_with_flags(pr0, r0, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: pr0 = r0; sl@0: if (!BN_mod_inverse(rsa->d,rsa->e,pr0,ctx)) goto err; /* d */ sl@0: sl@0: /* set up d for correct BN_FLG_CONSTTIME flag */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: d = &local_d; sl@0: BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: d = rsa->d; sl@0: /* calculate d mod (p-1) */ sl@0: if (!BN_mod(rsa->dmp1,d,r1,ctx)) goto err; sl@0: sl@0: /* calculate d mod (q-1) */ sl@0: if (!BN_mod(rsa->dmq1,d,r2,ctx)) goto err; sl@0: sl@0: /* calculate inverse of q mod p */ sl@0: if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) sl@0: { sl@0: p = &local_p; sl@0: BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); sl@0: } sl@0: else sl@0: p = rsa->p; sl@0: if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err; sl@0: sl@0: ok=1; sl@0: err: sl@0: if (ok == -1) sl@0: { sl@0: RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,ERR_LIB_BN); sl@0: ok=0; sl@0: } sl@0: if (ctx != NULL) sl@0: { sl@0: BN_CTX_end(ctx); sl@0: BN_CTX_free(ctx); sl@0: } sl@0: sl@0: return ok; sl@0: } sl@0: