]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ffc/ffc_params.c
cb8987b64d2fb77f1859df52b11e9a72ebe4dcb5
[thirdparty/openssl.git] / crypto / ffc / ffc_params.c
1 /*
2 * Copyright 2019-2020 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 "internal/ffc.h"
12 #ifndef FIPS_MODE
13 # include <openssl/asn1.h> /* ffc_params_print */
14 #endif
15
16 void ffc_params_init(FFC_PARAMS *params)
17 {
18 memset(params, 0, sizeof(*params));
19 params->pcounter = -1;
20 params->gindex = FFC_UNVERIFIABLE_GINDEX;
21 }
22
23 void ffc_params_cleanup(FFC_PARAMS *params)
24 {
25 BN_free(params->p);
26 BN_free(params->q);
27 BN_free(params->g);
28 BN_free(params->j);
29 OPENSSL_free(params->seed);
30 ffc_params_init(params);
31 }
32
33 void ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
34 {
35 if (p != NULL && p != d->p) {
36 BN_free(d->p);
37 d->p = p;
38 }
39 if (q != NULL && q != d->q) {
40 BN_free(d->q);
41 d->q = q;
42 }
43 if (g != NULL && g != d->g) {
44 BN_free(d->g);
45 d->g = g;
46 }
47 }
48
49 void ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
50 const BIGNUM **q, const BIGNUM **g)
51 {
52 if (p != NULL)
53 *p = d->p;
54 if (q != NULL)
55 *q = d->q;
56 if (g != NULL)
57 *g = d->g;
58 }
59
60
61 /* j is the 'cofactor' that is optionally output for ASN1. */
62 void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
63 {
64 BN_free(d->j);
65 d->j = NULL;
66 if (j != NULL)
67 d->j = j;
68 }
69
70 int ffc_params_set_validate_params(FFC_PARAMS *params,
71 const unsigned char *seed, size_t seedlen,
72 int counter)
73 {
74 if (params == NULL)
75 return 0;
76
77 if (params->seed != NULL)
78 OPENSSL_free(params->seed);
79
80 if (seed != NULL && seedlen > 0) {
81 params->seed = OPENSSL_memdup(seed, seedlen);
82 if (params->seed == NULL)
83 return 0;
84 params->seedlen = seedlen;
85 } else {
86 params->seed = NULL;
87 params->seedlen = 0;
88 }
89 params->pcounter = counter;
90 return 1;
91 }
92
93 void ffc_params_get_validate_params(const FFC_PARAMS *params,
94 unsigned char **seed, size_t *seedlen,
95 int *pcounter)
96 {
97 if (seed != NULL)
98 *seed = params->seed;
99 if (seedlen != NULL)
100 *seedlen = params->seedlen;
101 if (pcounter != NULL)
102 *pcounter = params->pcounter;
103 }
104
105 static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
106 {
107 BIGNUM *a;
108
109 /*
110 * If source is read only just copy the pointer, so
111 * we don't have to reallocate it.
112 */
113 if (src == NULL)
114 a = NULL;
115 else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
116 && !BN_get_flags(src, BN_FLG_MALLOCED))
117 a = (BIGNUM *)src;
118 else if ((a = BN_dup(src)) == NULL)
119 return 0;
120 BN_clear_free(*dst);
121 *dst = a;
122 return 1;
123 }
124
125 int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
126 {
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))
131 return 0;
132
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)
138 return 0;
139 } else {
140 dst->seed = NULL;
141 }
142 dst->pcounter = src->pcounter;
143 return 1;
144 }
145
146 int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
147 {
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 */
151 }
152
153 #ifndef FIPS_MODE
154 int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
155 {
156 if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
157 goto err;
158 if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
159 goto err;
160 if (ffc->q != NULL
161 && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
162 goto err;
163 if (ffc->j != NULL
164 && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
165 goto err;
166 if (ffc->seed != NULL) {
167 size_t i;
168 BIO_indent(bp, indent, 128);
169 BIO_puts(bp, "seed:");
170 for (i = 0; i < ffc->seedlen; i++) {
171 if ((i % 15) == 0) {
172 if (BIO_puts(bp, "\n") <= 0
173 || !BIO_indent(bp, indent + 4, 128))
174 goto err;
175 }
176 if (BIO_printf(bp, "%02x%s", ffc->seed[i],
177 ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
178 goto err;
179 }
180 if (BIO_write(bp, "\n", 1) <= 0)
181 return 0;
182 }
183 if (ffc->pcounter != -1) {
184 BIO_indent(bp, indent, 128);
185 if (BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
186 goto err;
187 }
188 return 1;
189 err:
190 return 0;
191 }
192 #endif /* FIPS_MODE */