-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfet.c
660 lines (519 loc) · 13.5 KB
/
sfet.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#ifdef __linux
#define _FILE_OFFSET_BITS 64
#include <sys/prctl.h>
#endif
#include <sys/resource.h>
#include <sys/mman.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <endian.h>
#include <err.h>
#include "utils.h"
#include "cleanup.h"
#include "buffer.h"
#include "burnstack.h"
#include "readpass.h"
#include "pbkdf2-hmac-sha512.h"
#include "poly1305-serpent.h"
#include "ctr-serpent.h"
#define ITERATIONS 256000
#define PASSWD_SRC "/dev/tty"
#define VERSION "pre3.0-0"
#define FILEVER 8
#define PASSLEN 512
#define CHUNKLEN (32*1024*1024)
struct config {
int verbose;
int force;
uint64_t iterations;
uint64_t chunklen;
const char *passfn;
};
struct header {
char magic[4];
uint16_t version;
uint64_t iter;
uint8_t nonce[16];
uint64_t chunklen;
} __attribute__((packed));
static void
printhex(FILE *fp, const uint8_t *vec, size_t n)
{
int i;
if (n > 0) {
for (i = 0; i < n; i++)
fprintf(fp, "%02x", vec[i]);
} else
fputc('-', fp);
fputc('\n', fp);
}
static void
printusage(FILE *fp)
{
fprintf(fp, "decrypt:\tsfet [-d] [-vf] [-p <fn>] [<input>] [<output>]\n");
fprintf(fp, "encrypt:\tsfet -e [-vf] [-p <fn>] [-i <iter>] [-c <length>] [<input>] [<output>]\n");
fprintf(fp, "show metadata:\tsfet -s [-v] [<input>]\n");
fprintf(fp, "\n");
fprintf(fp, "options:\n");
fprintf(fp, " -v\t\tincrease verbosity level\n");
fprintf(fp, " -f\t\toverwrite outputfile, if it already exists\n");
fprintf(fp, " -p <file>\tread password from <file> instead of %s\n", PASSWD_SRC);
fprintf(fp, " -i <n>\tset pbkdf2 iteration number to <n>, encryption only\n");
fprintf(fp, " -c <length>\tset chunk size to <length>, encryption only\n");
fprintf(fp, " -V\t\tshow version\n");
fprintf(fp, " -h\t\tshow this help message\n");
}
static void
printversion(FILE *fp)
{
fprintf(fp, "sfet %s, file version: %d\n", VERSION, FILEVER);
#if defined(USE_ASM_X86_64) || defined(USE_ASM_AVX)
fprintf(fp, "build with: ");
#ifdef USE_ASM_X86_64
fprintf(fp, "ASM-X86-64 ");
#endif
#ifdef USE_ASM_AVX
fprintf(fp, "AVX ");
#endif
fprintf(fp, "\n");
#endif
}
static int
parse_chunklen(uint64_t *out, const char *str)
{
char *endp;
long long int n;
n = strtoll(str, &endp, 10);
if (n <= 0 || n == LONG_MAX)
return -1;
switch (*endp) {
case '\0':
*out = n;
break;
case 'k': case 'K':
*out = (uint64_t)n * 1024;
break;
case 'm': case 'M':
*out = (uint64_t)n * 1024 * 1024;
break;
case 'g': case 'G':
*out = (uint64_t)n * 1024 * 1024 * 1024;
break;
default:
return -1;
}
return 0;
}
static inline void
next_nonce(uint8_t nonce[16])
{
int i;
for (i = 15; i >= 0 && ++nonce[i] == 0; i--);
}
/*
* main functions
*/
static int
encrypt(const char *inputfn, const char *outputfn, const struct config *conf)
{
cu_fclose FILE *in = stdin;
cu_fclose FILE *out = stdout;
cu_freebuffer struct buffer *buffer = NULL;
struct header header;
size_t n;
uint8_t passwd[PASSLEN];
uint8_t key[32+32]; /* 32 serpent ctr + 32 poly1305-serpent */
uint8_t nonce[16];
struct ctr_serpent ctrctx;
struct poly1305_serpent polyctx;
/* open input file */
if (strcmp(inputfn, "-") != 0) {
in = fopen(inputfn, "r");
if (in == NULL) {
warn("%s: can't open input file", inputfn);
return 1;
}
}
/* read password (read_pass_fn is verbose) */
if (read_pass_fn(conf->passfn, passwd, sizeof(passwd),
"Password: ", "Confirm: ") == -1)
return 1;
/* initialize nonce with random data */
if (secrand(nonce, 16) == -1) {
warn("can't read random data");
return 1;
}
/* initialize crypto */
if (conf->verbose > 0) {
fprintf(stderr, "chunk length: %" PRIu64 "\n", conf->chunklen);
fprintf(stderr, "iterations: %" PRIu64 "\n", conf->iterations);
fprintf(stderr, "nonce: ");
printhex(stderr, nonce, 16);
}
pbkdf2_hmac_sha512(key, sizeof(key), passwd, PASSLEN, nonce, 16, conf->iterations);
ctr_serpent_init(&ctrctx, key);
ctr_serpent_nonce(&ctrctx, nonce);
poly1305_serpent_setkey(&polyctx, key+32);
/* allocate chunk buffer */
buffer = buffer_alloc(conf->chunklen + 16);
if (buffer == NULL) {
warn("can't allocate memory");
return 1;
}
/* open output file */
if (strcmp(outputfn, "-") != 0) {
out = fopen(outputfn, conf->force ? "w" : "wx");
if (out == NULL) {
warn("%s: can't open output file", outputfn);
return 1;
}
}
/* create header */
memcpy(header.magic, "SFET", 4);
header.version = htobe16(FILEVER);
header.iter = htobe64(conf->iterations);
memcpy(header.nonce, nonce, 16);
header.chunklen = htobe64(conf->chunklen);
memcpy(buffer->data, &header, sizeof(struct header));
/* authenticate and write header */
poly1305_serpent_authdata(&polyctx, buffer->data, sizeof(struct header),
nonce, buffer->data+sizeof(struct header));
next_nonce(nonce);
if (fwrite(buffer->data, sizeof(struct header)+16, 1, out) != 1) {
warn("%s: can't write to output file", outputfn);
return 1;
}
/* encryption loop */
do {
n = fread(buffer->data, 1, conf->chunklen, in);
ctr_serpent_crypt(&ctrctx, buffer->data, buffer->data, n);
poly1305_serpent_authdata(&polyctx, buffer->data, n, nonce, buffer->data+n);
next_nonce(nonce);
if (fwrite(buffer->data, 1, n+16, out) != n+16) {
warn("%s: can't write to output file", outputfn);
return 1;
}
} while (n == conf->chunklen);
/* check for reading error */
if (ferror(in)) {
warn("%s: error reading file", inputfn);
return 1;
}
return 0;
}
static int
decrypt(const char *inputfn, const char *outputfn, const struct config *conf)
{
cu_fclose FILE *in = stdin;
cu_fclose FILE *out = stdout;
cu_freebuffer struct buffer *buffer = NULL;
struct header header;
size_t n;
uint8_t mac[16], check[16];
uint64_t chunklen;
uint8_t passwd[PASSLEN];
uint8_t key[32+32]; /* 32 serpent ctr + 32 poly1305-serpent */
uint8_t nonce[16];
struct ctr_serpent ctrctx;
struct poly1305_serpent polyctx;
/* open input file */
if (strcmp(inputfn, "-") != 0) {
in = fopen(inputfn, "r");
if (in == NULL) {
warn("%s: can't open input file", inputfn);
return 1;
}
}
/* read header */
n = fread(&header, sizeof(struct header), 1, in);
if (n != 1) {
if (feof(in))
warnx("%s: file too short, can't read header", inputfn);
else
warn("%s: can't read header", inputfn);
return 1;
}
if (memcmp(header.magic, "SFET", 4) != 0) {
warnx("%s: not a sfet file", inputfn);
return 1;
}
if (be16toh(header.version) != FILEVER) {
warnx("%s: unsupported file version: %u\n",
inputfn, be16toh(header.version));
return 1;
}
memcpy(nonce, header.nonce, 16);
chunklen = be64toh(header.chunklen);
/* read password */
if (read_pass_fn(conf->passfn, passwd, sizeof(passwd),
"Password: ", NULL) == -1)
return 1; /* read_pass_fn is verbose */
/* initialize cryptography */
if (conf->verbose > 0) {
fprintf(stderr, "chunk length: %" PRIu64 "\n", chunklen);
fprintf(stderr, "iterations: %" PRIu64 "\n", be64toh(header.iter));
fprintf(stderr, "nonce: ");
printhex(stderr, nonce, 16);
}
pbkdf2_hmac_sha512(key, sizeof(key), passwd, PASSLEN, nonce, 16, be64toh(header.iter));
ctr_serpent_init(&ctrctx, key);
ctr_serpent_nonce(&ctrctx, nonce);
poly1305_serpent_setkey(&polyctx, key+32);
/* read and check header mac */
if (fread(mac, 1, 16, in) != 16) {
if (feof(in))
warnx("%s: file too short, header mac missing", inputfn);
else
warn("%s: can't read header mac", inputfn);
return 1;
}
poly1305_serpent_authdata(&polyctx,
(uint8_t*)&header, sizeof(struct header),
nonce, check);
next_nonce(nonce);
if (!ctiseq(mac, check, 16)) {
warnx("%s: corrupt header or wrong password", inputfn);
return 1;
}
/* allocate chunk buffer */
buffer = buffer_alloc(chunklen + 16);
if (buffer == NULL) {
warn("can't allocate memory");
return 1;
}
/* open output file */
if (strcmp(outputfn, "-") != 0) {
out = fopen(outputfn, conf->force ? "w" : "wx");
if (out == NULL) {
warn("%s: can't open output file", outputfn);
return 1;
}
}
/* decryption loop */
do {
n = fread(buffer->data, 1, chunklen+16, in);
if (n < 16) {
/* is this an error or is the file damaged? */
if (ferror(in))
warn("%s: can't read from input file", inputfn);
else
warnx("%s: incomplete chunk, file is damaged", inputfn);
return 1;
}
/* set n to the data length in this chunk */
n -= 16;
poly1305_serpent_authdata(&polyctx, buffer->data, n, nonce, check);
next_nonce(nonce);
if (!ctiseq(buffer->data+n, check, 16)) {
warnx("%s: WARNING, file was modified!", inputfn);
return 1;
}
ctr_serpent_crypt(&ctrctx, buffer->data, buffer->data, n);
if (fwrite(buffer->data, 1, n, out) != n) {
warn("%s: can't write to output file", outputfn);
return 1;
}
} while (n == chunklen);
/* check for input error */
if (ferror(in)) {
warn("%s: can't read from input file", inputfn);
return 1;
}
return 0;
}
int
show(const char *inputfn, const struct config *conf)
{
cu_fclose FILE *in = stdin;
cu_freebuffer struct buffer *buffer = NULL;
struct header header;
size_t n;
uint64_t chunklen;
uint8_t mac[16];
if (strcmp(inputfn, "-") != 0) {
in = fopen(inputfn, "r");
if (in == NULL) {
warn("%s: can't open input file", inputfn);
return 1;
}
}
/* read header */
n = fread(&header, sizeof(struct header), 1, in);
if (n != 1) {
if (feof(in))
warnx("%s: file too short, can't read header", inputfn);
else
warn("%s: can't read header", inputfn);
return 1;
}
if (memcmp(header.magic, "SFET", 4) != 0) {
warnx("%s: not a sfet file", inputfn);
return 1;
}
printf("sfet file, version: %u\n", be16toh(header.version));
if (be16toh(header.version) != FILEVER) {
warnx("%s: unsupported file version: %u",
inputfn, be16toh(header.version));
return 1;
}
chunklen = be64toh(header.chunklen);
/* show key param values */
printf("iterations: %" PRIu64 "\n", be64toh(header.iter));
printf("chunk length: %" PRIu64 "\n", chunklen);
printf("nonce: ");
printhex(stdout, header.nonce, 16);
if (fread(mac, 1, 16, in) != 16) {
if (feof(in))
warnx("%s: file too short, can't read header mac", inputfn);
else
warn("%s: can't read header mac", inputfn);
return 1;
}
printf("header mac: ");
printhex(stdout, mac, 16);
if (conf->verbose > 0) {
buffer = buffer_alloc(chunklen+16);
if (buffer == NULL) {
warn("can't allocate memory");
return 1;
}
do {
n = fread(buffer->data, 1, chunklen+16, in);
if (n < 16) {
if (feof(in))
warnx("%s: file too short, can't read chunk", inputfn);
else
warn("%s: error reading chunk", inputfn);
return 1;
}
n -= 16;
printf("chunk mac: ");
printhex(stdout, buffer->data+n, 16);
} while (n == chunklen);
if (ferror(in)) {
warn("%s: can't read input file", inputfn);
return 1;
}
}
return 0;
}
int
main(int argc, char *argv[])
{
const char *inputfn = "-";
const char *outputfn = "-";
struct config conf;
int option;
int rval = 1;
enum {
MODE_ENCRYPT,
MODE_DECRYPT,
MODE_SHOW,
} mode;
/*
* set core limit to 0, which disables core dumps on most unix
* systems.
*
* please note: this does not work for linux, if a pipe is being used
* for core_pattern (like systemd-coredump in a typical systemd setup).
* a hack wourd be to set the core limit to 1, but we use the
* 'official' method with prctl on linux, see below.
*/
if (setrlimit(RLIMIT_CORE, &(struct rlimit){0, 0}) == -1)
err(1, "cat't disable core dumps via setrlimit()");
#ifdef __linux
/* disable core dumps under linux */
if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
err(1, "can't disable core dumps");
#endif
/* lock memory */
if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1)
err(1, "can't lock memory");
/* set default config values */
conf.verbose = 0;
conf.force = 0;
conf.iterations = ITERATIONS;
conf.chunklen = CHUNKLEN;
conf.passfn = PASSWD_SRC;
mode = MODE_DECRYPT;
/* parse parameters */
while ((option = getopt(argc, argv, "hVedsvfi:c:p:")) != -1) {
switch (option) {
/* options */
case 'v':
conf.verbose++;
break;
case 'f':
conf.force = 1;
break;
case 'i':
conf.iterations = atoll(optarg);
break;
case 'p':
conf.passfn = optarg;
break;
case 'c':
if (parse_chunklen(&conf.chunklen, optarg) == -1)
errx(1, "illegal chunk length: %s", optarg);
break;
/* main modes */
case 'h':
printusage(stdout);
exit(0);
case 'V':
printversion(stdout);
exit(0);
case 'e':
mode = MODE_ENCRYPT;
break;
case 'd':
mode = MODE_DECRYPT;
break;
case 's':
mode = MODE_SHOW;
break;
default:
printusage(stderr);
printf("version: %s, fileversion: %d\n", VERSION, FILEVER);
exit(1);
}
}
argc -= optind;
argv += optind;
/* check parameter */
if (argc > 0)
inputfn = argv[0];
if (argc > 1)
outputfn = argv[1];
if (conf.iterations < 1024)
errx(1, "illegal number of pbkdf2 iterations: %" PRIu64,
conf.iterations);
if (conf.chunklen < sizeof(struct header))
errx(1, "chunk size too small: %" PRIu64, conf.chunklen);
/* early warning if output file already exists... */
if (strcmp(outputfn, "-") != 0) {
if (!conf.force && exists(outputfn))
errx(1, "%s: output file already exists, use -f to overwrite", outputfn);
}
/* now do our job */
switch (mode) {
case MODE_ENCRYPT:
rval = encrypt(inputfn, outputfn, &conf);
break;
case MODE_DECRYPT:
rval = decrypt(inputfn, outputfn, &conf);
break;
case MODE_SHOW:
rval = show(inputfn, &conf);
break;
}
/* cleanup stack */
burnstack(64);
return rval;
}