]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/prime.c
Re-introduce legacy EVP_PKEY types for provided keys
[thirdparty/openssl.git] / apps / prime.c
CommitLineData
846e33c7 1/*
33388b44 2 * Copyright 2004-2020 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
RS
16typedef enum OPTION_choice {
17 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
6bd4e3f2
P
18 OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS,
19 OPT_PROV_ENUM
7e1b7485
RS
20} OPTION_CHOICE;
21
44c83ebd 22const OPTIONS prime_options[] = {
7e1b7485 23 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
5388f986
RS
24
25 OPT_SECTION("General"),
7e1b7485 26 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
27 {"bits", OPT_BITS, 'p', "Size of number in bits"},
28 {"checks", OPT_CHECKS, 'p', "Number of checks"},
29
30 OPT_SECTION("Output"),
7e1b7485
RS
31 {"hex", OPT_HEX, '-', "Hex output"},
32 {"generate", OPT_GENERATE, '-', "Generate a prime"},
7e1b7485
RS
33 {"safe", OPT_SAFE, '-',
34 "When used with -generate, generate a safe prime"},
92de469f 35
6bd4e3f2
P
36 OPT_PROV_OPTIONS,
37
92de469f
RS
38 OPT_PARAMETERS(),
39 {"number", 0, 0, "Number(s) to check for primality if not generating"},
7e1b7485
RS
40 {NULL}
41};
42
43int prime_main(int argc, char **argv)
0f113f3e 44{
0f113f3e 45 BIGNUM *bn = NULL;
42619397 46 int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1;
7e1b7485
RS
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:
c27363f5 55opthelp:
7e1b7485
RS
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:
0f113f3e 63 hex = 1;
7e1b7485
RS
64 break;
65 case OPT_GENERATE:
0f113f3e 66 generate = 1;
7e1b7485
RS
67 break;
68 case OPT_BITS:
69 bits = atoi(opt_arg());
70 break;
71 case OPT_SAFE:
0f113f3e 72 safe = 1;
7e1b7485
RS
73 break;
74 case OPT_CHECKS:
42619397
KR
75 /* ignore parameter and argument */
76 opt_arg();
7e1b7485 77 break;
6bd4e3f2
P
78 case OPT_PROV_CASES:
79 if (!opt_provider(o))
80 goto end;
81 break;
0f113f3e 82 }
0f113f3e 83 }
7e1b7485
RS
84 argc = opt_num_rest();
85 argv = opt_rest();
0f113f3e 86
c27363f5
RS
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) {
7e1b7485 93 BIO_printf(bio_err, "%s: No prime specified\n", prog);
c27363f5 94 goto opthelp;
0f113f3e
MC
95 }
96
97 if (generate) {
98 char *s;
99
100 if (!bits) {
5573ee36 101 BIO_printf(bio_err, "Specify the number of bits.\n");
7e1b7485 102 goto end;
0f113f3e
MC
103 }
104 bn = BN_new();
9b5164ce
DSH
105 if (bn == NULL) {
106 BIO_printf(bio_err, "Out of memory.\n");
107 goto end;
108 }
e69f2a22
MC
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 }
0f113f3e 113 s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
9b5164ce
DSH
114 if (s == NULL) {
115 BIO_printf(bio_err, "Out of memory.\n");
116 goto end;
117 }
0f113f3e
MC
118 BIO_printf(bio_out, "%s\n", s);
119 OPENSSL_free(s);
120 } else {
7e1b7485 121 for ( ; *argv; argv++) {
e69f2a22
MC
122 int r;
123
7e1b7485 124 if (hex)
e69f2a22 125 r = BN_hex2bn(&bn, argv[0]);
7e1b7485 126 else
e69f2a22
MC
127 r = BN_dec2bn(&bn, argv[0]);
128
28b86f31 129 if (!r) {
e69f2a22
MC
130 BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
131 goto end;
132 }
0f113f3e 133
7e1b7485
RS
134 BN_print(bio_out, bn);
135 BIO_printf(bio_out, " (%s) %s prime\n",
136 argv[0],
42619397 137 BN_check_prime(bn, NULL, NULL)
7e1b7485
RS
138 ? "is" : "is not");
139 }
0f113f3e 140 }
b08868c4 141
e69f2a22 142 ret = 0;
7e1b7485 143 end:
5bf7c772 144 BN_free(bn);
7e1b7485 145 return ret;
0f113f3e 146}