Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / luks1 / keymanage.c
index 8ae2af8..a08ff50 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * LUKS - Linux Unified Key Setup
  *
- * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
- * Copyright (C) 2013-2014, Milan Broz
+ * Copyright (C) 2004-2006 Clemens Fruhwirth <clemens@endorphin.org>
+ * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2013-2020 Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -23,7 +23,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <netinet/in.h>
-#include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdio.h>
 #include "af.h"
 #include "internal.h"
 
-/* Get size of struct luks_phdr with all keyslots material space */
-static size_t LUKS_device_sectors(size_t keyLen)
-{
-       size_t keyslot_sectors, sector;
-       int i;
-
-       keyslot_sectors = AF_split_sectors(keyLen, LUKS_STRIPES);
-       sector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
-
-       for (i = 0; i < LUKS_NUMKEYS; i++) {
-               sector = size_round_up(sector, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
-               sector += keyslot_sectors;
-       }
-
-       return sector;
-}
-
-int LUKS_keyslot_area(struct luks_phdr *hdr,
+int LUKS_keyslot_area(const struct luks_phdr *hdr,
        int keyslot,
        uint64_t *offset,
        uint64_t *length)
@@ -62,32 +44,70 @@ int LUKS_keyslot_area(struct luks_phdr *hdr,
        if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
                return -EINVAL;
 
-       *offset = hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZE;
+       *offset = (uint64_t)hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZE;
        *length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE;
 
        return 0;
 }
 
-static int LUKS_check_device_size(struct crypt_device *ctx, size_t keyLength)
+/* insertsort: because the array has 8 elements and it's mostly sorted. that's why */
+static void LUKS_sort_keyslots(const struct luks_phdr *hdr, int *array)
+{
+       int i, j, x;
+
+       for (i = 1; i < LUKS_NUMKEYS; i++) {
+               j = i;
+               while (j > 0 && hdr->keyblock[array[j-1]].keyMaterialOffset > hdr->keyblock[array[j]].keyMaterialOffset) {
+                       x = array[j];
+                       array[j] = array[j-1];
+                       array[j-1] = x;
+                       j--;
+               }
+       }
+}
+
+size_t LUKS_device_sectors(const struct luks_phdr *hdr)
+{
+       int sorted_areas[LUKS_NUMKEYS] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+
+       LUKS_sort_keyslots(hdr, sorted_areas);
+
+       return hdr->keyblock[sorted_areas[LUKS_NUMKEYS-1]].keyMaterialOffset + AF_split_sectors(hdr->keyBytes, LUKS_STRIPES);
+}
+
+size_t LUKS_keyslots_offset(const struct luks_phdr *hdr)
+{
+       int sorted_areas[LUKS_NUMKEYS] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+
+       LUKS_sort_keyslots(hdr, sorted_areas);
+
+       return hdr->keyblock[sorted_areas[0]].keyMaterialOffset;
+}
+
+static int LUKS_check_device_size(struct crypt_device *ctx, const struct luks_phdr *hdr, int falloc)
 {
        struct device *device = crypt_metadata_device(ctx);
        uint64_t dev_sectors, hdr_sectors;
 
-       if (!keyLength)
+       if (!hdr->keyBytes)
                return -EINVAL;
 
-       if(device_size(device, &dev_sectors)) {
-               log_dbg("Cannot get device size for device %s.", device_path(device));
+       if (device_size(device, &dev_sectors)) {
+               log_dbg(ctx, "Cannot get device size for device %s.", device_path(device));
                return -EIO;
        }
 
        dev_sectors >>= SECTOR_SHIFT;
-       hdr_sectors = LUKS_device_sectors(keyLength);
-       log_dbg("Key length %zu, device size %" PRIu64 " sectors, header size %"
-               PRIu64 " sectors.",keyLength, dev_sectors, hdr_sectors);
+       hdr_sectors = LUKS_device_sectors(hdr);
+       log_dbg(ctx, "Key length %u, device size %" PRIu64 " sectors, header size %"
+               PRIu64 " sectors.", hdr->keyBytes, dev_sectors, hdr_sectors);
 
        if (hdr_sectors > dev_sectors) {
-               log_err(ctx, _("Device %s is too small. (LUKS requires at least %" PRIu64 " bytes.)\n"),
+               /* If it is header file, increase its size */
+               if (falloc && !device_fallocate(device, hdr_sectors << SECTOR_SHIFT))
+                       return 0;
+
+               log_err(ctx, _("Device %s is too small. (LUKS1 requires at least %" PRIu64 " bytes.)"),
                        device_path(device), hdr_sectors * SECTOR_SIZE);
                return -EINVAL;
        }
@@ -95,40 +115,67 @@ static int LUKS_check_device_size(struct crypt_device *ctx, size_t keyLength)
        return 0;
 }
 
-/* Check keyslot to prevent access outside of header and keyslot area */
-static int LUKS_check_keyslot_size(const struct luks_phdr *phdr, unsigned int keyIndex)
+static int LUKS_check_keyslots(struct crypt_device *ctx, const struct luks_phdr *phdr)
 {
-       uint32_t secs_per_stripes;
+       int i, prev, next, sorted_areas[LUKS_NUMKEYS] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+       uint32_t secs_per_stripes = AF_split_sectors(phdr->keyBytes, LUKS_STRIPES);
 
-       /* First sectors is the header itself */
-       if (phdr->keyblock[keyIndex].keyMaterialOffset * SECTOR_SIZE < sizeof(*phdr)) {
-               log_dbg("Invalid offset %u in keyslot %u.",
-                       phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex);
-               return 1;
-       }
+       LUKS_sort_keyslots(phdr, sorted_areas);
+
+       /* Check keyslot to prevent access outside of header and keyslot area */
+       for (i = 0; i < LUKS_NUMKEYS; i++) {
+               /* enforce stripes == 4000 */
+               if (phdr->keyblock[i].stripes != LUKS_STRIPES) {
+                       log_dbg(ctx, "Invalid stripes count %u in keyslot %u.",
+                               phdr->keyblock[i].stripes, i);
+                       log_err(ctx, _("LUKS keyslot %u is invalid."), i);
+                       return -1;
+               }
 
-       /* Ignore following check for detached header where offset can be zero. */
-       if (phdr->payloadOffset == 0)
-               return 0;
+               /* First sectors is the header itself */
+               if (phdr->keyblock[i].keyMaterialOffset * SECTOR_SIZE < sizeof(*phdr)) {
+                       log_dbg(ctx, "Invalid offset %u in keyslot %u.",
+                               phdr->keyblock[i].keyMaterialOffset, i);
+                       log_err(ctx, _("LUKS keyslot %u is invalid."), i);
+                       return -1;
+               }
 
-       if (phdr->payloadOffset <= phdr->keyblock[keyIndex].keyMaterialOffset) {
-               log_dbg("Invalid offset %u in keyslot %u (beyond data area offset %u).",
-                       phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex,
-                       phdr->payloadOffset);
-               return 1;
-       }
+               /* Ignore following check for detached header where offset can be zero. */
+               if (phdr->payloadOffset == 0)
+                       continue;
 
-       secs_per_stripes = AF_split_sectors(phdr->keyBytes, phdr->keyblock[keyIndex].stripes);
+               if (phdr->payloadOffset <= phdr->keyblock[i].keyMaterialOffset) {
+                       log_dbg(ctx, "Invalid offset %u in keyslot %u (beyond data area offset %u).",
+                               phdr->keyblock[i].keyMaterialOffset, i,
+                               phdr->payloadOffset);
+                       log_err(ctx, _("LUKS keyslot %u is invalid."), i);
+                       return -1;
+               }
+
+               if (phdr->payloadOffset < (phdr->keyblock[i].keyMaterialOffset + secs_per_stripes)) {
+                       log_dbg(ctx, "Invalid keyslot size %u (offset %u, stripes %u) in "
+                               "keyslot %u (beyond data area offset %u).",
+                               secs_per_stripes,
+                               phdr->keyblock[i].keyMaterialOffset,
+                               phdr->keyblock[i].stripes,
+                               i, phdr->payloadOffset);
+                       log_err(ctx, _("LUKS keyslot %u is invalid."), i);
+                       return -1;
+               }
+       }
 
-       if (phdr->payloadOffset < (phdr->keyblock[keyIndex].keyMaterialOffset + secs_per_stripes)) {
-               log_dbg("Invalid keyslot size %u (offset %u, stripes %u) in "
-                       "keyslot %u (beyond data area offset %u).",
-                       secs_per_stripes,
-                       phdr->keyblock[keyIndex].keyMaterialOffset,
-                       phdr->keyblock[keyIndex].stripes,
-                       keyIndex, phdr->payloadOffset);
-               return 1;
+       /* check no keyslot overlaps with each other */
+       for (i = 1; i < LUKS_NUMKEYS; i++) {
+               prev = sorted_areas[i-1];
+               next = sorted_areas[i];
+               if (phdr->keyblock[next].keyMaterialOffset <
+                   (phdr->keyblock[prev].keyMaterialOffset + secs_per_stripes)) {
+                       log_dbg(ctx, "Not enough space in LUKS keyslot %d.", prev);
+                       log_err(ctx, _("LUKS keyslot %u is invalid."), prev);
+                       return -1;
+               }
        }
+       /* do not check last keyslot on purpose, it must be tested in device size check */
 
        return 0;
 }
@@ -152,16 +199,17 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
 {
        struct device *device = crypt_metadata_device(ctx);
        struct luks_phdr hdr;
-       int r = 0, devfd = -1;
-       ssize_t hdr_size;
-       ssize_t buffer_size;
+       int fd, devfd, r = 0;
+       size_t hdr_size;
+       size_t buffer_size;
+       ssize_t ret;
        char *buffer = NULL;
 
        r = LUKS_read_phdr(&hdr, 1, 0, ctx);
        if (r)
                return r;
 
-       hdr_size = LUKS_device_sectors(hdr.keyBytes) << SECTOR_SHIFT;
+       hdr_size = LUKS_device_sectors(&hdr) << SECTOR_SHIFT;
        buffer_size = size_round_up(hdr_size, crypt_getpagesize());
 
        buffer = crypt_safe_alloc(buffer_size);
@@ -170,49 +218,48 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
                goto out;
        }
 
-       log_dbg("Storing backup of header (%zu bytes) and keyslot area (%zu bytes).",
+       log_dbg(ctx, "Storing backup of header (%zu bytes) and keyslot area (%zu bytes).",
                sizeof(hdr), hdr_size - LUKS_ALIGN_KEYSLOTS);
 
-       log_dbg("Output backup file size: %zu bytes.", buffer_size);
+       log_dbg(ctx, "Output backup file size: %zu bytes.", buffer_size);
 
-       devfd = device_open(device, O_RDONLY);
-       if(devfd == -1) {
-               log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device_path(device));
+       devfd = device_open(ctx, device, O_RDONLY);
+       if (devfd < 0) {
+               log_err(ctx, _("Device %s is not a valid LUKS device."), device_path(device));
                r = -EINVAL;
                goto out;
        }
 
-       if (read_blockwise(devfd, device_block_size(device), buffer, hdr_size) < hdr_size) {
+       if (read_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
+                          buffer, hdr_size, 0) < (ssize_t)hdr_size) {
                r = -EIO;
                goto out;
        }
-       close(devfd);
 
        /* Wipe unused area, so backup cannot contain old signatures */
        if (hdr.keyblock[0].keyMaterialOffset * SECTOR_SIZE == LUKS_ALIGN_KEYSLOTS)
                memset(buffer + sizeof(hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(hdr));
 
-       devfd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
-       if (devfd == -1) {
+       fd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
+       if (fd == -1) {
                if (errno == EEXIST)
-                       log_err(ctx, _("Requested header backup file %s already exists.\n"), backup_file);
+                       log_err(ctx, _("Requested header backup file %s already exists."), backup_file);
                else
-                       log_err(ctx, _("Cannot create header backup file %s.\n"), backup_file);
+                       log_err(ctx, _("Cannot create header backup file %s."), backup_file);
                r = -EINVAL;
                goto out;
        }
-       if (write(devfd, buffer, buffer_size) < buffer_size) {
-               log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
+       ret = write_buffer(fd, buffer, buffer_size);
+       close(fd);
+       if (ret < (ssize_t)buffer_size) {
+               log_err(ctx, _("Cannot write header backup file %s."), backup_file);
                r = -EIO;
                goto out;
        }
-       close(devfd);
 
        r = 0;
 out:
-       if (devfd != -1)
-               close(devfd);
-       crypt_memzero(&hdr, sizeof(hdr));
+       crypt_safe_memzero(&hdr, sizeof(hdr));
        crypt_safe_free(buffer);
        return r;
 }
@@ -223,8 +270,8 @@ int LUKS_hdr_restore(
        struct crypt_device *ctx)
 {
        struct device *device = crypt_metadata_device(ctx);
-       int r = 0, devfd = -1, diff_uuid = 0;
-       ssize_t buffer_size = 0;
+       int fd, r = 0, devfd = -1, diff_uuid = 0;
+       ssize_t ret, buffer_size = 0;
        char *buffer = NULL, msg[200];
        struct luks_phdr hdr_file;
 
@@ -233,10 +280,10 @@ int LUKS_hdr_restore(
                return r;
 
        if (!r)
-               buffer_size = LUKS_device_sectors(hdr_file.keyBytes) << SECTOR_SHIFT;
+               buffer_size = LUKS_device_sectors(&hdr_file) << SECTOR_SHIFT;
 
        if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
-               log_err(ctx, _("Backup file doesn't contain valid LUKS header.\n"));
+               log_err(ctx, _("Backup file does not contain valid LUKS header."));
                r = -EINVAL;
                goto out;
        }
@@ -247,26 +294,27 @@ int LUKS_hdr_restore(
                goto out;
        }
 
-       devfd = open(backup_file, O_RDONLY);
-       if (devfd == -1) {
-               log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
+       fd = open(backup_file, O_RDONLY);
+       if (fd == -1) {
+               log_err(ctx, _("Cannot open header backup file %s."), backup_file);
                r = -EINVAL;
                goto out;
        }
 
-       if (read(devfd, buffer, buffer_size) < buffer_size) {
-               log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
+       ret = read_buffer(fd, buffer, buffer_size);
+       close(fd);
+       if (ret < buffer_size) {
+               log_err(ctx, _("Cannot read header backup file %s."), backup_file);
                r = -EIO;
                goto out;
        }
-       close(devfd);
 
        r = LUKS_read_phdr(hdr, 0, 0, ctx);
        if (r == 0) {
-               log_dbg("Device %s already contains LUKS header, checking UUID and offset.", device_path(device));
+               log_dbg(ctx, "Device %s already contains LUKS header, checking UUID and offset.", device_path(device));
                if(hdr->payloadOffset != hdr_file.payloadOffset ||
                   hdr->keyBytes != hdr_file.keyBytes) {
-                       log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.\n"));
+                       log_err(ctx, _("Data offset or key size differs on device and backup, restore failed."));
                        r = -EINVAL;
                        goto out;
                }
@@ -287,31 +335,30 @@ int LUKS_hdr_restore(
                goto out;
        }
 
-       log_dbg("Storing backup of header (%zu bytes) and keyslot area (%zu bytes) to device %s.",
+       log_dbg(ctx, "Storing backup of header (%zu bytes) and keyslot area (%zu bytes) to device %s.",
                sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(device));
 
-       devfd = device_open(device, O_RDWR);
-       if (devfd == -1) {
+       devfd = device_open(ctx, device, O_RDWR);
+       if (devfd < 0) {
                if (errno == EACCES)
-                       log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
+                       log_err(ctx, _("Cannot write to device %s, permission denied."),
                                device_path(device));
                else
-                       log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
+                       log_err(ctx, _("Cannot open device %s."), device_path(device));
                r = -EINVAL;
                goto out;
        }
 
-       if (write_blockwise(devfd, device_block_size(device), buffer, buffer_size) < buffer_size) {
+       if (write_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
+                           buffer, buffer_size, 0) < buffer_size) {
                r = -EIO;
                goto out;
        }
-       close(devfd);
 
        /* Be sure to reload new data */
        r = LUKS_read_phdr(hdr, 1, 0, ctx);
 out:
-       if (devfd != -1)
-               close(devfd);
+       device_sync(ctx, device);
        crypt_safe_free(buffer);
        return r;
 }
@@ -322,45 +369,44 @@ static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
        struct luks_phdr temp_phdr;
        const unsigned char *sector = (const unsigned char*)phdr;
        struct volume_key *vk;
-       uint64_t PBKDF2_per_sec = 1;
        int i, bad, r, need_write = 0;
 
        if (phdr->keyBytes != 16 && phdr->keyBytes != 32 && phdr->keyBytes != 64) {
-               log_err(ctx, _("Non standard key size, manual repair required.\n"));
+               log_err(ctx, _("Non standard key size, manual repair required."));
                return -EINVAL;
        }
        /* cryptsetup 1.0 did not align to 4k, cannot repair this one */
-       if (phdr->keyblock[0].keyMaterialOffset < (LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE)) {
-               log_err(ctx, _("Non standard keyslots alignment, manual repair required.\n"));
+       if (LUKS_keyslots_offset(phdr) < (LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE)) {
+               log_err(ctx, _("Non standard keyslots alignment, manual repair required."));
                return -EINVAL;
        }
 
+       r = LUKS_check_cipher(ctx, phdr->keyBytes, phdr->cipherName, phdr->cipherMode);
+       if (r < 0)
+               return -EINVAL;
+
        vk = crypt_alloc_volume_key(phdr->keyBytes, NULL);
 
-       log_verbose(ctx, _("Repairing keyslots.\n"));
+       log_verbose(ctx, _("Repairing keyslots."));
 
-       log_dbg("Generating second header with the same parameters for check.");
+       log_dbg(ctx, "Generating second header with the same parameters for check.");
        /* cipherName, cipherMode, hashSpec, uuid are already null terminated */
        /* payloadOffset - cannot check */
        r = LUKS_generate_phdr(&temp_phdr, vk, phdr->cipherName, phdr->cipherMode,
-                              phdr->hashSpec,phdr->uuid, LUKS_STRIPES,
-                              phdr->payloadOffset, 0,
-                              1, &PBKDF2_per_sec,
-                              1, ctx);
-       if (r < 0) {
-               log_err(ctx, _("Repair failed."));
+                              phdr->hashSpec, phdr->uuid,
+                              phdr->payloadOffset * SECTOR_SIZE, 0, 0, ctx);
+       if (r < 0)
                goto out;
-       }
 
        for(i = 0; i < LUKS_NUMKEYS; ++i) {
                if (phdr->keyblock[i].active == LUKS_KEY_ENABLED)  {
-                       log_dbg("Skipping repair for active keyslot %i.", i);
+                       log_dbg(ctx, "Skipping repair for active keyslot %i.", i);
                        continue;
                }
 
                bad = 0;
                if (phdr->keyblock[i].keyMaterialOffset != temp_phdr.keyblock[i].keyMaterialOffset) {
-                       log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u).\n"), i,
+                       log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u)."), i,
                                (unsigned)phdr->keyblock[i].keyMaterialOffset,
                                (unsigned)temp_phdr.keyblock[i].keyMaterialOffset);
                        phdr->keyblock[i].keyMaterialOffset = temp_phdr.keyblock[i].keyMaterialOffset;
@@ -368,7 +414,7 @@ static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
                }
 
                if (phdr->keyblock[i].stripes != temp_phdr.keyblock[i].stripes) {
-                       log_err(ctx, _("Keyslot %i: stripes repaired (%u -> %u).\n"), i,
+                       log_err(ctx, _("Keyslot %i: stripes repaired (%u -> %u)."), i,
                                (unsigned)phdr->keyblock[i].stripes,
                                (unsigned)temp_phdr.keyblock[i].stripes);
                        phdr->keyblock[i].stripes = temp_phdr.keyblock[i].stripes;
@@ -377,12 +423,12 @@ static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
 
                /* Known case - MSDOS partition table signature */
                if (i == 6 && sector[0x1fe] == 0x55 && sector[0x1ff] == 0xaa) {
-                       log_err(ctx, _("Keyslot %i: bogus partition signature.\n"), i);
+                       log_err(ctx, _("Keyslot %i: bogus partition signature."), i);
                        bad = 1;
                }
 
                if(bad) {
-                       log_err(ctx, _("Keyslot %i: salt wiped.\n"), i);
+                       log_err(ctx, _("Keyslot %i: salt wiped."), i);
                        phdr->keyblock[i].active = LUKS_KEY_DISABLED;
                        memset(&phdr->keyblock[i].passwordSalt, 0x00, LUKS_SALTSIZE);
                        phdr->keyblock[i].passwordIterations = 0;
@@ -392,13 +438,21 @@ static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
                        need_write = 1;
        }
 
-       if (need_write) {
-               log_verbose(ctx, _("Writing LUKS header to disk.\n"));
+       /*
+        * check repair result before writing because repair can't fix out of order
+        * keyslot offsets and would corrupt header again
+        */
+       if (LUKS_check_keyslots(ctx, phdr))
+               r = -EINVAL;
+       else if (need_write) {
+               log_verbose(ctx, _("Writing LUKS header to disk."));
                r = LUKS_write_phdr(phdr, ctx);
        }
 out:
+       if (r)
+               log_err(ctx, _("Repair failed."));
        crypt_free_volume_key(vk);
-       crypt_memzero(&temp_phdr, sizeof(temp_phdr));
+       crypt_safe_memzero(&temp_phdr, sizeof(temp_phdr));
        return r;
 }
 
@@ -413,18 +467,18 @@ static int _check_and_convert_hdr(const char *device,
        char luksMagic[] = LUKS_MAGIC;
 
        if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */
-               log_dbg("LUKS header not detected.");
+               log_dbg(ctx, "LUKS header not detected.");
                if (require_luks_device)
-                       log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
+                       log_err(ctx, _("Device %s is not a valid LUKS device."), device);
                return -EINVAL;
        } else if((hdr->version = ntohs(hdr->version)) != 1) {  /* Convert every uint16/32_t item from network byte order */
-               log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
+               log_err(ctx, _("Unsupported LUKS version %d."), hdr->version);
                return -EINVAL;
        }
 
        hdr->hashSpec[LUKS_HASHSPEC_L - 1] = '\0';
        if (crypt_hmac_size(hdr->hashSpec) < LUKS_DIGESTSIZE) {
-               log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hdr->hashSpec);
+               log_err(ctx, _("Requested LUKS hash %s is not supported."), hdr->hashSpec);
                return -EINVAL;
        }
 
@@ -438,12 +492,11 @@ static int _check_and_convert_hdr(const char *device,
                hdr->keyblock[i].passwordIterations = ntohl(hdr->keyblock[i].passwordIterations);
                hdr->keyblock[i].keyMaterialOffset  = ntohl(hdr->keyblock[i].keyMaterialOffset);
                hdr->keyblock[i].stripes            = ntohl(hdr->keyblock[i].stripes);
-               if (LUKS_check_keyslot_size(hdr, i)) {
-                       log_err(ctx, _("LUKS keyslot %u is invalid.\n"), i);
-                       r = -EINVAL;
-               }
        }
 
+       if (LUKS_check_keyslots(ctx, hdr))
+               r = -EINVAL;
+
        /* Avoid unterminated strings */
        hdr->cipherName[LUKS_CIPHERNAME_L - 1] = '\0';
        hdr->cipherMode[LUKS_CIPHERMODE_L - 1] = '\0';
@@ -453,7 +506,7 @@ static int _check_and_convert_hdr(const char *device,
                if (r == -EINVAL)
                        r = _keyslot_repair(hdr, ctx);
                else
-                       log_verbose(ctx, _("No known problems detected for LUKS header.\n"));
+                       log_verbose(ctx, _("No known problems detected for LUKS header."));
        }
 
        return r;
@@ -468,7 +521,7 @@ static void _to_lower(char *str, unsigned max_len)
 
 static void LUKS_fix_header_compatible(struct luks_phdr *header)
 {
-       /* Old cryptsetup expects "sha1", gcrypt allows case insensistive names,
+       /* Old cryptsetup expects "sha1", gcrypt allows case insensitive names,
         * so always convert hash to lower case in header */
        _to_lower(header->hashSpec, LUKS_HASHSPEC_L);
 
@@ -488,16 +541,16 @@ int LUKS_read_phdr_backup(const char *backup_file,
        ssize_t hdr_size = sizeof(struct luks_phdr);
        int devfd = 0, r = 0;
 
-       log_dbg("Reading LUKS header of size %d from backup file %s",
+       log_dbg(ctx, "Reading LUKS header of size %d from backup file %s",
                (int)hdr_size, backup_file);
 
        devfd = open(backup_file, O_RDONLY);
-       if(-1 == devfd) {
-               log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
+       if (devfd == -1) {
+               log_err(ctx, _("Cannot open header backup file %s."), backup_file);
                return -ENOENT;
        }
 
-       if (read(devfd, hdr, hdr_size) < hdr_size)
+       if (read_buffer(devfd, hdr, hdr_size) < hdr_size)
                r = -EIO;
        else {
                LUKS_fix_header_compatible(hdr);
@@ -514,9 +567,9 @@ int LUKS_read_phdr(struct luks_phdr *hdr,
                   int repair,
                   struct crypt_device *ctx)
 {
+       int devfd, r = 0;
        struct device *device = crypt_metadata_device(ctx);
        ssize_t hdr_size = sizeof(struct luks_phdr);
-       int devfd = 0, r = 0;
 
        /* LUKS header starts at offset 0, first keyslot on LUKS_ALIGN_KEYSLOTS */
        assert(sizeof(struct luks_phdr) <= LUKS_ALIGN_KEYSLOTS);
@@ -527,25 +580,35 @@ int LUKS_read_phdr(struct luks_phdr *hdr,
        if (repair && !require_luks_device)
                return -EINVAL;
 
-       log_dbg("Reading LUKS header of size %zu from device %s",
+       log_dbg(ctx, "Reading LUKS header of size %zu from device %s",
                hdr_size, device_path(device));
 
-       devfd = device_open(device, O_RDONLY);
-       if (devfd == -1) {
-               log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
+       devfd = device_open(ctx, device, O_RDONLY);
+       if (devfd < 0) {
+               log_err(ctx, _("Cannot open device %s."), device_path(device));
                return -EINVAL;
        }
 
-       if (read_blockwise(devfd, device_block_size(device), hdr, hdr_size) < hdr_size)
+       if (read_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
+                          hdr, hdr_size, 0) < hdr_size)
                r = -EIO;
        else
                r = _check_and_convert_hdr(device_path(device), hdr, require_luks_device,
                                           repair, ctx);
 
        if (!r)
-               r = LUKS_check_device_size(ctx, hdr->keyBytes);
+               r = LUKS_check_device_size(ctx, hdr, 0);
+
+       /*
+        * Cryptsetup 1.0.0 did not align keyslots to 4k (very rare version).
+        * Disable direct-io to avoid possible IO errors if underlying device
+        * has bigger sector size.
+        */
+       if (!r && hdr->keyblock[0].keyMaterialOffset * SECTOR_SIZE < LUKS_ALIGN_KEYSLOTS) {
+               log_dbg(ctx, "Old unaligned LUKS keyslot detected, disabling direct-io.");
+               device_disable_direct_io(device);
+       }
 
-       close(devfd);
        return r;
 }
 
@@ -559,20 +622,20 @@ int LUKS_write_phdr(struct luks_phdr *hdr,
        struct luks_phdr convHdr;
        int r;
 
-       log_dbg("Updating LUKS header of size %zu on device %s",
+       log_dbg(ctx, "Updating LUKS header of size %zu on device %s",
                sizeof(struct luks_phdr), device_path(device));
 
-       r = LUKS_check_device_size(ctx, hdr->keyBytes);
+       r = LUKS_check_device_size(ctx, hdr, 1);
        if (r)
                return r;
 
-       devfd = device_open(device, O_RDWR);
-       if(-1 == devfd) {
+       devfd = device_open(ctx, device, O_RDWR);
+       if (devfd < 0) {
                if (errno == EACCES)
-                       log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
+                       log_err(ctx, _("Cannot write to device %s, permission denied."),
                                device_path(device));
                else
-                       log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
+                       log_err(ctx, _("Cannot open device %s."), device_path(device));
                return -EINVAL;
        }
 
@@ -591,16 +654,18 @@ int LUKS_write_phdr(struct luks_phdr *hdr,
                convHdr.keyblock[i].stripes            = htonl(hdr->keyblock[i].stripes);
        }
 
-       r = write_blockwise(devfd, device_block_size(device), &convHdr, hdr_size) < hdr_size ? -EIO : 0;
+       r = write_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
+                           &convHdr, hdr_size, 0) < hdr_size ? -EIO : 0;
        if (r)
-               log_err(ctx, _("Error during update of LUKS header on device %s.\n"), device_path(device));
-       close(devfd);
+               log_err(ctx, _("Error during update of LUKS header on device %s."), device_path(device));
+
+       device_sync(ctx, device);
 
        /* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
        if (!r) {
                r = LUKS_read_phdr(hdr, 1, 0, ctx);
                if (r)
-                       log_err(ctx, _("Error re-reading LUKS header after update on device %s.\n"),
+                       log_err(ctx, _("Error re-reading LUKS header after update on device %s."),
                                device_path(device));
        }
 
@@ -608,139 +673,142 @@ int LUKS_write_phdr(struct luks_phdr *hdr,
 }
 
 /* Check that kernel supports requested cipher by decryption of one sector */
-static int LUKS_check_cipher(struct luks_phdr *hdr, struct crypt_device *ctx)
+int LUKS_check_cipher(struct crypt_device *ctx, size_t keylength, const char *cipher, const char *cipher_mode)
 {
        int r;
        struct volume_key *empty_key;
        char buf[SECTOR_SIZE];
 
-       log_dbg("Checking if cipher %s-%s is usable.", hdr->cipherName, hdr->cipherMode);
+       log_dbg(ctx, "Checking if cipher %s-%s is usable.", cipher, cipher_mode);
 
-       empty_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
+       empty_key = crypt_alloc_volume_key(keylength, NULL);
        if (!empty_key)
                return -ENOMEM;
 
-       r = LUKS_decrypt_from_storage(buf, sizeof(buf),
-                                     hdr->cipherName, hdr->cipherMode,
-                                     empty_key, 0, ctx);
+       /* No need to get KEY quality random but it must avoid known weak keys. */
+       r = crypt_random_get(ctx, empty_key->key, empty_key->keylength, CRYPT_RND_NORMAL);
+       if (!r)
+               r = LUKS_decrypt_from_storage(buf, sizeof(buf), cipher, cipher_mode, empty_key, 0, ctx);
 
        crypt_free_volume_key(empty_key);
-       crypt_memzero(buf, sizeof(buf));
+       crypt_safe_memzero(buf, sizeof(buf));
        return r;
 }
 
 int LUKS_generate_phdr(struct luks_phdr *header,
-                      const struct volume_key *vk,
-                      const char *cipherName, const char *cipherMode, const char *hashSpec,
-                      const char *uuid, unsigned int stripes,
-                      unsigned int alignPayload,
-                      unsigned int alignOffset,
-                      uint32_t iteration_time_ms,
-                      uint64_t *PBKDF2_per_sec,
-                      int detached_metadata_device,
-                      struct crypt_device *ctx)
+       const struct volume_key *vk,
+       const char *cipherName,
+       const char *cipherMode,
+       const char *hashSpec,
+       const char *uuid,
+       uint64_t data_offset,        /* in bytes */
+       uint64_t align_offset,       /* in bytes */
+       uint64_t required_alignment, /* in bytes */
+       struct crypt_device *ctx)
 {
-       unsigned int i = 0, hdr_sectors = LUKS_device_sectors(vk->keylength);
-       size_t blocksPerStripeSet, currentSector;
-       int r;
+       int i, r;
+       size_t keyslot_sectors, header_sectors;
        uuid_t partitionUuid;
+       struct crypt_pbkdf_type *pbkdf;
+       double PBKDF2_temp;
        char luksMagic[] = LUKS_MAGIC;
 
-       /* For separate metadata device allow zero alignment */
-       if (alignPayload == 0 && !detached_metadata_device)
-               alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
+       if (data_offset % SECTOR_SIZE || align_offset % SECTOR_SIZE ||
+           required_alignment % SECTOR_SIZE)
+               return -EINVAL;
 
-       if (alignPayload && detached_metadata_device && alignPayload < hdr_sectors) {
-               log_err(ctx, _("Data offset for detached LUKS header must be "
-                              "either 0 or higher than header size (%d sectors).\n"),
-                              hdr_sectors);
+       memset(header, 0, sizeof(struct luks_phdr));
+
+       keyslot_sectors = AF_split_sectors(vk->keylength, LUKS_STRIPES);
+       header_sectors = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
+
+       for (i = 0; i < LUKS_NUMKEYS; i++) {
+               header->keyblock[i].active = LUKS_KEY_DISABLED;
+               header->keyblock[i].keyMaterialOffset = header_sectors;
+               header->keyblock[i].stripes = LUKS_STRIPES;
+               header_sectors = size_round_up(header_sectors + keyslot_sectors,
+                                              LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
+       }
+       /* In sector is now size of all keyslot material space */
+
+       /* Data offset has priority */
+       if (data_offset)
+               header->payloadOffset = data_offset / SECTOR_SIZE;
+       else if (required_alignment) {
+               header->payloadOffset = size_round_up(header_sectors, (required_alignment / SECTOR_SIZE));
+               header->payloadOffset += (align_offset / SECTOR_SIZE);
+       } else
+               header->payloadOffset = 0;
+
+       if (header->payloadOffset && header->payloadOffset < header_sectors) {
+               log_err(ctx, _("Data offset for LUKS header must be "
+                              "either 0 or higher than header size."));
                return -EINVAL;
        }
 
        if (crypt_hmac_size(hashSpec) < LUKS_DIGESTSIZE) {
-               log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hashSpec);
+               log_err(ctx, _("Requested LUKS hash %s is not supported."), hashSpec);
                return -EINVAL;
        }
 
        if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
-               log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
+               log_err(ctx, _("Wrong LUKS UUID format provided."));
                return -EINVAL;
        }
        if (!uuid)
                uuid_generate(partitionUuid);
 
-       memset(header,0,sizeof(struct luks_phdr));
-
        /* Set Magic */
        memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
        header->version=1;
-       strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L);
-       strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L);
-       strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L);
+       strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L-1);
+       strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L-1);
+       strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L-1);
 
        header->keyBytes=vk->keylength;
 
        LUKS_fix_header_compatible(header);
 
-       r = LUKS_check_cipher(header, ctx);
-       if (r < 0)
-               return r;
-
-       log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
+       log_dbg(ctx, "Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
                header->version, header->hashSpec ,header->cipherName, header->cipherMode,
                header->keyBytes);
 
        r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_SALT);
        if(r < 0) {
-               log_err(ctx, _("Cannot create LUKS header: reading random salt failed.\n"));
+               log_err(ctx, _("Cannot create LUKS header: reading random salt failed."));
                return r;
        }
 
-       r = crypt_benchmark_kdf(ctx, "pbkdf2", header->hashSpec,
-                               "foo", 3, "bar", 3, PBKDF2_per_sec);
-       if (r < 0) {
-               log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"),
-                       header->hashSpec);
+       /* Compute master key digest */
+       pbkdf = crypt_get_pbkdf(ctx);
+       r = crypt_benchmark_pbkdf_internal(ctx, pbkdf, vk->keylength);
+       if (r < 0)
                return r;
-       }
+       assert(pbkdf->iterations);
 
-       /* Compute master key digest */
-       iteration_time_ms /= 8;
-       header->mkDigestIterations = at_least((uint32_t)(*PBKDF2_per_sec/1024) * iteration_time_ms,
-                                             LUKS_MKD_ITERATIONS_MIN);
+       if (pbkdf->flags & CRYPT_PBKDF_NO_BENCHMARK && pbkdf->time_ms == 0)
+               PBKDF2_temp = LUKS_MKD_ITERATIONS_MIN;
+       else    /* iterations per ms * LUKS_MKD_ITERATIONS_MS */
+               PBKDF2_temp = (double)pbkdf->iterations * LUKS_MKD_ITERATIONS_MS / pbkdf->time_ms;
 
-       r = crypt_pbkdf("pbkdf2", header->hashSpec, vk->key,vk->keylength,
+       if (PBKDF2_temp > (double)UINT32_MAX)
+               return -EINVAL;
+       header->mkDigestIterations = at_least((uint32_t)PBKDF2_temp, LUKS_MKD_ITERATIONS_MIN);
+       assert(header->mkDigestIterations);
+
+       r = crypt_pbkdf(CRYPT_KDF_PBKDF2, header->hashSpec, vk->key,vk->keylength,
                        header->mkDigestSalt, LUKS_SALTSIZE,
                        header->mkDigest,LUKS_DIGESTSIZE,
-                       header->mkDigestIterations);
-       if(r < 0) {
-               log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s).\n"),
+                       header->mkDigestIterations, 0, 0);
+       if (r < 0) {
+               log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s)."),
                        header->hashSpec);
                return r;
        }
 
-       currentSector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
-       blocksPerStripeSet = AF_split_sectors(vk->keylength, stripes);
-       for(i = 0; i < LUKS_NUMKEYS; ++i) {
-               header->keyblock[i].active = LUKS_KEY_DISABLED;
-               header->keyblock[i].keyMaterialOffset = currentSector;
-               header->keyblock[i].stripes = stripes;
-               currentSector = size_round_up(currentSector + blocksPerStripeSet,
-                                               LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
-       }
-
-       if (detached_metadata_device) {
-               /* for separate metadata device use alignPayload directly */
-               header->payloadOffset = alignPayload;
-       } else {
-               /* alignOffset - offset from natural device alignment provided by topology info */
-               currentSector = size_round_up(currentSector, alignPayload);
-               header->payloadOffset = currentSector + alignOffset;
-       }
-
         uuid_unparse(partitionUuid, header->uuid);
 
-       log_dbg("Data offset %d, UUID %s, digest iterations %" PRIu32,
+       log_dbg(ctx, "Data offset %d, UUID %s, digest iterations %" PRIu32,
                header->payloadOffset, header->uuid, header->mkDigestIterations);
 
        return 0;
@@ -754,7 +822,7 @@ int LUKS_hdr_uuid_set(
        uuid_t partitionUuid;
 
        if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
-               log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
+               log_err(ctx, _("Wrong LUKS UUID format provided."));
                return -EINVAL;
        }
        if (!uuid)
@@ -768,50 +836,40 @@ int LUKS_hdr_uuid_set(
 int LUKS_set_key(unsigned int keyIndex,
                 const char *password, size_t passwordLen,
                 struct luks_phdr *hdr, struct volume_key *vk,
-                uint32_t iteration_time_ms,
-                uint64_t *PBKDF2_per_sec,
                 struct crypt_device *ctx)
 {
        struct volume_key *derived_key;
        char *AfKey = NULL;
        size_t AFEKSize;
-       uint64_t PBKDF2_temp;
+       struct crypt_pbkdf_type *pbkdf;
        int r;
 
        if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) {
-               log_err(ctx, _("Key slot %d active, purge first.\n"), keyIndex);
+               log_err(ctx, _("Key slot %d active, purge first."), keyIndex);
                return -EINVAL;
        }
 
-       /* LUKS keyslot has always at least 4000 stripes accoding to specification */
+       /* LUKS keyslot has always at least 4000 stripes according to specification */
        if(hdr->keyblock[keyIndex].stripes < 4000) {
-               log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?\n"),
+               log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?"),
                        keyIndex);
                 return -EINVAL;
        }
 
-       log_dbg("Calculating data for key slot %d", keyIndex);
-
-       r = crypt_benchmark_kdf(ctx, "pbkdf2", hdr->hashSpec,
-                               "foo", 3, "bar", 3, PBKDF2_per_sec);
-       if (r < 0) {
-               log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"),
-                       hdr->hashSpec);
+       log_dbg(ctx, "Calculating data for key slot %d", keyIndex);
+       pbkdf = crypt_get_pbkdf(ctx);
+       r = crypt_benchmark_pbkdf_internal(ctx, pbkdf, vk->keylength);
+       if (r < 0)
                return r;
-       }
+       assert(pbkdf->iterations);
 
        /*
-        * Avoid floating point operation
         * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
         */
-       PBKDF2_temp = (*PBKDF2_per_sec / 2) * (uint64_t)iteration_time_ms;
-       PBKDF2_temp /= 1024;
-       if (PBKDF2_temp > UINT32_MAX)
-               PBKDF2_temp = UINT32_MAX;
-       hdr->keyblock[keyIndex].passwordIterations = at_least((uint32_t)PBKDF2_temp,
-                                                             LUKS_SLOT_ITERATIONS_MIN);
-
-       log_dbg("Key slot %d use %" PRIu32 " password iterations.", keyIndex, hdr->keyblock[keyIndex].passwordIterations);
+       hdr->keyblock[keyIndex].passwordIterations =
+               at_least(pbkdf->iterations, LUKS_SLOT_ITERATIONS_MIN);
+       log_dbg(ctx, "Key slot %d use %" PRIu32 " password iterations.", keyIndex,
+               hdr->keyblock[keyIndex].passwordIterations);
 
        derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
        if (!derived_key)
@@ -822,10 +880,10 @@ int LUKS_set_key(unsigned int keyIndex,
        if (r < 0)
                goto out;
 
-       r = crypt_pbkdf("pbkdf2", hdr->hashSpec, password, passwordLen,
+       r = crypt_pbkdf(CRYPT_KDF_PBKDF2, hdr->hashSpec, password, passwordLen,
                        hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
                        derived_key->key, hdr->keyBytes,
-                       hdr->keyblock[keyIndex].passwordIterations);
+                       hdr->keyblock[keyIndex].passwordIterations, 0, 0);
        if (r < 0)
                goto out;
 
@@ -840,13 +898,13 @@ int LUKS_set_key(unsigned int keyIndex,
                goto out;
        }
 
-       log_dbg("Using hash %s for AF in key slot %d, %d stripes",
+       log_dbg(ctx, "Using hash %s for AF in key slot %d, %d stripes",
                hdr->hashSpec, keyIndex, hdr->keyblock[keyIndex].stripes);
-       r = AF_split(vk->key,AfKey,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
+       r = AF_split(ctx, vk->key, AfKey, vk->keylength, hdr->keyblock[keyIndex].stripes, hdr->hashSpec);
        if (r < 0)
                goto out;
 
-       log_dbg("Updating key slot %d [0x%04x] area.", keyIndex,
+       log_dbg(ctx, "Updating key slot %d [0x%04x] area.", keyIndex,
                hdr->keyblock[keyIndex].keyMaterialOffset << 9);
        /* Encryption via dm */
        r = LUKS_encrypt_to_storage(AfKey,
@@ -859,7 +917,7 @@ int LUKS_set_key(unsigned int keyIndex,
                goto out;
 
        /* Mark the key as active in phdr */
-       r = LUKS_keyslot_set(hdr, (int)keyIndex, 1);
+       r = LUKS_keyslot_set(hdr, (int)keyIndex, 1, ctx);
        if (r < 0)
                goto out;
 
@@ -880,10 +938,10 @@ int LUKS_verify_volume_key(const struct luks_phdr *hdr,
 {
        char checkHashBuf[LUKS_DIGESTSIZE];
 
-       if (crypt_pbkdf("pbkdf2", hdr->hashSpec, vk->key, vk->keylength,
+       if (crypt_pbkdf(CRYPT_KDF_PBKDF2, hdr->hashSpec, vk->key, vk->keylength,
                        hdr->mkDigestSalt, LUKS_SALTSIZE,
                        checkHashBuf, LUKS_DIGESTSIZE,
-                       hdr->mkDigestIterations) < 0)
+                       hdr->mkDigestIterations, 0, 0) < 0)
                return -EINVAL;
 
        if (memcmp(checkHashBuf, hdr->mkDigest, LUKS_DIGESTSIZE))
@@ -906,7 +964,7 @@ static int LUKS_open_key(unsigned int keyIndex,
        size_t AFEKSize;
        int r;
 
-       log_dbg("Trying to open key slot %d [%s].", keyIndex,
+       log_dbg(ctx, "Trying to open key slot %d [%s].", keyIndex,
                dbg_slot_state(ki));
 
        if (ki < CRYPT_SLOT_ACTIVE)
@@ -924,14 +982,16 @@ static int LUKS_open_key(unsigned int keyIndex,
                goto out;
        }
 
-       r = crypt_pbkdf("pbkdf2", hdr->hashSpec, password, passwordLen,
+       r = crypt_pbkdf(CRYPT_KDF_PBKDF2, hdr->hashSpec, password, passwordLen,
                        hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
                        derived_key->key, hdr->keyBytes,
-                       hdr->keyblock[keyIndex].passwordIterations);
-       if (r < 0)
+                       hdr->keyblock[keyIndex].passwordIterations, 0, 0);
+       if (r < 0) {
+               log_err(ctx, _("Cannot open keyslot (using hash %s)."), hdr->hashSpec);
                goto out;
+       }
 
-       log_dbg("Reading key slot %d area.", keyIndex);
+       log_dbg(ctx, "Reading key slot %d area.", keyIndex);
        r = LUKS_decrypt_from_storage(AfKey,
                                      AFEKSize,
                                      hdr->cipherName, hdr->cipherMode,
@@ -941,13 +1001,15 @@ static int LUKS_open_key(unsigned int keyIndex,
        if (r < 0)
                goto out;
 
-       r = AF_merge(AfKey,vk->key,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
+       r = AF_merge(ctx, AfKey, vk->key, vk->keylength, hdr->keyblock[keyIndex].stripes, hdr->hashSpec);
        if (r < 0)
                goto out;
 
        r = LUKS_verify_volume_key(hdr, vk);
-       if (!r)
-               log_verbose(ctx, _("Key slot %d unlocked.\n"), keyIndex);
+
+       /* Allow only empty passphrase with null cipher */
+       if (!r && !strcmp(hdr->cipherName, "cipher_null") && passwordLen)
+               r = -EPERM;
 out:
        crypt_safe_free(AfKey);
        crypt_free_volume_key(derived_key);
@@ -961,7 +1023,7 @@ int LUKS_open_key_with_hdr(int keyIndex,
                           struct volume_key **vk,
                           struct crypt_device *ctx)
 {
-       unsigned int i;
+       unsigned int i, tried = 0;
        int r;
 
        *vk = crypt_alloc_volume_key(hdr->keyBytes, NULL);
@@ -971,7 +1033,7 @@ int LUKS_open_key_with_hdr(int keyIndex,
                return (r < 0) ? r : keyIndex;
        }
 
-       for(i = 0; i < LUKS_NUMKEYS; i++) {
+       for (i = 0; i < LUKS_NUMKEYS; i++) {
                r = LUKS_open_key(i, password, passwordLen, hdr, *vk, ctx);
                if(r == 0)
                        return i;
@@ -980,10 +1042,11 @@ int LUKS_open_key_with_hdr(int keyIndex,
                   former meaning password wrong, latter key slot inactive */
                if ((r != -EPERM) && (r != -ENOENT))
                        return r;
+               if (r == -EPERM)
+                       tried++;
        }
        /* Warning, early returns above */
-       log_err(ctx, _("No key available with this passphrase.\n"));
-       return -EPERM;
+       return tried ? -EPERM : -ENOENT;
 }
 
 int LUKS_del_key(unsigned int keyIndex,
@@ -998,9 +1061,9 @@ int LUKS_del_key(unsigned int keyIndex,
        if (r)
                return r;
 
-       r = LUKS_keyslot_set(hdr, keyIndex, 0);
+       r = LUKS_keyslot_set(hdr, keyIndex, 0, ctx);
        if (r) {
-               log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d.\n"),
+               log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d."),
                        keyIndex, LUKS_NUMKEYS - 1);
                return r;
        }
@@ -1009,16 +1072,16 @@ int LUKS_del_key(unsigned int keyIndex,
        startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
        endOffset = startOffset + AF_split_sectors(hdr->keyBytes, hdr->keyblock[keyIndex].stripes);
 
-       r = crypt_wipe(device, startOffset * SECTOR_SIZE,
-                      (endOffset - startOffset) * SECTOR_SIZE,
-                      CRYPT_WIPE_DISK, 0);
+       r = crypt_wipe_device(ctx, device, CRYPT_WIPE_SPECIAL, startOffset * SECTOR_SIZE,
+                             (endOffset - startOffset) * SECTOR_SIZE,
+                             (endOffset - startOffset) * SECTOR_SIZE, NULL, NULL);
        if (r) {
                if (r == -EACCES) {
-                       log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
+                       log_err(ctx, _("Cannot write to device %s, permission denied."),
                                device_path(device));
                        r = -EINVAL;
                } else
-                       log_err(ctx, _("Cannot wipe device %s.\n"),
+                       log_err(ctx, _("Cannot wipe device %s."),
                                device_path(device));
                return r;
        }
@@ -1077,7 +1140,7 @@ int LUKS_keyslot_active_count(struct luks_phdr *hdr)
        return num;
 }
 
-int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
+int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable, struct crypt_device *ctx)
 {
        crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);
 
@@ -1085,7 +1148,7 @@ int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
                return -EINVAL;
 
        hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISABLED;
-       log_dbg("Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");
+       log_dbg(ctx, "Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");
        return 0;
 }
 
@@ -1095,39 +1158,87 @@ int LUKS1_activate(struct crypt_device *cd,
                   uint32_t flags)
 {
        int r;
-       char *dm_cipher = NULL;
-       enum devcheck device_check;
        struct crypt_dm_active_device dmd = {
-               .target = DM_CRYPT,
-               .uuid   = crypt_get_uuid(cd),
-               .flags  = flags,
-               .size   = 0,
-               .data_device = crypt_data_device(cd),
-               .u.crypt = {
-                       .cipher = NULL,
-                       .vk     = vk,
-                       .offset = crypt_get_data_offset(cd),
-                       .iv_offset = 0,
-               }
+               .flags = flags,
+               .uuid = crypt_get_uuid(cd),
        };
 
-       if (dmd.flags & CRYPT_ACTIVATE_SHARED)
-               device_check = DEV_SHARED;
-       else
-               device_check = DEV_EXCL;
+       r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
+                       vk, crypt_get_cipher_spec(cd), crypt_get_iv_offset(cd),
+                       crypt_get_data_offset(cd), crypt_get_integrity(cd),
+                       crypt_get_integrity_tag_size(cd), crypt_get_sector_size(cd));
+       if (!r)
+               r = create_or_reload_device(cd, name, CRYPT_LUKS1, &dmd);
 
-       r = device_block_adjust(cd, dmd.data_device, device_check,
-                                dmd.u.crypt.offset, &dmd.size, &dmd.flags);
-       if (r)
-               return r;
+       dm_targets_free(cd, &dmd);
+
+       return r;
+}
+
+int LUKS_wipe_header_areas(struct luks_phdr *hdr,
+       struct crypt_device *ctx)
+{
+       int i, r;
+       uint64_t offset, length;
+       size_t wipe_block;
+
+       /* Wipe complete header, keyslots and padding areas with zeroes. */
+       offset = 0;
+       length = (uint64_t)hdr->payloadOffset * SECTOR_SIZE;
+       wipe_block = 1024 * 1024;
+
+       /* On detached header or bogus header, wipe at least the first 4k */
+       if (length == 0 || length > (LUKS_MAX_KEYSLOT_SIZE * LUKS_NUMKEYS)) {
+               length = 4096;
+               wipe_block = 4096;
+       }
 
-       r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
+       log_dbg(ctx, "Wiping LUKS areas (0x%06" PRIx64 " - 0x%06" PRIx64") with zeroes.",
+               offset, length + offset);
+
+       r = crypt_wipe_device(ctx, crypt_metadata_device(ctx), CRYPT_WIPE_ZERO,
+                             offset, length, wipe_block, NULL, NULL);
        if (r < 0)
-               return -ENOMEM;
+               return r;
+
+       /* Wipe keyslots areas */
+       wipe_block = 1024 * 1024;
+       for (i = 0; i < LUKS_NUMKEYS; i++) {
+               r = LUKS_keyslot_area(hdr, i, &offset, &length);
+               if (r < 0)
+                       return r;
+
+               /* Ignore too big LUKS1 keyslots here */
+               if (length > LUKS_MAX_KEYSLOT_SIZE ||
+                   offset > (LUKS_MAX_KEYSLOT_SIZE - length))
+                       continue;
+
+               if (length == 0 || offset < 4096)
+                       return -EINVAL;
 
-       dmd.u.crypt.cipher = dm_cipher;
-       r = dm_create_device(cd, name, CRYPT_LUKS1, &dmd, 0);
+               log_dbg(ctx, "Wiping keyslot %i area (0x%06" PRIx64 " - 0x%06" PRIx64") with random data.",
+                       i, offset, length + offset);
+
+               r = crypt_wipe_device(ctx, crypt_metadata_device(ctx), CRYPT_WIPE_RANDOM,
+                               offset, length, wipe_block, NULL, NULL);
+               if (r < 0)
+                       return r;
+       }
 
-       free(dm_cipher);
        return r;
 }
+
+int LUKS_keyslot_pbkdf(struct luks_phdr *hdr, int keyslot, struct crypt_pbkdf_type *pbkdf)
+{
+       if (LUKS_keyslot_info(hdr, keyslot) < CRYPT_SLOT_ACTIVE)
+               return -EINVAL;
+
+       pbkdf->type = CRYPT_KDF_PBKDF2;
+       pbkdf->hash = hdr->hashSpec;
+       pbkdf->iterations = hdr->keyblock[keyslot].passwordIterations;
+       pbkdf->max_memory_kb = 0;
+       pbkdf->parallel_threads = 0;
+       pbkdf->time_ms = 0;
+       pbkdf->flags = 0;
+       return 0;
+}