forked from bwalex/tc-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdr.c
432 lines (365 loc) · 10.8 KB
/
hdr.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
/*
* 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.
*/
#include <sys/types.h>
#if defined(__DragonFly__)
#include <sys/endian.h>
#elif defined(__linux__)
#include <endian.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "crc32.h"
#include "tcplay.h"
/* Endianess macros */
#define BE_TO_HOST(n, v) v = be ## n ## toh(v)
#define LE_TO_HOST(n, v) v = le ## n ## toh(v)
#define HOST_TO_BE(n, v) v = htobe ## n (v)
#define HOST_TO_LE(n, v) v = htole ## n (v)
struct sig_hdr_cfg {
const char *sig;
uint16_t min_ver;
};
struct sig_hdr_cfg sig_hdr_cfgs[] = {
{ TC_SIG, 0x0007 },
{ VC_SIG, 0x0b01 },
{ NULL, 0x0000 }
};
static
const
struct sig_hdr_cfg *hdr_cfg_from_sig(const char *sig)
{
const struct sig_hdr_cfg *cfg;
for (cfg = &sig_hdr_cfgs[0]; cfg->sig != NULL; cfg++) {
if (strcmp(cfg->sig, sig) == 0)
return cfg;
}
return NULL;
}
struct tchdr_dec *
decrypt_hdr(struct tchdr_enc *ehdr, struct tc_cipher_chain *cipher_chain,
unsigned char *key)
{
struct tchdr_dec *dhdr;
unsigned char iv[128];
int error;
if ((dhdr = alloc_safe_mem(sizeof(struct tchdr_dec))) == NULL) {
tc_log(1, "Error allocating safe tchdr_dec memory\n");
return NULL;
}
memset(iv, 0, sizeof(iv));
error = tc_decrypt(cipher_chain, key, iv, ehdr->enc,
sizeof(struct tchdr_dec), (unsigned char *)dhdr);
if (error) {
tc_log(1, "Header decryption failed\n");
free_safe_mem(dhdr);
return NULL;
}
BE_TO_HOST(16, dhdr->tc_ver);
LE_TO_HOST(16, dhdr->tc_min_ver);
BE_TO_HOST(32, dhdr->crc_keys);
BE_TO_HOST(64, dhdr->vol_ctime);
BE_TO_HOST(64, dhdr->hdr_ctime);
BE_TO_HOST(64, dhdr->sz_hidvol);
BE_TO_HOST(64, dhdr->sz_vol);
BE_TO_HOST(64, dhdr->off_mk_scope);
BE_TO_HOST(64, dhdr->sz_mk_scope);
BE_TO_HOST(32, dhdr->flags);
BE_TO_HOST(32, dhdr->sec_sz);
BE_TO_HOST(32, dhdr->crc_dhdr);
return dhdr;
}
int
verify_hdr(struct tchdr_dec *hdr, struct pbkdf_prf_algo *prf_algo)
{
uint32_t crc;
if (memcmp(hdr->tc_str, prf_algo->sig, sizeof(hdr->tc_str)) != 0) {
#ifdef DEBUG
fprintf(stderr, "Signature mismatch\n");
#endif
return 0;
}
crc = crc32((void *)&hdr->keys, 256);
if (crc != hdr->crc_keys) {
#ifdef DEBUG
fprintf(stderr, "CRC32 mismatch (crc_keys)\n");
#endif
return 0;
}
switch(hdr->tc_ver) {
case 1:
case 2:
/* Unsupported header version */
tc_log(1, "Header version %d unsupported\n", hdr->tc_ver);
return 0;
case 3:
case 4:
hdr->sec_sz = 512;
break;
}
return 1;
}
struct tchdr_enc *
create_hdr(unsigned char *pass, int passlen, struct pbkdf_prf_algo *prf_algo,
struct tc_cipher_chain *cipher_chain, size_t sec_sz,
disksz_t total_blocks __unused,
off_t offset, disksz_t blocks, int hidden, int weak, struct tchdr_enc **backup_hdr)
{
struct tchdr_enc *ehdr, *ehdr_backup;
struct tchdr_dec *dhdr;
unsigned char *key, *key_backup;
unsigned char iv[128];
const struct sig_hdr_cfg *hdr_cfg;
int error;
key = key_backup = NULL;
dhdr = NULL;
ehdr = ehdr_backup = NULL;
if (backup_hdr != NULL)
*backup_hdr = NULL;
if ((dhdr = (struct tchdr_dec *)alloc_safe_mem(sizeof(*dhdr))) == NULL) {
tc_log(1, "could not allocate safe dhdr memory\n");
goto error;
}
if ((ehdr = (struct tchdr_enc *)alloc_safe_mem(sizeof(*ehdr))) == NULL) {
tc_log(1, "could not allocate safe ehdr memory\n");
goto error;
}
if ((ehdr_backup = (struct tchdr_enc *)alloc_safe_mem
(sizeof(*ehdr_backup))) == NULL) {
tc_log(1, "could not allocate safe ehdr_backup memory\n");
goto error;
}
if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
tc_log(1, "could not allocate safe key memory\n");
goto error;
}
if ((key_backup = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
tc_log(1, "could not allocate safe backup key memory\n");
goto error;
}
if ((error = get_random(ehdr->salt, sizeof(ehdr->salt), weak)) != 0) {
tc_log(1, "could not get salt\n");
goto error;
}
if ((error = get_random(ehdr_backup->salt, sizeof(ehdr_backup->salt), weak))
!= 0) {
tc_log(1, "could not get salt for backup header\n");
goto error;
}
error = pbkdf2(prf_algo, (char *)pass, passlen,
ehdr->salt, sizeof(ehdr->salt),
MAX_KEYSZ, key);
if (error) {
tc_log(1, "could not derive key\n");
goto error;
}
error = pbkdf2(prf_algo, (char *)pass, passlen,
ehdr_backup->salt, sizeof(ehdr_backup->salt),
MAX_KEYSZ, key_backup);
if (error) {
tc_log(1, "could not derive backup key\n");
goto error;
}
memset(dhdr, 0, sizeof(*dhdr));
if ((error = get_random(dhdr->keys, sizeof(dhdr->keys), weak)) != 0) {
tc_log(1, "could not get key random bits\n");
goto error;
}
if ((hdr_cfg = hdr_cfg_from_sig(prf_algo->sig)) == NULL) {
tc_log(1, "could not find internal header configuration\n");
goto error;
}
memcpy(dhdr->tc_str, prf_algo->sig, 4);
dhdr->tc_ver = 5;
dhdr->tc_min_ver = hdr_cfg->min_ver;
dhdr->crc_keys = crc32((void *)&dhdr->keys, 256);
dhdr->sz_vol = blocks * sec_sz;
if (hidden)
dhdr->sz_hidvol = dhdr->sz_vol;
dhdr->off_mk_scope = offset * sec_sz;
dhdr->sz_mk_scope = blocks * sec_sz;
dhdr->sec_sz = sec_sz;
dhdr->flags = 0;
HOST_TO_BE(16, dhdr->tc_ver);
HOST_TO_LE(16, dhdr->tc_min_ver);
HOST_TO_BE(32, dhdr->crc_keys);
HOST_TO_BE(64, dhdr->sz_vol);
HOST_TO_BE(64, dhdr->sz_hidvol);
HOST_TO_BE(64, dhdr->off_mk_scope);
HOST_TO_BE(64, dhdr->sz_mk_scope);
HOST_TO_BE(32, dhdr->sec_sz);
HOST_TO_BE(32, dhdr->flags);
dhdr->crc_dhdr = crc32((void *)dhdr, 188);
HOST_TO_BE(32, dhdr->crc_dhdr);
memset(iv, 0, sizeof(iv));
error = tc_encrypt(cipher_chain, key, iv, (unsigned char *)dhdr,
sizeof(struct tchdr_dec), ehdr->enc);
if (error) {
tc_log(1, "Header encryption failed\n");
goto error;
}
memset(iv, 0, sizeof(iv));
error = tc_encrypt(cipher_chain, key_backup, iv,
(unsigned char *)dhdr,
sizeof(struct tchdr_dec), ehdr_backup->enc);
if (error) {
tc_log(1, "Backup header encryption failed\n");
goto error;
}
free_safe_mem(key);
free_safe_mem(key_backup);
free_safe_mem(dhdr);
if (backup_hdr != NULL)
*backup_hdr = ehdr_backup;
else
free_safe_mem(ehdr_backup);
return ehdr;
/* NOT REACHED */
error:
if (key)
free_safe_mem(key);
if (key_backup)
free_safe_mem(key_backup);
if (dhdr)
free_safe_mem(dhdr);
if (ehdr)
free_safe_mem(ehdr);
if (ehdr_backup)
free_safe_mem(ehdr_backup);
return NULL;
}
struct tchdr_enc *copy_reencrypt_hdr(unsigned char *pass, int passlen,
struct pbkdf_prf_algo *prf_algo, int weak, struct tcplay_info *info,
struct tchdr_enc **backup_hdr)
{
struct tchdr_enc *ehdr, *ehdr_backup;
unsigned char *key, *key_backup;
unsigned char iv[128];
const struct sig_hdr_cfg *hdr_cfg;
int error;
key = key_backup = NULL;
ehdr = ehdr_backup = NULL;
/* By default stick to current PRF algo */
if (prf_algo == NULL)
prf_algo = info->pbkdf_prf;
if ((ehdr = (struct tchdr_enc *)alloc_safe_mem(sizeof(*ehdr))) == NULL) {
tc_log(1, "could not allocate safe ehdr memory\n");
goto error;
}
if ((ehdr_backup = (struct tchdr_enc *)alloc_safe_mem
(sizeof(*ehdr_backup))) == NULL) {
tc_log(1, "could not allocate safe ehdr_backup memory\n");
goto error;
}
if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
tc_log(1, "could not allocate safe key memory\n");
goto error;
}
if ((key_backup = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
tc_log(1, "could not allocate safe backup key memory\n");
goto error;
}
if ((error = get_random(ehdr->salt, sizeof(ehdr->salt), weak)) != 0) {
tc_log(1, "could not get salt\n");
goto error;
}
if ((error = get_random(ehdr_backup->salt, sizeof(ehdr_backup->salt), weak))
!= 0) {
tc_log(1, "could not get salt for backup header\n");
goto error;
}
error = pbkdf2(prf_algo, (char *)pass, passlen,
ehdr->salt, sizeof(ehdr->salt),
MAX_KEYSZ, key);
if (error) {
tc_log(1, "could not derive key\n");
goto error;
}
error = pbkdf2(prf_algo, (char *)pass, passlen,
ehdr_backup->salt, sizeof(ehdr_backup->salt),
MAX_KEYSZ, key_backup);
if (error) {
tc_log(1, "could not derive backup key\n");
goto error;
}
if ((hdr_cfg = hdr_cfg_from_sig(prf_algo->sig)) == NULL) {
tc_log(1, "could not find internal header configuration\n");
goto error;
}
/* Update signature and min_ver depending on selected PBKDF2 PRF algo */
memcpy(info->hdr->tc_str, prf_algo->sig, 4);
info->hdr->tc_min_ver = hdr_cfg->min_ver;
HOST_TO_BE(16, info->hdr->tc_ver);
HOST_TO_LE(16, info->hdr->tc_min_ver);
HOST_TO_BE(32, info->hdr->crc_keys);
HOST_TO_BE(64, info->hdr->vol_ctime);
HOST_TO_BE(64, info->hdr->hdr_ctime);
HOST_TO_BE(64, info->hdr->sz_vol);
HOST_TO_BE(64, info->hdr->sz_hidvol);
HOST_TO_BE(64, info->hdr->off_mk_scope);
HOST_TO_BE(64, info->hdr->sz_mk_scope);
HOST_TO_BE(32, info->hdr->sec_sz);
HOST_TO_BE(32, info->hdr->flags);
HOST_TO_BE(32, info->hdr->crc_dhdr);
memset(iv, 0, sizeof(iv));
error = tc_encrypt(info->cipher_chain, key, iv,
(unsigned char *)info->hdr, sizeof(struct tchdr_dec), ehdr->enc);
if (error) {
tc_log(1, "Header encryption failed\n");
goto error;
}
memset(iv, 0, sizeof(iv));
error = tc_encrypt(info->cipher_chain, key_backup, iv,
(unsigned char *)info->hdr,
sizeof(struct tchdr_dec), ehdr_backup->enc);
if (error) {
tc_log(1, "Backup header encryption failed\n");
goto error;
}
free_safe_mem(key);
free_safe_mem(key_backup);
if (backup_hdr != NULL)
*backup_hdr = ehdr_backup;
else
free_safe_mem(ehdr_backup);
return ehdr;
/* NOT REACHED */
error:
if (key)
free_safe_mem(key);
if (key_backup)
free_safe_mem(key_backup);
if (ehdr)
free_safe_mem(ehdr);
if (ehdr_backup)
free_safe_mem(ehdr_backup);
return NULL;
}