]>
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: | |
46 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
47 | goto end; | |
48 | case OPT_HELP: | |
49 | opt_help(prime_options); | |
50 | ret = 0; | |
51 | goto end; | |
52 | case OPT_HEX: | |
0f113f3e | 53 | hex = 1; |
7e1b7485 RS |
54 | break; |
55 | case OPT_GENERATE: | |
0f113f3e | 56 | generate = 1; |
7e1b7485 RS |
57 | break; |
58 | case OPT_BITS: | |
59 | bits = atoi(opt_arg()); | |
60 | break; | |
61 | case OPT_SAFE: | |
0f113f3e | 62 | safe = 1; |
7e1b7485 RS |
63 | break; |
64 | case OPT_CHECKS: | |
65 | checks = atoi(opt_arg()); | |
66 | break; | |
0f113f3e | 67 | } |
0f113f3e | 68 | } |
7e1b7485 RS |
69 | argc = opt_num_rest(); |
70 | argv = opt_rest(); | |
0f113f3e | 71 | |
7e1b7485 RS |
72 | if (argc == 0 && !generate) { |
73 | BIO_printf(bio_err, "%s: No prime specified\n", prog); | |
74 | goto end; | |
0f113f3e MC |
75 | } |
76 | ||
77 | if (generate) { | |
78 | char *s; | |
79 | ||
80 | if (!bits) { | |
5573ee36 | 81 | BIO_printf(bio_err, "Specify the number of bits.\n"); |
7e1b7485 | 82 | goto end; |
0f113f3e MC |
83 | } |
84 | bn = BN_new(); | |
9b5164ce DSH |
85 | if (bn == NULL) { |
86 | BIO_printf(bio_err, "Out of memory.\n"); | |
87 | goto end; | |
88 | } | |
e69f2a22 MC |
89 | if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) { |
90 | BIO_printf(bio_err, "Failed to generate prime.\n"); | |
91 | goto end; | |
92 | } | |
0f113f3e | 93 | s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn); |
9b5164ce DSH |
94 | if (s == NULL) { |
95 | BIO_printf(bio_err, "Out of memory.\n"); | |
96 | goto end; | |
97 | } | |
0f113f3e MC |
98 | BIO_printf(bio_out, "%s\n", s); |
99 | OPENSSL_free(s); | |
100 | } else { | |
7e1b7485 | 101 | for ( ; *argv; argv++) { |
e69f2a22 MC |
102 | int r; |
103 | ||
7e1b7485 | 104 | if (hex) |
e69f2a22 | 105 | r = BN_hex2bn(&bn, argv[0]); |
7e1b7485 | 106 | else |
e69f2a22 MC |
107 | r = BN_dec2bn(&bn, argv[0]); |
108 | ||
109 | if(!r) { | |
110 | BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]); | |
111 | goto end; | |
112 | } | |
0f113f3e | 113 | |
7e1b7485 RS |
114 | BN_print(bio_out, bn); |
115 | BIO_printf(bio_out, " (%s) %s prime\n", | |
116 | argv[0], | |
117 | BN_is_prime_ex(bn, checks, NULL, NULL) | |
118 | ? "is" : "is not"); | |
119 | } | |
0f113f3e | 120 | } |
b08868c4 | 121 | |
e69f2a22 | 122 | ret = 0; |
7e1b7485 | 123 | end: |
5bf7c772 | 124 | BN_free(bn); |
7e1b7485 | 125 | return ret; |
0f113f3e | 126 | } |