diff options
Diffstat (limited to 'src/usr.bin/openssl/prime.c')
-rw-r--r-- | src/usr.bin/openssl/prime.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/usr.bin/openssl/prime.c b/src/usr.bin/openssl/prime.c new file mode 100644 index 0000000000..e7fb3257d8 --- /dev/null +++ b/src/usr.bin/openssl/prime.c | |||
@@ -0,0 +1,147 @@ | |||
1 | /* $OpenBSD: prime.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */ | ||
2 | /* ==================================================================== | ||
3 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in | ||
14 | * the documentation and/or other materials provided with the | ||
15 | * distribution. | ||
16 | * | ||
17 | * 3. All advertising materials mentioning features or use of this | ||
18 | * software must display the following acknowledgment: | ||
19 | * "This product includes software developed by the OpenSSL Project | ||
20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
21 | * | ||
22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
23 | * endorse or promote products derived from this software without | ||
24 | * prior written permission. For written permission, please contact | ||
25 | * openssl-core@openssl.org. | ||
26 | * | ||
27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
28 | * nor may "OpenSSL" appear in their names without prior written | ||
29 | * permission of the OpenSSL Project. | ||
30 | * | ||
31 | * 6. Redistributions of any form whatsoever must retain the following | ||
32 | * acknowledgment: | ||
33 | * "This product includes software developed by the OpenSSL Project | ||
34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
35 | * | ||
36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
48 | * | ||
49 | */ | ||
50 | |||
51 | #include <string.h> | ||
52 | #include <limits.h> | ||
53 | |||
54 | #include "apps.h" | ||
55 | |||
56 | #include <openssl/bn.h> | ||
57 | |||
58 | int prime_main(int, char **); | ||
59 | |||
60 | int | ||
61 | prime_main(int argc, char **argv) | ||
62 | { | ||
63 | int hex = 0; | ||
64 | int checks = 20; | ||
65 | int generate = 0; | ||
66 | int bits = 0; | ||
67 | int safe = 0; | ||
68 | BIGNUM *bn = NULL; | ||
69 | const char *errstr = NULL; | ||
70 | BIO *bio_out; | ||
71 | |||
72 | --argc; | ||
73 | ++argv; | ||
74 | while (argc >= 1 && **argv == '-') { | ||
75 | if (!strcmp(*argv, "-hex")) | ||
76 | hex = 1; | ||
77 | else if (!strcmp(*argv, "-generate")) | ||
78 | generate = 1; | ||
79 | else if (!strcmp(*argv, "-bits")) { | ||
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 | } | ||
102 | |||
103 | if (argv[0] == NULL && !generate) { | ||
104 | BIO_printf(bio_err, "No prime specified\n"); | ||
105 | goto bad; | ||
106 | } | ||
107 | if ((bio_out = BIO_new(BIO_s_file())) != NULL) { | ||
108 | BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); | ||
109 | } | ||
110 | if (generate) { | ||
111 | char *s; | ||
112 | |||
113 | if (!bits) { | ||
114 | BIO_printf(bio_err, "Specifiy the number of bits.\n"); | ||
115 | return 1; | ||
116 | } | ||
117 | bn = BN_new(); | ||
118 | BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL); | ||
119 | s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn); | ||
120 | BIO_printf(bio_out, "%s\n", s); | ||
121 | free(s); | ||
122 | } else { | ||
123 | if (hex) | ||
124 | BN_hex2bn(&bn, argv[0]); | ||
125 | else | ||
126 | BN_dec2bn(&bn, argv[0]); | ||
127 | |||
128 | BN_print(bio_out, bn); | ||
129 | BIO_printf(bio_out, " is %sprime\n", | ||
130 | BN_is_prime_ex(bn, checks, NULL, NULL) ? "" : "not "); | ||
131 | } | ||
132 | |||
133 | BN_free(bn); | ||
134 | BIO_free_all(bio_out); | ||
135 | |||
136 | 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 | } | ||