2 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/bn.h>
16 typedef enum OPTION_choice {
17 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
18 OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS
21 const OPTIONS prime_options[] = {
22 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
23 {OPT_HELP_STR, 1, '-',
24 " number Number to check for primality\n"},
26 OPT_SECTION("General"),
27 {"help", OPT_HELP, '-', "Display this summary"},
28 {"bits", OPT_BITS, 'p', "Size of number in bits"},
29 {"checks", OPT_CHECKS, 'p', "Number of checks"},
31 OPT_SECTION("Output"),
32 {"hex", OPT_HEX, '-', "Hex output"},
33 {"generate", OPT_GENERATE, '-', "Generate a prime"},
34 {"safe", OPT_SAFE, '-',
35 "When used with -generate, generate a safe prime"},
39 int prime_main(int argc, char **argv)
42 int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1;
46 prog = opt_init(argc, argv, prime_options);
47 while ((o = opt_next()) != OPT_EOF) {
52 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
55 opt_help(prime_options);
65 bits = atoi(opt_arg());
71 /* ignore parameter and argument */
76 argc = opt_num_rest();
81 BIO_printf(bio_err, "Extra arguments given.\n");
84 } else if (argc == 0) {
85 BIO_printf(bio_err, "%s: No prime specified\n", prog);
93 BIO_printf(bio_err, "Specify the number of bits.\n");
98 BIO_printf(bio_err, "Out of memory.\n");
101 if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
102 BIO_printf(bio_err, "Failed to generate prime.\n");
105 s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
107 BIO_printf(bio_err, "Out of memory.\n");
110 BIO_printf(bio_out, "%s\n", s);
113 for ( ; *argv; argv++) {
117 r = BN_hex2bn(&bn, argv[0]);
119 r = BN_dec2bn(&bn, argv[0]);
122 BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
126 BN_print(bio_out, bn);
127 BIO_printf(bio_out, " (%s) %s prime\n",
129 BN_check_prime(bn, NULL, NULL)