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>
39 #define div_round_up(a,b) ({ \
40 typeof(a) __a = (a); \
41 typeof(b) __b = (b); \
42 (__a - 1) / __b + 1; \
45 static inline int round_up_modulo(int x, int m) {
46 return div_round_up(x, m) * m;
49 /* Get size of struct luks_phrd with all keyslots material space */
50 static uint64_t LUKS_device_sectors(size_t keyLen)
52 uint64_t keyslot_sectors, sector;
55 keyslot_sectors = div_round_up(keyLen * LUKS_STRIPES, SECTOR_SIZE);
56 sector = round_up_modulo(LUKS_PHDR_SIZE, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
58 for (i = 0; i < LUKS_NUMKEYS; i++) {
59 sector = round_up_modulo(sector, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
60 sector += keyslot_sectors;
66 static int LUKS_check_device_size(struct crypt_device *ctx, const char *device,
69 uint64_t dev_sectors, hdr_sectors;
74 if(device_size(device, &dev_sectors)) {
75 log_dbg("Cannot get device size for device %s.", device);
79 dev_sectors >>= SECTOR_SHIFT;
80 hdr_sectors = LUKS_device_sectors(keyLength);
81 log_dbg("Key length %u, device size %" PRIu64 " sectors, header size %"
82 PRIu64 " sectors.",keyLength, dev_sectors, hdr_sectors);
84 if (hdr_sectors > dev_sectors) {
85 log_err(ctx, _("Device %s is too small.\n"), device);
92 /* Check keyslot to prevent access outside of header and keyslot area */
93 static int LUKS_check_keyslot_size(const struct luks_phdr *phdr, unsigned int keyIndex)
95 uint32_t secs_per_stripes;
97 /* First sectors is the header itself */
98 if (phdr->keyblock[keyIndex].keyMaterialOffset * SECTOR_SIZE < sizeof(*phdr)) {
99 log_dbg("Invalid offset %u in keyslot %u.",
100 phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex);
104 /* Ignore following check for detached header where offset can be zero. */
105 if (phdr->payloadOffset == 0)
108 if (phdr->payloadOffset <= phdr->keyblock[keyIndex].keyMaterialOffset) {
109 log_dbg("Invalid offset %u in keyslot %u (beyond data area offset %u).",
110 phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex,
111 phdr->payloadOffset);
115 secs_per_stripes = div_round_up(phdr->keyBytes * phdr->keyblock[keyIndex].stripes, SECTOR_SIZE);
117 if (phdr->payloadOffset < (phdr->keyblock[keyIndex].keyMaterialOffset + secs_per_stripes)) {
118 log_dbg("Invalid keyslot size %u (offset %u, stripes %u) in "
119 "keyslot %u (beyond data area offset %u).",
121 phdr->keyblock[keyIndex].keyMaterialOffset,
122 phdr->keyblock[keyIndex].stripes,
123 keyIndex, phdr->payloadOffset);
130 static const char *dbg_slot_state(crypt_keyslot_info ki)
133 case CRYPT_SLOT_INACTIVE:
135 case CRYPT_SLOT_ACTIVE:
137 case CRYPT_SLOT_ACTIVE_LAST:
138 return "ACTIVE_LAST";
139 case CRYPT_SLOT_INVALID:
146 const char *backup_file,
148 struct luks_phdr *hdr,
149 struct crypt_device *ctx)
151 int r = 0, devfd = -1;
156 if(stat(backup_file, &st) == 0) {
157 log_err(ctx, _("Requested file %s already exist.\n"), backup_file);
161 r = LUKS_read_phdr(device, hdr, 1, 0, ctx);
165 buffer_size = LUKS_device_sectors(hdr->keyBytes) << SECTOR_SHIFT;
166 buffer = crypt_safe_alloc(buffer_size);
167 if (!buffer || buffer_size < LUKS_ALIGN_KEYSLOTS) {
172 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes).",
173 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS);
175 devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
177 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
182 if(read_blockwise(devfd, buffer, buffer_size) < buffer_size) {
188 /* Wipe unused area, so backup cannot contain old signatures */
189 memset(buffer + sizeof(*hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(*hdr));
191 devfd = creat(backup_file, S_IRUSR);
196 if(write(devfd, buffer, buffer_size) < buffer_size) {
197 log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
207 crypt_safe_free(buffer);
211 int LUKS_hdr_restore(
212 const char *backup_file,
214 struct luks_phdr *hdr,
215 struct crypt_device *ctx)
217 int r = 0, devfd = -1, diff_uuid = 0;
219 char *buffer = NULL, msg[200];
221 struct luks_phdr hdr_file;
223 if(stat(backup_file, &st) < 0) {
224 log_err(ctx, _("Backup file %s doesn't exist.\n"), backup_file);
228 r = LUKS_read_phdr_backup(backup_file, device, &hdr_file, 0, ctx);
230 buffer_size = LUKS_device_sectors(hdr_file.keyBytes) << SECTOR_SHIFT;
232 if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
233 log_err(ctx, _("Backup file doesn't contain valid LUKS header.\n"));
238 buffer = crypt_safe_alloc(buffer_size);
244 devfd = open(backup_file, O_RDONLY);
246 log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
251 if(read(devfd, buffer, buffer_size) < buffer_size) {
252 log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
258 r = LUKS_read_phdr(device, hdr, 0, 0, ctx);
260 log_dbg("Device %s already contains LUKS header, checking UUID and offset.", device);
261 if(hdr->payloadOffset != hdr_file.payloadOffset ||
262 hdr->keyBytes != hdr_file.keyBytes) {
263 log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.\n"));
267 if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L))
271 if (snprintf(msg, sizeof(msg), _("Device %s %s%s"), device,
272 r ? _("does not contain LUKS header. Replacing header can destroy data on that device.") :
273 _("already contains LUKS header. Replacing header will destroy existing keyslots."),
274 diff_uuid ? _("\nWARNING: real device header has different UUID than backup!") : "") < 0) {
279 if (!crypt_confirm(ctx, msg)) {
284 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes) to device %s.",
285 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device);
287 devfd = open(device, O_WRONLY | O_DIRECT | O_SYNC);
289 log_err(ctx, _("Cannot open device %s.\n"), device);
294 if(write_blockwise(devfd, buffer, buffer_size) < buffer_size) {
300 /* Be sure to reload new data */
301 r = LUKS_read_phdr(device, hdr, 1, 0, ctx);
305 crypt_safe_free(buffer);
309 /* This routine should do some just basic recovery for known problems. */
310 static int _keyslot_repair(const char *device, struct luks_phdr *phdr, struct crypt_device *ctx)
312 struct luks_phdr temp_phdr;
313 const unsigned char *sector = (const unsigned char*)phdr;
314 struct volume_key *vk;
315 uint64_t PBKDF2_per_sec = 1;
316 int i, bad, r, need_write = 0;
318 if (phdr->keyBytes != 16 && phdr->keyBytes != 32) {
319 log_err(ctx, _("Non standard key size, manual repair required.\n"));
322 /* cryptsetup 1.0 did not align to 4k, cannot repair this one */
323 if (phdr->keyblock[0].keyMaterialOffset < (LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE)) {
324 log_err(ctx, _("Non standard keyslots alignment, manual repair required.\n"));
328 vk = crypt_alloc_volume_key(phdr->keyBytes, NULL);
330 log_verbose(ctx, _("Repairing keyslots.\n"));
332 log_dbg("Generating second header with the same parameters for check.");
333 /* cipherName, cipherMode, hashSpec, uuid are already null terminated */
334 /* payloadOffset - cannot check */
335 r = LUKS_generate_phdr(&temp_phdr, vk, phdr->cipherName, phdr->cipherMode,
336 phdr->hashSpec,phdr->uuid, LUKS_STRIPES,
337 phdr->payloadOffset, 0,
341 log_err(ctx, _("Repair failed."));
345 for(i = 0; i < LUKS_NUMKEYS; ++i) {
346 if (phdr->keyblock[i].active == LUKS_KEY_ENABLED) {
347 log_dbg("Skipping repair for active keyslot %i.", i);
352 if (phdr->keyblock[i].keyMaterialOffset != temp_phdr.keyblock[i].keyMaterialOffset) {
353 log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u).\n"), i,
354 (unsigned)phdr->keyblock[i].keyMaterialOffset,
355 (unsigned)temp_phdr.keyblock[i].keyMaterialOffset);
356 phdr->keyblock[i].keyMaterialOffset = temp_phdr.keyblock[i].keyMaterialOffset;
360 if (phdr->keyblock[i].stripes != temp_phdr.keyblock[i].stripes) {
361 log_err(ctx, _("Keyslot %i: stripes repaired (%u -> %u).\n"), i,
362 (unsigned)phdr->keyblock[i].stripes,
363 (unsigned)temp_phdr.keyblock[i].stripes);
364 phdr->keyblock[i].stripes = temp_phdr.keyblock[i].stripes;
368 /* Known case - MSDOS partition table signature */
369 if (i == 6 && sector[0x1fe] == 0x55 && sector[0x1ff] == 0xaa) {
370 log_err(ctx, _("Keyslot %i: bogus partition signature.\n"), i);
375 log_err(ctx, _("Keyslot %i: salt wiped.\n"), i);
376 phdr->keyblock[i].active = LUKS_KEY_DISABLED;
377 memset(&phdr->keyblock[i].passwordSalt, 0x00, LUKS_SALTSIZE);
378 phdr->keyblock[i].passwordIterations = 0;
386 log_verbose(ctx, _("Writing LUKS header to disk.\n"));
387 r = LUKS_write_phdr(device, phdr, ctx);
390 crypt_free_volume_key(vk);
391 memset(&temp_phdr, 0, sizeof(temp_phdr));
395 static int _check_and_convert_hdr(const char *device,
396 struct luks_phdr *hdr,
397 int require_luks_device,
399 struct crypt_device *ctx)
403 char luksMagic[] = LUKS_MAGIC;
405 if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */
406 log_dbg("LUKS header not detected.");
407 if (require_luks_device)
408 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
410 } else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */
411 log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
415 hdr->hashSpec[LUKS_HASHSPEC_L - 1] = '\0';
416 if (PBKDF2_HMAC_ready(hdr->hashSpec) < 0) {
417 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hdr->hashSpec);
421 /* Header detected */
422 hdr->payloadOffset = ntohl(hdr->payloadOffset);
423 hdr->keyBytes = ntohl(hdr->keyBytes);
424 hdr->mkDigestIterations = ntohl(hdr->mkDigestIterations);
426 for(i = 0; i < LUKS_NUMKEYS; ++i) {
427 hdr->keyblock[i].active = ntohl(hdr->keyblock[i].active);
428 hdr->keyblock[i].passwordIterations = ntohl(hdr->keyblock[i].passwordIterations);
429 hdr->keyblock[i].keyMaterialOffset = ntohl(hdr->keyblock[i].keyMaterialOffset);
430 hdr->keyblock[i].stripes = ntohl(hdr->keyblock[i].stripes);
431 if (LUKS_check_keyslot_size(hdr, i)) {
432 log_err(ctx, _("LUKS keyslot %u is invalid.\n"), i);
437 /* Avoid unterminated strings */
438 hdr->cipherName[LUKS_CIPHERNAME_L - 1] = '\0';
439 hdr->cipherMode[LUKS_CIPHERMODE_L - 1] = '\0';
440 hdr->uuid[UUID_STRING_L - 1] = '\0';
444 r = _keyslot_repair(device, hdr, ctx);
446 log_verbose(ctx, _("No known problems detected for LUKS header.\n"));
452 static void _to_lower(char *str, unsigned max_len)
454 for(; *str && max_len; str++, max_len--)
456 *str = tolower(*str);
459 static void LUKS_fix_header_compatible(struct luks_phdr *header)
461 /* Old cryptsetup expects "sha1", gcrypt allows case insensistive names,
462 * so always convert hash to lower case in header */
463 _to_lower(header->hashSpec, LUKS_HASHSPEC_L);
466 int LUKS_read_phdr_backup(const char *backup_file,
468 struct luks_phdr *hdr,
469 int require_luks_device,
470 struct crypt_device *ctx)
472 ssize_t hdr_size = sizeof(struct luks_phdr);
473 int devfd = 0, r = 0;
475 log_dbg("Reading LUKS header of size %d from backup file %s",
476 (int)hdr_size, backup_file);
478 devfd = open(backup_file, O_RDONLY);
480 log_err(ctx, _("Cannot open file %s.\n"), device);
484 if (read(devfd, hdr, hdr_size) < hdr_size)
487 LUKS_fix_header_compatible(hdr);
488 r = _check_and_convert_hdr(backup_file, hdr,
489 require_luks_device, 0, ctx);
496 int LUKS_read_phdr(const char *device,
497 struct luks_phdr *hdr,
498 int require_luks_device,
500 struct crypt_device *ctx)
502 ssize_t hdr_size = sizeof(struct luks_phdr);
503 int devfd = 0, r = 0;
505 if (repair && !require_luks_device)
508 log_dbg("Reading LUKS header of size %d from device %s",
511 devfd = open(device,O_RDONLY | O_DIRECT | O_SYNC);
513 log_err(ctx, _("Cannot open device %s.\n"), device);
517 if (read_blockwise(devfd, hdr, hdr_size) < hdr_size)
520 r = _check_and_convert_hdr(device, hdr, require_luks_device,
524 r = LUKS_check_device_size(ctx, device, hdr->keyBytes);
530 int LUKS_write_phdr(const char *device,
531 struct luks_phdr *hdr,
532 struct crypt_device *ctx)
534 ssize_t hdr_size = sizeof(struct luks_phdr);
537 struct luks_phdr convHdr;
540 log_dbg("Updating LUKS header of size %d on device %s",
541 sizeof(struct luks_phdr), device);
543 r = LUKS_check_device_size(ctx, device, hdr->keyBytes);
547 devfd = open(device,O_RDWR | O_DIRECT | O_SYNC);
549 log_err(ctx, _("Cannot open device %s.\n"), device);
553 memcpy(&convHdr, hdr, hdr_size);
554 memset(&convHdr._padding, 0, sizeof(convHdr._padding));
556 /* Convert every uint16/32_t item to network byte order */
557 convHdr.version = htons(hdr->version);
558 convHdr.payloadOffset = htonl(hdr->payloadOffset);
559 convHdr.keyBytes = htonl(hdr->keyBytes);
560 convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations);
561 for(i = 0; i < LUKS_NUMKEYS; ++i) {
562 convHdr.keyblock[i].active = htonl(hdr->keyblock[i].active);
563 convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].passwordIterations);
564 convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].keyMaterialOffset);
565 convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].stripes);
568 r = write_blockwise(devfd, &convHdr, hdr_size) < hdr_size ? -EIO : 0;
570 log_err(ctx, _("Error during update of LUKS header on device %s.\n"), device);
573 /* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
575 r = LUKS_read_phdr(device, hdr, 1, 0, ctx);
577 log_err(ctx, _("Error re-reading LUKS header after update on device %s.\n"), device);
583 static int LUKS_PBKDF2_performance_check(const char *hashSpec,
584 uint64_t *PBKDF2_per_sec,
585 struct crypt_device *ctx)
587 if (!*PBKDF2_per_sec) {
588 if (PBKDF2_performance_check(hashSpec, PBKDF2_per_sec) < 0) {
589 log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"), hashSpec);
592 log_dbg("PBKDF2: %" PRIu64 " iterations per second using hash %s.", *PBKDF2_per_sec, hashSpec);
598 int LUKS_generate_phdr(struct luks_phdr *header,
599 const struct volume_key *vk,
600 const char *cipherName, const char *cipherMode, const char *hashSpec,
601 const char *uuid, unsigned int stripes,
602 unsigned int alignPayload,
603 unsigned int alignOffset,
604 uint32_t iteration_time_ms,
605 uint64_t *PBKDF2_per_sec,
606 const char *metadata_device,
607 struct crypt_device *ctx)
610 unsigned int blocksPerStripeSet = div_round_up(vk->keylength*stripes,SECTOR_SIZE);
612 uuid_t partitionUuid;
614 char luksMagic[] = LUKS_MAGIC;
616 /* For separate metadata device allow zero alignment */
617 if (alignPayload == 0 && !metadata_device)
618 alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
620 if (PBKDF2_HMAC_ready(hashSpec) < 0) {
621 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hashSpec);
625 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
626 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
630 uuid_generate(partitionUuid);
632 memset(header,0,sizeof(struct luks_phdr));
635 memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
637 strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L);
638 strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L);
639 strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L);
641 header->keyBytes=vk->keylength;
643 LUKS_fix_header_compatible(header);
645 log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
646 header->version, header->hashSpec ,header->cipherName, header->cipherMode,
649 r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_SALT);
651 log_err(ctx, _("Cannot create LUKS header: reading random salt failed.\n"));
655 if ((r = LUKS_PBKDF2_performance_check(header->hashSpec, PBKDF2_per_sec, ctx)))
658 /* Compute master key digest */
659 iteration_time_ms /= 8;
660 header->mkDigestIterations = at_least((uint32_t)(*PBKDF2_per_sec/1024) * iteration_time_ms,
661 LUKS_MKD_ITERATIONS_MIN);
663 r = PBKDF2_HMAC(header->hashSpec,vk->key,vk->keylength,
664 header->mkDigestSalt,LUKS_SALTSIZE,
665 header->mkDigestIterations,
666 header->mkDigest,LUKS_DIGESTSIZE);
668 log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s).\n"),
673 currentSector = round_up_modulo(LUKS_PHDR_SIZE, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
674 for(i = 0; i < LUKS_NUMKEYS; ++i) {
675 header->keyblock[i].active = LUKS_KEY_DISABLED;
676 header->keyblock[i].keyMaterialOffset = currentSector;
677 header->keyblock[i].stripes = stripes;
678 currentSector = round_up_modulo(currentSector + blocksPerStripeSet,
679 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
682 if (metadata_device) {
683 /* for separate metadata device use alignPayload directly */
684 header->payloadOffset = alignPayload;
686 /* alignOffset - offset from natural device alignment provided by topology info */
687 currentSector = round_up_modulo(currentSector, alignPayload);
688 header->payloadOffset = currentSector + alignOffset;
691 uuid_unparse(partitionUuid, header->uuid);
693 log_dbg("Data offset %d, UUID %s, digest iterations %" PRIu32,
694 header->payloadOffset, header->uuid, header->mkDigestIterations);
699 int LUKS_hdr_uuid_set(
701 struct luks_phdr *hdr,
703 struct crypt_device *ctx)
705 uuid_t partitionUuid;
707 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
708 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
712 uuid_generate(partitionUuid);
714 uuid_unparse(partitionUuid, hdr->uuid);
716 return LUKS_write_phdr(device, hdr, ctx);
719 int LUKS_set_key(const char *device, unsigned int keyIndex,
720 const char *password, size_t passwordLen,
721 struct luks_phdr *hdr, struct volume_key *vk,
722 uint32_t iteration_time_ms,
723 uint64_t *PBKDF2_per_sec,
724 struct crypt_device *ctx)
726 struct volume_key *derived_key;
728 unsigned int AFEKSize;
729 uint64_t PBKDF2_temp;
732 if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) {
733 log_err(ctx, _("Key slot %d active, purge first.\n"), keyIndex);
737 if(hdr->keyblock[keyIndex].stripes < LUKS_STRIPES) {
738 log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?\n"),
743 log_dbg("Calculating data for key slot %d", keyIndex);
745 if ((r = LUKS_PBKDF2_performance_check(hdr->hashSpec, PBKDF2_per_sec, ctx)))
749 * Avoid floating point operation
750 * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
752 PBKDF2_temp = (*PBKDF2_per_sec / 2) * (uint64_t)iteration_time_ms;
754 if (PBKDF2_temp > UINT32_MAX)
755 PBKDF2_temp = UINT32_MAX;
756 hdr->keyblock[keyIndex].passwordIterations = at_least((uint32_t)PBKDF2_temp,
757 LUKS_SLOT_ITERATIONS_MIN);
759 log_dbg("Key slot %d use %d password iterations.", keyIndex, hdr->keyblock[keyIndex].passwordIterations);
761 derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
765 r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt,
766 LUKS_SALTSIZE, CRYPT_RND_SALT);
770 r = PBKDF2_HMAC(hdr->hashSpec, password,passwordLen,
771 hdr->keyblock[keyIndex].passwordSalt,LUKS_SALTSIZE,
772 hdr->keyblock[keyIndex].passwordIterations,
773 derived_key->key, hdr->keyBytes);
778 * AF splitting, the masterkey stored in vk->key is split to AfKey
780 assert(vk->keylength == hdr->keyBytes);
781 AFEKSize = hdr->keyblock[keyIndex].stripes*vk->keylength;
782 AfKey = crypt_safe_alloc(AFEKSize);
788 log_dbg("Using hash %s for AF in key slot %d, %d stripes",
789 hdr->hashSpec, keyIndex, hdr->keyblock[keyIndex].stripes);
790 r = AF_split(vk->key,AfKey,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
794 log_dbg("Updating key slot %d [0x%04x] area on device %s.", keyIndex,
795 hdr->keyblock[keyIndex].keyMaterialOffset << 9, device);
796 /* Encryption via dm */
797 r = LUKS_encrypt_to_storage(AfKey,
802 hdr->keyblock[keyIndex].keyMaterialOffset,
805 log_err(ctx, _("Failed to write to key storage.\n"));
809 /* Mark the key as active in phdr */
810 r = LUKS_keyslot_set(hdr, (int)keyIndex, 1);
814 r = LUKS_write_phdr(device, hdr, ctx);
820 crypt_safe_free(AfKey);
821 crypt_free_volume_key(derived_key);
825 /* Check whether a volume key is invalid. */
826 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
827 const struct volume_key *vk)
829 char checkHashBuf[LUKS_DIGESTSIZE];
831 if (PBKDF2_HMAC(hdr->hashSpec, vk->key, vk->keylength,
832 hdr->mkDigestSalt, LUKS_SALTSIZE,
833 hdr->mkDigestIterations, checkHashBuf,
834 LUKS_DIGESTSIZE) < 0)
837 if (memcmp(checkHashBuf, hdr->mkDigest, LUKS_DIGESTSIZE))
843 /* Try to open a particular key slot */
844 static int LUKS_open_key(const char *device,
845 unsigned int keyIndex,
846 const char *password,
848 struct luks_phdr *hdr,
849 struct volume_key *vk,
850 struct crypt_device *ctx)
852 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex);
853 struct volume_key *derived_key;
858 log_dbg("Trying to open key slot %d [%s].", keyIndex,
861 if (ki < CRYPT_SLOT_ACTIVE)
864 derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
868 assert(vk->keylength == hdr->keyBytes);
869 AFEKSize = hdr->keyblock[keyIndex].stripes*vk->keylength;
870 AfKey = crypt_safe_alloc(AFEKSize);
874 r = PBKDF2_HMAC(hdr->hashSpec, password,passwordLen,
875 hdr->keyblock[keyIndex].passwordSalt,LUKS_SALTSIZE,
876 hdr->keyblock[keyIndex].passwordIterations,
877 derived_key->key, hdr->keyBytes);
881 log_dbg("Reading key slot %d area.", keyIndex);
882 r = LUKS_decrypt_from_storage(AfKey,
887 hdr->keyblock[keyIndex].keyMaterialOffset,
890 log_err(ctx, _("Failed to read from key storage.\n"));
894 r = AF_merge(AfKey,vk->key,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
898 r = LUKS_verify_volume_key(hdr, vk);
900 log_verbose(ctx, _("Key slot %d unlocked.\n"), keyIndex);
902 crypt_safe_free(AfKey);
903 crypt_free_volume_key(derived_key);
907 int LUKS_open_key_with_hdr(const char *device,
909 const char *password,
911 struct luks_phdr *hdr,
912 struct volume_key **vk,
913 struct crypt_device *ctx)
918 *vk = crypt_alloc_volume_key(hdr->keyBytes, NULL);
921 r = LUKS_open_key(device, keyIndex, password, passwordLen, hdr, *vk, ctx);
922 return (r < 0) ? r : keyIndex;
925 for(i = 0; i < LUKS_NUMKEYS; i++) {
926 r = LUKS_open_key(device, i, password, passwordLen, hdr, *vk, ctx);
930 /* Do not retry for errors that are no -EPERM or -ENOENT,
931 former meaning password wrong, latter key slot inactive */
932 if ((r != -EPERM) && (r != -ENOENT))
935 /* Warning, early returns above */
936 log_err(ctx, _("No key available with this passphrase.\n"));
940 int LUKS_del_key(const char *device,
941 unsigned int keyIndex,
942 struct luks_phdr *hdr,
943 struct crypt_device *ctx)
945 unsigned int startOffset, endOffset, stripesLen;
948 r = LUKS_read_phdr(device, hdr, 1, 0, ctx);
952 r = LUKS_keyslot_set(hdr, keyIndex, 0);
954 log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d.\n"),
955 keyIndex, LUKS_NUMKEYS - 1);
959 /* secure deletion of key material */
960 startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
961 stripesLen = hdr->keyBytes * hdr->keyblock[keyIndex].stripes;
962 endOffset = startOffset + div_round_up(stripesLen, SECTOR_SIZE);
964 r = crypt_wipe(device, startOffset * SECTOR_SIZE,
965 (endOffset - startOffset) * SECTOR_SIZE,
968 log_err(ctx, _("Cannot wipe device %s.\n"), device);
972 /* Wipe keyslot info */
973 memset(&hdr->keyblock[keyIndex].passwordSalt, 0, LUKS_SALTSIZE);
974 hdr->keyblock[keyIndex].passwordIterations = 0;
976 r = LUKS_write_phdr(device, hdr, ctx);
981 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot)
985 if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
986 return CRYPT_SLOT_INVALID;
988 if (hdr->keyblock[keyslot].active == LUKS_KEY_DISABLED)
989 return CRYPT_SLOT_INACTIVE;
991 if (hdr->keyblock[keyslot].active != LUKS_KEY_ENABLED)
992 return CRYPT_SLOT_INVALID;
994 for(i = 0; i < LUKS_NUMKEYS; i++)
995 if(i != keyslot && hdr->keyblock[i].active == LUKS_KEY_ENABLED)
996 return CRYPT_SLOT_ACTIVE;
998 return CRYPT_SLOT_ACTIVE_LAST;
1001 int LUKS_keyslot_find_empty(struct luks_phdr *hdr)
1005 for (i = 0; i < LUKS_NUMKEYS; i++)
1006 if(hdr->keyblock[i].active == LUKS_KEY_DISABLED)
1009 if (i == LUKS_NUMKEYS)
1015 int LUKS_keyslot_active_count(struct luks_phdr *hdr)
1019 for (i = 0; i < LUKS_NUMKEYS; i++)
1020 if(hdr->keyblock[i].active == LUKS_KEY_ENABLED)
1026 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
1028 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);
1030 if (ki == CRYPT_SLOT_INVALID)
1033 hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISABLED;
1034 log_dbg("Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");
1038 int LUKS1_activate(struct crypt_device *cd,
1040 struct volume_key *vk,
1044 char *dm_cipher = NULL;
1045 enum devcheck device_check;
1046 struct crypt_dm_active_device dmd = {
1048 .uuid = crypt_get_uuid(cd),
1051 .data_device = crypt_get_device_name(cd),
1055 .offset = crypt_get_data_offset(cd),
1060 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
1061 device_check = DEV_SHARED;
1063 device_check = DEV_EXCL;
1065 r = device_check_and_adjust(cd, dmd.data_device, device_check,
1066 &dmd.size, &dmd.u.crypt.offset,
1071 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
1075 dmd.u.crypt.cipher = dm_cipher;
1076 r = dm_create_device(name, CRYPT_LUKS1, &dmd, 0);