version 1.16, 2023/03/05 13:12:53 |
version 1.17, 2023/03/06 14:32:06 |
|
|
int generate; |
int generate; |
int hex; |
int hex; |
int safe; |
int safe; |
} prime_config; |
} cfg; |
|
|
static const struct option prime_options[] = { |
static const struct option prime_options[] = { |
{ |
{ |
|
|
.argname = "n", |
.argname = "n", |
.desc = "Number of bits in the generated prime number", |
.desc = "Number of bits in the generated prime number", |
.type = OPTION_ARG_INT, |
.type = OPTION_ARG_INT, |
.opt.value = &prime_config.bits, |
.opt.value = &cfg.bits, |
}, |
}, |
{ |
{ |
.name = "checks", |
.name = "checks", |
.argname = "n", |
.argname = "n", |
.desc = "Miller-Rabin probabilistic primality test iterations", |
.desc = "Miller-Rabin probabilistic primality test iterations", |
.type = OPTION_ARG_INT, |
.type = OPTION_ARG_INT, |
.opt.value = &prime_config.checks, |
.opt.value = &cfg.checks, |
}, |
}, |
{ |
{ |
.name = "generate", |
.name = "generate", |
.desc = "Generate a pseudo-random prime number", |
.desc = "Generate a pseudo-random prime number", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &prime_config.generate, |
.opt.flag = &cfg.generate, |
}, |
}, |
{ |
{ |
.name = "hex", |
.name = "hex", |
.desc = "Hexadecimal prime numbers", |
.desc = "Hexadecimal prime numbers", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &prime_config.hex, |
.opt.flag = &cfg.hex, |
}, |
}, |
{ |
{ |
.name = "safe", |
.name = "safe", |
.desc = "Generate only \"safe\" prime numbers", |
.desc = "Generate only \"safe\" prime numbers", |
.type = OPTION_FLAG, |
.type = OPTION_FLAG, |
.opt.flag = &prime_config.safe, |
.opt.flag = &cfg.safe, |
}, |
}, |
{NULL}, |
{NULL}, |
}; |
}; |
|
|
exit(1); |
exit(1); |
} |
} |
|
|
memset(&prime_config, 0, sizeof(prime_config)); |
memset(&cfg, 0, sizeof(cfg)); |
|
|
/* Default iterations for Miller-Rabin probabilistic primality test. */ |
/* Default iterations for Miller-Rabin probabilistic primality test. */ |
prime_config.checks = 20; |
cfg.checks = 20; |
|
|
if (options_parse(argc, argv, prime_options, &prime, NULL) != 0) { |
if (options_parse(argc, argv, prime_options, &prime, NULL) != 0) { |
prime_usage(); |
prime_usage(); |
return (1); |
return (1); |
} |
} |
|
|
if (prime == NULL && prime_config.generate == 0) { |
if (prime == NULL && cfg.generate == 0) { |
BIO_printf(bio_err, "No prime specified.\n"); |
BIO_printf(bio_err, "No prime specified.\n"); |
prime_usage(); |
prime_usage(); |
return (1); |
return (1); |
|
|
} |
} |
BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); |
BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); |
|
|
if (prime_config.generate != 0) { |
if (cfg.generate != 0) { |
if (prime_config.bits == 0) { |
if (cfg.bits == 0) { |
BIO_printf(bio_err, "Specify the number of bits.\n"); |
BIO_printf(bio_err, "Specify the number of bits.\n"); |
goto end; |
goto end; |
} |
} |
|
|
BIO_printf(bio_err, "Out of memory.\n"); |
BIO_printf(bio_err, "Out of memory.\n"); |
goto end; |
goto end; |
} |
} |
if (!BN_generate_prime_ex(bn, prime_config.bits, |
if (!BN_generate_prime_ex(bn, cfg.bits, |
prime_config.safe, NULL, NULL, NULL)) { |
cfg.safe, NULL, NULL, NULL)) { |
BIO_printf(bio_err, "Prime generation error.\n"); |
BIO_printf(bio_err, "Prime generation error.\n"); |
goto end; |
goto end; |
} |
} |
s = prime_config.hex ? BN_bn2hex(bn) : BN_bn2dec(bn); |
s = cfg.hex ? BN_bn2hex(bn) : BN_bn2dec(bn); |
if (s == NULL) { |
if (s == NULL) { |
BIO_printf(bio_err, "Out of memory.\n"); |
BIO_printf(bio_err, "Out of memory.\n"); |
goto end; |
goto end; |
|
|
BIO_printf(bio_out, "%s\n", s); |
BIO_printf(bio_out, "%s\n", s); |
free(s); |
free(s); |
} else { |
} else { |
if (prime_config.hex) { |
if (cfg.hex) { |
if (!BN_hex2bn(&bn, prime)) { |
if (!BN_hex2bn(&bn, prime)) { |
BIO_printf(bio_err, "%s is an invalid hex " |
BIO_printf(bio_err, "%s is an invalid hex " |
"value.\n", prime); |
"value.\n", prime); |
|
|
} |
} |
} |
} |
|
|
is_prime = BN_is_prime_ex(bn, prime_config.checks, NULL, NULL); |
is_prime = BN_is_prime_ex(bn, cfg.checks, NULL, NULL); |
if (is_prime < 0) { |
if (is_prime < 0) { |
BIO_printf(bio_err, "BN_is_prime_ex failed.\n"); |
BIO_printf(bio_err, "BN_is_prime_ex failed.\n"); |
goto end; |
goto end; |