2 * Copyright 2019-2021 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"
15 #include "e_os.h" /* strcasecmp */
18 # include <openssl/asn1.h> /* ossl_ffc_params_print */
21 void ossl_ffc_params_init(FFC_PARAMS *params)
23 memset(params, 0, sizeof(*params));
24 params->pcounter = -1;
25 params->gindex = FFC_UNVERIFIABLE_GINDEX;
26 params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
29 void ossl_ffc_params_cleanup(FFC_PARAMS *params)
35 OPENSSL_free(params->seed);
36 ossl_ffc_params_init(params);
39 void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
41 if (p != NULL && p != d->p) {
45 if (q != NULL && q != d->q) {
49 if (g != NULL && g != d->g) {
55 void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
56 const BIGNUM **q, const BIGNUM **g)
67 /* j is the 'cofactor' that is optionally output for ASN1. */
68 void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
76 int ossl_ffc_params_set_seed(FFC_PARAMS *params,
77 const unsigned char *seed, size_t seedlen)
82 if (params->seed != NULL) {
83 if (params->seed == seed)
85 OPENSSL_free(params->seed);
88 if (seed != NULL && seedlen > 0) {
89 params->seed = OPENSSL_memdup(seed, seedlen);
90 if (params->seed == NULL)
92 params->seedlen = seedlen;
100 void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index)
102 params->gindex = index;
105 void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index)
107 params->pcounter = index;
110 void ossl_ffc_params_set_h(FFC_PARAMS *params, int index)
115 void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags)
117 params->flags = flags;
120 void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
124 params->flags |= flags;
126 params->flags &= ~flags;
129 int ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props)
131 params->mdname = alg;
132 params->mdprops = props;
136 int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
137 const unsigned char *seed,
138 size_t seedlen, int counter)
140 if (!ossl_ffc_params_set_seed(params, seed, seedlen))
142 params->pcounter = counter;
146 void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
147 unsigned char **seed, size_t *seedlen,
151 *seed = params->seed;
153 *seedlen = params->seedlen;
154 if (pcounter != NULL)
155 *pcounter = params->pcounter;
158 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
163 * If source is read only just copy the pointer, so
164 * we don't have to reallocate it.
168 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
169 && !BN_get_flags(src, BN_FLG_MALLOCED))
171 else if ((a = BN_dup(src)) == NULL)
178 int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
180 if (!ffc_bn_cpy(&dst->p, src->p)
181 || !ffc_bn_cpy(&dst->g, src->g)
182 || !ffc_bn_cpy(&dst->q, src->q)
183 || !ffc_bn_cpy(&dst->j, src->j))
186 OPENSSL_free(dst->seed);
187 dst->seedlen = src->seedlen;
188 if (src->seed != NULL) {
189 dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
190 if (dst->seed == NULL)
196 dst->pcounter = src->pcounter;
198 dst->gindex = src->gindex;
199 dst->flags = src->flags;
203 int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
205 return BN_cmp(a->p, b->p) == 0
206 && BN_cmp(a->g, b->g) == 0
207 && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
210 int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
219 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
222 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
225 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
228 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
231 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
234 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
237 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
239 if (ffc->seed != NULL
240 && !ossl_param_build_set_octet_string(bld, params,
241 OSSL_PKEY_PARAM_FFC_SEED,
242 ffc->seed, ffc->seedlen))
244 if (ffc->nid != NID_undef) {
245 const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
246 const char *name = ossl_ffc_named_group_get_name(group);
249 || !ossl_param_build_set_utf8_string(bld, params,
250 OSSL_PKEY_PARAM_GROUP_NAME,
254 test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
255 if (!ossl_param_build_set_int(bld, params,
256 OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
258 test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
259 if (!ossl_param_build_set_int(bld, params,
260 OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
262 test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
263 if (!ossl_param_build_set_int(bld, params,
264 OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
268 if (ffc->mdname != NULL
269 && !ossl_param_build_set_utf8_string(bld, params,
270 OSSL_PKEY_PARAM_FFC_DIGEST,
273 if (ffc->mdprops != NULL
274 && !ossl_param_build_set_utf8_string(bld, params,
275 OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
282 int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
284 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
286 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
289 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
292 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
294 if (ffc->seed != NULL) {
297 if (!BIO_indent(bp, indent, 128)
298 || BIO_puts(bp, "seed:") <= 0)
300 for (i = 0; i < ffc->seedlen; i++) {
302 if (BIO_puts(bp, "\n") <= 0
303 || !BIO_indent(bp, indent + 4, 128))
306 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
307 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
310 if (BIO_write(bp, "\n", 1) <= 0)
313 if (ffc->pcounter != -1) {
314 if (!BIO_indent(bp, indent, 128)
315 || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
322 #endif /* FIPS_MODULE */