1 /* $OpenBSD: prime.c,v 1.13 2019/07/14 03:30:46 guenther Exp $ */
2 /* ====================================================================
3 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
56 #include <openssl/bn.h>
57 #include <openssl/err.h>
67 static const struct option prime_options[] = {
71 .desc = "Number of bits in the generated prime number",
72 .type = OPTION_ARG_INT,
73 .opt.value = &prime_config.bits,
78 .desc = "Miller-Rabin probablistic primality test iterations",
79 .type = OPTION_ARG_INT,
80 .opt.value = &prime_config.checks,
84 .desc = "Generate a pseudo-random prime number",
86 .opt.flag = &prime_config.generate,
90 .desc = "Hexadecimal prime numbers",
92 .opt.flag = &prime_config.hex,
96 .desc = "Generate only \"safe\" prime numbers",
98 .opt.flag = &prime_config.safe,
107 "usage: prime [-bits n] [-checks n] [-generate] [-hex] [-safe] "
109 options_usage(prime_options);
113 prime_main(int argc, char **argv)
119 int is_prime, ret = 1;
121 if (single_execution) {
122 if (pledge("stdio rpath", NULL) == -1) {
128 memset(&prime_config, 0, sizeof(prime_config));
130 /* Default iterations for Miller-Rabin probabilistic primality test. */
131 prime_config.checks = 20;
133 if (options_parse(argc, argv, prime_options, &prime, NULL) != 0) {
138 if (prime == NULL && prime_config.generate == 0) {
139 BIO_printf(bio_err, "No prime specified.\n");
144 if ((bio_out = BIO_new(BIO_s_file())) == NULL) {
145 ERR_print_errors(bio_err);
148 BIO_set_fp(bio_out, stdout, BIO_NOCLOSE);
150 if (prime_config.generate != 0) {
151 if (prime_config.bits == 0) {
152 BIO_printf(bio_err, "Specify the number of bits.\n");
157 BIO_printf(bio_err, "Out of memory.\n");
160 if (!BN_generate_prime_ex(bn, prime_config.bits,
161 prime_config.safe, NULL, NULL, NULL)) {
162 BIO_printf(bio_err, "Prime generation error.\n");
165 s = prime_config.hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
167 BIO_printf(bio_err, "Out of memory.\n");
170 BIO_printf(bio_out, "%s\n", s);
173 if (prime_config.hex) {
174 if (!BN_hex2bn(&bn, prime)) {
175 BIO_printf(bio_err, "%s is an invalid hex "
180 if (!BN_dec2bn(&bn, prime)) {
181 BIO_printf(bio_err, "%s is an invalid decimal "
187 is_prime = BN_is_prime_ex(bn, prime_config.checks, NULL, NULL);
189 BIO_printf(bio_err, "BN_is_prime_ex failed.\n");
192 BIO_printf(bio_out, "%s is %sprime\n", prime,
193 is_prime == 1 ? "" : "not ");
200 BIO_free_all(bio_out);