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