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