2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
17 # include <openssl/asn1.h> /* ossl_ffc_params_print */
20 void ossl_ffc_params_init(FFC_PARAMS *params)
22 memset(params, 0, sizeof(*params));
23 params->pcounter = -1;
24 params->gindex = FFC_UNVERIFIABLE_GINDEX;
25 params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
28 void ossl_ffc_params_cleanup(FFC_PARAMS *params)
34 OPENSSL_free(params->seed);
35 ossl_ffc_params_init(params);
38 void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
40 if (p != NULL && p != d->p) {
44 if (q != NULL && q != d->q) {
48 if (g != NULL && g != d->g) {
54 void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
55 const BIGNUM **q, const BIGNUM **g)
66 /* j is the 'cofactor' that is optionally output for ASN1. */
67 void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
75 int ossl_ffc_params_set_seed(FFC_PARAMS *params,
76 const unsigned char *seed, size_t seedlen)
81 if (params->seed != NULL) {
82 if (params->seed == seed)
84 OPENSSL_free(params->seed);
87 if (seed != NULL && seedlen > 0) {
88 params->seed = OPENSSL_memdup(seed, seedlen);
89 if (params->seed == NULL)
91 params->seedlen = seedlen;
99 void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index)
101 params->gindex = index;
104 void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index)
106 params->pcounter = index;
109 void ossl_ffc_params_set_h(FFC_PARAMS *params, int index)
114 void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags)
116 params->flags = flags;
119 void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
123 params->flags |= flags;
125 params->flags &= ~flags;
128 int ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props)
130 params->mdname = alg;
131 params->mdprops = props;
135 int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
136 const unsigned char *seed,
137 size_t seedlen, int counter)
139 if (!ossl_ffc_params_set_seed(params, seed, seedlen))
141 params->pcounter = counter;
145 void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
146 unsigned char **seed, size_t *seedlen,
150 *seed = params->seed;
152 *seedlen = params->seedlen;
153 if (pcounter != NULL)
154 *pcounter = params->pcounter;
157 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
162 * If source is read only just copy the pointer, so
163 * we don't have to reallocate it.
167 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
168 && !BN_get_flags(src, BN_FLG_MALLOCED))
170 else if ((a = BN_dup(src)) == NULL)
177 int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
179 if (!ffc_bn_cpy(&dst->p, src->p)
180 || !ffc_bn_cpy(&dst->g, src->g)
181 || !ffc_bn_cpy(&dst->q, src->q)
182 || !ffc_bn_cpy(&dst->j, src->j))
185 OPENSSL_free(dst->seed);
186 dst->seedlen = src->seedlen;
187 if (src->seed != NULL) {
188 dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
189 if (dst->seed == NULL)
195 dst->pcounter = src->pcounter;
197 dst->gindex = src->gindex;
198 dst->flags = src->flags;
202 int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
204 return BN_cmp(a->p, b->p) == 0
205 && BN_cmp(a->g, b->g) == 0
206 && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
209 int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
218 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
221 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
224 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
227 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
230 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
233 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
236 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
238 if (ffc->seed != NULL
239 && !ossl_param_build_set_octet_string(bld, params,
240 OSSL_PKEY_PARAM_FFC_SEED,
241 ffc->seed, ffc->seedlen))
243 if (ffc->nid != NID_undef) {
244 const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
245 const char *name = ossl_ffc_named_group_get_name(group);
248 || !ossl_param_build_set_utf8_string(bld, params,
249 OSSL_PKEY_PARAM_GROUP_NAME,
253 test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
254 if (!ossl_param_build_set_int(bld, params,
255 OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
257 test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
258 if (!ossl_param_build_set_int(bld, params,
259 OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
261 test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
262 if (!ossl_param_build_set_int(bld, params,
263 OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
267 if (ffc->mdname != NULL
268 && !ossl_param_build_set_utf8_string(bld, params,
269 OSSL_PKEY_PARAM_FFC_DIGEST,
272 if (ffc->mdprops != NULL
273 && !ossl_param_build_set_utf8_string(bld, params,
274 OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
281 int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
283 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
285 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
288 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
291 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
293 if (ffc->seed != NULL) {
296 if (!BIO_indent(bp, indent, 128)
297 || BIO_puts(bp, "seed:") <= 0)
299 for (i = 0; i < ffc->seedlen; i++) {
301 if (BIO_puts(bp, "\n") <= 0
302 || !BIO_indent(bp, indent + 4, 128))
305 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
306 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
309 if (BIO_write(bp, "\n", 1) <= 0)
312 if (ffc->pcounter != -1) {
313 if (!BIO_indent(bp, indent, 128)
314 || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
321 #endif /* FIPS_MODULE */