2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/ffc.h"
11 #include "internal/nelem.h"
12 #include "crypto/bn_dh.h"
13 #include "e_os.h" /* strcasecmp */
17 # define FFDHE(sz) { \
18 SN_ffdhe##sz, NID_ffdhe##sz, \
20 &ossl_bignum_ffdhe##sz##_p, &ossl_bignum_ffdhe##sz##_q, \
21 &ossl_bignum_const_2, \
25 SN_modp_##sz, NID_modp_##sz, \
27 &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \
28 &ossl_bignum_const_2 \
31 # define RFC5114(name, uid, sz, tag) { \
34 &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \
35 &ossl_bignum_dh##tag##_g \
40 # define FFDHE(sz) { SN_ffdhe##sz, NID_ffdhe##sz }
41 # define MODP(sz) { SN_modp_##sz, NID_modp_##sz }
42 # define RFC5114(name, uid, sz, tag) { name, uid }
46 struct dh_named_group_st {
57 static const DH_NAMED_GROUP dh_named_groups[] = {
72 * Additional dh named groups from RFC 5114 that have a different g.
73 * The uid can be any unique identifier.
76 RFC5114("dh_1024_160", 1, 1024, 1024_160),
77 RFC5114("dh_2048_224", 2, 2048, 2048_224),
78 RFC5114("dh_2048_256", 3, 2048, 2048_256),
82 const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name)
86 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
87 if (strcasecmp(dh_named_groups[i].name, name) == 0)
88 return &dh_named_groups[i];
93 const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid)
97 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
98 if (dh_named_groups[i].uid == uid)
99 return &dh_named_groups[i];
104 #ifndef OPENSSL_NO_DH
105 const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
111 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
112 /* Keep searching until a matching p and g is found */
113 if (BN_cmp(p, dh_named_groups[i].p) == 0
114 && BN_cmp(g, dh_named_groups[i].g) == 0
115 /* Verify q is correct if it exists */
116 && (q == NULL || BN_cmp(q, dh_named_groups[i].q) == 0))
117 return &dh_named_groups[i];
123 int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group)
130 const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *group)
137 #ifndef OPENSSL_NO_DH
138 const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group)
145 int ossl_ffc_named_group_set_pqg(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group)
147 if (ffc == NULL || group == NULL)
150 ossl_ffc_params_set0_pqg(ffc, (BIGNUM *)group->p, (BIGNUM *)group->q,
153 /* flush the cached nid, The DH layer is responsible for caching */
154 ffc->nid = NID_undef;