TCRYPT: add dump command
[platform/upstream/cryptsetup.git] / lib / setup.c
index ac732b2..156e270 100644 (file)
 #include "luks.h"
 #include "loopaes.h"
 #include "verity.h"
+#include "tcrypt.h"
 #include "internal.h"
 
 struct crypt_device {
        char *type;
 
-       char *device;
-       char *metadata_device;
-
-       /* loopback automatic detach helpers */
-       int loop_device_fd;
-       int loop_metadata_device_fd;
+       struct device *device;
+       struct device *metadata_device;
 
        struct volume_key *volume_key;
        uint64_t timeout;
@@ -49,6 +46,10 @@ struct crypt_device {
        int password_verify;
        int rng_type;
 
+       // FIXME: switch to union
+       // FIXME: privatre binary headers and access it properly
+       // through sub-library (LUKS1, TCRYPT)
+
        /* used in CRYPT_LUKS1 */
        struct luks_phdr hdr;
        uint64_t PBKDF2_per_sec;
@@ -70,9 +71,13 @@ struct crypt_device {
        /* used in CRYPT_VERITY */
        struct crypt_params_verity verity_hdr;
        char *verity_root_hash;
-       uint64_t verity_root_hash_size;
+       unsigned int verity_root_hash_size;
        char *verity_uuid;
 
+       /* used in CRYPT_TCRYPT */
+       struct crypt_params_tcrypt tcrypt_params;
+       struct tcrypt_phdr tcrypt_hdr;
+
        /* callbacks definitions */
        void (*log)(int level, const char *msg, void *usrptr);
        void *log_usrptr;
@@ -156,12 +161,23 @@ void logger(struct crypt_device *cd, int level, const char *file,
        free(target);
 }
 
-static const char *mdata_device(struct crypt_device *cd)
+static const char *mdata_device_path(struct crypt_device *cd)
+{
+       return device_path(cd->metadata_device ?: cd->device);
+}
+
+/* internal only */
+struct device *crypt_metadata_device(struct crypt_device *cd)
 {
        return cd->metadata_device ?: cd->device;
 }
 
-static int init_crypto(struct crypt_device *ctx)
+struct device *crypt_data_device(struct crypt_device *cd)
+{
+       return cd->device;
+}
+
+int init_crypto(struct crypt_device *ctx)
 {
        int r;
 
@@ -236,6 +252,11 @@ static int isVERITY(const char *type)
        return (type && !strcmp(CRYPT_VERITY, type));
 }
 
+static int isTCRYPT(const char *type)
+{
+       return (type && !strcmp(CRYPT_TCRYPT, type));
+}
+
 /* keyslot helpers */
 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
 {
@@ -307,7 +328,7 @@ int PLAIN_activate(struct crypt_device *cd,
                .uuid   = crypt_get_uuid(cd),
                .size   = size,
                .flags  = flags,
-               .data_device = crypt_get_device_name(cd),
+               .data_device = crypt_data_device(cd),
                .u.crypt  = {
                        .cipher = NULL,
                        .vk     = vk,
@@ -321,8 +342,8 @@ int PLAIN_activate(struct crypt_device *cd,
        else
                device_check = DEV_EXCL;
 
-       r = device_check_and_adjust(cd, dmd.data_device, device_check,
-                                   &dmd.size, &dmd.u.crypt.offset, &dmd.flags);
+       r = device_block_adjust(cd, dmd.data_device, device_check,
+                               dmd.u.crypt.offset, &dmd.size, &dmd.flags);
        if (r)
                return r;
 
@@ -337,10 +358,10 @@ int PLAIN_activate(struct crypt_device *cd,
        log_dbg("Trying to activate PLAIN device %s using cipher %s.",
                name, dmd.u.crypt.cipher);
 
-       r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
+       r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
 
        // FIXME
-       if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
+       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);
@@ -417,7 +438,7 @@ static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslo
                if(r < 0)
                        goto out;
 
-               r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
+               r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
                                           passphrase_size_read, &cd->hdr, vk, cd);
                if (r == -EPERM)
                        eperm = 1;
@@ -504,44 +525,6 @@ const char *crypt_get_dir(void)
        return dm_get_dir();
 }
 
-static int set_device_or_loop(const char *device_org, char **device, int *loop_fd)
-{
-       int r, readonly = 0;
-
-       if (!device_org)
-               return 0;
-
-       r = device_ready(NULL, device_org, O_RDONLY);
-       if (r == -ENOTBLK) {
-               *device = crypt_loop_get_device();
-               log_dbg("Not a block device, %s%s.", *device ?
-                       "using free loop device " : "no free loop device found",
-                       *device ?: "");
-               if (!*device) {
-                       log_err(NULL, _("Cannot find a free loopback device.\n"));
-                       return -ENOSYS;
-               }
-
-               /* Keep the loop open, dettached on last close. */
-               *loop_fd = crypt_loop_attach(*device, device_org, 0, 1, &readonly);
-               if (*loop_fd == -1) {
-                       log_err(NULL, _("Attaching loopback device failed "
-                               "(loop device with autoclear flag is required).\n"));
-                       return -EINVAL;
-               }
-
-               r = device_ready(NULL, *device, O_RDONLY);
-       }
-
-       if (r < 0)
-               return -ENOTBLK;
-
-       if (!*device && device_org && !(*device = strdup(device_org)))
-               return -ENOMEM;
-
-       return 0;
-}
-
 int crypt_init(struct crypt_device **cd, const char *device)
 {
        struct crypt_device *h = NULL;
@@ -556,17 +539,12 @@ int crypt_init(struct crypt_device **cd, const char *device)
                return -ENOMEM;
 
        memset(h, 0, sizeof(*h));
-       h->loop_device_fd = -1;
-       h->loop_metadata_device_fd = -1;
 
-       r = set_device_or_loop(device, &h->device, &h->loop_device_fd);
+       r = device_alloc(&h->device, device);
        if (r < 0)
                goto bad;
 
-       if (dm_init(h, 1) < 0) {
-               r = -ENOSYS;
-               goto bad;
-       }
+       dm_backend_init();
 
        h->iteration_time = 1000;
        h->password_verify = 0;
@@ -575,12 +553,7 @@ int crypt_init(struct crypt_device **cd, const char *device)
        *cd = h;
        return 0;
 bad:
-
-       if (h) {
-               if (h->loop_device_fd != -1)
-                       close(h->loop_device_fd);
-               free(h->device);
-       }
+       device_free(h->device);
        free(h);
        return r;
 }
@@ -593,13 +566,13 @@ static int crypt_check_data_device_size(struct crypt_device *cd)
        /* Check data device size, require at least one sector */
        size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
 
-       r = device_size(crypt_get_device_name(cd), &size);
+       r = device_size(cd->device, &size);
        if (r < 0)
                return r;
 
        if (size < size_min) {
-               log_err(cd, _("LUKS header detected but device %s is too small.\n"),
-                       crypt_get_device_name(cd));
+               log_err(cd, _("Header detected but device %s is too small.\n"),
+                       device_path(cd->device));
                return -EINVAL;
        }
 
@@ -608,8 +581,8 @@ static int crypt_check_data_device_size(struct crypt_device *cd)
 
 int crypt_set_data_device(struct crypt_device *cd, const char *device)
 {
-       char *data_device = NULL;
-       int r, loop_fd = -1;
+       struct device *dev = NULL;
+       int r;
 
        log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
 
@@ -622,21 +595,16 @@ int crypt_set_data_device(struct crypt_device *cd, const char *device)
        if (!cd->device || !device)
                return -EINVAL;
 
-       r = set_device_or_loop(device, &data_device, &loop_fd);
+       r = device_alloc(&dev, device);
        if (r < 0)
                return r;
 
        if (!cd->metadata_device) {
                cd->metadata_device = cd->device;
-               cd->loop_metadata_device_fd = cd->loop_device_fd;
-       } else {
-               free(cd->device);
-               if (cd->loop_device_fd != -1)
-                       close(cd->loop_device_fd);
-       }
+       } else
+               device_free(cd->device);
 
-       cd->device = data_device;
-       cd->loop_device_fd = loop_fd;
+       cd->device = dev;
 
        return crypt_check_data_device_size(cd);
 }
@@ -650,7 +618,7 @@ static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int re
        if (r < 0)
                return r;
 
-       r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
+       r = LUKS_read_phdr(&hdr, require_header, repair, cd);
        if (r < 0)
                return r;
 
@@ -662,6 +630,32 @@ static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int re
        return r;
 }
 
+static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
+{
+       int r;
+
+       r = init_crypto(cd);
+       if (r < 0)
+               return r;
+
+       memcpy(&cd->tcrypt_params, params, sizeof(*params));
+
+       r = TCRYPT_read_phdr(cd, &cd->tcrypt_hdr, &cd->tcrypt_params);
+
+       cd->tcrypt_params.passphrase = NULL;
+       cd->tcrypt_params.passphrase_size = 0;
+       cd->tcrypt_params.keyfiles = NULL;
+       cd->tcrypt_params.keyfiles_count = 0;
+
+       if (r < 0)
+               return r;
+
+       if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
+               return -ENOMEM;
+
+       return r;
+}
+
 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
 {
        int r;
@@ -671,29 +665,31 @@ 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, mdata_device(cd), sb_offset,
-                          &cd->verity_uuid, &cd->verity_hdr);
+       r = VERITY_read_sb(cd, sb_offset, &cd->verity_uuid, &cd->verity_hdr);
        if (r < 0)
                return r;
 
        if (params)
                cd->verity_hdr.flags = params->flags;
 
-       if (params && params->data_device &&
-           (r = crypt_set_data_device(cd, params->data_device)) < 0)
-               return r;
-
+       /* 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)
+               return -EINVAL;
 
        if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
                return -ENOMEM;
 
+       if (params && params->data_device &&
+           (r = crypt_set_data_device(cd, params->data_device)) < 0)
+               return r;
+
        return r;
 }
 
@@ -703,10 +699,11 @@ static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
        char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
        int key_nums, r;
 
-       r = dm_query_device(name, DM_ACTIVE_DEVICE |
-                                  DM_ACTIVE_UUID |
-                                  DM_ACTIVE_CRYPT_CIPHER |
-                                  DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
+       r = dm_query_device(cd, name,
+                       DM_ACTIVE_DEVICE |
+                       DM_ACTIVE_UUID |
+                       DM_ACTIVE_CRYPT_CIPHER |
+                       DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
        if (r < 0)
                goto out;
 
@@ -737,7 +734,7 @@ static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
                        cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
                }
        } else if (isLUKS(cd->type)) {
-               if (mdata_device(cd)) {
+               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.");
@@ -757,11 +754,14 @@ static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
                                goto out;
                        }
                }
+       } else if (isTCRYPT(cd->type)) {
+               r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
+                                       &cd->tcrypt_params, &cd->tcrypt_hdr);
        }
 out:
        crypt_free_volume_key(dmd.u.crypt.vk);
+       device_free(dmd.data_device);
        free(CONST_CAST(void*)dmd.u.crypt.cipher);
-       free(CONST_CAST(void*)dmd.data_device);
        free(CONST_CAST(void*)dmd.uuid);
        return r;
 }
@@ -775,10 +775,11 @@ static int _init_by_name_verity(struct crypt_device *cd, const char *name)
        };
        int r;
 
-       r = dm_query_device(name, DM_ACTIVE_DEVICE |
-                                  DM_ACTIVE_UUID |
-                                  DM_ACTIVE_VERITY_HASH_DEVICE |
-                                  DM_ACTIVE_VERITY_PARAMS, &dmd);
+       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;
 
@@ -798,12 +799,10 @@ static int _init_by_name_verity(struct crypt_device *cd, const char *name)
                cd->verity_hdr.flags = params.flags;
                cd->verity_hdr.salt_size = params.salt_size;
                cd->verity_hdr.salt = params.salt;
-               if (!(cd->metadata_device = strdup(dmd.u.verity.hash_device)))
-                       r = -ENOMEM;
+               cd->metadata_device = dmd.u.verity.hash_device;
        }
 out:
-       free(CONST_CAST(void*)dmd.u.verity.hash_device);
-       free(CONST_CAST(void*)dmd.data_device);
+       device_free(dmd.data_device);
        free(CONST_CAST(void*)dmd.uuid);
        return r;
 }
@@ -827,7 +826,7 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
                return -ENODEV;
        }
 
-       r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
+       r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
        if (r < 0)
                goto out;
 
@@ -836,7 +835,7 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
        if (header_device) {
                r = crypt_init(cd, header_device);
        } else {
-               r = crypt_init(cd, dmd.data_device);
+               r = crypt_init(cd, device_path(dmd.data_device));
 
                /* Underlying device disappeared but mapping still active */
                if (!dmd.data_device || r == -ENOTBLK)
@@ -845,7 +844,7 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
 
                /* Underlying device is not readable but crypt mapping exists */
                if (r == -ENOTBLK) {
-                       free(CONST_CAST(void*)dmd.data_device);
+                       device_free(dmd.data_device);
                        dmd.data_device = NULL;
                        r = crypt_init(cd, NULL);
                }
@@ -863,13 +862,15 @@ 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
                log_dbg("Active device has no UUID set, some parameters are not set.");
 
        if (header_device) {
-               r = crypt_set_data_device(*cd, dmd.data_device);
+               r = crypt_set_data_device(*cd, device_path(dmd.data_device));
                if (r < 0)
                        goto out;
        }
@@ -885,7 +886,7 @@ out:
                crypt_free(*cd);
                *cd = NULL;
        }
-       free(CONST_CAST(void*)dmd.data_device);
+       device_free(dmd.data_device);
        free(CONST_CAST(void*)dmd.uuid);
        return r;
 }
@@ -912,6 +913,9 @@ static int _crypt_format_plain(struct crypt_device *cd,
                return -EINVAL;
        }
 
+       if (!(cd->type = strdup(CRYPT_PLAIN)))
+               return -ENOMEM;
+
        cd->plain_key_size = volume_key_size;
        cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
        if (!cd->volume_key)
@@ -948,11 +952,14 @@ static int _crypt_format_luks1(struct crypt_device *cd,
        unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
        unsigned long alignment_offset = 0;
 
-       if (!mdata_device(cd)) {
+       if (!crypt_metadata_device(cd)) {
                log_err(cd, _("Can't format LUKS without device.\n"));
                return -EINVAL;
        }
 
+       if (!(cd->type = strdup(CRYPT_LUKS1)))
+               return -ENOMEM;
+
        if (volume_key)
                cd->volume_key = crypt_alloc_volume_key(volume_key_size,
                                                      volume_key);
@@ -964,39 +971,50 @@ static int _crypt_format_luks1(struct crypt_device *cd,
 
        if (params && params->data_device) {
                cd->metadata_device = cd->device;
-               if (!(cd->device = strdup(params->data_device)))
+               cd->device = NULL;
+               if (device_alloc(&cd->device, params->data_device) < 0)
                        return -ENOMEM;
                required_alignment = params->data_alignment * SECTOR_SIZE;
        } else if (params && params->data_alignment) {
                required_alignment = params->data_alignment * SECTOR_SIZE;
        } else
-               get_topology_alignment(cd->device, &required_alignment,
+               device_topology_alignment(cd->device,
+                                      &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,
                               (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->metadata_device, cd);
+                              cd->metadata_device ? 1 : 0, cd);
        if(r < 0)
                return r;
 
        /* Wipe first 8 sectors - fs magic numbers etc. */
-       r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
+       r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
        if(r < 0) {
                if (r == -EBUSY)
                        log_err(cd, _("Cannot format device %s which is still in use.\n"),
-                               mdata_device(cd));
-               else
+                               mdata_device_path(cd));
+               else if (r == -EACCES) {
+                       log_err(cd, _("Cannot format device %s, permission denied.\n"),
+                               mdata_device_path(cd));
+                       r = -EINVAL;
+               } else
                        log_err(cd, _("Cannot wipe header on device %s.\n"),
-                               mdata_device(cd));
+                               mdata_device_path(cd));
 
                return r;
        }
 
-       r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
+       r = LUKS_write_phdr(&cd->hdr, cd);
 
        return r;
 }
@@ -1007,7 +1025,7 @@ static int _crypt_format_loopaes(struct crypt_device *cd,
                                 size_t volume_key_size,
                                 struct crypt_params_loopaes *params)
 {
-       if (!mdata_device(cd)) {
+       if (!crypt_metadata_device(cd)) {
                log_err(cd, _("Can't format LOOPAES without device.\n"));
                return -EINVAL;
        }
@@ -1017,6 +1035,9 @@ static int _crypt_format_loopaes(struct crypt_device *cd,
                return -EINVAL;
        }
 
+       if (!(cd->type = strdup(CRYPT_LOOPAES)))
+               return -ENOMEM;
+
        cd->loopaes_key_size = volume_key_size;
 
        cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
@@ -1037,10 +1058,10 @@ static int _crypt_format_verity(struct crypt_device *cd,
                                 const char *uuid,
                                 struct crypt_params_verity *params)
 {
-       int r = 0;
+       int r = 0, hash_size;
        uint64_t data_device_size;
 
-       if (!mdata_device(cd)) {
+       if (!crypt_metadata_device(cd)) {
                log_err(cd, _("Can't format VERITY without device.\n"));
                return -EINVAL;
        }
@@ -1048,14 +1069,30 @@ static int _crypt_format_verity(struct crypt_device *cd,
        if (!params || !params->data_device)
                return -EINVAL;
 
-       /* set data device */
-       cd->type = CRYPT_VERITY;
+       if (params->hash_type > VERITY_MAX_HASH_TYPE) {
+               log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
+               return -EINVAL;
+       }
+
+       if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
+           VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
+               log_err(cd, _("Unsupported VERITY block size.\n"));
+               return -EINVAL;
+       }
+
+       if (params->hash_area_offset % 512) {
+               log_err(cd, _("Unsupported VERITY hash offset.\n"));
+               return -EINVAL;
+       }
+
+       if (!(cd->type = strdup(CRYPT_VERITY)))
+               return -ENOMEM;
+
        r = crypt_set_data_device(cd, params->data_device);
-       cd->type = NULL;
        if (r)
                return r;
        if (!params->data_size) {
-               r = device_size(params->data_device, &data_device_size);
+               r = device_size(cd->device, &data_device_size);
                if (r < 0)
                        return r;
 
@@ -1063,16 +1100,21 @@ static int _crypt_format_verity(struct crypt_device *cd,
        } else
                cd->verity_hdr.data_size = params->data_size;
 
-       cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
-       if (!cd->verity_root_hash_size)
+       hash_size = crypt_hash_size(params->hash_name);
+       if (hash_size <= 0) {
+               log_err(cd, _("Hash algorithm %s not supported.\n"),
+                       params->hash_name);
                return -EINVAL;
+       }
+       cd->verity_root_hash_size = hash_size;
 
        cd->verity_root_hash = malloc(cd->verity_root_hash_size);
        if (!cd->verity_root_hash)
                return -ENOMEM;
 
        cd->verity_hdr.flags = params->flags;
-       cd->verity_hdr.hash_name = strdup(params->hash_name);
+       if (!(cd->verity_hdr.hash_name = strdup(params->hash_name)))
+               return -ENOMEM;
        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;
@@ -1080,7 +1122,9 @@ static int _crypt_format_verity(struct crypt_device *cd,
        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 (!(cd->verity_hdr.salt = malloc(params->salt_size)))
+               return -ENOMEM;
+
        if (params->salt)
                memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
                       params->salt_size);
@@ -1091,7 +1135,7 @@ static int _crypt_format_verity(struct crypt_device *cd,
                return r;
 
        if (params->flags & CRYPT_VERITY_CREATE_HASH) {
-               r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
+               r = VERITY_create(cd, &cd->verity_hdr,
                                  cd->verity_root_hash, cd->verity_root_hash_size);
                if (r)
                        return r;
@@ -1106,8 +1150,7 @@ static int _crypt_format_verity(struct crypt_device *cd,
                                return r;
                }
 
-               r = VERITY_write_sb(cd, mdata_device(cd),
-                                   cd->verity_hdr.hash_area_offset,
+               r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
                                    cd->verity_uuid,
                                    &cd->verity_hdr);
        }
@@ -1133,7 +1176,7 @@ int crypt_format(struct crypt_device *cd,
                return -EINVAL;
        }
 
-       log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
+       log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
 
        r = init_crypto(cd);
        if (r < 0)
@@ -1154,10 +1197,9 @@ int crypt_format(struct crypt_device *cd,
                r = -EINVAL;
        }
 
-       if (!r && !(cd->type = strdup(type)))
-               r = -ENOMEM;
-
        if (r < 0) {
+               free(cd->type);
+               cd->type = NULL;
                crypt_free_volume_key(cd->volume_key);
                cd->volume_key = NULL;
        }
@@ -1172,9 +1214,9 @@ int crypt_load(struct crypt_device *cd,
        int r;
 
        log_dbg("Trying to load %s crypt type from device %s.",
-               requested_type ?: "any", mdata_device(cd) ?: "(none)");
+               requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
 
-       if (!mdata_device(cd))
+       if (!crypt_metadata_device(cd))
                return -EINVAL;
 
        if (!requested_type || isLUKS(requested_type)) {
@@ -1190,19 +1232,15 @@ 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;
 
-       if (r < 0)
-               return r;
-
-       /* 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;
-       }
-
        return r;
 }
 
@@ -1213,9 +1251,9 @@ int crypt_repair(struct crypt_device *cd,
        int r;
 
        log_dbg("Trying to repair %s crypt type from device %s.",
-               requested_type ?: "any", mdata_device(cd) ?: "(none)");
+               requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
 
-       if (!mdata_device(cd))
+       if (!crypt_metadata_device(cd))
                return -EINVAL;
 
        if (requested_type && !isLUKS(requested_type))
@@ -1248,7 +1286,7 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
 
        log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
 
-       r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
+       r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
                                  DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
                                  DM_ACTIVE_CRYPT_KEY, &dmd);
        if (r < 0) {
@@ -1261,8 +1299,8 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
                goto out;
        }
 
-       r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
-                                   &dmd.u.crypt.offset, &dmd.flags);
+       r = device_block_adjust(cd, dmd.data_device, DEV_OK,
+                               dmd.u.crypt.offset, &new_size, &dmd.flags);
        if (r)
                goto out;
 
@@ -1272,7 +1310,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(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) {
@@ -1294,19 +1335,19 @@ int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
 
        if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
                log_dbg("UUID is the same as requested (%s) for device %s.",
-                       uuid, mdata_device(cd));
+                       uuid, mdata_device_path(cd));
                return 0;
        }
 
        if (uuid)
-               log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
+               log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
        else
-               log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
+               log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
 
        if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
                return -EPERM;
 
-       return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
+       return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
 }
 
 int crypt_header_backup(struct crypt_device *cd,
@@ -1323,9 +1364,9 @@ int crypt_header_backup(struct crypt_device *cd,
                return r;
 
        log_dbg("Requested header backup of device %s (%s) to "
-               "file %s.", mdata_device(cd), requested_type, backup_file);
+               "file %s.", mdata_device_path(cd), requested_type, backup_file);
 
-       return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
+       return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
 }
 
 int crypt_header_restore(struct crypt_device *cd,
@@ -1342,26 +1383,21 @@ int crypt_header_restore(struct crypt_device *cd,
                return r;
 
        log_dbg("Requested header restore to device %s (%s) from "
-               "file %s.", mdata_device(cd), requested_type, backup_file);
+               "file %s.", mdata_device_path(cd), requested_type, backup_file);
 
-       return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
+       return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
 }
 
 void crypt_free(struct crypt_device *cd)
 {
        if (cd) {
-               log_dbg("Releasing crypt device %s context.", mdata_device(cd));
+               log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
 
-               if (cd->loop_device_fd != -1)
-                       close(cd->loop_device_fd);
-               if (cd->loop_metadata_device_fd != -1)
-                       close(cd->loop_metadata_device_fd);
-
-               dm_exit();
+               dm_backend_exit();
                crypt_free_volume_key(cd->volume_key);
 
-               free(cd->device);
-               free(cd->metadata_device);
+               device_free(cd->device);
+               device_free(cd->metadata_device);
                free(cd->type);
 
                /* used in plain device only */
@@ -1393,7 +1429,7 @@ int crypt_suspend(struct crypt_device *cd,
 
        log_dbg("Suspending volume %s.", name);
 
-       if (!isLUKS(cd->type)) {
+       if (!cd || !isLUKS(cd->type)) {
                log_err(cd, _("This operation is supported only for LUKS device.\n"));
                r = -EINVAL;
                goto out;
@@ -1405,10 +1441,9 @@ int crypt_suspend(struct crypt_device *cd,
                return -EINVAL;
        }
 
-       if (!cd && dm_init(NULL, 1) < 0)
-               return -ENOSYS;
+       dm_backend_init();
 
-       r = dm_status_suspended(name);
+       r = dm_status_suspended(cd, name);
        if (r < 0)
                goto out;
 
@@ -1418,14 +1453,13 @@ int crypt_suspend(struct crypt_device *cd,
                goto out;
        }
 
-       r = dm_suspend_and_wipe_key(name);
+       r = dm_suspend_and_wipe_key(cd, name);
        if (r == -ENOTSUP)
                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);
 out:
-       if (!cd)
-               dm_exit();
+       dm_backend_exit();
        return r;
 }
 
@@ -1446,7 +1480,7 @@ int crypt_resume_by_passphrase(struct crypt_device *cd,
                goto out;
        }
 
-       r = dm_status_suspended(name);
+       r = dm_status_suspended(cd, name);
        if (r < 0)
                return r;
 
@@ -1456,14 +1490,14 @@ int crypt_resume_by_passphrase(struct crypt_device *cd,
        }
 
        if (passphrase) {
-               r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
-                                          passphrase_size, &cd->hdr, &vk, cd);
+               r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
+                                          &cd->hdr, &vk, cd);
        } else
                r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
 
        if (r >= 0) {
                keyslot = r;
-               r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
+               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);
                else if (r)
@@ -1495,7 +1529,7 @@ int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
                goto out;
        }
 
-       r = dm_status_suspended(name);
+       r = dm_status_suspended(cd, name);
        if (r < 0)
                return r;
 
@@ -1513,13 +1547,13 @@ int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
        if (r < 0)
                goto out;
 
-       r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
+       r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
                                   passphrase_size_read, &cd->hdr, &vk, cd);
        if (r < 0)
                goto out;
 
        keyslot = r;
-       r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
+       r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
        if (r)
                log_err(cd, "Error during resuming device %s.\n", name);
 out:
@@ -1575,7 +1609,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(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
+               r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
                                           passphrase_size, &cd->hdr, &vk, cd);
        } else {
                /* Passphrase not provided, ask first and use it to unlock existing keyslot */
@@ -1584,7 +1618,7 @@ int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
                if (r < 0)
                        goto out;
 
-               r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
+               r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
                                           passwordLen, &cd->hdr, &vk, cd);
                crypt_safe_free(password);
        }
@@ -1602,7 +1636,7 @@ int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
                        goto out;
        }
 
-       r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
+       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;
 
@@ -1661,7 +1695,7 @@ int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
                if (r < 0)
                        goto out;
 
-               r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
+               r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
                                           &cd->hdr, &vk, cd);
        }
 
@@ -1678,7 +1712,7 @@ int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
        if (r < 0)
                goto out;
 
-       r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
+       r = LUKS_set_key(keyslot, new_password, new_passwordLen,
                         &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
 out:
        crypt_safe_free(password);
@@ -1744,7 +1778,7 @@ int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
                passphrase_size = new_passwordLen;
        }
 
-       r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
+       r = LUKS_set_key(keyslot, passphrase, passphrase_size,
                         &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
 out:
        crypt_safe_free(new_password);
@@ -1774,7 +1808,7 @@ int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
                return -EINVAL;
        }
 
-       return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
+       return LUKS_del_key(keyslot, &cd->hdr, cd);
 }
 
 // activation/deactivation of device mapping
@@ -1830,7 +1864,7 @@ int crypt_activate_by_passphrase(struct crypt_device *cd,
        } else if (isLUKS(cd->type)) {
                /* provided passphrase, do not retry */
                if (passphrase) {
-                       r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
+                       r = LUKS_open_key_with_hdr(keyslot, passphrase,
                                                   passphrase_size, &cd->hdr, &vk, cd);
                } else
                        r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
@@ -1902,7 +1936,7 @@ int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
                          &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
                if (r < 0)
                        goto out;
-               r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
+               r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
                                           passphrase_size_read, &cd->hdr, &vk, cd);
                if (r < 0)
                        goto out;
@@ -2012,8 +2046,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
                        return -EINVAL;
                }
 
