155714Skris/* crypto/dh/dh_key.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/bn.h> 6255714Skris#include <openssl/rand.h> 6355714Skris#include <openssl/dh.h> 6455714Skris 6559191Skrisstatic int generate_key(DH *dh); 66109998Smarkmstatic int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); 67109998Smarkmstatic int dh_bn_mod_exp(const DH *dh, BIGNUM *r, 68280304Sjkim const BIGNUM *a, const BIGNUM *p, 69280304Sjkim const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 7059191Skrisstatic int dh_init(DH *dh); 7159191Skrisstatic int dh_finish(DH *dh); 7259191Skris 7355714Skrisint DH_generate_key(DH *dh) 74280304Sjkim{ 75238405Sjkim#ifdef OPENSSL_FIPS 76280304Sjkim if (FIPS_mode() && !(dh->meth->flags & DH_FLAG_FIPS_METHOD) 77280304Sjkim && !(dh->flags & DH_FLAG_NON_FIPS_ALLOW)) { 78280304Sjkim DHerr(DH_F_DH_GENERATE_KEY, DH_R_NON_FIPS_METHOD); 79280304Sjkim return 0; 80280304Sjkim } 81238405Sjkim#endif 82280304Sjkim return dh->meth->generate_key(dh); 83280304Sjkim} 8459191Skris 85109998Smarkmint DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) 86280304Sjkim{ 87238405Sjkim#ifdef OPENSSL_FIPS 88280304Sjkim if (FIPS_mode() && !(dh->meth->flags & DH_FLAG_FIPS_METHOD) 89280304Sjkim && !(dh->flags & DH_FLAG_NON_FIPS_ALLOW)) { 90280304Sjkim DHerr(DH_F_DH_COMPUTE_KEY, DH_R_NON_FIPS_METHOD); 91280304Sjkim return 0; 92280304Sjkim } 93238405Sjkim#endif 94280304Sjkim return dh->meth->compute_key(key, pub_key, dh); 95280304Sjkim} 9659191Skris 9759191Skrisstatic DH_METHOD dh_ossl = { 98280304Sjkim "OpenSSL DH Method", 99280304Sjkim generate_key, 100280304Sjkim compute_key, 101280304Sjkim dh_bn_mod_exp, 102280304Sjkim dh_init, 103280304Sjkim dh_finish, 104280304Sjkim 0, 105280304Sjkim NULL, 106280304Sjkim NULL 10759191Skris}; 10859191Skris 109109998Smarkmconst DH_METHOD *DH_OpenSSL(void) 11059191Skris{ 111280304Sjkim return &dh_ossl; 11259191Skris} 11359191Skris 11459191Skrisstatic int generate_key(DH *dh) 115280304Sjkim{ 116280304Sjkim int ok = 0; 117280304Sjkim int generate_new_key = 0; 118280304Sjkim unsigned l; 119280304Sjkim BN_CTX *ctx; 120280304Sjkim BN_MONT_CTX *mont = NULL; 121280304Sjkim BIGNUM *pub_key = NULL, *priv_key = NULL; 12255714Skris 123280304Sjkim ctx = BN_CTX_new(); 124280304Sjkim if (ctx == NULL) 125280304Sjkim goto err; 12655714Skris 127280304Sjkim if (dh->priv_key == NULL) { 128280304Sjkim priv_key = BN_new(); 129280304Sjkim if (priv_key == NULL) 130280304Sjkim goto err; 131280304Sjkim generate_new_key = 1; 132280304Sjkim } else 133280304Sjkim priv_key = dh->priv_key; 13455714Skris 135280304Sjkim if (dh->pub_key == NULL) { 136280304Sjkim pub_key = BN_new(); 137280304Sjkim if (pub_key == NULL) 138280304Sjkim goto err; 139280304Sjkim } else 140280304Sjkim pub_key = dh->pub_key; 14155714Skris 142280304Sjkim if (dh->flags & DH_FLAG_CACHE_MONT_P) { 143280304Sjkim mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, 144280304Sjkim CRYPTO_LOCK_DH, dh->p, ctx); 145280304Sjkim if (!mont) 146280304Sjkim goto err; 147280304Sjkim } 148160814Ssimon 149280304Sjkim if (generate_new_key) { 150280304Sjkim if (dh->q) { 151280304Sjkim do { 152280304Sjkim if (!BN_rand_range(priv_key, dh->q)) 153280304Sjkim goto err; 154280304Sjkim } 155280304Sjkim while (BN_is_zero(priv_key) || BN_is_one(priv_key)); 156280304Sjkim } else { 157280304Sjkim /* secret exponent length */ 158280304Sjkim l = dh->length ? dh->length : BN_num_bits(dh->p) - 1; 159280304Sjkim if (!BN_rand(priv_key, l, 0, 0)) 160280304Sjkim goto err; 161280304Sjkim } 162280304Sjkim } 16355714Skris 164280304Sjkim { 165280304Sjkim BIGNUM local_prk; 166280304Sjkim BIGNUM *prk; 167160814Ssimon 168280304Sjkim if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) { 169280304Sjkim BN_init(&local_prk); 170280304Sjkim prk = &local_prk; 171280304Sjkim BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); 172280304Sjkim } else 173280304Sjkim prk = priv_key; 174160814Ssimon 175280304Sjkim if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) 176280304Sjkim goto err; 177280304Sjkim } 178160814Ssimon 179280304Sjkim dh->pub_key = pub_key; 180280304Sjkim dh->priv_key = priv_key; 181280304Sjkim ok = 1; 182280304Sjkim err: 183280304Sjkim if (ok != 1) 184280304Sjkim DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB); 18555714Skris 186280304Sjkim if ((pub_key != NULL) && (dh->pub_key == NULL)) 187280304Sjkim BN_free(pub_key); 188280304Sjkim if ((priv_key != NULL) && (dh->priv_key == NULL)) 189280304Sjkim BN_free(priv_key); 190280304Sjkim BN_CTX_free(ctx); 191280304Sjkim return (ok); 192280304Sjkim} 19355714Skris 194109998Smarkmstatic int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) 195280304Sjkim{ 196280304Sjkim BN_CTX *ctx = NULL; 197280304Sjkim BN_MONT_CTX *mont = NULL; 198280304Sjkim BIGNUM *tmp; 199280304Sjkim int ret = -1; 200280304Sjkim int check_result; 20155714Skris 202280304Sjkim if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) { 203280304Sjkim DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE); 204280304Sjkim goto err; 205280304Sjkim } 206162911Ssimon 207280304Sjkim ctx = BN_CTX_new(); 208280304Sjkim if (ctx == NULL) 209280304Sjkim goto err; 210280304Sjkim BN_CTX_start(ctx); 211280304Sjkim tmp = BN_CTX_get(ctx); 212160814Ssimon 213280304Sjkim if (dh->priv_key == NULL) { 214280304Sjkim DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE); 215280304Sjkim goto err; 216280304Sjkim } 21755714Skris 218280304Sjkim if (dh->flags & DH_FLAG_CACHE_MONT_P) { 219280304Sjkim mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, 220280304Sjkim CRYPTO_LOCK_DH, dh->p, ctx); 221280304Sjkim if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) { 222280304Sjkim /* XXX */ 223280304Sjkim BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); 224280304Sjkim } 225280304Sjkim if (!mont) 226280304Sjkim goto err; 227280304Sjkim } 228160814Ssimon 229280304Sjkim if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) { 230280304Sjkim DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY); 231280304Sjkim goto err; 232280304Sjkim } 23355714Skris 234280304Sjkim if (!dh-> 235280304Sjkim meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) { 236280304Sjkim DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB); 237280304Sjkim goto err; 238280304Sjkim } 23959191Skris 240280304Sjkim ret = BN_bn2bin(tmp, key); 241280304Sjkim err: 242280304Sjkim if (ctx != NULL) { 243280304Sjkim BN_CTX_end(ctx); 244280304Sjkim BN_CTX_free(ctx); 245280304Sjkim } 246280304Sjkim return (ret); 247280304Sjkim} 248280304Sjkim 249109998Smarkmstatic int dh_bn_mod_exp(const DH *dh, BIGNUM *r, 250280304Sjkim const BIGNUM *a, const BIGNUM *p, 251280304Sjkim const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 252280304Sjkim{ 253280304Sjkim /* 254280304Sjkim * If a is only one word long and constant time is false, use the faster 255280304Sjkim * exponenentiation function. 256280304Sjkim */ 257280304Sjkim if (a->top == 1 && ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) != 0)) { 258280304Sjkim BN_ULONG A = a->d[0]; 259280304Sjkim return BN_mod_exp_mont_word(r, A, p, m, ctx, m_ctx); 260280304Sjkim } else 261280304Sjkim return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); 262280304Sjkim} 26359191Skris 26459191Skrisstatic int dh_init(DH *dh) 265280304Sjkim{ 266280304Sjkim dh->flags |= DH_FLAG_CACHE_MONT_P; 267280304Sjkim return (1); 268280304Sjkim} 26959191Skris 27059191Skrisstatic int dh_finish(DH *dh) 271280304Sjkim{ 272280304Sjkim if (dh->method_mont_p) 273280304Sjkim BN_MONT_CTX_free(dh->method_mont_p); 274280304Sjkim return (1); 275280304Sjkim} 276