156083Skris/* crypto/rsa/rsa_gen.c */ 256083Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 356083Skris * All rights reserved. 456083Skris * 556083Skris * This package is an SSL implementation written 656083Skris * by Eric Young (eay@cryptsoft.com). 756083Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 956083Skris * This library is free for commercial and non-commercial use as long as 1056083Skris * the following conditions are aheared to. The following conditions 1156083Skris * apply to all code found in this distribution, be it the RC4, RSA, 1256083Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1356083Skris * included with this distribution is covered by the same copyright terms 1456083Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1656083Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1756083Skris * the code are not to be removed. 1856083Skris * If this package is used in a product, Eric Young should be given attribution 1956083Skris * as the author of the parts of the library used. 2056083Skris * This can be in the form of a textual message at program startup or 2156083Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2356083Skris * Redistribution and use in source and binary forms, with or without 2456083Skris * modification, are permitted provided that the following conditions 2556083Skris * are met: 2656083Skris * 1. Redistributions of source code must retain the copyright 2756083Skris * notice, this list of conditions and the following disclaimer. 2856083Skris * 2. Redistributions in binary form must reproduce the above copyright 2956083Skris * notice, this list of conditions and the following disclaimer in the 3056083Skris * documentation and/or other materials provided with the distribution. 3156083Skris * 3. All advertising materials mentioning features or use of this software 3256083Skris * must display the following acknowledgement: 3356083Skris * "This product includes cryptographic software written by 3456083Skris * Eric Young (eay@cryptsoft.com)" 3556083Skris * The word 'cryptographic' can be left out if the rouines from the library 3656083Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3856083Skris * the apps directory (application code) you must include an acknowledgement: 3956083Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4156083Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4256083Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4356083Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4456083Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4556083Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4656083Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4756083Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4856083Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4956083Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5056083Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5156083Skris * SUCH DAMAGE. 52296341Sdelphij * 5356083Skris * The licence and distribution terms for any publically available version or 5456083Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5556083Skris * copied and put under another distribution licence 5656083Skris * [including the GNU Public Licence.] 5756083Skris */ 5856083Skris 59296341Sdelphij/* 60296341Sdelphij * NB: these functions have been "upgraded", the deprecated versions (which 61296341Sdelphij * are compatibility wrappers using these functions) are in rsa_depr.c. - 62296341Sdelphij * Geoff 63160814Ssimon */ 64160814Ssimon 6556083Skris#include <stdio.h> 6656083Skris#include <time.h> 6756083Skris#include "cryptlib.h" 6856083Skris#include <openssl/bn.h> 6956083Skris#include <openssl/rsa.h> 70238405Sjkim#ifdef OPENSSL_FIPS 71296341Sdelphij# include <openssl/fips.h> 72238405Sjkim#endif 7356083Skris 74296341Sdelphijstatic int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, 75296341Sdelphij BN_GENCB *cb); 76142425Snectar 77296341Sdelphij/* 78296341Sdelphij * NB: this wrapper would normally be placed in rsa_lib.c and the static 79296341Sdelphij * implementation would probably be in rsa_eay.c. Nonetheless, is kept here 80296341Sdelphij * so that we don't introduce a new linker dependency. Eg. any application 81296341Sdelphij * that wasn't previously linking object code related to key-generation won't 82296341Sdelphij * have to now just because key-generation is part of RSA_METHOD. 83296341Sdelphij */ 84160814Ssimonint RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) 85296341Sdelphij{ 86238405Sjkim#ifdef OPENSSL_FIPS 87296341Sdelphij if (FIPS_mode() && !(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) 88296341Sdelphij && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) { 89296341Sdelphij RSAerr(RSA_F_RSA_GENERATE_KEY_EX, RSA_R_NON_FIPS_RSA_METHOD); 90296341Sdelphij return 0; 91296341Sdelphij } 92238405Sjkim#endif 93296341Sdelphij if (rsa->meth->rsa_keygen) 94296341Sdelphij return rsa->meth->rsa_keygen(rsa, bits, e_value, cb); 95238405Sjkim#ifdef OPENSSL_FIPS 96296341Sdelphij if (FIPS_mode()) 97296341Sdelphij return FIPS_rsa_generate_key_ex(rsa, bits, e_value, cb); 98238405Sjkim#endif 99296341Sdelphij return rsa_builtin_keygen(rsa, bits, e_value, cb); 100296341Sdelphij} 101160814Ssimon 102296341Sdelphijstatic int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, 103296341Sdelphij BN_GENCB *cb) 104296341Sdelphij{ 105296341Sdelphij BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp; 106296341Sdelphij BIGNUM local_r0, local_d, local_p; 107296341Sdelphij BIGNUM *pr0, *d, *p; 108296341Sdelphij int bitsp, bitsq, ok = -1, n = 0; 109296341Sdelphij BN_CTX *ctx = NULL; 11056083Skris 111296341Sdelphij ctx = BN_CTX_new(); 112296341Sdelphij if (ctx == NULL) 113296341Sdelphij goto err; 114296341Sdelphij BN_CTX_start(ctx); 115296341Sdelphij r0 = BN_CTX_get(ctx); 116296341Sdelphij r1 = BN_CTX_get(ctx); 117296341Sdelphij r2 = BN_CTX_get(ctx); 118296341Sdelphij r3 = BN_CTX_get(ctx); 119296341Sdelphij if (r3 == NULL) 120296341Sdelphij goto err; 12156083Skris 122296341Sdelphij bitsp = (bits + 1) / 2; 123296341Sdelphij bitsq = bits - bitsp; 12456083Skris 125296341Sdelphij /* We need the RSA components non-NULL */ 126296341Sdelphij if (!rsa->n && ((rsa->n = BN_new()) == NULL)) 127296341Sdelphij goto err; 128296341Sdelphij if (!rsa->d && ((rsa->d = BN_new()) == NULL)) 129296341Sdelphij goto err; 130296341Sdelphij if (!rsa->e && ((rsa->e = BN_new()) == NULL)) 131296341Sdelphij goto err; 132296341Sdelphij if (!rsa->p && ((rsa->p = BN_new()) == NULL)) 133296341Sdelphij goto err; 134296341Sdelphij if (!rsa->q && ((rsa->q = BN_new()) == NULL)) 135296341Sdelphij goto err; 136296341Sdelphij if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) 137296341Sdelphij goto err; 138296341Sdelphij if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) 139296341Sdelphij goto err; 140296341Sdelphij if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) 141296341Sdelphij goto err; 14256083Skris 143296341Sdelphij BN_copy(rsa->e, e_value); 14456083Skris 145296341Sdelphij /* generate p and q */ 146296341Sdelphij for (;;) { 147296341Sdelphij if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb)) 148296341Sdelphij goto err; 149296341Sdelphij if (!BN_sub(r2, rsa->p, BN_value_one())) 150296341Sdelphij goto err; 151296341Sdelphij if (!BN_gcd(r1, r2, rsa->e, ctx)) 152296341Sdelphij goto err; 153296341Sdelphij if (BN_is_one(r1)) 154296341Sdelphij break; 155296341Sdelphij if (!BN_GENCB_call(cb, 2, n++)) 156296341Sdelphij goto err; 157296341Sdelphij } 158296341Sdelphij if (!BN_GENCB_call(cb, 3, 0)) 159296341Sdelphij goto err; 160296341Sdelphij for (;;) { 161296341Sdelphij /* 162296341Sdelphij * When generating ridiculously small keys, we can get stuck 163296341Sdelphij * continually regenerating the same prime values. Check for this and 164296341Sdelphij * bail if it happens 3 times. 165296341Sdelphij */ 166296341Sdelphij unsigned int degenerate = 0; 167296341Sdelphij do { 168296341Sdelphij if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb)) 169296341Sdelphij goto err; 170296341Sdelphij } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3)); 171296341Sdelphij if (degenerate == 3) { 172296341Sdelphij ok = 0; /* we set our own err */ 173296341Sdelphij RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL); 174296341Sdelphij goto err; 175296341Sdelphij } 176296341Sdelphij if (!BN_sub(r2, rsa->q, BN_value_one())) 177296341Sdelphij goto err; 178296341Sdelphij if (!BN_gcd(r1, r2, rsa->e, ctx)) 179296341Sdelphij goto err; 180296341Sdelphij if (BN_is_one(r1)) 181296341Sdelphij break; 182296341Sdelphij if (!BN_GENCB_call(cb, 2, n++)) 183296341Sdelphij goto err; 184296341Sdelphij } 185296341Sdelphij if (!BN_GENCB_call(cb, 3, 1)) 186296341Sdelphij goto err; 187296341Sdelphij if (BN_cmp(rsa->p, rsa->q) < 0) { 188296341Sdelphij tmp = rsa->p; 189296341Sdelphij rsa->p = rsa->q; 190296341Sdelphij rsa->q = tmp; 191296341Sdelphij } 19256083Skris 193296341Sdelphij /* calculate n */ 194296341Sdelphij if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx)) 195296341Sdelphij goto err; 19656083Skris 197296341Sdelphij /* calculate d */ 198296341Sdelphij if (!BN_sub(r1, rsa->p, BN_value_one())) 199296341Sdelphij goto err; /* p-1 */ 200296341Sdelphij if (!BN_sub(r2, rsa->q, BN_value_one())) 201296341Sdelphij goto err; /* q-1 */ 202296341Sdelphij if (!BN_mul(r0, r1, r2, ctx)) 203296341Sdelphij goto err; /* (p-1)(q-1) */ 204296341Sdelphij if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { 205296341Sdelphij pr0 = &local_r0; 206296341Sdelphij BN_with_flags(pr0, r0, BN_FLG_CONSTTIME); 207296341Sdelphij } else 208296341Sdelphij pr0 = r0; 209296341Sdelphij if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) 210296341Sdelphij goto err; /* d */ 21156083Skris 212296341Sdelphij /* set up d for correct BN_FLG_CONSTTIME flag */ 213296341Sdelphij if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { 214296341Sdelphij d = &local_d; 215296341Sdelphij BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); 216296341Sdelphij } else 217296341Sdelphij d = rsa->d; 218194206Ssimon 219296341Sdelphij /* calculate d mod (p-1) */ 220296341Sdelphij if (!BN_mod(rsa->dmp1, d, r1, ctx)) 221296341Sdelphij goto err; 22256083Skris 223296341Sdelphij /* calculate d mod (q-1) */ 224296341Sdelphij if (!BN_mod(rsa->dmq1, d, r2, ctx)) 225296341Sdelphij goto err; 22656083Skris 227296341Sdelphij /* calculate inverse of q mod p */ 228296341Sdelphij if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { 229296341Sdelphij p = &local_p; 230296341Sdelphij BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); 231296341Sdelphij } else 232296341Sdelphij p = rsa->p; 233296341Sdelphij if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) 234296341Sdelphij goto err; 23556083Skris 236296341Sdelphij ok = 1; 237296341Sdelphij err: 238296341Sdelphij if (ok == -1) { 239296341Sdelphij RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN); 240296341Sdelphij ok = 0; 241296341Sdelphij } 242296341Sdelphij if (ctx != NULL) { 243296341Sdelphij BN_CTX_end(ctx); 244296341Sdelphij BN_CTX_free(ctx); 245296341Sdelphij } 246160814Ssimon 247296341Sdelphij return ok; 248296341Sdelphij} 249