]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ffc/ffc_params.c
Add ACVP fips module tests
[thirdparty/openssl.git] / crypto / ffc / ffc_params.c
1 /*
2 * Copyright 2019-2020 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 #include "e_os.h" /* strcasecmp */
16
17 #ifndef FIPS_MODULE
18 # include <openssl/asn1.h> /* ffc_params_print */
19 #endif
20
21 void ffc_params_init(FFC_PARAMS *params)
22 {
23 memset(params, 0, sizeof(*params));
24 params->pcounter = -1;
25 params->gindex = FFC_UNVERIFIABLE_GINDEX;
26 params->flags = FFC_PARAM_FLAG_VALIDATE_ALL;
27 }
28
29 void ffc_params_cleanup(FFC_PARAMS *params)
30 {
31 BN_free(params->p);
32 BN_free(params->q);
33 BN_free(params->g);
34 BN_free(params->j);
35 OPENSSL_free(params->seed);
36 ffc_params_init(params);
37 }
38
39 void ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
40 {
41 if (p != NULL && p != d->p) {
42 BN_free(d->p);
43 d->p = p;
44 }
45 if (q != NULL && q != d->q) {
46 BN_free(d->q);
47 d->q = q;
48 }
49 if (g != NULL && g != d->g) {
50 BN_free(d->g);
51 d->g = g;
52 }
53 }
54
55 void ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
56 const BIGNUM **q, const BIGNUM **g)
57 {
58 if (p != NULL)
59 *p = d->p;
60 if (q != NULL)
61 *q = d->q;
62 if (g != NULL)
63 *g = d->g;
64 }
65
66
67 /* j is the 'cofactor' that is optionally output for ASN1. */
68 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
69 {
70 BN_free(d->j);
71 d->j = NULL;
72 if (j != NULL)
73 d->j = j;
74 }
75
76 int ffc_params_set_seed(FFC_PARAMS *params,
77 const unsigned char *seed, size_t seedlen)
78 {
79 if (params == NULL)
80 return 0;
81
82 if (params->seed != NULL) {
83 if (params->seed == seed)
84 return 1;
85 OPENSSL_free(params->seed);
86 }
87
88 if (seed != NULL && seedlen > 0) {
89 params->seed = OPENSSL_memdup(seed, seedlen);
90 if (params->seed == NULL)
91 return 0;
92 params->seedlen = seedlen;
93 } else {
94 params->seed = NULL;
95 params->seedlen = 0;
96 }
97 return 1;
98 }
99
100 void ffc_params_set_gindex(FFC_PARAMS *params, int index)
101 {
102 params->gindex = index;
103 }
104
105 void ffc_params_set_pcounter(FFC_PARAMS *params, int index)
106 {
107 params->pcounter = index;
108 }
109
110 void ffc_params_set_h(FFC_PARAMS *params, int index)
111 {
112 params->h = index;
113 }
114
115 void ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags)
116 {
117 params->flags = flags;
118 }
119
120 int ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props)
121 {
122 params->mdname = alg;
123 params->mdprops = props;
124 return 1;
125 }
126
127 int ffc_params_set_validate_params(FFC_PARAMS *params,
128 const unsigned char *seed, size_t seedlen,
129 int counter)
130 {
131 if (!ffc_params_set_seed(params, seed, seedlen))
132 return 0;
133 params->pcounter = counter;
134 return 1;
135 }
136
137 void ffc_params_get_validate_params(const FFC_PARAMS *params,
138 unsigned char **seed, size_t *seedlen,
139 int *pcounter)
140 {
141 if (seed != NULL)
142 *seed = params->seed;
143 if (seedlen != NULL)
144 *seedlen = params->seedlen;
145 if (pcounter != NULL)
146 *pcounter = params->pcounter;
147 }
148
149 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
150 {
151 BIGNUM *a;
152
153 /*
154 * If source is read only just copy the pointer, so
155 * we don't have to reallocate it.
156 */
157 if (src == NULL)
158 a = NULL;
159 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
160 && !BN_get_flags(src, BN_FLG_MALLOCED))
161 a = (BIGNUM *)src;
162 else if ((a = BN_dup(src)) == NULL)
163 return 0;
164 BN_clear_free(*dst);
165 *dst = a;
166 return 1;
167 }
168
169 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
170 {
171 if (!ffc_bn_cpy(&dst->p, src->p)
172 || !ffc_bn_cpy(&dst->g, src->g)
173 || !ffc_bn_cpy(&dst->q, src->q)
174 || !ffc_bn_cpy(&dst->j, src->j))
175 return 0;
176
177 OPENSSL_free(dst->seed);
178 dst->seedlen = src->seedlen;
179 if (src->seed != NULL) {
180 dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
181 if (dst->seed == NULL)
182 return 0;
183 } else {
184 dst->seed = NULL;
185 }
186 dst->nid = src->nid;
187 dst->pcounter = src->pcounter;
188 dst->h = src->h;
189 dst->gindex = src->gindex;
190 return 1;
191 }
192
193 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
194 {
195 return BN_cmp(a->p, b->p) == 0
196 && BN_cmp(a->g, b->g) == 0
197 && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
198 }
199
200 static const OSSL_ITEM flag_map[] = {
201 { FFC_PARAM_FLAG_VALIDATE_PQ, OSSL_FFC_PARAM_VALIDATE_PQ },
202 { FFC_PARAM_FLAG_VALIDATE_G, OSSL_FFC_PARAM_VALIDATE_G },
203 { FFC_PARAM_FLAG_VALIDATE_ALL, OSSL_FFC_PARAM_VALIDATE_PQG },
204 { 0, "" }
205 };
206
207 int ffc_params_flags_from_name(const char *name)
208 {
209 size_t i;
210
211 for (i = 0; i < OSSL_NELEM(flag_map); ++i) {
212 if (strcasecmp(flag_map[i].ptr, name) == 0)
213 return flag_map[i].id;
214 }
215 return NID_undef;
216 }
217
218 const char *ffc_params_flags_to_name(int flags)
219 {
220 size_t i;
221
222 flags &= FFC_PARAM_FLAG_VALIDATE_ALL;
223 for (i = 0; i < OSSL_NELEM(flag_map); ++i) {
224 if ((int)flag_map[i].id == flags)
225 return flag_map[i].ptr;
226 }
227 return "";
228 }
229
230 int ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
231 OSSL_PARAM params[])
232 {
233 if (ffc == NULL)
234 return 0;
235
236 if (ffc->p != NULL
237 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
238 return 0;
239 if (ffc->q != NULL
240 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
241 return 0;
242 if (ffc->g != NULL
243 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
244 return 0;
245 if (ffc->j != NULL
246 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
247 ffc->j))
248 return 0;
249 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
250 ffc->gindex))
251 return 0;
252 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
253 ffc->pcounter))
254 return 0;
255 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
256 return 0;
257 if (ffc->seed != NULL
258 && !ossl_param_build_set_octet_string(bld, params,
259 OSSL_PKEY_PARAM_FFC_SEED,
260 ffc->seed, ffc->seedlen))
261 return 0;
262 if (ffc->nid != NID_undef) {
263 #ifndef OPENSSL_NO_DH
264 const char *name = ffc_named_group_from_uid(ffc->nid);
265
266 if (name == NULL
267 || !ossl_param_build_set_utf8_string(bld, params,
268 OSSL_PKEY_PARAM_DH_GROUP,
269 name))
270 return 0;
271 #else
272 /* How could this be? We should not have a nid in a no-dh build. */
273 return 0;
274 #endif
275 }
276 if (!ossl_param_build_set_utf8_string(bld, params,
277 OSSL_PKEY_PARAM_FFC_VALIDATE_TYPE,
278 ffc_params_flags_to_name(ffc->flags)))
279 return 0;
280 if (ffc->mdname != NULL
281 && !ossl_param_build_set_utf8_string(bld, params,
282 OSSL_PKEY_PARAM_FFC_DIGEST,
283 ffc->mdname))
284 return 0;
285 if (ffc->mdprops != NULL
286 && !ossl_param_build_set_utf8_string(bld, params,
287 OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
288 ffc->mdprops))
289 return 0;
290 return 1;
291 }
292
293 #ifndef FIPS_MODULE
294 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
295 {
296 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
297 goto err;
298 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
299 goto err;
300 if (ffc->q != NULL
301 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
302 goto err;
303 if (ffc->j != NULL
304 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
305 goto err;
306 if (ffc->seed != NULL) {
307 size_t i;
308 BIO_indent(bp, indent, 128);
309 BIO_puts(bp, "seed:");
310 for (i = 0; i < ffc->seedlen; i++) {
311 if ((i % 15) == 0) {
312 if (BIO_puts(bp, "\n") <= 0
313 || !BIO_indent(bp, indent + 4, 128))
314 goto err;
315 }
316 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
317 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
318 goto err;
319 }
320 if (BIO_write(bp, "\n", 1) <= 0)
321 return 0;
322 }
323 if (ffc->pcounter != -1) {
324 BIO_indent(bp, indent, 128);
325 if (BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
326 goto err;
327 }
328 return 1;
329 err:
330 return 0;
331 }
332 #endif /* FIPS_MODULE */