Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/ffc/ffc_dh.c

raw
1 /*
2 * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include "internal/ffc.h"
11 #include "internal/nelem.h"
12 #include "crypto/bn_dh.h"
13
14 #ifndef OPENSSL_NO_DH
15
16 #define FFDHE(sz, keylength) { \
17 SN_ffdhe##sz, \
18 NID_ffdhe##sz, \
19 sz, \
20 keylength, \
21 &ossl_bignum_ffdhe##sz##_p, \
22 &ossl_bignum_ffdhe##sz##_q, \
23 &ossl_bignum_const_2, \
24 }
25
26 #define MODP(sz, keylength) { \
27 SN_modp_##sz, NID_modp_##sz, \
28 sz, \
29 keylength, \
30 &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \
31 &ossl_bignum_const_2 \
32 }
33
34 #define RFC5114(name, uid, sz, tag) { \
35 name, uid, \
36 sz, \
37 0, \
38 &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \
39 &ossl_bignum_dh##tag##_g \
40 }
41
42 #else
43
44 #define FFDHE(sz, keylength) { SN_ffdhe##sz, NID_ffdhe##sz }
45 #define MODP(sz, keylength) { SN_modp_##sz, NID_modp_##sz }
46 #define RFC5114(name, uid, sz, tag) { name, uid }
47
48 #endif
49
50 struct dh_named_group_st {
51 const char *name;
52 int uid;
53 #ifndef OPENSSL_NO_DH
54 int32_t nbits;
55 int keylength;
56 const BIGNUM *p;
57 const BIGNUM *q;
58 const BIGNUM *g;
59 #endif
60 };
61
62 /*
63 * The private key length values are taken from RFC7919 with the values for
64 * MODP primes given the same lengths as the equivalent FFDHE.
65 * The MODP 1536 value is approximated.
66 */
67 static const DH_NAMED_GROUP dh_named_groups[] = {
68 FFDHE(2048, 225),
69 FFDHE(3072, 275),
70 FFDHE(4096, 325),
71 FFDHE(6144, 375),
72 FFDHE(8192, 400),
73 #ifndef FIPS_MODULE
74 MODP(1536, 200),
75 #endif
76 MODP(2048, 225),
77 MODP(3072, 275),
78 MODP(4096, 325),
79 MODP(6144, 375),
80 MODP(8192, 400),
81 /*
82 * Additional dh named groups from RFC 5114 that have a different g.
83 * The uid can be any unique identifier.
84 */
85 #ifndef FIPS_MODULE
86 RFC5114("dh_1024_160", 1, 1024, 1024_160),
87 RFC5114("dh_2048_224", 2, 2048, 2048_224),
88 RFC5114("dh_2048_256", 3, 2048, 2048_256),
89 #endif
90 };
91
92 const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name)
93 {
94 size_t i;
95
96 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
97 if (OPENSSL_strcasecmp(dh_named_groups[i].name, name) == 0)
98 return &dh_named_groups[i];
99 }
100 return NULL;
101 }
102
103 const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid)
104 {
105 size_t i;
106
107 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
108 if (dh_named_groups[i].uid == uid)
109 return &dh_named_groups[i];
110 }
111 return NULL;
112 }
113
114 #ifndef OPENSSL_NO_DH
115 const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
116 const BIGNUM *q,
117 const BIGNUM *g)
118 {
119 size_t i;
120
121 for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
122 /* Keep searching until a matching p and g is found */
123 if (BN_cmp(p, dh_named_groups[i].p) == 0
124 && BN_cmp(g, dh_named_groups[i].g) == 0
125 /* Verify q is correct if it exists */
126 && (q == NULL || BN_cmp(q, dh_named_groups[i].q) == 0))
127 return &dh_named_groups[i];
128 }
129 return NULL;
130 }
131 #endif
132
133 int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group)
134 {
135 if (group == NULL)
136 return NID_undef;
137 return group->uid;
138 }
139
140 const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *group)
141 {
142 if (group == NULL)
143 return NULL;
144 return group->name;
145 }
146
147 #ifndef OPENSSL_NO_DH
148 int ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group)
149 {
150 if (group == NULL)
151 return 0;
152 return group->keylength;
153 }
154
155 const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group)
156 {
157 if (group == NULL)
158 return NULL;
159 return group->q;
160 }
161
162 int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group)
163 {
164 if (ffc == NULL || group == NULL)
165 return 0;
166
167 ossl_ffc_params_set0_pqg(ffc, (BIGNUM *)group->p, (BIGNUM *)group->q,
168 (BIGNUM *)group->g);
169 ffc->keylength = group->keylength;
170
171 /* flush the cached nid, The DH layer is responsible for caching */
172 ffc->nid = NID_undef;
173 return 1;
174 }
175 #endif