Imported Upstream version 1.6.7
[platform/upstream/cryptsetup.git] / lib / setup.c
index d0b25ac..01e2c80 100644 (file)
@@ -1,13 +1,15 @@
 /*
  * libcryptsetup - cryptsetup library
  *
- * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
+ * Copyright (C) 2004, Jana Saout <jana@saout.de>
  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2014, Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -23,6 +25,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <sys/utsname.h>
 #include <fcntl.h>
 #include <errno.h>
 
@@ -30,6 +33,7 @@
 #include "luks.h"
 #include "loopaes.h"
 #include "verity.h"
+#include "tcrypt.h"
 #include "internal.h"
 
 struct crypt_device {
@@ -45,29 +49,44 @@ struct crypt_device {
        int password_verify;
        int rng_type;
 
-       /* used in CRYPT_LUKS1 */
-       struct luks_phdr hdr;
-       uint64_t PBKDF2_per_sec;
-
-       /* used in CRYPT_PLAIN */
-       struct crypt_params_plain plain_hdr;
-       char *plain_cipher;
-       char *plain_cipher_mode;
-       char *plain_uuid;
-       unsigned int plain_key_size;
-
-       /* used in CRYPT_LOOPAES */
-       struct crypt_params_loopaes loopaes_hdr;
-       char *loopaes_cipher;
-       char *loopaes_cipher_mode;
-       char *loopaes_uuid;
-       unsigned int loopaes_key_size;
-
-       /* used in CRYPT_VERITY */
-       struct crypt_params_verity verity_hdr;
-       char *verity_root_hash;
-       unsigned int verity_root_hash_size;
-       char *verity_uuid;
+       // FIXME: private binary headers and access it properly
+       // through sub-library (LUKS1, TCRYPT)
+
+       union {
+       struct { /* used in CRYPT_LUKS1 */
+               struct luks_phdr hdr;
+               uint64_t PBKDF2_per_sec;
+       } luks1;
+       struct { /* used in CRYPT_PLAIN */
+               struct crypt_params_plain hdr;
+               char *cipher;
+               char *cipher_mode;
+               unsigned int key_size;
+       } plain;
+       struct { /* used in CRYPT_LOOPAES */
+               struct crypt_params_loopaes hdr;
+               char *cipher;
+               char *cipher_mode;
+               unsigned int key_size;
+       } loopaes;
+       struct { /* used in CRYPT_VERITY */
+               struct crypt_params_verity hdr;
+               char *root_hash;
+               unsigned int root_hash_size;
+               char *uuid;
+       } verity;
+       struct { /* used in CRYPT_TCRYPT */
+               struct crypt_params_tcrypt params;
+               struct tcrypt_phdr hdr;
+       } tcrypt;
+       struct { /* used if initialized without header by name */
+               char *active_name;
+               /* buffers, must refresh from kernel on every query */
+               char cipher[MAX_CIPHER_LEN];
+               char cipher_mode[MAX_CIPHER_LEN];
+               unsigned int key_size;
+       } none;
+       } u;
 
        /* callbacks definitions */
        void (*log)(int level, const char *msg, void *usrptr);
@@ -81,6 +100,9 @@ struct crypt_device {
        char error[MAX_ERROR_LENGTH];
 };
 
+/* Just to suppress redundant messages about crypto backend */
+static int _crypto_logged = 0;
+
 /* Global error */
 /* FIXME: not thread safe, remove this later */
 static char global_error[MAX_ERROR_LENGTH] = {0};
@@ -168,12 +190,11 @@ struct device *crypt_data_device(struct crypt_device *cd)
        return cd->device;
 }
 
-static int init_crypto(struct crypt_device *ctx)
+int init_crypto(struct crypt_device *ctx)
 {
+       struct utsname uts;
        int r;
 
-       crypt_fips_libcryptsetup_check(ctx);
-
        r = crypt_random_init(ctx);
        if (r < 0) {
                log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
@@ -184,7 +205,14 @@ static int init_crypto(struct crypt_device *ctx)
        if (r < 0)
                log_err(ctx, _("Cannot initialize crypto backend.\n"));
 
-       log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
+       if (!r && !_crypto_logged) {
+               log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
+               if (!uname(&uts))
+                       log_dbg("Detected kernel %s %s %s.",
+                               uts.sysname, uts.release, uts.machine);
+               _crypto_logged = 1;
+       }
+
        return r;
 }
 
@@ -243,18 +271,58 @@ static int isVERITY(const char *type)
        return (type && !strcmp(CRYPT_VERITY, type));
 }
 
+static int isTCRYPT(const char *type)
+{
+       return (type && !strcmp(CRYPT_TCRYPT, type));
+}
+
+static int onlyLUKS(struct crypt_device *cd)
+{
+       int r = 0;
+
+       if (cd && !cd->type) {
+               log_err(cd, _("Cannot determine device type. Incompatible activation of device?\n"));
+               r = -EINVAL;
+       }
+       if (!cd || !isLUKS(cd->type)) {
+               log_err(cd, _("This operation is supported only for LUKS device.\n"));
+               r = -EINVAL;
+       }
+
+       return r;
+}
+
+static void crypt_set_null_type(struct crypt_device *cd)
+{
+       if (!cd->type)
+               return;
+
+       free(cd->type);
+       cd->type = NULL;
+       cd->u.none.active_name = NULL;
+}
+
+static void crypt_reset_null_type(struct crypt_device *cd)
+{
+       if (cd->type)
+               return;
+
+       free(cd->u.none.active_name);
+       cd->u.none.active_name = NULL;
+}
+
 /* keyslot helpers */
 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
 {
        if (*keyslot == CRYPT_ANY_SLOT) {
-               *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
+               *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
                if (*keyslot < 0) {
                        log_err(cd, _("All key slots full.\n"));
                        return -EINVAL;
                }
        }
 
-       switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
+       switch (LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot)) {
                case CRYPT_SLOT_INVALID:
                        log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
                                *keyslot, LUKS_NUMKEYS - 1);
@@ -300,6 +368,36 @@ static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
        return 0;
 }
 