-               r = VERITY_activate(cd, name, mdata_device(cd),
-                                   volume_key, volume_key_size,
+               r = VERITY_activate(cd, name, volume_key, volume_key_size,
                                    &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
 
                if (r == -EPERM) {
@@ -2026,6 +2059,11 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
                        if (cd->verity_root_hash)
                                memcpy(cd->verity_root_hash, volume_key, volume_key_size);
                }
+       } else if (isTCRYPT(cd->type)) {
+               if (!name)
+                       return 0;
+               r = TCRYPT_activate(cd, name, &cd->tcrypt_hdr,
+                                   &cd->tcrypt_params, flags);
        } else
                log_err(cd, _("Device type is not properly initialised.\n"));
 
@@ -2043,13 +2081,16 @@ int crypt_deactivate(struct crypt_device *cd, const char *name)
 
        log_dbg("Deactivating volume %s.", name);
 
-       if (!cd && dm_init(NULL, 1) < 0)
-               return -ENOSYS;
+       if (!cd)
+               dm_backend_init();
 
        switch (crypt_status(cd, name)) {
                case CRYPT_ACTIVE:
                case CRYPT_BUSY:
-                       r = dm_remove_device(name, 0, 0);
+                       if (isTCRYPT(cd->type))
+                               r = TCRYPT_deactivate(cd, name);
+                       else
+                               r = dm_remove_device(cd, name, 0, 0);
                        break;
                case CRYPT_INACTIVE:
                        log_err(cd, _("Device %s is not active.\n"), name);
@@ -2061,7 +2102,7 @@ int crypt_deactivate(struct crypt_device *cd, const char *name)
        }
 
        if (!cd)
-               dm_exit();
+               dm_backend_exit();
 
        return r;
 }
