TCRYPT: add dump command
[platform/upstream/cryptsetup.git] / lib / tcrypt / tcrypt.c
index 6ccd148..fdd3ce4 100644 (file)
 
 /* TCRYPT PBKDF variants */
 static struct {
+       unsigned int legacy:1;
        char *name;
        char *hash;
        unsigned int iterations;
 } tcrypt_kdf[] = {
-       { "pbkdf2", "ripemd160", 2000 },
-       { "pbkdf2", "ripemd160", 1000 },
-       { "pbkdf2", "sha512",    1000 },
-       { "pbkdf2", "whirlpool", 1000 },
-       { NULL,     NULL,           0 }
+       { 0, "pbkdf2", "ripemd160", 2000 },
+       { 0, "pbkdf2", "ripemd160", 1000 },
+       { 0, "pbkdf2", "sha512",    1000 },
+       { 0, "pbkdf2", "whirlpool", 1000 },
+       { 1, "pbkdf2", "sha1",      2000 },
+       { 0, NULL,     NULL,           0 }
+};
+
+struct tcrypt_alg {
+               const char *name;
+               unsigned int key_size;
+               unsigned int iv_size;
 };
 
 /* TCRYPT cipher variants */
 static struct {
-       const char *cipher[3];
+       unsigned int legacy:1;
        const char *mode;
-       int key_size;
+       struct tcrypt_alg cipher[3];
 } tcrypt_cipher[] = {
-       { { "aes",     NULL,      NULL      }, "xts-plain64", 64 },
-       { { "twofish", NULL,      NULL      }, "xts-plain64", 64 },
-       { { "serpent", NULL,      NULL      }, "xts-plain64", 64 },
-       { { "aes",     "twofish", "serpent" }, "xts-plain64", 64 },
-       { { "serpent", "twofish", "aes"     }, "xts-plain64", 64 },
-       { { "twofish", "aes",     NULL      }, "xts-plain64", 64 },
-       { { "aes",     "serpent", NULL      }, "xts-plain64", 64 },
-       { { "serpent", "twofish", NULL      }, "xts-plain64", 64 },
-       { { "aes",     NULL,      NULL      }, "lrw-benbi",   48 },
-       { { "twofish", NULL,      NULL      }, "lrw-benbi",   48 },
-       { { "serpent", NULL,      NULL      }, "lrw-benbi",   48 },
-       { { "aes",     "twofish", "serpent" }, "lrw-benbi",   48 },
-       { { "serpent", "twofish", "aes"     }, "lrw-benbi",   48 },
-       { { "twofish", "aes",     NULL      }, "lrw-benbi",   48 },
-       { { "aes",     "serpent", NULL      }, "lrw-benbi",   48 },
-       { { "serpent", "twofish", NULL      }, "lrw-benbi",   48 },
-       { { NULL,      NULL,      NULL      }, NULL,           0 }
+       { 0, "xts-plain64",{{"aes",    64,16}}},
+       { 0, "xts-plain64",{{"serpent",64,16}}},
+       { 0, "xts-plain64",{{"twofish",64,16}}},
+       { 0, "xts-plain64",{{"twofish",64,16},{"aes",    64,16}}},
+       { 0, "xts-plain64",{{"serpent",64,16},{"twofish",64,16},{"aes",    64,16}}},
+       { 0, "xts-plain64",{{"aes",    64,16},{"serpent",64,16}}},
+       { 0, "xts-plain64",{{"aes",    64,16},{"twofish",64,16},{"serpent",64,16}}},
+       { 0, "xts-plain64",{{"serpent",64,16},{"twofish",64,16}}},
+
+       { 0, "lrw-benbi",  {{"aes",    48,16}}},
+       { 0, "lrw-benbi",  {{"serpent",48,16}}},
+       { 0, "lrw-benbi",  {{"twofish",48,16}}},
+       { 0, "lrw-benbi",  {{"twofish",48,16},{"aes",    48,16}}},
+       { 0, "lrw-benbi",  {{"serpent",48,16},{"twofish",48,16},{"aes",    48,16}}},
+       { 0, "lrw-benbi",  {{"aes",    48,16},{"serpent",48,16}}},
+       { 0, "lrw-benbi",  {{"aes",    48,16},{"twofish",48,16},{"serpent",48,16}}},
+       { 0, "lrw-benbi",  {{"serpent",48,16},{"twofish",48,16}}},
+
+       { 1, "cbc-tcrypt", {{"aes",    32,16}}},
+       { 1, "cbc-tcrypt", {{"serpent",32,16}}},
+       { 1, "cbc-tcrypt", {{"twofish",32,16}}},
+       { 1, "cbci-tcrypt",{{"twofish",32,16},{"aes",    32,16}}},
+       { 1, "cbci-tcrypt",{{"serpent",32,16},{"twofish",32,16},{"aes",    32,16}}},
+       { 1, "cbci-tcrypt",{{"aes",    32,16},{"serpent",32,16}}},
+       { 1, "cbci-tcrypt",{{"aes",    32,16},{"twofish",32,16},{"serpent",32,16}}},
+       { 1, "cbci-tcrypt",{{"serpent",32,16},{"twofish",32,16}}},
+
+       { 1, "cbc-tcrypt", {{"cast5",   16,8}}},
+       { 1, "cbc-tcrypt", {{"des3_ede",24,8}}},
+
+       // kernel LRW block size is fixed to 16 bytes
+       // thus cannot be used with blowfish where block is 8 bytes
+       //{ 1,"lrw-benbi",{{"blowfish",64,8}}},
+       //{ 1,"lrw-benbi",{{"blowfish",64,8},{"aes",48,16}}},
+       //{ 1,"lrw-benbi",{{"serpent",48,16},{"blowfish",64,8},{"aes",48,16}}},
+
+       // FIXME: why this doesn't work (blowfish key wrong)?
+       //{ 1,"cbc-tcrypt",{{"blowfish",56,8}}},
+       //{ 1,"cbc-tcrypt",{{"blowfish",56,8},{"aes",32,16}}},
+       //{ 1,"cbc-tcrypt",{{"serpent",32,16},{"blowfish",56,8},{"aes",32,16}}},
+       {}
 };
 
 static void hdr_info(struct crypt_device *cd, struct tcrypt_phdr *hdr,
@@ -79,17 +111,14 @@ static void hdr_info(struct crypt_device *cd, struct tcrypt_phdr *hdr,
        log_dbg("Flags: %d", (int)hdr->d.flags);
        log_dbg("MK: offset %d, size %d", (int)hdr->d.mk_offset, (int)hdr->d.mk_size);
        log_dbg("KDF: PBKDF2, hash %s", params->hash_name);
-       log_dbg("Cipher: %s%s%s%s%s-%s",
-               params->cipher[0],
-               params->cipher[1] ? "-" : "", params->cipher[1] ?: "",
-               params->cipher[2] ? "-" : "", params->cipher[2] ?: "",
-               params->mode);
+       log_dbg("Cipher: %s-%s", params->cipher, params->mode);
 }
 
 static int hdr_from_disk(struct tcrypt_phdr *hdr,
                         struct crypt_params_tcrypt *params,
                         int kdf_index, int cipher_index)
 {
+       char cipher_name[MAX_CIPHER_LEN * 4];
        uint32_t crc32;
        size_t size;
 
@@ -138,24 +167,38 @@ static int hdr_from_disk(struct tcrypt_phdr *hdr,
 
        params->hash_name  = tcrypt_kdf[kdf_index].hash;
 
-       params->cipher[0]  = tcrypt_cipher[cipher_index].cipher[0];
-       params->cipher[1]  = tcrypt_cipher[cipher_index].cipher[1];
-       params->cipher[2]  = tcrypt_cipher[cipher_index].cipher[2];
-       params->mode     = tcrypt_cipher[cipher_index].mode;
-       params->key_size = tcrypt_cipher[cipher_index].key_size;
+       params->key_size = tcrypt_cipher[cipher_index].cipher[0].key_size;
+       strncpy(cipher_name, tcrypt_cipher[cipher_index].cipher[0].name,
+               sizeof(cipher_name));
+
+       if (tcrypt_cipher[cipher_index].cipher[1].name) {
+               strcat(cipher_name, "-");
+               strncat(cipher_name, tcrypt_cipher[cipher_index].cipher[1].name,
+                       MAX_CIPHER_LEN);
+               params->key_size += tcrypt_cipher[cipher_index].cipher[1].key_size;
+       }
+
+       if (tcrypt_cipher[cipher_index].cipher[2].name) {
+               strcat(cipher_name, "-");
+               strncat(cipher_name, tcrypt_cipher[cipher_index].cipher[2].name,
+                       MAX_CIPHER_LEN);
+               params->key_size += tcrypt_cipher[cipher_index].cipher[2].key_size;
+       }
+
+       params->cipher = strdup(cipher_name);
+       params->mode   = strdup(tcrypt_cipher[cipher_index].mode);
 
        return 0;
 }
 
 static int decrypt_hdr_one(const char *name, const char *mode,
                           const char *key, size_t key_size,
-                          struct tcrypt_phdr *hdr)
+                          size_t iv_size, struct tcrypt_phdr *hdr)
 {
        char iv[TCRYPT_HDR_IV_LEN] = {};
        char mode_name[MAX_CIPHER_LEN];
        struct crypt_cipher *cipher;
-       void *buf = &hdr->e;
-       char *c;
+       char *c, *buf = (char*)&hdr->e;
        int r;
 
        /* Remove IV if present */
@@ -165,14 +208,15 @@ static int decrypt_hdr_one(const char *name, const char *mode,
                *c = '\0';
 
        if (!strncmp(mode, "lrw", 3))
-               iv[15] = 1;
+               iv[iv_size - 1] = 1;
+       else if (!strncmp(mode, "cbc", 3))
+               memcpy(iv, &key[key_size], iv_size);
 
        r = crypt_cipher_init(&cipher, name, mode_name, key, key_size);
        if (r < 0)
                return r;
 
-       r = crypt_cipher_decrypt(cipher, buf, buf, TCRYPT_HDR_LEN,
-                                iv, TCRYPT_HDR_IV_LEN);
+       r = crypt_cipher_decrypt(cipher, buf, buf, TCRYPT_HDR_LEN, iv, iv_size);
        crypt_cipher_destroy(cipher);
 
        return r;
@@ -191,47 +235,113 @@ static void copy_key(char *out_key, const char *key, int key_num,
                ks -= TCRYPT_LRW_IKEY_LEN;
                memcpy(out_key, &key[ks * ki], ks);
                memcpy(&out_key[ks * ki], key, TCRYPT_LRW_IKEY_LEN);
+       } else if (!strncmp(mode, "cbc", 3)) {
+               ki++;
+               memcpy(out_key, &key[ki * 32], ks);
+               memcpy(&out_key[ks], key, 32);
        }
 }
 
-static int top_cipher(const char *cipher[3])
+/*
+ * For chanined ciphers and CBC mode we need "inner" decryption.
+ * Backend doesn't provide this, so implement it here directly using ECB.
+ */
+static int decrypt_hdr_cbci(struct tcrypt_alg ciphers[3],
+                            const char *key, struct tcrypt_phdr *hdr)
 {
-       if (cipher[2])
+       struct crypt_cipher *cipher[3] = {};
+       int bs = ciphers[0].iv_size;
+       char *buf = (char*)&hdr->e, iv[bs], iv_old[bs];
+       int i, j, r;
+
+       memcpy(iv, key, bs);
+
+       /* Initialize all ciphers in chain in ECB mode */
+       for (j = 0; j < 3; j++) {
+               if (!ciphers[j].name)
+                       continue;
+               r = crypt_cipher_init(&cipher[j], ciphers[j].name, "ecb",
+                                     &key[(j+1)*32], ciphers[j].key_size);
+               if (r < 0)
+                       goto out;
+       }
+
+       /* Implements CBC with chained ciphers in inner loop */
+       for (i = 0; i < TCRYPT_HDR_LEN; i += bs) {
+               memcpy(iv_old, &buf[i], bs);
+               for (j = 2; j >= 0; j--) {
+                       if (!cipher[j])
+                               continue;
+                       r = crypt_cipher_decrypt(cipher[j], &buf[i], &buf[i],
+                                                 bs, NULL, 0);
+                       if (r < 0)
+                               goto out;
+               }
+               for (j = 0; j < bs; j++)
+                       buf[i + j] ^= iv[j];
+               memcpy(iv, iv_old, bs);
+       }
+out:
+       for (j = 0; j < 3; j++)
+               if (cipher[j])
+                       crypt_cipher_destroy(cipher[j]);
+
+       return r;
+}
+
+static int top_cipher(struct tcrypt_alg cipher[3])
+{
+       if (cipher[2].name)
                return 2;
 
-       if (cipher[1])
+       if (cipher[1].name)
                return 1;
 
        return 0;
 }
 
 static int decrypt_hdr(struct crypt_device *cd, struct tcrypt_phdr *hdr,
-                       const char *key)
+                       const char *key, int legacy_modes)
 {
        char one_key[TCRYPT_HDR_KEY_LEN];
        struct tcrypt_phdr hdr2;
        int i, j, r;
 
-       for (i = 0; tcrypt_cipher[i].cipher[0]; i++) {
+       for (i = 0; tcrypt_cipher[i].cipher[0].name; i++) {
+               if (!legacy_modes && tcrypt_cipher[i].legacy)
+                       continue;
                log_dbg("TCRYPT:  trying cipher: %s%s%s%s%s-%s.",
-                       tcrypt_cipher[i].cipher[0],
-                       tcrypt_cipher[i].cipher[1] ? "-" : "", tcrypt_cipher[i].cipher[1] ?: "",
-                       tcrypt_cipher[i].cipher[2] ? "-" : "", tcrypt_cipher[i].cipher[2] ?: "",
+                       tcrypt_cipher[i].cipher[0].name,
+                       tcrypt_cipher[i].cipher[1].name ? "-" : "", tcrypt_cipher[i].cipher[1].name ?: "",
+                       tcrypt_cipher[i].cipher[2].name ? "-" : "", tcrypt_cipher[i].cipher[2].name ?: "",
                        tcrypt_cipher[i].mode);
 
                memcpy(&hdr2.e, &hdr->e, TCRYPT_HDR_LEN);
 
-               for (j = 2; j >= 0 ; j--) {
-                       if (!tcrypt_cipher[i].cipher[j])
+               /* Remove CBC whitening */
+               if (!strncmp(tcrypt_cipher[i].mode, "cbc", 3)) {
+                       char *buf = (char*)&hdr2.e;
+                       for (j = 0; j < TCRYPT_HDR_LEN; j++)
+                               buf[j] ^= key[8 + j % 8];
+               }
+
+               /* For chained (inner) CBC we do not have API support */
+               if (!strncmp(tcrypt_cipher[i].mode, "cbci", 4))
+                       r = decrypt_hdr_cbci(tcrypt_cipher[i].cipher, key, &hdr2);
+               else for (j = 2; j >= 0 ; j--) {
+                       if (!tcrypt_cipher[i].cipher[j].name)
                                continue;
                        copy_key(one_key, key, top_cipher(tcrypt_cipher[i].cipher),
-                                tcrypt_cipher[i].key_size,
+                                tcrypt_cipher[i].cipher[j].key_size,
                                 j, tcrypt_cipher[i].mode);
-                       r = decrypt_hdr_one(tcrypt_cipher[i].cipher[j],
+                       r = decrypt_hdr_one(tcrypt_cipher[i].cipher[j].name,
                                            tcrypt_cipher[i].mode, one_key,
-                                           tcrypt_cipher[i].key_size, &hdr2);
-                       if (r < 0)
+                                           tcrypt_cipher[i].cipher[j].key_size,
+                                           tcrypt_cipher[i].cipher[j].iv_size, &hdr2);
+                       if (r < 0) {
+                               log_dbg("Error %s.", tcrypt_cipher[i].cipher[j].name);
                                break;
+                       }
                }
 
                if (!strncmp(hdr2.d.magic, TCRYPT_HDR_MAGIC, TCRYPT_HDR_MAGIC_LEN)) {
@@ -295,7 +405,7 @@ static int TCRYPT_init_hdr(struct crypt_device *cd,
        unsigned char pwd[TCRYPT_KEY_POOL_LEN] = {};
        size_t passphrase_size;
        char *key;
-       int r, i;
+       int r, i, legacy_modes;
 
        if (posix_memalign((void*)&key, crypt_getpagesize(), TCRYPT_HDR_KEY_LEN))
                return -ENOMEM;
@@ -316,7 +426,10 @@ static int TCRYPT_init_hdr(struct crypt_device *cd,
        for (i = 0; i < params->passphrase_size; i++)
                pwd[i] += params->passphrase[i];
 
+       legacy_modes = params->flags & CRYPT_TCRYPT_LEGACY_MODES ? 1 : 0;
        for (i = 0; tcrypt_kdf[i].name; i++) {
+               if (!legacy_modes && tcrypt_kdf[i].legacy)
+                       continue;
                /* Derive header key */
                log_dbg("TCRYPT: trying KDF: %s-%s-%d.",
                        tcrypt_kdf[i].name, tcrypt_kdf[i].hash, tcrypt_kdf[i].iterations);
@@ -329,7 +442,7 @@ static int TCRYPT_init_hdr(struct crypt_device *cd,
                        break;
 
                /* Decrypt header */
-               r = decrypt_hdr(cd, hdr, key);
+               r = decrypt_hdr(cd, hdr, key, legacy_modes);
                if (r != -EPERM)
                        break;
        }
@@ -393,7 +506,8 @@ int TCRYPT_activate(struct crypt_device *cd,
 {
        char cipher[MAX_CIPHER_LEN], dm_name[PATH_MAX], dm_dev_name[PATH_MAX];
        struct device *device = NULL;
-       int i, r;
+       int i, r, num_ciphers;
+       char cname[3][MAX_CIPHER_LEN];
        struct crypt_dm_active_device dmd = {
                .target = DM_CRYPT,
                .size   = 0,
@@ -405,18 +519,44 @@ int TCRYPT_activate(struct crypt_device *cd,
                }
        };
 
+       if (!hdr->d.version) {
+               log_dbg("TCRYPT: this function is not supported without encrypted header load.");
+               return -ENOTSUP;
+       }
+
+       if (hdr->d.sector_size && hdr->d.sector_size != SECTOR_SIZE) {
+               log_err(cd, _("Activation is not supported for %d sector size.\n"),
+                       hdr->d.sector_size);
+               return -ENOTSUP;
+       }
+
+       if (strstr(params->mode, "-tcrypt")) {
+               log_err(cd, _("Kernel doesn't support activation for this TCRYPT legacy mode.\n"));
+               return -ENOTSUP;
+       }
+
        r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
                                dmd.u.crypt.offset, &dmd.size, &dmd.flags);
        if (r)
                return r;
 
-       dmd.u.crypt.vk = crypt_alloc_volume_key(params->key_size, NULL);
+       /* Parse cipher chain from c1[-c2[-c3]] */
+       cname[0][0] = cname[1][0] = cname[2][0] = '\0';
+       num_ciphers = sscanf(params->cipher, "%" MAX_CIPHER_LEN_STR "[^-]-%"
+                                                 MAX_CIPHER_LEN_STR "[^-]-%"
+                                                 MAX_CIPHER_LEN_STR "s",
+                     cname[0], cname[1], cname[2]);
+       if (num_ciphers < 1)
+               return -EINVAL;
+
+       /* Frome here, key size for every cipher must be the same */
+       dmd.u.crypt.vk = crypt_alloc_volume_key(params->key_size / num_ciphers, NULL);
        if (!dmd.u.crypt.vk)
                return -ENOMEM;
 
        for (i = 2; i >= 0; i--) {
 
-               if (!params->cipher[i])
+               if (!cname[i][0])
                        continue;
 
                if (i == 0) {
@@ -428,12 +568,11 @@ int TCRYPT_activate(struct crypt_device *cd,
                }
 
                snprintf(cipher, sizeof(cipher), "%s-%s",
-                        params->cipher[i], params->mode);
-               copy_key(dmd.u.crypt.vk->key, hdr->d.keys,
-                        top_cipher(params->cipher),
-                        params->key_size, i, params->mode);
+                        cname[i], params->mode);
+               copy_key(dmd.u.crypt.vk->key, hdr->d.keys, num_ciphers - 1,
+                        params->key_size / num_ciphers, i, params->mode);
 
-               if (top_cipher(params->cipher) != i) {
+               if ((num_ciphers -1) != i) {
                        snprintf(dm_dev_name, sizeof(dm_dev_name), "%s/%s_%d",
                                 dm_get_dir(), name, i + 1);
                        r = device_alloc(&device, dm_dev_name);
@@ -462,3 +601,188 @@ int TCRYPT_activate(struct crypt_device *cd,
        crypt_free_volume_key(dmd.u.crypt.vk);
        return r;
 }
+
+static int remove_one(struct crypt_device *cd, const char *name,
+                     const char *base_uuid, int index)
+{
+       struct crypt_dm_active_device dmd = {};
+       char dm_name[PATH_MAX];
+       int r;
+
+       if (snprintf(dm_name, sizeof(dm_name), "%s_%d", name, index) < 0)
+               return -ENOMEM;
+
+       r = dm_status_device(cd, dm_name);
+       if (r < 0)
+               return r;
+
+       r = dm_query_device(cd, dm_name, DM_ACTIVE_UUID, &dmd);
+       if (!r && !strncmp(dmd.uuid, base_uuid, strlen(base_uuid)))
+               r = dm_remove_device(cd, dm_name, 0, 0);
+
+       free(CONST_CAST(void*)dmd.uuid);
+       return r;
+}
+
+int TCRYPT_deactivate(struct crypt_device *cd, const char *name)
+{
+       struct crypt_dm_active_device dmd = {};
+       int r;
+
+       r = dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd);
+       if (r < 0)
+               return r;
+       if (!dmd.uuid)
+               return -EINVAL;
+
+       r = dm_remove_device(cd, name, 0, 0);
+       if (r < 0)
+               goto out;
+
+       r = remove_one(cd, name, dmd.uuid, 1);
+       if (r < 0)
+               goto out;
+
+       r = remove_one(cd, name, dmd.uuid, 2);
+       if (r < 0)
+               goto out;
+out:
+       free(CONST_CAST(void*)dmd.uuid);
+       return (r == -ENODEV) ? 0 : r;
+}
+
+static int status_one(struct crypt_device *cd, const char *name,
+                     const char *base_uuid, int index,
+                     size_t *key_size, char *cipher, uint64_t *data_offset,
+                     struct device **device)
+{
+       struct crypt_dm_active_device dmd = {};
+       char dm_name[PATH_MAX], *c;
+       int r;
+
+       if (snprintf(dm_name, sizeof(dm_name), "%s_%d", name, index) < 0)
+               return -ENOMEM;
+
+       r = dm_status_device(cd, dm_name);
+       if (r < 0)
+               return r;
+
+       r = dm_query_device(cd, dm_name, DM_ACTIVE_DEVICE |
+                                         DM_ACTIVE_UUID |
+                                         DM_ACTIVE_CRYPT_CIPHER |
+                                         DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
+       if (r > 0)
+               r = 0;
+       if (!r && !strncmp(dmd.uuid, base_uuid, strlen(base_uuid))) {
+               if ((c = strchr(dmd.u.crypt.cipher, '-')))
+                       *c = '\0';
+               strcat(cipher, "-");
+               strncat(cipher, dmd.u.crypt.cipher, MAX_CIPHER_LEN);
+               *key_size += dmd.u.crypt.vk->keylength;
+               *data_offset = dmd.u.crypt.offset * SECTOR_SIZE;
+               device_free(*device);
+               *device = dmd.data_device;
+       } else {
+               device_free(dmd.data_device);
+               r = -ENODEV;
+       }
+
+       free(CONST_CAST(void*)dmd.uuid);
+       free(CONST_CAST(void*)dmd.u.crypt.cipher);
+       crypt_free_volume_key(dmd.u.crypt.vk);
+       return r;
+}
+
+int TCRYPT_init_by_name(struct crypt_device *cd, const char *name,
+                       const struct crypt_dm_active_device *dmd,
+                       struct device **device,
+                       struct crypt_params_tcrypt *tcrypt_params,
+                       struct tcrypt_phdr *tcrypt_hdr)
+{
+       char cipher[MAX_CIPHER_LEN * 4], *mode;
+
+       memset(tcrypt_params, 0, sizeof(*tcrypt_params));
+       memset(tcrypt_hdr, 0, sizeof(*tcrypt_hdr));
+       tcrypt_hdr->d.sector_size = SECTOR_SIZE;
+       tcrypt_hdr->d.mk_offset = dmd->u.crypt.offset * SECTOR_SIZE;
+
+       strncpy(cipher, dmd->u.crypt.cipher, MAX_CIPHER_LEN);
+
+       if ((mode = strchr(cipher, '-'))) {
+               *mode = '\0';
+               tcrypt_params->mode = strdup(++mode);
+       }
+       tcrypt_params->key_size = dmd->u.crypt.vk->keylength;
+
+       if (!status_one(cd, name, dmd->uuid, 1, &tcrypt_params->key_size,
+                       cipher, &tcrypt_hdr->d.mk_offset, device))
+               status_one(cd, name, dmd->uuid, 2, &tcrypt_params->key_size,
+                          cipher, &tcrypt_hdr->d.mk_offset, device);
+
+       tcrypt_params->cipher = strdup(cipher);
+       return 0;
+}
+
+uint64_t TCRYPT_get_data_offset(struct tcrypt_phdr *hdr)
+{
+       // FIXME: system vol.
+       if (!hdr->d.mk_offset)
+               return 1;
+       return (hdr->d.mk_offset / hdr->d.sector_size);
+}
+
+uint64_t TCRYPT_get_iv_offset(struct tcrypt_phdr *hdr)
+{
+       if (!hdr->d.mk_offset)
+               return 0;
+       return (hdr->d.mk_offset / hdr->d.sector_size);
+}
+
+int TCRYPT_get_volume_key(struct crypt_device *cd,
+                         struct tcrypt_phdr *hdr,
+                         struct crypt_params_tcrypt *params,
+                         struct volume_key **vk)
+{
+       int i, num_keys = 1, key_size;
+       const char *c;
+
+       if (!hdr->d.version) {
+               log_dbg("TCRYPT: this function is not supported without encrypted header load.");
+               return -ENOTSUP;
+       }
+
+       *vk = crypt_alloc_volume_key(params->key_size, NULL);
+       if (!*vk)
+               return -ENOMEM;
+
+       for (num_keys = 0, c = params->cipher; c ; num_keys++)
+               c = strchr(++c, '-');
+
+       key_size = params->key_size / num_keys;
+
+       for (i = 0; i < num_keys; i++)
+               copy_key(&(*vk)->key[key_size * i], hdr->d.keys, num_keys - 1,
+                        key_size, i, params->mode);
+
+       return 0;
+}
+
+int TCRYPT_dump(struct crypt_device *cd,
+               struct tcrypt_phdr *hdr,
+               struct crypt_params_tcrypt *params)
+{
+       log_std(cd, "TCRYPT header information for %s\n",
+               device_path(crypt_metadata_device(cd)));
+       if (hdr->d.version) {
+               log_std(cd, "Version:       \t%d\n", hdr->d.version);
+               log_std(cd, "Driver req.:\t%d\n", hdr->d.version_tc);
+
+               log_std(cd, "Sector size:\t%" PRIu32 "\n", hdr->d.sector_size);
+               log_std(cd, "MK offset:\t%" PRIu64 "\n", hdr->d.mk_offset);
+               log_std(cd, "PBKDF2 hash:\t%s\n", params->hash_name);
+       }
+       log_std(cd, "Cipher chain:\t%s\n", params->cipher);
+       log_std(cd, "Cipher mode:\t%s\n", params->mode);
+       log_std(cd, "MK bits:       \t%d\n", params->key_size * 8);
+       return 0;
+}