2 * TCRYPT compatible volume handling
4 * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2012, Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * 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;
37 unsigned int iterations;
39 { 0, "pbkdf2", "ripemd160", 2000 },
40 { 0, "pbkdf2", "ripemd160", 1000 },
41 { 0, "pbkdf2", "sha512", 1000 },
42 { 0, "pbkdf2", "whirlpool", 1000 },
43 { 1, "pbkdf2", "sha1", 2000 },
49 unsigned int key_size;
53 /* TCRYPT cipher variants */
55 unsigned int legacy:1;
57 struct tcrypt_alg cipher[3];
59 { 0, "xts-plain64",{{"aes", 64,16}}},
60 { 0, "xts-plain64",{{"serpent",64,16}}},
61 { 0, "xts-plain64",{{"twofish",64,16}}},
62 { 0, "xts-plain64",{{"twofish",64,16},{"aes", 64,16}}},
63 { 0, "xts-plain64",{{"serpent",64,16},{"twofish",64,16},{"aes", 64,16}}},
64 { 0, "xts-plain64",{{"aes", 64,16},{"serpent",64,16}}},
65 { 0, "xts-plain64",{{"aes", 64,16},{"twofish",64,16},{"serpent",64,16}}},
66 { 0, "xts-plain64",{{"serpent",64,16},{"twofish",64,16}}},
68 { 0, "lrw-benbi", {{"aes", 48,16}}},
69 { 0, "lrw-benbi", {{"serpent",48,16}}},
70 { 0, "lrw-benbi", {{"twofish",48,16}}},
71 { 0, "lrw-benbi", {{"twofish",48,16},{"aes", 48,16}}},
72 { 0, "lrw-benbi", {{"serpent",48,16},{"twofish",48,16},{"aes", 48,16}}},
73 { 0, "lrw-benbi", {{"aes", 48,16},{"serpent",48,16}}},
74 { 0, "lrw-benbi", {{"aes", 48,16},{"twofish",48,16},{"serpent",48,16}}},
75 { 0, "lrw-benbi", {{"serpent",48,16},{"twofish",48,16}}},
77 { 1, "cbc-tcrypt", {{"aes", 32,16}}},
78 { 1, "cbc-tcrypt", {{"serpent",32,16}}},
79 { 1, "cbc-tcrypt", {{"twofish",32,16}}},
80 { 1, "cbci-tcrypt",{{"twofish",32,16},{"aes", 32,16}}},
81 { 1, "cbci-tcrypt",{{"serpent",32,16},{"twofish",32,16},{"aes", 32,16}}},
82 { 1, "cbci-tcrypt",{{"aes", 32,16},{"serpent",32,16}}},
83 { 1, "cbci-tcrypt",{{"aes", 32,16},{"twofish",32,16},{"serpent",32,16}}},
84 { 1, "cbci-tcrypt",{{"serpent",32,16},{"twofish",32,16}}},
86 { 1, "cbc-tcrypt", {{"cast5", 16,8}}},
87 { 1, "cbc-tcrypt", {{"des3_ede",24,8}}},
89 // kernel LRW block size is fixed to 16 bytes
90 // thus cannot be used with blowfish where block is 8 bytes
91 //{ 1,"lrw-benbi",{{"blowfish",64,8}}},
92 //{ 1,"lrw-benbi",{{"blowfish",64,8},{"aes",48,16}}},
93 //{ 1,"lrw-benbi",{{"serpent",48,16},{"blowfish",64,8},{"aes",48,16}}},
95 // FIXME: why this doesn't work (blowfish key wrong)?
96 //{ 1,"cbc-tcrypt",{{"blowfish",56,8}}},
97 //{ 1,"cbc-tcrypt",{{"blowfish",56,8},{"aes",32,16}}},
98 //{ 1,"cbc-tcrypt",{{"serpent",32,16},{"blowfish",56,8},{"aes",32,16}}},
102 static void hdr_info(struct crypt_device *cd, struct tcrypt_phdr *hdr,
103 struct crypt_params_tcrypt *params)
105 log_dbg("Version: %d, required %d", (int)hdr->d.version, (int)hdr->d.version_tc);
107 log_dbg("Hidden size: %" PRIu64, hdr->d.hidden_volume_size);
108 log_dbg("Volume size: %" PRIu64, hdr->d.volume_size);
110 log_dbg("Sector size: %" PRIu64, hdr->d.sector_size);
111 log_dbg("Flags: %d", (int)hdr->d.flags);
112 log_dbg("MK: offset %d, size %d", (int)hdr->d.mk_offset, (int)hdr->d.mk_size);
113 log_dbg("KDF: PBKDF2, hash %s", params->hash_name);
114 log_dbg("Cipher: %s%s%s%s%s-%s",
116 params->cipher[1] ? "-" : "", params->cipher[1] ?: "",
117 params->cipher[2] ? "-" : "", params->cipher[2] ?: "",
121 static int hdr_from_disk(struct tcrypt_phdr *hdr,
122 struct crypt_params_tcrypt *params,
123 int kdf_index, int cipher_index)
128 /* Check CRC32 of header */
129 size = TCRYPT_HDR_LEN - sizeof(hdr->d.keys) - sizeof(hdr->d.header_crc32);
130 crc32 = crypt_crc32(~0, (unsigned char*)&hdr->d, size) ^ ~0;
131 if (be16_to_cpu(hdr->d.version) > 3 &&
132 crc32 != be32_to_cpu(hdr->d.header_crc32)) {
133 log_dbg("TCRYPT header CRC32 mismatch.");
137 /* Check CRC32 of keys */
138 crc32 = crypt_crc32(~0, (unsigned char*)hdr->d.keys, sizeof(hdr->d.keys)) ^ ~0;
139 if (crc32 != be32_to_cpu(hdr->d.keys_crc32)) {
140 log_dbg("TCRYPT keys CRC32 mismatch.");
144 /* Convert header to cpu format */
145 hdr->d.version = be16_to_cpu(hdr->d.version);
146 hdr->d.version_tc = le16_to_cpu(hdr->d.version_tc); // ???
148 hdr->d.keys_crc32 = be32_to_cpu(hdr->d.keys_crc32);
150 hdr->d.hidden_volume_size = be64_to_cpu(hdr->d.hidden_volume_size);
151 hdr->d.volume_size = be64_to_cpu(hdr->d.volume_size);
153 hdr->d.mk_offset = be64_to_cpu(hdr->d.mk_offset);
154 if (!hdr->d.mk_offset)
155 hdr->d.mk_offset = 512;
157 hdr->d.mk_size = be64_to_cpu(hdr->d.mk_size);
159 hdr->d.flags = be32_to_cpu(hdr->d.flags);
161 hdr->d.sector_size = be32_to_cpu(hdr->d.sector_size);
162 if (!hdr->d.sector_size)
163 hdr->d.sector_size = 512;
165 hdr->d.header_crc32 = be32_to_cpu(hdr->d.header_crc32);
168 params->passphrase = NULL;
169 params->passphrase_size = 0;
171 params->hash_name = tcrypt_kdf[kdf_index].hash;
173 params->cipher[0] = tcrypt_cipher[cipher_index].cipher[0].name;
174 params->cipher[1] = tcrypt_cipher[cipher_index].cipher[1].name;
175 params->cipher[2] = tcrypt_cipher[cipher_index].cipher[2].name;
176 params->mode = tcrypt_cipher[cipher_index].mode;
177 params->key_size = tcrypt_cipher[cipher_index].cipher[0].key_size; //fixme
182 static int decrypt_hdr_one(const char *name, const char *mode,
183 const char *key, size_t key_size,
184 size_t iv_size, struct tcrypt_phdr *hdr)
186 char iv[TCRYPT_HDR_IV_LEN] = {};
187 char mode_name[MAX_CIPHER_LEN];
188 struct crypt_cipher *cipher;
189 char *c, *buf = (char*)&hdr->e;
192 /* Remove IV if present */
193 strncpy(mode_name, mode, MAX_CIPHER_LEN);
194 c = strchr(mode_name, '-');
198 if (!strncmp(mode, "lrw", 3))
200 else if (!strncmp(mode, "cbc", 3))
201 memcpy(iv, &key[key_size], iv_size);
203 r = crypt_cipher_init(&cipher, name, mode_name, key, key_size);
207 r = crypt_cipher_decrypt(cipher, buf, buf, TCRYPT_HDR_LEN, iv, iv_size);
208 crypt_cipher_destroy(cipher);
213 static void copy_key(char *out_key, const char *key, int key_num,
214 int ks, int ki, const char *mode)
216 if (!strncmp(mode, "xts", 3)) {
218 memcpy(out_key, &key[ks2 * ki], ks2);
219 memcpy(&out_key[ks2], &key[ks2 * (++key_num + ki)], ks2);
220 } else if (!strncmp(mode, "lrw", 3)) {
221 /* First is LRW index key */
223 ks -= TCRYPT_LRW_IKEY_LEN;
224 memcpy(out_key, &key[ks * ki], ks);
225 memcpy(&out_key[ks * ki], key, TCRYPT_LRW_IKEY_LEN);
226 } else if (!strncmp(mode, "cbc", 3)) {
228 memcpy(out_key, &key[ki * 32], ks);
229 memcpy(&out_key[ks], key, 32);
234 * For chanined ciphers and CBC mode we need "inner" decryption.
235 * Backend doesn't provide this, so implement it here directly using ECB.
237 static int decrypt_hdr_cbci(struct tcrypt_alg ciphers[3],
238 const char *key, struct tcrypt_phdr *hdr)
240 struct crypt_cipher *cipher[3] = {};
241 int bs = ciphers[0].iv_size;
242 char *buf = (char*)&hdr->e, iv[bs], iv_old[bs];
247 /* Initialize all ciphers in chain in ECB mode */
248 for (j = 0; j < 3; j++) {
249 if (!ciphers[j].name)
251 r = crypt_cipher_init(&cipher[j], ciphers[j].name, "ecb",
252 &key[(j+1)*32], ciphers[j].key_size);
257 /* Implements CBC with chained ciphers in inner loop */
258 for (i = 0; i < TCRYPT_HDR_LEN; i += bs) {
259 memcpy(iv_old, &buf[i], bs);
260 for (j = 2; j >= 0; j--) {
263 r = crypt_cipher_decrypt(cipher[j], &buf[i], &buf[i],
268 for (j = 0; j < bs; j++)
270 memcpy(iv, iv_old, bs);
273 for (j = 0; j < 3; j++)
275 crypt_cipher_destroy(cipher[j]);
280 static int top_cipher(struct tcrypt_alg cipher[3])
291 static int decrypt_hdr(struct crypt_device *cd, struct tcrypt_phdr *hdr,
292 const char *key, int legacy_modes)
294 char one_key[TCRYPT_HDR_KEY_LEN];
295 struct tcrypt_phdr hdr2;
298 for (i = 0; tcrypt_cipher[i].cipher[0].name; i++) {
299 if (!legacy_modes && tcrypt_cipher[i].legacy)
301 log_dbg("TCRYPT: trying cipher: %s%s%s%s%s-%s.",
302 tcrypt_cipher[i].cipher[0].name,
303 tcrypt_cipher[i].cipher[1].name ? "-" : "", tcrypt_cipher[i].cipher[1].name ?: "",
304 tcrypt_cipher[i].cipher[2].name ? "-" : "", tcrypt_cipher[i].cipher[2].name ?: "",
305 tcrypt_cipher[i].mode);
307 memcpy(&hdr2.e, &hdr->e, TCRYPT_HDR_LEN);
309 /* Remove CBC whitening */
310 if (!strncmp(tcrypt_cipher[i].mode, "cbc", 3)) {
311 char *buf = (char*)&hdr2.e;
312 for (j = 0; j < TCRYPT_HDR_LEN; j++)
313 buf[j] ^= key[8 + j % 8];
316 /* For chained (inner) CBC we do not have API support */
317 if (!strncmp(tcrypt_cipher[i].mode, "cbci", 4))
318 r = decrypt_hdr_cbci(tcrypt_cipher[i].cipher, key, &hdr2);
319 else for (j = 2; j >= 0 ; j--) {
320 if (!tcrypt_cipher[i].cipher[j].name)
322 copy_key(one_key, key, top_cipher(tcrypt_cipher[i].cipher),
323 tcrypt_cipher[i].cipher[j].key_size,
324 j, tcrypt_cipher[i].mode);
325 r = decrypt_hdr_one(tcrypt_cipher[i].cipher[j].name,
326 tcrypt_cipher[i].mode, one_key,
327 tcrypt_cipher[i].cipher[j].key_size,
328 tcrypt_cipher[i].cipher[j].iv_size, &hdr2);
330 log_dbg("Error %s.", tcrypt_cipher[i].cipher[j].name);
335 if (!strncmp(hdr2.d.magic, TCRYPT_HDR_MAGIC, TCRYPT_HDR_MAGIC_LEN)) {
336 log_dbg("TCRYPT: Signature magic detected.");
337 memcpy(&hdr->e, &hdr2.e, TCRYPT_HDR_LEN);
338 memset(&hdr2.e, 0, TCRYPT_HDR_LEN);
345 memset(one_key, 0, sizeof(*one_key));
349 static int pool_keyfile(struct crypt_device *cd,
350 unsigned char pool[TCRYPT_KEY_POOL_LEN],
353 unsigned char data[TCRYPT_KEYFILE_LEN];
354 int i, j, fd, data_size;
356 unsigned char *crc_c = (unsigned char*)&crc;
358 log_dbg("TCRYPT: using keyfile %s.", keyfile);
360 fd = open(keyfile, O_RDONLY);
362 log_err(cd, _("Failed to open key file.\n"));
366 /* FIXME: add while */
367 data_size = read(fd, data, TCRYPT_KEYFILE_LEN);
370 log_err(cd, _("Error reading keyfile %s.\n"), keyfile);
374 for (i = 0, j = 0, crc = ~0U; i < data_size; i++) {
375 crc = crypt_crc32(crc, &data[i], 1);
376 pool[j++] += crc_c[3];
377 pool[j++] += crc_c[2];
378 pool[j++] += crc_c[1];
379 pool[j++] += crc_c[0];
380 j %= TCRYPT_KEY_POOL_LEN;
384 memset(data, 0, TCRYPT_KEYFILE_LEN);
389 static int TCRYPT_init_hdr(struct crypt_device *cd,
390 struct tcrypt_phdr *hdr,
391 struct crypt_params_tcrypt *params)
393 unsigned char pwd[TCRYPT_KEY_POOL_LEN] = {};
394 size_t passphrase_size;
396 int r, i, legacy_modes;
398 if (posix_memalign((void*)&key, crypt_getpagesize(), TCRYPT_HDR_KEY_LEN))
401 if (params->keyfiles_count)
402 passphrase_size = TCRYPT_KEY_POOL_LEN;
404 passphrase_size = params->passphrase_size;
406 /* Calculate pool content from keyfiles */
407 for (i = 0; i < params->keyfiles_count; i++) {
408 r = pool_keyfile(cd, pwd, params->keyfiles[i]);
413 /* If provided password, combine it with pool */
414 for (i = 0; i < params->passphrase_size; i++)
415 pwd[i] += params->passphrase[i];
417 legacy_modes = params->flags & CRYPT_TCRYPT_LEGACY_MODES ? 1 : 0;
418 for (i = 0; tcrypt_kdf[i].name; i++) {
419 if (!legacy_modes && tcrypt_kdf[i].legacy)
421 /* Derive header key */
422 log_dbg("TCRYPT: trying KDF: %s-%s-%d.",
423 tcrypt_kdf[i].name, tcrypt_kdf[i].hash, tcrypt_kdf[i].iterations);
424 r = crypt_pbkdf(tcrypt_kdf[i].name, tcrypt_kdf[i].hash,
425 (char*)pwd, passphrase_size,
426 hdr->salt, TCRYPT_HDR_SALT_LEN,
427 key, TCRYPT_HDR_KEY_LEN,
428 tcrypt_kdf[i].iterations);
433 r = decrypt_hdr(cd, hdr, key, legacy_modes);
441 r = hdr_from_disk(hdr, params, i, r);
445 hdr_info(cd, hdr, params);
447 memset(pwd, 0, TCRYPT_KEY_POOL_LEN);
449 memset(key, 0, TCRYPT_HDR_KEY_LEN);
454 int TCRYPT_read_phdr(struct crypt_device *cd,
455 struct tcrypt_phdr *hdr,
456 struct crypt_params_tcrypt *params)
458 struct device *device = crypt_metadata_device(cd);
459 ssize_t hdr_size = sizeof(struct tcrypt_phdr);
462 assert(sizeof(struct tcrypt_phdr) == 512);
464 log_dbg("Reading TCRYPT header of size %d bytes from device %s.",
465 hdr_size, device_path(device));
467 devfd = open(device_path(device), O_RDONLY | O_DIRECT);
469 log_err(cd, _("Cannot open device %s.\n"), device_path(device));
473 if ((params->flags & CRYPT_TCRYPT_HIDDEN_HEADER) &&
474 lseek(devfd, TCRYPT_HDR_HIDDEN_OFFSET, SEEK_SET) < 0) {
475 log_err(cd, _("Cannot seek to hidden header for %s.\n"), device_path(device));
480 if (read_blockwise(devfd, device_block_size(device), hdr, hdr_size) == hdr_size)
481 r = TCRYPT_init_hdr(cd, hdr, params);
489 int TCRYPT_activate(struct crypt_device *cd,
491 struct tcrypt_phdr *hdr,
492 struct crypt_params_tcrypt *params,
495 char cipher[MAX_CIPHER_LEN], dm_name[PATH_MAX], dm_dev_name[PATH_MAX];
496 struct device *device = NULL;
498 struct tcrypt_alg tcipher[3] = {
499 { params->cipher[0], params->key_size, 0 },
500 { params->cipher[1], params->key_size, 0 },
501 { params->cipher[2], params->key_size, 0 }
503 struct crypt_dm_active_device dmd = {
506 .data_device = crypt_data_device(cd),
509 .offset = crypt_get_data_offset(cd),
510 .iv_offset = crypt_get_iv_offset(cd),
514 if (strstr(params->mode, "-tcrypt")) {
515 log_err(cd, _("Kernel doesn't support activation for this TCRYPT legacy mode.\n"));
519 r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
520 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
524 dmd.u.crypt.vk = crypt_alloc_volume_key(params->key_size, NULL);
528 for (i = 2; i >= 0; i--) {
530 if (!params->cipher[i])
534 strncpy(dm_name, name, sizeof(dm_name));
537 snprintf(dm_name, sizeof(dm_name), "%s_%d", name, i);
538 dmd.flags = flags | CRYPT_ACTIVATE_PRIVATE;
541 snprintf(cipher, sizeof(cipher), "%s-%s",
542 params->cipher[i], params->mode);
543 copy_key(dmd.u.crypt.vk->key, hdr->d.keys,
545 params->key_size, i, params->mode);
547 if (top_cipher(tcipher) != i) {
548 snprintf(dm_dev_name, sizeof(dm_dev_name), "%s/%s_%d",
549 dm_get_dir(), name, i + 1);
550 r = device_alloc(&device, dm_dev_name);
553 dmd.data_device = device;
554 dmd.u.crypt.offset = 0;
557 log_dbg("Trying to activate TCRYPT device %s using cipher %s.",
558 dm_name, dmd.u.crypt.cipher);
559 r = dm_create_device(cd, dm_name, CRYPT_TCRYPT, &dmd, 0);
568 if (!r && !(dm_flags() & DM_PLAIN64_SUPPORTED)) {
569 log_err(cd, _("Kernel doesn't support plain64 IV.\n"));
573 crypt_free_volume_key(dmd.u.crypt.vk);