+/*
+ * compares type of active device to provided string (only if there is no explicit type)
+ */
+static int crypt_uuid_type_cmp(struct crypt_device *cd, const char *type)
+{
+       struct crypt_dm_active_device dmd = {};
+       size_t len;
+       int r;
+
+       /* Must user header-on-disk if we know type here */
+       if (cd->type || !cd->u.none.active_name)
+               return -EINVAL;
+
+       log_dbg("Checking if active device %s without header has UUID type %s.",
+               cd->u.none.active_name, type);
+
+       r = dm_query_device(cd, cd->u.none.active_name, DM_ACTIVE_UUID, &dmd);
+       if (r < 0)
+               return r;
+
+       r = -ENODEV;
+       len = strlen(type);
+       if (dmd.uuid && strlen(dmd.uuid) > len &&
+           !strncmp(dmd.uuid, type, len) && dmd.uuid[len] == '-')
+               r = 0;
+
+       free(CONST_CAST(void*)dmd.uuid);
+       return r;
+}
+
 int PLAIN_activate(struct crypt_device *cd,
                     const char *name,
                     struct volume_key *vk,
@@ -311,7 +409,6 @@ int PLAIN_activate(struct crypt_device *cd,
        enum devcheck device_check;
        struct crypt_dm_active_device dmd = {
                .target = DM_CRYPT,
-               .uuid   = crypt_get_uuid(cd),
                .size   = size,
                .flags  = flags,
                .data_device = crypt_data_device(cd),
@@ -346,10 +443,6 @@ int PLAIN_activate(struct crypt_device *cd,
 
        r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
 
-       // FIXME
-       if (!cd->plain_uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
-               cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
-
        free(dm_cipher);
        return r;
 }
@@ -425,7 +518,7 @@ static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslo
                        goto out;
 
                r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
-                                          passphrase_size_read, &cd->hdr, vk, cd);
+                                          passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
                if (r == -EPERM)
                        eperm = 1;
                crypt_safe_free(passphrase_read);
@@ -611,7 +704,36 @@ static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int re
        if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
                return -ENOMEM;
 
-       memcpy(&cd->hdr, &hdr, sizeof(hdr));
+       memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
+
+       return r;
+}
+
+static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
+{
+       int r;
+
+       if (!params)
+               return -EINVAL;
+
+       r = init_crypto(cd);
+       if (r < 0)
+               return r;
+
+       memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
+
+       r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
+
+       cd->u.tcrypt.params.passphrase = NULL;
+       cd->u.tcrypt.params.passphrase_size = 0;
+       cd->u.tcrypt.params.keyfiles = NULL;
+       cd->u.tcrypt.params.keyfiles_count = 0;
+
+       if (r < 0)
+               return r;
+
+       if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
+               return -ENOMEM;
 
        return r;
 }
@@ -625,22 +747,22 @@ static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verit
        if (r < 0)
                return r;
 
-       if (params->flags & CRYPT_VERITY_NO_HEADER)
+       if (params && params->flags & CRYPT_VERITY_NO_HEADER)
                return -EINVAL;
 
        if (params)
                sb_offset = params->hash_area_offset;
 
-       r = VERITY_read_sb(cd, sb_offset, &cd->verity_uuid, &cd->verity_hdr);
+       r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
        if (r < 0)
                return r;
 
        if (params)
-               cd->verity_hdr.flags = params->flags;
+               cd->u.verity.hdr.flags = params->flags;
 
        /* Hash availability checked in sb load */
-       cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
-       if (cd->verity_root_hash_size > 4096)
+       cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
+       if (cd->u.verity.root_hash_size > 4096)
                return -EINVAL;
 
        if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
@@ -666,54 +788,58 @@ static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
                        DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
        if (r < 0)
                goto out;
+       if (r > 0)
+               r = 0;
 
        if (isPLAIN(cd->type)) {
-               cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
-               cd->plain_hdr.hash = NULL; /* no way to get this */
-               cd->plain_hdr.offset = dmd.u.crypt.offset;
-               cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
-               cd->plain_key_size = dmd.u.crypt.vk->keylength;
+               cd->u.plain.hdr.hash = NULL; /* no way to get this */
+               cd->u.plain.hdr.offset = dmd.u.crypt.offset;
+               cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
+               cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
 
                r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
                if (!r) {
-                       cd->plain_cipher = strdup(cipher);
-                       cd->plain_cipher_mode = strdup(cipher_mode);
+                       cd->u.plain.cipher = strdup(cipher);
+                       cd->u.plain.cipher_mode = strdup(cipher_mode);
                }
        } else if (isLOOPAES(cd->type)) {
-               cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
-               cd->loopaes_hdr.offset = dmd.u.crypt.offset;
+               cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
 
                r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
                                              &key_nums, cipher_mode);
                if (!r) {
-                       cd->loopaes_cipher = strdup(cipher);
-                       cd->loopaes_cipher_mode = strdup(cipher_mode);
+                       cd->u.loopaes.cipher = strdup(cipher);
+                       cd->u.loopaes.cipher_mode = strdup(cipher_mode);
                        /* version 3 uses last key for IV */
                        if (dmd.u.crypt.vk->keylength % key_nums)
                                key_nums++;
-                       cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
+                       cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
                }
        } else if (isLUKS(cd->type)) {
                if (crypt_metadata_device(cd)) {
                        r = _crypt_load_luks1(cd, 0, 0);
                        if (r < 0) {
                                log_dbg("LUKS device header does not match active device.");
-                               free(cd->type);
-                               cd->type = NULL;
+                               crypt_set_null_type(cd);
                                r = 0;
                                goto out;
                        }
                        /* check whether UUIDs match each other */
-                       r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
+                       r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
                        if (r < 0) {
                                log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
-                                       cd->hdr.uuid, dmd.uuid);
-                               free(cd->type);
-                               cd->type = NULL;
+                                       cd->u.luks1.hdr.uuid, dmd.uuid);
+                               crypt_set_null_type(cd);
                                r = 0;
-                               goto out;
                        }
+               } else {
+                       log_dbg("LUKS device header not available.");
+                       crypt_set_null_type(cd);
+                       r = 0;
                }
+       } else if (isTCRYPT(cd->type)) {
+               r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
+                                       &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
        }
 out:
        crypt_free_volume_key(dmd.u.crypt.vk);
@@ -734,33 +860,33 @@ static int _init_by_name_verity(struct crypt_device *cd, const char *name)
 
        r = dm_query_device(cd, name,
                                DM_ACTIVE_DEVICE |
-                               DM_ACTIVE_UUID |
                                DM_ACTIVE_VERITY_HASH_DEVICE |
                                DM_ACTIVE_VERITY_PARAMS, &dmd);
        if (r < 0)
                goto out;
+       if (r > 0)
+               r = 0;
 
        if (isVERITY(cd->type)) {
-               cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
-               cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
-               cd->verity_hdr.data_size = params.data_size;
-               cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
-               cd->verity_root_hash = NULL;
-               cd->verity_hdr.hash_name = params.hash_name;
-               cd->verity_hdr.data_device = NULL;
-               cd->verity_hdr.hash_device = NULL;
-               cd->verity_hdr.data_block_size = params.data_block_size;
-               cd->verity_hdr.hash_block_size = params.hash_block_size;
-               cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
-               cd->verity_hdr.hash_type = params.hash_type;
-               cd->verity_hdr.flags = params.flags;
-               cd->verity_hdr.salt_size = params.salt_size;
-               cd->verity_hdr.salt = params.salt;
+               cd->u.verity.uuid = NULL; // FIXME
+               cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
+               cd->u.verity.hdr.data_size = params.data_size;
+               cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
+               cd->u.verity.root_hash = NULL;
+               cd->u.verity.hdr.hash_name = params.hash_name;
+               cd->u.verity.hdr.data_device = NULL;
+               cd->u.verity.hdr.hash_device = NULL;
+               cd->u.verity.hdr.data_block_size = params.data_block_size;
+               cd->u.verity.hdr.hash_block_size = params.hash_block_size;
+               cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
+               cd->u.verity.hdr.hash_type = params.hash_type;
+               cd->u.verity.hdr.flags = params.flags;
+               cd->u.verity.hdr.salt_size = params.salt_size;
+               cd->u.verity.hdr.salt = params.salt;
                cd->metadata_device = dmd.u.verity.hash_device;
        }
 out:
        device_free(dmd.data_device);
