summaryrefslogtreecommitdiff
path: root/src/usr.bin/openssl/ecparam.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr.bin/openssl/ecparam.c')
-rw-r--r--src/usr.bin/openssl/ecparam.c152
1 files changed, 2 insertions, 150 deletions
diff --git a/src/usr.bin/openssl/ecparam.c b/src/usr.bin/openssl/ecparam.c
index 2d2755acfc..285f5d563e 100644
--- a/src/usr.bin/openssl/ecparam.c
+++ b/src/usr.bin/openssl/ecparam.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecparam.c,v 1.24 2025/01/19 07:41:52 tb Exp $ */ 1/* $OpenBSD: ecparam.c,v 1.25 2025/01/19 10:24:17 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -87,11 +87,7 @@
87#include <openssl/pem.h> 87#include <openssl/pem.h>
88#include <openssl/x509.h> 88#include <openssl/x509.h>
89 89
90static int ecparam_print_var(BIO *, BIGNUM *, const char *, int,
91 unsigned char *);
92
93static struct { 90static struct {
94 int C;
95 int asn1_flag; 91 int asn1_flag;
96 int check; 92 int check;
97 char *curve_name; 93 char *curve_name;
@@ -141,12 +137,6 @@ ecparam_opt_enctype(char *arg)
141 137
142static const struct option ecparam_options[] = { 138static const struct option ecparam_options[] = {
143 { 139 {
144 .name = "C",
145 .desc = "Convert the EC parameters into C code",
146 .type = OPTION_FLAG,
147 .opt.flag = &cfg.C,
148 },
149 {
150 .name = "check", 140 .name = "check",
151 .desc = "Validate the elliptic curve parameters", 141 .desc = "Validate the elliptic curve parameters",
152 .type = OPTION_FLAG, 142 .type = OPTION_FLAG,
@@ -241,7 +231,7 @@ static const struct option ecparam_options[] = {
241static void 231static void
242ecparam_usage(void) 232ecparam_usage(void)
243{ 233{
244 fprintf(stderr, "usage: ecparam [-C] [-check] [-conv_form arg] " 234 fprintf(stderr, "usage: ecparam [-check] [-conv_form arg] "
245 " [-genkey]\n" 235 " [-genkey]\n"
246 " [-in file] [-inform DER | PEM] [-list_curves] [-name arg]\n" 236 " [-in file] [-inform DER | PEM] [-list_curves] [-name arg]\n"
247 " [-no_seed] [-noout] [-out file] [-outform DER | PEM]\n" 237 " [-no_seed] [-noout] [-out file] [-outform DER | PEM]\n"
@@ -252,10 +242,7 @@ ecparam_usage(void)
252int 242int
253ecparam_main(int argc, char **argv) 243ecparam_main(int argc, char **argv)
254{ 244{
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; 245 EC_GROUP *group = NULL;
258 unsigned char *buffer = NULL;
259 BIO *in = NULL, *out = NULL; 246 BIO *in = NULL, *out = NULL;
260 int i, ret = 1; 247 int i, ret = 1;
261 248
@@ -403,109 +390,6 @@ ecparam_main(int argc, char **argv)
403 BIO_printf(bio_err, "ok\n"); 390 BIO_printf(bio_err, "ok\n");
404 391
405 } 392 }
406 if (cfg.C) {
407 size_t buf_len = 0, tmp_len = 0;
408 const EC_POINT *point;
409 int len = 0;
410
411 if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
412 (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
413 (ec_order = BN_new()) == NULL ||
414 (ec_cofactor = BN_new()) == NULL) {
415 perror("malloc");
416 goto end;
417 }
418
419 if (!EC_GROUP_get_curve(group, ec_p, ec_a, ec_b, NULL))
420 goto end;
421
422 if ((point = EC_GROUP_get0_generator(group)) == NULL)
423 goto end;
424 if (!EC_POINT_point2bn(group, point,
425 EC_GROUP_get_point_conversion_form(group), ec_gen,
426 NULL))
427 goto end;
428 if (!EC_GROUP_get_order(group, ec_order, NULL))
429 goto end;
430 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
431 goto end;
432
433 len = BN_num_bits(ec_order);
434
435 if ((tmp_len = (size_t) BN_num_bytes(ec_p)) > buf_len)
436 buf_len = tmp_len;
437 if ((tmp_len = (size_t) BN_num_bytes(ec_a)) > buf_len)
438 buf_len = tmp_len;
439 if ((tmp_len = (size_t) BN_num_bytes(ec_b)) > buf_len)
440 buf_len = tmp_len;
441 if ((tmp_len = (size_t) BN_num_bytes(ec_gen)) > buf_len)
442 buf_len = tmp_len;
443 if ((tmp_len = (size_t) BN_num_bytes(ec_order)) > buf_len)
444 buf_len = tmp_len;
445 if ((tmp_len = (size_t) BN_num_bytes(ec_cofactor)) > buf_len)
446 buf_len = tmp_len;
447
448 buffer = malloc(buf_len);
449
450 if (buffer == NULL) {
451 perror("malloc");
452 goto end;
453 }
454 ecparam_print_var(out, ec_p, "ec_p", len, buffer);
455 ecparam_print_var(out, ec_a, "ec_a", len, buffer);
456 ecparam_print_var(out, ec_b, "ec_b", len, buffer);
457 ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
458 ecparam_print_var(out, ec_order, "ec_order", len, buffer);
459 ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
460 buffer);
461
462 BIO_printf(out, "\n\n");
463
464 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
465 BIO_printf(out, "\tint ok=0;\n");
466 BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
467 BIO_printf(out, "\tEC_POINT *point = NULL;\n");
468 BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
469 "*tmp_3 = NULL;\n\n");
470 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
471 "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
472 "goto err;\n", len, len);
473 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
474 "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
475 "goto err;\n", len, len);
476 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
477 "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
478 "goto err;\n", len, len);
479 BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_GFp"
480 "(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n\t\tgoto err;\n\n");
481 BIO_printf(out, "\t/* build generator */\n");
482 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
483 "sizeof(ec_gen_%d), tmp_1)) == NULL)"
484 "\n\t\tgoto err;\n", len, len);
485 BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
486 "NULL, NULL);\n");
487 BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
488 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
489 "sizeof(ec_order_%d), tmp_2)) == NULL)"
490 "\n\t\tgoto err;\n", len, len);
491 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
492 "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
493 "\n\t\tgoto err;\n", len, len);
494 BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
495 " tmp_2, tmp_3))\n\t\tgoto err;\n");
496 BIO_printf(out, "\n\tok=1;\n");
497 BIO_printf(out, "err:\n");
498 BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
499 BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
500 BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
501 BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
502 BIO_printf(out, "\tif (!ok)\n");
503 BIO_printf(out, "\t\t{\n");
504 BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
505 BIO_printf(out, "\t\tgroup = NULL;\n");
506 BIO_printf(out, "\t\t}\n");
507 BIO_printf(out, "\treturn(group);\n\t}\n");
508 }
509 if (!cfg.noout) { 393 if (!cfg.noout) {
510 if (cfg.outformat == FORMAT_ASN1) 394 if (cfg.outformat == FORMAT_ASN1)
511 i = i2d_ECPKParameters_bio(out, group); 395 i = i2d_ECPKParameters_bio(out, group);
@@ -554,15 +438,6 @@ ecparam_main(int argc, char **argv)
554 ret = 0; 438 ret = 0;
555 439
556 end: 440 end:
557 BN_free(ec_p);
558 BN_free(ec_a);
559 BN_free(ec_b);
560 BN_free(ec_gen);
561 BN_free(ec_order);
562 BN_free(ec_cofactor);
563
564 free(buffer);
565
566 BIO_free(in); 441 BIO_free(in);
567 BIO_free_all(out); 442 BIO_free_all(out);
568 EC_GROUP_free(group); 443 EC_GROUP_free(group);
@@ -570,27 +445,4 @@ ecparam_main(int argc, char **argv)
570 return (ret); 445 return (ret);
571} 446}
572 447
573static int
574ecparam_print_var(BIO * out, BIGNUM * in, const char *var,
575 int len, unsigned char *buffer)
576{
577 BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
578 if (BN_is_zero(in))
579 BIO_printf(out, "\n\t0x00");
580 else {
581 int i, l;
582
583 l = BN_bn2bin(in, buffer);
584 for (i = 0; i < l - 1; i++) {
585 if ((i % 12) == 0)
586 BIO_printf(out, "\n\t");
587 BIO_printf(out, "0x%02X,", buffer[i]);
588 }
589 if ((i % 12) == 0)
590 BIO_printf(out, "\n\t");
591 BIO_printf(out, "0x%02X", buffer[i]);
592 }
593 BIO_printf(out, "\n\t};\n\n");
594 return 1;
595}
596#endif 448#endif