@@ -2094,9 +2135,10 @@ int crypt_volume_key_get(struct crypt_device *cd,
                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(mdata_device(cd), keyslot, passphrase,
+               r = LUKS_open_key_with_hdr(keyslot, passphrase,
                                        passphrase_size, &cd->hdr, &vk, cd);
-
+       } else if (isTCRYPT(cd->type)) {
+               r = TCRYPT_get_volume_key(cd, &cd->tcrypt_hdr, &cd->tcrypt_params, &vk);
        } else
                log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
 
@@ -2191,13 +2233,13 @@ crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
 {
        int r;
 
-       if (!cd && dm_init(NULL, 1) < 0)
-               return CRYPT_INVALID;
+       if (!cd)
+               dm_backend_init();
 
-       r = dm_status_device(name);
+       r = dm_status_device(cd, name);
 
        if (!cd)
-               dm_exit();
+               dm_backend_exit();
 
        if (r < 0 && r != -ENODEV)
                return CRYPT_INVALID;
@@ -2222,7 +2264,7 @@ static int _luks_dump(struct crypt_device *cd)
 {
        int i;
 
-       log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
+       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);
@@ -2265,7 +2307,7 @@ 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(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);
@@ -2292,6 +2334,8 @@ 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->tcrypt_hdr, &cd->tcrypt_params);
 
        log_err(cd, _("Dump operation is not supported for this device type.\n"));
        return -EINVAL;
@@ -2308,6 +2352,9 @@ const char *crypt_get_cipher(struct crypt_device *cd)
        if (isLOOPAES(cd->type))
                return cd->loopaes_cipher;
 
+       if (isTCRYPT(cd->type))
+               return cd->tcrypt_params.cipher;
+
        return NULL;
 }
 
