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