2 * Copyright 2020-2022 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"
16 # define FFDHE(sz) { \
17 SN_ffdhe##sz, NID_ffdhe##sz, \
19 &ossl_bignum_ffdhe##sz##_p, &ossl_bignum_ffdhe##sz##_q, \
20 &ossl_bignum_const_2, \
24 SN_modp_##sz, NID_modp_##sz, \
26 &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \
27 &ossl_bignum_const_2 \
30 # define RFC5114(name, uid, sz, tag) { \
33 &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \
34 &ossl_bignum_dh##tag##_g \
39 # define FFDHE(sz) { SN_ffdhe##sz, NID_ffdhe##sz }
40 # define MODP(sz) { SN_modp_##sz, NID_modp_##sz }
41 # define RFC5114(name, uid, sz, tag) { name, uid }
45 struct dh_named_group_st {
56 static const DH_NAMED_GROUP dh_named_groups[] = {
71 * Additional dh named groups from RFC 5114 that have a different g.
72 * The uid can be any unique identifier.
75 RFC5114("dh_1024_160", 1, 1024, 1024_160),
76 RFC5114("dh_2048_224", 2, 2048, 2048_224),
77 RFC5114("dh_2048_256", 3, 2048, 2048_256),
81 const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name)
85 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
86 if (OPENSSL_strcasecmp(dh_named_groups[i].name, name) == 0)
87 return &dh_named_groups[i];
92 const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid)
96 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
97 if (dh_named_groups[i].uid == uid)
98 return &dh_named_groups[i];
103 #ifndef OPENSSL_NO_DH
104 const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
110 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
111 /* Keep searching until a matching p and g is found */
112 if (BN_cmp(p, dh_named_groups[i].p) == 0
113 && BN_cmp(g, dh_named_groups[i].g) == 0
114 /* Verify q is correct if it exists */
115 && (q == NULL || BN_cmp(q, dh_named_groups[i].q) == 0))
116 return &dh_named_groups[i];
122 int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group)
129 const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *group)
136 #ifndef OPENSSL_NO_DH
137 const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group)
144 int ossl_ffc_named_group_set_pqg(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group)
146 if (ffc == NULL || group == NULL)
149 ossl_ffc_params_set0_pqg(ffc, (BIGNUM *)group->p, (BIGNUM *)group->q,
152 /* flush the cached nid, The DH layer is responsible for caching */
153 ffc->nid = NID_undef;