2016-05-18 03:38:09 +08:00
|
|
|
/*
|
2020-03-03 12:02:36 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2015-01-22 11:40:55 +08:00
|
|
|
*
|
2018-12-06 20:36:05 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* NB: These functions have been upgraded - the previous prototypes are in
|
|
|
|
* dh_depr.c as wrappers to these ones. - Geoff
|
2002-12-08 13:24:31 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-03 17:05:31 +08:00
|
|
|
/*
|
|
|
|
* DH low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
2020-03-03 12:02:36 +08:00
|
|
|
*
|
|
|
|
* NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
|
|
|
|
* states that no additional pairwise tests are required (apart from the tests
|
|
|
|
* specified in SP800-56A) when generating keys. Hence DH pairwise tests are
|
|
|
|
* omitted here.
|
2020-02-03 17:05:31 +08:00
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
2020-04-15 23:14:00 +08:00
|
|
|
#include <openssl/sha.h>
|
2020-02-06 20:28:36 +08:00
|
|
|
#include "crypto/dh.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "dh_local.h"
|
2012-06-04 06:00:21 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2015-01-22 11:40:55 +08:00
|
|
|
static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
|
|
|
|
BN_GENCB *cb);
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|
2020-02-06 20:28:36 +08:00
|
|
|
|
2020-06-17 09:33:16 +08:00
|
|
|
int dh_generate_ffc_parameters(DH *dh, int type, int pbits, int qbits,
|
|
|
|
BN_GENCB *cb)
|
2020-02-06 20:28:36 +08:00
|
|
|
{
|
|
|
|
int ret, res;
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2020-04-15 23:14:00 +08:00
|
|
|
if (type == DH_PARAMGEN_TYPE_FIPS_186_2)
|
|
|
|
ret = ffc_params_FIPS186_2_generate(dh->libctx, &dh->params,
|
|
|
|
FFC_PARAM_TYPE_DH,
|
2020-06-17 09:33:16 +08:00
|
|
|
pbits, qbits, &res, cb);
|
2020-04-15 23:14:00 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
ret = ffc_params_FIPS186_4_generate(dh->libctx, &dh->params,
|
|
|
|
FFC_PARAM_TYPE_DH,
|
2020-06-17 09:33:16 +08:00
|
|
|
pbits, qbits, &res, cb);
|
2020-02-06 20:28:36 +08:00
|
|
|
if (ret > 0)
|
|
|
|
dh->dirty_cnt++;
|
|
|
|
return ret;
|
|
|
|
}
|
2003-01-15 10:01:55 +08:00
|
|
|
|
2020-04-15 23:14:00 +08:00
|
|
|
int dh_get_named_group_uid_from_size(int pbits)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-02-06 20:28:36 +08:00
|
|
|
/*
|
|
|
|
* Just choose an approved safe prime group.
|
|
|
|
* The alternative to this is to generate FIPS186-4 domain parameters i.e.
|
2020-04-15 23:14:00 +08:00
|
|
|
* return dh_generate_ffc_parameters(ret, prime_len, 0, NULL, cb);
|
2020-06-06 03:27:34 +08:00
|
|
|
* As the FIPS186-4 generated params are for backwards compatibility,
|
2020-02-06 20:28:36 +08:00
|
|
|
* the safe prime group should be used as the default.
|
|
|
|
*/
|
2020-04-15 23:14:00 +08:00
|
|
|
int nid;
|
2020-02-06 20:28:36 +08:00
|
|
|
|
2020-04-15 23:14:00 +08:00
|
|
|
switch (pbits) {
|
2020-02-06 20:28:36 +08:00
|
|
|
case 2048:
|
|
|
|
nid = NID_ffdhe2048;
|
|
|
|
break;
|
|
|
|
case 3072:
|
|
|
|
nid = NID_ffdhe3072;
|
|
|
|
break;
|
|
|
|
case 4096:
|
|
|
|
nid = NID_ffdhe4096;
|
|
|
|
break;
|
|
|
|
case 6144:
|
|
|
|
nid = NID_ffdhe6144;
|
|
|
|
break;
|
|
|
|
case 8192:
|
|
|
|
nid = NID_ffdhe8192;
|
|
|
|
break;
|
|
|
|
/* unsupported prime_len */
|
|
|
|
default:
|
2020-04-15 23:14:00 +08:00
|
|
|
return NID_undef;
|
2020-02-06 20:28:36 +08:00
|
|
|
}
|
2020-04-15 23:14:00 +08:00
|
|
|
return nid;
|
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifdef FIPS_MODULE
|
2020-04-15 23:14:00 +08:00
|
|
|
|
|
|
|
static int dh_gen_named_group(OPENSSL_CTX *libctx, DH *ret, int prime_len)
|
|
|
|
{
|
|
|
|
DH *dh;
|
|
|
|
int ok = 0;
|
|
|
|
int nid = dh_get_named_group_uid_from_size(prime_len);
|
|
|
|
|
|
|
|
if (nid == NID_undef)
|
|
|
|
return 0;
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
dh = dh_new_by_nid_ex(libctx, nid);
|
2020-04-15 23:14:00 +08:00
|
|
|
if (dh != NULL
|
|
|
|
&& ffc_params_copy(&ret->params, &dh->params)) {
|
2020-02-06 20:28:36 +08:00
|
|
|
ok = 1;
|
|
|
|
ret->dirty_cnt++;
|
|
|
|
}
|
|
|
|
DH_free(dh);
|
|
|
|
return ok;
|
2020-04-15 23:14:00 +08:00
|
|
|
}
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|
2020-04-15 23:14:00 +08:00
|
|
|
|
|
|
|
int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
|
|
|
|
BN_GENCB *cb)
|
|
|
|
{
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifdef FIPS_MODULE
|
2020-04-15 23:14:00 +08:00
|
|
|
if (generator != 2)
|
|
|
|
return 0;
|
|
|
|
return dh_gen_named_group(ret->libctx, ret, prime_len);
|
2020-02-06 20:28:36 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ret->meth->generate_params)
|
|
|
|
return ret->meth->generate_params(ret, prime_len, generator, cb);
|
|
|
|
return dh_builtin_genparams(ret, prime_len, generator, cb);
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2003-01-15 10:01:55 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2014-12-28 10:48:40 +08:00
|
|
|
/*-
|
|
|
|
* We generate DH parameters as follows
|
2019-07-10 21:52:36 +08:00
|
|
|
* find a prime p which is prime_len bits long,
|
|
|
|
* where q=(p-1)/2 is also prime.
|
|
|
|
* In the following we assume that g is not 0, 1 or p-1, since it
|
|
|
|
* would generate only trivial subgroups.
|
|
|
|
* For this case, g is a generator of the order-q subgroup if
|
|
|
|
* g^q mod p == 1.
|
|
|
|
* Or in terms of the Legendre symbol: (g/p) == 1.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
|
|
|
* Having said all that,
|
|
|
|
* there is another special case method for the generators 2, 3 and 5.
|
2019-07-10 21:52:36 +08:00
|
|
|
* Using the quadratic reciprocity law it is possible to solve
|
|
|
|
* (g/p) == 1 for the special values 2, 3, 5:
|
|
|
|
* (2/p) == 1 if p mod 8 == 1 or 7.
|
|
|
|
* (3/p) == 1 if p mod 12 == 1 or 11.
|
|
|
|
* (5/p) == 1 if p mod 5 == 1 or 4.
|
|
|
|
* See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2019-07-10 21:52:36 +08:00
|
|
|
* Since all safe primes > 7 must satisfy p mod 12 == 11
|
|
|
|
* and all safe primes > 11 must satisfy p mod 5 != 1
|
|
|
|
* we can further improve the condition for g = 2, 3 and 5:
|
|
|
|
* for 2, p mod 24 == 23
|
|
|
|
* for 3, p mod 12 == 11
|
|
|
|
* for 5, p mod 60 == 59
|
2002-03-21 00:04:04 +08:00
|
|
|
*/
|
2015-01-22 11:40:55 +08:00
|
|
|
static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
|
|
|
|
BN_GENCB *cb)
|
|
|
|
{
|
|
|
|
BIGNUM *t1, *t2;
|
|
|
|
int g, ok = -1;
|
|
|
|
BN_CTX *ctx = NULL;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-07-23 04:50:19 +08:00
|
|
|
if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
|
|
|
|
DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prime_len < DH_MIN_MODULUS_BITS) {
|
|
|
|
DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
t1 = BN_CTX_get(ctx);
|
|
|
|
t2 = BN_CTX_get(ctx);
|
2017-06-22 18:52:29 +08:00
|
|
|
if (t2 == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2002-12-08 13:24:31 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Make sure 'ret' has the necessary elements */
|
2020-01-24 12:09:33 +08:00
|
|
|
if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (generator <= 1) {
|
|
|
|
DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (generator == DH_GENERATOR_2) {
|
|
|
|
if (!BN_set_word(t1, 24))
|
|
|
|
goto err;
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!BN_set_word(t2, 23))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
g = 2;
|
2015-02-04 00:20:56 +08:00
|
|
|
} else if (generator == DH_GENERATOR_5) {
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!BN_set_word(t1, 60))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!BN_set_word(t2, 59))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
g = 5;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* in the general case, don't worry if 'generator' is a generator or
|
|
|
|
* not: since we are using safe primes, it will generate either an
|
|
|
|
* order-q or an order-2q group, which both is OK
|
|
|
|
*/
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!BN_set_word(t1, 12))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!BN_set_word(t2, 11))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
g = generator;
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
if (!BN_generate_prime_ex(ret->params.p, prime_len, 1, t1, t2, cb))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (!BN_GENCB_call(cb, 3, 0))
|
|
|
|
goto err;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (!BN_set_word(ret->params.g, g))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2019-07-07 16:56:46 +08:00
|
|
|
ret->dirty_cnt++;
|
2015-01-22 11:40:55 +08:00
|
|
|
ok = 1;
|
|
|
|
err:
|
|
|
|
if (ok == -1) {
|
|
|
|
DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
|
|
|
|
ok = 0;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-03-19 07:58:09 +08:00
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return ok;
|
|
|
|
}
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|