]>
Commit | Line | Data |
---|---|---|
dc8de3e6 SL |
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 */ | |
b03ec3b5 | 11 | #include <openssl/core_names.h> |
dc8de3e6 | 12 | #include "internal/ffc.h" |
b03ec3b5 SL |
13 | #include "internal/param_build_set.h" |
14 | ||
dc8de3e6 SL |
15 | #ifndef FIPS_MODE |
16 | # include <openssl/asn1.h> /* ffc_params_print */ | |
17 | #endif | |
18 | ||
19 | void ffc_params_init(FFC_PARAMS *params) | |
20 | { | |
86cde318 | 21 | memset(params, 0, sizeof(*params)); |
dc8de3e6 | 22 | params->pcounter = -1; |
f11f86f6 | 23 | params->gindex = FFC_UNVERIFIABLE_GINDEX; |
dc8de3e6 SL |
24 | } |
25 | ||
26 | void ffc_params_cleanup(FFC_PARAMS *params) | |
27 | { | |
28 | BN_free(params->p); | |
29 | BN_free(params->q); | |
30 | BN_free(params->g); | |
31 | BN_free(params->j); | |
32 | OPENSSL_free(params->seed); | |
33 | ffc_params_init(params); | |
34 | } | |
35 | ||
36 | void ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) | |
37 | { | |
38 | if (p != NULL && p != d->p) { | |
39 | BN_free(d->p); | |
40 | d->p = p; | |
41 | } | |
42 | if (q != NULL && q != d->q) { | |
43 | BN_free(d->q); | |
44 | d->q = q; | |
45 | } | |
46 | if (g != NULL && g != d->g) { | |
47 | BN_free(d->g); | |
48 | d->g = g; | |
49 | } | |
50 | } | |
51 | ||
52 | void ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p, | |
53 | const BIGNUM **q, const BIGNUM **g) | |
54 | { | |
55 | if (p != NULL) | |
56 | *p = d->p; | |
57 | if (q != NULL) | |
58 | *q = d->q; | |
59 | if (g != NULL) | |
60 | *g = d->g; | |
61 | } | |
62 | ||
63 | ||
64 | /* j is the 'cofactor' that is optionally output for ASN1. */ | |
65 | void ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j) | |
66 | { | |
67 | BN_free(d->j); | |
68 | d->j = NULL; | |
69 | if (j != NULL) | |
70 | d->j = j; | |
71 | } | |
72 | ||
b03ec3b5 SL |
73 | int ffc_params_set_seed(FFC_PARAMS *params, |
74 | const unsigned char *seed, size_t seedlen) | |
dc8de3e6 SL |
75 | { |
76 | if (params == NULL) | |
77 | return 0; | |
78 | ||
b03ec3b5 SL |
79 | if (params->seed != NULL) { |
80 | if (params->seed == seed) | |
81 | return 1; | |
dc8de3e6 | 82 | OPENSSL_free(params->seed); |
b03ec3b5 | 83 | } |
dc8de3e6 SL |
84 | |
85 | if (seed != NULL && seedlen > 0) { | |
86 | params->seed = OPENSSL_memdup(seed, seedlen); | |
87 | if (params->seed == NULL) | |
88 | return 0; | |
89 | params->seedlen = seedlen; | |
90 | } else { | |
91 | params->seed = NULL; | |
92 | params->seedlen = 0; | |
93 | } | |
b03ec3b5 SL |
94 | return 1; |
95 | } | |
96 | ||
97 | void ffc_params_set_gindex(FFC_PARAMS *params, int index) | |
98 | { | |
99 | params->gindex = index; | |
100 | } | |
101 | ||
102 | void ffc_params_set_pcounter(FFC_PARAMS *params, int index) | |
103 | { | |
104 | params->pcounter = index; | |
105 | } | |
106 | ||
107 | void ffc_params_set_h(FFC_PARAMS *params, int index) | |
108 | { | |
109 | params->h = index; | |
110 | } | |
111 | ||
112 | int ffc_params_set_validate_params(FFC_PARAMS *params, | |
113 | const unsigned char *seed, size_t seedlen, | |
114 | int counter) | |
115 | { | |
116 | if (!ffc_params_set_seed(params, seed, seedlen)) | |
117 | return 0; | |
dc8de3e6 SL |
118 | params->pcounter = counter; |
119 | return 1; | |
120 | } | |
121 | ||
122 | void ffc_params_get_validate_params(const FFC_PARAMS *params, | |
123 | unsigned char **seed, size_t *seedlen, | |
124 | int *pcounter) | |
125 | { | |
126 | if (seed != NULL) | |
127 | *seed = params->seed; | |
128 | if (seedlen != NULL) | |
129 | *seedlen = params->seedlen; | |
130 | if (pcounter != NULL) | |
131 | *pcounter = params->pcounter; | |
132 | } | |
133 | ||
134 | static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src) | |
135 | { | |
136 | BIGNUM *a; | |
137 | ||
138 | /* | |
139 | * If source is read only just copy the pointer, so | |
140 | * we don't have to reallocate it. | |
141 | */ | |
142 | if (src == NULL) | |
143 | a = NULL; | |
144 | else if (BN_get_flags(src, BN_FLG_STATIC_DATA) | |
145 | && !BN_get_flags(src, BN_FLG_MALLOCED)) | |
146 | a = (BIGNUM *)src; | |
147 | else if ((a = BN_dup(src)) == NULL) | |
148 | return 0; | |
149 | BN_clear_free(*dst); | |
150 | *dst = a; | |
151 | return 1; | |
152 | } | |
153 | ||
154 | int ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src) | |
155 | { | |
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)) | |
160 | return 0; | |
161 | ||
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) | |
167 | return 0; | |
168 | } else { | |
169 | dst->seed = NULL; | |
170 | } | |
b03ec3b5 | 171 | dst->nid = src->nid; |
dc8de3e6 | 172 | dst->pcounter = src->pcounter; |
b03ec3b5 SL |
173 | dst->h = src->h; |
174 | dst->gindex = src->gindex; | |
dc8de3e6 SL |
175 | return 1; |
176 | } | |
177 | ||
178 | int ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q) | |
179 | { | |
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 */ | |
183 | } | |
184 | ||
b03ec3b5 SL |
185 | int ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld, |
186 | OSSL_PARAM params[]) | |
187 | { | |
188 | if (ffc == NULL) | |
189 | return 0; | |
190 | ||
191 | if (ffc->p != NULL | |
192 | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p)) | |
193 | return 0; | |
194 | if (ffc->q != NULL | |
195 | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q)) | |
196 | return 0; | |
197 | if (ffc->g != NULL | |
198 | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g)) | |
199 | return 0; | |
200 | if (ffc->j != NULL | |
201 | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR, | |
202 | ffc->j)) | |
203 | return 0; | |
204 | if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX, | |
205 | ffc->gindex)) | |
206 | return 0; | |
207 | if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER, | |
208 | ffc->pcounter)) | |
209 | return 0; | |
210 | if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h)) | |
211 | return 0; | |
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)) | |
216 | return 0; | |
217 | if (ffc->nid != NID_undef) { | |
7165593c | 218 | const char *name = ffc_named_group_from_uid(ffc->nid); |
b03ec3b5 SL |
219 | |
220 | if (name == NULL | |
221 | || !ossl_param_build_set_utf8_string(bld, params, | |
222 | OSSL_PKEY_PARAM_FFC_GROUP, | |
223 | name)) | |
224 | return 0; | |
225 | } | |
226 | return 1; | |
227 | } | |
228 | ||
dc8de3e6 SL |
229 | #ifndef FIPS_MODE |
230 | int ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent) | |
231 | { | |
232 | if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent)) | |
233 | goto err; | |
234 | if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent)) | |
235 | goto err; | |
236 | if (ffc->q != NULL | |
237 | && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent)) | |
238 | goto err; | |
239 | if (ffc->j != NULL | |
240 | && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent)) | |
241 | goto err; | |
242 | if (ffc->seed != NULL) { | |
243 | size_t i; | |
244 | BIO_indent(bp, indent, 128); | |
245 | BIO_puts(bp, "seed:"); | |
246 | for (i = 0; i < ffc->seedlen; i++) { | |
247 | if ((i % 15) == 0) { | |
248 | if (BIO_puts(bp, "\n") <= 0 | |
249 | || !BIO_indent(bp, indent + 4, 128)) | |
250 | goto err; | |
251 | } | |
252 | if (BIO_printf(bp, "%02x%s", ffc->seed[i], | |
253 | ((i + 1) == ffc->seedlen) ? "" : ":") <= 0) | |
254 | goto err; | |
255 | } | |
256 | if (BIO_write(bp, "\n", 1) <= 0) | |
257 | return 0; | |
258 | } | |
259 | if (ffc->pcounter != -1) { | |
260 | BIO_indent(bp, indent, 128); | |
261 | if (BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0) | |
262 | goto err; | |
263 | } | |
264 | return 1; | |
265 | err: | |
266 | return 0; | |
267 | } | |
268 | #endif /* FIPS_MODE */ |