2020-01-31 06:18:46 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* DH parameters from RFC7919 and RFC3526 */
|
|
|
|
|
2020-02-03 17:05:31 +08:00
|
|
|
/*
|
|
|
|
* DH low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2020-01-31 06:18:46 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "internal/cryptlib.h"
|
2020-04-15 23:14:00 +08:00
|
|
|
#include "internal/ffc.h"
|
2020-01-31 06:18:46 +08:00
|
|
|
#include "dh_local.h"
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/objects.h>
|
2020-12-02 02:11:59 +08:00
|
|
|
#include "internal/nelem.h"
|
2020-02-16 11:03:46 +08:00
|
|
|
#include "crypto/dh.h"
|
2020-04-15 19:02:52 +08:00
|
|
|
#include "e_os.h" /* strcasecmp */
|
2020-03-07 05:47:58 +08:00
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
static DH *dh_param_init(OSSL_LIB_CTX *libctx, const DH_NAMED_GROUP *group)
|
2020-01-31 06:18:46 +08:00
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
DH *dh = dh_new_ex(libctx);
|
2020-01-31 06:18:46 +08:00
|
|
|
|
|
|
|
if (dh == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
ossl_ffc_named_group_set_pqg(&dh->params, group);
|
|
|
|
dh->params.nid = ossl_ffc_named_group_get_uid(group);
|
|
|
|
dh->length = BN_num_bits(dh->params.q);
|
2020-01-31 06:18:46 +08:00
|
|
|
dh->dirty_cnt++;
|
|
|
|
return dh;
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
DH *dh_new_by_nid_ex(OSSL_LIB_CTX *libctx, int nid)
|
2020-01-31 06:18:46 +08:00
|
|
|
{
|
2020-12-02 02:11:59 +08:00
|
|
|
const DH_NAMED_GROUP *group;
|
2020-03-07 05:47:58 +08:00
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
if ((group = ossl_ffc_uid_to_dh_named_group(nid)) != NULL)
|
|
|
|
return dh_param_init(libctx, group);
|
2020-04-15 23:14:00 +08:00
|
|
|
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DH, DH_R_INVALID_PARAMETER_NID);
|
2020-03-07 05:47:58 +08:00
|
|
|
return NULL;
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2020-02-16 11:03:46 +08:00
|
|
|
DH *DH_new_by_nid(int nid)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return dh_new_by_nid_ex(NULL, nid);
|
2020-04-15 23:14:00 +08:00
|
|
|
}
|
|
|
|
|
2020-04-20 09:07:38 +08:00
|
|
|
void dh_cache_named_group(DH *dh)
|
2020-01-31 06:18:46 +08:00
|
|
|
{
|
2020-12-02 02:11:59 +08:00
|
|
|
const DH_NAMED_GROUP *group;
|
2020-03-07 05:47:58 +08:00
|
|
|
|
|
|
|
if (dh == NULL)
|
2020-04-20 09:07:38 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
dh->params.nid = NID_undef; /* flush cached value */
|
2020-01-31 06:18:46 +08:00
|
|
|
|
2020-04-20 09:07:38 +08:00
|
|
|
/* Exit if p or g is not set */
|
|
|
|
if (dh->params.p == NULL
|
|
|
|
|| dh->params.g == NULL)
|
|
|
|
return;
|
2020-01-31 06:18:46 +08:00
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
if ((group = ossl_ffc_numbers_to_dh_named_group(dh->params.p,
|
|
|
|
dh->params.q,
|
|
|
|
dh->params.g)) != NULL) {
|
|
|
|
if (dh->params.q == NULL)
|
|
|
|
dh->params.q = (BIGNUM *)ossl_ffc_named_group_get_q(group);
|
|
|
|
/* cache the nid */
|
|
|
|
dh->params.nid = ossl_ffc_named_group_get_uid(group);
|
|
|
|
dh->length = BN_num_bits(dh->params.q);
|
|
|
|
dh->dirty_cnt++;
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|
2020-04-20 09:07:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int DH_get_nid(const DH *dh)
|
|
|
|
{
|
|
|
|
if (dh == NULL)
|
|
|
|
return NID_undef;
|
|
|
|
|
|
|
|
return dh->params.nid;
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|