-
Notifications
You must be signed in to change notification settings - Fork 1
/
keygen.c
200 lines (170 loc) · 5.41 KB
/
keygen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <gmp.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "randstate.h"
#include "ss.h"
#include "argparser.h"
#define KEYGEN_OPTIONS "b:i:n:d:s:vh"
int keygen_argparser(int argc, char **argv, uint32_t *nbits, uint32_t *iters, FILE **pbfile,
FILE **pvfile, uint64_t *seed, bool *verbose);
uint32_t get_number_from_command_line_argument(char *);
void generate_keys(
uint32_t nbits, uint32_t iters, FILE *pbfile, FILE *pvfile, uint64_t seed, bool verbose);
void print_help(void);
void print_verbose(const char *username, const mpz_t p, const mpz_t q, const mpz_t n,
const mpz_t pq, const mpz_t d);
void print_verbose_mpz_var(const mpz_t var, const char *name);
/*
Main function that initializes all variables, calls parser, verifies valid inputs, and generates keys.
*/
int main(int argc, char **argv) {
uint32_t nbits = 256;
uint32_t iters = 50;
FILE *pbfile = NULL;
FILE *pvfile = NULL;
uint64_t seed = (uint64_t) time(NULL);
bool verbose = false;
int response = keygen_argparser(argc, argv, &nbits, &iters, &pbfile, &pvfile, &seed, &verbose);
//Error
if (response != 0) {
if (pbfile != NULL) {
fclose(pbfile);
}
if (pvfile != NULL) {
fclose(pvfile);
}
return -1;
}
if (pbfile == NULL) {
bool is_open = open_file(&pbfile, "ss.pub", "w+");
if (!is_open) {
return -2; //Fail
}
}
if (pvfile == NULL) {
bool is_open = open_file(&pvfile, "ss.priv", "w+");
if (!is_open) {
return -3;
}
}
fchmod(fileno(pvfile), S_IRUSR + S_IWUSR); //Set file permissions 600 for private file
generate_keys(nbits, iters, pbfile, pvfile, seed, verbose);
return 0;
}
/*
Parses and sets keygen command line arguments
*/
int keygen_argparser(int argc, char **argv, uint32_t *nbits, uint32_t *iters, FILE **pbfile,
FILE **pvfile, uint64_t *seed, bool *verbose) {
int opt = 0;
bool is_open = false;
while ((opt = getopt(argc, argv, KEYGEN_OPTIONS)) != -1) {
switch (opt) {
case 'b':
*nbits = get_number_from_command_line_argument(optarg);
if (*nbits < 5) {
printf("Please enter the number of bits for the public key (> 4)\n");
return 4;
}
break;
case 'i': *iters = get_number_from_command_line_argument(optarg); break;
case 'n':
is_open = open_file(pbfile, optarg, "w+");
if (!is_open) {
return 2;
}
break;
case 'd':
is_open = open_file(pvfile, optarg, "w+");
if (!is_open) {
return 3;
}
break;
case 's': *seed = (uint64_t) strtoul(optarg, NULL, 10); break;
case 'v': *verbose = true; break;
case 'h': print_help(); return 1;
default: print_help(); return 1;
}
}
return 0;
}
/*
Returns number from inputted string command line argument.
*/
uint32_t get_number_from_command_line_argument(char *opt_argument) {
return (uint32_t) strtoul(opt_argument, NULL, 10);
}
/*
Generate keys function:
- Initializes random states.
- Makes public and private keys
- Gets username
- Writes public key to pbfile
- Writes private key to pvfile
*/
void generate_keys(
uint32_t nbits, uint32_t iters, FILE *pbfile, FILE *pvfile, uint64_t seed, bool verbose) {
randstate_init(seed);
srandom(seed);
mpz_t p, q, n, pq, d;
mpz_inits(p, q, n, pq, d, NULL);
ss_make_pub(p, q, n, nbits, iters);
ss_make_priv(d, pq, p, q);
char *username = getenv("USER");
ss_write_pub(n, username, pbfile);
fclose(pbfile);
ss_write_priv(pq, d, pvfile);
fclose(pvfile);
if (verbose) {
print_verbose(username, p, q, n, pq, d);
}
mpz_clears(p, q, n, pq, d, NULL);
randstate_clear();
return;
}
/*
Prints verbose arguements to screen.
*/
void print_verbose(const char *username, const mpz_t p, const mpz_t q, const mpz_t n,
const mpz_t pq, const mpz_t d) {
printf("user = %s\n", username);
print_verbose_mpz_var(p, "p ");
print_verbose_mpz_var(q, "q ");
print_verbose_mpz_var(n, "n ");
print_verbose_mpz_var(pq, "pq ");
print_verbose_mpz_var(d, "d ");
return;
}
/*
Formats and prints mpz variable
*/
void print_verbose_mpz_var(const mpz_t var, const char *name) {
uint32_t bits = (uint32_t) mpz_sizeinbase(var, 2);
printf("%s(%u bits) = ", name, bits);
mpz_out_str(stdout, 10, var);
printf("\n");
return;
}
/*
Prints help message
*/
void print_help(void) {
printf("SYNOPSIS\n"
" Generates an SS public/private key pair.\n\n"
"USAGE\n"
" ./keygen [OPTIONS]\n\n"
"OPTIONS\n"
" -h Display program help and usage.\n"
" -v Display verbose program output.\n"
" -b bits Minimum bits needed for public key n (default: 256).\n"
" -i iterations Miller-Rabin iterations for testing primes (default: 50).\n"
" -n pbfile Public key file (default: ss.pub).\n"
" -d pvfile Private key file (default: ss.priv).\n"
" -s seed Random seed for testing.\n");
}