-       free(CONST_CAST(void*)dmd.uuid);
        return r;
 }
 
@@ -819,6 +945,8 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
                        (*cd)->type = strdup(CRYPT_LUKS1);
                else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
                        (*cd)->type = strdup(CRYPT_VERITY);
+               else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
+                       (*cd)->type = strdup(CRYPT_TCRYPT);
                else
                        log_dbg("Unknown UUID set, some parameters are not set.");
        } else
@@ -840,7 +968,11 @@ out:
        if (r < 0) {
                crypt_free(*cd);
                *cd = NULL;
+       } else if (!(*cd)->type && name) {
+               /* For anonymous device (no header found) remember initialized name */
+               (*cd)->u.none.active_name = strdup(name);
        }
+
        device_free(dmd.data_device);
        free(CONST_CAST(void*)dmd.uuid);
        return r;
@@ -868,28 +1000,31 @@ static int _crypt_format_plain(struct crypt_device *cd,
                return -EINVAL;
        }
 
+       if (uuid) {
+               log_err(cd, _("UUID is not supported for this crypt type.\n"));
+               return -EINVAL;
+       }
+
        if (!(cd->type = strdup(CRYPT_PLAIN)))
                return -ENOMEM;
 
-       cd->plain_key_size = volume_key_size;
+       cd->u.plain.key_size = volume_key_size;
        cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
        if (!cd->volume_key)
                return -ENOMEM;
 
-       cd->plain_cipher = strdup(cipher);
-       cd->plain_cipher_mode = strdup(cipher_mode);
+       cd->u.plain.cipher = strdup(cipher);
+       cd->u.plain.cipher_mode = strdup(cipher_mode);
 
-       if (uuid)
-               cd->plain_uuid = strdup(uuid);
 
        if (params && params->hash)
-               cd->plain_hdr.hash = strdup(params->hash);
+               cd->u.plain.hdr.hash = strdup(params->hash);
 
-       cd->plain_hdr.offset = params ? params->offset : 0;
-       cd->plain_hdr.skip = params ? params->skip : 0;
-       cd->plain_hdr.size = params ? params->size : 0;
+       cd->u.plain.hdr.offset = params ? params->offset : 0;
+       cd->u.plain.hdr.skip = params ? params->skip : 0;
+       cd->u.plain.hdr.size = params ? params->size : 0;
 
-       if (!cd->plain_cipher || !cd->plain_cipher_mode)
+       if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
                return -ENOMEM;
 
        return 0;
@@ -937,17 +1072,12 @@ static int _crypt_format_luks1(struct crypt_device *cd,
                                       &required_alignment,
                                       &alignment_offset, DEFAULT_DISK_ALIGNMENT);
 
-       /* Check early if we cannot allocate block device for key slot access */
-       r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
-       if(r < 0)
-               return r;
-
-       r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
+       r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
                               (params && params->hash) ? params->hash : "sha1",
                               uuid, LUKS_STRIPES,
                               required_alignment / SECTOR_SIZE,
                               alignment_offset / SECTOR_SIZE,
-                              cd->iteration_time, &cd->PBKDF2_per_sec,
+                              cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
                               cd->metadata_device ? 1 : 0, cd);
        if(r < 0)
                return r;
@@ -969,7 +1099,7 @@ static int _crypt_format_luks1(struct crypt_device *cd,
                return r;
        }
 
-       r = LUKS_write_phdr(&cd->hdr, cd);
+       r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
 
        return r;
 }
@@ -990,21 +1120,23 @@ static int _crypt_format_loopaes(struct crypt_device *cd,
                return -EINVAL;
        }
 
+       if (uuid) {
+               log_err(cd, _("UUID is not supported for this crypt type.\n"));
+               return -EINVAL;
+       }
+
        if (!(cd->type = strdup(CRYPT_LOOPAES)))
                return -ENOMEM;
 
-       cd->loopaes_key_size = volume_key_size;
+       cd->u.loopaes.key_size = volume_key_size;
 
-       cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
-
-       if (uuid)
-               cd->loopaes_uuid = strdup(uuid);
+       cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
 
        if (params && params->hash)
-               cd->loopaes_hdr.hash = strdup(params->hash);
+               cd->u.loopaes.hdr.hash = strdup(params->hash);
 
-       cd->loopaes_hdr.offset = params ? params->offset : 0;
-       cd->loopaes_hdr.skip = params ? params->skip : 0;
+       cd->u.loopaes.hdr.offset = params ? params->offset : 0;
+       cd->u.loopaes.hdr.skip = params ? params->skip : 0;
 
        return 0;
 }
@@ -1051,9 +1183,9 @@ static int _crypt_format_verity(struct crypt_device *cd,
                if (r < 0)
                        return r;
 
-               cd->verity_hdr.data_size = data_device_size / params->data_block_size;
+               cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
        } else
-               cd->verity_hdr.data_size = params->data_size;
+               cd->u.verity.hdr.data_size = params->data_size;
 
        hash_size = crypt_hash_size(params->hash_name);
        if (hash_size <= 0) {
@@ -1061,50 +1193,53 @@ static int _crypt_format_verity(struct crypt_device *cd,
                        params->hash_name);
                return -EINVAL;
        }
-       cd->verity_root_hash_size = hash_size;
+       cd->u.verity.root_hash_size = hash_size;
+
+       cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
+       if (!cd->u.verity.root_hash)
+               return -ENOMEM;
 
-       cd->verity_root_hash = malloc(cd->verity_root_hash_size);
-       if (!cd->verity_root_hash)
+       cd->u.verity.hdr.flags = params->flags;
+       if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
+               return -ENOMEM;
+       cd->u.verity.hdr.data_device = NULL;
+       cd->u.verity.hdr.data_block_size = params->data_block_size;
+       cd->u.verity.hdr.hash_block_size = params->hash_block_size;
+       cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
+       cd->u.verity.hdr.hash_type = params->hash_type;
+       cd->u.verity.hdr.flags = params->flags;
+       cd->u.verity.hdr.salt_size = params->salt_size;
+       if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
                return -ENOMEM;
 
-       cd->verity_hdr.flags = params->flags;
-       cd->verity_hdr.hash_name = strdup(params->hash_name);
-       cd->verity_hdr.data_device = NULL;
-       cd->verity_hdr.data_block_size = params->data_block_size;
-       cd->verity_hdr.hash_block_size = params->hash_block_size;
-       cd->verity_hdr.hash_area_offset = params->hash_area_offset;
-       cd->verity_hdr.hash_type = params->hash_type;
-       cd->verity_hdr.flags = params->flags;
-       cd->verity_hdr.salt_size = params->salt_size;
-       cd->verity_hdr.salt = malloc(params->salt_size);
        if (params->salt)
-               memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
+               memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
                       params->salt_size);
        else
-               r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
+               r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
                                     params->salt_size, CRYPT_RND_SALT);
        if (r)
                return r;
 
        if (params->flags & CRYPT_VERITY_CREATE_HASH) {
-               r = VERITY_create(cd, &cd->verity_hdr,
-                                 cd->verity_root_hash, cd->verity_root_hash_size);
+               r = VERITY_create(cd, &cd->u.verity.hdr,
+                                 cd->u.verity.root_hash, cd->u.verity.root_hash_size);
                if (r)
                        return r;
        }
 
        if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
                if (uuid)
