blob: 3c72330c0a62ac9e6973885c02e88663546a97df [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (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
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +00008 */
9
10#include <string.h>
11
12#include "apps.h"
13#include <openssl/bn.h>
14
Rich Salz7e1b7482015-04-24 15:26:15 -040015typedef enum OPTION_choice {
16 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
17 OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS
18} OPTION_CHOICE;
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +000019
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010020const OPTIONS prime_options[] = {
Rich Salz7e1b7482015-04-24 15:26:15 -040021 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
22 {OPT_HELP_STR, 1, '-',
Michal Bozone61434b2015-10-29 16:48:00 +010023 " number Number to check for primality\n"},
Rich Salz7e1b7482015-04-24 15:26:15 -040024 {"help", OPT_HELP, '-', "Display this summary"},
25 {"hex", OPT_HEX, '-', "Hex output"},
26 {"generate", OPT_GENERATE, '-', "Generate a prime"},
27 {"bits", OPT_BITS, 'p', "Size of number in bits"},
28 {"safe", OPT_SAFE, '-',
29 "When used with -generate, generate a safe prime"},
30 {"checks", OPT_CHECKS, 'p', "Number of checks"},
31 {NULL}
32};
Ben Laurie0ff469d2005-05-01 13:49:56 +000033
Rich Salz7e1b7482015-04-24 15:26:15 -040034int prime_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +000035{
Matt Caswell0f113f32015-01-22 03:40:55 +000036 BIGNUM *bn = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -040037 int hex = 0, checks = 20, generate = 0, bits = 0, safe = 0, ret = 1;
38 char *prog;
39 OPTION_CHOICE o;
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +000040
Rich Salz7e1b7482015-04-24 15:26:15 -040041 prog = opt_init(argc, argv, prime_options);
42 while ((o = opt_next()) != OPT_EOF) {
43 switch (o) {
44 case OPT_EOF:
45 case OPT_ERR:
46 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
47 goto end;
48 case OPT_HELP:
49 opt_help(prime_options);
50 ret = 0;
51 goto end;
52 case OPT_HEX:
Matt Caswell0f113f32015-01-22 03:40:55 +000053 hex = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -040054 break;
55 case OPT_GENERATE:
Matt Caswell0f113f32015-01-22 03:40:55 +000056 generate = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -040057 break;
58 case OPT_BITS:
59 bits = atoi(opt_arg());
60 break;
61 case OPT_SAFE:
Matt Caswell0f113f32015-01-22 03:40:55 +000062 safe = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -040063 break;
64 case OPT_CHECKS:
65 checks = atoi(opt_arg());
66 break;
Matt Caswell0f113f32015-01-22 03:40:55 +000067 }
Matt Caswell0f113f32015-01-22 03:40:55 +000068 }
Rich Salz7e1b7482015-04-24 15:26:15 -040069 argc = opt_num_rest();
70 argv = opt_rest();
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +000071
Rich Salz7e1b7482015-04-24 15:26:15 -040072 if (argc == 0 && !generate) {
73 BIO_printf(bio_err, "%s: No prime specified\n", prog);
74 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +000075 }
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +000076
Matt Caswell0f113f32015-01-22 03:40:55 +000077 if (generate) {
78 char *s;
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +000079
Matt Caswell0f113f32015-01-22 03:40:55 +000080 if (!bits) {
Michal Bozon5573ee32015-09-28 12:59:27 -040081 BIO_printf(bio_err, "Specify the number of bits.\n");
Rich Salz7e1b7482015-04-24 15:26:15 -040082 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +000083 }
84 bn = BN_new();
Dr. Stephen Henson9b5164c2016-05-10 20:49:50 +010085 if (bn == NULL) {
86 BIO_printf(bio_err, "Out of memory.\n");
87 goto end;
88 }
Matt Caswelle69f2a22016-04-15 14:31:03 +010089 if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
90 BIO_printf(bio_err, "Failed to generate prime.\n");
91 goto end;
92 }
Matt Caswell0f113f32015-01-22 03:40:55 +000093 s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
Dr. Stephen Henson9b5164c2016-05-10 20:49:50 +010094 if (s == NULL) {
95 BIO_printf(bio_err, "Out of memory.\n");
96 goto end;
97 }
Matt Caswell0f113f32015-01-22 03:40:55 +000098 BIO_printf(bio_out, "%s\n", s);
99 OPENSSL_free(s);
100 } else {
Rich Salz7e1b7482015-04-24 15:26:15 -0400101 for ( ; *argv; argv++) {
Matt Caswelle69f2a22016-04-15 14:31:03 +0100102 int r;
103
Rich Salz7e1b7482015-04-24 15:26:15 -0400104 if (hex)
Matt Caswelle69f2a22016-04-15 14:31:03 +0100105 r = BN_hex2bn(&bn, argv[0]);
Rich Salz7e1b7482015-04-24 15:26:15 -0400106 else
Matt Caswelle69f2a22016-04-15 14:31:03 +0100107 r = BN_dec2bn(&bn, argv[0]);
108
FdaSilvaYY28b86f32016-08-24 00:17:31 +0200109 if (!r) {
Matt Caswelle69f2a22016-04-15 14:31:03 +0100110 BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
111 goto end;
112 }
Ben Laurie2c2e46d2005-08-23 13:48:17 +0000113
Rich Salz7e1b7482015-04-24 15:26:15 -0400114 BN_print(bio_out, bn);
115 BIO_printf(bio_out, " (%s) %s prime\n",
116 argv[0],
117 BN_is_prime_ex(bn, checks, NULL, NULL)
118 ? "is" : "is not");
119 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000120 }
Dr. Stephen Hensonb08868c2005-04-26 23:02:52 +0000121
Matt Caswelle69f2a22016-04-15 14:31:03 +0100122 ret = 0;
Rich Salz7e1b7482015-04-24 15:26:15 -0400123 end:
Matt Caswell5bf7c772016-04-26 18:29:49 +0100124 BN_free(bn);
Rich Salz7e1b7482015-04-24 15:26:15 -0400125 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000126}