2 * LUKS - Linux Unified Key Setup
4 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
24 #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 static const char *dbg_slot_state(crypt_keyslot_info ki)
52 case CRYPT_SLOT_INACTIVE:
54 case CRYPT_SLOT_ACTIVE:
56 case CRYPT_SLOT_ACTIVE_LAST:
58 case CRYPT_SLOT_INVALID:
65 const char *backup_file,
67 struct luks_phdr *hdr,
68 struct crypt_device *ctx)
70 int r = 0, devfd = -1;
75 if(stat(backup_file, &st) == 0) {
76 log_err(ctx, _("Requested file %s already exist.\n"), backup_file);
80 r = LUKS_read_phdr(device, hdr, 1, ctx);
84 buffer_size = hdr->payloadOffset << SECTOR_SHIFT;
85 buffer = crypt_safe_alloc(buffer_size);
86 if (!buffer || buffer_size < LUKS_ALIGN_KEYSLOTS) {
91 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes).",
92 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS);
94 devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
96 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
101 if(read_blockwise(devfd, buffer, buffer_size) < buffer_size) {
107 /* Wipe unused area, so backup cannot contain old signatures */
108 memset(buffer + sizeof(*hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(*hdr));
110 devfd = creat(backup_file, S_IRUSR);
115 if(write(devfd, buffer, buffer_size) < buffer_size) {
116 log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
126 crypt_safe_free(buffer);
130 int LUKS_hdr_restore(
131 const char *backup_file,
133 struct luks_phdr *hdr,
134 struct crypt_device *ctx)
136 int r = 0, devfd = -1, diff_uuid = 0;
138 char *buffer = NULL, msg[200];
140 struct luks_phdr hdr_file;
142 if(stat(backup_file, &st) < 0) {
143 log_err(ctx, _("Backup file %s doesn't exist.\n"), backup_file);
147 r = LUKS_read_phdr_backup(backup_file, device, &hdr_file, 0, ctx);
148 buffer_size = hdr_file.payloadOffset << SECTOR_SHIFT;
150 if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
151 log_err(ctx, _("Backup file do not contain valid LUKS header.\n"));
156 buffer = crypt_safe_alloc(buffer_size);
162 devfd = open(backup_file, O_RDONLY);
164 log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
169 if(read(devfd, buffer, buffer_size) < buffer_size) {
170 log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
176 r = LUKS_read_phdr(device, hdr, 0, ctx);
178 log_dbg("Device %s already contains LUKS header, checking UUID and offset.", device);
179 if(hdr->payloadOffset != hdr_file.payloadOffset ||
180 hdr->keyBytes != hdr_file.keyBytes) {
181 log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.\n"));
185 if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L))
189 if (snprintf(msg, sizeof(msg), _("Device %s %s%s"), device,
190 r ? _("does not contain LUKS header. Replacing header can destroy data on that device.") :
191 _("already contains LUKS header. Replacing header will destroy existing keyslots."),
192 diff_uuid ? _("\nWARNING: real device header has different UUID than backup!") : "") < 0) {
197 if (!crypt_confirm(ctx, msg)) {
202 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes) to device %s.",
203 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device);
205 devfd = open(device, O_WRONLY | O_DIRECT | O_SYNC);
207 log_err(ctx, _("Cannot open device %s.\n"), device);
212 if(write_blockwise(devfd, buffer, buffer_size) < buffer_size) {
218 /* Be sure to reload new data */
219 r = LUKS_read_phdr(device, hdr, 0, ctx);
223 crypt_safe_free(buffer);
227 static int _check_and_convert_hdr(const char *device,
228 struct luks_phdr *hdr,
229 int require_luks_device,
230 struct crypt_device *ctx)
234 char luksMagic[] = LUKS_MAGIC;
236 if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */
237 log_dbg("LUKS header not detected.");
238 if (require_luks_device)
239 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
241 set_error(_("Device %s is not a valid LUKS device."), device);
243 } else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */
244 log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
246 } else if (PBKDF2_HMAC_ready(hdr->hashSpec) < 0) {
247 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hdr->hashSpec);
250 hdr->payloadOffset = ntohl(hdr->payloadOffset);
251 hdr->keyBytes = ntohl(hdr->keyBytes);
252 hdr->mkDigestIterations = ntohl(hdr->mkDigestIterations);
254 for(i = 0; i < LUKS_NUMKEYS; ++i) {
255 hdr->keyblock[i].active = ntohl(hdr->keyblock[i].active);
256 hdr->keyblock[i].passwordIterations = ntohl(hdr->keyblock[i].passwordIterations);
257 hdr->keyblock[i].keyMaterialOffset = ntohl(hdr->keyblock[i].keyMaterialOffset);
258 hdr->keyblock[i].stripes = ntohl(hdr->keyblock[i].stripes);
265 static void _to_lower(char *str, unsigned max_len)
267 for(; *str && max_len; str++, max_len--)
269 *str = tolower(*str);
272 static void LUKS_fix_header_compatible(struct luks_phdr *header)
274 /* Old cryptsetup expects "sha1", gcrypt allows case insensistive names,
275 * so always convert hash to lower case in header */
276 _to_lower(header->hashSpec, LUKS_HASHSPEC_L);
279 int LUKS_read_phdr_backup(const char *backup_file,
281 struct luks_phdr *hdr,
282 int require_luks_device,
283 struct crypt_device *ctx)
285 ssize_t hdr_size = sizeof(struct luks_phdr);
286 int devfd = 0, r = 0;
288 log_dbg("Reading LUKS header of size %d from backup file %s",
289 (int)hdr_size, backup_file);
291 devfd = open(backup_file, O_RDONLY);
293 log_err(ctx, _("Cannot open file %s.\n"), device);
297 if (read(devfd, hdr, hdr_size) < hdr_size)
300 LUKS_fix_header_compatible(hdr);
301 r = _check_and_convert_hdr(backup_file, hdr, require_luks_device, ctx);
308 int LUKS_read_phdr(const char *device,
309 struct luks_phdr *hdr,
310 int require_luks_device,
311 struct crypt_device *ctx)
313 ssize_t hdr_size = sizeof(struct luks_phdr);
314 int devfd = 0, r = 0;
317 log_dbg("Reading LUKS header of size %d from device %s",
320 devfd = open(device,O_RDONLY | O_DIRECT | O_SYNC);
322 log_err(ctx, _("Cannot open device %s.\n"), device);
326 if (read_blockwise(devfd, hdr, hdr_size) < hdr_size)
329 r = _check_and_convert_hdr(device, hdr, require_luks_device, ctx);
332 if (r == 0 && (ioctl(devfd, BLKGETSIZE64, &size) < 0 ||
333 size < (uint64_t)hdr->payloadOffset)) {
334 log_err(ctx, _("LUKS header detected but device %s is too small.\n"), device);
343 int LUKS_write_phdr(const char *device,
344 struct luks_phdr *hdr,
345 struct crypt_device *ctx)
347 ssize_t hdr_size = sizeof(struct luks_phdr);
350 struct luks_phdr convHdr;
353 log_dbg("Updating LUKS header of size %d on device %s",
354 sizeof(struct luks_phdr), device);
356 devfd = open(device,O_RDWR | O_DIRECT | O_SYNC);
358 log_err(ctx, _("Cannot open device %s.\n"), device);
362 memcpy(&convHdr, hdr, hdr_size);
363 memset(&convHdr._padding, 0, sizeof(convHdr._padding));
365 /* Convert every uint16/32_t item to network byte order */
366 convHdr.version = htons(hdr->version);
367 convHdr.payloadOffset = htonl(hdr->payloadOffset);
368 convHdr.keyBytes = htonl(hdr->keyBytes);
369 convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations);
370 for(i = 0; i < LUKS_NUMKEYS; ++i) {
371 convHdr.keyblock[i].active = htonl(hdr->keyblock[i].active);
372 convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].passwordIterations);
373 convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].keyMaterialOffset);
374 convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].stripes);
377 r = write_blockwise(devfd, &convHdr, hdr_size) < hdr_size ? -EIO : 0;
379 log_err(ctx, _("Error during update of LUKS header on device %s.\n"), device);
382 /* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
384 r = LUKS_read_phdr(device, hdr, 1, ctx);
386 log_err(ctx, _("Error re-reading LUKS header after update on device %s.\n"), device);
392 static int LUKS_PBKDF2_performance_check(const char *hashSpec,
393 uint64_t *PBKDF2_per_sec,
394 struct crypt_device *ctx)
396 if (!*PBKDF2_per_sec) {
397 if (PBKDF2_performance_check(hashSpec, PBKDF2_per_sec) < 0) {
398 log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"), hashSpec);
401 log_dbg("PBKDF2: %" PRIu64 " iterations per second using hash %s.", *PBKDF2_per_sec, hashSpec);
407 int LUKS_generate_phdr(struct luks_phdr *header,
408 const struct volume_key *vk,
409 const char *cipherName, const char *cipherMode, const char *hashSpec,
410 const char *uuid, unsigned int stripes,
411 unsigned int alignPayload,
412 unsigned int alignOffset,
413 uint32_t iteration_time_ms,
414 uint64_t *PBKDF2_per_sec,
415 struct crypt_device *ctx)
418 unsigned int blocksPerStripeSet = div_round_up(vk->keylength*stripes,SECTOR_SIZE);
420 uuid_t partitionUuid;
422 char luksMagic[] = LUKS_MAGIC;
424 if (alignPayload == 0)
425 alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
427 if (PBKDF2_HMAC_ready(hashSpec) < 0) {
428 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hashSpec);
432 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
433 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
437 uuid_generate(partitionUuid);
439 memset(header,0,sizeof(struct luks_phdr));
442 memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
444 strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L);
445 strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L);
446 strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L);
448 header->keyBytes=vk->keylength;
450 LUKS_fix_header_compatible(header);
452 log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
453 header->version, header->hashSpec ,header->cipherName, header->cipherMode,
456 r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_NORMAL);
458 log_err(ctx, _("Cannot create LUKS header: reading random salt failed.\n"));
462 if ((r = LUKS_PBKDF2_performance_check(header->hashSpec, PBKDF2_per_sec, ctx)))
465 /* Compute master key digest */
466 iteration_time_ms /= 8;
467 header->mkDigestIterations = at_least((uint32_t)(*PBKDF2_per_sec/1024) * iteration_time_ms,
468 LUKS_MKD_ITERATIONS_MIN);
470 r = PBKDF2_HMAC(header->hashSpec,vk->key,vk->keylength,
471 header->mkDigestSalt,LUKS_SALTSIZE,
472 header->mkDigestIterations,
473 header->mkDigest,LUKS_DIGESTSIZE);
475 log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s).\n"),
480 currentSector = round_up_modulo(LUKS_PHDR_SIZE, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
481 for(i = 0; i < LUKS_NUMKEYS; ++i) {
482 header->keyblock[i].active = LUKS_KEY_DISABLED;
483 header->keyblock[i].keyMaterialOffset = currentSector;
484 header->keyblock[i].stripes = stripes;
485 currentSector = round_up_modulo(currentSector + blocksPerStripeSet,
486 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
488 currentSector = round_up_modulo(currentSector, alignPayload);
490 /* alignOffset - offset from natural device alignment provided by topology info */
491 header->payloadOffset = currentSector + alignOffset;
493 uuid_unparse(partitionUuid, header->uuid);
495 log_dbg("Data offset %d, UUID %s, digest iterations %" PRIu32,
496 header->payloadOffset, header->uuid, header->mkDigestIterations);
501 int LUKS_hdr_uuid_set(
503 struct luks_phdr *hdr,
505 struct crypt_device *ctx)
507 uuid_t partitionUuid;
509 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
510 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
514 uuid_generate(partitionUuid);
516 uuid_unparse(partitionUuid, hdr->uuid);
518 return LUKS_write_phdr(device, hdr, ctx);
521 int LUKS_set_key(const char *device, unsigned int keyIndex,
522 const char *password, size_t passwordLen,
523 struct luks_phdr *hdr, struct volume_key *vk,
524 uint32_t iteration_time_ms,
525 uint64_t *PBKDF2_per_sec,
526 struct crypt_device *ctx)
528 char derivedKey[hdr->keyBytes];
530 unsigned int AFEKSize;
531 uint64_t PBKDF2_temp;
534 if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) {
535 log_err(ctx, _("Key slot %d active, purge first.\n"), keyIndex);
539 if(hdr->keyblock[keyIndex].stripes < LUKS_STRIPES) {
540 log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?\n"),
545 log_dbg("Calculating data for key slot %d", keyIndex);
547 if ((r = LUKS_PBKDF2_performance_check(hdr->hashSpec, PBKDF2_per_sec, ctx)))
551 * Avoid floating point operation
552 * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
554 PBKDF2_temp = (*PBKDF2_per_sec / 2) * (uint64_t)iteration_time_ms;
556 if (PBKDF2_temp > UINT32_MAX)
557 PBKDF2_temp = UINT32_MAX;
558 hdr->keyblock[keyIndex].passwordIterations = at_least((uint32_t)PBKDF2_temp,
559 LUKS_SLOT_ITERATIONS_MIN);
561 log_dbg("Key slot %d use %d password iterations.", keyIndex, hdr->keyblock[keyIndex].passwordIterations);
563 r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt,
564 LUKS_SALTSIZE, CRYPT_RND_NORMAL);
568 // assert((vk->keylength % TWOFISH_BLOCKSIZE) == 0); FIXME
570 r = PBKDF2_HMAC(hdr->hashSpec, password,passwordLen,
571 hdr->keyblock[keyIndex].passwordSalt,LUKS_SALTSIZE,
572 hdr->keyblock[keyIndex].passwordIterations,
573 derivedKey, hdr->keyBytes);
578 * AF splitting, the masterkey stored in vk->key is split to AfKey
580 AFEKSize = hdr->keyblock[keyIndex].stripes*vk->keylength;
581 AfKey = crypt_safe_alloc(AFEKSize);
587 log_dbg("Using hash %s for AF in key slot %d, %d stripes",
588 hdr->hashSpec, keyIndex, hdr->keyblock[keyIndex].stripes);
589 r = AF_split(vk->key,AfKey,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
593 log_dbg("Updating key slot %d [0x%04x] area on device %s.", keyIndex,
594 hdr->keyblock[keyIndex].keyMaterialOffset << 9, device);
595 /* Encryption via dm */
596 r = LUKS_encrypt_to_storage(AfKey,
602 hdr->keyblock[keyIndex].keyMaterialOffset,
606 log_err(ctx, _("Failed to write to key storage.\n"));
610 /* Mark the key as active in phdr */
611 r = LUKS_keyslot_set(hdr, (int)keyIndex, 1);
615 r = LUKS_write_phdr(device, hdr, ctx);
621 crypt_safe_free(AfKey);
622 memset(derivedKey, 0, sizeof(derivedKey));
626 /* Check whether a volume key is invalid. */
627 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
628 const struct volume_key *vk)
630 char checkHashBuf[LUKS_DIGESTSIZE];
632 if (PBKDF2_HMAC(hdr->hashSpec, vk->key, vk->keylength,
633 hdr->mkDigestSalt, LUKS_SALTSIZE,
634 hdr->mkDigestIterations, checkHashBuf,
635 LUKS_DIGESTSIZE) < 0)
638 if (memcmp(checkHashBuf, hdr->mkDigest, LUKS_DIGESTSIZE))
644 /* Try to open a particular key slot */
645 static int LUKS_open_key(const char *device,
646 unsigned int keyIndex,
647 const char *password,
649 struct luks_phdr *hdr,
650 struct volume_key *vk,
651 struct crypt_device *ctx)
653 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex);
654 char derivedKey[hdr->keyBytes];
659 log_dbg("Trying to open key slot %d [%s].", keyIndex,
662 if (ki < CRYPT_SLOT_ACTIVE)
665 // assert((vk->keylength % TWOFISH_BLOCKSIZE) == 0); FIXME
667 AFEKSize = hdr->keyblock[keyIndex].stripes*vk->keylength;
668 AfKey = crypt_safe_alloc(AFEKSize);
672 r = PBKDF2_HMAC(hdr->hashSpec, password,passwordLen,
673 hdr->keyblock[keyIndex].passwordSalt,LUKS_SALTSIZE,
674 hdr->keyblock[keyIndex].passwordIterations,
675 derivedKey, hdr->keyBytes);
679 log_dbg("Reading key slot %d area.", keyIndex);
680 r = LUKS_decrypt_from_storage(AfKey,
686 hdr->keyblock[keyIndex].keyMaterialOffset,
689 log_err(ctx, _("Failed to read from key storage.\n"));
693 r = AF_merge(AfKey,vk->key,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
697 r = LUKS_verify_volume_key(hdr, vk);
699 log_verbose(ctx, _("Key slot %d unlocked.\n"), keyIndex);
701 crypt_safe_free(AfKey);
702 memset(derivedKey, 0, sizeof(derivedKey));
706 int LUKS_open_key_with_hdr(const char *device,
708 const char *password,
710 struct luks_phdr *hdr,
711 struct volume_key **vk,
712 struct crypt_device *ctx)
717 *vk = crypt_alloc_volume_key(hdr->keyBytes, NULL);
720 r = LUKS_open_key(device, keyIndex, password, passwordLen, hdr, *vk, ctx);
721 return (r < 0) ? r : keyIndex;
724 for(i = 0; i < LUKS_NUMKEYS; i++) {
725 r = LUKS_open_key(device, i, password, passwordLen, hdr, *vk, ctx);
729 /* Do not retry for errors that are no -EPERM or -ENOENT,
730 former meaning password wrong, latter key slot inactive */
731 if ((r != -EPERM) && (r != -ENOENT))
734 /* Warning, early returns above */
735 log_err(ctx, _("No key available with this passphrase.\n"));
740 * Wipe patterns according to Gutmann's Paper
743 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
747 unsigned char write_modes[][3] = {
748 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
749 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
750 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
751 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
752 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
753 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
754 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
755 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
756 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
759 for(i = 0; i < buffer_size / 3; ++i) {
760 memcpy(buffer, write_modes[turn], 3);
765 static int wipe(const char *device, unsigned int from, unsigned int to)
769 unsigned int i, bufLen;
772 devfd = open(device, O_RDWR | O_DIRECT | O_SYNC);
776 bufLen = (to - from) * SECTOR_SIZE;
777 buffer = malloc(bufLen);
783 for(i = 0; i < 39; ++i) {
784 if (i < 5) crypt_random_get(NULL, buffer, bufLen,
786 else if(i >= 5 && i < 32) wipeSpecial(buffer, bufLen, i - 5);
787 else if(i >= 32 && i < 38) crypt_random_get(NULL, buffer, bufLen,
789 else if(i >= 38 && i < 39) memset(buffer, 0xFF, bufLen);
791 written = write_lseek_blockwise(devfd, buffer, bufLen,
793 if (written < 0 || written != bufLen) {
805 int LUKS_del_key(const char *device,
806 unsigned int keyIndex,
807 struct luks_phdr *hdr,
808 struct crypt_device *ctx)
810 unsigned int startOffset, endOffset, stripesLen;
813 r = LUKS_read_phdr(device, hdr, 1, ctx);
817 r = LUKS_keyslot_set(hdr, keyIndex, 0);
819 log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d.\n"),
820 keyIndex, LUKS_NUMKEYS - 1);
824 /* secure deletion of key material */
825 startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
826 stripesLen = hdr->keyBytes * hdr->keyblock[keyIndex].stripes;
827 endOffset = startOffset + div_round_up(stripesLen, SECTOR_SIZE);
829 r = wipe(device, startOffset, endOffset);
831 log_err(ctx, _("Cannot wipe device %s.\n"), device);
835 /* Wipe keyslot info */
836 memset(&hdr->keyblock[keyIndex].passwordSalt, 0, LUKS_SALTSIZE);
837 hdr->keyblock[keyIndex].passwordIterations = 0;
839 r = LUKS_write_phdr(device, hdr, ctx);
844 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot)
848 if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
849 return CRYPT_SLOT_INVALID;
851 if (hdr->keyblock[keyslot].active == LUKS_KEY_DISABLED)
852 return CRYPT_SLOT_INACTIVE;
854 if (hdr->keyblock[keyslot].active != LUKS_KEY_ENABLED)
855 return CRYPT_SLOT_INVALID;
857 for(i = 0; i < LUKS_NUMKEYS; i++)
858 if(i != keyslot && hdr->keyblock[i].active == LUKS_KEY_ENABLED)
859 return CRYPT_SLOT_ACTIVE;
861 return CRYPT_SLOT_ACTIVE_LAST;
864 int LUKS_keyslot_find_empty(struct luks_phdr *hdr)
868 for (i = 0; i < LUKS_NUMKEYS; i++)
869 if(hdr->keyblock[i].active == LUKS_KEY_DISABLED)
872 if (i == LUKS_NUMKEYS)
878 int LUKS_keyslot_active_count(struct luks_phdr *hdr)
882 for (i = 0; i < LUKS_NUMKEYS; i++)
883 if(hdr->keyblock[i].active == LUKS_KEY_ENABLED)
889 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
891 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);
893 if (ki == CRYPT_SLOT_INVALID)
896 hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISABLED;
897 log_dbg("Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");