2 * LUKS - Linux Unified Key Setup
4 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <sys/types.h>
23 #include <netinet/in.h>
32 #include <uuid/uuid.h>
38 /* Get size of struct luks_phdr with all keyslots material space */
39 static size_t LUKS_device_sectors(size_t keyLen)
41 size_t keyslot_sectors, sector;
44 keyslot_sectors = AF_split_sectors(keyLen, LUKS_STRIPES);
45 sector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
47 for (i = 0; i < LUKS_NUMKEYS; i++) {
48 sector = size_round_up(sector, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
49 sector += keyslot_sectors;
55 int LUKS_keyslot_area(struct luks_phdr *hdr,
60 if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
63 *offset = hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZE;
64 *length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE;
69 static int LUKS_check_device_size(struct crypt_device *ctx, size_t keyLength)
71 struct device *device = crypt_metadata_device(ctx);
72 uint64_t dev_sectors, hdr_sectors;
77 if(device_size(device, &dev_sectors)) {
78 log_dbg("Cannot get device size for device %s.", device_path(device));
82 dev_sectors >>= SECTOR_SHIFT;
83 hdr_sectors = LUKS_device_sectors(keyLength);
84 log_dbg("Key length %u, device size %" PRIu64 " sectors, header size %"
85 PRIu64 " sectors.",keyLength, dev_sectors, hdr_sectors);
87 if (hdr_sectors > dev_sectors) {
88 log_err(ctx, _("Device %s is too small.\n"), device_path(device));
95 /* Check keyslot to prevent access outside of header and keyslot area */
96 static int LUKS_check_keyslot_size(const struct luks_phdr *phdr, unsigned int keyIndex)
98 uint32_t secs_per_stripes;
100 /* First sectors is the header itself */
101 if (phdr->keyblock[keyIndex].keyMaterialOffset * SECTOR_SIZE < sizeof(*phdr)) {
102 log_dbg("Invalid offset %u in keyslot %u.",
103 phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex);
107 /* Ignore following check for detached header where offset can be zero. */
108 if (phdr->payloadOffset == 0)
111 if (phdr->payloadOffset <= phdr->keyblock[keyIndex].keyMaterialOffset) {
112 log_dbg("Invalid offset %u in keyslot %u (beyond data area offset %u).",
113 phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex,
114 phdr->payloadOffset);
118 secs_per_stripes = AF_split_sectors(phdr->keyBytes, phdr->keyblock[keyIndex].stripes);
120 if (phdr->payloadOffset < (phdr->keyblock[keyIndex].keyMaterialOffset + secs_per_stripes)) {
121 log_dbg("Invalid keyslot size %u (offset %u, stripes %u) in "
122 "keyslot %u (beyond data area offset %u).",
124 phdr->keyblock[keyIndex].keyMaterialOffset,
125 phdr->keyblock[keyIndex].stripes,
126 keyIndex, phdr->payloadOffset);
133 static const char *dbg_slot_state(crypt_keyslot_info ki)
136 case CRYPT_SLOT_INACTIVE:
138 case CRYPT_SLOT_ACTIVE:
140 case CRYPT_SLOT_ACTIVE_LAST:
141 return "ACTIVE_LAST";
142 case CRYPT_SLOT_INVALID:
149 const char *backup_file,
150 struct luks_phdr *hdr,
151 struct crypt_device *ctx)
153 struct device *device = crypt_metadata_device(ctx);
154 int r = 0, devfd = -1;
158 r = LUKS_read_phdr(hdr, 1, 0, ctx);
162 buffer_size = LUKS_device_sectors(hdr->keyBytes) << SECTOR_SHIFT;
163 buffer = crypt_safe_alloc(buffer_size);
164 if (!buffer || buffer_size < LUKS_ALIGN_KEYSLOTS) {
169 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes).",
170 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS);
172 devfd = open(device_path(device), O_RDONLY | O_DIRECT | O_SYNC);
174 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device_path(device));
179 if (read_blockwise(devfd, device_block_size(device), buffer, buffer_size) < buffer_size) {
185 /* Wipe unused area, so backup cannot contain old signatures */
186 if (hdr->keyblock[0].keyMaterialOffset * SECTOR_SIZE == LUKS_ALIGN_KEYSLOTS)
187 memset(buffer + sizeof(*hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(*hdr));
189 devfd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
192 log_err(ctx, _("Requested header backup file %s already exists.\n"), backup_file);
194 log_err(ctx, _("Cannot create header backup file %s.\n"), backup_file);
198 if (write(devfd, buffer, buffer_size) < buffer_size) {
199 log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
209 crypt_safe_free(buffer);
213 int LUKS_hdr_restore(
214 const char *backup_file,
215 struct luks_phdr *hdr,
216 struct crypt_device *ctx)
218 struct device *device = crypt_metadata_device(ctx);
219 int r = 0, devfd = -1, diff_uuid = 0;
220 ssize_t buffer_size = 0;
221 char *buffer = NULL, msg[200];
222 struct luks_phdr hdr_file;
224 r = LUKS_read_phdr_backup(backup_file, &hdr_file, 0, ctx);
229 buffer_size = LUKS_device_sectors(hdr_file.keyBytes) << SECTOR_SHIFT;
231 if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
232 log_err(ctx, _("Backup file doesn't contain valid LUKS header.\n"));
237 buffer = crypt_safe_alloc(buffer_size);
243 devfd = open(backup_file, O_RDONLY);
245 log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
250 if (read(devfd, buffer, buffer_size) < buffer_size) {
251 log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
257 r = LUKS_read_phdr(hdr, 0, 0, ctx);
259 log_dbg("Device %s already contains LUKS header, checking UUID and offset.", device_path(device));
260 if(hdr->payloadOffset != hdr_file.payloadOffset ||
261 hdr->keyBytes != hdr_file.keyBytes) {
262 log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.\n"));
266 if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L))
270 if (snprintf(msg, sizeof(msg), _("Device %s %s%s"), device_path(device),
271 r ? _("does not contain LUKS header. Replacing header can destroy data on that device.") :
272 _("already contains LUKS header. Replacing header will destroy existing keyslots."),
273 diff_uuid ? _("\nWARNING: real device header has different UUID than backup!") : "") < 0) {
278 if (!crypt_confirm(ctx, msg)) {
283 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes) to device %s.",
284 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(device));
286 devfd = open(device_path(device), O_RDWR | O_DIRECT | O_SYNC);
289 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
290 device_path(device));
292 log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
297 if (write_blockwise(devfd, device_block_size(device), buffer, buffer_size) < buffer_size) {
303 /* Be sure to reload new data */
304 r = LUKS_read_phdr(hdr, 1, 0, ctx);
308 crypt_safe_free(buffer);
312 /* This routine should do some just basic recovery for known problems. */
313 static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
315 struct luks_phdr temp_phdr;
316 const unsigned char *sector = (const unsigned char*)phdr;
317 struct volume_key *vk;
318 uint64_t PBKDF2_per_sec = 1;
319 int i, bad, r, need_write = 0;
321 if (phdr->keyBytes != 16 && phdr->keyBytes != 32) {
322 log_err(ctx, _("Non standard key size, manual repair required.\n"));
325 /* cryptsetup 1.0 did not align to 4k, cannot repair this one */
326 if (phdr->keyblock[0].keyMaterialOffset < (LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE)) {
327 log_err(ctx, _("Non standard keyslots alignment, manual repair required.\n"));
331 vk = crypt_alloc_volume_key(phdr->keyBytes, NULL);
333 log_verbose(ctx, _("Repairing keyslots.\n"));
335 log_dbg("Generating second header with the same parameters for check.");
336 /* cipherName, cipherMode, hashSpec, uuid are already null terminated */
337 /* payloadOffset - cannot check */
338 r = LUKS_generate_phdr(&temp_phdr, vk, phdr->cipherName, phdr->cipherMode,
339 phdr->hashSpec,phdr->uuid, LUKS_STRIPES,
340 phdr->payloadOffset, 0,
344 log_err(ctx, _("Repair failed."));
348 for(i = 0; i < LUKS_NUMKEYS; ++i) {
349 if (phdr->keyblock[i].active == LUKS_KEY_ENABLED) {
350 log_dbg("Skipping repair for active keyslot %i.", i);
355 if (phdr->keyblock[i].keyMaterialOffset != temp_phdr.keyblock[i].keyMaterialOffset) {
356 log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u).\n"), i,
357 (unsigned)phdr->keyblock[i].keyMaterialOffset,
358 (unsigned)temp_phdr.keyblock[i].keyMaterialOffset);
359 phdr->keyblock[i].keyMaterialOffset = temp_phdr.keyblock[i].keyMaterialOffset;
363 if (phdr->keyblock[i].stripes != temp_phdr.keyblock[i].stripes) {
364 log_err(ctx, _("Keyslot %i: stripes repaired (%u -> %u).\n"), i,
365 (unsigned)phdr->keyblock[i].stripes,
366 (unsigned)temp_phdr.keyblock[i].stripes);
367 phdr->keyblock[i].stripes = temp_phdr.keyblock[i].stripes;
371 /* Known case - MSDOS partition table signature */
372 if (i == 6 && sector[0x1fe] == 0x55 && sector[0x1ff] == 0xaa) {
373 log_err(ctx, _("Keyslot %i: bogus partition signature.\n"), i);
378 log_err(ctx, _("Keyslot %i: salt wiped.\n"), i);
379 phdr->keyblock[i].active = LUKS_KEY_DISABLED;
380 memset(&phdr->keyblock[i].passwordSalt, 0x00, LUKS_SALTSIZE);
381 phdr->keyblock[i].passwordIterations = 0;
389 log_verbose(ctx, _("Writing LUKS header to disk.\n"));
390 r = LUKS_write_phdr(phdr, ctx);
393 crypt_free_volume_key(vk);
394 memset(&temp_phdr, 0, sizeof(temp_phdr));
398 static int _check_and_convert_hdr(const char *device,
399 struct luks_phdr *hdr,
400 int require_luks_device,
402 struct crypt_device *ctx)
406 char luksMagic[] = LUKS_MAGIC;
408 if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */
409 log_dbg("LUKS header not detected.");
410 if (require_luks_device)
411 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
413 } else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */
414 log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
418 hdr->hashSpec[LUKS_HASHSPEC_L - 1] = '\0';
419 if (crypt_hmac_size(hdr->hashSpec) < LUKS_DIGESTSIZE) {
420 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hdr->hashSpec);
424 /* Header detected */
425 hdr->payloadOffset = ntohl(hdr->payloadOffset);
426 hdr->keyBytes = ntohl(hdr->keyBytes);
427 hdr->mkDigestIterations = ntohl(hdr->mkDigestIterations);
429 for(i = 0; i < LUKS_NUMKEYS; ++i) {
430 hdr->keyblock[i].active = ntohl(hdr->keyblock[i].active);
431 hdr->keyblock[i].passwordIterations = ntohl(hdr->keyblock[i].passwordIterations);
432 hdr->keyblock[i].keyMaterialOffset = ntohl(hdr->keyblock[i].keyMaterialOffset);
433 hdr->keyblock[i].stripes = ntohl(hdr->keyblock[i].stripes);
434 if (LUKS_check_keyslot_size(hdr, i)) {
435 log_err(ctx, _("LUKS keyslot %u is invalid.\n"), i);
440 /* Avoid unterminated strings */
441 hdr->cipherName[LUKS_CIPHERNAME_L - 1] = '\0';
442 hdr->cipherMode[LUKS_CIPHERMODE_L - 1] = '\0';
443 hdr->uuid[UUID_STRING_L - 1] = '\0';
447 r = _keyslot_repair(hdr, ctx);
449 log_verbose(ctx, _("No known problems detected for LUKS header.\n"));
455 static void _to_lower(char *str, unsigned max_len)
457 for(; *str && max_len; str++, max_len--)
459 *str = tolower(*str);
462 static void LUKS_fix_header_compatible(struct luks_phdr *header)
464 /* Old cryptsetup expects "sha1", gcrypt allows case insensistive names,
465 * so always convert hash to lower case in header */
466 _to_lower(header->hashSpec, LUKS_HASHSPEC_L);
469 int LUKS_read_phdr_backup(const char *backup_file,
470 struct luks_phdr *hdr,
471 int require_luks_device,
472 struct crypt_device *ctx)
474 ssize_t hdr_size = sizeof(struct luks_phdr);
475 int devfd = 0, r = 0;
477 log_dbg("Reading LUKS header of size %d from backup file %s",
478 (int)hdr_size, backup_file);
480 devfd = open(backup_file, O_RDONLY);
482 log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
486 if (read(devfd, hdr, hdr_size) < hdr_size)
489 LUKS_fix_header_compatible(hdr);
490 r = _check_and_convert_hdr(backup_file, hdr,
491 require_luks_device, 0, ctx);
498 int LUKS_read_phdr(struct luks_phdr *hdr,
499 int require_luks_device,
501 struct crypt_device *ctx)
503 struct device *device = crypt_metadata_device(ctx);
504 ssize_t hdr_size = sizeof(struct luks_phdr);
505 int devfd = 0, r = 0;
507 /* LUKS header starts at offset 0, first keyslot on LUKS_ALIGN_KEYSLOTS */
508 assert(sizeof(struct luks_phdr) <= LUKS_ALIGN_KEYSLOTS);
510 /* Stripes count cannot be changed without additional code fixes yet */
511 assert(LUKS_STRIPES == 4000);
513 if (repair && !require_luks_device)
516 log_dbg("Reading LUKS header of size %d from device %s",
517 hdr_size, device_path(device));
519 devfd = open(device_path(device), O_RDONLY | O_DIRECT | O_SYNC);
521 log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
525 if (read_blockwise(devfd, device_block_size(device), hdr, hdr_size) < hdr_size)
528 r = _check_and_convert_hdr(device_path(device), hdr, require_luks_device,
532 r = LUKS_check_device_size(ctx, hdr->keyBytes);
538 int LUKS_write_phdr(struct luks_phdr *hdr,
539 struct crypt_device *ctx)
541 struct device *device = crypt_metadata_device(ctx);
542 ssize_t hdr_size = sizeof(struct luks_phdr);
545 struct luks_phdr convHdr;
548 log_dbg("Updating LUKS header of size %d on device %s",
549 sizeof(struct luks_phdr), device_path(device));
551 r = LUKS_check_device_size(ctx, hdr->keyBytes);
555 devfd = open(device_path(device), O_RDWR | O_DIRECT | O_SYNC);
558 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
559 device_path(device));
561 log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
565 memcpy(&convHdr, hdr, hdr_size);
566 memset(&convHdr._padding, 0, sizeof(convHdr._padding));
568 /* Convert every uint16/32_t item to network byte order */
569 convHdr.version = htons(hdr->version);
570 convHdr.payloadOffset = htonl(hdr->payloadOffset);
571 convHdr.keyBytes = htonl(hdr->keyBytes);
572 convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations);
573 for(i = 0; i < LUKS_NUMKEYS; ++i) {
574 convHdr.keyblock[i].active = htonl(hdr->keyblock[i].active);
575 convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].passwordIterations);
576 convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].keyMaterialOffset);
577 convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].stripes);
580 r = write_blockwise(devfd, device_block_size(device), &convHdr, hdr_size) < hdr_size ? -EIO : 0;
582 log_err(ctx, _("Error during update of LUKS header on device %s.\n"), device_path(device));
585 /* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
587 r = LUKS_read_phdr(hdr, 1, 0, ctx);
589 log_err(ctx, _("Error re-reading LUKS header after update on device %s.\n"),
590 device_path(device));
596 int LUKS_generate_phdr(struct luks_phdr *header,
597 const struct volume_key *vk,
598 const char *cipherName, const char *cipherMode, const char *hashSpec,
599 const char *uuid, unsigned int stripes,
600 unsigned int alignPayload,
601 unsigned int alignOffset,
602 uint32_t iteration_time_ms,
603 uint64_t *PBKDF2_per_sec,
604 int detached_metadata_device,
605 struct crypt_device *ctx)
608 size_t blocksPerStripeSet, currentSector;
610 uuid_t partitionUuid;
611 char luksMagic[] = LUKS_MAGIC;
613 /* For separate metadata device allow zero alignment */
614 if (alignPayload == 0 && !detached_metadata_device)
615 alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
617 if (crypt_hmac_size(hashSpec) < LUKS_DIGESTSIZE) {
618 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hashSpec);
622 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
623 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
627 uuid_generate(partitionUuid);
629 memset(header,0,sizeof(struct luks_phdr));
632 memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
634 strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L);
635 strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L);
636 strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L);
638 header->keyBytes=vk->keylength;
640 LUKS_fix_header_compatible(header);
642 log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
643 header->version, header->hashSpec ,header->cipherName, header->cipherMode,
646 r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_SALT);
648 log_err(ctx, _("Cannot create LUKS header: reading random salt failed.\n"));
652 r = crypt_benchmark_kdf(ctx, "pbkdf2", header->hashSpec,
653 "foo", 3, "bar", 3, PBKDF2_per_sec);
655 log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"),
660 /* Compute master key digest */
661 iteration_time_ms /= 8;
662 header->mkDigestIterations = at_least((uint32_t)(*PBKDF2_per_sec/1024) * iteration_time_ms,
663 LUKS_MKD_ITERATIONS_MIN);
665 r = crypt_pbkdf("pbkdf2", header->hashSpec, vk->key,vk->keylength,
666 header->mkDigestSalt, LUKS_SALTSIZE,
667 header->mkDigest,LUKS_DIGESTSIZE,
668 header->mkDigestIterations);
670 log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s).\n"),
675 currentSector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
676 blocksPerStripeSet = AF_split_sectors(vk->keylength, stripes);
677 for(i = 0; i < LUKS_NUMKEYS; ++i) {
678 header->keyblock[i].active = LUKS_KEY_DISABLED;
679 header->keyblock[i].keyMaterialOffset = currentSector;
680 header->keyblock[i].stripes = stripes;
681 currentSector = size_round_up(currentSector + blocksPerStripeSet,
682 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
685 if (detached_metadata_device) {
686 /* for separate metadata device use alignPayload directly */
687 header->payloadOffset = alignPayload;
689 /* alignOffset - offset from natural device alignment provided by topology info */
690 currentSector = size_round_up(currentSector, alignPayload);
691 header->payloadOffset = currentSector + alignOffset;
694 uuid_unparse(partitionUuid, header->uuid);
696 log_dbg("Data offset %d, UUID %s, digest iterations %" PRIu32,
697 header->payloadOffset, header->uuid, header->mkDigestIterations);
702 int LUKS_hdr_uuid_set(
703 struct luks_phdr *hdr,
705 struct crypt_device *ctx)
707 uuid_t partitionUuid;
709 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
710 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
714 uuid_generate(partitionUuid);
716 uuid_unparse(partitionUuid, hdr->uuid);
718 return LUKS_write_phdr(hdr, ctx);
721 int LUKS_set_key(unsigned int keyIndex,
722 const char *password, size_t passwordLen,
723 struct luks_phdr *hdr, struct volume_key *vk,
724 uint32_t iteration_time_ms,
725 uint64_t *PBKDF2_per_sec,
726 struct crypt_device *ctx)
728 struct volume_key *derived_key;
731 uint64_t PBKDF2_temp;
734 if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) {
735 log_err(ctx, _("Key slot %d active, purge first.\n"), keyIndex);
739 /* LUKS keyslot has always at least 4000 stripes accoding to specification */
740 if(hdr->keyblock[keyIndex].stripes < 4000) {
741 log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?\n"),
746 log_dbg("Calculating data for key slot %d", keyIndex);
748 r = crypt_benchmark_kdf(ctx, "pbkdf2", hdr->hashSpec,
749 "foo", 3, "bar", 3, PBKDF2_per_sec);
751 log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"),
757 * Avoid floating point operation
758 * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
760 PBKDF2_temp = (*PBKDF2_per_sec / 2) * (uint64_t)iteration_time_ms;
762 if (PBKDF2_temp > UINT32_MAX)
763 PBKDF2_temp = UINT32_MAX;
764 hdr->keyblock[keyIndex].passwordIterations = at_least((uint32_t)PBKDF2_temp,
765 LUKS_SLOT_ITERATIONS_MIN);
767 log_dbg("Key slot %d use %d password iterations.", keyIndex, hdr->keyblock[keyIndex].passwordIterations);
769 derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
773 r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt,
774 LUKS_SALTSIZE, CRYPT_RND_SALT);
778 r = crypt_pbkdf("pbkdf2", hdr->hashSpec, password, passwordLen,
779 hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
780 derived_key->key, hdr->keyBytes,
781 hdr->keyblock[keyIndex].passwordIterations);
786 * AF splitting, the masterkey stored in vk->key is split to AfKey
788 assert(vk->keylength == hdr->keyBytes);
789 AFEKSize = AF_split_sectors(vk->keylength, hdr->keyblock[keyIndex].stripes) * SECTOR_SIZE;
790 AfKey = crypt_safe_alloc(AFEKSize);
796 log_dbg("Using hash %s for AF in key slot %d, %d stripes",
797 hdr->hashSpec, keyIndex, hdr->keyblock[keyIndex].stripes);
798 r = AF_split(vk->key,AfKey,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
802 log_dbg("Updating key slot %d [0x%04x] area.", keyIndex,
803 hdr->keyblock[keyIndex].keyMaterialOffset << 9);
804 /* Encryption via dm */
805 r = LUKS_encrypt_to_storage(AfKey,
807 hdr->cipherName, hdr->cipherMode,
809 hdr->keyblock[keyIndex].keyMaterialOffset,
814 /* Mark the key as active in phdr */
815 r = LUKS_keyslot_set(hdr, (int)keyIndex, 1);
819 r = LUKS_write_phdr(hdr, ctx);
825 crypt_safe_free(AfKey);
826 crypt_free_volume_key(derived_key);
830 /* Check whether a volume key is invalid. */
831 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
832 const struct volume_key *vk)
834 char checkHashBuf[LUKS_DIGESTSIZE];
836 if (crypt_pbkdf("pbkdf2", hdr->hashSpec, vk->key, vk->keylength,
837 hdr->mkDigestSalt, LUKS_SALTSIZE,
838 checkHashBuf, LUKS_DIGESTSIZE,
839 hdr->mkDigestIterations) < 0)
842 if (memcmp(checkHashBuf, hdr->mkDigest, LUKS_DIGESTSIZE))
848 /* Try to open a particular key slot */
849 static int LUKS_open_key(unsigned int keyIndex,
850 const char *password,
852 struct luks_phdr *hdr,
853 struct volume_key *vk,
854 struct crypt_device *ctx)
856 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex);
857 struct volume_key *derived_key;
862 log_dbg("Trying to open key slot %d [%s].", keyIndex,
865 if (ki < CRYPT_SLOT_ACTIVE)
868 derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
872 assert(vk->keylength == hdr->keyBytes);
873 AFEKSize = AF_split_sectors(vk->keylength, hdr->keyblock[keyIndex].stripes) * SECTOR_SIZE;
874 AfKey = crypt_safe_alloc(AFEKSize);
880 r = crypt_pbkdf("pbkdf2", hdr->hashSpec, password, passwordLen,
881 hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
882 derived_key->key, hdr->keyBytes,
883 hdr->keyblock[keyIndex].passwordIterations);
887 log_dbg("Reading key slot %d area.", keyIndex);
888 r = LUKS_decrypt_from_storage(AfKey,
890 hdr->cipherName, hdr->cipherMode,
892 hdr->keyblock[keyIndex].keyMaterialOffset,
897 r = AF_merge(AfKey,vk->key,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
901 r = LUKS_verify_volume_key(hdr, vk);
903 log_verbose(ctx, _("Key slot %d unlocked.\n"), keyIndex);
905 crypt_safe_free(AfKey);
906 crypt_free_volume_key(derived_key);
910 int LUKS_open_key_with_hdr(int keyIndex,
911 const char *password,
913 struct luks_phdr *hdr,
914 struct volume_key **vk,
915 struct crypt_device *ctx)
920 *vk = crypt_alloc_volume_key(hdr->keyBytes, NULL);
923 r = LUKS_open_key(keyIndex, password, passwordLen, hdr, *vk, ctx);
924 return (r < 0) ? r : keyIndex;
927 for(i = 0; i < LUKS_NUMKEYS; i++) {
928 r = LUKS_open_key(i, password, passwordLen, hdr, *vk, ctx);
932 /* Do not retry for errors that are no -EPERM or -ENOENT,
933 former meaning password wrong, latter key slot inactive */
934 if ((r != -EPERM) && (r != -ENOENT))
937 /* Warning, early returns above */
938 log_err(ctx, _("No key available with this passphrase.\n"));
942 int LUKS_del_key(unsigned int keyIndex,
943 struct luks_phdr *hdr,
944 struct crypt_device *ctx)
946 struct device *device = crypt_metadata_device(ctx);
947 unsigned int startOffset, endOffset;
950 r = LUKS_read_phdr(hdr, 1, 0, ctx);
954 r = LUKS_keyslot_set(hdr, keyIndex, 0);
956 log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d.\n"),
957 keyIndex, LUKS_NUMKEYS - 1);
961 /* secure deletion of key material */
962 startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
963 endOffset = startOffset + AF_split_sectors(hdr->keyBytes, hdr->keyblock[keyIndex].stripes);
965 r = crypt_wipe(device, startOffset * SECTOR_SIZE,
966 (endOffset - startOffset) * SECTOR_SIZE,
970 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
971 device_path(device));
974 log_err(ctx, _("Cannot wipe device %s.\n"),
975 device_path(device));
979 /* Wipe keyslot info */
980 memset(&hdr->keyblock[keyIndex].passwordSalt, 0, LUKS_SALTSIZE);
981 hdr->keyblock[keyIndex].passwordIterations = 0;
983 r = LUKS_write_phdr(hdr, ctx);
988 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot)
992 if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
993 return CRYPT_SLOT_INVALID;
995 if (hdr->keyblock[keyslot].active == LUKS_KEY_DISABLED)
996 return CRYPT_SLOT_INACTIVE;
998 if (hdr->keyblock[keyslot].active != LUKS_KEY_ENABLED)
999 return CRYPT_SLOT_INVALID;
1001 for(i = 0; i < LUKS_NUMKEYS; i++)
1002 if(i != keyslot && hdr->keyblock[i].active == LUKS_KEY_ENABLED)
1003 return CRYPT_SLOT_ACTIVE;
1005 return CRYPT_SLOT_ACTIVE_LAST;
1008 int LUKS_keyslot_find_empty(struct luks_phdr *hdr)
1012 for (i = 0; i < LUKS_NUMKEYS; i++)
1013 if(hdr->keyblock[i].active == LUKS_KEY_DISABLED)
1016 if (i == LUKS_NUMKEYS)
1022 int LUKS_keyslot_active_count(struct luks_phdr *hdr)
1026 for (i = 0; i < LUKS_NUMKEYS; i++)
1027 if(hdr->keyblock[i].active == LUKS_KEY_ENABLED)
1033 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
1035 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);
1037 if (ki == CRYPT_SLOT_INVALID)
1040 hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISABLED;
1041 log_dbg("Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");
1045 int LUKS1_activate(struct crypt_device *cd,
1047 struct volume_key *vk,
1051 char *dm_cipher = NULL;
1052 enum devcheck device_check;
1053 struct crypt_dm_active_device dmd = {
1055 .uuid = crypt_get_uuid(cd),
1058 .data_device = crypt_data_device(cd),
1062 .offset = crypt_get_data_offset(cd),
1067 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
1068 device_check = DEV_SHARED;
1070 device_check = DEV_EXCL;
1072 r = device_block_adjust(cd, dmd.data_device, device_check,
1073 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
1077 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
1081 dmd.u.crypt.cipher = dm_cipher;
1082 r = dm_create_device(cd, name, CRYPT_LUKS1, &dmd, 0);