-                       cd->verity_uuid = strdup(uuid);
+                       cd->u.verity.uuid = strdup(uuid);
                else {
-                       r = VERITY_UUID_generate(cd, &cd->verity_uuid);
+                       r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
                        if (r)
                                return r;
                }
 
-               r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
-                                   cd->verity_uuid,
-                                   &cd->verity_hdr);
+               r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
+                                   cd->u.verity.uuid,
+                                   &cd->u.verity.hdr);
        }
        return r;
 }
@@ -1130,6 +1265,8 @@ int crypt_format(struct crypt_device *cd,
 
        log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
 
+       crypt_reset_null_type(cd);
+
        r = init_crypto(cd);
        if (r < 0)
                return r;
@@ -1150,8 +1287,7 @@ int crypt_format(struct crypt_device *cd,
        }
 
        if (r < 0) {
-               free(cd->type);
-               cd->type = NULL;
+               crypt_set_null_type(cd);
                crypt_free_volume_key(cd->volume_key);
                cd->volume_key = NULL;
        }
@@ -1171,6 +1307,8 @@ int crypt_load(struct crypt_device *cd,
        if (!crypt_metadata_device(cd))
                return -EINVAL;
 
+       crypt_reset_null_type(cd);
+
        if (!requested_type || isLUKS(requested_type)) {
                if (cd->type && !isLUKS(cd->type)) {
                        log_dbg("Context is already initialised to type %s", cd->type);
@@ -1184,6 +1322,12 @@ int crypt_load(struct crypt_device *cd,
                        return -EINVAL;
                }
                r = _crypt_load_verity(cd, params);
+       } else if (isTCRYPT(requested_type)) {
+               if (cd->type && !isTCRYPT(cd->type)) {
+                       log_dbg("Context is already initialised to type %s", cd->type);
+                       return -EINVAL;
+               }
+               r = _crypt_load_tcrypt(cd, params);
        } else
                return -EINVAL;
 
@@ -1213,10 +1357,8 @@ int crypt_repair(struct crypt_device *cd,
 
        /* cd->type and header must be set in context */
        r = crypt_check_data_device_size(cd);
-       if (r < 0) {
-               free(cd->type);
-               cd->type = NULL;
-       }
+       if (r < 0)
+               crypt_set_null_type(cd);
 
        return r;
 }
@@ -1227,7 +1369,7 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
        int r;
 
        /* Device context type must be initialised */
-       if (!cd->type || !crypt_get_uuid(cd))
+       if (!cd->type)
                return -EINVAL;
 
        log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
@@ -1256,7 +1398,10 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
                r = 0;
        } else {
                dmd.size = new_size;
-               r = dm_create_device(cd, name, cd->type, &dmd, 1);
+               if (isTCRYPT(cd->type))
+                       r = -ENOTSUP;
+               else
+                       r = dm_create_device(cd, name, cd->type, &dmd, 1);
        }
 out:
        if (dmd.target == DM_CRYPT) {
@@ -1276,7 +1421,7 @@ int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
                return  -EINVAL;
        }
 
-       if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
+       if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
                log_dbg("UUID is the same as requested (%s) for device %s.",
                        uuid, mdata_device_path(cd));
                return 0;
@@ -1290,7 +1435,7 @@ int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
        if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
                return -EPERM;
 
-       return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
+       return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
 }
 
 int crypt_header_backup(struct crypt_device *cd,
@@ -1302,6 +1447,9 @@ int crypt_header_backup(struct crypt_device *cd,
        if ((requested_type && !isLUKS(requested_type)) || !backup_file)
                return -EINVAL;
 
+       if (cd->type && !isLUKS(cd->type))
+               return -EINVAL;
+
        r = init_crypto(cd);
        if (r < 0)
                return r;
@@ -1309,18 +1457,23 @@ int crypt_header_backup(struct crypt_device *cd,
        log_dbg("Requested header backup of device %s (%s) to "
                "file %s.", mdata_device_path(cd), requested_type, backup_file);
 
-       return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
+       r = LUKS_hdr_backup(backup_file, cd);
+       return r;
 }
 
 int crypt_header_restore(struct crypt_device *cd,
                         const char *requested_type,
                         const char *backup_file)
 {
+       struct luks_phdr hdr;
        int r;
 
        if (requested_type && !isLUKS(requested_type))
                return -EINVAL;
 
+       if (cd->type && !isLUKS(cd->type))
+               return -EINVAL;
+
        r = init_crypto(cd);
        if (r < 0)
                return r;
@@ -1328,7 +1481,10 @@ int crypt_header_restore(struct crypt_device *cd,
        log_dbg("Requested header restore to device %s (%s) from "
                "file %s.", mdata_device_path(cd), requested_type, backup_file);
 
-       return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
+       r = LUKS_hdr_restore(backup_file, isLUKS(cd->type) ? &cd->u.luks1.hdr : &hdr, cd);
+
+       crypt_memzero(&hdr, sizeof(hdr));
+       return r;
 }
 
 void crypt_free(struct crypt_device *cd)
@@ -1341,25 +1497,26 @@ void crypt_free(struct crypt_device *cd)
 
                device_free(cd->device);
                device_free(cd->metadata_device);
-               free(cd->type);
-
-               /* used in plain device only */
-               free(CONST_CAST(void*)cd->plain_hdr.hash);
-               free(cd->plain_cipher);
-               free(cd->plain_cipher_mode);
-               free(cd->plain_uuid);
 
-               /* used in loop-AES device only */
-               free(CONST_CAST(void*)cd->loopaes_hdr.hash);
-               free(cd->loopaes_cipher);
-               free(cd->loopaes_uuid);
-
-               /* used in verity device only */
-               free(CONST_CAST(void*)cd->verity_hdr.hash_name);
-               free(CONST_CAST(void*)cd->verity_hdr.salt);
-               free(cd->verity_root_hash);
-               free(cd->verity_uuid);
+               if (isPLAIN(cd->type)) {
+                       free(CONST_CAST(void*)cd->u.plain.hdr.hash);
+                       free(cd->u.plain.cipher);
+                       free(cd->u.plain.cipher_mode);
+               } else if (isLOOPAES(cd->type)) {
+                       free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
+                       free(cd->u.loopaes.cipher);
+               } else if (isVERITY(cd->type)) {
+                       free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
+                       free(CONST_CAST(void*)cd->u.verity.hdr.salt);
+                       free(cd->u.verity.root_hash);
+                       free(cd->u.verity.uuid);
+               } else if (!cd->type) {
+                       free(cd->u.none.active_name);
+               }
 
+               free(cd->type);
+               /* Some structures can contain keys (TCRYPT), wipe it */
+               crypt_memzero(cd, sizeof(*cd));
                free(cd);
        }
 }
@@ -1372,20 +1529,24 @@ int crypt_suspend(struct crypt_device *cd,
 
        log_dbg("Suspending volume %s.", name);
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               r = -EINVAL;
-               goto out;
+       if (cd->type) {
+               r = onlyLUKS(cd);
+       } else {
+               r = crypt_uuid_type_cmp(cd, CRYPT_LUKS1);
+               if (r < 0)
+                       log_err(cd, _("This operation is supported only for LUKS device.\n"));
        }
 
+       if (r < 0)
+               return r;
+
        ci = crypt_status(NULL, name);
        if (ci < CRYPT_ACTIVE) {
                log_err(cd, _("Volume %s is not active.\n"), name);
                return -EINVAL;
        }
 
-       if (!cd)
-               dm_backend_init();
+       dm_backend_init();
 
        r = dm_status_suspended(cd, name);
        if (r < 0)
@@ -1399,12 +1560,11 @@ int crypt_suspend(struct crypt_device *cd,
 
        r = dm_suspend_and_wipe_key(cd, name);
        if (r == -ENOTSUP)
-               log_err(cd, "Suspend is not supported for device %s.\n", name);
+               log_err(cd, _("Suspend is not supported for device %s.\n"), name);
        else if (r)
-               log_err(cd, "Error during suspending device %s.\n", name);
+               log_err(cd, _("Error during suspending device %s.\n"), name);
 out:
-       if (!cd)
-               dm_backend_exit();
+       dm_backend_exit();
        return r;
 }
 
@@ -1419,11 +1579,9 @@ int crypt_resume_by_passphrase(struct crypt_device *cd,
 
        log_dbg("Resuming volume %s.", name);
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               r = -EINVAL;
-               goto out;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        r = dm_status_suspended(cd, name);
        if (r < 0)
@@ -1436,7 +1594,7 @@ int crypt_resume_by_passphrase(struct crypt_device *cd,
 
        if (passphrase) {
                r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
-                                          &cd->hdr, &vk, cd);
+                                          &cd->u.luks1.hdr, &vk, cd);
        } else
                r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
 
@@ -1444,12 +1602,12 @@ int crypt_resume_by_passphrase(struct crypt_device *cd,
                keyslot = r;
                r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
                if (r == -ENOTSUP)
-                       log_err(cd, "Resume is not supported for device %s.\n", name);
+                       log_err(cd, _("Resume is not supported for device %s.\n"), name);
                else if (r)
-                       log_err(cd, "Error during resuming device %s.\n", name);
+                       log_err(cd, _("Error during resuming device %s.\n"), name);
        } else
                r = keyslot;
-out:
+
        crypt_free_volume_key(vk);
        return r < 0 ? r : keyslot;
 }
@@ -1468,11 +1626,9 @@ int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
 
        log_dbg("Resuming volume %s.", name);
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               r = -EINVAL;
-               goto out;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        r = dm_status_suspended(cd, name);
        if (r < 0)
@@ -1493,14 +1649,14 @@ int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
                goto out;
 
        r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
-                                  passphrase_size_read, &cd->hdr, &vk, cd);
+                                  passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
        if (r < 0)
                goto out;
 
        keyslot = r;
        r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
        if (r)
-               log_err(cd, "Error during resuming device %s.\n", name);
+               log_err(cd, _("Error during resuming device %s.\n"), name);
 out:
        crypt_safe_free(passphrase_read);
        crypt_free_volume_key(vk);
@@ -1534,16 +1690,15 @@ int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
                "new passphrase %sprovided.",
                passphrase ? "" : "not ", new_passphrase  ? "" : "not ");
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               return -EINVAL;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        r = keyslot_verify_or_find_empty(cd, &keyslot);
        if (r)
                return r;
 
