-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenc.c
More file actions
452 lines (392 loc) · 13.3 KB
/
enc.c
File metadata and controls
452 lines (392 loc) · 13.3 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* THIS IS THE OLD VERSION I KEPT FOR LEGACY PURPOSES (it's useless) */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/random.h>
#include <sys/uio.h>
#include "monocypher.h"
#include "kem/api.h"
#include "sign/api.h"
#define USAGE "[-gedvf] [-k key-path] [-r recipient-path] " \
"[-i input-file] [-o output-file]"
/* global variables */
static unsigned char read_buf[65536];
static unsigned char enc_buf[65536];
static int force;
/* enc funcs */
int enc_keygen(void);
int enc_encrypt(char *keypath, char *recpath, char *filepath, char *outpath);
int enc_decrypt(char *keypath, char *secpath, char *filepath, char *outpath);
int enc_verify(char *keypath, char *sigpath, char *filepath);
/* helper funcs */
void hex(unsigned char in[], unsigned char out[], size_t len);
void unhex(unsigned char in[], unsigned char out[], size_t len);
ssize_t readfile(const char *path, unsigned char *out, size_t len);
enum {
NONE,
GEN,
ENC,
DEC,
VER,
};
static char *argv0;
int
main(int argc, char *argv[])
{
int ch, op;
char *keypath, *recpath, *filepath, *outpath;
argv0 = argv[0];
op = NONE;
force = 0;
keypath = recpath = filepath = outpath = NULL;
while ((ch = getopt(argc, argv, "gedvfk:r:i:o:")) != -1) {
switch (ch) {
case 'g':
op = GEN;
break;
case 'e':
op = ENC;
break;
case 'd':
op = DEC;
break;
case 'v':
op = VER;
break;
case 'f':
force = 1;
break;
case 'k':
keypath = optarg;
break;
case 'r':
recpath = optarg;
break;
case 'i':
filepath = optarg;
break;
case 'o':
outpath = optarg;
break;
default:
fprintf(stderr, "usage: %s "USAGE"\n", argv0);
return 1;
}
}
switch (op) {
case GEN: return enc_keygen();
case ENC: return enc_encrypt(keypath, recpath, filepath, outpath);
case DEC: return enc_decrypt(keypath, recpath, filepath, outpath);
case VER: return enc_verify(keypath, recpath, filepath);
default:
fprintf(stderr, "usage: %s "USAGE"\n", argv0);
return 1;
}
}
#define PUBKEYFILESIZE (SKIBIDI768_PUBLICKEYBYTES + SIGMA3_PUBLICKEYBYTES)
#define SECKEYFILESIZE (SKIBIDI768_SECRETKEYBYTES + SIGMA3_SECRETKEYBYTES)
void
readpubkeyfile(const char *path,
unsigned char kem[SKIBIDI768_PUBLICKEYBYTES],
unsigned char sign[SIGMA3_PUBLICKEYBYTES])
{
static unsigned char keys[PUBKEYFILESIZE];
readfile(path, keys, PUBKEYFILESIZE);
if (kem != NULL)
memcpy(kem, keys, SKIBIDI768_PUBLICKEYBYTES);
if (sign != NULL)
memcpy(sign, keys+SKIBIDI768_PUBLICKEYBYTES, SIGMA3_PUBLICKEYBYTES);
}
void
readseckeyfile(const char *path,
unsigned char kem[SKIBIDI768_SECRETKEYBYTES],
unsigned char sign[SIGMA3_SECRETKEYBYTES])
{
static unsigned char keys[SECKEYFILESIZE];
readfile(path, keys, SECKEYFILESIZE);
if (kem != NULL)
memcpy(kem, keys, SKIBIDI768_SECRETKEYBYTES);
if (sign != NULL)
memcpy(sign, keys+SKIBIDI768_SECRETKEYBYTES, SIGMA3_SECRETKEYBYTES);
crypto_wipe(keys, SECKEYFILESIZE);
}
int
enc_encrypt(char *keypath, char *recpath, char *filepath, char *outpath)
{
struct blake2b_ctx ctx;
static unsigned char hash[64];
static unsigned char sm[64 + SIGMA3_BYTES];
static unsigned char ss[32], shared[32];
static unsigned char signkey[SIGMA3_SECRETKEYBYTES];
static unsigned char reckey[SKIBIDI768_PUBLICKEYBYTES];
static unsigned char ct[SKIBIDI768_CIPHERTEXTBYTES];
static unsigned char nonce[24], mac[16];
static char sigpath[64];
uint64_t counter;
int infd, outfd, sigfd;
ssize_t len;
size_t smlen;
char *out;
if (filepath == NULL || strcmp(filepath, "-") == 0) {
infd = STDIN_FILENO;
} else {
infd = open(filepath, O_RDONLY);
if (infd == -1) err(1, "open(%s)", filepath);
}
out = NULL;
if (outpath == NULL && infd == STDIN_FILENO && (!isatty(STDOUT_FILENO) || force)) {
outfd = STDOUT_FILENO;
} else if (outpath == NULL) {
if (filepath == NULL && !force && isatty(STDOUT_FILENO)) {
fprintf(stderr, "must set output file "
"or pass -f to write to stdout\n");
fprintf(stderr, "usage: %s "USAGE"\n", argv0);
exit(1);
}
len = strlen(filepath)+5;
out = calloc(len, 1);
snprintf(out, len, "%s.out", filepath);
outfd = open(out, O_RDWR | O_CREAT, 0644);
if (outfd == -1) err(1, "open(%s)", out);
outpath = out;
} else {
outfd = open(outpath, O_RDWR | O_CREAT, 0644);
if (outfd == -1) err(1, "open(%s)", outpath);
}
if (outpath != NULL) {
snprintf(sigpath, 64, "%s.sig", outpath);
sigfd = open((const char*)sigpath, O_RDWR | O_CREAT, 0644);
if (sigfd == -1) err(1, "open(%s)", sigpath);
} else {
snprintf(sigpath, 64, "out.sig");
sigfd = open(sigpath, O_RDWR | O_CREAT, 0644);
if (sigfd == -1) err(1, "open(out.sig)");
}
if (keypath == NULL && !force) {
fprintf(stderr, "no key signing path specified\n");
fprintf(stderr, "usage: %s "USAGE"\n", argv0);
exit(1);
}
readpubkeyfile(recpath, reckey, NULL);
skibidi768_enc(ct, ss, reckey);
crypto_blake2b(shared, 32, ss, 32);
readseckeyfile(keypath, NULL, signkey);
len = write(outfd, ct, SKIBIDI768_CIPHERTEXTBYTES);
if (len != SKIBIDI768_CIPHERTEXTBYTES) err(1, "write(ct)");
getrandom(nonce, 24, 0);
write(outfd, nonce, 24);
crypto_blake2b_init(&ctx, 64);
counter = 0;
/* main encryption loop */
again:
while ((len = read(infd, read_buf, 65536)) == 65536) {
crypto_blake2b_update(&ctx, read_buf, len);
crypto_poly1305(mac, read_buf, len, shared);
if (write(outfd, mac, 16) != 16) err(1, "write(mac)");
counter = crypto_chacha20_x(enc_buf, read_buf, len,
shared, nonce, counter);
if (write(outfd, enc_buf, len) != len) err(1, "write(enc)");
}
/* TODO: incorporate into the loop above */
if (len > 0) {
crypto_blake2b_update(&ctx, read_buf, len);
crypto_poly1305(mac, read_buf, len, shared);
if (write(outfd, mac, 16) != 16) err(1, "write(mac)");
counter = crypto_chacha20_x(enc_buf, read_buf, len,
shared, nonce, counter);
if (write(outfd, enc_buf, len) != len) err(1, "write(enc)");
}
if (len == -1) {
if (errno == EINTR) goto again;
err(1, "wtf??");
}
if (outpath != NULL)
fprintf(stderr, "wrote encrypted file to %s.\n", outpath);
crypto_blake2b_final(&ctx, hash);
/* signing */
sigma3(sm, &smlen, hash, 64, NULL, 0, signkey);
if (write(sigfd, sm, smlen) != smlen)
err(1, "write(signature)");
close(sigfd);
fprintf(stderr, "wrote signature to %s.\n", sigpath);
crypto_wipe(ss, 32);
crypto_wipe(shared, 32);
crypto_wipe(signkey, SIGMA3_SECRETKEYBYTES);
if (out) free(out);
if (outfd != STDOUT_FILENO) close(outfd);
if (infd != STDIN_FILENO) close(infd);
return 0;
}
int
enc_decrypt(char *keypath, char *secpath, char *filepath, char *outpath)
{
unsigned char ss[32], shared[32];
unsigned char seckey[SKIBIDI768_SECRETKEYBYTES];
unsigned char ct[SKIBIDI768_CIPHERTEXTBYTES];
unsigned char nonce[24], mac[16], mac2[16];
uint64_t counter;
int infd, outfd;
ssize_t len;
char *out;
if (filepath == NULL || strcmp(filepath, "-") == 0) {
infd = STDIN_FILENO;
} else {
infd = open(filepath, O_RDONLY);
if (infd == -1) err(1, "open(%s)", filepath);
}
out = NULL;
if (outpath == NULL && infd == STDIN_FILENO && (!isatty(STDOUT_FILENO) || force)) {
outfd = STDOUT_FILENO;
} else if (outpath == NULL) {
if (filepath == NULL && !force && isatty(STDOUT_FILENO)) {
fprintf(stderr, "must set output file "
"or pass -f to write to stdout\n");
fprintf(stderr, "usage: %s "USAGE"\n", argv0);
exit(1);
}
len = strlen(filepath)+5;
out = calloc(len, 1);
snprintf(out, len, "%s.out", filepath);
outfd = open(out, O_RDWR | O_CREAT, 0644);
if (outfd == -1) err(1, "open(%s)", out);
} else {
outfd = open(outpath, O_RDWR | O_CREAT, 0644);
if (outfd == -1) err(1, "open(%s)", outpath);
}
readseckeyfile(secpath, seckey, NULL);
len = read(infd, ct, SKIBIDI768_CIPHERTEXTBYTES);
if (len != SKIBIDI768_CIPHERTEXTBYTES) err(1, "read(ct)");
skibidi768_dec(ss, ct, seckey);
crypto_blake2b(shared, 32, ss, 32);
if (read(infd, nonce, 24) != 24) err(1, "read(nonce)");
counter = 0;
/* main encryption loop */
again:
for (;;) {
len = read(infd, mac, 16);
if (len == 0) break;
if (len == -1) err(1, "read(mac)");
len = read(infd, read_buf, 65536);
if (len <= 0) err(1, "invalid state (no encrypted text)");
counter = crypto_chacha20_x(enc_buf, read_buf, len,
shared, nonce, counter);
if (write(outfd, enc_buf, len) != len) err(1, "write(enc)");
crypto_poly1305(mac2, enc_buf, len, shared);
if (memcmp(mac, mac2, 16) != 0) err(1, "invalid state (mac)");
}
if (len == -1) {
if (errno == EINTR) goto again;
err(1, "wtf??");
}
crypto_wipe(ss, 32);
crypto_wipe(shared, 32);
if (out) free(out);
return 0;
}
int
enc_verify(char *keyfile, char *sigfile, char *filepath)
{
struct blake2b_ctx ctx;
static unsigned char hash1[64], hash2[64];
static unsigned char sm[64 + SIGMA3_BYTES];
static unsigned char pubkey[SIGMA3_PUBLICKEYBYTES];
int fd, ret;
ssize_t len;
size_t mlen;
if (keyfile == NULL) errx(1, "must pass key file");
if (sigfile == NULL) errx(1, "must specify sig file");
if (filepath == NULL) errx(1, "must specify file to verify");
fd = open(filepath, O_RDONLY);
if (fd == -1) err(1, "read(%s)", filepath);
crypto_blake2b_init(&ctx, 64);
while ((len = read(fd, read_buf, 65536)) > 0) {
crypto_blake2b_update(&ctx, read_buf, len);
}
crypto_blake2b_final(&ctx, hash1);
readpubkeyfile(keyfile, NULL, pubkey);
len = readfile(sigfile, sm, 64 + SIGMA3_BYTES);
ret = sigma3_open(hash2, &mlen, sm, len, NULL, 0, pubkey);
if (ret == -1) errx(1, "signature failed: sigma3_open()");
if (mlen != 64) errx(1, "signature failed: mlen = %zu", mlen);
if (memcmp(hash1, hash2, 64)) errx(1, "signature failed: not equal");
fprintf(stderr, "signature passed\n");
return 0;
}
int
enc_keygen(void)
{
unsigned char skisec[SKIBIDI768_SECRETKEYBYTES];
unsigned char skipub[SKIBIDI768_PUBLICKEYBYTES];
unsigned char sigsec[SIGMA3_SECRETKEYBYTES];
unsigned char sigpub[SIGMA3_PUBLICKEYBYTES];
int pubfd, secfd;
ssize_t len;
skibidi768_keypair(skipub, skisec);
sigma3_keypair(sigpub, sigsec);
pubfd = open("public.key", O_RDWR | O_CREAT, 0644);
if (pubfd == -1) err(1, "open(public.key)");
secfd = open("secret.key", O_RDWR | O_CREAT, 0600);
if (secfd == -1) err(1, "open(secret.key)");
len = write(pubfd, skipub, SKIBIDI768_PUBLICKEYBYTES);
if (len != SKIBIDI768_PUBLICKEYBYTES) err(1, "write(public.key)");
len = write(pubfd, sigpub, SIGMA3_PUBLICKEYBYTES);
if (len != SIGMA3_PUBLICKEYBYTES) err(1, "write(public.key)");
fprintf(stderr, "wrote public key to public.key\n");
len = write(secfd, skisec, SKIBIDI768_SECRETKEYBYTES);
if (len != SKIBIDI768_SECRETKEYBYTES) err(1, "write(secret.key)");
len = write(secfd, sigsec, SIGMA3_SECRETKEYBYTES);
if (len != SIGMA3_SECRETKEYBYTES) err(1, "write(secret.key)");
fprintf(stderr, "wrote secret key to secret.key\n");
close(secfd);
close(pubfd);
crypto_wipe(skisec, SKIBIDI768_SECRETKEYBYTES);
crypto_wipe(sigsec, SIGMA3_SECRETKEYBYTES);
return 0;
}
ssize_t
readfile(const char *path, unsigned char *out, size_t outlen)
{
int fd;
ssize_t len;
fd = open(path, O_RDONLY);
if (fd == -1) err(1, "open(%s)", path);
if ((len = read(fd, out, outlen)) <= 0)
err(1, "read(%s)", path);
close(fd);
return len;
}
void
hex(unsigned char in[], unsigned char out[], size_t len)
{
unsigned char b1, b2;
static const char table[] = "0123456789ABCDEF";
for (int i = 0; i < len; i++) {
b1 = in[i] >> 4;
b2 = in[i] & 0x0F;
out[i*2] = table[b1];
out[i*2+1] = table[b2];
}
}
void
unhex(unsigned char in[], unsigned char out[], size_t len)
{
unsigned char b1, b2, p;
static const char table[] = {
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3,
['4'] = 4, ['5'] = 5, ['6'] = 6, ['7'] = 7,
['8'] = 8, ['9'] = 9, ['A'] = 10, ['B'] = 11,
['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
};
p = 0;
for (int i = 0; i < len; i += 2) {
b1 = table[in[i]];
b2 = table[in[i+1]];
out[p] = (b1 << 4) + b2;
p++;
}
}