]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved. | |
b08868c4 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
b08868c4 DSH |
8 | */ |
9 | ||
10 | #include <string.h> | |
11 | ||
12 | #include "apps.h" | |
13 | #include <openssl/bn.h> | |
14 | ||
7e1b7485 RS |
15 | typedef enum OPTION_choice { |
16 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
17 | OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS | |
18 | } OPTION_CHOICE; | |
19 | ||
44c83ebd | 20 | const OPTIONS prime_options[] = { |
7e1b7485 RS |
21 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"}, |
22 | {OPT_HELP_STR, 1, '-', | |
e61434b4 | 23 | " number Number to check for primality\n"}, |
7e1b7485 RS |
24 | {"help", OPT_HELP, '-', "Display this summary"}, |
25 | {"hex", OPT_HEX, '-', "Hex output"}, | |
26 | {"generate", OPT_GENERATE, '-', "Generate a prime"}, | |
27 | {"bits", OPT_BITS, 'p', "Size of number in bits"}, | |
28 | {"safe", OPT_SAFE, '-', | |
29 | "When used with -generate, generate a safe prime"}, | |
30 | {"checks", OPT_CHECKS, 'p', "Number of checks"}, | |
31 | {NULL} | |
32 | }; | |
33 | ||
34 | int prime_main(int argc, char **argv) | |
0f113f3e | 35 | { |
0f113f3e | 36 | BIGNUM *bn = NULL; |
7e1b7485 RS |
37 | int hex = 0, checks = 20, generate = 0, bits = 0, safe = 0, ret = 1; |
38 | char *prog; | |
39 | OPTION_CHOICE o; | |
40 | ||
41 | prog = opt_init(argc, argv, prime_options); | |
42 | while ((o = opt_next()) != OPT_EOF) { | |
43 | switch (o) { | |
44 | case OPT_EOF: | |
45 | case OPT_ERR: | |
c27363f5 | 46 | opthelp: |
7e1b7485 RS |
47 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
48 | goto end; | |
49 | case OPT_HELP: | |
50 | opt_help(prime_options); | |
51 | ret = 0; | |
52 | goto end; | |
53 | case OPT_HEX: | |
0f113f3e | 54 | hex = 1; |
7e1b7485 RS |
55 | break; |
56 | case OPT_GENERATE: | |
0f113f3e | 57 | generate = 1; |
7e1b7485 RS |
58 | break; |
59 | case OPT_BITS: | |
60 | bits = atoi(opt_arg()); | |
61 | break; | |
62 | case OPT_SAFE: | |
0f113f3e | 63 | safe = 1; |
7e1b7485 RS |
64 | break; |
65 | case OPT_CHECKS: | |
66 | checks = atoi(opt_arg()); | |
67 | break; | |
0f113f3e | 68 | } |
0f113f3e | 69 | } |
7e1b7485 RS |
70 | argc = opt_num_rest(); |
71 | argv = opt_rest(); | |
0f113f3e | 72 | |
c27363f5 RS |
73 | if (generate) { |
74 | if (argc != 0) { | |
75 | BIO_printf(bio_err, "Extra arguments given.\n"); | |
76 | goto opthelp; | |
77 | } | |
78 | } else if (argc == 0) { | |
7e1b7485 | 79 | BIO_printf(bio_err, "%s: No prime specified\n", prog); |
c27363f5 | 80 | goto opthelp; |
0f113f3e MC |
81 | } |
82 | ||
83 | if (generate) { | |
84 | char *s; | |
85 | ||
86 | if (!bits) { | |
5573ee36 | 87 | BIO_printf(bio_err, "Specify the number of bits.\n"); |
7e1b7485 | 88 | goto end; |
0f113f3e MC |
89 | } |
90 | bn = BN_new(); | |
9b5164ce DSH |
91 | if (bn == NULL) { |
92 | BIO_printf(bio_err, "Out of memory.\n"); | |
93 | goto end; | |
94 | } | |
e69f2a22 MC |
95 | if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) { |
96 | BIO_printf(bio_err, "Failed to generate prime.\n"); | |
97 | goto end; | |
98 | } | |
0f113f3e | 99 | s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn); |
9b5164ce DSH |
100 | if (s == NULL) { |
101 | BIO_printf(bio_err, "Out of memory.\n"); | |
102 | goto end; | |
103 | } | |
0f113f3e MC |
104 | BIO_printf(bio_out, "%s\n", s); |
105 | OPENSSL_free(s); | |
106 | } else { | |
7e1b7485 | 107 | for ( ; *argv; argv++) { |
e69f2a22 MC |
108 | int r; |
109 | ||
7e1b7485 | 110 | if (hex) |
e69f2a22 | 111 | r = BN_hex2bn(&bn, argv[0]); |
7e1b7485 | 112 | else |
e69f2a22 MC |
113 | r = BN_dec2bn(&bn, argv[0]); |
114 | ||
28b86f31 | 115 | if (!r) { |
e69f2a22 MC |
116 | BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]); |
117 | goto end; | |
118 | } | |
0f113f3e | 119 | |
7e1b7485 RS |
120 | BN_print(bio_out, bn); |
121 | BIO_printf(bio_out, " (%s) %s prime\n", | |
122 | argv[0], | |
123 | BN_is_prime_ex(bn, checks, NULL, NULL) | |
124 | ? "is" : "is not"); | |
125 | } | |
0f113f3e | 126 | } |
b08868c4 | 127 | |
e69f2a22 | 128 | ret = 0; |
7e1b7485 | 129 | end: |
5bf7c772 | 130 | BN_free(bn); |
7e1b7485 | 131 | return ret; |
0f113f3e | 132 | } |