1 |
|
|
/* $OpenBSD: ecparam.c,v 1.16 2017/01/20 08:57:12 deraadt Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Written by Nils Larsch for the OpenSSL project. |
4 |
|
|
*/ |
5 |
|
|
/* ==================================================================== |
6 |
|
|
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
7 |
|
|
* |
8 |
|
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
|
* modification, are permitted provided that the following conditions |
10 |
|
|
* are met: |
11 |
|
|
* |
12 |
|
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
|
* |
15 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
|
|
* notice, this list of conditions and the following disclaimer in |
17 |
|
|
* the documentation and/or other materials provided with the |
18 |
|
|
* distribution. |
19 |
|
|
* |
20 |
|
|
* 3. All advertising materials mentioning features or use of this |
21 |
|
|
* software must display the following acknowledgment: |
22 |
|
|
* "This product includes software developed by the OpenSSL Project |
23 |
|
|
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
24 |
|
|
* |
25 |
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
26 |
|
|
* endorse or promote products derived from this software without |
27 |
|
|
* prior written permission. For written permission, please contact |
28 |
|
|
* openssl-core@openssl.org. |
29 |
|
|
* |
30 |
|
|
* 5. Products derived from this software may not be called "OpenSSL" |
31 |
|
|
* nor may "OpenSSL" appear in their names without prior written |
32 |
|
|
* permission of the OpenSSL Project. |
33 |
|
|
* |
34 |
|
|
* 6. Redistributions of any form whatsoever must retain the following |
35 |
|
|
* acknowledgment: |
36 |
|
|
* "This product includes software developed by the OpenSSL Project |
37 |
|
|
* for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
38 |
|
|
* |
39 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
40 |
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
42 |
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
43 |
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
44 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
45 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
46 |
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
48 |
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 |
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
50 |
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
51 |
|
|
* ==================================================================== |
52 |
|
|
* |
53 |
|
|
* This product includes cryptographic software written by Eric Young |
54 |
|
|
* (eay@cryptsoft.com). This product includes software written by Tim |
55 |
|
|
* Hudson (tjh@cryptsoft.com). |
56 |
|
|
* |
57 |
|
|
*/ |
58 |
|
|
/* ==================================================================== |
59 |
|
|
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
60 |
|
|
* |
61 |
|
|
* Portions of the attached software ("Contribution") are developed by |
62 |
|
|
* SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
63 |
|
|
* |
64 |
|
|
* The Contribution is licensed pursuant to the OpenSSL open source |
65 |
|
|
* license provided above. |
66 |
|
|
* |
67 |
|
|
* The elliptic curve binary polynomial software is originally written by |
68 |
|
|
* Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. |
69 |
|
|
* |
70 |
|
|
*/ |
71 |
|
|
|
72 |
|
|
#include <openssl/opensslconf.h> |
73 |
|
|
|
74 |
|
|
#ifndef OPENSSL_NO_EC |
75 |
|
|
|
76 |
|
|
#include <stdio.h> |
77 |
|
|
#include <stdlib.h> |
78 |
|
|
#include <string.h> |
79 |
|
|
#include <time.h> |
80 |
|
|
|
81 |
|
|
#include "apps.h" |
82 |
|
|
|
83 |
|
|
#include <openssl/bio.h> |
84 |
|
|
#include <openssl/bn.h> |
85 |
|
|
#include <openssl/ec.h> |
86 |
|
|
#include <openssl/err.h> |
87 |
|
|
#include <openssl/pem.h> |
88 |
|
|
#include <openssl/x509.h> |
89 |
|
|
|
90 |
|
|
static int ecparam_print_var(BIO *, BIGNUM *, const char *, int, |
91 |
|
|
unsigned char *); |
92 |
|
|
|
93 |
|
|
static struct { |
94 |
|
|
int C; |
95 |
|
|
int asn1_flag; |
96 |
|
|
int check; |
97 |
|
|
char *curve_name; |
98 |
|
|
point_conversion_form_t form; |
99 |
|
|
int genkey; |
100 |
|
|
char *infile; |
101 |
|
|
int informat; |
102 |
|
|
int list_curves; |
103 |
|
|
int new_asn1_flag; |
104 |
|
|
int new_form; |
105 |
|
|
int no_seed; |
106 |
|
|
int noout; |
107 |
|
|
char *outfile; |
108 |
|
|
int outformat; |
109 |
|
|
int text; |
110 |
|
|
} ecparam_config; |
111 |
|
|
|
112 |
|
|
static int |
113 |
|
|
ecparam_opt_form(char *arg) |
114 |
|
|
{ |
115 |
✗✓ |
720 |
if (strcmp(arg, "compressed") == 0) |
116 |
|
|
ecparam_config.form = POINT_CONVERSION_COMPRESSED; |
117 |
|
|
else if (strcmp(arg, "uncompressed") == 0) |
118 |
|
|
ecparam_config.form = POINT_CONVERSION_UNCOMPRESSED; |
119 |
|
|
else if (strcmp(arg, "hybrid") == 0) |
120 |
|
|
ecparam_config.form = POINT_CONVERSION_HYBRID; |
121 |
|
|
else |
122 |
|
|
return (1); |
123 |
|
|
|
124 |
|
360 |
ecparam_config.new_form = 1; |
125 |
|
360 |
return (0); |
126 |
|
360 |
} |
127 |
|
|
|
128 |
|
|
static int |
129 |
|
|
ecparam_opt_enctype(char *arg) |
130 |
|
|
{ |
131 |
✗✓ |
720 |
if (strcmp(arg, "explicit") == 0) |
132 |
|
|
ecparam_config.asn1_flag = 0; |
133 |
|
|
else if (strcmp(arg, "named_curve") == 0) |
134 |
|
|
ecparam_config.asn1_flag = OPENSSL_EC_NAMED_CURVE; |
135 |
|
|
else |
136 |
|
|
return (1); |
137 |
|
|
|
138 |
|
360 |
ecparam_config.new_asn1_flag = 1; |
139 |
|
360 |
return (0); |
140 |
|
360 |
} |
141 |
|
|
|
142 |
|
|
struct option ecparam_options[] = { |
143 |
|
|
{ |
144 |
|
|
.name = "C", |
145 |
|
|
.desc = "Convert the EC parameters into C code", |
146 |
|
|
.type = OPTION_FLAG, |
147 |
|
|
.opt.flag = &ecparam_config.C, |
148 |
|
|
}, |
149 |
|
|
{ |
150 |
|
|
.name = "check", |
151 |
|
|
.desc = "Validate the elliptic curve parameters", |
152 |
|
|
.type = OPTION_FLAG, |
153 |
|
|
.opt.flag = &ecparam_config.check, |
154 |
|
|
}, |
155 |
|
|
{ |
156 |
|
|
.name = "conv_form", |
157 |
|
|
.argname = "form", |
158 |
|
|
.desc = "Specify point conversion form:\n" |
159 |
|
|
" compressed, uncompressed (default), hybrid", |
160 |
|
|
.type = OPTION_ARG_FUNC, |
161 |
|
|
.opt.argfunc = ecparam_opt_form, |
162 |
|
|
}, |
163 |
|
|
{ |
164 |
|
|
.name = "genkey", |
165 |
|
|
.desc = "Generate an EC private key using the specified " |
166 |
|
|
"parameters", |
167 |
|
|
.type = OPTION_FLAG, |
168 |
|
|
.opt.flag = &ecparam_config.genkey, |
169 |
|
|
}, |
170 |
|
|
{ |
171 |
|
|
.name = "in", |
172 |
|
|
.argname = "file", |
173 |
|
|
.desc = "Input file to read parameters from (default stdin)", |
174 |
|
|
.type = OPTION_ARG, |
175 |
|
|
.opt.arg = &ecparam_config.infile, |
176 |
|
|
}, |
177 |
|
|
{ |
178 |
|
|
.name = "inform", |
179 |
|
|
.argname = "format", |
180 |
|
|
.desc = "Input format (DER or PEM)", |
181 |
|
|
.type = OPTION_ARG_FORMAT, |
182 |
|
|
.opt.value = &ecparam_config.informat, |
183 |
|
|
}, |
184 |
|
|
{ |
185 |
|
|
.name = "list_curves", |
186 |
|
|
.desc = "Print list of all currently implemented EC " |
187 |
|
|
"parameter names", |
188 |
|
|
.type = OPTION_FLAG, |
189 |
|
|
.opt.flag = &ecparam_config.list_curves, |
190 |
|
|
}, |
191 |
|
|
{ |
192 |
|
|
.name = "name", |
193 |
|
|
.argname = "curve", |
194 |
|
|
.desc = "Use the EC parameters with the specified name", |
195 |
|
|
.type = OPTION_ARG, |
196 |
|
|
.opt.arg = &ecparam_config.curve_name, |
197 |
|
|
}, |
198 |
|
|
{ |
199 |
|
|
.name = "no_seed", |
200 |
|
|
.desc = "Do not output seed with explicit parameter encoding", |
201 |
|
|
.type = OPTION_FLAG, |
202 |
|
|
.opt.flag = &ecparam_config.no_seed, |
203 |
|
|
}, |
204 |
|
|
{ |
205 |
|
|
.name = "noout", |
206 |
|
|
.desc = "Do not output encoded version of EC parameters", |
207 |
|
|
.type = OPTION_FLAG, |
208 |
|
|
.opt.flag = &ecparam_config.noout, |
209 |
|
|
}, |
210 |
|
|
{ |
211 |
|
|
.name = "out", |
212 |
|
|
.argname = "file", |
213 |
|
|
.desc = "Output file to write parameters to (default stdout)", |
214 |
|
|
.type = OPTION_ARG, |
215 |
|
|
.opt.arg = &ecparam_config.outfile, |
216 |
|
|
}, |
217 |
|
|
{ |
218 |
|
|
.name = "outform", |
219 |
|
|
.argname = "format", |
220 |
|
|
.desc = "Output format (DER or PEM)", |
221 |
|
|
.type = OPTION_ARG_FORMAT, |
222 |
|
|
.opt.value = &ecparam_config.outformat, |
223 |
|
|
}, |
224 |
|
|
{ |
225 |
|
|
.name = "param_enc", |
226 |
|
|
.argname = "type", |
227 |
|
|
.desc = "Specify EC parameter ASN.1 encoding type:\n" |
228 |
|
|
" explicit, named_curve (default)", |
229 |
|
|
.type = OPTION_ARG_FUNC, |
230 |
|
|
.opt.argfunc = ecparam_opt_enctype, |
231 |
|
|
}, |
232 |
|
|
{ |
233 |
|
|
.name = "text", |
234 |
|
|
.desc = "Print out the EC parameters in human readable form", |
235 |
|
|
.type = OPTION_FLAG, |
236 |
|
|
.opt.flag = &ecparam_config.text, |
237 |
|
|
}, |
238 |
|
|
{NULL}, |
239 |
|
|
}; |
240 |
|
|
|
241 |
|
|
static void |
242 |
|
|
ecparam_usage(void) |
243 |
|
|
{ |
244 |
|
8 |
fprintf(stderr, "usage: ecparam [-C] [-check] [-conv_form arg] " |
245 |
|
|
" [-genkey]\n" |
246 |
|
|
" [-in file] [-inform DER | PEM] [-list_curves] [-name arg]\n" |
247 |
|
|
" [-no_seed] [-noout] [-out file] [-outform DER | PEM]\n" |
248 |
|
|
" [-param_enc arg] [-text]\n\n"); |
249 |
|
4 |
options_usage(ecparam_options); |
250 |
|
4 |
} |
251 |
|
|
|
252 |
|
|
int |
253 |
|
|
ecparam_main(int argc, char **argv) |
254 |
|
|
{ |
255 |
|
|
BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL, *ec_gen = NULL; |
256 |
|
|
BIGNUM *ec_order = NULL, *ec_cofactor = NULL; |
257 |
|
|
EC_GROUP *group = NULL; |
258 |
|
|
unsigned char *buffer = NULL; |
259 |
|
|
BIO *in = NULL, *out = NULL; |
260 |
|
|
int i, ret = 1; |
261 |
|
|
|
262 |
✓✗ |
744 |
if (single_execution) { |
263 |
✗✓ |
372 |
if (pledge("stdio cpath wpath rpath flock", NULL) == -1) { |
264 |
|
|
perror("pledge"); |
265 |
|
|
exit(1); |
266 |
|
|
} |
267 |
|
|
} |
268 |
|
|
|
269 |
|
372 |
memset(&ecparam_config, 0, sizeof(ecparam_config)); |
270 |
|
372 |
ecparam_config.asn1_flag = OPENSSL_EC_NAMED_CURVE; |
271 |
|
372 |
ecparam_config.form = POINT_CONVERSION_UNCOMPRESSED; |
272 |
|
372 |
ecparam_config.informat = FORMAT_PEM; |
273 |
|
372 |
ecparam_config.outformat = FORMAT_PEM; |
274 |
|
|
|
275 |
✓✓ |
372 |
if (options_parse(argc, argv, ecparam_options, NULL, NULL) != 0) { |
276 |
|
4 |
ecparam_usage(); |
277 |
|
4 |
goto end; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
368 |
in = BIO_new(BIO_s_file()); |
281 |
|
368 |
out = BIO_new(BIO_s_file()); |
282 |
✗✓ |
368 |
if ((in == NULL) || (out == NULL)) { |
283 |
|
|
ERR_print_errors(bio_err); |
284 |
|
|
goto end; |
285 |
|
|
} |
286 |
✓✗ |
368 |
if (ecparam_config.infile == NULL) |
287 |
|
368 |
BIO_set_fp(in, stdin, BIO_NOCLOSE); |
288 |
|
|
else { |
289 |
|
|
if (BIO_read_filename(in, ecparam_config.infile) <= 0) { |
290 |
|
|
perror(ecparam_config.infile); |
291 |
|
|
goto end; |
292 |
|
|
} |
293 |
|
|
} |
294 |
✓✓ |
368 |
if (ecparam_config.outfile == NULL) { |
295 |
|
8 |
BIO_set_fp(out, stdout, BIO_NOCLOSE); |
296 |
|
8 |
} else { |
297 |
✗✓ |
360 |
if (BIO_write_filename(out, ecparam_config.outfile) <= 0) { |
298 |
|
|
perror(ecparam_config.outfile); |
299 |
|
|
goto end; |
300 |
|
|
} |
301 |
|
|
} |
302 |
|
|
|
303 |
✓✓ |
368 |
if (ecparam_config.list_curves) { |
304 |
|
|
EC_builtin_curve *curves = NULL; |
305 |
|
|
size_t crv_len = 0; |
306 |
|
|
size_t n = 0; |
307 |
|
|
|
308 |
|
8 |
crv_len = EC_get_builtin_curves(NULL, 0); |
309 |
|
|
|
310 |
|
8 |
curves = reallocarray(NULL, crv_len, sizeof(EC_builtin_curve)); |
311 |
✗✓ |
8 |
if (curves == NULL) |
312 |
|
|
goto end; |
313 |
|
|
|
314 |
✗✓ |
8 |
if (!EC_get_builtin_curves(curves, crv_len)) { |
315 |
|
|
free(curves); |
316 |
|
|
goto end; |
317 |
|
|
} |
318 |
✓✓ |
1456 |
for (n = 0; n < crv_len; n++) { |
319 |
|
|
const char *comment; |
320 |
|
|
const char *sname; |
321 |
|
720 |
comment = curves[n].comment; |
322 |
|
720 |
sname = OBJ_nid2sn(curves[n].nid); |
323 |
✗✓ |
720 |
if (comment == NULL) |
324 |
|
|
comment = "CURVE DESCRIPTION NOT AVAILABLE"; |
325 |
✗✓ |
720 |
if (sname == NULL) |
326 |
|
|
sname = ""; |
327 |
|
|
|
328 |
|
720 |
BIO_printf(out, " %-10s: ", sname); |
329 |
|
720 |
BIO_printf(out, "%s\n", comment); |
330 |
|
|
} |
331 |
|
|
|
332 |
|
8 |
free(curves); |
333 |
|
|
ret = 0; |
334 |
✓✗ |
8 |
goto end; |
335 |
|
|
} |
336 |
✓✗ |
360 |
if (ecparam_config.curve_name != NULL) { |
337 |
|
|
int nid; |
338 |
|
|
|
339 |
|
|
/* |
340 |
|
|
* workaround for the SECG curve names secp192r1 and |
341 |
|
|
* secp256r1 (which are the same as the curves prime192v1 and |
342 |
|
|
* prime256v1 defined in X9.62) |
343 |
|
|
*/ |
344 |
✗✓ |
360 |
if (!strcmp(ecparam_config.curve_name, "secp192r1")) { |
345 |
|
|
BIO_printf(bio_err, "using curve name prime192v1 " |
346 |
|
|
"instead of secp192r1\n"); |
347 |
|
|
nid = NID_X9_62_prime192v1; |
348 |
✗✓ |
360 |
} else if (!strcmp(ecparam_config.curve_name, "secp256r1")) { |
349 |
|
|
BIO_printf(bio_err, "using curve name prime256v1 " |
350 |
|
|
"instead of secp256r1\n"); |
351 |
|
|
nid = NID_X9_62_prime256v1; |
352 |
|
|
} else |
353 |
|
360 |
nid = OBJ_sn2nid(ecparam_config.curve_name); |
354 |
|
|
|
355 |
✗✓ |
360 |
if (nid == 0) |
356 |
|
|
nid = EC_curve_nist2nid(ecparam_config.curve_name); |
357 |
|
|
|
358 |
✗✓ |
360 |
if (nid == 0) { |
359 |
|
|
BIO_printf(bio_err, "unknown curve name (%s)\n", |
360 |
|
|
ecparam_config.curve_name); |
361 |
|
|
goto end; |
362 |
|
|
} |
363 |
|
360 |
group = EC_GROUP_new_by_curve_name(nid); |
364 |
✗✓ |
360 |
if (group == NULL) { |
365 |
|
|
BIO_printf(bio_err, "unable to create curve (%s)\n", |
366 |
|
|
ecparam_config.curve_name); |
367 |
|
|
goto end; |
368 |
|
|
} |
369 |
|
360 |
EC_GROUP_set_asn1_flag(group, ecparam_config.asn1_flag); |
370 |
|
360 |
EC_GROUP_set_point_conversion_form(group, ecparam_config.form); |
371 |
✓✗✗✗ ✗ |
360 |
} else if (ecparam_config.informat == FORMAT_ASN1) { |
372 |
|
|
group = d2i_ECPKParameters_bio(in, NULL); |
373 |
|
|
} else if (ecparam_config.informat == FORMAT_PEM) { |
374 |
|
|
group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL); |
375 |
|
|
} else { |
376 |
|
|
BIO_printf(bio_err, "bad input format specified\n"); |
377 |
|
|
goto end; |
378 |
|
|
} |
379 |
|
|
|
380 |
✗✓ |
360 |
if (group == NULL) { |
381 |
|
|
BIO_printf(bio_err, |
382 |
|
|
"unable to load elliptic curve parameters\n"); |
383 |
|
|
ERR_print_errors(bio_err); |
384 |
|
|
goto end; |
385 |
|
|
} |
386 |
✓✗ |
360 |
if (ecparam_config.new_form) |
387 |
|
360 |
EC_GROUP_set_point_conversion_form(group, ecparam_config.form); |
388 |
|
|
|
389 |
✓✗ |
360 |
if (ecparam_config.new_asn1_flag) |
390 |
|
360 |
EC_GROUP_set_asn1_flag(group, ecparam_config.asn1_flag); |
391 |
|
|
|
392 |
✗✓ |
360 |
if (ecparam_config.no_seed) |
393 |
|
|
EC_GROUP_set_seed(group, NULL, 0); |
394 |
|
|
|
395 |
✗✓ |
360 |
if (ecparam_config.text) { |
396 |
|
|
if (!ECPKParameters_print(out, group, 0)) |
397 |
|
|
goto end; |
398 |
|
|
} |
399 |
✗✓ |
360 |
if (ecparam_config.check) { |
400 |
|
|
BIO_printf(bio_err, "checking elliptic curve parameters: "); |
401 |
|
|
if (!EC_GROUP_check(group, NULL)) { |
402 |
|
|
BIO_printf(bio_err, "failed\n"); |
403 |
|
|
ERR_print_errors(bio_err); |
404 |
|
|
} else |
405 |
|
|
BIO_printf(bio_err, "ok\n"); |
406 |
|
|
|
407 |
|
|
} |
408 |
✓✗ |
360 |
if (ecparam_config.C) { |
409 |
|
|
size_t buf_len = 0, tmp_len = 0; |
410 |
|
|
const EC_POINT *point; |
411 |
|
|
int is_prime, len = 0; |
412 |
|
360 |
const EC_METHOD *meth = EC_GROUP_method_of(group); |
413 |
|
|
|
414 |
✓✗✓✗ ✗✓ |
1080 |
if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL || |
415 |
✓✗✓✗
|
720 |
(ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL || |
416 |
✓✗ |
360 |
(ec_order = BN_new()) == NULL || |
417 |
|
360 |
(ec_cofactor = BN_new()) == NULL) { |
418 |
|
|
perror("malloc"); |
419 |
|
|
goto end; |
420 |
|
|
} |
421 |
|
360 |
is_prime = (EC_METHOD_get_field_type(meth) == |
422 |
|
|
NID_X9_62_prime_field); |
423 |
|
|
|
424 |
✓✓ |
360 |
if (is_prime) { |
425 |
✗✓ |
192 |
if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, |
426 |
|
|
ec_b, NULL)) |
427 |
|
|
goto end; |
428 |
|
|
} else { |
429 |
✗✓ |
168 |
if (!EC_GROUP_get_curve_GF2m(group, ec_p, ec_a, |
430 |
|
|
ec_b, NULL)) |
431 |
|
|
goto end; |
432 |
|
|
} |
433 |
|
|
|
434 |
✗✓ |
360 |
if ((point = EC_GROUP_get0_generator(group)) == NULL) |
435 |
|
|
goto end; |
436 |
✗✓ |
360 |
if (!EC_POINT_point2bn(group, point, |
437 |
|
360 |
EC_GROUP_get_point_conversion_form(group), ec_gen, |
438 |
|
|
NULL)) |
439 |
|
|
goto end; |
440 |
✗✓ |
360 |
if (!EC_GROUP_get_order(group, ec_order, NULL)) |
441 |
|
|
goto end; |
442 |
✗✓ |
360 |
if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL)) |
443 |
|
|
goto end; |
444 |
|
|
|
445 |
|
360 |
len = BN_num_bits(ec_order); |
446 |
|
|
|
447 |
✓✗ |
360 |
if ((tmp_len = (size_t) BN_num_bytes(ec_p)) > buf_len) |
448 |
|
360 |
buf_len = tmp_len; |
449 |
✗✓ |
360 |
if ((tmp_len = (size_t) BN_num_bytes(ec_a)) > buf_len) |
450 |
|
|
buf_len = tmp_len; |
451 |
✗✓ |
360 |
if ((tmp_len = (size_t) BN_num_bytes(ec_b)) > buf_len) |
452 |
|
|
buf_len = tmp_len; |
453 |
✓✓ |
360 |
if ((tmp_len = (size_t) BN_num_bytes(ec_gen)) > buf_len) |
454 |
|
340 |
buf_len = tmp_len; |
455 |
✗✓ |
360 |
if ((tmp_len = (size_t) BN_num_bytes(ec_order)) > buf_len) |
456 |
|
|
buf_len = tmp_len; |
457 |
✗✓ |
360 |
if ((tmp_len = (size_t) BN_num_bytes(ec_cofactor)) > buf_len) |
458 |
|
|
buf_len = tmp_len; |
459 |
|
|
|
460 |
|
360 |
buffer = malloc(buf_len); |
461 |
|
|
|
462 |
✗✓ |
360 |
if (buffer == NULL) { |
463 |
|
|
perror("malloc"); |
464 |
|
|
goto end; |
465 |
|
|
} |
466 |
|
360 |
ecparam_print_var(out, ec_p, "ec_p", len, buffer); |
467 |
|
360 |
ecparam_print_var(out, ec_a, "ec_a", len, buffer); |
468 |
|
360 |
ecparam_print_var(out, ec_b, "ec_b", len, buffer); |
469 |
|
360 |
ecparam_print_var(out, ec_gen, "ec_gen", len, buffer); |
470 |
|
360 |
ecparam_print_var(out, ec_order, "ec_order", len, buffer); |
471 |
|
360 |
ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, |
472 |
|
|
buffer); |
473 |
|
|
|
474 |
|
360 |
BIO_printf(out, "\n\n"); |
475 |
|
|
|
476 |
|
360 |
BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len); |
477 |
|
360 |
BIO_printf(out, "\tint ok=0;\n"); |
478 |
|
360 |
BIO_printf(out, "\tEC_GROUP *group = NULL;\n"); |
479 |
|
360 |
BIO_printf(out, "\tEC_POINT *point = NULL;\n"); |
480 |
|
360 |
BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, " |
481 |
|
|
"*tmp_3 = NULL;\n\n"); |
482 |
|
360 |
BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, " |
483 |
|
|
"sizeof(ec_p_%d), NULL)) == NULL)\n\t\t" |
484 |
|
|
"goto err;\n", len, len); |
485 |
|
360 |
BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, " |
486 |
|
|
"sizeof(ec_a_%d), NULL)) == NULL)\n\t\t" |
487 |
|
|
"goto err;\n", len, len); |
488 |
|
360 |
BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, " |
489 |
|
|
"sizeof(ec_b_%d), NULL)) == NULL)\n\t\t" |
490 |
|
|
"goto err;\n", len, len); |
491 |
✓✓ |
360 |
if (is_prime) { |
492 |
|
192 |
BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_" |
493 |
|
|
"GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)" |
494 |
|
|
"\n\t\tgoto err;\n\n"); |
495 |
|
192 |
} else { |
496 |
|
168 |
BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_" |
497 |
|
|
"GF2m(tmp_1, tmp_2, tmp_3, NULL)) == NULL)" |
498 |
|
|
"\n\t\tgoto err;\n\n"); |
499 |
|
|
} |
500 |
|
360 |
BIO_printf(out, "\t/* build generator */\n"); |
501 |
|
360 |
BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, " |
502 |
|
|
"sizeof(ec_gen_%d), tmp_1)) == NULL)" |
503 |
|
|
"\n\t\tgoto err;\n", len, len); |
504 |
|
360 |
BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, " |
505 |
|
|
"NULL, NULL);\n"); |
506 |
|
360 |
BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n"); |
507 |
|
360 |
BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, " |
508 |
|
|
"sizeof(ec_order_%d), tmp_2)) == NULL)" |
509 |
|
|
"\n\t\tgoto err;\n", len, len); |
510 |
|
360 |
BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, " |
511 |
|
|
"sizeof(ec_cofactor_%d), tmp_3)) == NULL)" |
512 |
|
|
"\n\t\tgoto err;\n", len, len); |
513 |
|
360 |
BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point," |
514 |
|
|
" tmp_2, tmp_3))\n\t\tgoto err;\n"); |
515 |
|
360 |
BIO_printf(out, "\n\tok=1;\n"); |
516 |
|
360 |
BIO_printf(out, "err:\n"); |
517 |
|
360 |
BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n"); |
518 |
|
360 |
BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n"); |
519 |
|
360 |
BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n"); |
520 |
|
360 |
BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n"); |
521 |
|
360 |
BIO_printf(out, "\tif (!ok)\n"); |
522 |
|
360 |
BIO_printf(out, "\t\t{\n"); |
523 |
|
360 |
BIO_printf(out, "\t\tEC_GROUP_free(group);\n"); |
524 |
|
360 |
BIO_printf(out, "\t\tgroup = NULL;\n"); |
525 |
|
360 |
BIO_printf(out, "\t\t}\n"); |
526 |
|
360 |
BIO_printf(out, "\treturn(group);\n\t}\n"); |
527 |
✓✗✗ |
360 |
} |
528 |
✓✗ |
360 |
if (!ecparam_config.noout) { |
529 |
✗✓ |
360 |
if (ecparam_config.outformat == FORMAT_ASN1) |
530 |
|
|
i = i2d_ECPKParameters_bio(out, group); |
531 |
✓✗ |
360 |
else if (ecparam_config.outformat == FORMAT_PEM) |
532 |
|
360 |
i = PEM_write_bio_ECPKParameters(out, group); |
533 |
|
|
else { |
534 |
|
|
BIO_printf(bio_err, "bad output format specified for" |
535 |
|
|
" outfile\n"); |
536 |
|
|
goto end; |
537 |
|
|
} |
538 |
✗✓ |
360 |
if (!i) { |
539 |
|
|
BIO_printf(bio_err, "unable to write elliptic " |
540 |
|
|
"curve parameters\n"); |
541 |
|
|
ERR_print_errors(bio_err); |
542 |
|
|
goto end; |
543 |
|
|
} |
544 |
|
|
} |
545 |
✓✗ |
360 |
if (ecparam_config.genkey) { |
546 |
|
360 |
EC_KEY *eckey = EC_KEY_new(); |
547 |
|
|
|
548 |
✗✓ |
360 |
if (eckey == NULL) |
549 |
|
|
goto end; |
550 |
|
|
|
551 |
✗✓ |
360 |
if (EC_KEY_set_group(eckey, group) == 0) { |
552 |
|
|
EC_KEY_free(eckey); |
553 |
|
|
goto end; |
554 |
|
|
} |
555 |
|
|
|
556 |
✗✓ |
360 |
if (!EC_KEY_generate_key(eckey)) { |
557 |
|
|
EC_KEY_free(eckey); |
558 |
|
|
goto end; |
559 |
|
|
} |
560 |
✗✓ |
360 |
if (ecparam_config.outformat == FORMAT_ASN1) |
561 |
|
|
i = i2d_ECPrivateKey_bio(out, eckey); |
562 |
✓✗ |
360 |
else if (ecparam_config.outformat == FORMAT_PEM) |
563 |
|
360 |
i = PEM_write_bio_ECPrivateKey(out, eckey, NULL, |
564 |
|
|
NULL, 0, NULL, NULL); |
565 |
|
|
else { |
566 |
|
|
BIO_printf(bio_err, "bad output format specified " |
567 |
|
|
"for outfile\n"); |
568 |
|
|
EC_KEY_free(eckey); |
569 |
|
|
goto end; |
570 |
|
|
} |
571 |
|
360 |
EC_KEY_free(eckey); |
572 |
✓✓✓ |
360 |
} |
573 |
|
360 |
ret = 0; |
574 |
|
|
|
575 |
|
|
end: |
576 |
|
372 |
BN_free(ec_p); |
577 |
|
372 |
BN_free(ec_a); |
578 |
|
372 |
BN_free(ec_b); |
579 |
|
372 |
BN_free(ec_gen); |
580 |
|
372 |
BN_free(ec_order); |
581 |
|
372 |
BN_free(ec_cofactor); |
582 |
|
|
|
583 |
|
372 |
free(buffer); |
584 |
|
|
|
585 |
|
372 |
BIO_free(in); |
586 |
|
372 |
BIO_free_all(out); |
587 |
|
372 |
EC_GROUP_free(group); |
588 |
|
|
|
589 |
|
372 |
return (ret); |
590 |
|
372 |
} |
591 |
|
|
|
592 |
|
|
static int |
593 |
|
|
ecparam_print_var(BIO * out, BIGNUM * in, const char *var, |
594 |
|
|
int len, unsigned char *buffer) |
595 |
|
|
{ |
596 |
|
4320 |
BIO_printf(out, "static unsigned char %s_%d[] = {", var, len); |
597 |
✓✓ |
2160 |
if (BN_is_zero(in)) |
598 |
|
60 |
BIO_printf(out, "\n\t0x00"); |
599 |
|
|
else { |
600 |
|
|
int i, l; |
601 |
|
|
|
602 |
|
2100 |
l = BN_bn2bin(in, buffer); |
603 |
✓✓ |
100544 |
for (i = 0; i < l - 1; i++) { |
604 |
✓✓ |
48172 |
if ((i % 12) == 0) |
605 |
|
4784 |
BIO_printf(out, "\n\t"); |
606 |
|
48172 |
BIO_printf(out, "0x%02X,", buffer[i]); |
607 |
|
|
} |
608 |
✓✓ |
2100 |
if ((i % 12) == 0) |
609 |
|
540 |
BIO_printf(out, "\n\t"); |
610 |
|
2100 |
BIO_printf(out, "0x%02X", buffer[i]); |
611 |
|
|
} |
612 |
|
2160 |
BIO_printf(out, "\n\t};\n\n"); |
613 |
|
2160 |
return 1; |
614 |
|
|
} |
615 |
|
|
#endif |