diff options
author | jsing <> | 2014-08-27 15:55:23 +0000 |
---|---|---|
committer | jsing <> | 2014-08-27 15:55:23 +0000 |
commit | cfb0c7a009844448f5c960b5f0e5bcf641f2f953 (patch) | |
tree | 6a2ce98b8e6443591887f18c31bfdc9d5ba8a4b2 /src | |
parent | 809f43040a62797ab71961c1b3812627db0b5cef (diff) | |
download | openbsd-cfb0c7a009844448f5c960b5f0e5bcf641f2f953.tar.gz openbsd-cfb0c7a009844448f5c960b5f0e5bcf641f2f953.tar.bz2 openbsd-cfb0c7a009844448f5c960b5f0e5bcf641f2f953.zip |
Convert openssl(1) prime to the new options/usage handling.
Diffstat (limited to 'src')
-rw-r--r-- | src/usr.bin/openssl/prime.c | 141 |
1 files changed, 81 insertions, 60 deletions
diff --git a/src/usr.bin/openssl/prime.c b/src/usr.bin/openssl/prime.c index e7fb3257d8..06414fde0d 100644 --- a/src/usr.bin/openssl/prime.c +++ b/src/usr.bin/openssl/prime.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: prime.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */ | 1 | /* $OpenBSD: prime.c,v 1.2 2014/08/27 15:55:23 jsing Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -55,93 +55,114 @@ | |||
55 | 55 | ||
56 | #include <openssl/bn.h> | 56 | #include <openssl/bn.h> |
57 | 57 | ||
58 | struct { | ||
59 | int bits; | ||
60 | int checks; | ||
61 | int generate; | ||
62 | int hex; | ||
63 | int safe; | ||
64 | } prime_config; | ||
65 | |||
66 | struct option prime_options[] = { | ||
67 | { | ||
68 | .name = "bits", | ||
69 | .argname = "n", | ||
70 | .desc = "Number of bits in the generated prime number", | ||
71 | .type = OPTION_ARG_INT, | ||
72 | .opt.value = &prime_config.bits, | ||
73 | }, | ||
74 | { | ||
75 | .name = "checks", | ||
76 | .argname = "n", | ||
77 | .desc = "Miller-Rabin probablistic primality test iterations", | ||
78 | .type = OPTION_ARG_INT, | ||
79 | .opt.value = &prime_config.checks, | ||
80 | }, | ||
81 | { | ||
82 | .name = "generate", | ||
83 | .desc = "Generate a pseudo-random prime number", | ||
84 | .type = OPTION_FLAG, | ||
85 | .opt.flag = &prime_config.generate, | ||
86 | }, | ||
87 | { | ||
88 | .name = "hex", | ||
89 | .desc = "Hexadecimal prime numbers", | ||
90 | .type = OPTION_FLAG, | ||
91 | .opt.flag = &prime_config.hex, | ||
92 | }, | ||
93 | { | ||
94 | .name = "safe", | ||
95 | .desc = "Generate only \"safe\" prime numbers", | ||
96 | .type = OPTION_FLAG, | ||
97 | .opt.flag = &prime_config.safe, | ||
98 | }, | ||
99 | {}, | ||
100 | }; | ||
101 | |||
102 | static void | ||
103 | prime_usage() | ||
104 | { | ||
105 | fprintf(stderr, | ||
106 | "usage: prime [-bits n] [-checks n] [-generate] [-hex] [-safe] " | ||
107 | "p\n"); | ||
108 | options_usage(prime_options); | ||
109 | } | ||
110 | |||
58 | int prime_main(int, char **); | 111 | int prime_main(int, char **); |
59 | 112 | ||
60 | int | 113 | int |
61 | prime_main(int argc, char **argv) | 114 | prime_main(int argc, char **argv) |
62 | { | 115 | { |
63 | int hex = 0; | ||
64 | int checks = 20; | ||
65 | int generate = 0; | ||
66 | int bits = 0; | ||
67 | int safe = 0; | ||
68 | BIGNUM *bn = NULL; | 116 | BIGNUM *bn = NULL; |
69 | const char *errstr = NULL; | 117 | char *prime = NULL; |
70 | BIO *bio_out; | 118 | BIO *bio_out; |
119 | char *s; | ||
71 | 120 | ||
72 | --argc; | 121 | memset(&prime_config, 0, sizeof(prime_config)); |
73 | ++argv; | 122 | |
74 | while (argc >= 1 && **argv == '-') { | 123 | /* Default iterations for Miller-Rabin probabilistic primality test. */ |
75 | if (!strcmp(*argv, "-hex")) | 124 | prime_config.checks = 20; |
76 | hex = 1; | 125 | |
77 | else if (!strcmp(*argv, "-generate")) | 126 | if (options_parse(argc, argv, prime_options, &prime) != 0) { |
78 | generate = 1; | 127 | prime_usage(); |
79 | else if (!strcmp(*argv, "-bits")) { | 128 | return (1); |
80 | if (--argc < 1) | ||
81 | goto bad; | ||
82 | else | ||
83 | bits = strtonum(*(++argv), 0, INT_MAX, &errstr); | ||
84 | if (errstr) | ||
85 | goto bad; | ||
86 | } else if (!strcmp(*argv, "-safe")) | ||
87 | safe = 1; | ||
88 | else if (!strcmp(*argv, "-checks")) { | ||
89 | if (--argc < 1) | ||
90 | goto bad; | ||
91 | else | ||
92 | checks = strtonum(*(++argv), 0, INT_MAX, &errstr); | ||
93 | if (errstr) | ||
94 | goto bad; | ||
95 | } else { | ||
96 | BIO_printf(bio_err, "Unknown option '%s'\n", *argv); | ||
97 | goto bad; | ||
98 | } | ||
99 | --argc; | ||
100 | ++argv; | ||
101 | } | 129 | } |
102 | 130 | ||
103 | if (argv[0] == NULL && !generate) { | 131 | if (prime == NULL && prime_config.generate == 0) { |
104 | BIO_printf(bio_err, "No prime specified\n"); | 132 | BIO_printf(bio_err, "No prime specified.\n"); |
105 | goto bad; | 133 | prime_usage(); |
134 | return (1); | ||
106 | } | 135 | } |
136 | |||
107 | if ((bio_out = BIO_new(BIO_s_file())) != NULL) { | 137 | if ((bio_out = BIO_new(BIO_s_file())) != NULL) { |
108 | BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); | 138 | BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); |
109 | } | 139 | } |
110 | if (generate) { | ||
111 | char *s; | ||
112 | 140 | ||
113 | if (!bits) { | 141 | if (prime_config.generate != 0) { |
114 | BIO_printf(bio_err, "Specifiy the number of bits.\n"); | 142 | if (prime_config.bits == 0) { |
143 | BIO_printf(bio_err, "Specify the number of bits.\n"); | ||
115 | return 1; | 144 | return 1; |
116 | } | 145 | } |
117 | bn = BN_new(); | 146 | bn = BN_new(); /* XXX - unchecked malloc. */ |
118 | BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL); | 147 | BN_generate_prime_ex(bn, prime_config.bits, prime_config.safe, |
119 | s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn); | 148 | NULL, NULL, NULL); |
149 | s = prime_config.hex ? BN_bn2hex(bn) : BN_bn2dec(bn); | ||
120 | BIO_printf(bio_out, "%s\n", s); | 150 | BIO_printf(bio_out, "%s\n", s); |
121 | free(s); | 151 | free(s); |
122 | } else { | 152 | } else { |
123 | if (hex) | 153 | if (prime_config.hex) |
124 | BN_hex2bn(&bn, argv[0]); | 154 | BN_hex2bn(&bn, prime); |
125 | else | 155 | else |
126 | BN_dec2bn(&bn, argv[0]); | 156 | BN_dec2bn(&bn, prime); |
127 | 157 | ||
128 | BN_print(bio_out, bn); | 158 | BN_print(bio_out, bn); |
129 | BIO_printf(bio_out, " is %sprime\n", | 159 | BIO_printf(bio_out, " is %sprime\n", |
130 | BN_is_prime_ex(bn, checks, NULL, NULL) ? "" : "not "); | 160 | BN_is_prime_ex(bn, prime_config.checks, |
161 | NULL, NULL) ? "" : "not "); | ||
131 | } | 162 | } |
132 | 163 | ||
133 | BN_free(bn); | 164 | BN_free(bn); |
134 | BIO_free_all(bio_out); | 165 | BIO_free_all(bio_out); |
135 | 166 | ||
136 | return 0; | 167 | return 0; |
137 | |||
138 | bad: | ||
139 | if (errstr) | ||
140 | BIO_printf(bio_err, "invalid argument %s: %s\n", *argv, errstr); | ||
141 | else { | ||
142 | BIO_printf(bio_err, "options are\n"); | ||
143 | BIO_printf(bio_err, "%-14s hex\n", "-hex"); | ||
144 | BIO_printf(bio_err, "%-14s number of checks\n", "-checks <n>"); | ||
145 | } | ||
146 | return 1; | ||
147 | } | 168 | } |