"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/luks1/keymanage.c" between
cryptsetup-2.0.6.tar.xz and cryptsetup-2.1.0.tar.xz

About: cryptsetup is a utility used to conveniently setup disk encryption based on the dm-crypt kernel module. These include plain dm-crypt volumes, LUKS volumes, loop-AES and TrueCrypt compatible format.

keymanage.c  (cryptsetup-2.0.6.tar.xz):keymanage.c  (cryptsetup-2.1.0.tar.xz)
/* /*
* LUKS - Linux Unified Key Setup * LUKS - Linux Unified Key Setup
* *
* Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org> * Copyright (C) 2004-2006 Clemens Fruhwirth <clemens@endorphin.org>
* Copyright (C) 2009-2018, Red Hat, Inc. All rights reserved. * Copyright (C) 2009-2019 Red Hat, Inc. All rights reserved.
* Copyright (C) 2013-2018, Milan Broz * Copyright (C) 2013-2019 Milan Broz
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
skipping to change at line 40 skipping to change at line 40
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h> #include <assert.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include "luks.h" #include "luks.h"
#include "af.h" #include "af.h"
#include "internal.h" #include "internal.h"
/* Get size of struct luks_phdr with all keyslots material space */
static size_t LUKS_calculate_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(const struct luks_phdr *hdr, int LUKS_keyslot_area(const struct luks_phdr *hdr,
int keyslot, int keyslot,
uint64_t *offset, uint64_t *offset,
uint64_t *length) uint64_t *length)
{ {
if(keyslot >= LUKS_NUMKEYS || keyslot < 0) if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
return -EINVAL; return -EINVAL;
*offset = (uint64_t)hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZ E; *offset = (uint64_t)hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZ E;
*length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE; *length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE;
skipping to change at line 114 skipping to change at line 97
static int LUKS_check_device_size(struct crypt_device *ctx, const struct luks_ph dr *hdr, int falloc) static int LUKS_check_device_size(struct crypt_device *ctx, const struct luks_ph dr *hdr, int falloc)
{ {
struct device *device = crypt_metadata_device(ctx); struct device *device = crypt_metadata_device(ctx);
uint64_t dev_sectors, hdr_sectors; uint64_t dev_sectors, hdr_sectors;
if (!hdr->keyBytes) if (!hdr->keyBytes)
return -EINVAL; return -EINVAL;
if (device_size(device, &dev_sectors)) { if (device_size(device, &dev_sectors)) {
log_dbg("Cannot get device size for device %s.", device_path(devi ce)); log_dbg(ctx, "Cannot get device size for device %s.", device_path (device));
return -EIO; return -EIO;
} }
dev_sectors >>= SECTOR_SHIFT; dev_sectors >>= SECTOR_SHIFT;
hdr_sectors = LUKS_device_sectors(hdr); hdr_sectors = LUKS_device_sectors(hdr);
log_dbg("Key length %u, device size %" PRIu64 " sectors, header size %" log_dbg(ctx, "Key length %u, device size %" PRIu64 " sectors, header size %"
PRIu64 " sectors.", hdr->keyBytes, dev_sectors, hdr_sectors); PRIu64 " sectors.", hdr->keyBytes, dev_sectors, hdr_sectors);
if (hdr_sectors > dev_sectors) { if (hdr_sectors > dev_sectors) {
/* If it is header file, increase its size */ /* If it is header file, increase its size */
if (falloc && !device_fallocate(device, hdr_sectors << SECTOR_SHI FT)) if (falloc && !device_fallocate(device, hdr_sectors << SECTOR_SHI FT))
return 0; return 0;
log_err(ctx, _("Device %s is too small. (LUKS1 requires at least %" PRIu64 " bytes.)"), log_err(ctx, _("Device %s is too small. (LUKS1 requires at least %" PRIu64 " bytes.)"),
device_path(device), hdr_sectors * SECTOR_SIZE); device_path(device), hdr_sectors * SECTOR_SIZE);
return -EINVAL; return -EINVAL;
skipping to change at line 147 skipping to change at line 130
{ {
int i, prev, next, sorted_areas[LUKS_NUMKEYS] = { 0, 1, 2, 3, 4, 5, 6, 7 }; 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 ); uint32_t secs_per_stripes = AF_split_sectors(phdr->keyBytes, LUKS_STRIPES );
LUKS_sort_keyslots(phdr, sorted_areas); LUKS_sort_keyslots(phdr, sorted_areas);
/* Check keyslot to prevent access outside of header and keyslot area */ /* Check keyslot to prevent access outside of header and keyslot area */
for (i = 0; i < LUKS_NUMKEYS; i++) { for (i = 0; i < LUKS_NUMKEYS; i++) {
/* enforce stripes == 4000 */ /* enforce stripes == 4000 */
if (phdr->keyblock[i].stripes != LUKS_STRIPES) { if (phdr->keyblock[i].stripes != LUKS_STRIPES) {
log_dbg("Invalid stripes count %u in keyslot %u.", log_dbg(ctx, "Invalid stripes count %u in keyslot %u.",
phdr->keyblock[i].stripes, i); phdr->keyblock[i].stripes, i);
log_err(ctx, _("LUKS keyslot %u is invalid."), i); log_err(ctx, _("LUKS keyslot %u is invalid."), i);
return -1; return -1;
} }
/* First sectors is the header itself */ /* First sectors is the header itself */
if (phdr->keyblock[i].keyMaterialOffset * SECTOR_SIZE < sizeof(*p hdr)) { if (phdr->keyblock[i].keyMaterialOffset * SECTOR_SIZE < sizeof(*p hdr)) {
log_dbg("Invalid offset %u in keyslot %u.", log_dbg(ctx, "Invalid offset %u in keyslot %u.",
phdr->keyblock[i].keyMaterialOffset, i); phdr->keyblock[i].keyMaterialOffset, i);
log_err(ctx, _("LUKS keyslot %u is invalid."), i); log_err(ctx, _("LUKS keyslot %u is invalid."), i);
return -1; return -1;
} }
/* Ignore following check for detached header where offset can be zero. */ /* Ignore following check for detached header where offset can be zero. */
if (phdr->payloadOffset == 0) if (phdr->payloadOffset == 0)
continue; continue;
if (phdr->payloadOffset <= phdr->keyblock[i].keyMaterialOffset) { if (phdr->payloadOffset <= phdr->keyblock[i].keyMaterialOffset) {
log_dbg("Invalid offset %u in keyslot %u (beyond data are a offset %u).", log_dbg(ctx, "Invalid offset %u in keyslot %u (beyond dat a area offset %u).",
phdr->keyblock[i].keyMaterialOffset, i, phdr->keyblock[i].keyMaterialOffset, i,
phdr->payloadOffset); phdr->payloadOffset);
log_err(ctx, _("LUKS keyslot %u is invalid."), i); log_err(ctx, _("LUKS keyslot %u is invalid."), i);
return -1; return -1;
} }
if (phdr->payloadOffset < (phdr->keyblock[i].keyMaterialOffset + secs_per_stripes)) { if (phdr->payloadOffset < (phdr->keyblock[i].keyMaterialOffset + secs_per_stripes)) {
log_dbg("Invalid keyslot size %u (offset %u, stripes %u) in " log_dbg(ctx, "Invalid keyslot size %u (offset %u, stripes %u) in "
"keyslot %u (beyond data area offset %u).", "keyslot %u (beyond data area offset %u).",
secs_per_stripes, secs_per_stripes,
phdr->keyblock[i].keyMaterialOffset, phdr->keyblock[i].keyMaterialOffset,
phdr->keyblock[i].stripes, phdr->keyblock[i].stripes,
i, phdr->payloadOffset); i, phdr->payloadOffset);
log_err(ctx, _("LUKS keyslot %u is invalid."), i); log_err(ctx, _("LUKS keyslot %u is invalid."), i);
return -1; return -1;
} }
} }
/* check no keyslot overlaps with each other */ /* check no keyslot overlaps with each other */
for (i = 1; i < LUKS_NUMKEYS; i++) { for (i = 1; i < LUKS_NUMKEYS; i++) {
prev = sorted_areas[i-1]; prev = sorted_areas[i-1];
next = sorted_areas[i]; next = sorted_areas[i];
if (phdr->keyblock[next].keyMaterialOffset < if (phdr->keyblock[next].keyMaterialOffset <
(phdr->keyblock[prev].keyMaterialOffset + secs_per_stripes)) { (phdr->keyblock[prev].keyMaterialOffset + secs_per_stripes)) {
log_dbg("Not enough space in LUKS keyslot %d.", prev); log_dbg(ctx, "Not enough space in LUKS keyslot %d.", prev );
log_err(ctx, _("LUKS keyslot %u is invalid."), prev); log_err(ctx, _("LUKS keyslot %u is invalid."), prev);
return -1; return -1;
} }
} }
/* do not check last keyslot on purpose, it must be tested in device size check */ /* do not check last keyslot on purpose, it must be tested in device size check */
return 0; return 0;
} }
static const char *dbg_slot_state(crypt_keyslot_info ki) static const char *dbg_slot_state(crypt_keyslot_info ki)
skipping to change at line 238 skipping to change at line 221
hdr_size = LUKS_device_sectors(&hdr) << SECTOR_SHIFT; hdr_size = LUKS_device_sectors(&hdr) << SECTOR_SHIFT;
buffer_size = size_round_up(hdr_size, crypt_getpagesize()); buffer_size = size_round_up(hdr_size, crypt_getpagesize());
buffer = crypt_safe_alloc(buffer_size); buffer = crypt_safe_alloc(buffer_size);
if (!buffer || hdr_size < LUKS_ALIGN_KEYSLOTS || hdr_size > buffer_size) { if (!buffer || hdr_size < LUKS_ALIGN_KEYSLOTS || hdr_size > buffer_size) {
r = -ENOMEM; r = -ENOMEM;
goto out; 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); 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); devfd = device_open(ctx, device, O_RDONLY);
if (devfd < 0) { if (devfd < 0) {
log_err(ctx, _("Device %s is not a valid LUKS device."), device_p ath(device)); log_err(ctx, _("Device %s is not a valid LUKS device."), device_p ath(device));
r = -EINVAL; r = -EINVAL;
goto out; goto out;
} }
if (read_blockwise(devfd, device_block_size(device), device_alignment(dev ice), if (read_blockwise(devfd, device_block_size(ctx, device), device_alignmen t(device),
buffer, hdr_size) < (ssize_t)hdr_size) { buffer, hdr_size) < (ssize_t)hdr_size) {
r = -EIO; r = -EIO;
goto out; goto out;
} }
close(devfd); close(devfd);
/* Wipe unused area, so backup cannot contain old signatures */ /* Wipe unused area, so backup cannot contain old signatures */
if (hdr.keyblock[0].keyMaterialOffset * SECTOR_SIZE == LUKS_ALIGN_KEYSLOT S) if (hdr.keyblock[0].keyMaterialOffset * SECTOR_SIZE == LUKS_ALIGN_KEYSLOT S)
memset(buffer + sizeof(hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(hdr) ); memset(buffer + sizeof(hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(hdr) );
skipping to change at line 332 skipping to change at line 315
if (read_buffer(devfd, buffer, buffer_size) < buffer_size) { if (read_buffer(devfd, buffer, buffer_size) < buffer_size) {
log_err(ctx, _("Cannot read header backup file %s."), backup_file ); log_err(ctx, _("Cannot read header backup file %s."), backup_file );
r = -EIO; r = -EIO;
goto out; goto out;
} }
close(devfd); close(devfd);
devfd = -1; devfd = -1;
r = LUKS_read_phdr(hdr, 0, 0, ctx); r = LUKS_read_phdr(hdr, 0, 0, ctx);
if (r == 0) { if (r == 0) {
log_dbg("Device %s already contains LUKS header, checking UUID an d offset.", device_path(device)); log_dbg(ctx, "Device %s already contains LUKS header, checking UU ID and offset.", device_path(device));
if(hdr->payloadOffset != hdr_file.payloadOffset || if(hdr->payloadOffset != hdr_file.payloadOffset ||
hdr->keyBytes != hdr_file.keyBytes) { hdr->keyBytes != hdr_file.keyBytes) {
log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.")); log_err(ctx, _("Data offset or key size differs on device and backup, restore failed."));
r = -EINVAL; r = -EINVAL;
goto out; goto out;
} }
if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L)) if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L))
diff_uuid = 1; diff_uuid = 1;
} }
skipping to change at line 356 skipping to change at line 339
diff_uuid ? _("\nWARNING: real device header has different U UID than backup!") : "") < 0) { diff_uuid ? _("\nWARNING: real device header has different U UID than backup!") : "") < 0) {
r = -ENOMEM; r = -ENOMEM;
goto out; goto out;
} }
if (!crypt_confirm(ctx, msg)) { if (!crypt_confirm(ctx, msg)) {
r = -EINVAL; r = -EINVAL;
goto out; 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(devi ce)); sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(devi ce));
devfd = device_open(device, O_RDWR); devfd = device_open(ctx, device, O_RDWR);
if (devfd < 0) { if (devfd < 0) {
if (errno == EACCES) if (errno == EACCES)
log_err(ctx, _("Cannot write to device %s, permission den ied."), log_err(ctx, _("Cannot write to device %s, permission den ied."),
device_path(device)); device_path(device));
else else
log_err(ctx, _("Cannot open device %s."), device_path(dev ice)); log_err(ctx, _("Cannot open device %s."), device_path(dev ice));
r = -EINVAL; r = -EINVAL;
goto out; goto out;
} }
if (write_blockwise(devfd, device_block_size(device), device_alignment(de vice), if (write_blockwise(devfd, device_block_size(ctx, device), device_alignme nt(device),
buffer, buffer_size) < buffer_size) { buffer, buffer_size) < buffer_size) {
r = -EIO; r = -EIO;
goto out; goto out;
} }
close(devfd); close(devfd);
devfd = -1; devfd = -1;
/* Be sure to reload new data */ /* Be sure to reload new data */
r = LUKS_read_phdr(hdr, 1, 0, ctx); r = LUKS_read_phdr(hdr, 1, 0, ctx);
out: out:
if (devfd >= 0) { if (devfd >= 0) {
device_sync(device, devfd); device_sync(ctx, device, devfd);
close(devfd); close(devfd);
} }
crypt_safe_free(buffer); crypt_safe_free(buffer);
return r; return r;
} }
/* This routine should do some just basic recovery for known problems. */ /* This routine should do some just basic recovery for known problems. */
static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx) static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
{ {
struct luks_phdr temp_phdr; struct luks_phdr temp_phdr;
skipping to change at line 415 skipping to change at line 398
} }
r = LUKS_check_cipher(ctx, phdr->keyBytes, phdr->cipherName, phdr->cipher Mode); r = LUKS_check_cipher(ctx, phdr->keyBytes, phdr->cipherName, phdr->cipher Mode);
if (r < 0) if (r < 0)
return -EINVAL; return -EINVAL;
vk = crypt_alloc_volume_key(phdr->keyBytes, NULL); vk = crypt_alloc_volume_key(phdr->keyBytes, NULL);
log_verbose(ctx, _("Repairing keyslots.")); 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 */ /* cipherName, cipherMode, hashSpec, uuid are already null terminated */
/* payloadOffset - cannot check */ /* payloadOffset - cannot check */
r = LUKS_generate_phdr(&temp_phdr, vk, phdr->cipherName, phdr->cipherMode , r = LUKS_generate_phdr(&temp_phdr, vk, phdr->cipherName, phdr->cipherMode ,
phdr->hashSpec,phdr->uuid, LUKS_STRIPES, phdr->hashSpec, phdr->uuid,
phdr->payloadOffset, 0, phdr->payloadOffset * SECTOR_SIZE, 0, 0, ctx);
1, ctx);
if (r < 0) if (r < 0)
goto out; goto out;
for(i = 0; i < LUKS_NUMKEYS; ++i) { for(i = 0; i < LUKS_NUMKEYS; ++i) {
if (phdr->keyblock[i].active == LUKS_KEY_ENABLED) { 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; continue;
} }
bad = 0; bad = 0;
if (phdr->keyblock[i].keyMaterialOffset != temp_phdr.keyblock[i]. keyMaterialOffset) { if (phdr->keyblock[i].keyMaterialOffset != temp_phdr.keyblock[i]. keyMaterialOffset) {
log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u).") , i, log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u).") , i,
(unsigned)phdr->keyblock[i].keyMaterialOffset, (unsigned)phdr->keyblock[i].keyMaterialOffset,
(unsigned)temp_phdr.keyblock[i].keyMaterialOffset ); (unsigned)temp_phdr.keyblock[i].keyMaterialOffset );
phdr->keyblock[i].keyMaterialOffset = temp_phdr.keyblock[ i].keyMaterialOffset; phdr->keyblock[i].keyMaterialOffset = temp_phdr.keyblock[ i].keyMaterialOffset;
bad = 1; bad = 1;
skipping to change at line 494 skipping to change at line 476
struct luks_phdr *hdr, struct luks_phdr *hdr,
int require_luks_device, int require_luks_device,
int repair, int repair,
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
int r = 0; int r = 0;
unsigned int i; unsigned int i;
char luksMagic[] = LUKS_MAGIC; char luksMagic[] = LUKS_MAGIC;
if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check 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) if (require_luks_device)
log_err(ctx, _("Device %s is not a valid LUKS device."), device); log_err(ctx, _("Device %s is not a valid LUKS device."), device);
return -EINVAL; return -EINVAL;
} else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */ } 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."), hdr->version); log_err(ctx, _("Unsupported LUKS version %d."), hdr->version);
return -EINVAL; return -EINVAL;
} }
hdr->hashSpec[LUKS_HASHSPEC_L - 1] = '\0'; hdr->hashSpec[LUKS_HASHSPEC_L - 1] = '\0';
if (crypt_hmac_size(hdr->hashSpec) < LUKS_DIGESTSIZE) { if (crypt_hmac_size(hdr->hashSpec) < LUKS_DIGESTSIZE) {
skipping to change at line 568 skipping to change at line 550
} }
int LUKS_read_phdr_backup(const char *backup_file, int LUKS_read_phdr_backup(const char *backup_file,
struct luks_phdr *hdr, struct luks_phdr *hdr,
int require_luks_device, int require_luks_device,
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
ssize_t hdr_size = sizeof(struct luks_phdr); ssize_t hdr_size = sizeof(struct luks_phdr);
int devfd = 0, r = 0; 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); (int)hdr_size, backup_file);
devfd = open(backup_file, O_RDONLY); devfd = open(backup_file, O_RDONLY);
if (devfd == -1) { if (devfd == -1) {
log_err(ctx, _("Cannot open header backup file %s."), backup_file ); log_err(ctx, _("Cannot open header backup file %s."), backup_file );
return -ENOENT; return -ENOENT;
} }
if (read_buffer(devfd, hdr, hdr_size) < hdr_size) if (read_buffer(devfd, hdr, hdr_size) < hdr_size)
r = -EIO; r = -EIO;
skipping to change at line 607 skipping to change at line 589
/* LUKS header starts at offset 0, first keyslot on LUKS_ALIGN_KEYSLOTS * / /* LUKS header starts at offset 0, first keyslot on LUKS_ALIGN_KEYSLOTS * /
assert(sizeof(struct luks_phdr) <= LUKS_ALIGN_KEYSLOTS); assert(sizeof(struct luks_phdr) <= LUKS_ALIGN_KEYSLOTS);
/* Stripes count cannot be changed without additional code fixes yet */ /* Stripes count cannot be changed without additional code fixes yet */
assert(LUKS_STRIPES == 4000); assert(LUKS_STRIPES == 4000);
if (repair && !require_luks_device) if (repair && !require_luks_device)
return -EINVAL; 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)); hdr_size, device_path(device));
devfd = device_open(device, O_RDONLY); devfd = device_open(ctx, device, O_RDONLY);
if (devfd < 0) { if (devfd < 0) {
log_err(ctx, _("Cannot open device %s."), device_path(device)); log_err(ctx, _("Cannot open device %s."), device_path(device));
return -EINVAL; return -EINVAL;
} }
if (read_blockwise(devfd, device_block_size(device), device_alignment(dev ice), if (read_blockwise(devfd, device_block_size(ctx, device), device_alignmen t(device),
hdr, hdr_size) < hdr_size) hdr, hdr_size) < hdr_size)
r = -EIO; r = -EIO;
else else
r = _check_and_convert_hdr(device_path(device), hdr, require_luks _device, r = _check_and_convert_hdr(device_path(device), hdr, require_luks _device,
repair, ctx); repair, ctx);
if (!r) if (!r)
r = LUKS_check_device_size(ctx, hdr, 0); r = LUKS_check_device_size(ctx, hdr, 0);
/* /*
* Cryptsetup 1.0.0 did not align keyslots to 4k (very rare version). * Cryptsetup 1.0.0 did not align keyslots to 4k (very rare version).
* Disable direct-io to avoid possible IO errors if underlying device * Disable direct-io to avoid possible IO errors if underlying device
* has bigger sector size. * has bigger sector size.
*/ */
if (!r && hdr->keyblock[0].keyMaterialOffset * SECTOR_SIZE < LUKS_ALIGN_K EYSLOTS) { if (!r && hdr->keyblock[0].keyMaterialOffset * SECTOR_SIZE < LUKS_ALIGN_K EYSLOTS) {
log_dbg("Old unaligned LUKS keyslot detected, disabling direct-io ."); log_dbg(ctx, "Old unaligned LUKS keyslot detected, disabling dire ct-io.");
device_disable_direct_io(device); device_disable_direct_io(device);
} }
close(devfd); close(devfd);
return r; return r;
} }
int LUKS_write_phdr(struct luks_phdr *hdr, int LUKS_write_phdr(struct luks_phdr *hdr,
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
struct device *device = crypt_metadata_device(ctx); struct device *device = crypt_metadata_device(ctx);
ssize_t hdr_size = sizeof(struct luks_phdr); ssize_t hdr_size = sizeof(struct luks_phdr);
int devfd = 0; int devfd = 0;
unsigned int i; unsigned int i;
struct luks_phdr convHdr; struct luks_phdr convHdr;
int r; 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)); sizeof(struct luks_phdr), device_path(device));
r = LUKS_check_device_size(ctx, hdr, 1); r = LUKS_check_device_size(ctx, hdr, 1);
if (r) if (r)
return r; return r;
devfd = device_open(device, O_RDWR); devfd = device_open(ctx, device, O_RDWR);
if (devfd < 0) { if (devfd < 0) {
if (errno == EACCES) if (errno == EACCES)
log_err(ctx, _("Cannot write to device %s, permission den ied."), log_err(ctx, _("Cannot write to device %s, permission den ied."),
device_path(device)); device_path(device));
else else
log_err(ctx, _("Cannot open device %s."), device_path(dev ice)); log_err(ctx, _("Cannot open device %s."), device_path(dev ice));
return -EINVAL; return -EINVAL;
} }
memcpy(&convHdr, hdr, hdr_size); memcpy(&convHdr, hdr, hdr_size);
skipping to change at line 682 skipping to change at line 664
convHdr.payloadOffset = htonl(hdr->payloadOffset); convHdr.payloadOffset = htonl(hdr->payloadOffset);
convHdr.keyBytes = htonl(hdr->keyBytes); convHdr.keyBytes = htonl(hdr->keyBytes);
convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations); convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations);
for(i = 0; i < LUKS_NUMKEYS; ++i) { for(i = 0; i < LUKS_NUMKEYS; ++i) {
convHdr.keyblock[i].active = htonl(hdr->keyblock[i].a ctive); convHdr.keyblock[i].active = htonl(hdr->keyblock[i].a ctive);
convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].p asswordIterations); convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].p asswordIterations);
convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].k eyMaterialOffset); convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].k eyMaterialOffset);
convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].s tripes); convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].s tripes);
} }
r = write_blockwise(devfd, device_block_size(device), device_alignment(de vice), r = write_blockwise(devfd, device_block_size(ctx, device), device_alignme nt(device),
&convHdr, hdr_size) < hdr_size ? -EIO : 0; &convHdr, hdr_size) < hdr_size ? -EIO : 0;
if (r) if (r)
log_err(ctx, _("Error during update of LUKS header on device %s." ), device_path(device)); log_err(ctx, _("Error during update of LUKS header on device %s." ), device_path(device));
device_sync(device, devfd); device_sync(ctx, device, devfd);
close(devfd); close(devfd);
/* Re-read header from disk to be sure that in-memory and on-disk data ar e the same. */ /* Re-read header from disk to be sure that in-memory and on-disk data ar e the same. */
if (!r) { if (!r) {
r = LUKS_read_phdr(hdr, 1, 0, ctx); r = LUKS_read_phdr(hdr, 1, 0, ctx);
if (r) if (r)
log_err(ctx, _("Error re-reading LUKS header after update on device %s."), log_err(ctx, _("Error re-reading LUKS header after update on device %s."),
device_path(device)); device_path(device));
} }
return r; return r;
} }
/* Check that kernel supports requested cipher by decryption of one sector */ /* Check that kernel supports requested cipher by decryption of one sector */
int LUKS_check_cipher(struct crypt_device *ctx, size_t keylength, const char *ci pher, const char *cipher_mode) int LUKS_check_cipher(struct crypt_device *ctx, size_t keylength, const char *ci pher, const char *cipher_mode)
{ {
int r; int r;
struct volume_key *empty_key; struct volume_key *empty_key;
char buf[SECTOR_SIZE]; char buf[SECTOR_SIZE];
log_dbg("Checking if cipher %s-%s is usable.", cipher, cipher_mode); log_dbg(ctx, "Checking if cipher %s-%s is usable.", cipher, cipher_mode);
empty_key = crypt_alloc_volume_key(keylength, NULL); empty_key = crypt_alloc_volume_key(keylength, NULL);
if (!empty_key) if (!empty_key)
return -ENOMEM; return -ENOMEM;
/* No need to get KEY quality random but it must avoid known weak keys. * / /* 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); r = crypt_random_get(ctx, empty_key->key, empty_key->keylength, CRYPT_RND _NORMAL);
if (!r) if (!r)
r = LUKS_decrypt_from_storage(buf, sizeof(buf), cipher, cipher_mo de, empty_key, 0, ctx); r = LUKS_decrypt_from_storage(buf, sizeof(buf), cipher, cipher_mo de, empty_key, 0, ctx);
crypt_free_volume_key(empty_key); crypt_free_volume_key(empty_key);
crypt_memzero(buf, sizeof(buf)); crypt_memzero(buf, sizeof(buf));
return r; return r;
} }
int LUKS_generate_phdr(struct luks_phdr *header, int LUKS_generate_phdr(struct luks_phdr *header,
const struct volume_key *vk, const struct volume_key *vk,
const char *cipherName, const char *cipherMode, const char const char *cipherName,
*hashSpec, const char *cipherMode,
const char *uuid, unsigned int stripes, const char *hashSpec,
unsigned int alignPayload, const char *uuid,
unsigned int alignOffset, uint64_t data_offset, /* in bytes */
int detached_metadata_device, uint64_t align_offset, /* in bytes */
struct crypt_device *ctx) uint64_t required_alignment, /* in bytes */
struct crypt_device *ctx)
{ {
unsigned int i = 0, hdr_sectors = LUKS_calculate_device_sectors(vk->keyle int i, r;
ngth); size_t keyslot_sectors, header_sectors;
size_t blocksPerStripeSet, currentSector;
int r;
uuid_t partitionUuid; uuid_t partitionUuid;
struct crypt_pbkdf_type *pbkdf; struct crypt_pbkdf_type *pbkdf;
double PBKDF2_temp; double PBKDF2_temp;
char luksMagic[] = LUKS_MAGIC; char luksMagic[] = LUKS_MAGIC;
/* For separate metadata device allow zero alignment */ if (data_offset % SECTOR_SIZE || align_offset % SECTOR_SIZE ||
if (alignPayload == 0 && !detached_metadata_device) required_alignment % SECTOR_SIZE)
alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE; return -EINVAL;
if (alignPayload && detached_metadata_device && alignPayload < hdr_sector memset(header, 0, sizeof(struct luks_phdr));
s) {
log_err(ctx, _("Data offset for detached LUKS header must be " keyslot_sectors = AF_split_sectors(vk->keylength, LUKS_STRIPES);
"either 0 or higher than header size (%d sectors). header_sectors = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
"),
hdr_sectors); 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_a
lignment / 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; return -EINVAL;
} }
if (crypt_hmac_size(hashSpec) < LUKS_DIGESTSIZE) { if (crypt_hmac_size(hashSpec) < LUKS_DIGESTSIZE) {
log_err(ctx, _("Requested LUKS hash %s is not supported."), hashS pec); log_err(ctx, _("Requested LUKS hash %s is not supported."), hashS pec);
return -EINVAL; return -EINVAL;
} }
if (uuid && uuid_parse(uuid, partitionUuid) == -1) { if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
log_err(ctx, _("Wrong LUKS UUID format provided.")); log_err(ctx, _("Wrong LUKS UUID format provided."));
return -EINVAL; return -EINVAL;
} }
if (!uuid) if (!uuid)
uuid_generate(partitionUuid); uuid_generate(partitionUuid);
memset(header,0,sizeof(struct luks_phdr));
/* Set Magic */ /* Set Magic */
memcpy(header->magic,luksMagic,LUKS_MAGIC_L); memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
header->version=1; header->version=1;
strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L-1); strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L-1);
strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L-1); strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L-1);
strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L-1); strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L-1);
header->keyBytes=vk->keylength; header->keyBytes=vk->keylength;
LUKS_fix_header_compatible(header); LUKS_fix_header_compatible(header);
log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d b ytes", log_dbg(ctx, "Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
header->version, header->hashSpec ,header->cipherName, header->ci pherMode, header->version, header->hashSpec ,header->cipherName, header->ci pherMode,
header->keyBytes); header->keyBytes);
r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_ SALT); r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_ SALT);
if(r < 0) { if(r < 0) {
log_err(ctx, _("Cannot create LUKS header: reading random salt fa iled.")); log_err(ctx, _("Cannot create LUKS header: reading random salt fa iled."));
return r; return r;
} }
/* Compute master key digest */ /* Compute master key digest */
skipping to change at line 803 skipping to change at line 806
PBKDF2_temp = (double)pbkdf->iterations * LUKS_MKD_ITERATIONS_MS / pbkdf- >time_ms; PBKDF2_temp = (double)pbkdf->iterations * LUKS_MKD_ITERATIONS_MS / pbkdf- >time_ms;
if (PBKDF2_temp > (double)UINT32_MAX) if (PBKDF2_temp > (double)UINT32_MAX)
return -EINVAL; return -EINVAL;
header->mkDigestIterations = at_least((uint32_t)PBKDF2_temp, LUKS_MKD_ITE RATIONS_MIN); header->mkDigestIterations = at_least((uint32_t)PBKDF2_temp, LUKS_MKD_ITE RATIONS_MIN);
r = crypt_pbkdf(CRYPT_KDF_PBKDF2, header->hashSpec, vk->key,vk->keylength , r = crypt_pbkdf(CRYPT_KDF_PBKDF2, header->hashSpec, vk->key,vk->keylength ,
header->mkDigestSalt, LUKS_SALTSIZE, header->mkDigestSalt, LUKS_SALTSIZE,
header->mkDigest,LUKS_DIGESTSIZE, header->mkDigest,LUKS_DIGESTSIZE,
header->mkDigestIterations, 0, 0); header->mkDigestIterations, 0, 0);
if(r < 0) { if (r < 0) {
log_err(ctx, _("Cannot create LUKS header: header digest failed ( using hash %s)."), log_err(ctx, _("Cannot create LUKS header: header digest failed ( using hash %s)."),
header->hashSpec); header->hashSpec);
return r; 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); 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); header->payloadOffset, header->uuid, header->mkDigestIterations);
return 0; return 0;
} }
int LUKS_hdr_uuid_set( int LUKS_hdr_uuid_set(
struct luks_phdr *hdr, struct luks_phdr *hdr,
const char *uuid, const char *uuid,
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
skipping to change at line 878 skipping to change at line 862
return -EINVAL; return -EINVAL;
} }
/* LUKS keyslot has always at least 4000 stripes according to specificati on */ /* LUKS keyslot has always at least 4000 stripes according to specificati on */
if(hdr->keyblock[keyIndex].stripes < 4000) { if(hdr->keyblock[keyIndex].stripes < 4000) {
log_err(ctx, _("Key slot %d material includes too few stripes. He ader manipulation?"), log_err(ctx, _("Key slot %d material includes too few stripes. He ader manipulation?"),
keyIndex); keyIndex);
return -EINVAL; return -EINVAL;
} }
log_dbg("Calculating data for key slot %d", keyIndex); log_dbg(ctx, "Calculating data for key slot %d", keyIndex);
pbkdf = crypt_get_pbkdf(ctx); pbkdf = crypt_get_pbkdf(ctx);
r = crypt_benchmark_pbkdf_internal(ctx, pbkdf, vk->keylength); r = crypt_benchmark_pbkdf_internal(ctx, pbkdf, vk->keylength);
if (r < 0) if (r < 0)
return r; return r;
assert(pbkdf->iterations); assert(pbkdf->iterations);
/* /*
* Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
*/ */
hdr->keyblock[keyIndex].passwordIterations = hdr->keyblock[keyIndex].passwordIterations =
at_least(pbkdf->iterations, LUKS_SLOT_ITERATIONS_MIN); at_least(pbkdf->iterations, LUKS_SLOT_ITERATIONS_MIN);
log_dbg("Key slot %d use %" PRIu32 " password iterations.", keyIndex, log_dbg(ctx, "Key slot %d use %" PRIu32 " password iterations.", keyIndex ,
hdr->keyblock[keyIndex].passwordIterations); hdr->keyblock[keyIndex].passwordIterations);
derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL); derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
if (!derived_key) if (!derived_key)
return -ENOMEM; return -ENOMEM;
r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt, r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt,
LUKS_SALTSIZE, CRYPT_RND_SALT); LUKS_SALTSIZE, CRYPT_RND_SALT);
if (r < 0) if (r < 0)
goto out; goto out;
skipping to change at line 920 skipping to change at line 904
* AF splitting, the masterkey stored in vk->key is split to AfKey * AF splitting, the masterkey stored in vk->key is split to AfKey
*/ */
assert(vk->keylength == hdr->keyBytes); assert(vk->keylength == hdr->keyBytes);
AFEKSize = AF_split_sectors(vk->keylength, hdr->keyblock[keyIndex].stripe s) * SECTOR_SIZE; AFEKSize = AF_split_sectors(vk->keylength, hdr->keyblock[keyIndex].stripe s) * SECTOR_SIZE;
AfKey = crypt_safe_alloc(AFEKSize); AfKey = crypt_safe_alloc(AFEKSize);
if (!AfKey) { if (!AfKey) {
r = -ENOMEM; r = -ENOMEM;
goto out; 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); 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) if (r < 0)
goto out; 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); hdr->keyblock[keyIndex].keyMaterialOffset << 9);
/* Encryption via dm */ /* Encryption via dm */
r = LUKS_encrypt_to_storage(AfKey, r = LUKS_encrypt_to_storage(AfKey,
AFEKSize, AFEKSize,
hdr->cipherName, hdr->cipherMode, hdr->cipherName, hdr->cipherMode,
derived_key, derived_key,
hdr->keyblock[keyIndex].keyMaterialOffset, hdr->keyblock[keyIndex].keyMaterialOffset,
ctx); ctx);
if (r < 0) if (r < 0)
goto out; goto out;
/* Mark the key as active in phdr */ /* 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) if (r < 0)
goto out; goto out;
r = LUKS_write_phdr(hdr, ctx); r = LUKS_write_phdr(hdr, ctx);
if (r < 0) if (r < 0)
goto out; goto out;
r = 0; r = 0;
out: out:
crypt_safe_free(AfKey); crypt_safe_free(AfKey);
skipping to change at line 986 skipping to change at line 970
struct luks_phdr *hdr, struct luks_phdr *hdr,
struct volume_key *vk, struct volume_key *vk,
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex); crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex);
struct volume_key *derived_key; struct volume_key *derived_key;
char *AfKey; char *AfKey;
size_t AFEKSize; size_t AFEKSize;
int r; 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)); dbg_slot_state(ki));
if (ki < CRYPT_SLOT_ACTIVE) if (ki < CRYPT_SLOT_ACTIVE)
return -ENOENT; return -ENOENT;
derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL); derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
if (!derived_key) if (!derived_key)
return -ENOMEM; return -ENOMEM;
assert(vk->keylength == hdr->keyBytes); assert(vk->keylength == hdr->keyBytes);
skipping to change at line 1011 skipping to change at line 995
goto out; goto out;
} }
r = crypt_pbkdf(CRYPT_KDF_PBKDF2, hdr->hashSpec, password, passwordLen, r = crypt_pbkdf(CRYPT_KDF_PBKDF2, hdr->hashSpec, password, passwordLen,
hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE, hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
derived_key->key, hdr->keyBytes, derived_key->key, hdr->keyBytes,
hdr->keyblock[keyIndex].passwordIterations, 0, 0); hdr->keyblock[keyIndex].passwordIterations, 0, 0);
if (r < 0) if (r < 0)
goto out; 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, r = LUKS_decrypt_from_storage(AfKey,
AFEKSize, AFEKSize,
hdr->cipherName, hdr->cipherMode, hdr->cipherName, hdr->cipherMode,
derived_key, derived_key,
hdr->keyblock[keyIndex].keyMaterialOffset, hdr->keyblock[keyIndex].keyMaterialOffset,
ctx); ctx);
if (r < 0) if (r < 0)
goto out; 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) if (r < 0)
goto out; goto out;
r = LUKS_verify_volume_key(hdr, vk); r = LUKS_verify_volume_key(hdr, vk);
/* Allow only empty passphrase with null cipher */ /* Allow only empty passphrase with null cipher */
if (!r && !strcmp(hdr->cipherName, "cipher_null") && passwordLen) if (!r && !strcmp(hdr->cipherName, "cipher_null") && passwordLen)
r = -EPERM; r = -EPERM;
out: out:
crypt_safe_free(AfKey); crypt_safe_free(AfKey);
skipping to change at line 1079 skipping to change at line 1063
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
struct device *device = crypt_metadata_device(ctx); struct device *device = crypt_metadata_device(ctx);
unsigned int startOffset, endOffset; unsigned int startOffset, endOffset;
int r; int r;
r = LUKS_read_phdr(hdr, 1, 0, ctx); r = LUKS_read_phdr(hdr, 1, 0, ctx);
if (r) if (r)
return r; return r;
r = LUKS_keyslot_set(hdr, keyIndex, 0); r = LUKS_keyslot_set(hdr, keyIndex, 0, ctx);
if (r) { if (r) {
log_err(ctx, _("Key slot %d is invalid, please select keyslot bet ween 0 and %d."), log_err(ctx, _("Key slot %d is invalid, please select keyslot bet ween 0 and %d."),
keyIndex, LUKS_NUMKEYS - 1); keyIndex, LUKS_NUMKEYS - 1);
return r; return r;
} }
/* secure deletion of key material */ /* secure deletion of key material */
startOffset = hdr->keyblock[keyIndex].keyMaterialOffset; startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
endOffset = startOffset + AF_split_sectors(hdr->keyBytes, hdr->keyblock[k eyIndex].stripes); endOffset = startOffset + AF_split_sectors(hdr->keyBytes, hdr->keyblock[k eyIndex].stripes);
skipping to change at line 1158 skipping to change at line 1142
{ {
int i, num = 0; int i, num = 0;
for (i = 0; i < LUKS_NUMKEYS; i++) for (i = 0; i < LUKS_NUMKEYS; i++)
if(hdr->keyblock[i].active == LUKS_KEY_ENABLED) if(hdr->keyblock[i].active == LUKS_KEY_ENABLED)
num++; num++;
return num; 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 cryp t_device *ctx)
{ {
crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot); crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);
if (ki == CRYPT_SLOT_INVALID) if (ki == CRYPT_SLOT_INVALID)
return -EINVAL; return -EINVAL;
hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISA BLED; hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISA BLED;
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 ? "ena bled" : "disabled");
return 0; return 0;
} }
int LUKS1_activate(struct crypt_device *cd, int LUKS1_activate(struct crypt_device *cd,
const char *name, const char *name,
struct volume_key *vk, struct volume_key *vk,
uint32_t flags) uint32_t flags)
{ {
int r; int r;
char *dm_cipher = NULL;
enum devcheck device_check;
struct crypt_dm_active_device dmd = { struct crypt_dm_active_device dmd = {
.target = DM_CRYPT, .flags = flags,
.uuid = crypt_get_uuid(cd), .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,
.sector_size = crypt_get_sector_size(cd),
}
}; };
if (dmd.flags & CRYPT_ACTIVATE_SHARED) r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
device_check = DEV_SHARED; vk, crypt_get_cipher_spec(cd), crypt_get_iv_offset(cd),
else crypt_get_data_offset(cd), crypt_get_integrity(cd),
device_check = DEV_EXCL; crypt_get_integrity_tag_size(cd), crypt_get_sector_size(c
d));
r = device_block_adjust(cd, dmd.data_device, device_check, if (!r)
dmd.u.crypt.offset, &dmd.size, &dmd.flags); r = create_or_reload_device(cd, name, CRYPT_LUKS1, &dmd);
if (r)
return r;
r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_
mode(cd));
if (r < 0)
return -ENOMEM;
dmd.u.crypt.cipher = dm_cipher; dm_targets_free(cd, &dmd);
r = dm_create_device(cd, name, CRYPT_LUKS1, &dmd, 0);
free(dm_cipher);
return r; return r;
} }
int LUKS_wipe_header_areas(struct luks_phdr *hdr, int LUKS_wipe_header_areas(struct luks_phdr *hdr,
struct crypt_device *ctx) struct crypt_device *ctx)
{ {
int i, r; int i, r;
uint64_t offset, length; uint64_t offset, length;
size_t wipe_block; size_t wipe_block;
skipping to change at line 1232 skipping to change at line 1195
offset = 0; offset = 0;
length = (uint64_t)hdr->payloadOffset * SECTOR_SIZE; length = (uint64_t)hdr->payloadOffset * SECTOR_SIZE;
wipe_block = 1024 * 1024; wipe_block = 1024 * 1024;
/* On detached header or bogus header, wipe at least the first 4k */ /* On detached header or bogus header, wipe at least the first 4k */
if (length == 0 || length > (LUKS_MAX_KEYSLOT_SIZE * LUKS_NUMKEYS)) { if (length == 0 || length > (LUKS_MAX_KEYSLOT_SIZE * LUKS_NUMKEYS)) {
length = 4096; length = 4096;
wipe_block = 4096; wipe_block = 4096;
} }
log_dbg("Wiping LUKS areas (0x%06" PRIx64 " - 0x%06" PRIx64") with zeroes .", log_dbg(ctx, "Wiping LUKS areas (0x%06" PRIx64 " - 0x%06" PRIx64") with z eroes.",
offset, length + offset); offset, length + offset);
r = crypt_wipe_device(ctx, crypt_metadata_device(ctx), CRYPT_WIPE_ZERO, r = crypt_wipe_device(ctx, crypt_metadata_device(ctx), CRYPT_WIPE_ZERO,
offset, length, wipe_block, NULL, NULL); offset, length, wipe_block, NULL, NULL);
if (r < 0) if (r < 0)
return r; return r;
/* Wipe keyslots areas */ /* Wipe keyslots areas */
wipe_block = 1024 * 1024; wipe_block = 1024 * 1024;
for (i = 0; i < LUKS_NUMKEYS; i++) { for (i = 0; i < LUKS_NUMKEYS; i++) {
skipping to change at line 1255 skipping to change at line 1218
return r; return r;
/* Ignore too big LUKS1 keyslots here */ /* Ignore too big LUKS1 keyslots here */
if (length > LUKS_MAX_KEYSLOT_SIZE || if (length > LUKS_MAX_KEYSLOT_SIZE ||
offset > (LUKS_MAX_KEYSLOT_SIZE - length)) offset > (LUKS_MAX_KEYSLOT_SIZE - length))
continue; continue;
if (length == 0 || offset < 4096) if (length == 0 || offset < 4096)
return -EINVAL; return -EINVAL;
log_dbg("Wiping keyslot %i area (0x%06" PRIx64 " - 0x%06" PRIx64" ) with random data.", log_dbg(ctx, "Wiping keyslot %i area (0x%06" PRIx64 " - 0x%06" PR Ix64") with random data.",
i, offset, length + offset); i, offset, length + offset);
r = crypt_wipe_device(ctx, crypt_metadata_device(ctx), CRYPT_WIPE _RANDOM, r = crypt_wipe_device(ctx, crypt_metadata_device(ctx), CRYPT_WIPE _RANDOM,
offset, length, wipe_block, NULL, NULL); offset, length, wipe_block, NULL, NULL);
if (r < 0) if (r < 0)
return r; return r;
} }
return r; return r;
} }
int LUKS_keyslot_pbkdf(struct luks_phdr *hdr, int keyslot, struct crypt_pbkdf_ty
pe *pbkdf)
{
if (keyslot >= LUKS_NUMKEYS || keyslot < 0)
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;
}
 End of changes. 60 change blocks. 
146 lines changed or deleted 104 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)