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 "internal/ffc.h"
13 # include <openssl/asn1.h> /* ffc_params_print */
16 void ffc_params_init(FFC_PARAMS *params)
18 memset(params, 0, sizeof(*params));
19 params->pcounter = -1;
20 params->gindex = FFC_UNVERIFIABLE_GINDEX;
23 void ffc_params_cleanup(FFC_PARAMS *params)
29 OPENSSL_free(params->seed);
30 ffc_params_init(params);
33 void ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
35 if (p != NULL && p != d->p) {
39 if (q != NULL && q != d->q) {
43 if (g != NULL && g != d->g) {
49 void ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
50 const BIGNUM **q, const BIGNUM **g)
61 /* j is the 'cofactor' that is optionally output for ASN1. */
62 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
70 int ffc_params_set_validate_params(FFC_PARAMS *params,
71 const unsigned char *seed, size_t seedlen,
77 if (params->seed != NULL)
78 OPENSSL_free(params->seed);
80 if (seed != NULL && seedlen > 0) {
81 params->seed = OPENSSL_memdup(seed, seedlen);
82 if (params->seed == NULL)
84 params->seedlen = seedlen;
89 params->pcounter = counter;
93 void ffc_params_get_validate_params(const FFC_PARAMS *params,
94 unsigned char **seed, size_t *seedlen,
100 *seedlen = params->seedlen;
101 if (pcounter != NULL)
102 *pcounter = params->pcounter;
105 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
110 * If source is read only just copy the pointer, so
111 * we don't have to reallocate it.
115 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
116 && !BN_get_flags(src, BN_FLG_MALLOCED))
118 else if ((a = BN_dup(src)) == NULL)
125 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
127 if (!ffc_bn_cpy(&dst->p, src->p)
128 || !ffc_bn_cpy(&dst->g, src->g)
129 || !ffc_bn_cpy(&dst->q, src->q)
130 || !ffc_bn_cpy(&dst->j, src->j))
133 OPENSSL_free(dst->seed);
134 dst->seedlen = src->seedlen;
135 if (src->seed != NULL) {
136 dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
137 if (dst->seed == NULL)
142 dst->pcounter = src->pcounter;
146 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
148 return BN_cmp(a->p, b->p) == 0
149 && BN_cmp(a->g, b->g) == 0
150 && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
154 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
156 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
158 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
161 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
164 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
166 if (ffc->seed != NULL) {
168 BIO_indent(bp, indent, 128);
169 BIO_puts(bp, "seed:");
170 for (i = 0; i < ffc->seedlen; i++) {
172 if (BIO_puts(bp, "\n") <= 0
173 || !BIO_indent(bp, indent + 4, 128))
176 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
177 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
180 if (BIO_write(bp, "\n", 1) <= 0)
183 if (ffc->pcounter != -1) {
184 BIO_indent(bp, indent, 128);
185 if (BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
192 #endif /* FIPS_MODE */