2 * Copyright 2019-2020 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"
16 # include <openssl/asn1.h> /* ffc_params_print */
19 void ffc_params_init(FFC_PARAMS *params)
21 memset(params, 0, sizeof(*params));
22 params->pcounter = -1;
23 params->gindex = FFC_UNVERIFIABLE_GINDEX;
26 void ffc_params_cleanup(FFC_PARAMS *params)
32 OPENSSL_free(params->seed);
33 ffc_params_init(params);
36 void ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
38 if (p != NULL && p != d->p) {
42 if (q != NULL && q != d->q) {
46 if (g != NULL && g != d->g) {
52 void ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
53 const BIGNUM **q, const BIGNUM **g)
64 /* j is the 'cofactor' that is optionally output for ASN1. */
65 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
73 int ffc_params_set_seed(FFC_PARAMS *params,
74 const unsigned char *seed, size_t seedlen)
79 if (params->seed != NULL) {
80 if (params->seed == seed)
82 OPENSSL_free(params->seed);
85 if (seed != NULL && seedlen > 0) {
86 params->seed = OPENSSL_memdup(seed, seedlen);
87 if (params->seed == NULL)
89 params->seedlen = seedlen;
97 void ffc_params_set_gindex(FFC_PARAMS *params, int index)
99 params->gindex = index;
102 void ffc_params_set_pcounter(FFC_PARAMS *params, int index)
104 params->pcounter = index;
107 void ffc_params_set_h(FFC_PARAMS *params, int index)
112 int ffc_params_set_validate_params(FFC_PARAMS *params,
113 const unsigned char *seed, size_t seedlen,
116 if (!ffc_params_set_seed(params, seed, seedlen))
118 params->pcounter = counter;
122 void ffc_params_get_validate_params(const FFC_PARAMS *params,
123 unsigned char **seed, size_t *seedlen,
127 *seed = params->seed;
129 *seedlen = params->seedlen;
130 if (pcounter != NULL)
131 *pcounter = params->pcounter;
134 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
139 * If source is read only just copy the pointer, so
140 * we don't have to reallocate it.
144 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
145 && !BN_get_flags(src, BN_FLG_MALLOCED))
147 else if ((a = BN_dup(src)) == NULL)
154 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
156 if (!ffc_bn_cpy(&dst->p, src->p)
157 || !ffc_bn_cpy(&dst->g, src->g)
158 || !ffc_bn_cpy(&dst->q, src->q)
159 || !ffc_bn_cpy(&dst->j, src->j))
162 OPENSSL_free(dst->seed);
163 dst->seedlen = src->seedlen;
164 if (src->seed != NULL) {
165 dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
166 if (dst->seed == NULL)
172 dst->pcounter = src->pcounter;
174 dst->gindex = src->gindex;
178 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
180 return BN_cmp(a->p, b->p) == 0
181 && BN_cmp(a->g, b->g) == 0
182 && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
185 int ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
192 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
195 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
198 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
201 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
204 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
207 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
210 if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
212 if (ffc->seed != NULL
213 && !ossl_param_build_set_octet_string(bld, params,
214 OSSL_PKEY_PARAM_FFC_SEED,
215 ffc->seed, ffc->seedlen))
217 if (ffc->nid != NID_undef) {
218 #ifndef OPENSSL_NO_DH
219 const char *name = ffc_named_group_from_uid(ffc->nid);
222 || !ossl_param_build_set_utf8_string(bld, params,
223 OSSL_PKEY_PARAM_DH_GROUP,
227 /* How could this be? We should not have a nid in a no-dh build. */
235 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
237 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
239 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
242 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
245 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
247 if (ffc->seed != NULL) {
249 BIO_indent(bp, indent, 128);
250 BIO_puts(bp, "seed:");
251 for (i = 0; i < ffc->seedlen; i++) {
253 if (BIO_puts(bp, "\n") <= 0
254 || !BIO_indent(bp, indent + 4, 128))
257 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
258 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
261 if (BIO_write(bp, "\n", 1) <= 0)
264 if (ffc->pcounter != -1) {
265 BIO_indent(bp, indent, 128);
266 if (BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
273 #endif /* FIPS_MODULE */