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