-       if (!LUKS_keyslot_active_count(&cd->hdr)) {
+       if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
                /* No slots used, try to use pre-generated key in header */
                if (cd->volume_key) {
                        vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
@@ -1555,7 +1710,7 @@ int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
        } else if (passphrase) {
                /* Passphrase provided, use it to unlock existing keyslot */
                r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
-                                          passphrase_size, &cd->hdr, &vk, cd);
+                                          passphrase_size, &cd->u.luks1.hdr, &vk, cd);
        } else {
                /* Passphrase not provided, ask first and use it to unlock existing keyslot */
                r = key_from_terminal(cd, _("Enter any passphrase: "),
@@ -1564,7 +1719,7 @@ int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
                        goto out;
 
                r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
-                                          passwordLen, &cd->hdr, &vk, cd);
+                                          passwordLen, &cd->u.luks1.hdr, &vk, cd);
                crypt_safe_free(password);
        }
 
@@ -1582,15 +1737,76 @@ int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
        }
 
        r = LUKS_set_key(keyslot, new_password, new_passwordLen,
-                        &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
-       if(r < 0) goto out;
+                        &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
+       if(r < 0)
+               goto out;
 
        r = 0;
 out:
        if (!new_passphrase)
                crypt_safe_free(new_password);
        crypt_free_volume_key(vk);
-       return r ?: keyslot;
+       return r < 0 ? r : keyslot;
+}
+
+int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
+       int keyslot_old,
+       int keyslot_new,
+       const char *passphrase,
+       size_t passphrase_size,
+       const char *new_passphrase,
+       size_t new_passphrase_size)
+{
+       struct volume_key *vk = NULL;
+       int r;
+
+       log_dbg("Changing passphrase from old keyslot %d to new %d.",
+               keyslot_old, keyslot_new);
+
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
+
+       r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
+                                  &cd->u.luks1.hdr, &vk, cd);
+       if (r < 0)
+               goto out;
+
+       if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
+               log_dbg("Keyslot mismatch.");
+               goto out;
+       }
+       keyslot_old = r;
+
+       if (keyslot_new == CRYPT_ANY_SLOT) {
+               keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
+               if (keyslot_new < 0)
+                       keyslot_new = keyslot_old;
+       }
+
+       if (keyslot_old == keyslot_new) {
+               log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
+               (void)crypt_keyslot_destroy(cd, keyslot_old);
+       }
+
+       r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
+                        &cd->u.luks1.hdr, vk, cd->iteration_time,
+                        &cd->u.luks1.PBKDF2_per_sec, cd);
+
+       if (keyslot_old == keyslot_new) {
+               if (r >= 0)
+                       log_verbose(cd, _("Key slot %d changed.\n"), keyslot_new);
+       } else {
+               if (r >= 0) {
+                       log_verbose(cd, _("Replaced with key slot %d.\n"), keyslot_new);
+                       r = crypt_keyslot_destroy(cd, keyslot_old);
+               }
+       }
+       if (r < 0)
+               log_err(cd, _("Failed to swap new key slot.\n"));
+out:
+       crypt_free_volume_key(vk);
+       return r < 0 ? r : keyslot_new;
 }
 
 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
@@ -1610,16 +1826,15 @@ int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
        log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
                keyfile ?: "[none]", new_keyfile ?: "[none]");
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               return -EINVAL;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        r = keyslot_verify_or_find_empty(cd, &keyslot);
        if (r)
                return r;
 
-       if (!LUKS_keyslot_active_count(&cd->hdr)) {
+       if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
                /* No slots used, try to use pre-generated key in header */
                if (cd->volume_key) {
                        vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
@@ -1641,7 +1856,7 @@ int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
                        goto out;
 
                r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
-                                          &cd->hdr, &vk, cd);
+                                          &cd->u.luks1.hdr, &vk, cd);
        }
 
        if(r < 0)
