/src/openssl33/crypto/ffc/ffc_params.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2023 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 <string.h> /* memset */ |
11 | | #include <openssl/core_names.h> |
12 | | #include "internal/ffc.h" |
13 | | #include "internal/param_build_set.h" |
14 | | #include "internal/nelem.h" |
15 | | |
16 | | #ifndef FIPS_MODULE |
17 | | #include <openssl/asn1.h> /* ossl_ffc_params_print */ |
18 | | #endif |
19 | | |
20 | | void ossl_ffc_params_init(FFC_PARAMS *params) |
21 | 1.86M | { |
22 | 1.86M | memset(params, 0, sizeof(*params)); |
23 | 1.86M | params->pcounter = -1; |
24 | 1.86M | params->gindex = FFC_UNVERIFIABLE_GINDEX; |
25 | 1.86M | params->flags = FFC_PARAM_FLAG_VALIDATE_PQG; |
26 | 1.86M | } |
27 | | |
28 | | void ossl_ffc_params_cleanup(FFC_PARAMS *params) |
29 | 935k | { |
30 | 935k | BN_free(params->p); |
31 | 935k | BN_free(params->q); |
32 | 935k | BN_free(params->g); |
33 | 935k | BN_free(params->j); |
34 | 935k | OPENSSL_free(params->seed); |
35 | 935k | ossl_ffc_params_init(params); |
36 | 935k | } |
37 | | |
38 | | void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
39 | 148k | { |
40 | 148k | if (p != NULL && p != d->p) { |
41 | 148k | BN_free(d->p); |
42 | 148k | d->p = p; |
43 | 148k | } |
44 | 148k | if (q != NULL && q != d->q) { |
45 | 137k | BN_free(d->q); |
46 | 137k | d->q = q; |
47 | 137k | } |
48 | 148k | if (g != NULL && g != d->g) { |
49 | 148k | BN_free(d->g); |
50 | 148k | d->g = g; |
51 | 148k | } |
52 | 148k | } |
53 | | |
54 | | void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p, |
55 | | const BIGNUM **q, const BIGNUM **g) |
56 | 83.8k | { |
57 | 83.8k | if (p != NULL) |
58 | 83.8k | *p = d->p; |
59 | 83.8k | if (q != NULL) |
60 | 72.3k | *q = d->q; |
61 | 83.8k | if (g != NULL) |
62 | 72.3k | *g = d->g; |
63 | 83.8k | } |
64 | | |
65 | | /* j is the 'cofactor' that is optionally output for ASN1. */ |
66 | | void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j) |
67 | 144k | { |
68 | 144k | BN_free(d->j); |
69 | 144k | d->j = NULL; |
70 | 144k | if (j != NULL) |
71 | 7.86k | d->j = j; |
72 | 144k | } |
73 | | |
74 | | int ossl_ffc_params_set_seed(FFC_PARAMS *params, |
75 | | const unsigned char *seed, size_t seedlen) |
76 | 4.70k | { |
77 | 4.70k | if (params->seed != NULL) { |
78 | 0 | if (params->seed == seed) |
79 | 0 | return 1; |
80 | 0 | OPENSSL_free(params->seed); |
81 | 0 | } |
82 | | |
83 | 4.70k | if (seed != NULL && seedlen > 0) { |
84 | 355 | params->seed = OPENSSL_memdup(seed, seedlen); |
85 | 355 | if (params->seed == NULL) |
86 | 0 | return 0; |
87 | 355 | params->seedlen = seedlen; |
88 | 4.34k | } else { |
89 | 4.34k | params->seed = NULL; |
90 | 4.34k | params->seedlen = 0; |
91 | 4.34k | } |
92 | 4.70k | return 1; |
93 | 4.70k | } |
94 | | |
95 | | void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index) |
96 | 0 | { |
97 | 0 | params->gindex = index; |
98 | 0 | } |
99 | | |
100 | | void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index) |
101 | 0 | { |
102 | 0 | params->pcounter = index; |
103 | 0 | } |
104 | | |
105 | | void ossl_ffc_params_set_h(FFC_PARAMS *params, int index) |
106 | 0 | { |
107 | 0 | params->h = index; |
108 | 0 | } |
109 | | |
110 | | void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags) |
111 | 0 | { |
112 | 0 | params->flags = flags; |
113 | 0 | } |
114 | | |
115 | | void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags, |
116 | | int enable) |
117 | 159k | { |
118 | 159k | if (enable) |
119 | 102k | params->flags |= flags; |
120 | 56.6k | else |
121 | 56.6k | params->flags &= ~flags; |
122 | 159k | } |
123 | | |
124 | | void ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props) |
125 | 0 | { |
126 | 0 | params->mdname = alg; |
127 | 0 | params->mdprops = props; |
128 | 0 | } |
129 | | |
130 | | int ossl_ffc_params_set_validate_params(FFC_PARAMS *params, |
131 | | const unsigned char *seed, |
132 | | size_t seedlen, int counter) |
133 | 437 | { |
134 | 437 | if (!ossl_ffc_params_set_seed(params, seed, seedlen)) |
135 | 0 | return 0; |
136 | 437 | params->pcounter = counter; |
137 | 437 | return 1; |
138 | 437 | } |
139 | | |
140 | | void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params, |
141 | | unsigned char **seed, size_t *seedlen, |
142 | | int *pcounter) |
143 | 399 | { |
144 | 399 | if (seed != NULL) |
145 | 399 | *seed = params->seed; |
146 | 399 | if (seedlen != NULL) |
147 | 399 | *seedlen = params->seedlen; |
148 | 399 | if (pcounter != NULL) |
149 | 399 | *pcounter = params->pcounter; |
150 | 399 | } |
151 | | |
152 | | static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src) |
153 | 78.2k | { |
154 | 78.2k | BIGNUM *a; |
155 | | |
156 | | /* |
157 | | * If source is read only just copy the pointer, so |
158 | | * we don't have to reallocate it. |
159 | | */ |
160 | 78.2k | if (src == NULL) |
161 | 25.3k | a = NULL; |
162 | 52.9k | else if (BN_get_flags(src, BN_FLG_STATIC_DATA) |
163 | 3.08k | && !BN_get_flags(src, BN_FLG_MALLOCED)) |
164 | 3.08k | a = (BIGNUM *)src; |
165 | 49.8k | else if ((a = BN_dup(src)) == NULL) |
166 | 0 | return 0; |
167 | 78.2k | BN_clear_free(*dst); |
168 | 78.2k | *dst = a; |
169 | 78.2k | return 1; |
170 | 78.2k | } |
171 | | |
172 | | int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src) |
173 | 19.5k | { |
174 | 19.5k | if (!ffc_bn_cpy(&dst->p, src->p) |
175 | 19.5k | || !ffc_bn_cpy(&dst->g, src->g) |
176 | 19.5k | || !ffc_bn_cpy(&dst->q, src->q) |
177 | 19.5k | || !ffc_bn_cpy(&dst->j, src->j)) |
178 | 0 | return 0; |
179 | | |
180 | 19.5k | dst->mdname = src->mdname; |
181 | 19.5k | dst->mdprops = src->mdprops; |
182 | 19.5k | OPENSSL_free(dst->seed); |
183 | 19.5k | dst->seedlen = src->seedlen; |
184 | 19.5k | if (src->seed != NULL) { |
185 | 220 | dst->seed = OPENSSL_memdup(src->seed, src->seedlen); |
186 | 220 | if (dst->seed == NULL) |
187 | 0 | return 0; |
188 | 19.3k | } else { |
189 | 19.3k | dst->seed = NULL; |
190 | 19.3k | } |
191 | 19.5k | dst->nid = src->nid; |
192 | 19.5k | dst->pcounter = src->pcounter; |
193 | 19.5k | dst->h = src->h; |
194 | 19.5k | dst->gindex = src->gindex; |
195 | 19.5k | dst->flags = src->flags; |
196 | 19.5k | dst->keylength = src->keylength; |
197 | 19.5k | return 1; |
198 | 19.5k | } |
199 | | |
200 | | int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q) |
201 | 111k | { |
202 | 111k | return BN_cmp(a->p, b->p) == 0 |
203 | 111k | && BN_cmp(a->g, b->g) == 0 |
204 | 111k | && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */ |
205 | 111k | } |
206 | | |
207 | | int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld, |
208 | | OSSL_PARAM params[]) |
209 | 181k | { |
210 | 181k | int test_flags; |
211 | | |
212 | 181k | if (ffc->p != NULL |
213 | 181k | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p)) |
214 | 0 | return 0; |
215 | 181k | if (ffc->q != NULL |
216 | 159k | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q)) |
217 | 0 | return 0; |
218 | 181k | if (ffc->g != NULL |
219 | 181k | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g)) |
220 | 0 | return 0; |
221 | 181k | if (ffc->j != NULL |
222 | 6.85k | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR, |
223 | 6.85k | ffc->j)) |
224 | 0 | return 0; |
225 | 181k | if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX, |
226 | 181k | ffc->gindex)) |
227 | 0 | return 0; |
228 | 181k | if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER, |
229 | 181k | ffc->pcounter)) |
230 | 0 | return 0; |
231 | 181k | if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h)) |
232 | 0 | return 0; |
233 | 181k | if (ffc->seed != NULL |
234 | 434 | && !ossl_param_build_set_octet_string(bld, params, |
235 | 434 | OSSL_PKEY_PARAM_FFC_SEED, |
236 | 434 | ffc->seed, ffc->seedlen)) |
237 | 0 | return 0; |
238 | 181k | if (ffc->nid != NID_undef) { |
239 | 2.86k | const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid); |
240 | 2.86k | const char *name = ossl_ffc_named_group_get_name(group); |
241 | | |
242 | 2.86k | if (name == NULL |
243 | 2.86k | || !ossl_param_build_set_utf8_string(bld, params, |
244 | 2.86k | OSSL_PKEY_PARAM_GROUP_NAME, |
245 | 2.86k | name)) |
246 | 0 | return 0; |
247 | 2.86k | } |
248 | 181k | test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0); |
249 | 181k | if (!ossl_param_build_set_int(bld, params, |
250 | 181k | OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags)) |
251 | 0 | return 0; |
252 | 181k | test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0); |
253 | 181k | if (!ossl_param_build_set_int(bld, params, |
254 | 181k | OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags)) |
255 | 0 | return 0; |
256 | 181k | test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0); |
257 | 181k | if (!ossl_param_build_set_int(bld, params, |
258 | 181k | OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, |
259 | 181k | test_flags)) |
260 | 0 | return 0; |
261 | | |
262 | 181k | if (ffc->mdname != NULL |
263 | 0 | && !ossl_param_build_set_utf8_string(bld, params, |
264 | 0 | OSSL_PKEY_PARAM_FFC_DIGEST, |
265 | 0 | ffc->mdname)) |
266 | 0 | return 0; |
267 | 181k | if (ffc->mdprops != NULL |
268 | 0 | && !ossl_param_build_set_utf8_string(bld, params, |
269 | 0 | OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, |
270 | 0 | ffc->mdprops)) |
271 | 0 | return 0; |
272 | 181k | return 1; |
273 | 181k | } |
274 | | |
275 | | #ifndef FIPS_MODULE |
276 | | int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent) |
277 | 115 | { |
278 | 115 | if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent)) |
279 | 0 | goto err; |
280 | 115 | if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent)) |
281 | 0 | goto err; |
282 | 115 | if (ffc->q != NULL |
283 | 115 | && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent)) |
284 | 0 | goto err; |
285 | 115 | if (ffc->j != NULL |
286 | 0 | && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent)) |
287 | 0 | goto err; |
288 | 115 | if (ffc->seed != NULL) { |
289 | 0 | size_t i; |
290 | |
|
291 | 0 | if (!BIO_indent(bp, indent, 128) |
292 | 0 | || BIO_puts(bp, "seed:") <= 0) |
293 | 0 | goto err; |
294 | 0 | for (i = 0; i < ffc->seedlen; i++) { |
295 | 0 | if ((i % 15) == 0) { |
296 | 0 | if (BIO_puts(bp, "\n") <= 0 |
297 | 0 | || !BIO_indent(bp, indent + 4, 128)) |
298 | 0 | goto err; |
299 | 0 | } |
300 | 0 | if (BIO_printf(bp, "%02x%s", ffc->seed[i], |
301 | 0 | ((i + 1) == ffc->seedlen) ? "" : ":") |
302 | 0 | <= 0) |
303 | 0 | goto err; |
304 | 0 | } |
305 | 0 | if (BIO_write(bp, "\n", 1) <= 0) |
306 | 0 | return 0; |
307 | 0 | } |
308 | 115 | if (ffc->pcounter != -1) { |
309 | 0 | if (!BIO_indent(bp, indent, 128) |
310 | 0 | || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0) |
311 | 0 | goto err; |
312 | 0 | } |
313 | 115 | return 1; |
314 | 0 | err: |
315 | 0 | return 0; |
316 | 115 | } |
317 | | #endif /* FIPS_MODULE */ |