-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
201 lines (173 loc) · 5.18 KB
/
main.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
201
/*
* Copyright (c) 2020 Joseph Fierro <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "hoplite.h"
#include "defines.h"
#include "compat.h"
#include "readpassphrase.h"
/* Global variable to control readpassphrase flags.
* It is only possible to modify this once, through a
* command line flag.
*/
int global_rpp_flags = RPP_REQUIRE_TTY;
static void usage(void);
int
main(int argc, char **argv)
{
int ch, flag = 0;
long long rounds = ARGON2_T;
long long mem = ARGON2_MEM * 1024;
long long threads = ARGON2_P;
FILE *infile = NULL;
FILE *pkey = NULL;
FILE *skey = NULL;
char filename[FILENAME_SIZE];
char id[IDSIZE];
const char *errstr;
#ifdef __OpenBSD__
if ((pledge("stdio rpath wpath cpath tty", NULL)) == -1)
err(1, "pledge");
#endif /* __OpenBSD__ */
memset(id, 0, sizeof(id));
if (argc < 2)
usage();
while ((ch = getopt(argc, argv, "VvscedSg:k:s:p:f:r:m:t:")) != -1) {
switch (ch) {
case 'g':
if (portable_strlcpy(id, optarg, sizeof(id)) >= sizeof(id))
errx(1, "Name too long");
flag = 1;
break;
case 'e':
flag = 2;
break;
case 'c':
flag = 3;
break;
case 'd':
flag = 4;
break;
case 's':
flag = 5;
break;
case 'v':
flag = 6;
break;
case 'V':
printf("%s\n", VERSION);
return (0);
break;
case 'S':
global_rpp_flags = RPP_STDIN;
break;
case 'k':
if ((skey = fopen(optarg, "r")) == NULL)
err(1, "couldn't open secret key");
break;
case 'p':
if ((pkey = fopen(optarg, "r")) == NULL)
err(1, "couldn't open public key");
break;
case 'f':
if ((infile = fopen(optarg, "r")) == NULL)
err(1, "couldn't open file");
if (portable_strlcpy(filename, optarg, FILENAME_SIZE) >= FILENAME_SIZE)
errx(1, "filename too long");
break;
case 'r':
rounds = strtonum(optarg, MIN_ROUNDS, MAX_ROUNDS, &errstr);
if (errstr != NULL)
errx(1, "error getting rounds: %s", errstr);
break;
case 'm':
mem = strtonum(optarg, MIN_MEM, MAX_MEM, &errstr);
if (errstr != NULL)
errx(1, "error setting KDF memory: %s", errstr);
mem = (mem * 1024);
break;
case 't':
threads = strtonum(optarg, MIN_THREADS, MAX_THREADS, &errstr);
if (errstr != NULL)
errx(1, "error setting KDF threads: %s", errstr);
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
usage();
if (flag == 0)
usage();
if (flag == 1) {
/* Generating new key pairs */
hoplite_newkey(id, rounds, mem, threads);
} else if (flag == 2) {
/* Asymmetric encryption */
if (infile == NULL)
errx(1, "must provide a file for encryption");
if (pkey == NULL)
errx(1, "must provide recipient's public key");
if (skey == NULL) {
if ((skey = fopen(getenv("HOPLITE_SECKEY"), "r")) == NULL)
errx(1, "no secret key found");
}
hoplite_encrypt(infile, pkey, skey, filename, 1, 1, 1, 1);
} else if (flag == 3) {
/* Symmetric encryption */
if (infile == NULL)
errx(1, "must provide a file for encryption");
hoplite_encrypt(infile, NULL, NULL, filename, 2, rounds, mem, threads);
} else if (flag == 4) {
/* Decryption */
if (infile == NULL)
errx(1, "must provide a file for decryption");
hoplite_decrypt(infile, pkey, skey, filename);
} else if (flag == 5) {
/* Signing */
if (infile == NULL)
errx(1, "Must provide a file for signing");
if (skey == NULL) {
if ((skey = fopen(getenv("HOPLITE_SIGNING_SECKEY"), "r")) == NULL)
errx(1, "no secret key found");
}
hoplite_sign(infile, skey, filename);
} else if (flag == 6) {
/* Verifying signed file */
if (infile == NULL)
errx(1, "must provide a file for sig verification");
if (pkey == NULL)
errx(1, "must provide signer's public key");
hoplite_verify(infile, pkey, filename);
}
return (0);
}
void
usage(void)
{
errx(1, "\nusage:\n\thoplite -c [-S] [-r rounds] [-m memory] [-t threads] -f file\
\n\thoplite -e [-S] -p recip-pubkey -k sender-seckey -f file \
\n\thoplite -d [-S] [-p sender-pubkey -k recip-seckey] -f file \
\n\thoplite -s [-S] -k signer-signing-seckey -f file\
\n\thoplite -v -p signer-signing-pubkey -f file\
\n\thoplite -g key-id [-S] [-r rounds] [-m memory] [-t threads]");
}