]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/prime.c
2 * Copyright 2004-2020 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
{
18 OPT_HEX
, OPT_GENERATE
, OPT_BITS
, OPT_SAFE
, OPT_CHECKS
,
22 const OPTIONS prime_options
[] = {
23 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [number...]\n"},
25 OPT_SECTION("General"),
26 {"help", OPT_HELP
, '-', "Display this summary"},
27 {"bits", OPT_BITS
, 'p', "Size of number in bits"},
28 {"checks", OPT_CHECKS
, 'p', "Number of checks"},
30 OPT_SECTION("Output"),
31 {"hex", OPT_HEX
, '-', "Hex output"},
32 {"generate", OPT_GENERATE
, '-', "Generate a prime"},
33 {"safe", OPT_SAFE
, '-',
34 "When used with -generate, generate a safe prime"},
39 {"number", 0, 0, "Number(s) to check for primality if not generating"},
43 int prime_main(int argc
, char **argv
)
46 int hex
= 0, generate
= 0, bits
= 0, safe
= 0, ret
= 1;
50 prog
= opt_init(argc
, argv
, prime_options
);
51 while ((o
= opt_next()) != OPT_EOF
) {
56 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
59 opt_help(prime_options
);
69 bits
= atoi(opt_arg());
75 /* ignore parameter and argument */
85 /* Optional arguments are numbers to check. */
86 argc
= opt_num_rest();
91 } else if (argc
== 0) {
99 BIO_printf(bio_err
, "Specify the number of bits.\n");
104 BIO_printf(bio_err
, "Out of memory.\n");
107 if (!BN_generate_prime_ex(bn
, bits
, safe
, NULL
, NULL
, NULL
)) {
108 BIO_printf(bio_err
, "Failed to generate prime.\n");
111 s
= hex
? BN_bn2hex(bn
) : BN_bn2dec(bn
);
113 BIO_printf(bio_err
, "Out of memory.\n");
116 BIO_printf(bio_out
, "%s\n", s
);
119 for ( ; *argv
; argv
++) {
123 r
= BN_hex2bn(&bn
, argv
[0]);
125 r
= BN_dec2bn(&bn
, argv
[0]);
128 BIO_printf(bio_err
, "Failed to process value (%s)\n", argv
[0]);
132 BN_print(bio_out
, bn
);
133 BIO_printf(bio_out
, " (%s) %s prime\n",
135 BN_check_prime(bn
, NULL
, NULL
)