VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/crypto/ffc/ffc_params.c

Last change on this file was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette