forked from bwalex/tc-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcplay.c
2187 lines (1809 loc) · 48.8 KB
/
tcplay.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011 Alex Hornung <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define _BSD_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__DragonFly__)
#include <sys/param.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <err.h>
#include <time.h>
#if defined(__linux__)
#include <libdevmapper.h>
#include <uuid/uuid.h>
#elif defined(__DragonFly__)
#include <libdm.h>
#include <uuid.h>
#endif
#include <dirent.h>
#include "crc32.h"
#include "tcplay.h"
#include "humanize.h"
/* XXX TODO:
* - LRW-benbi support? needs further work in dm-crypt and even opencrypto
* - secure buffer review (i.e: is everything that needs it using secure mem?)
* - mlockall? (at least MCL_FUTURE, which is the only one we support)
*/
summary_fn_t summary_fn = NULL;
int tc_internal_verbose = 1;
char tc_internal_log_buffer[LOG_BUFFER_SZ];
int tc_internal_state = STATE_UNKNOWN;
void
tc_log(int is_err, const char *fmt, ...)
{
va_list ap;
FILE *fp;
if (is_err)
fp = stderr;
else
fp = stdout;
va_start(ap, fmt);
vsnprintf(tc_internal_log_buffer, LOG_BUFFER_SZ, fmt, ap);
va_end(ap);
if (tc_internal_verbose)
fprintf(fp, "%s", tc_internal_log_buffer);
}
/* Supported algorithms */
struct pbkdf_prf_algo pbkdf_prf_algos[] = {
{ "RIPEMD160", 2000 }, /* needs to come before the other RIPEMD160 */
{ "RIPEMD160", 1000 },
{ "SHA512", 1000 },
{ "whirlpool", 1000 },
{ NULL, 0 }
};
struct tc_crypto_algo tc_crypto_algos[] = {
#if 0
/* XXX: turns out TC doesn't support AES-128-XTS */
{ "AES-128-XTS", "aes-xts-plain", 32, 8 },
{ "TWOFISH-128-XTS", "twofish-xts-plain", 32, 8 },
{ "SERPENT-128-XTS", "serpent-xts-plain", 32, 8 },
#endif
{ "AES-256-XTS", "aes-xts-plain64", 64, 8 },
{ "TWOFISH-256-XTS", "twofish-xts-plain64", 64, 8 },
{ "SERPENT-256-XTS", "serpent-xts-plain64", 64, 8 },
{ NULL, NULL, 0, 0 }
};
const char *valid_cipher_chains[][MAX_CIPHER_CHAINS] = {
{ "AES-256-XTS", NULL },
{ "TWOFISH-256-XTS", NULL },
{ "SERPENT-256-XTS", NULL },
{ "AES-256-XTS", "TWOFISH-256-XTS", "SERPENT-256-XTS", NULL },
{ "SERPENT-256-XTS", "TWOFISH-256-XTS", "AES-256-XTS", NULL },
#if 0
/* It seems that all the two-way cascades are the other way round... */
{ "AES-256-XTS", "TWOFISH-256-XTS", NULL },
{ "SERPENT-256-XTS", "AES-256-XTS", NULL },
{ "TWOFISH-256-XTS", "SERPENT-256-XTS", NULL },
#endif
{ "TWOFISH-256-XTS", "AES-256-XTS", NULL },
{ "AES-256-XTS", "SERPENT-256-XTS", NULL },
{ "SERPENT-256-XTS", "TWOFISH-256-XTS", NULL },
{ NULL }
};
struct tc_cipher_chain *tc_cipher_chains[MAX_CIPHER_CHAINS];
static
int
tc_build_cipher_chains(void)
{
struct tc_cipher_chain *chain, *elem, *prev;
int i = 0;
int k;
while (valid_cipher_chains[i][0] != NULL) {
chain = NULL;
prev = NULL;
k = 0;
while (valid_cipher_chains[i][k] != NULL) {
if ((elem = alloc_safe_mem(sizeof(*elem))) == NULL) {
tc_log(1, "Error allocating memory for "
"cipher chain\n");
return -1;
}
/* Initialize first element of chain */
if (chain == NULL) {
chain = elem;
elem->prev = NULL;
}
/* Populate previous element */
if (prev != NULL) {
prev->next = elem;
elem->prev = prev;
}
/* Assume we are the last element in the chain */
elem->next = NULL;
/* Initialize other fields */
elem->cipher = check_cipher(valid_cipher_chains[i][k], 0);
if (elem->cipher == NULL)
return -1;
elem->key = NULL;
prev = elem;
++k;
}
/* Store cipher chain */
tc_cipher_chains[i++] = chain;
/* Integrity check */
if (i >= MAX_CIPHER_CHAINS) {
tc_log(1, "FATAL: tc_cipher_chains is full!!\n");
return -1;
}
/* Make sure array is NULL terminated */
tc_cipher_chains[i] = NULL;
}
return 0;
}
static
struct tc_cipher_chain *
tc_dup_cipher_chain(struct tc_cipher_chain *src)
{
struct tc_cipher_chain *first = NULL, *prev = NULL, *elem;
for (; src != NULL; src = src->next) {
if ((elem = alloc_safe_mem(sizeof(*elem))) == NULL) {
tc_log(1, "Error allocating memory for "
"duplicate cipher chain\n");
return NULL;
}
memcpy(elem, src, sizeof(*elem));
if (src->key != NULL) {
if ((elem->key = alloc_safe_mem(src->cipher->klen)) == NULL) {
tc_log(1, "Error allocating memory for "
"duplicate key in cipher chain\n");
return NULL;
}
memcpy(elem->key, src->key, src->cipher->klen);
}
if (first == NULL)
first = elem;
elem->next = NULL;
elem->prev = prev;
if (prev != NULL)
prev->next = elem;
prev = elem;
}
return first;
}
static
int
tc_free_cipher_chain(struct tc_cipher_chain *chain)
{
struct tc_cipher_chain *next = chain;
while ((chain = next) != NULL) {
next = chain->next;
if (chain->key != NULL)
free_safe_mem(chain->key);
free_safe_mem(chain);
}
return 0;
}
int
tc_cipher_chain_length(struct tc_cipher_chain *chain)
{
int len = 0;
for (; chain != NULL; chain = chain->next)
++len;
return len;
}
int
tc_cipher_chain_klen(struct tc_cipher_chain *chain)
{
int klen_bytes = 0;
for (; chain != NULL; chain = chain->next) {
klen_bytes += chain->cipher->klen;
}
return klen_bytes;
}
char *
tc_cipher_chain_sprint(char *buf, size_t bufsz, struct tc_cipher_chain *chain)
{
static char sbuf[256];
int n = 0;
if (buf == NULL) {
buf = sbuf;
bufsz = sizeof(sbuf);
}
for (; chain != NULL; chain = chain->next) {
n += snprintf(buf+n, bufsz-n, "%s%s", chain->cipher->name,
(chain->next != NULL) ? "," : "\0");
}
return buf;
}
#ifdef DEBUG
static void
print_hex(unsigned char *buf, off_t start, size_t len)
{
size_t i;
for (i = start; i < start+len; i++)
printf("%02x", buf[i]);
printf("\n");
}
#endif
void
print_info(struct tcplay_info *info)
{
printf("Device:\t\t\t%s\n", info->dev);
if (info->pbkdf_prf != NULL) {
printf("PBKDF2 PRF:\t\t%s\n", info->pbkdf_prf->name);
printf("PBKDF2 iterations:\t%d\n",
info->pbkdf_prf->iteration_count);
}
printf("Cipher:\t\t\t%s\n",
tc_cipher_chain_sprint(NULL, 0, info->cipher_chain));
printf("Key Length:\t\t%d bits\n",
8*tc_cipher_chain_klen(info->cipher_chain));
if (info->hdr != NULL) {
printf("CRC Key Data:\t\t%#x\n", info->hdr->crc_keys);
printf("Sector size:\t\t%d\n", info->hdr->sec_sz);
} else {
printf("Sector size:\t\t512\n");
}
printf("Volume size:\t\t%"DISKSZ_FMT" sectors\n", info->size);
#if 0
/* Don't print this; it's always 0 and is rather confusing */
printf("Volume offset:\t\t%"PRIu64"\n", (uint64_t)info->start);
#endif
#ifdef DEBUG
printf("Vol Flags:\t\t%d\n", info->volflags);
#endif
printf("IV offset:\t\t%"PRIu64" sectors\n",
(uint64_t)info->skip);
printf("Block offset:\t\t%"PRIu64" sectors\n",
(uint64_t)info->offset);
}
static
struct tcplay_info *
new_info(const char *dev, int flags, struct tc_cipher_chain *cipher_chain,
struct pbkdf_prf_algo *prf, struct tchdr_dec *hdr, off_t start)
{
struct tc_cipher_chain *chain_start;
struct tcplay_info *info;
int i;
int error;
chain_start = cipher_chain;
if ((info = (struct tcplay_info *)alloc_safe_mem(sizeof(*info))) == NULL) {
tc_log(1, "could not allocate safe info memory\n");
return NULL;
}
strncpy(info->dev, dev, sizeof(info->dev));
info->cipher_chain = cipher_chain;
info->pbkdf_prf = prf;
info->start = start;
info->hdr = hdr;
info->blk_sz = hdr->sec_sz;
info->size = hdr->sz_mk_scope / hdr->sec_sz; /* volume size */
info->skip = hdr->off_mk_scope / hdr->sec_sz; /* iv skip */
info->volflags = hdr->flags;
info->flags = flags;
if (TC_FLAG_SET(flags, SYS))
info->offset = 0; /* offset is 0 for system volumes */
else
info->offset = hdr->off_mk_scope / hdr->sec_sz; /* block offset */
/* Associate a key out of the key pool with each cipher in the chain */
error = tc_cipher_chain_populate_keys(cipher_chain, hdr->keys);
if (error) {
tc_log(1, "could not populate keys in cipher chain\n");
return NULL;
}
for (; cipher_chain != NULL; cipher_chain = cipher_chain->next) {
for (i = 0; i < cipher_chain->cipher->klen; i++)
sprintf(&cipher_chain->dm_key[i*2], "%02x",
cipher_chain->key[i]);
}
tc_cipher_chain_free_keys(chain_start);
return info;
}
int
free_info(struct tcplay_info *info)
{
if (info->cipher_chain)
tc_free_cipher_chain(info->cipher_chain);
if (info->hdr)
free_safe_mem(info->hdr);
free_safe_mem(info);
return 0;
}
int
adjust_info(struct tcplay_info *info, struct tcplay_info *hinfo)
{
if (hinfo->hdr->sz_hidvol == 0)
return 1;
info->size -= hinfo->hdr->sz_hidvol / hinfo->hdr->sec_sz;
return 0;
}
int
process_hdr(const char *dev, int flags, unsigned char *pass, int passlen,
struct tchdr_enc *ehdr, struct tcplay_info **pinfo)
{
struct tchdr_dec *dhdr;
struct tcplay_info *info;
struct tc_cipher_chain *cipher_chain = NULL;
unsigned char *key;
int i, j, found, error;
*pinfo = NULL;
if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
tc_log(1, "could not allocate safe key memory\n");
return ENOMEM;
}
/* Start search for correct algorithm combination */
found = 0;
for (i = 0; !found && pbkdf_prf_algos[i].name != NULL; i++) {
#ifdef DEBUG
printf("\nTrying PRF algo %s (%d)\n", pbkdf_prf_algos[i].name,
pbkdf_prf_algos[i].iteration_count);
printf("Salt: ");
print_hex(ehdr->salt, 0, sizeof(ehdr->salt));
#endif
error = pbkdf2(&pbkdf_prf_algos[i], (char *)pass, passlen,
ehdr->salt, sizeof(ehdr->salt),
MAX_KEYSZ, key);
if (error) {
tc_log(1, "pbkdf failed for algorithm %s\n",
pbkdf_prf_algos[i].name);
free_safe_mem(key);
return EINVAL;
}
#if 0
printf("Derived Key: ");
print_hex(key, 0, MAX_KEYSZ);
#endif
for (j = 0; !found && tc_cipher_chains[j] != NULL; j++) {
cipher_chain = tc_dup_cipher_chain(tc_cipher_chains[j]);
#ifdef DEBUG
printf("\nTrying cipher chain %d\n", j);
#endif
dhdr = decrypt_hdr(ehdr, cipher_chain, key);
if (dhdr == NULL) {
tc_log(1, "hdr decryption failed for cipher "
"chain %d\n", j);
free_safe_mem(key);
return EINVAL;
}
if (verify_hdr(dhdr)) {
#ifdef DEBUG
printf("tc_str: %.4s, tc_ver: %d, tc_min_ver: %d, "
"crc_keys: %d, sz_vol: %"PRIu64", "
"off_mk_scope: %"PRIu64", sz_mk_scope: %"PRIu64", "
"flags: %d, sec_sz: %d crc_dhdr: %d\n",
dhdr->tc_str, dhdr->tc_ver, dhdr->tc_min_ver,
dhdr->crc_keys, dhdr->sz_vol, dhdr->off_mk_scope,
dhdr->sz_mk_scope, dhdr->flags, dhdr->sec_sz,
dhdr->crc_dhdr);
#endif
found = 1;
} else {
free_safe_mem(dhdr);
tc_free_cipher_chain(cipher_chain);
}
}
}
free_safe_mem(key);
if (!found)
return EINVAL;
if ((info = new_info(dev, flags, cipher_chain,
&pbkdf_prf_algos[i-1], dhdr, 0)) == NULL) {
free_safe_mem(dhdr);
return ENOMEM;
}
*pinfo = info;
return 0;
}
int
create_volume(struct tcplay_opts *opts)
{
char *pass, *pass_again;
char *h_pass = NULL;
char buf[1024];
disksz_t blocks, hidden_blocks = 0;
size_t blksz;
struct tchdr_enc *ehdr, *hehdr;
struct tchdr_enc *ehdr_backup, *hehdr_backup;
uint64_t tmp;
int error, r, ret;
pass = h_pass = pass_again = NULL;
ehdr = hehdr = NULL;
ehdr_backup = hehdr_backup = NULL;
ret = -1; /* Default to returning error */
if (opts->cipher_chain == NULL)
opts->cipher_chain = tc_cipher_chains[0];
if (opts->prf_algo == NULL)
opts->prf_algo = &pbkdf_prf_algos[0];
if (opts->h_cipher_chain == NULL)
opts->h_cipher_chain = opts->cipher_chain;
if (opts->h_prf_algo == NULL)
opts->h_prf_algo = opts->prf_algo;
if ((error = get_disk_info(opts->dev, &blocks, &blksz)) != 0) {
tc_log(1, "could not get disk info\n");
return -1;
}
if ((blocks*blksz) <= MIN_VOL_BYTES) {
tc_log(1, "Cannot create volumes on devices with less "
"than %d bytes\n", MIN_VOL_BYTES);
return -1;
}
if (opts->interactive) {
if (((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) ||
((pass_again = alloc_safe_mem(PASS_BUFSZ)) == NULL)) {
tc_log(1, "could not allocate safe passphrase memory\n");
goto out;
}
if ((error = read_passphrase("Passphrase: ", pass, MAX_PASSSZ,
PASS_BUFSZ, 0) ||
(read_passphrase("Repeat passphrase: ", pass_again,
MAX_PASSSZ, PASS_BUFSZ, 0)))) {
tc_log(1, "could not read passphrase\n");
goto out;
}
if (strcmp(pass, pass_again) != 0) {
tc_log(1, "Passphrases don't match\n");
goto out;
}
free_safe_mem(pass_again);
pass_again = NULL;
} else {
/* In batch mode, use provided passphrase */
if ((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
tc_log(1, "could not allocate safe "
"passphrase memory");
goto out;
}
if (opts->passphrase != NULL) {
strncpy(pass, opts->passphrase, MAX_PASSSZ);
pass[MAX_PASSSZ] = '\0';
}
}
if (opts->nkeyfiles > 0) {
/* Apply keyfiles to 'pass' */
if ((error = apply_keyfiles((unsigned char *)pass, PASS_BUFSZ,
opts->keyfiles, opts->nkeyfiles))) {
tc_log(1, "could not apply keyfiles\n");
goto out;
}
}
if (opts->hidden) {
if (opts->interactive) {
if (((h_pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) ||
((pass_again = alloc_safe_mem(PASS_BUFSZ)) == NULL)) {
tc_log(1, "could not allocate safe "
"passphrase memory\n");
goto out;
}
if ((error = read_passphrase("Passphrase for hidden volume: ",
h_pass, MAX_PASSSZ, PASS_BUFSZ, 0) ||
(read_passphrase("Repeat passphrase: ", pass_again,
MAX_PASSSZ, PASS_BUFSZ, 0)))) {
tc_log(1, "could not read passphrase\n");
goto out;
}
if (strcmp(h_pass, pass_again) != 0) {
tc_log(1, "Passphrases for hidden volume don't "
"match\n");
goto out;
}
free_safe_mem(pass_again);
pass_again = NULL;
} else {
/* In batch mode, use provided passphrase */
if ((h_pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
tc_log(1, "could not allocate safe "
"passphrase memory");
goto out;
}
if (opts->h_passphrase != NULL) {
strncpy(h_pass, opts->h_passphrase, MAX_PASSSZ);
h_pass[MAX_PASSSZ] = '\0';
}
}
if (opts->n_hkeyfiles > 0) {
/* Apply keyfiles to 'h_pass' */
if ((error = apply_keyfiles((unsigned char *)h_pass,
PASS_BUFSZ, opts->h_keyfiles, opts->n_hkeyfiles))) {
tc_log(1, "could not apply keyfiles\n");
goto out;
}
}
if (opts->interactive) {
hidden_blocks = 0;
} else {
hidden_blocks = opts->hidden_size_bytes/blksz;
if (hidden_blocks == 0) {
tc_log(1, "hidden_blocks to create volume "
"cannot be zero!\n");
goto out;
}
if (opts->hidden_size_bytes >=
(blocks*blksz) - MIN_VOL_BYTES) {
tc_log(1, "Hidden volume needs to be "
"smaller than the outer volume\n");
goto out;
}
}
/* This only happens in interactive mode */
while (hidden_blocks == 0) {
if ((r = _humanize_number(buf, sizeof(buf),
(uint64_t)(blocks * blksz))) < 0) {
sprintf(buf, "%"DISKSZ_FMT" bytes", (blocks * blksz));
}
printf("The total volume size of %s is %s (bytes)\n", opts->dev, buf);
memset(buf, 0, sizeof(buf));
printf("Size of hidden volume (e.g. 127M): ");
fflush(stdout);
if ((fgets(buf, sizeof(buf), stdin)) == NULL) {
tc_log(1, "Could not read from stdin\n");
goto out;
}
/* get rid of trailing newline */
buf[strlen(buf)-1] = '\0';
if ((error = _dehumanize_number(buf,
&tmp)) != 0) {
tc_log(1, "Could not interpret input: %s\n", buf);
continue;
}
if (tmp >= (blocks*blksz) - MIN_VOL_BYTES) {
tc_log(1, "Hidden volume needs to be "
"smaller than the outer volume\n");
hidden_blocks = 0;
continue;
}
hidden_blocks = (size_t)tmp;
hidden_blocks /= blksz;
}
}
if (opts->interactive) {
/* Show summary and ask for confirmation */
printf("Summary of actions:\n");
if (opts->secure_erase)
printf(" - Completely erase *EVERYTHING* on %s\n", opts->dev);
printf(" - Create %svolume on %s\n", opts->hidden?("outer "):"", opts->dev);
if (opts->hidden) {
printf(" - Create hidden volume of %"DISKSZ_FMT" bytes at end of "
"outer volume\n",
hidden_blocks * blksz);
}
printf("\n Are you sure you want to proceed? (y/n) ");
fflush(stdout);
if ((fgets(buf, sizeof(buf), stdin)) == NULL) {
tc_log(1, "Could not read from stdin\n");
goto out;
}
if ((buf[0] != 'y') && (buf[0] != 'Y')) {
tc_log(1, "User cancelled action(s)\n");
goto out;
}
}
/* erase volume */
if (opts->secure_erase) {
tc_log(0, "Securely erasing the volume...\nThis process may take "
"some time depending on the size of the volume\n");
if (opts->state_change_fn)
opts->state_change_fn(opts->api_ctx, "secure_erase", 1);
if ((error = secure_erase(opts->dev, blocks * blksz, blksz)) != 0) {
tc_log(1, "could not securely erase device %s\n", opts->dev);
goto out;
}
if (opts->state_change_fn)
opts->state_change_fn(opts->api_ctx, "secure_erase", 0);
}
tc_log(0, "Creating volume headers...\nDepending on your system, this "
"process may take a few minutes as it uses true random data which "
"might take a while to refill\n");
if (opts->weak_keys_and_salt) {
tc_log(0, "WARNING: Using a weak random generator to get "
"entropy for the key material. Odds are this is NOT "
"what you want.\n");
}
if (opts->state_change_fn)
opts->state_change_fn(opts->api_ctx, "create_header", 1);
/* create encrypted headers */
ehdr = create_hdr((unsigned char *)pass,
(opts->nkeyfiles > 0)?MAX_PASSSZ:strlen(pass),
opts->prf_algo, opts->cipher_chain, blksz, blocks, VOL_RSVD_BYTES_START/blksz,
blocks - (MIN_VOL_BYTES/blksz), 0, opts->weak_keys_and_salt, &ehdr_backup);
if (ehdr == NULL) {
tc_log(1, "Could not create header\n");
goto out;
}
if (opts->hidden) {
hehdr = create_hdr((unsigned char *)h_pass,
(opts->n_hkeyfiles > 0)?MAX_PASSSZ:strlen(h_pass), opts->h_prf_algo,
opts->h_cipher_chain,
blksz, blocks,
blocks - (VOL_RSVD_BYTES_END/blksz) - hidden_blocks,
hidden_blocks, 1, opts->weak_keys_and_salt, &hehdr_backup);
if (hehdr == NULL) {
tc_log(1, "Could not create hidden volume header\n");
goto out;
}
}
if (opts->state_change_fn)
opts->state_change_fn(opts->api_ctx, "create_header", 0);
tc_log(0, "Writing volume headers to disk...\n");
if ((error = write_to_disk(opts->dev, 0, blksz, ehdr, sizeof(*ehdr))) != 0) {
tc_log(1, "Could not write volume header to device\n");
goto out;
}
/* Write backup header; it's offset is relative to the end */
if ((error = write_to_disk(opts->dev, (blocks*blksz - BACKUP_HDR_OFFSET_END),
blksz, ehdr_backup, sizeof(*ehdr_backup))) != 0) {
tc_log(1, "Could not write backup volume header to device\n");
goto out;
}
if (opts->hidden) {
if ((error = write_to_disk(opts->dev, HDR_OFFSET_HIDDEN, blksz, hehdr,
sizeof(*hehdr))) != 0) {
tc_log(1, "Could not write hidden volume header to "
"device\n");
goto out;
}
/* Write backup hidden header; offset is relative to end */
if ((error = write_to_disk(opts->dev,
(blocks*blksz - BACKUP_HDR_HIDDEN_OFFSET_END), blksz,
hehdr_backup, sizeof(*hehdr_backup))) != 0) {
tc_log(1, "Could not write backup hidden volume "
"header to device\n");
goto out;
}
}
/* Everything went ok */
tc_log(0, "All done!\n");
ret = 0;
out:
if (pass)
free_safe_mem(pass);
if (h_pass)
free_safe_mem(h_pass);
if (pass_again)
free_safe_mem(pass_again);
if (ehdr)
free_safe_mem(ehdr);
if (hehdr)
free_safe_mem(hehdr);
if (ehdr_backup)
free_safe_mem(ehdr_backup);
if (hehdr_backup)
free_safe_mem(hehdr_backup);
return ret;
}
struct tcplay_info *
info_map_common(struct tcplay_opts *opts, char *passphrase_out)
{
struct tchdr_enc *ehdr, *hehdr = NULL;
struct tcplay_info *info, *hinfo = NULL;
char *pass;
char *h_pass;
int error, error2 = 0;
size_t sz;
size_t blksz;
disksz_t blocks;
int is_hidden = 0;
int try_empty = 0;
int retries;
if ((error = get_disk_info(opts->dev, &blocks, &blksz)) != 0) {
tc_log(1, "could not get disk information\n");
return NULL;
}
if (opts->retries < 1)
retries = 1;
else
retries = opts->retries;
/*
* Add one retry so we can do a first try without asking for
* a password if keyfiles are passed in.
*/
if (opts->interactive && (opts->nkeyfiles > 0)) {
try_empty = 1;
++retries;
}
info = NULL;
ehdr = NULL;
pass = h_pass = NULL;
while ((info == NULL) && retries-- > 0)
{
pass = h_pass = NULL;
ehdr = hehdr = NULL;
info = hinfo = NULL;
if ((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
tc_log(1, "could not allocate safe passphrase memory\n");
goto out;
}
if (try_empty) {
pass[0] = '\0';
} else if (opts->interactive) {
if ((error = read_passphrase("Passphrase: ", pass,
MAX_PASSSZ, PASS_BUFSZ, opts->timeout))) {
tc_log(1, "could not read passphrase\n");
/* XXX: handle timeout differently? */
goto out;
}
pass[MAX_PASSSZ] = '\0';
} else {
/* In batch mode, use provided passphrase */
if (opts->passphrase != NULL) {
strncpy(pass, opts->passphrase, MAX_PASSSZ);
pass[MAX_PASSSZ] = '\0';
}
}
if (passphrase_out != NULL) {
strcpy(passphrase_out, pass);
}
if (opts->nkeyfiles > 0) {
/* Apply keyfiles to 'pass' */
if ((error = apply_keyfiles((unsigned char *)pass, PASS_BUFSZ,
opts->keyfiles, opts->nkeyfiles))) {
tc_log(1, "could not apply keyfiles");
goto out;
}
}
if (opts->protect_hidden) {
if ((h_pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
tc_log(1, "could not allocate safe passphrase memory\n");
goto out;
}
if (opts->interactive) {
if ((error = read_passphrase(
"Passphrase for hidden volume: ", h_pass,
MAX_PASSSZ, PASS_BUFSZ, opts->timeout))) {
tc_log(1, "could not read passphrase\n");
goto out;
}
h_pass[MAX_PASSSZ] = '\0';
} else {
/* In batch mode, use provided passphrase */
if (opts->h_passphrase != NULL) {
strncpy(h_pass, opts->h_passphrase, MAX_PASSSZ);
h_pass[MAX_PASSSZ] = '\0';
}
}
if (opts->n_hkeyfiles > 0) {
/* Apply keyfiles to 'pass' */
if ((error = apply_keyfiles((unsigned char *)h_pass, PASS_BUFSZ,
opts->h_keyfiles, opts->n_hkeyfiles))) {
tc_log(1, "could not apply keyfiles");
goto out;
}
}
}
/* Always read blksz-sized chunks */
sz = blksz;
if (TC_FLAG_SET(opts->flags, HDR_FROM_FILE)) {
ehdr = (struct tchdr_enc *)read_to_safe_mem(
opts->hdr_file_in, 0, &sz);
if (ehdr == NULL) {
tc_log(1, "error read hdr_enc: %s", opts->hdr_file_in);
goto out;
}
} else {
ehdr = (struct tchdr_enc *)read_to_safe_mem(
(TC_FLAG_SET(opts->flags, SYS)) ? opts->sys_dev : opts->dev,
(TC_FLAG_SET(opts->flags, SYS) || TC_FLAG_SET(opts->flags, FDE)) ?
HDR_OFFSET_SYS :
(!TC_FLAG_SET(opts->flags, BACKUP)) ? 0 : -BACKUP_HDR_OFFSET_END,
&sz);
if (ehdr == NULL) {
tc_log(1, "error read hdr_enc: %s", opts->dev);
goto out;
}
}
if (!TC_FLAG_SET(opts->flags, SYS)) {
/* Always read blksz-sized chunks */
sz = blksz;
if (TC_FLAG_SET(opts->flags, H_HDR_FROM_FILE)) {
hehdr = (struct tchdr_enc *)read_to_safe_mem(
opts->h_hdr_file_in, 0, &sz);
if (hehdr == NULL) {
tc_log(1, "error read hdr_enc: %s", opts->h_hdr_file_in);
goto out;
}
} else {
hehdr = (struct tchdr_enc *)read_to_safe_mem(opts->dev,
(!TC_FLAG_SET(opts->flags, BACKUP)) ? HDR_OFFSET_HIDDEN :
-BACKUP_HDR_HIDDEN_OFFSET_END, &sz);
if (hehdr == NULL) {
tc_log(1, "error read hdr_enc: %s", opts->dev);
goto out;
}
}
} else {
hehdr = NULL;
}
error = process_hdr(opts->dev, opts->flags, (unsigned char *)pass,
(opts->nkeyfiles > 0)?MAX_PASSSZ:strlen(pass),