@@ -2322,6 +2369,9 @@ const char *crypt_get_cipher_mode(struct crypt_device *cd)
        if (isLOOPAES(cd->type))
                return cd->loopaes_cipher_mode;
 
+       if (isTCRYPT(cd->type))
+               return cd->tcrypt_params.mode;
+
        return NULL;
 }
 
@@ -2344,7 +2394,12 @@ const char *crypt_get_uuid(struct crypt_device *cd)
 
 const char *crypt_get_device_name(struct crypt_device *cd)
 {
-       return cd->device;
+       const char *path = device_block_path(cd->device);
+
+       if (!path)
+               path = device_path(cd->device);
+
+       return path;
 }
 
 int crypt_get_volume_key_size(struct crypt_device *cd)
@@ -2361,6 +2416,9 @@ int crypt_get_volume_key_size(struct crypt_device *cd)
        if (isVERITY(cd->type))
                return cd->verity_root_hash_size;
 
+       if (isTCRYPT(cd->type))
+               return cd->tcrypt_params.key_size;
+
        return 0;
 }
 
@@ -2375,6 +2433,9 @@ uint64_t crypt_get_data_offset(struct crypt_device *cd)
        if (isLOOPAES(cd->type))
                return cd->loopaes_hdr.offset;
 
+       if (isTCRYPT(cd->type))
+               return TCRYPT_get_data_offset(&cd->tcrypt_hdr);
+
        return 0;
 }
 