@@ -1658,7 +1873,7 @@ int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
                goto out;
 
        r = LUKS_set_key(keyslot, new_password, new_passwordLen,
-                        &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
+                        &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
 out:
        crypt_safe_free(password);
        crypt_safe_free(new_password);
@@ -1686,15 +1901,14 @@ int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
        size_t passphrase_size)
 {
        struct volume_key *vk = NULL;
-       int r = -EINVAL;
+       int r;
        char *new_password = NULL; size_t new_passwordLen;
 
        log_dbg("Adding new keyslot %d using volume key.", keyslot);
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               return -EINVAL;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        if (volume_key)
                vk = crypt_alloc_volume_key(volume_key_size, volume_key);
@@ -1704,7 +1918,7 @@ int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
        if (!vk)
                return -ENOMEM;
 
-       r = LUKS_verify_volume_key(&cd->hdr, vk);
+       r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
        if (r < 0) {
                log_err(cd, _("Volume key does not match the volume.\n"));
                goto out;
@@ -1724,7 +1938,7 @@ int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
        }
 
        r = LUKS_set_key(keyslot, passphrase, passphrase_size,
-                        &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
+                        &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
 out:
        crypt_safe_free(new_password);
        crypt_free_volume_key(vk);
@@ -1734,13 +1948,13 @@ out:
 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
 {
        crypt_keyslot_info ki;
+       int r;
 
        log_dbg("Destroying keyslot %d.", keyslot);
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               return -EINVAL;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        ki = crypt_keyslot_status(cd, keyslot);
        if (ki == CRYPT_SLOT_INVALID) {
@@ -1753,7 +1967,7 @@ int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
                return -EINVAL;
        }
 
-       return LUKS_del_key(keyslot, &cd->hdr, cd);
+       return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
 }
 
 // activation/deactivation of device mapping
@@ -1798,19 +2012,19 @@ int crypt_activate_by_passphrase(struct crypt_device *cd,
                        passphrase_size = passphraseLen;
                }
 
-               r = process_key(cd, cd->plain_hdr.hash,
-                               cd->plain_key_size,
+               r = process_key(cd, cd->u.plain.hdr.hash,
+                               cd->u.plain.key_size,
                                passphrase, passphrase_size, &vk);
                if (r < 0)
                        goto out;
 
-               r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
+               r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
                keyslot = 0;
        } else if (isLUKS(cd->type)) {
                /* provided passphrase, do not retry */
                if (passphrase) {
                        r = LUKS_open_key_with_hdr(keyslot, passphrase,
-                                                  passphrase_size, &cd->hdr, &vk, cd);
+                                                  passphrase_size, &cd->u.luks1.hdr, &vk, cd);
                } else
                        r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
 
@@ -1869,20 +2083,20 @@ int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
                if (r < 0)
                        goto out;
 
-               r = process_key(cd, cd->plain_hdr.hash,
-                               cd->plain_key_size,
+               r = process_key(cd, cd->u.plain.hdr.hash,
+                               cd->u.plain.key_size,
                                passphrase_read, passphrase_size_read, &vk);
                if (r < 0)
                        goto out;
 
-               r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
+               r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
        } else if (isLUKS(cd->type)) {
                r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
                          &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
                if (r < 0)
                        goto out;
                r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
-                                          passphrase_size_read, &cd->hdr, &vk, cd);
+                                          passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
                if (r < 0)
                        goto out;
                keyslot = r;
@@ -1898,12 +2112,12 @@ int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
                                  keyfile, keyfile_offset, keyfile_size);
                if (r < 0)
                        goto out;
-               r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
+               r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
                                          passphrase_read, passphrase_size_read);
                if (r < 0)
                        goto out;
                if (name)
-                       r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
+                       r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
                                             key_count, vk, flags);
        } else
                r = -EINVAL;
@@ -1953,7 +2167,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
                if (!name)
                        return -EINVAL;
 
-               if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
+               if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
                        log_err(cd, _("Incorrect volume key specified for plain device.\n"));
                        return -EINVAL;
                }
@@ -1962,7 +2176,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
                if (!vk)
                        return -ENOMEM;
 
-               r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
+               r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
        } else if (isLUKS(cd->type)) {
                /* If key is not provided, try to use internal key */
                if (!volume_key) {
@@ -1977,7 +2191,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
                vk = crypt_alloc_volume_key(volume_key_size, volume_key);
                if (!vk)
                        return -ENOMEM;
-               r = LUKS_verify_volume_key(&cd->hdr, vk);
+               r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
 
                if (r == -EPERM)
                        log_err(cd, _("Volume key does not match the volume.\n"));
@@ -1992,18 +2206,23 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
                }
 
                r = VERITY_activate(cd, name, volume_key, volume_key_size,
-                                   &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
+                                   &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
 
                if (r == -EPERM) {
-                       free(cd->verity_root_hash);
-                       cd->verity_root_hash = NULL;
+                       free(cd->u.verity.root_hash);
+                       cd->u.verity.root_hash = NULL;
                } if (!r) {
-                       cd->verity_root_hash_size = volume_key_size;
-                       if (!cd->verity_root_hash)
-                               cd->verity_root_hash = malloc(volume_key_size);
-                       if (cd->verity_root_hash)
-                               memcpy(cd->verity_root_hash, volume_key, volume_key_size);
+                       cd->u.verity.root_hash_size = volume_key_size;
+                       if (!cd->u.verity.root_hash)
+                               cd->u.verity.root_hash = malloc(volume_key_size);
+                       if (cd->u.verity.root_hash)
+                               memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
                }
+       } else if (isTCRYPT(cd->type)) {
+               if (!name)
+                       return 0;
+               r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
+                                   &cd->u.tcrypt.params, flags);
        } else
                log_err(cd, _("Device type is not properly initialised.\n"));
 
@@ -2014,6 +2233,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
 
 int crypt_deactivate(struct crypt_device *cd, const char *name)
 {
+       struct crypt_device *fake_cd = NULL;
        int r;
 
        if (!name)
@@ -2021,13 +2241,24 @@ int crypt_deactivate(struct crypt_device *cd, const char *name)
 
        log_dbg("Deactivating volume %s.", name);
 
-       if (!cd)
-               dm_backend_init();
+       if (!cd) {
+               r = crypt_init_by_name(&fake_cd, name);
+               if (r < 0)
+                       return r;
+               cd = fake_cd;
+       }
 
        switch (crypt_status(cd, name)) {
                case CRYPT_ACTIVE:
                case CRYPT_BUSY:
-                       r = dm_remove_device(cd, name, 0, 0);
+                       if (isTCRYPT(cd->type))
+                               r = TCRYPT_deactivate(cd, name);
+                       else
+                               r = dm_remove_device(cd, name, 0, 0);
+                       if (r < 0 && crypt_status(cd, name) == CRYPT_BUSY) {
+                               log_err(cd, _("Device %s is still in use.\n"), name);
+                               r = -EBUSY;
+                       }
                        break;
                case CRYPT_INACTIVE:
                        log_err(cd, _("Device %s is not active.\n"), name);
@@ -2038,8 +2269,7 @@ int crypt_deactivate(struct crypt_device *cd, const char *name)
                        r = -EINVAL;
        }
 
-       if (!cd)
-               dm_backend_exit();
+       crypt_free(fake_cd);
 
        return r;
 }
