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