@@ -2389,6 +2450,9 @@ uint64_t crypt_get_iv_offset(struct crypt_device *cd)
        if (isLOOPAES(cd->type))
                return cd->loopaes_hdr.skip;
 
+       if (isTCRYPT(cd->type))
+               return TCRYPT_get_iv_offset(&cd->tcrypt_hdr);
+
        return 0;
 }
 
@@ -2410,6 +2474,17 @@ int crypt_keyslot_max(const char *type)
        return -EINVAL;
 }
 
+int crypt_keyslot_area(struct crypt_device *cd,
+       int keyslot,
+       uint64_t *offset,
+       uint64_t *length)
+{
+       if (!isLUKS(cd->type))
+               return -EINVAL;
+
+       return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
+}
+
 const char *crypt_get_type(struct crypt_device *cd)
 {
        return cd->type;
@@ -2421,8 +2496,8 @@ int crypt_get_verity_info(struct crypt_device *cd,
        if (!isVERITY(cd->type) || !vp)
                return -EINVAL;
 
-       vp->data_device = cd->device;
-       vp->hash_device = mdata_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;
@@ -2435,22 +2510,26 @@ int crypt_get_verity_info(struct crypt_device *cd,
        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;
        int r;
 
-       r = dm_query_device(name, 0, &dmd);
+       r = dm_query_device(cd, name, 0, &dmd);
        if (r < 0)
                return r;
 
        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->tcrypt_hdr);
+               cad->iv_offset  = TCRYPT_get_iv_offset(&cd->tcrypt_hdr);
+       } else {
+               cad->offset     = dmd.u.crypt.offset;
+               cad->iv_offset  = dmd.u.crypt.iv_offset;
+       }
        cad->size       = dmd.size;
        cad->flags      = dmd.flags;