@@ -2056,7 +2286,7 @@ int crypt_volume_key_get(struct crypt_device *cd,
        int r = -EINVAL;
 
        if (crypt_fips_mode()) {
-               log_err(cd, "Function not available in FIPS mode.\n");
+               log_err(cd, _("Function not available in FIPS mode.\n"));
                return -EACCES;
        }
 
@@ -2066,15 +2296,16 @@ int crypt_volume_key_get(struct crypt_device *cd,
                return -ENOMEM;
        }
 
-       if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
-               r = process_key(cd, cd->plain_hdr.hash, key_len,
+       if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
+               r = process_key(cd, cd->u.plain.hdr.hash, key_len,
                                passphrase, passphrase_size, &vk);
                if (r < 0)
                        log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
        } else if (isLUKS(cd->type)) {
                r = LUKS_open_key_with_hdr(keyslot, passphrase,
-                                       passphrase_size, &cd->hdr, &vk, cd);
-
+                                       passphrase_size, &cd->u.luks1.hdr, &vk, cd);
+       } else if (isTCRYPT(cd->type)) {
+               r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
        } else
                log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
 
@@ -2094,16 +2325,15 @@ int crypt_volume_key_verify(struct crypt_device *cd,
        struct volume_key *vk;
        int r;
 
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
-               return -EINVAL;
-       }
+       r = onlyLUKS(cd);
+       if (r < 0)
+               return r;
 
        vk = crypt_alloc_volume_key(volume_key_size, volume_key);
        if (!vk)
                return -ENOMEM;
 
-       r = LUKS_verify_volume_key(&cd->hdr, vk);
+       r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
 
        if (r == -EPERM)
                log_err(cd, _("Volume key does not match the volume.\n"));
@@ -2201,39 +2431,39 @@ static int _luks_dump(struct crypt_device *cd)
        int i;
 
        log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
-       log_std(cd, "Version:       \t%d\n", cd->hdr.version);
-       log_std(cd, "Cipher name:   \t%s\n", cd->hdr.cipherName);
-       log_std(cd, "Cipher mode:   \t%s\n", cd->hdr.cipherMode);
-       log_std(cd, "Hash spec:     \t%s\n", cd->hdr.hashSpec);
-       log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
-       log_std(cd, "MK bits:       \t%d\n", cd->hdr.keyBytes * 8);
+       log_std(cd, "Version:       \t%" PRIu16 "\n", cd->u.luks1.hdr.version);
+       log_std(cd, "Cipher name:   \t%s\n", cd->u.luks1.hdr.cipherName);
+       log_std(cd, "Cipher mode:   \t%s\n", cd->u.luks1.hdr.cipherMode);
+       log_std(cd, "Hash spec:     \t%s\n", cd->u.luks1.hdr.hashSpec);
+       log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset);
+       log_std(cd, "MK bits:       \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8);
        log_std(cd, "MK digest:     \t");
-       hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
+       hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
        log_std(cd, "\n");
        log_std(cd, "MK salt:       \t");
-       hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
+       hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
        log_std(cd, "\n               \t");
-       hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
+       hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
        log_std(cd, "\n");
-       log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
-       log_std(cd, "UUID:          \t%s\n\n", cd->hdr.uuid);
+       log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations);
+       log_std(cd, "UUID:          \t%s\n\n", cd->u.luks1.hdr.uuid);
        for(i = 0; i < LUKS_NUMKEYS; i++) {
-               if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
+               if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
                        log_std(cd, "Key Slot %d: ENABLED\n",i);
-                       log_std(cd, "\tIterations:         \t%d\n",
-                               cd->hdr.keyblock[i].passwordIterations);
+                       log_std(cd, "\tIterations:         \t%" PRIu32 "\n",
+                               cd->u.luks1.hdr.keyblock[i].passwordIterations);
                        log_std(cd, "\tSalt:               \t");
-                       hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
+                       hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
                                 LUKS_SALTSIZE/2, " ");
                        log_std(cd, "\n\t                      \t");
-                       hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
+                       hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
                                 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
                        log_std(cd, "\n");
 
-                       log_std(cd, "\tKey material offset:\t%d\n",
-                               cd->hdr.keyblock[i].keyMaterialOffset);
-                       log_std(cd, "\tAF stripes:            \t%d\n",
-                               cd->hdr.keyblock[i].stripes);
+                       log_std(cd, "\tKey material offset:\t%" PRIu32 "\n",
+                               cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
+                       log_std(cd, "\tAF stripes:            \t%" PRIu32 "\n",
+                               cd->u.luks1.hdr.keyblock[i].stripes);
                }
                else 
                        log_std(cd, "Key Slot %d: DISABLED\n", i);
