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