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