1*fbadb84bStb /* $OpenBSD: dh_key.c,v 1.42 2024/05/09 20:43:36 tb Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker * All rights reserved.
45b37fcf3Sryker *
55b37fcf3Sryker * This package is an SSL implementation written
65b37fcf3Sryker * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker *
95b37fcf3Sryker * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker * the following conditions are aheared to. The following conditions
115b37fcf3Sryker * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker * lhash, DES, etc., code; not just the SSL code. The SSL documentation
135b37fcf3Sryker * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker *
165b37fcf3Sryker * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker * the code are not to be removed.
185b37fcf3Sryker * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker * as the author of the parts of the library used.
205b37fcf3Sryker * This can be in the form of a textual message at program startup or
215b37fcf3Sryker * in documentation (online or textual) provided with the package.
225b37fcf3Sryker *
235b37fcf3Sryker * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker * modification, are permitted provided that the following conditions
255b37fcf3Sryker * are met:
265b37fcf3Sryker * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker * notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker * notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker * documentation and/or other materials provided with the distribution.
315b37fcf3Sryker * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker * must display the following acknowledgement:
335b37fcf3Sryker * "This product includes cryptographic software written by
345b37fcf3Sryker * Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker * The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker * being used are not cryptographic related :-).
375b37fcf3Sryker * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker * the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker *
415b37fcf3Sryker * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker * SUCH DAMAGE.
525b37fcf3Sryker *
535b37fcf3Sryker * The licence and distribution terms for any publically available version or
545b37fcf3Sryker * derivative of this code cannot be changed. i.e. this code cannot simply be
555b37fcf3Sryker * copied and put under another distribution licence
565b37fcf3Sryker * [including the GNU Public Licence.]
575b37fcf3Sryker */
585b37fcf3Sryker
595b37fcf3Sryker #include <stdio.h>
60b6ab114eSjsing
61913ec974Sbeck #include <openssl/bn.h>
62913ec974Sbeck #include <openssl/dh.h>
63b6ab114eSjsing #include <openssl/err.h>
645b37fcf3Sryker
65c9675a23Stb #include "bn_local.h"
66a69386beStb #include "dh_local.h"
673a88f7afSbeck
685b87c283Smiod static int
generate_key(DH * dh)695b87c283Smiod generate_key(DH *dh)
70ba5406e9Sbeck {
715b37fcf3Sryker int ok = 0;
72da347917Sbeck unsigned l;
73da347917Sbeck BN_CTX *ctx;
746d388760Sdjm BN_MONT_CTX *mont = NULL;
7512347e81Stb BIGNUM *pub_key = NULL, *priv_key = NULL;
765b37fcf3Sryker
7754293529Ssthen if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
7854293529Ssthen DHerror(DH_R_MODULUS_TOO_LARGE);
7954293529Ssthen return 0;
8054293529Ssthen }
8154293529Ssthen
82da347917Sbeck ctx = BN_CTX_new();
835b87c283Smiod if (ctx == NULL)
845b87c283Smiod goto err;
855b37fcf3Sryker
864435d402Stb if ((priv_key = dh->priv_key) == NULL) {
87eb7398b7Stb if ((priv_key = BN_new()) == NULL)
885b87c283Smiod goto err;
89eb7398b7Stb }
905b37fcf3Sryker
91f03cf33cStb if ((pub_key = dh->pub_key) == NULL) {
92eb7398b7Stb if ((pub_key = BN_new()) == NULL)
935b87c283Smiod goto err;
94eb7398b7Stb }
955b37fcf3Sryker
965b87c283Smiod if (dh->flags & DH_FLAG_CACHE_MONT_P) {
974fcf65c5Sdjm mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
986d388760Sdjm CRYPTO_LOCK_DH, dh->p, ctx);
996d388760Sdjm if (!mont)
1006d388760Sdjm goto err;
101913ec974Sbeck }
102913ec974Sbeck
1034435d402Stb if (dh->priv_key == NULL) {
1045b87c283Smiod if (dh->q) {
10512347e81Stb if (!bn_rand_interval(priv_key, 2, dh->q))
1068a144a0fStb goto err;
1075b87c283Smiod } else {
1085cdd308eSdjm /* secret exponent length */
1095cdd308eSdjm l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
1105b87c283Smiod if (!BN_rand(priv_key, l, 0, 0))
1115b87c283Smiod goto err;
112da347917Sbeck }
1135cdd308eSdjm }
1146d388760Sdjm
115a5369e1aStb if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, priv_key, dh->p, ctx,
116a5369e1aStb mont))
1175b87c283Smiod goto err;
1185b37fcf3Sryker
1195b37fcf3Sryker dh->pub_key = pub_key;
1205b37fcf3Sryker dh->priv_key = priv_key;
1215b37fcf3Sryker ok = 1;
1225b37fcf3Sryker err:
1235b37fcf3Sryker if (ok != 1)
1245067ae9fSbeck DHerror(ERR_R_BN_LIB);
1255b37fcf3Sryker
126eb7398b7Stb if (dh->pub_key == NULL)
1275b87c283Smiod BN_free(pub_key);
128eb7398b7Stb if (dh->priv_key == NULL)
1295b87c283Smiod BN_free(priv_key);
130da347917Sbeck BN_CTX_free(ctx);
13112347e81Stb
1325b87c283Smiod return ok;
1335b37fcf3Sryker }
1345b37fcf3Sryker
1355b87c283Smiod static int
compute_key(unsigned char * key,const BIGNUM * pub_key,DH * dh)1365b87c283Smiod compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1375b37fcf3Sryker {
1384fcf65c5Sdjm BN_CTX *ctx = NULL;
1396d388760Sdjm BN_MONT_CTX *mont = NULL;
1405b37fcf3Sryker BIGNUM *tmp;
1415b37fcf3Sryker int ret = -1;
1423f6aedb7Sdjm int check_result;
1435b37fcf3Sryker
1445b87c283Smiod if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
1455067ae9fSbeck DHerror(DH_R_MODULUS_TOO_LARGE);
1464fcf65c5Sdjm goto err;
147ee1f122aSpvalchev }
148ee1f122aSpvalchev
149da347917Sbeck ctx = BN_CTX_new();
1505b87c283Smiod if (ctx == NULL)
1515b87c283Smiod goto err;
152da347917Sbeck BN_CTX_start(ctx);
153aa389b8cSjsing if ((tmp = BN_CTX_get(ctx)) == NULL)
154aa389b8cSjsing goto err;
1555b37fcf3Sryker
1565b87c283Smiod if (dh->priv_key == NULL) {
1575067ae9fSbeck DHerror(DH_R_NO_PRIVATE_VALUE);
1585b37fcf3Sryker goto err;
1595b37fcf3Sryker }
1606d388760Sdjm
1615b87c283Smiod if (dh->flags & DH_FLAG_CACHE_MONT_P) {
1624fcf65c5Sdjm mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
1636d388760Sdjm CRYPTO_LOCK_DH, dh->p, ctx);
1641b2bbd6aSbcook
1654fcf65c5Sdjm BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
1661b2bbd6aSbcook
1676d388760Sdjm if (!mont)
1686d388760Sdjm goto err;
1696d388760Sdjm }
1704fcf65c5Sdjm
1715b87c283Smiod if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
1725067ae9fSbeck DHerror(DH_R_INVALID_PUBKEY);
1733f6aedb7Sdjm goto err;
1743f6aedb7Sdjm }
1754fcf65c5Sdjm
1765b87c283Smiod if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx,
1775b87c283Smiod mont)) {
1785067ae9fSbeck DHerror(ERR_R_BN_LIB);
1795b37fcf3Sryker goto err;
1805b37fcf3Sryker }
1815b37fcf3Sryker
1825b37fcf3Sryker ret = BN_bn2bin(tmp, key);
1835b37fcf3Sryker err:
1845b87c283Smiod if (ctx != NULL) {
185da347917Sbeck BN_CTX_end(ctx);
186da347917Sbeck BN_CTX_free(ctx);
1876d388760Sdjm }
1885b87c283Smiod return ret;
1895b37fcf3Sryker }
190ba5406e9Sbeck
1915b87c283Smiod static int
dh_bn_mod_exp(const DH * dh,BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)1925b87c283Smiod dh_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1935b87c283Smiod const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
194ba5406e9Sbeck {
1953a88f7afSbeck return BN_mod_exp_mont_ct(r, a, p, m, ctx, m_ctx);
196ba5406e9Sbeck }
197ba5406e9Sbeck
1985b87c283Smiod static int
dh_init(DH * dh)1995b87c283Smiod dh_init(DH *dh)
200ba5406e9Sbeck {
201ba5406e9Sbeck dh->flags |= DH_FLAG_CACHE_MONT_P;
2025b87c283Smiod return 1;
203ba5406e9Sbeck }
204ba5406e9Sbeck
2055b87c283Smiod static int
dh_finish(DH * dh)2065b87c283Smiod dh_finish(DH *dh)
207ba5406e9Sbeck {
2084fcf65c5Sdjm BN_MONT_CTX_free(dh->method_mont_p);
2095b87c283Smiod return 1;
210ba5406e9Sbeck }
211f5e89b37Stb
212f5e89b37Stb int
DH_generate_key(DH * dh)213f5e89b37Stb DH_generate_key(DH *dh)
214f5e89b37Stb {
215f5e89b37Stb return dh->meth->generate_key(dh);
216f5e89b37Stb }
217f5e89b37Stb LCRYPTO_ALIAS(DH_generate_key);
218f5e89b37Stb
219f5e89b37Stb int
DH_compute_key(unsigned char * key,const BIGNUM * pub_key,DH * dh)220f5e89b37Stb DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
221f5e89b37Stb {
222f5e89b37Stb return dh->meth->compute_key(key, pub_key, dh);
223f5e89b37Stb }
224f5e89b37Stb LCRYPTO_ALIAS(DH_compute_key);
225f5e89b37Stb
226*fbadb84bStb static const DH_METHOD dh_ossl = {
227f5e89b37Stb .name = "OpenSSL DH Method",
228f5e89b37Stb .generate_key = generate_key,
229f5e89b37Stb .compute_key = compute_key,
230f5e89b37Stb .bn_mod_exp = dh_bn_mod_exp,
231f5e89b37Stb .init = dh_init,
232f5e89b37Stb .finish = dh_finish,
233f5e89b37Stb };
234f5e89b37Stb
235f5e89b37Stb const DH_METHOD *
DH_OpenSSL(void)236f5e89b37Stb DH_OpenSSL(void)
237f5e89b37Stb {
238f5e89b37Stb return &dh_ossl;
239f5e89b37Stb }
240f5e89b37Stb LCRYPTO_ALIAS(DH_OpenSSL);
241