-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlenc.c
More file actions
512 lines (448 loc) · 14.8 KB
/
lenc.c
File metadata and controls
512 lines (448 loc) · 14.8 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* current version of lenc */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <err.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/random.h>
#include "monocypher.h"
#include "kem/api.h"
#include "sign/api.h"
struct file {
struct file *next;
struct stat st;
const char *path;
int fd, set;
} file_init = {
.next = NULL,
.st = {0},
.path = NULL,
.fd = -1,
.set = 0,
};
#define PUBKEYFILESIZE (SKIBIDI768_PUBLICKEYBYTES + SIGMA3_PUBLICKEYBYTES)
#define SECKEYFILESIZE (SKIBIDI768_SECRETKEYBYTES + SIGMA3_SECRETKEYBYTES)
struct pubkey {
union {
unsigned char buf[PUBKEYFILESIZE];
struct {
unsigned char kem[SKIBIDI768_PUBLICKEYBYTES];
unsigned char sign[SIGMA3_PUBLICKEYBYTES];
};
};
};
struct seckey {
union {
unsigned char buf[SECKEYFILESIZE];
struct {
unsigned char kem[SKIBIDI768_SECRETKEYBYTES];
unsigned char sign[SIGMA3_SECRETKEYBYTES];
};
};
};
#define FILE_INIT(ptr) memcpy(ptr, &file_init, sizeof(*ptr))
enum {
NONE,
GENERATE,
ENCRYPT,
DECRYPT,
SIGN,
VERIFY,
};
void *xcalloc(size_t nmemb, size_t size);
void process_args(struct file *keypath, struct file *recpath,
struct file *inpath, struct file *outpath,
struct file *sigpath);
void readpubkeyfile(int fd, struct pubkey *out);
void readseckeyfile(int fd, struct seckey *out);
/* commands */
int lenc_generate(void);
int lenc_encrypt(struct file *key, struct file *recs,
struct file *in, struct file *out);
int lenc_decrypt(struct file *key, struct file *recs,
struct file *in, struct file *out);
/* globals */
int force, verbose, quiet;
unsigned char read_buf[65536];
unsigned char enc_buf[65536];
static char *argv0;
static struct option longopts[] = {
/* commands */
{ "generate", no_argument, NULL, 'G' },
{ "encrypt", no_argument, NULL, 'E' },
{ "decrypt", no_argument, NULL, 'D' },
{ "sign", no_argument, NULL, 'S' },
{ "verify", no_argument, NULL, 'V' },
/* flags */
{ "verbose", no_argument, NULL, 'v' },
{ "quiet", no_argument, NULL, 'q' },
{ "force", no_argument, NULL, 'f' },
/* args */
{ "key", required_argument, NULL, 'k' },
{ "recipient", required_argument, NULL, 'r' },
{ "output", required_argument, NULL, 'o' },
{ "signature", required_argument, NULL, 's' },
{ NULL, 0, NULL, 0 },
};
/* file format v1
*
* |----------------------------------------------------|
* | recipient_count: uint16_t |
* |----------------------------------------------------|
* | recipients: SKIBIDI768_CIPHERTEXTBYTES * nr |
* |----------------------------------------------------|
* | cipher_text: <file size> |
* |----------------------------------------------------|
* | signature: SIGMA3_BYTES + 64 |
* |----------------------------------------------------|
*/
int
main(int argc, char *argv[])
{
int ch, op;
const char *optstring;
struct file keypath, inpath, outpath, sigpath;
struct file recpath, *reccur = &recpath;
FILE_INIT(&keypath);
FILE_INIT(&recpath);
FILE_INIT(&inpath);
FILE_INIT(&outpath);
FILE_INIT(&sigpath);
argv0 = argv[0];
op = NONE;
optstring = "GEDSVvqfk:r:o:s:";
while ((ch = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
switch (ch) {
/* commands */
case 'G': op = GENERATE; break;
case 'E': op = ENCRYPT; break;
case 'D': op = DECRYPT; break;
case 'S': op = SIGN; break;
case 'V': op = VERIFY; break;
/* flags */
case 'v': verbose = 1; break;
case 'q': quiet = 1; break;
case 'f': force = 1; break;
/* args */
case 'k':
keypath.path = optarg;
keypath.set = 1;
break;
case 'r':
reccur->path = optarg;
reccur->set = 1;
reccur->next = xcalloc(1, sizeof(*reccur));
reccur = reccur->next;
break;
case 'o':
outpath.path = optarg;
outpath.set = 1;
break;
case 's':
sigpath.path = optarg;
sigpath.set = 1;
break;
default:
fprintf(stderr, "usage: go fuck yourself\n");
return 1;
}
}
if (argv[optind] != NULL) {
inpath.path = argv[optind];
inpath.set = 1;
}
process_args(&keypath, &recpath, &inpath, &outpath, &sigpath);
switch (op) {
case GENERATE: return lenc_generate();
case ENCRYPT: return lenc_encrypt(&keypath, &recpath, &inpath, &outpath);
case DECRYPT: return lenc_decrypt(&keypath, &recpath, &inpath, &outpath);
case SIGN: break;
case VERIFY: break;
default:
fprintf(stderr, "usage: go fuck yourself\n");
return 1;
}
}
int
lenc_encrypt(struct file *key, struct file *recs,
struct file *in, struct file *out)
{
static unsigned char sm[64 + SIGMA3_BYTES];
static unsigned char ct[SKIBIDI768_CIPHERTEXTBYTES];
static struct pubkey pub;
static struct seckey sec;
static struct blake2b_ctx ctx;
unsigned char nonce[24], mac[16];
unsigned char hash[64];
unsigned char ss[32], shared[32], coins[64];
uint16_t nrecs;
uint64_t counter, enc_len, len2;
ssize_t len;
size_t smlen;
if (!out->set) errx(1, "need output file");
if (!key->set) errx(1, "need key file");
if (!recs->set) errx(1, "need recipients");
if (!in->set) errx(1, "need file to encrypt");
getrandom(coins, 64, 0);
readpubkeyfile(recs->fd, &pub);
readseckeyfile(key->fd, &sec);
/* TODO: replace with actual nrecs */
nrecs = 1;
if (write(out->fd, &nrecs, sizeof(nrecs)) != sizeof(nrecs)) {
err(1, "write(nrecs)");
}
/* only runs once until i add actual nrecs */
for (uint16_t i = 0; i < nrecs; i++) {
skibidi768_enc_derand(ct, ss, pub.kem, coins);
crypto_blake2b(shared, 32, ss, 32);
crypto_wipe(ss, 32);
len = write(out->fd, ct, SKIBIDI768_CIPHERTEXTBYTES);
if (len != SKIBIDI768_CIPHERTEXTBYTES) err(1, "write(ct)");
}
getrandom(nonce, 24, 0);
if (write(out->fd, nonce, 24) != 24) err(1, "write(nonce)");
crypto_blake2b_init(&ctx, 64);
counter = 0;
again:
/* TODO: guarantee 65536 is read until the very end */
while ((len = read(in->fd, read_buf, 65536)) > 0) {
crypto_blake2b_update(&ctx, read_buf, len);
len2 = len;
counter = crypto_chacha20_x((uint8_t*)&enc_len, (uint8_t*)&len2,
sizeof(uint64_t), shared, nonce, counter);
counter = crypto_chacha20_x(enc_buf, read_buf, len,
shared, nonce, counter);
crypto_poly1305(mac, enc_buf, len, shared);
if (write(out->fd, mac, 16) != 16) err(1, "write(mac)");
if (write(out->fd, &enc_len, sizeof(uint64_t)) != sizeof(uint64_t)) {
err(1, "write(enc_len)");
}
if (write(out->fd, enc_buf, len) != len) err(1, "write(enc)");
}
if (len == -1) {
if (errno == EINTR) goto again;
err(1, "wtf??");
}
crypto_blake2b_final(&ctx, hash);
sigma3(sm, &smlen, hash, 64, NULL, 0, sec.sign);
if (smlen != 64 + SIGMA3_BYTES) {
errx(1, "erm what the sigma");
}
if (write(out->fd, sm, smlen) != (ssize_t)smlen) {
err(1, "write(sm)");
}
if (out->fd != STDOUT_FILENO && !quiet) {
fprintf(stderr, "wrote encrypted file to %s.\n", out->path);
}
close(out->fd);
close(in->fd);
close(key->fd);
close(recs->fd);
crypto_wipe(shared, 32);
crypto_wipe(&sec, sizeof(sec));
return 0;
}
int
lenc_decrypt(struct file *key, struct file *rec,
struct file *in, struct file *out)
{
static unsigned char ct[SKIBIDI768_CIPHERTEXTBYTES];
static unsigned char sm[64 + SIGMA3_BYTES];
static struct pubkey pub;
static struct seckey sec;
static struct blake2b_ctx ctx;
unsigned char hash1[64], hash2[64];
unsigned char ss[32], shared[32];
unsigned char nonce[24], mac1[16], mac2[16];
uint64_t counter, enc_len, len2;
uint16_t nrecs;
ssize_t len;
size_t mlen;
int ret;
if (!out->set) errx(1, "need output file");
if (!key->set) errx(1, "need key file");
if (!rec->set) errx(1, "need recipients");
if (!in->set) errx(1, "need file to decrypt");
readseckeyfile(rec->fd, &sec);
readpubkeyfile(key->fd, &pub);
/* TODO: handle multiple recipients */
len = read(in->fd, &nrecs, sizeof(nrecs));
if (len != sizeof(nrecs)) err(1, "read(nrecs)");
len = read(in->fd, ct, SKIBIDI768_CIPHERTEXTBYTES);
if (len != SKIBIDI768_CIPHERTEXTBYTES) err(1, "read(ct)");
skibidi768_dec(ss, ct, sec.kem);
crypto_blake2b(shared, 32, ss, 32);
crypto_wipe(ss, 32);
if (read(in->fd, nonce, 24) != 24) err(1, "read(nonce)");
crypto_blake2b_init(&ctx, 64);
counter = 0;
for (;;) {
len = read(in->fd, mac1, 16);
if (len == -1) {
err(1, "read(mac)");
}
if (len != 16) err(1, "read(mac)");
len = read(in->fd, &enc_len, sizeof(uint64_t));
if (len != sizeof(uint64_t)) err(1, "read(enc_len)");
counter = crypto_chacha20_x((uint8_t*)&len2, (uint8_t*)&enc_len,
sizeof(uint64_t), shared, nonce, counter);
if (len2 == 0 || len2 > 65536) errx(1, "invalid state");
len = read(in->fd, read_buf, len2);
if (len == 0) errx(1, "invalid state (no encrypted text)");
if (len == -1) {
err(1, "read(enc)");
}
if (len != (ssize_t)len2) err(1, "read(enc)");
crypto_poly1305(mac2, read_buf, len, shared);
if (memcmp(mac1, mac2, 16) != 0) err(1, "invalid state (mac)");
counter = crypto_chacha20_x(enc_buf, read_buf, len,
shared, nonce, counter);
if (write(out->fd, enc_buf, len) != len) err(1, "write(enc)");
crypto_blake2b_update(&ctx, enc_buf, len);
if (len < 65536) break;
}
crypto_blake2b_final(&ctx, hash1);
if (read(in->fd, sm, 64 + SIGMA3_BYTES) != 64 + SIGMA3_BYTES) {
err(1, "read(sm)");
}
ret = sigma3_open(hash2, &mlen, sm, 64 + SIGMA3_BYTES,
NULL, 0, pub.sign);
if (ret == -1) errx(1, "signature failed: 0x01");
if (mlen != 64) errx(1, "signature failed: 0x02");
if (memcmp(hash1, hash2, 64)) errx(1, "signature failed: 0x03");
if (!quiet) {
fprintf(stderr, "signature passed\n");
if (out->fd != STDOUT_FILENO) {
fprintf(stderr, "wrote to %s.\n", out->path);
}
}
close(out->fd);
close(in->fd);
close(key->fd);
close(rec->fd);
crypto_wipe(shared, 32);
crypto_wipe(&sec, sizeof(sec));
return 0;
}
int
lenc_generate(void)
{
static unsigned char skisec[SKIBIDI768_SECRETKEYBYTES];
static unsigned char skipub[SKIBIDI768_PUBLICKEYBYTES];
static unsigned char sigsec[SIGMA3_SECRETKEYBYTES];
static 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)");
if (!quiet) 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)");
if (!quiet) 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;
}
void
readpubkeyfile(int fd,
struct pubkey *out)
{
ssize_t ret;
if (out == NULL) errx(1, "pubkey out == NULL");
again:
if ((ret = read(fd, out->buf, PUBKEYFILESIZE)) == -1) {
if (errno == EINTR) goto again;
else err(1, "read(pubkey)");
}
if (ret != PUBKEYFILESIZE) errx(1, "error reading pubkey");
}
void
readseckeyfile(int fd,
struct seckey *out)
{
ssize_t ret;
if (out == NULL) errx(1, "seckey out == NULL");
again:
if ((ret = read(fd, out->buf, SECKEYFILESIZE)) == -1) {
if (errno == EINTR) goto again;
else err(1, "read(seckey)");
}
if (ret != SECKEYFILESIZE) err(1, "error reading seckey: %zd", ret);
}
int
process_inarg(const char *name, struct file *f)
{
if (f->path != NULL) {
if (strcmp(f->path, "-") == 0) {
f->fd = STDIN_FILENO;
return 1;
} else {
f->fd = open(f->path, O_RDONLY);
if (f->fd == -1) {
err(1, "%s: open(%s)", name, f->path);
}
fstat(f->fd, &f->st);
}
} else if (!f->set) {
return 0;
} else {
/* btw i have no idea if this is reachable */
errx(1, "must pass arg for %s", name);
}
return 0;
}
void
process_args(struct file *keypath, struct file *recpath,
struct file *inpath, struct file *outpath,
struct file *sigpath)
{
int nstdin;
nstdin = 0;
nstdin += process_inarg("key path", keypath);
nstdin += process_inarg("input file", inpath);
nstdin += process_inarg("signal file", sigpath);
for (;recpath != NULL; recpath = recpath->next) {
nstdin += process_inarg("recipient path", recpath);
}
if (outpath->set) {
if (strcmp(outpath->path, "-") == 0) {
outpath->fd = STDOUT_FILENO;
} else {
outpath->fd = open(outpath->path, O_RDWR | O_CREAT, 0644);
if (outpath->fd == -1) err(1, "open(%s)", outpath->path);
}
} else {
if (!isatty(STDOUT_FILENO)) {
outpath->fd = STDOUT_FILENO;
}
}
if (nstdin > 1) {
errx(1, "only 1 input may be accepted from stdin");
}
}
void *
xcalloc(size_t nmemb, size_t size)
{
void *ret = calloc(nmemb, size);
if (ret == NULL) {
err(1, "xcalloc(%zu, %zu)", nmemb, size);
}
return ret;
}