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