static int ltm_rsa_generate_key(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb) { mp_int el, p, q, n, d, dmp1, dmq1, iqmp, t1, t2, t3; int counter, ret, bitsp; if (bits < 789) return -1; bitsp = (bits + 1) / 2; ret = -1; mp_init_multi(&el, &p, &q, &n, &d, &dmp1, &dmq1, &iqmp, &t1, &t2, &t3, NULL); BN2mpz(&el, e); /* generate p and q so that p != q and bits(pq) ~ bits */ counter = 0; do { BN_GENCB_call(cb, 2, counter++); CHECK(random_num(&p, bitsp), 0); CHECK(mp_find_prime(&p), MP_YES); mp_sub_d(&p, 1, &t1); mp_gcd(&t1, &el, &t2); } while(mp_cmp_d(&t2, 1) != 0); BN_GENCB_call(cb, 3, 0); counter = 0; do { BN_GENCB_call(cb, 2, counter++); CHECK(random_num(&q, bits - bitsp), 0); CHECK(mp_find_prime(&q), MP_YES); if (mp_cmp(&p, &q) == 0) /* don't let p and q be the same */ continue; mp_sub_d(&q, 1, &t1); mp_gcd(&t1, &el, &t2); } while(mp_cmp_d(&t2, 1) != 0); /* make p > q */ if (mp_cmp(&p, &q) < 0) { mp_int c; c = p; p = q; q = c; } BN_GENCB_call(cb, 3, 1); /* calculate n, n = p * q */ mp_mul(&p, &q, &n); /* calculate d, d = 1/e mod (p - 1)(q - 1) */ mp_sub_d(&p, 1, &t1); mp_sub_d(&q, 1, &t2); mp_mul(&t1, &t2, &t3); mp_invmod(&el, &t3, &d); /* calculate dmp1 dmp1 = d mod (p-1) */ mp_mod(&d, &t1, &dmp1); /* calculate dmq1 dmq1 = d mod (q-1) */ mp_mod(&d, &t2, &dmq1); /* calculate iqmp iqmp = 1/q mod p */ mp_invmod(&q, &p, &iqmp); /* fill in RSA key */ rsa->e = mpz2BN(&el); rsa->p = mpz2BN(&p); rsa->q = mpz2BN(&q); rsa->n = mpz2BN(&n); rsa->d = mpz2BN(&d); rsa->dmp1 = mpz2BN(&dmp1); rsa->dmq1 = mpz2BN(&dmq1); rsa->iqmp = mpz2BN(&iqmp); ret = 1; out: mp_clear_multi(&el, &p, &q, &n, &d, &dmp1, &dmq1, &iqmp, &t1, &t2, &t3, NULL); return ret; }
static int ltm_dh_generate_key(DH *dh) { mp_int pub, priv_key, g, p; int have_private_key = (dh->priv_key != NULL); int codes, times = 0; int res; if (dh->p == NULL || dh->g == NULL) return 0; while (times++ < DH_NUM_TRIES) { if (!have_private_key) { size_t bits = BN_num_bits(dh->p); if (dh->priv_key) BN_free(dh->priv_key); dh->priv_key = BN_new(); if (dh->priv_key == NULL) return 0; if (!BN_rand(dh->priv_key, bits - 1, 0, 0)) { BN_clear_free(dh->priv_key); dh->priv_key = NULL; return 0; } } if (dh->pub_key) BN_free(dh->pub_key); mp_init_multi(&pub, &priv_key, &g, &p, NULL); BN2mpz(&priv_key, dh->priv_key); BN2mpz(&g, dh->g); BN2mpz(&p, dh->p); res = mp_exptmod(&g, &priv_key, &p, &pub); mp_clear_multi(&priv_key, &g, &p, NULL); if (res != 0) continue; dh->pub_key = mpz2BN(&pub); mp_clear(&pub); if (dh->pub_key == NULL) return 0; if (DH_check_pubkey(dh, dh->pub_key, &codes) && codes == 0) break; if (have_private_key) return 0; } if (times >= DH_NUM_TRIES) { if (!have_private_key && dh->priv_key) { BN_free(dh->priv_key); dh->priv_key = NULL; } if (dh->pub_key) { BN_free(dh->pub_key); dh->pub_key = NULL; } return 0; } return 1; }
static int gmp_rsa_generate_key(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb) { mpz_t el, p, q, n, d, dmp1, dmq1, iqmp, t1, t2, t3; int counter, ret; if (bits < 789) return -1; ret = -1; mpz_init(el); mpz_init(p); mpz_init(q); mpz_init(n); mpz_init(d); mpz_init(dmp1); mpz_init(dmq1); mpz_init(iqmp); mpz_init(t1); mpz_init(t2); mpz_init(t3); BN2mpz(el, e); /* generate p and q so that p != q and bits(pq) ~ bits */ counter = 0; do { BN_GENCB_call(cb, 2, counter++); random_num(p, bits / 2 + 1); mpz_nextprime(p, p); mpz_sub_ui(t1, p, 1); mpz_gcd(t2, t1, el); } while(mpz_cmp_ui(t2, 1) != 0); BN_GENCB_call(cb, 3, 0); counter = 0; do { BN_GENCB_call(cb, 2, counter++); random_num(q, bits / 2 + 1); mpz_nextprime(q, q); mpz_sub_ui(t1, q, 1); mpz_gcd(t2, t1, el); } while(mpz_cmp_ui(t2, 1) != 0); /* make p > q */ if (mpz_cmp(p, q) < 0) mpz_swap(p, q); BN_GENCB_call(cb, 3, 1); /* calculate n, n = p * q */ mpz_mul(n, p, q); /* calculate d, d = 1/e mod (p - 1)(q - 1) */ mpz_sub_ui(t1, p, 1); mpz_sub_ui(t2, q, 1); mpz_mul(t3, t1, t2); mpz_invert(d, el, t3); /* calculate dmp1 dmp1 = d mod (p-1) */ mpz_mod(dmp1, d, t1); /* calculate dmq1 dmq1 = d mod (q-1) */ mpz_mod(dmq1, d, t2); /* calculate iqmp iqmp = 1/q mod p */ mpz_invert(iqmp, q, p); /* fill in RSA key */ rsa->e = mpz2BN(el); rsa->p = mpz2BN(p); rsa->q = mpz2BN(q); rsa->n = mpz2BN(n); rsa->d = mpz2BN(d); rsa->dmp1 = mpz2BN(dmp1); rsa->dmq1 = mpz2BN(dmq1); rsa->iqmp = mpz2BN(iqmp); ret = 1; mpz_clear(el); mpz_clear(p); mpz_clear(q); mpz_clear(n); mpz_clear(d); mpz_clear(dmp1); mpz_clear(dmq1); mpz_clear(iqmp); mpz_clear(t1); mpz_clear(t2); mpz_clear(t3); return ret; }