| 1 |
/* |
| 2 |
* Copyright 2004-2025 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 |
/* Consistent with RSA modulus size limit and the size of plausible individual primes */ |
| 17 |
#define BUFSIZE 4098 |
| 18 |
|
| 19 |
typedef enum OPTION_choice { |
| 20 |
OPT_COMMON, |
| 21 |
OPT_HEX, |
| 22 |
OPT_GENERATE, |
| 23 |
OPT_BITS, |
| 24 |
OPT_SAFE, |
| 25 |
OPT_CHECKS, |
| 26 |
OPT_PROV_ENUM, |
| 27 |
OPT_IN_FILE |
| 28 |
} OPTION_CHOICE; |
| 29 |
|
| 30 |
static int check_num(const char *s, const int is_hex) |
| 31 |
{ |
| 32 |
int i; |
| 33 |
/* |
| 34 |
* It would make sense to use ossl_isxdigit and ossl_isdigit here, |
| 35 |
* but ossl_ctype_check is a local symbol in libcrypto.so. |
| 36 |
*/ |
| 37 |
if (is_hex) { |
| 38 |
for (i = 0; ('0' <= s[i] && s[i] <= '9') |
| 39 |
|| ('A' <= s[i] && s[i] <= 'F') |
| 40 |
|| ('a' <= s[i] && s[i] <= 'f'); |
| 41 |
i++) |
| 42 |
; |
| 43 |
} else { |
| 44 |
for (i = 0; '0' <= s[i] && s[i] <= '9'; i++) |
| 45 |
; |
| 46 |
} |
| 47 |
return s[i] == 0; |
| 48 |
} |
| 49 |
|
| 50 |
static void process_num(const char *s, const int is_hex) |
| 51 |
{ |
| 52 |
int r; |
| 53 |
BIGNUM *bn = NULL; |
| 54 |
|
| 55 |
r = check_num(s, is_hex); |
| 56 |
|
| 57 |
if (r) |
| 58 |
r = is_hex ? BN_hex2bn(&bn, s) : BN_dec2bn(&bn, s); |
| 59 |
|
| 60 |
if (!r) { |
| 61 |
BIO_printf(bio_err, "Failed to process value (%s)\n", s); |
| 62 |
BN_free(bn); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
BN_print(bio_out, bn); |
| 67 |
r = BN_check_prime(bn, NULL, NULL); |
| 68 |
BN_free(bn); |
| 69 |
if (r < 0) { |
| 70 |
BIO_printf(bio_err, "Error checking prime\n"); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
BIO_printf(bio_out, " (%s) %s prime\n", s, r == 1 ? "is" : "is not"); |
| 75 |
} |
| 76 |
|
| 77 |
const OPTIONS prime_options[] = { |
| 78 |
{ OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n" }, |
| 79 |
|
| 80 |
OPT_SECTION("General"), |
| 81 |
{ "help", OPT_HELP, '-', "Display this summary" }, |
| 82 |
{ "bits", OPT_BITS, 'p', "Size of number in bits" }, |
| 83 |
{ "checks", OPT_CHECKS, 'p', "Number of checks" }, |
| 84 |
{ "hex", OPT_HEX, '-', |
| 85 |
"Enables hex format for output from prime generation or input to primality checking" }, |
| 86 |
{ "in", OPT_IN_FILE, '-', "Provide file names containing numbers for primality checking" }, |
| 87 |
|
| 88 |
OPT_SECTION("Output"), |
| 89 |
{ "generate", OPT_GENERATE, '-', "Generate a prime" }, |
| 90 |
{ "safe", OPT_SAFE, '-', |
| 91 |
"When used with -generate, generate a safe prime" }, |
| 92 |
|
| 93 |
OPT_PROV_OPTIONS, |
| 94 |
|
| 95 |
OPT_PARAMETERS(), |
| 96 |
{ "number", 0, 0, "Number(s) to check for primality if not generating" }, |
| 97 |
{ NULL } |
| 98 |
}; |
| 99 |
|
| 100 |
int prime_main(int argc, char **argv) |
| 101 |
{ |
| 102 |
BIGNUM *bn = NULL; |
| 103 |
int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1, in_file = 0; |
| 104 |
char *prog; |
| 105 |
OPTION_CHOICE o; |
| 106 |
char file_read_buf[BUFSIZE] = { 0 }; |
| 107 |
BIO *in = NULL; |
| 108 |
|
| 109 |
prog = opt_init(argc, argv, prime_options); |
| 110 |
while ((o = opt_next()) != OPT_EOF) { |
| 111 |
switch (o) { |
| 112 |
case OPT_EOF: |
| 113 |
case OPT_ERR: |
| 114 |
opthelp: |
| 115 |
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 116 |
goto end; |
| 117 |
case OPT_HELP: |
| 118 |
opt_help(prime_options); |
| 119 |
ret = 0; |
| 120 |
goto end; |
| 121 |
case OPT_HEX: |
| 122 |
hex = 1; |
| 123 |
break; |
| 124 |
case OPT_GENERATE: |
| 125 |
generate = 1; |
| 126 |
break; |
| 127 |
case OPT_BITS: |
| 128 |
bits = atoi(opt_arg()); |
| 129 |
break; |
| 130 |
case OPT_SAFE: |
| 131 |
safe = 1; |
| 132 |
break; |
| 133 |
case OPT_CHECKS: |
| 134 |
/* ignore parameter and argument */ |
| 135 |
opt_arg(); |
| 136 |
break; |
| 137 |
case OPT_PROV_CASES: |
| 138 |
if (!opt_provider(o)) |
| 139 |
goto end; |
| 140 |
break; |
| 141 |
case OPT_IN_FILE: |
| 142 |
in_file = 1; |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/* Optional arguments are numbers to check. */ |
| 148 |
if (generate && !opt_check_rest_arg(NULL)) |
| 149 |
goto opthelp; |
| 150 |
argc = opt_num_rest(); |
| 151 |
argv = opt_rest(); |
| 152 |
if (!generate && argc == 0) { |
| 153 |
BIO_printf(bio_err, "Missing number (s) to check\n"); |
| 154 |
goto opthelp; |
| 155 |
} |
| 156 |
|
| 157 |
if (generate) { |
| 158 |
char *s; |
| 159 |
|
| 160 |
if (!bits) { |
| 161 |
BIO_printf(bio_err, "Specify the number of bits.\n"); |
| 162 |
goto end; |
| 163 |
} |
| 164 |
bn = BN_new(); |
| 165 |
if (bn == NULL) { |
| 166 |
BIO_printf(bio_err, "Out of memory.\n"); |
| 167 |
goto end; |
| 168 |
} |
| 169 |
if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) { |
| 170 |
BIO_printf(bio_err, "Failed to generate prime.\n"); |
| 171 |
goto end; |
| 172 |
} |
| 173 |
s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn); |
| 174 |
if (s == NULL) { |
| 175 |
BIO_printf(bio_err, "Out of memory.\n"); |
| 176 |
goto end; |
| 177 |
} |
| 178 |
BIO_printf(bio_out, "%s\n", s); |
| 179 |
OPENSSL_free(s); |
| 180 |
} else { |
| 181 |
for (; *argv; argv++) { |
| 182 |
int bytes_read = 0; |
| 183 |
|
| 184 |
if (!in_file) { |
| 185 |
process_num(argv[0], hex); |
| 186 |
} else { |
| 187 |
in = bio_open_default_quiet(argv[0], 'r', 0); |
| 188 |
if (in == NULL) { |
| 189 |
BIO_printf(bio_err, "Error opening file %s\n", argv[0]); |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
while ((bytes_read = BIO_get_line(in, file_read_buf, BUFSIZE)) > 0) { |
| 194 |
size_t valid_digits_length; |
| 195 |
|
| 196 |
/* Number is too long. Discard remainder of the line */ |
| 197 |
if (bytes_read == BUFSIZE - 1 && file_read_buf[BUFSIZE - 2] != '\n') { |
| 198 |
BIO_printf(bio_err, "Value in %s is over the maximum size (%d digits)\n", |
| 199 |
argv[0], BUFSIZE - 2); |
| 200 |
while (BIO_get_line(in, file_read_buf, BUFSIZE) == BUFSIZE - 1) |
| 201 |
; |
| 202 |
continue; |
| 203 |
} |
| 204 |
|
| 205 |
valid_digits_length = strspn(file_read_buf, "1234567890abcdefABCDEF"); |
| 206 |
file_read_buf[valid_digits_length] = '\0'; |
| 207 |
|
| 208 |
process_num(file_read_buf, hex); |
| 209 |
} |
| 210 |
|
| 211 |
if (bytes_read < 0) |
| 212 |
BIO_printf(bio_err, "Read error in %s\n", argv[0]); |
| 213 |
|
| 214 |
BIO_free(in); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
ret = 0; |
| 220 |
end: |
| 221 |
BN_free(bn); |
| 222 |
return ret; |
| 223 |
} |