@@ -2244,21 +2474,21 @@ static int _luks_dump(struct crypt_device *cd)
 static int _verity_dump(struct crypt_device *cd)
 {
        log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
-       log_std(cd, "UUID:            \t%s\n", cd->verity_uuid ?: "");
-       log_std(cd, "Hash type:       \t%u\n", cd->verity_hdr.hash_type);
-       log_std(cd, "Data blocks:     \t%" PRIu64 "\n", cd->verity_hdr.data_size);
-       log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
-       log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
-       log_std(cd, "Hash algorithm:  \t%s\n", cd->verity_hdr.hash_name);
+       log_std(cd, "UUID:            \t%s\n", cd->u.verity.uuid ?: "");
+       log_std(cd, "Hash type:       \t%u\n", cd->u.verity.hdr.hash_type);
+       log_std(cd, "Data blocks:     \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
+       log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
+       log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
+       log_std(cd, "Hash algorithm:  \t%s\n", cd->u.verity.hdr.hash_name);
        log_std(cd, "Salt:            \t");
-       if (cd->verity_hdr.salt_size)
-               hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
+       if (cd->u.verity.hdr.salt_size)
+               hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
        else
                log_std(cd, "-");
        log_std(cd, "\n");
-       if (cd->verity_root_hash) {
+       if (cd->u.verity.root_hash) {
                log_std(cd, "Root hash:      \t");
-               hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
+               hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
                log_std(cd, "\n");
        }
        return 0;
@@ -2270,21 +2500,54 @@ int crypt_dump(struct crypt_device *cd)
                return _luks_dump(cd);
        else if (isVERITY(cd->type))
                return _verity_dump(cd);
+       else if (isTCRYPT(cd->type))
+               return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
 
        log_err(cd, _("Dump operation is not supported for this device type.\n"));
        return -EINVAL;
 }
 
+
+static int _init_by_name_crypt_none(struct crypt_device *cd)
+{
+       struct crypt_dm_active_device dmd = {};
+       int r;
+
+       if (cd->type || !cd->u.none.active_name)
+               return -EINVAL;
+
+       r = dm_query_device(cd, cd->u.none.active_name,
+                       DM_ACTIVE_CRYPT_CIPHER |
+                       DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
+       if (r >= 0)
+               r = crypt_parse_name_and_mode(dmd.u.crypt.cipher,
+                                             cd->u.none.cipher, NULL,
+                                             cd->u.none.cipher_mode);
+
+       if (!r)
+               cd->u.none.key_size = dmd.u.crypt.vk->keylength;
+
+       crypt_free_volume_key(dmd.u.crypt.vk);
+       free(CONST_CAST(void*)dmd.u.crypt.cipher);
+       return r;
+}
+
 const char *crypt_get_cipher(struct crypt_device *cd)
 {
        if (isPLAIN(cd->type))
-               return cd->plain_cipher;
+               return cd->u.plain.cipher;
 
        if (isLUKS(cd->type))
-               return cd->hdr.cipherName;
+               return cd->u.luks1.hdr.cipherName;
 
        if (isLOOPAES(cd->type))
-               return cd->loopaes_cipher;
+               return cd->u.loopaes.cipher;
+
+       if (isTCRYPT(cd->type))
+               return cd->u.tcrypt.params.cipher;
+
+       if (!cd->type && !_init_by_name_crypt_none(cd))
+               return cd->u.none.cipher;
 
        return NULL;
 }
@@ -2292,13 +2555,19 @@ const char *crypt_get_cipher(struct crypt_device *cd)
 const char *crypt_get_cipher_mode(struct crypt_device *cd)
 {
        if (isPLAIN(cd->type))
-               return cd->plain_cipher_mode;
+               return cd->u.plain.cipher_mode;
 
        if (isLUKS(cd->type))
-               return cd->hdr.cipherMode;
+               return cd->u.luks1.hdr.cipherMode;
 
        if (isLOOPAES(cd->type))
-               return cd->loopaes_cipher_mode;
+               return cd->u.loopaes.cipher_mode;
+
+       if (isTCRYPT(cd->type))
+               return cd->u.tcrypt.params.mode;
+
+       if (!cd->type && !_init_by_name_crypt_none(cd))
+               return cd->u.none.cipher_mode;
 
        return NULL;
 }
@@ -2306,16 +2575,10 @@ const char *crypt_get_cipher_mode(struct crypt_device *cd)
 const char *crypt_get_uuid(struct crypt_device *cd)
 {
        if (isLUKS(cd->type))
-               return cd->hdr.uuid;
-
-       if (isPLAIN(cd->type))
-               return cd->plain_uuid;
-
-       if (isLOOPAES(cd->type))
-               return cd->loopaes_uuid;
+               return cd->u.luks1.hdr.uuid;
 
        if (isVERITY(cd->type))
-               return cd->verity_uuid;
+               return cd->u.verity.uuid;
 
        return NULL;
 }
@@ -2333,16 +2596,22 @@ const char *crypt_get_device_name(struct crypt_device *cd)
 int crypt_get_volume_key_size(struct crypt_device *cd)
 {
        if (isPLAIN(cd->type))
-               return cd->plain_key_size;
+               return cd->u.plain.key_size;
 
        if (isLUKS(cd->type))
-               return cd->hdr.keyBytes;
+               return cd->u.luks1.hdr.keyBytes;
 
        if (isLOOPAES(cd->type))
-               return cd->loopaes_key_size;
+               return cd->u.loopaes.key_size;
 
        if (isVERITY(cd->type))
-               return cd->verity_root_hash_size;
+               return cd->u.verity.root_hash_size;
+
+       if (isTCRYPT(cd->type))
+               return cd->u.tcrypt.params.key_size;
+
+       if (!cd->type && !_init_by_name_crypt_none(cd))
+               return cd->u.none.key_size;
 
        return 0;
 }
@@ -2350,13 +2619,16 @@ int crypt_get_volume_key_size(struct crypt_device *cd)
 uint64_t crypt_get_data_offset(struct crypt_device *cd)
 {
        if (isPLAIN(cd->type))
-               return cd->plain_hdr.offset;
+               return cd->u.plain.hdr.offset;
 
        if (isLUKS(cd->type))
-               return cd->hdr.payloadOffset;
+               return cd->u.luks1.hdr.payloadOffset;
 
        if (isLOOPAES(cd->type))
-               return cd->loopaes_hdr.offset;
+               return cd->u.loopaes.hdr.offset;
+
+       if (isTCRYPT(cd->type))
+               return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
 
        return 0;
 }
@@ -2364,25 +2636,26 @@ uint64_t crypt_get_data_offset(struct crypt_device *cd)
 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
 {
        if (isPLAIN(cd->type))
-               return cd->plain_hdr.skip;
+               return cd->u.plain.hdr.skip;
 
        if (isLUKS(cd->type))
                return 0;
 
        if (isLOOPAES(cd->type))
-               return cd->loopaes_hdr.skip;
+               return cd->u.loopaes.hdr.skip;
+
+       if (isTCRYPT(cd->type))
+               return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
 
        return 0;
 }
 
 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
 {
-       if (!isLUKS(cd->type)) {
-               log_err(cd, _("This operation is supported only for LUKS device.\n"));
+       if (onlyLUKS(cd) < 0)
                return CRYPT_SLOT_INVALID;
-       }
 
-       return LUKS_keyslot_info(&cd->hdr, keyslot);
+       return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
 }
 
 int crypt_keyslot_max(const char *type)
@@ -2401,7 +2674,7 @@ int crypt_keyslot_area(struct crypt_device *cd,
        if (!isLUKS(cd->type))
                return -EINVAL;
 
-       return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
+       return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
 }
 
 const char *crypt_get_type(struct crypt_device *cd)
@@ -2417,20 +2690,19 @@ int crypt_get_verity_info(struct crypt_device *cd,
 
        vp->data_device = device_path(cd->device);
        vp->hash_device = mdata_device_path(cd);
-       vp->hash_name = cd->verity_hdr.hash_name;
-       vp->salt = cd->verity_hdr.salt;
-       vp->salt_size = cd->verity_hdr.salt_size;
-       vp->data_block_size = cd->verity_hdr.data_block_size;
-       vp->hash_block_size = cd->verity_hdr.hash_block_size;
-       vp->data_size = cd->verity_hdr.data_size;
-       vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
-       vp->hash_type = cd->verity_hdr.hash_type;
-       vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
+       vp->hash_name = cd->u.verity.hdr.hash_name;
+       vp->salt = cd->u.verity.hdr.salt;
+       vp->salt_size = cd->u.verity.hdr.salt_size;
+       vp->data_block_size = cd->u.verity.hdr.data_block_size;
+       vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
+       vp->data_size = cd->u.verity.hdr.data_size;
+       vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
+       vp->hash_type = cd->u.verity.hdr.hash_type;
+       vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
        return 0;
 }
 
-int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
-                           const char *name,
+int crypt_get_active_device(struct crypt_device *cd, const char *name,
                            struct crypt_active_device *cad)
 {
        struct crypt_dm_active_device dmd;
@@ -2443,8 +2715,13 @@ int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
        if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
                return -ENOTSUP;
 
-       cad->offset     = dmd.u.crypt.offset;
-       cad->iv_offset  = dmd.u.crypt.iv_offset;
+       if (cd && isTCRYPT(cd->type)) {
+               cad->offset     = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
+               cad->iv_offset  = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
+       } else {
+               cad->offset     = dmd.u.crypt.offset;
+               cad->iv_offset  = dmd.u.crypt.iv_offset;
+       }
        cad->size       = dmd.size;
        cad->flags      = dmd.flags;