2 * TCRYPT (TrueCrypt-compatible) and VeraCrypt volume handling
4 * Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2012-2020 Milan Broz
7 * This file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this file; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "libcryptsetup.h"
32 /* TCRYPT PBKDF variants */
34 unsigned int legacy:1;
35 unsigned int veracrypt:1;
38 unsigned int iterations;
39 uint32_t veracrypt_pim_const;
40 uint32_t veracrypt_pim_mult;
42 { 0, 0, "pbkdf2", "ripemd160", 2000, 0, 0 },
43 { 0, 0, "pbkdf2", "ripemd160", 1000, 0, 0 },
44 { 0, 0, "pbkdf2", "sha512", 1000, 0, 0 },
45 { 0, 0, "pbkdf2", "whirlpool", 1000, 0, 0 },
46 { 1, 0, "pbkdf2", "sha1", 2000, 0, 0 },
47 { 0, 1, "pbkdf2", "sha512", 500000, 15000, 1000 },
48 { 0, 1, "pbkdf2", "whirlpool", 500000, 15000, 1000 },
49 { 0, 1, "pbkdf2", "sha256", 500000, 15000, 1000 }, // VeraCrypt 1.0f
50 { 0, 1, "pbkdf2", "sha256", 200000, 0, 2048 }, // boot only
51 { 0, 1, "pbkdf2", "ripemd160", 655331, 15000, 1000 },
52 { 0, 1, "pbkdf2", "ripemd160", 327661, 0, 2048 }, // boot only
53 { 0, 1, "pbkdf2", "stribog512",500000, 15000, 1000 },
54 // { 0, 1, "pbkdf2", "stribog512",200000, 0, 2048 }, // boot only
55 { 0, 0, NULL, NULL, 0, 0, 0 }
60 unsigned int key_size;
62 unsigned int key_offset;
63 unsigned int iv_offset; /* or tweak key offset */
64 unsigned int key_extra_size;
68 unsigned int legacy:1;
69 unsigned int chain_count;
70 unsigned int chain_key_size;
71 const char *long_name;
73 struct tcrypt_alg cipher[3];
76 /* TCRYPT cipher variants */
77 static struct tcrypt_algs tcrypt_cipher[] = {
79 {0,1,64,"aes","xts-plain64",
80 {{"aes", 64,16,0,32,0}}},
81 {0,1,64,"serpent","xts-plain64",
82 {{"serpent",64,16,0,32,0}}},
83 {0,1,64,"twofish","xts-plain64",
84 {{"twofish",64,16,0,32,0}}},
85 {0,2,128,"twofish-aes","xts-plain64",
86 {{"twofish",64,16, 0,64,0},
87 {"aes", 64,16,32,96,0}}},
88 {0,3,192,"serpent-twofish-aes","xts-plain64",
89 {{"serpent",64,16, 0, 96,0},
90 {"twofish",64,16,32,128,0},
91 {"aes", 64,16,64,160,0}}},
92 {0,2,128,"aes-serpent","xts-plain64",
93 {{"aes", 64,16, 0,64,0},
94 {"serpent",64,16,32,96,0}}},
95 {0,3,192,"aes-twofish-serpent","xts-plain64",
96 {{"aes", 64,16, 0, 96,0},
97 {"twofish",64,16,32,128,0},
98 {"serpent",64,16,64,160,0}}},
99 {0,2,128,"serpent-twofish","xts-plain64",
100 {{"serpent",64,16, 0,64,0},
101 {"twofish",64,16,32,96,0}}},
102 {0,1,64,"camellia","xts-plain64",
103 {{"camellia", 64,16,0,32,0}}},
104 {0,1,64,"kuznyechik","xts-plain64",
105 {{"kuznyechik", 64,16,0,32,0}}},
106 {0,2,128,"kuznyechik-camellia","xts-plain64",
107 {{"kuznyechik",64,16, 0,64,0},
108 {"camellia", 64,16,32,96,0}}},
109 {0,2,128,"twofish-kuznyechik","xts-plain64",
110 {{"twofish", 64,16, 0,64,0},
111 {"kuznyechik",64,16,32,96,0}}},
112 {0,2,128,"serpent-camellia","xts-plain64",
113 {{"serpent", 64,16, 0,64,0},
114 {"camellia", 64,16,32,96,0}}},
115 {0,2,128,"aes-kuznyechik","xts-plain64",
116 {{"aes", 64,16, 0,64,0},
117 {"kuznyechik",64,16,32,96,0}}},
118 {0,3,192,"camellia-serpent-kuznyechik","xts-plain64",
119 {{"camellia", 64,16, 0, 96,0},
120 {"serpent", 64,16,32,128,0},
121 {"kuznyechik",64,16,64,160,0}}},
124 {0,1,48,"aes","lrw-benbi",
125 {{"aes", 48,16,32,0,0}}},
126 {0,1,48,"serpent","lrw-benbi",
127 {{"serpent",48,16,32,0,0}}},
128 {0,1,48,"twofish","lrw-benbi",
129 {{"twofish",48,16,32,0,0}}},
130 {0,2,96,"twofish-aes","lrw-benbi",
131 {{"twofish",48,16,32,0,0},
132 {"aes", 48,16,64,0,0}}},
133 {0,3,144,"serpent-twofish-aes","lrw-benbi",
134 {{"serpent",48,16,32,0,0},
135 {"twofish",48,16,64,0,0},
136 {"aes", 48,16,96,0,0}}},
137 {0,2,96,"aes-serpent","lrw-benbi",
138 {{"aes", 48,16,32,0,0},
139 {"serpent",48,16,64,0,0}}},
140 {0,3,144,"aes-twofish-serpent","lrw-benbi",
141 {{"aes", 48,16,32,0,0},
142 {"twofish",48,16,64,0,0},
143 {"serpent",48,16,96,0,0}}},
144 {0,2,96,"serpent-twofish", "lrw-benbi",
145 {{"serpent",48,16,32,0,0},
146 {"twofish",48,16,64,0,0}}},
148 /* Kernel LRW block size is fixed to 16 bytes for GF(2^128)
149 * thus cannot be used with blowfish where block is 8 bytes.
150 * There also no GF(2^64) support.
151 {1,1,64,"blowfish_le","lrw-benbi",
152 {{"blowfish_le",64,8,32,0,0}}},
153 {1,2,112,"blowfish_le-aes","lrw-benbi",
154 {{"blowfish_le",64, 8,32,0,0},
155 {"aes", 48,16,88,0,0}}},
156 {1,3,160,"serpent-blowfish_le-aes","lrw-benbi",
157 {{"serpent", 48,16, 32,0,0},
158 {"blowfish_le",64, 8, 64,0,0},
159 {"aes", 48,16,120,0,0}}},*/
162 * CBC + "outer" CBC (both with whitening)
163 * chain_key_size: alg_keys_bytes + IV_seed_bytes + whitening_bytes
165 {1,1,32+16+16,"aes","cbc-tcw",
166 {{"aes", 32,16,32,0,32}}},
167 {1,1,32+16+16,"serpent","cbc-tcw",
168 {{"serpent",32,16,32,0,32}}},
169 {1,1,32+16+16,"twofish","cbc-tcw",
170 {{"twofish",32,16,32,0,32}}},
171 {1,2,64+16+16,"twofish-aes","cbci-tcrypt",
172 {{"twofish",32,16,32,0,0},
173 {"aes", 32,16,64,0,32}}},
174 {1,3,96+16+16,"serpent-twofish-aes","cbci-tcrypt",
175 {{"serpent",32,16,32,0,0},
176 {"twofish",32,16,64,0,0},
177 {"aes", 32,16,96,0,32}}},
178 {1,2,64+16+16,"aes-serpent","cbci-tcrypt",
179 {{"aes", 32,16,32,0,0},
180 {"serpent",32,16,64,0,32}}},
181 {1,3,96+16+16,"aes-twofish-serpent", "cbci-tcrypt",
182 {{"aes", 32,16,32,0,0},
183 {"twofish",32,16,64,0,0},
184 {"serpent",32,16,96,0,32}}},
185 {1,2,64+16+16,"serpent-twofish", "cbci-tcrypt",
186 {{"serpent",32,16,32,0,0},
187 {"twofish",32,16,64,0,32}}},
188 {1,1,16+8+16,"cast5","cbc-tcw",
189 {{"cast5", 16,8,32,0,24}}},
190 {1,1,24+8+16,"des3_ede","cbc-tcw",
191 {{"des3_ede",24,8,32,0,24}}},
192 {1,1,56+8+16,"blowfish_le","cbc-tcrypt",
193 {{"blowfish_le",56,8,32,0,24}}},
194 {1,2,88+16+16,"blowfish_le-aes","cbc-tcrypt",
195 {{"blowfish_le",56, 8,32,0,0},
196 {"aes", 32,16,88,0,32}}},
197 {1,3,120+16+16,"serpent-blowfish_le-aes","cbc-tcrypt",
198 {{"serpent", 32,16, 32,0,0},
199 {"blowfish_le",56, 8, 64,0,0},
200 {"aes", 32,16,120,0,32}}},
204 static int TCRYPT_hdr_from_disk(struct crypt_device *cd,
205 struct tcrypt_phdr *hdr,
206 struct crypt_params_tcrypt *params,
207 int kdf_index, int cipher_index)
212 /* Check CRC32 of header */
213 size = TCRYPT_HDR_LEN - sizeof(hdr->d.keys) - sizeof(hdr->d.header_crc32);
214 crc32 = crypt_crc32(~0, (unsigned char*)&hdr->d, size) ^ ~0;
215 if (be16_to_cpu(hdr->d.version) > 3 &&
216 crc32 != be32_to_cpu(hdr->d.header_crc32)) {
217 log_dbg(cd, "TCRYPT header CRC32 mismatch.");
221 /* Check CRC32 of keys */
222 crc32 = crypt_crc32(~0, (unsigned char*)hdr->d.keys, sizeof(hdr->d.keys)) ^ ~0;
223 if (crc32 != be32_to_cpu(hdr->d.keys_crc32)) {
224 log_dbg(cd, "TCRYPT keys CRC32 mismatch.");
228 /* Convert header to cpu format */
229 hdr->d.version = be16_to_cpu(hdr->d.version);
230 hdr->d.version_tc = be16_to_cpu(hdr->d.version_tc);
232 hdr->d.keys_crc32 = be32_to_cpu(hdr->d.keys_crc32);
234 hdr->d.hidden_volume_size = be64_to_cpu(hdr->d.hidden_volume_size);
235 hdr->d.volume_size = be64_to_cpu(hdr->d.volume_size);
237 hdr->d.mk_offset = be64_to_cpu(hdr->d.mk_offset);
238 if (!hdr->d.mk_offset)
239 hdr->d.mk_offset = 512;
241 hdr->d.mk_size = be64_to_cpu(hdr->d.mk_size);
243 hdr->d.flags = be32_to_cpu(hdr->d.flags);
245 hdr->d.sector_size = be32_to_cpu(hdr->d.sector_size);
246 if (!hdr->d.sector_size)
247 hdr->d.sector_size = 512;
249 hdr->d.header_crc32 = be32_to_cpu(hdr->d.header_crc32);
252 params->passphrase = NULL;
253 params->passphrase_size = 0;
254 params->hash_name = tcrypt_kdf[kdf_index].hash;
255 params->key_size = tcrypt_cipher[cipher_index].chain_key_size;
256 params->cipher = tcrypt_cipher[cipher_index].long_name;
257 params->mode = tcrypt_cipher[cipher_index].mode;
263 * Kernel implements just big-endian version of blowfish, hack it here
265 static void TCRYPT_swab_le(char *buf)
267 uint32_t *l = (uint32_t*)&buf[0];
268 uint32_t *r = (uint32_t*)&buf[4];
273 static int decrypt_blowfish_le_cbc(struct tcrypt_alg *alg,
274 const char *key, char *buf)
276 int bs = alg->iv_size;
277 char iv[bs], iv_old[bs];
278 struct crypt_cipher *cipher = NULL;
281 assert(bs == 2*sizeof(uint32_t));
283 r = crypt_cipher_init(&cipher, "blowfish", "ecb",
284 &key[alg->key_offset], alg->key_size);
288 memcpy(iv, &key[alg->iv_offset], alg->iv_size);
289 for (i = 0; i < TCRYPT_HDR_LEN; i += bs) {
290 memcpy(iv_old, &buf[i], bs);
291 TCRYPT_swab_le(&buf[i]);
292 r = crypt_cipher_decrypt(cipher, &buf[i], &buf[i],
294 TCRYPT_swab_le(&buf[i]);
297 for (j = 0; j < bs; j++)
299 memcpy(iv, iv_old, bs);
302 crypt_cipher_destroy(cipher);
303 crypt_safe_memzero(iv, bs);
304 crypt_safe_memzero(iv_old, bs);
308 static void TCRYPT_remove_whitening(char *buf, const char *key)
312 for (j = 0; j < TCRYPT_HDR_LEN; j++)
313 buf[j] ^= key[j % 8];
316 static void TCRYPT_copy_key(struct tcrypt_alg *alg, const char *mode,
317 char *out_key, const char *key)
320 if (!strncmp(mode, "xts", 3)) {
321 ks2 = alg->key_size / 2;
322 memcpy(out_key, &key[alg->key_offset], ks2);
323 memcpy(&out_key[ks2], &key[alg->iv_offset], ks2);
324 } else if (!strncmp(mode, "lrw", 3)) {
325 ks2 = alg->key_size - TCRYPT_LRW_IKEY_LEN;
326 memcpy(out_key, &key[alg->key_offset], ks2);
327 memcpy(&out_key[ks2], key, TCRYPT_LRW_IKEY_LEN);
328 } else if (!strncmp(mode, "cbc", 3)) {
329 memcpy(out_key, &key[alg->key_offset], alg->key_size);
331 memcpy(&out_key[alg->key_size], &key[alg->iv_offset],
332 alg->key_extra_size);
336 static int TCRYPT_decrypt_hdr_one(struct tcrypt_alg *alg, const char *mode,
337 const char *key,struct tcrypt_phdr *hdr)
339 char backend_key[TCRYPT_HDR_KEY_LEN];
340 char iv[TCRYPT_HDR_IV_LEN] = {};
341 char mode_name[MAX_CIPHER_LEN + 1];
342 struct crypt_cipher *cipher;
343 char *c, *buf = (char*)&hdr->e;
346 /* Remove IV if present */
347 mode_name[MAX_CIPHER_LEN] = '\0';
348 strncpy(mode_name, mode, MAX_CIPHER_LEN);
349 c = strchr(mode_name, '-');
353 if (!strncmp(mode, "lrw", 3))
354 iv[alg->iv_size - 1] = 1;
355 else if (!strncmp(mode, "cbc", 3)) {
356 TCRYPT_remove_whitening(buf, &key[8]);
357 if (!strcmp(alg->name, "blowfish_le"))
358 return decrypt_blowfish_le_cbc(alg, key, buf);
359 memcpy(iv, &key[alg->iv_offset], alg->iv_size);
362 TCRYPT_copy_key(alg, mode, backend_key, key);
363 r = crypt_cipher_init(&cipher, alg->name, mode_name,
364 backend_key, alg->key_size);
366 r = crypt_cipher_decrypt(cipher, buf, buf, TCRYPT_HDR_LEN,
368 crypt_cipher_destroy(cipher);
371 crypt_safe_memzero(backend_key, sizeof(backend_key));
372 crypt_safe_memzero(iv, TCRYPT_HDR_IV_LEN);
377 * For chained ciphers and CBC mode we need "outer" decryption.
378 * Backend doesn't provide this, so implement it here directly using ECB.
380 static int TCRYPT_decrypt_cbci(struct tcrypt_algs *ciphers,
381 const char *key, struct tcrypt_phdr *hdr)
383 struct crypt_cipher *cipher[ciphers->chain_count];
384 unsigned int bs = ciphers->cipher[0].iv_size;
385 char *buf = (char*)&hdr->e, iv[bs], iv_old[bs];
389 TCRYPT_remove_whitening(buf, &key[8]);
391 memcpy(iv, &key[ciphers->cipher[0].iv_offset], bs);
393 /* Initialize all ciphers in chain in ECB mode */
394 for (j = 0; j < ciphers->chain_count; j++)
396 for (j = 0; j < ciphers->chain_count; j++) {
397 r = crypt_cipher_init(&cipher[j], ciphers->cipher[j].name, "ecb",
398 &key[ciphers->cipher[j].key_offset],
399 ciphers->cipher[j].key_size);
404 /* Implements CBC with chained ciphers in loop inside */
405 for (i = 0; i < TCRYPT_HDR_LEN; i += bs) {
406 memcpy(iv_old, &buf[i], bs);
407 for (j = ciphers->chain_count; j > 0; j--) {
408 r = crypt_cipher_decrypt(cipher[j - 1], &buf[i], &buf[i],
413 for (j = 0; j < bs; j++)
415 memcpy(iv, iv_old, bs);
418 for (j = 0; j < ciphers->chain_count; j++)
420 crypt_cipher_destroy(cipher[j]);
422 crypt_safe_memzero(iv, bs);
423 crypt_safe_memzero(iv_old, bs);
427 static int TCRYPT_decrypt_hdr(struct crypt_device *cd, struct tcrypt_phdr *hdr,
428 const char *key, uint32_t flags)
430 struct tcrypt_phdr hdr2;
431 int i, j, r = -EINVAL;
433 for (i = 0; tcrypt_cipher[i].chain_count; i++) {
434 if (!(flags & CRYPT_TCRYPT_LEGACY_MODES) && tcrypt_cipher[i].legacy)
436 log_dbg(cd, "TCRYPT: trying cipher %s-%s",
437 tcrypt_cipher[i].long_name, tcrypt_cipher[i].mode);
439 memcpy(&hdr2.e, &hdr->e, TCRYPT_HDR_LEN);
441 if (!strncmp(tcrypt_cipher[i].mode, "cbci", 4))
442 r = TCRYPT_decrypt_cbci(&tcrypt_cipher[i], key, &hdr2);
443 else for (j = tcrypt_cipher[i].chain_count - 1; j >= 0 ; j--) {
444 if (!tcrypt_cipher[i].cipher[j].name)
446 r = TCRYPT_decrypt_hdr_one(&tcrypt_cipher[i].cipher[j],
447 tcrypt_cipher[i].mode, key, &hdr2);
453 log_dbg(cd, "TCRYPT: returned error %d, skipped.", r);
460 if (!strncmp(hdr2.d.magic, TCRYPT_HDR_MAGIC, TCRYPT_HDR_MAGIC_LEN)) {
461 log_dbg(cd, "TCRYPT: Signature magic detected.");
462 memcpy(&hdr->e, &hdr2.e, TCRYPT_HDR_LEN);
466 if ((flags & CRYPT_TCRYPT_VERA_MODES) &&
467 !strncmp(hdr2.d.magic, VCRYPT_HDR_MAGIC, TCRYPT_HDR_MAGIC_LEN)) {
468 log_dbg(cd, "TCRYPT: Signature magic detected (Veracrypt).");
469 memcpy(&hdr->e, &hdr2.e, TCRYPT_HDR_LEN);
476 crypt_safe_memzero(&hdr2, sizeof(hdr2));
480 static int TCRYPT_pool_keyfile(struct crypt_device *cd,
481 unsigned char pool[VCRYPT_KEY_POOL_LEN],
482 const char *keyfile, int keyfiles_pool_length)
485 int i, j, fd, data_size, r = -EIO;
488 log_dbg(cd, "TCRYPT: using keyfile %s.", keyfile);
490 data = malloc(TCRYPT_KEYFILE_LEN);
493 memset(data, 0, TCRYPT_KEYFILE_LEN);
495 fd = open(keyfile, O_RDONLY);
497 log_err(cd, _("Failed to open key file."));
501 data_size = read_buffer(fd, data, TCRYPT_KEYFILE_LEN);
504 log_err(cd, _("Error reading keyfile %s."), keyfile);
508 for (i = 0, j = 0, crc = ~0U; i < data_size; i++) {
509 crc = crypt_crc32(crc, &data[i], 1);
510 pool[j++] += (unsigned char)(crc >> 24);
511 pool[j++] += (unsigned char)(crc >> 16);
512 pool[j++] += (unsigned char)(crc >> 8);
513 pool[j++] += (unsigned char)(crc);
514 j %= keyfiles_pool_length;
518 crypt_safe_memzero(&crc, sizeof(crc));
519 crypt_safe_memzero(data, TCRYPT_KEYFILE_LEN);
525 static int TCRYPT_init_hdr(struct crypt_device *cd,
526 struct tcrypt_phdr *hdr,
527 struct crypt_params_tcrypt *params)
529 unsigned char pwd[VCRYPT_KEY_POOL_LEN] = {};
530 size_t passphrase_size, max_passphrase_size;
532 unsigned int i, skipped = 0, iterations;
533 int r = -EPERM, keyfiles_pool_length;
535 if (posix_memalign((void*)&key, crypt_getpagesize(), TCRYPT_HDR_KEY_LEN))
538 if (params->flags & CRYPT_TCRYPT_VERA_MODES &&
539 params->passphrase_size > TCRYPT_KEY_POOL_LEN) {
540 /* Really. Keyfile pool length depends on passphrase size in Veracrypt. */
541 max_passphrase_size = VCRYPT_KEY_POOL_LEN;
542 keyfiles_pool_length = VCRYPT_KEY_POOL_LEN;
544 max_passphrase_size = TCRYPT_KEY_POOL_LEN;
545 keyfiles_pool_length = TCRYPT_KEY_POOL_LEN;
548 if (params->keyfiles_count)
549 passphrase_size = max_passphrase_size;
551 passphrase_size = params->passphrase_size;
553 if (params->passphrase_size > max_passphrase_size) {
554 log_err(cd, _("Maximum TCRYPT passphrase length (%zu) exceeded."),
555 max_passphrase_size);
559 /* Calculate pool content from keyfiles */
560 for (i = 0; i < params->keyfiles_count; i++) {
561 r = TCRYPT_pool_keyfile(cd, pwd, params->keyfiles[i], keyfiles_pool_length);
566 /* If provided password, combine it with pool */
567 for (i = 0; i < params->passphrase_size; i++)
568 pwd[i] += params->passphrase[i];
570 for (i = 0; tcrypt_kdf[i].name; i++) {
571 if (!(params->flags & CRYPT_TCRYPT_LEGACY_MODES) && tcrypt_kdf[i].legacy)
573 if (!(params->flags & CRYPT_TCRYPT_VERA_MODES) && tcrypt_kdf[i].veracrypt)
575 if ((params->flags & CRYPT_TCRYPT_VERA_MODES) && params->veracrypt_pim) {
576 /* Do not try TrueCrypt modes if we have PIM value */
577 if (!tcrypt_kdf[i].veracrypt)
579 /* adjust iterations to given PIM cmdline parameter */
580 iterations = tcrypt_kdf[i].veracrypt_pim_const +
581 (tcrypt_kdf[i].veracrypt_pim_mult * params->veracrypt_pim);
583 iterations = tcrypt_kdf[i].iterations;
585 /* Derive header key */
586 log_dbg(cd, "TCRYPT: trying KDF: %s-%s-%d%s.",
587 tcrypt_kdf[i].name, tcrypt_kdf[i].hash, tcrypt_kdf[i].iterations,
588 params->veracrypt_pim && tcrypt_kdf[i].veracrypt ? "-PIM" : "");
589 r = crypt_pbkdf(tcrypt_kdf[i].name, tcrypt_kdf[i].hash,
590 (char*)pwd, passphrase_size,
591 hdr->salt, TCRYPT_HDR_SALT_LEN,
592 key, TCRYPT_HDR_KEY_LEN,
595 log_verbose(cd, _("PBKDF2 hash algorithm %s not available, skipping."),
601 r = TCRYPT_decrypt_hdr(cd, hdr, key, params->flags);
610 if ((r < 0 && r != -EPERM && skipped && skipped == i) || r == -ENOTSUP) {
611 log_err(cd, _("Required kernel crypto interface not available."));
613 log_err(cd, _("Ensure you have algif_skcipher kernel module loaded."));
619 r = TCRYPT_hdr_from_disk(cd, hdr, params, i, r);
621 log_dbg(cd, "TCRYPT: Magic: %s, Header version: %d, req. %d, sector %d"
622 ", mk_offset %" PRIu64 ", hidden_size %" PRIu64
623 ", volume size %" PRIu64, tcrypt_kdf[i].veracrypt ?
624 VCRYPT_HDR_MAGIC : TCRYPT_HDR_MAGIC,
625 (int)hdr->d.version, (int)hdr->d.version_tc, (int)hdr->d.sector_size,
626 hdr->d.mk_offset, hdr->d.hidden_volume_size, hdr->d.volume_size);
627 log_dbg(cd, "TCRYPT: Header cipher %s-%s, key size %zu",
628 params->cipher, params->mode, params->key_size);
631 crypt_safe_memzero(pwd, TCRYPT_KEY_POOL_LEN);
633 crypt_safe_memzero(key, TCRYPT_HDR_KEY_LEN);
638 int TCRYPT_read_phdr(struct crypt_device *cd,
639 struct tcrypt_phdr *hdr,
640 struct crypt_params_tcrypt *params)
642 struct device *base_device = NULL, *device = crypt_metadata_device(cd);
643 ssize_t hdr_size = sizeof(struct tcrypt_phdr);
644 char *base_device_path;
647 assert(sizeof(struct tcrypt_phdr) == 512);
649 log_dbg(cd, "Reading TCRYPT header of size %zu bytes from device %s.",
650 hdr_size, device_path(device));
652 if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER &&
653 crypt_dev_is_partition(device_path(device))) {
654 base_device_path = crypt_get_base_device(device_path(device));
656 log_dbg(cd, "Reading TCRYPT system header from device %s.", base_device_path ?: "?");
657 if (!base_device_path)
660 r = device_alloc(cd, &base_device, base_device_path);
661 free(base_device_path);
664 devfd = device_open(cd, base_device, O_RDONLY);
666 devfd = device_open(cd, device, O_RDONLY);
669 device_free(cd, base_device);
670 log_err(cd, _("Cannot open device %s."), device_path(device));
675 if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER) {
676 if (read_lseek_blockwise(devfd, device_block_size(cd, device),
677 device_alignment(device), hdr, hdr_size,
678 TCRYPT_HDR_SYSTEM_OFFSET) == hdr_size) {
679 r = TCRYPT_init_hdr(cd, hdr, params);
681 } else if (params->flags & CRYPT_TCRYPT_HIDDEN_HEADER) {
682 if (params->flags & CRYPT_TCRYPT_BACKUP_HEADER) {
683 if (read_lseek_blockwise(devfd, device_block_size(cd, device),
684 device_alignment(device), hdr, hdr_size,
685 TCRYPT_HDR_HIDDEN_OFFSET_BCK) == hdr_size)
686 r = TCRYPT_init_hdr(cd, hdr, params);
688 if (read_lseek_blockwise(devfd, device_block_size(cd, device),
689 device_alignment(device), hdr, hdr_size,
690 TCRYPT_HDR_HIDDEN_OFFSET) == hdr_size)
691 r = TCRYPT_init_hdr(cd, hdr, params);
692 if (r && read_lseek_blockwise(devfd, device_block_size(cd, device),
693 device_alignment(device), hdr, hdr_size,
694 TCRYPT_HDR_HIDDEN_OFFSET_OLD) == hdr_size)
695 r = TCRYPT_init_hdr(cd, hdr, params);
697 } else if (params->flags & CRYPT_TCRYPT_BACKUP_HEADER) {
698 if (read_lseek_blockwise(devfd, device_block_size(cd, device),
699 device_alignment(device), hdr, hdr_size,
700 TCRYPT_HDR_OFFSET_BCK) == hdr_size)
701 r = TCRYPT_init_hdr(cd, hdr, params);
702 } else if (read_lseek_blockwise(devfd, device_block_size(cd, device),
703 device_alignment(device), hdr, hdr_size, 0) == hdr_size)
704 r = TCRYPT_init_hdr(cd, hdr, params);
706 device_free(cd, base_device);
708 memset(hdr, 0, sizeof (*hdr));
712 static struct tcrypt_algs *TCRYPT_get_algs(const char *cipher, const char *mode)
716 if (!cipher || !mode)
719 for (i = 0; tcrypt_cipher[i].chain_count; i++)
720 if (!strcmp(tcrypt_cipher[i].long_name, cipher) &&
721 !strcmp(tcrypt_cipher[i].mode, mode))
722 return &tcrypt_cipher[i];
727 int TCRYPT_activate(struct crypt_device *cd,
729 struct tcrypt_phdr *hdr,
730 struct crypt_params_tcrypt *params,
733 char dm_name[PATH_MAX], dm_dev_name[PATH_MAX], cipher_spec[MAX_CIPHER_LEN*2+1];
737 uint32_t req_flags, dmc_flags;
738 struct tcrypt_algs *algs;
739 enum devcheck device_check;
740 uint64_t offset = crypt_get_data_offset(cd);
741 struct volume_key *vk = NULL;
742 struct device *ptr_dev = crypt_data_device(cd), *device = NULL, *part_device = NULL;
743 struct crypt_dm_active_device dmd = {
747 if (!hdr->d.version) {
748 log_dbg(cd, "TCRYPT: this function is not supported without encrypted header load.");
752 if (hdr->d.sector_size && hdr->d.sector_size != SECTOR_SIZE) {
753 log_err(cd, _("Activation is not supported for %d sector size."),
758 if (strstr(params->mode, "-tcrypt")) {
759 log_err(cd, _("Kernel does not support activation for this TCRYPT legacy mode."));
763 if (strstr(params->mode, "-tcw"))
764 req_flags = DM_TCW_SUPPORTED;
766 req_flags = DM_PLAIN64_SUPPORTED;
768 algs = TCRYPT_get_algs(params->cipher, params->mode);
772 if (hdr->d.sector_size == 0)
775 if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER)
777 else if (params->flags & CRYPT_TCRYPT_HIDDEN_HEADER)
778 dmd.size = hdr->d.hidden_volume_size / hdr->d.sector_size;
780 dmd.size = hdr->d.volume_size / hdr->d.sector_size;
782 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
783 device_check = DEV_OK;
785 device_check = DEV_EXCL;
787 if ((params->flags & CRYPT_TCRYPT_SYSTEM_HEADER) &&
788 !crypt_dev_is_partition(device_path(crypt_data_device(cd)))) {
789 part_path = crypt_get_partition_device(device_path(crypt_data_device(cd)),
790 crypt_get_data_offset(cd), dmd.size);
792 if (!device_alloc(cd, &part_device, part_path)) {
793 log_verbose(cd, _("Activating TCRYPT system encryption for partition %s."),
795 ptr_dev = part_device;
801 * System encryption use the whole device mapping, there can
802 * be active partitions.
804 device_check = DEV_OK;
807 r = device_block_adjust(cd, ptr_dev, device_check,
808 offset, &dmd.size, &dmd.flags);
812 /* From here, key size for every cipher must be the same */
813 vk = crypt_alloc_volume_key(algs->cipher[0].key_size +
814 algs->cipher[0].key_extra_size, NULL);
820 for (i = algs->chain_count; i > 0; i--) {
822 dm_name[sizeof(dm_name)-1] = '\0';
823 strncpy(dm_name, name, sizeof(dm_name)-1);
826 snprintf(dm_name, sizeof(dm_name), "%s_%d", name, i-1);
827 dmd.flags = flags | CRYPT_ACTIVATE_PRIVATE;
830 TCRYPT_copy_key(&algs->cipher[i-1], algs->mode,
831 vk->key, hdr->d.keys);
833 if (algs->chain_count != i) {
834 snprintf(dm_dev_name, sizeof(dm_dev_name), "%s/%s_%d",
835 dm_get_dir(), name, i);
836 r = device_alloc(cd, &device, dm_dev_name);
843 r = snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", algs->cipher[i-1].name, algs->mode);
844 if (r < 0 || (size_t)r >= sizeof(cipher_spec)) {
849 r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, ptr_dev, vk,
850 cipher_spec, crypt_get_iv_offset(cd), offset,
851 crypt_get_integrity(cd),
852 crypt_get_integrity_tag_size(cd),
853 crypt_get_sector_size(cd));
857 log_dbg(cd, "Trying to activate TCRYPT device %s using cipher %s.",
858 dm_name, dmd.segment.u.crypt.cipher);
859 r = dm_create_device(cd, dm_name, CRYPT_TCRYPT, &dmd);
861 dm_targets_free(cd, &dmd);
862 device_free(cd, device);
870 (dm_flags(cd, DM_CRYPT, &dmc_flags) || ((dmc_flags & req_flags) != req_flags))) {
871 log_err(cd, _("Kernel does not support TCRYPT compatible mapping."));
876 crypt_free_volume_key(vk);
877 device_free(cd, device);
878 device_free(cd, part_device);
882 static int TCRYPT_remove_one(struct crypt_device *cd, const char *name,
883 const char *base_uuid, int index, uint32_t flags)
885 struct crypt_dm_active_device dmd;
886 char dm_name[PATH_MAX];
889 if (snprintf(dm_name, sizeof(dm_name), "%s_%d", name, index) < 0)
892 r = dm_status_device(cd, dm_name);
896 r = dm_query_device(cd, dm_name, DM_ACTIVE_UUID, &dmd);
897 if (!r && !strncmp(dmd.uuid, base_uuid, strlen(base_uuid)))
898 r = dm_remove_device(cd, dm_name, flags);
900 free(CONST_CAST(void*)dmd.uuid);
904 int TCRYPT_deactivate(struct crypt_device *cd, const char *name, uint32_t flags)
906 struct crypt_dm_active_device dmd;
909 r = dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd);
915 r = dm_remove_device(cd, name, flags);
919 r = TCRYPT_remove_one(cd, name, dmd.uuid, 1, flags);
923 r = TCRYPT_remove_one(cd, name, dmd.uuid, 2, flags);
925 free(CONST_CAST(void*)dmd.uuid);
926 return (r == -ENODEV) ? 0 : r;
929 static int TCRYPT_status_one(struct crypt_device *cd, const char *name,
930 const char *base_uuid, int index,
931 size_t *key_size, char *cipher,
932 struct tcrypt_phdr *tcrypt_hdr,
933 struct device **device)
935 struct crypt_dm_active_device dmd;
936 struct dm_target *tgt = &dmd.segment;
937 char dm_name[PATH_MAX], *c;
940 if (snprintf(dm_name, sizeof(dm_name), "%s_%d", name, index) < 0)
943 r = dm_status_device(cd, dm_name);
947 r = dm_query_device(cd, dm_name, DM_ACTIVE_DEVICE |
949 DM_ACTIVE_CRYPT_CIPHER |
950 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
953 if (!single_segment(&dmd) || tgt->type != DM_CRYPT) {
960 if (!strncmp(dmd.uuid, base_uuid, strlen(base_uuid))) {
961 if ((c = strchr(tgt->u.crypt.cipher, '-')))
964 strncat(cipher, tgt->u.crypt.cipher, MAX_CIPHER_LEN);
965 *key_size += tgt->u.crypt.vk->keylength;
966 tcrypt_hdr->d.mk_offset = tgt->u.crypt.offset * SECTOR_SIZE;
967 device_free(cd, *device);
968 MOVE_REF(*device, tgt->data_device);
972 dm_targets_free(cd, &dmd);
973 free(CONST_CAST(void*)dmd.uuid);
977 int TCRYPT_init_by_name(struct crypt_device *cd, const char *name,
979 const struct dm_target *tgt,
980 struct device **device,
981 struct crypt_params_tcrypt *tcrypt_params,
982 struct tcrypt_phdr *tcrypt_hdr)
984 struct tcrypt_algs *algs;
985 char cipher[MAX_CIPHER_LEN * 4], mode[MAX_CIPHER_LEN+1], *tmp;
989 memset(tcrypt_params, 0, sizeof(*tcrypt_params));
990 memset(tcrypt_hdr, 0, sizeof(*tcrypt_hdr));
991 tcrypt_hdr->d.sector_size = SECTOR_SIZE;
992 tcrypt_hdr->d.mk_offset = tgt->u.crypt.offset * SECTOR_SIZE;
994 strncpy(cipher, tgt->u.crypt.cipher, MAX_CIPHER_LEN);
995 tmp = strchr(cipher, '-');
999 mode[MAX_CIPHER_LEN] = '\0';
1000 strncpy(mode, ++tmp, MAX_CIPHER_LEN);
1002 key_size = tgt->u.crypt.vk->keylength;
1003 r = TCRYPT_status_one(cd, name, uuid, 1, &key_size,
1004 cipher, tcrypt_hdr, device);
1006 r = TCRYPT_status_one(cd, name, uuid, 2, &key_size,
1007 cipher, tcrypt_hdr, device);
1009 if (r < 0 && r != -ENODEV)
1012 algs = TCRYPT_get_algs(cipher, mode);
1013 if (!algs || key_size != algs->chain_key_size)
1016 tcrypt_params->key_size = algs->chain_key_size;
1017 tcrypt_params->cipher = algs->long_name;
1018 tcrypt_params->mode = algs->mode;
1022 uint64_t TCRYPT_get_data_offset(struct crypt_device *cd,
1023 struct tcrypt_phdr *hdr,
1024 struct crypt_params_tcrypt *params)
1028 /* No real header loaded, initialized by active device */
1029 if (!hdr->d.version)
1032 /* Mapping through whole device, not partition! */
1033 if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER) {
1034 if (crypt_dev_is_partition(device_path(crypt_metadata_device(cd))))
1039 if (params->mode && !strncmp(params->mode, "xts", 3)) {
1040 if (hdr->d.version < 3)
1043 if (params->flags & CRYPT_TCRYPT_HIDDEN_HEADER) {
1044 if (hdr->d.version > 3)
1045 return (hdr->d.mk_offset / hdr->d.sector_size);
1046 if (device_size(crypt_metadata_device(cd), &size) < 0)
1048 return (size - hdr->d.hidden_volume_size +
1049 (TCRYPT_HDR_HIDDEN_OFFSET_OLD)) / hdr->d.sector_size;
1054 if (params->flags & CRYPT_TCRYPT_HIDDEN_HEADER) {
1055 if (device_size(crypt_metadata_device(cd), &size) < 0)
1057 return (size - hdr->d.hidden_volume_size +
1058 (TCRYPT_HDR_HIDDEN_OFFSET_OLD)) / hdr->d.sector_size;
1062 return hdr->d.mk_offset / hdr->d.sector_size;
1065 uint64_t TCRYPT_get_iv_offset(struct crypt_device *cd,
1066 struct tcrypt_phdr *hdr,
1067 struct crypt_params_tcrypt *params)
1071 if (params->mode && !strncmp(params->mode, "xts", 3))
1072 iv_offset = TCRYPT_get_data_offset(cd, hdr, params);
1073 else if (params->mode && !strncmp(params->mode, "lrw", 3))
1076 iv_offset = hdr->d.mk_offset / hdr->d.sector_size;
1078 if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER)
1079 iv_offset += crypt_dev_partition_offset(device_path(crypt_metadata_device(cd)));
1084 int TCRYPT_get_volume_key(struct crypt_device *cd,
1085 struct tcrypt_phdr *hdr,
1086 struct crypt_params_tcrypt *params,
1087 struct volume_key **vk)
1089 struct tcrypt_algs *algs;
1090 unsigned int i, key_index;
1092 if (!hdr->d.version) {
1093 log_err(cd, _("This function is not supported without TCRYPT header load."));
1097 algs = TCRYPT_get_algs(params->cipher, params->mode);
1101 *vk = crypt_alloc_volume_key(params->key_size, NULL);
1105 for (i = 0, key_index = 0; i < algs->chain_count; i++) {
1106 TCRYPT_copy_key(&algs->cipher[i], algs->mode,
1107 &(*vk)->key[key_index], hdr->d.keys);
1108 key_index += algs->cipher[i].key_size;
1114 int TCRYPT_dump(struct crypt_device *cd,
1115 struct tcrypt_phdr *hdr,
1116 struct crypt_params_tcrypt *params)
1118 log_std(cd, "%s header information for %s\n",
1119 hdr->d.magic[0] == 'T' ? "TCRYPT" : "VERACRYPT",
1120 device_path(crypt_metadata_device(cd)));
1121 if (hdr->d.version) {
1122 log_std(cd, "Version: \t%d\n", hdr->d.version);
1123 log_std(cd, "Driver req.:\t%x.%x\n", hdr->d.version_tc >> 8,
1124 hdr->d.version_tc & 0xFF);
1126 log_std(cd, "Sector size:\t%" PRIu32 "\n", hdr->d.sector_size);
1127 log_std(cd, "MK offset:\t%" PRIu64 "\n", hdr->d.mk_offset);
1128 log_std(cd, "PBKDF2 hash:\t%s\n", params->hash_name);
1130 log_std(cd, "Cipher chain:\t%s\n", params->cipher);
1131 log_std(cd, "Cipher mode:\t%s\n", params->mode);
1132 log_std(cd, "MK bits: \t%zu\n", params->key_size * 8);