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