]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/prime.c
Update copyright year
[thirdparty/openssl.git] / apps / prime.c
CommitLineData
846e33c7 1/*
fecb3aae 2 * Copyright 2004-2022 The OpenSSL Project Authors. All Rights Reserved.
b08868c4 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
b08868c4
DSH
8 */
9
10#include <string.h>
11
12#include "apps.h"
dab2cd68 13#include "progs.h"
b08868c4
DSH
14#include <openssl/bn.h>
15
7e1b7485 16typedef enum OPTION_choice {
b0f96018 17 OPT_COMMON,
6bd4e3f2
P
18 OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS,
19 OPT_PROV_ENUM
7e1b7485
RS
20} OPTION_CHOICE;
21
3b74fdcf
DF
22static int check_num(const char *s, const int is_hex)
23{
24 int i;
25 /*
26 * It would make sense to use ossl_isxdigit and ossl_isdigit here,
27 * but ossl_ctype_check is a local symbol in libcrypto.so.
28 */
29 if (is_hex) {
30 for (i = 0; ('0' <= s[i] && s[i] <= '9')
31 || ('A' <= s[i] && s[i] <= 'F')
32 || ('a' <= s[i] && s[i] <= 'f'); i++);
33 } else {
34 for (i = 0; '0' <= s[i] && s[i] <= '9'; i++);
35 }
36 return s[i] == 0;
37}
38
44c83ebd 39const OPTIONS prime_options[] = {
7e1b7485 40 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
5388f986
RS
41
42 OPT_SECTION("General"),
7e1b7485 43 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
44 {"bits", OPT_BITS, 'p', "Size of number in bits"},
45 {"checks", OPT_CHECKS, 'p', "Number of checks"},
46
47 OPT_SECTION("Output"),
7e1b7485
RS
48 {"hex", OPT_HEX, '-', "Hex output"},
49 {"generate", OPT_GENERATE, '-', "Generate a prime"},
7e1b7485
RS
50 {"safe", OPT_SAFE, '-',
51 "When used with -generate, generate a safe prime"},
92de469f 52
6bd4e3f2
P
53 OPT_PROV_OPTIONS,
54
92de469f
RS
55 OPT_PARAMETERS(),
56 {"number", 0, 0, "Number(s) to check for primality if not generating"},
7e1b7485
RS
57 {NULL}
58};
59
60int prime_main(int argc, char **argv)
0f113f3e 61{
0f113f3e 62 BIGNUM *bn = NULL;
42619397 63 int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1;
7e1b7485
RS
64 char *prog;
65 OPTION_CHOICE o;
66
67 prog = opt_init(argc, argv, prime_options);
68 while ((o = opt_next()) != OPT_EOF) {
69 switch (o) {
70 case OPT_EOF:
71 case OPT_ERR:
c27363f5 72opthelp:
7e1b7485
RS
73 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74 goto end;
75 case OPT_HELP:
76 opt_help(prime_options);
77 ret = 0;
78 goto end;
79 case OPT_HEX:
0f113f3e 80 hex = 1;
7e1b7485
RS
81 break;
82 case OPT_GENERATE:
0f113f3e 83 generate = 1;
7e1b7485
RS
84 break;
85 case OPT_BITS:
86 bits = atoi(opt_arg());
87 break;
88 case OPT_SAFE:
0f113f3e 89 safe = 1;
7e1b7485
RS
90 break;
91 case OPT_CHECKS:
42619397
KR
92 /* ignore parameter and argument */
93 opt_arg();
7e1b7485 94 break;
6bd4e3f2
P
95 case OPT_PROV_CASES:
96 if (!opt_provider(o))
97 goto end;
98 break;
0f113f3e 99 }
0f113f3e 100 }
021410ea
RS
101
102 /* Optional arguments are numbers to check. */
d9f07357
DDO
103 if (generate && !opt_check_rest_arg(NULL))
104 goto opthelp;
7e1b7485
RS
105 argc = opt_num_rest();
106 argv = opt_rest();
d9f07357
DDO
107 if (!generate && argc == 0) {
108 BIO_printf(bio_err, "Missing number (s) to check\n");
c27363f5 109 goto opthelp;
0f113f3e
MC
110 }
111
112 if (generate) {
113 char *s;
114
115 if (!bits) {
5573ee36 116 BIO_printf(bio_err, "Specify the number of bits.\n");
7e1b7485 117 goto end;
0f113f3e
MC
118 }
119 bn = BN_new();
9b5164ce
DSH
120 if (bn == NULL) {
121 BIO_printf(bio_err, "Out of memory.\n");
122 goto end;
123 }
e69f2a22
MC
124 if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
125 BIO_printf(bio_err, "Failed to generate prime.\n");
126 goto end;
127 }
0f113f3e 128 s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
9b5164ce
DSH
129 if (s == NULL) {
130 BIO_printf(bio_err, "Out of memory.\n");
131 goto end;
132 }
0f113f3e
MC
133 BIO_printf(bio_out, "%s\n", s);
134 OPENSSL_free(s);
135 } else {
7e1b7485 136 for ( ; *argv; argv++) {
3b74fdcf 137 int r = check_num(argv[0], hex);
e69f2a22 138
3b74fdcf
DF
139 if (r)
140 r = hex ? BN_hex2bn(&bn, argv[0]) : BN_dec2bn(&bn, argv[0]);
e69f2a22 141
28b86f31 142 if (!r) {
e69f2a22
MC
143 BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
144 goto end;
145 }
0f113f3e 146
7e1b7485
RS
147 BN_print(bio_out, bn);
148 BIO_printf(bio_out, " (%s) %s prime\n",
149 argv[0],
42619397 150 BN_check_prime(bn, NULL, NULL)
7e1b7485
RS
151 ? "is" : "is not");
152 }
0f113f3e 153 }
b08868c4 154
e69f2a22 155 ret = 0;
7e1b7485 156 end:
5bf7c772 157 BN_free(bn);
7e1b7485 158 return ret;
0f113f3e 159}