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 <netinet/in.h>
31 #include <uuid/uuid.h>
38 #define div_round_up(a,b) ({ \
39 typeof(a) __a = (a); \
40 typeof(b) __b = (b); \
41 (__a - 1) / __b + 1; \
44 static inline int round_up_modulo(int x, int m) {
45 return div_round_up(x, m) * m;
48 static const char *dbg_slot_state(crypt_keyslot_info ki)
51 case CRYPT_SLOT_INACTIVE:
53 case CRYPT_SLOT_ACTIVE:
55 case CRYPT_SLOT_ACTIVE_LAST:
57 case CRYPT_SLOT_INVALID:
64 const char *backup_file,
66 struct luks_phdr *hdr,
67 struct crypt_device *ctx)
69 int r = 0, devfd = -1;
74 if(stat(backup_file, &st) == 0) {
75 log_err(ctx, _("Requested file %s already exist.\n"), backup_file);
79 r = LUKS_read_phdr(device, hdr, 1, ctx);
83 buffer_size = hdr->payloadOffset << SECTOR_SHIFT;
84 buffer = crypt_safe_alloc(buffer_size);
85 if (!buffer || buffer_size < LUKS_ALIGN_KEYSLOTS) {
90 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes).",
91 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS);
93 devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
95 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
100 if(read_blockwise(devfd, buffer, buffer_size) < buffer_size) {
106 /* Wipe unused area, so backup cannot contain old signatures */
107 memset(buffer + sizeof(*hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(*hdr));
109 devfd = creat(backup_file, S_IRUSR);
114 if(write(devfd, buffer, buffer_size) < buffer_size) {
115 log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
125 crypt_safe_free(buffer);
129 int LUKS_hdr_restore(
130 const char *backup_file,
132 struct luks_phdr *hdr,
133 struct crypt_device *ctx)
135 int r = 0, devfd = -1, diff_uuid = 0;
137 char *buffer = NULL, msg[200];
139 struct luks_phdr hdr_file;
141 if(stat(backup_file, &st) < 0) {
142 log_err(ctx, _("Backup file %s doesn't exist.\n"), backup_file);
146 r = LUKS_read_phdr_backup(backup_file, device, &hdr_file, 0, ctx);
147 buffer_size = hdr_file.payloadOffset << SECTOR_SHIFT;
149 if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
150 log_err(ctx, _("Backup file do not contain valid LUKS header.\n"));
155 buffer = crypt_safe_alloc(buffer_size);
161 devfd = open(backup_file, O_RDONLY);
163 log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
168 if(read(devfd, buffer, buffer_size) < buffer_size) {
169 log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
175 r = LUKS_read_phdr(device, hdr, 0, ctx);
177 log_dbg("Device %s already contains LUKS header, checking UUID and offset.", device);
178 if(hdr->payloadOffset != hdr_file.payloadOffset ||
179 hdr->keyBytes != hdr_file.keyBytes) {
180 log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.\n"));
184 if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L))
188 if (snprintf(msg, sizeof(msg), _("Device %s %s%s"), device,
189 r ? _("does not contain LUKS header. Replacing header can destroy data on that device.") :
190 _("already contains LUKS header. Replacing header will destroy existing keyslots."),
191 diff_uuid ? _("\nWARNING: real device header has different UUID than backup!") : "") < 0) {
196 if (!crypt_confirm(ctx, msg)) {
201 log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes) to device %s.",
202 sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device);
204 devfd = open(device, O_WRONLY | O_DIRECT | O_SYNC);
206 log_err(ctx, _("Cannot open device %s.\n"), device);
211 if(write_blockwise(devfd, buffer, buffer_size) < buffer_size) {
217 /* Be sure to reload new data */
218 r = LUKS_read_phdr(device, hdr, 0, ctx);
222 crypt_safe_free(buffer);
226 static int _check_and_convert_hdr(const char *device,
227 struct luks_phdr *hdr,
228 int require_luks_device,
229 struct crypt_device *ctx)
233 char luksMagic[] = LUKS_MAGIC;
235 if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */
236 log_dbg("LUKS header not detected.");
237 if (require_luks_device)
238 log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
240 set_error(_("Device %s is not a valid LUKS device."), device);
242 } else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */
243 log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
245 } else if (PBKDF2_HMAC_ready(hdr->hashSpec) < 0) {
246 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hdr->hashSpec);
249 hdr->payloadOffset = ntohl(hdr->payloadOffset);
250 hdr->keyBytes = ntohl(hdr->keyBytes);
251 hdr->mkDigestIterations = ntohl(hdr->mkDigestIterations);
253 for(i = 0; i < LUKS_NUMKEYS; ++i) {
254 hdr->keyblock[i].active = ntohl(hdr->keyblock[i].active);
255 hdr->keyblock[i].passwordIterations = ntohl(hdr->keyblock[i].passwordIterations);
256 hdr->keyblock[i].keyMaterialOffset = ntohl(hdr->keyblock[i].keyMaterialOffset);
257 hdr->keyblock[i].stripes = ntohl(hdr->keyblock[i].stripes);
264 static void _to_lower(char *str, unsigned max_len)
266 for(; *str && max_len; str++, max_len--)
268 *str = tolower(*str);
271 static void LUKS_fix_header_compatible(struct luks_phdr *header)
273 /* Old cryptsetup expects "sha1", gcrypt allows case insensistive names,
274 * so always convert hash to lower case in header */
275 _to_lower(header->hashSpec, LUKS_HASHSPEC_L);
278 int LUKS_read_phdr_backup(const char *backup_file,
280 struct luks_phdr *hdr,
281 int require_luks_device,
282 struct crypt_device *ctx)
284 ssize_t hdr_size = sizeof(struct luks_phdr);
285 int devfd = 0, r = 0;
287 log_dbg("Reading LUKS header of size %d from backup file %s",
288 (int)hdr_size, backup_file);
290 devfd = open(backup_file, O_RDONLY);
292 log_err(ctx, _("Cannot open file %s.\n"), device);
296 if (read(devfd, hdr, hdr_size) < hdr_size)
299 LUKS_fix_header_compatible(hdr);
300 r = _check_and_convert_hdr(backup_file, hdr, require_luks_device, ctx);
307 int LUKS_read_phdr(const char *device,
308 struct luks_phdr *hdr,
309 int require_luks_device,
310 struct crypt_device *ctx)
312 ssize_t hdr_size = sizeof(struct luks_phdr);
313 int devfd = 0, r = 0;
315 log_dbg("Reading LUKS header of size %d from device %s",
318 devfd = open(device,O_RDONLY | O_DIRECT | O_SYNC);
320 log_err(ctx, _("Cannot open device %s.\n"), device);
324 if (read_blockwise(devfd, hdr, hdr_size) < hdr_size)
327 r = _check_and_convert_hdr(device, hdr, require_luks_device, ctx);
333 int LUKS_write_phdr(const char *device,
334 struct luks_phdr *hdr,
335 struct crypt_device *ctx)
337 ssize_t hdr_size = sizeof(struct luks_phdr);
340 struct luks_phdr convHdr;
343 log_dbg("Updating LUKS header of size %d on device %s",
344 sizeof(struct luks_phdr), device);
346 devfd = open(device,O_RDWR | O_DIRECT | O_SYNC);
348 log_err(ctx, _("Cannot open device %s.\n"), device);
352 memcpy(&convHdr, hdr, hdr_size);
353 memset(&convHdr._padding, 0, sizeof(convHdr._padding));
355 /* Convert every uint16/32_t item to network byte order */
356 convHdr.version = htons(hdr->version);
357 convHdr.payloadOffset = htonl(hdr->payloadOffset);
358 convHdr.keyBytes = htonl(hdr->keyBytes);
359 convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations);
360 for(i = 0; i < LUKS_NUMKEYS; ++i) {
361 convHdr.keyblock[i].active = htonl(hdr->keyblock[i].active);
362 convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].passwordIterations);
363 convHdr.keyblock[i].keyMaterialOffset = htonl(hdr->keyblock[i].keyMaterialOffset);
364 convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].stripes);
367 r = write_blockwise(devfd, &convHdr, hdr_size) < hdr_size ? -EIO : 0;
369 log_err(ctx, _("Error during update of LUKS header on device %s.\n"), device);
372 /* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
374 r = LUKS_read_phdr(device, hdr, 1, ctx);
376 log_err(ctx, _("Error re-reading LUKS header after update on device %s.\n"), device);
382 static int LUKS_PBKDF2_performance_check(const char *hashSpec,
383 uint64_t *PBKDF2_per_sec,
384 struct crypt_device *ctx)
386 if (!*PBKDF2_per_sec) {
387 if (PBKDF2_performance_check(hashSpec, PBKDF2_per_sec) < 0) {
388 log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"), hashSpec);
391 log_dbg("PBKDF2: %" PRIu64 " iterations per second using hash %s.", *PBKDF2_per_sec, hashSpec);
397 int LUKS_generate_phdr(struct luks_phdr *header,
398 const struct volume_key *vk,
399 const char *cipherName, const char *cipherMode, const char *hashSpec,
400 const char *uuid, unsigned int stripes,
401 unsigned int alignPayload,
402 unsigned int alignOffset,
403 uint32_t iteration_time_ms,
404 uint64_t *PBKDF2_per_sec,
405 const char *metadata_device,
406 struct crypt_device *ctx)
409 unsigned int blocksPerStripeSet = div_round_up(vk->keylength*stripes,SECTOR_SIZE);
411 uuid_t partitionUuid;
413 char luksMagic[] = LUKS_MAGIC;
415 /* For separate metadata device allow zero alignment */
416 if (alignPayload == 0 && !metadata_device)
417 alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
419 if (PBKDF2_HMAC_ready(hashSpec) < 0) {
420 log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hashSpec);
424 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
425 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
429 uuid_generate(partitionUuid);
431 memset(header,0,sizeof(struct luks_phdr));
434 memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
436 strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L);
437 strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L);
438 strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L);
440 header->keyBytes=vk->keylength;
442 LUKS_fix_header_compatible(header);
444 log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
445 header->version, header->hashSpec ,header->cipherName, header->cipherMode,
448 r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_NORMAL);
450 log_err(ctx, _("Cannot create LUKS header: reading random salt failed.\n"));
454 if ((r = LUKS_PBKDF2_performance_check(header->hashSpec, PBKDF2_per_sec, ctx)))
457 /* Compute master key digest */
458 iteration_time_ms /= 8;
459 header->mkDigestIterations = at_least((uint32_t)(*PBKDF2_per_sec/1024) * iteration_time_ms,
460 LUKS_MKD_ITERATIONS_MIN);
462 r = PBKDF2_HMAC(header->hashSpec,vk->key,vk->keylength,
463 header->mkDigestSalt,LUKS_SALTSIZE,
464 header->mkDigestIterations,
465 header->mkDigest,LUKS_DIGESTSIZE);
467 log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s).\n"),
472 currentSector = round_up_modulo(LUKS_PHDR_SIZE, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
473 for(i = 0; i < LUKS_NUMKEYS; ++i) {
474 header->keyblock[i].active = LUKS_KEY_DISABLED;
475 header->keyblock[i].keyMaterialOffset = currentSector;
476 header->keyblock[i].stripes = stripes;
477 currentSector = round_up_modulo(currentSector + blocksPerStripeSet,
478 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
481 if (metadata_device) {
482 /* for separate metadata device use alignPayload directly */
483 header->payloadOffset = alignPayload;
485 /* alignOffset - offset from natural device alignment provided by topology info */
486 currentSector = round_up_modulo(currentSector, alignPayload);
487 header->payloadOffset = currentSector + alignOffset;
490 uuid_unparse(partitionUuid, header->uuid);
492 log_dbg("Data offset %d, UUID %s, digest iterations %" PRIu32,
493 header->payloadOffset, header->uuid, header->mkDigestIterations);
498 int LUKS_hdr_uuid_set(
500 struct luks_phdr *hdr,
502 struct crypt_device *ctx)
504 uuid_t partitionUuid;
506 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
507 log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
511 uuid_generate(partitionUuid);
513 uuid_unparse(partitionUuid, hdr->uuid);
515 return LUKS_write_phdr(device, hdr, ctx);
518 int LUKS_set_key(const char *device, unsigned int keyIndex,
519 const char *password, size_t passwordLen,
520 struct luks_phdr *hdr, struct volume_key *vk,
521 uint32_t iteration_time_ms,
522 uint64_t *PBKDF2_per_sec,
523 struct crypt_device *ctx)
525 struct volume_key *derived_key;
527 unsigned int AFEKSize;
528 uint64_t PBKDF2_temp;
531 if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) {
532 log_err(ctx, _("Key slot %d active, purge first.\n"), keyIndex);
536 if(hdr->keyblock[keyIndex].stripes < LUKS_STRIPES) {
537 log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?\n"),
542 log_dbg("Calculating data for key slot %d", keyIndex);
544 if ((r = LUKS_PBKDF2_performance_check(hdr->hashSpec, PBKDF2_per_sec, ctx)))
548 * Avoid floating point operation
549 * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
551 PBKDF2_temp = (*PBKDF2_per_sec / 2) * (uint64_t)iteration_time_ms;
553 if (PBKDF2_temp > UINT32_MAX)
554 PBKDF2_temp = UINT32_MAX;
555 hdr->keyblock[keyIndex].passwordIterations = at_least((uint32_t)PBKDF2_temp,
556 LUKS_SLOT_ITERATIONS_MIN);
558 log_dbg("Key slot %d use %d password iterations.", keyIndex, hdr->keyblock[keyIndex].passwordIterations);
560 derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
564 r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt,
565 LUKS_SALTSIZE, CRYPT_RND_NORMAL);
569 r = PBKDF2_HMAC(hdr->hashSpec, password,passwordLen,
570 hdr->keyblock[keyIndex].passwordSalt,LUKS_SALTSIZE,
571 hdr->keyblock[keyIndex].passwordIterations,
572 derived_key->key, hdr->keyBytes);
577 * AF splitting, the masterkey stored in vk->key is split to AfKey
579 assert(vk->keylength == hdr->keyBytes);
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,
601 hdr->keyblock[keyIndex].keyMaterialOffset,
605 log_err(ctx, _("Failed to write to key storage.\n"));
609 /* Mark the key as active in phdr */
610 r = LUKS_keyslot_set(hdr, (int)keyIndex, 1);
614 r = LUKS_write_phdr(device, hdr, ctx);
620 crypt_safe_free(AfKey);
621 crypt_free_volume_key(derived_key);
625 /* Check whether a volume key is invalid. */
626 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
627 const struct volume_key *vk)
629 char checkHashBuf[LUKS_DIGESTSIZE];
631 if (PBKDF2_HMAC(hdr->hashSpec, vk->key, vk->keylength,
632 hdr->mkDigestSalt, LUKS_SALTSIZE,
633 hdr->mkDigestIterations, checkHashBuf,
634 LUKS_DIGESTSIZE) < 0)
637 if (memcmp(checkHashBuf, hdr->mkDigest, LUKS_DIGESTSIZE))
643 /* Try to open a particular key slot */
644 static int LUKS_open_key(const char *device,
645 unsigned int keyIndex,
646 const char *password,
648 struct luks_phdr *hdr,
649 struct volume_key *vk,
650 struct crypt_device *ctx)
652 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex);
653 struct volume_key *derived_key;
658 log_dbg("Trying to open key slot %d [%s].", keyIndex,
661 if (ki < CRYPT_SLOT_ACTIVE)
664 derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
668 assert(vk->keylength == hdr->keyBytes);
669 AFEKSize = hdr->keyblock[keyIndex].stripes*vk->keylength;
670 AfKey = crypt_safe_alloc(AFEKSize);
674 r = PBKDF2_HMAC(hdr->hashSpec, password,passwordLen,
675 hdr->keyblock[keyIndex].passwordSalt,LUKS_SALTSIZE,
676 hdr->keyblock[keyIndex].passwordIterations,
677 derived_key->key, hdr->keyBytes);
681 log_dbg("Reading key slot %d area.", keyIndex);
682 r = LUKS_decrypt_from_storage(AfKey,
687 hdr->keyblock[keyIndex].keyMaterialOffset,
690 log_err(ctx, _("Failed to read from key storage.\n"));
694 r = AF_merge(AfKey,vk->key,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
698 r = LUKS_verify_volume_key(hdr, vk);
700 log_verbose(ctx, _("Key slot %d unlocked.\n"), keyIndex);
702 crypt_safe_free(AfKey);
703 crypt_free_volume_key(derived_key);
707 int LUKS_open_key_with_hdr(const char *device,
709 const char *password,
711 struct luks_phdr *hdr,
712 struct volume_key **vk,
713 struct crypt_device *ctx)
718 *vk = crypt_alloc_volume_key(hdr->keyBytes, NULL);
721 r = LUKS_open_key(device, keyIndex, password, passwordLen, hdr, *vk, ctx);
722 return (r < 0) ? r : keyIndex;
725 for(i = 0; i < LUKS_NUMKEYS; i++) {
726 r = LUKS_open_key(device, i, password, passwordLen, hdr, *vk, ctx);
730 /* Do not retry for errors that are no -EPERM or -ENOENT,
731 former meaning password wrong, latter key slot inactive */
732 if ((r != -EPERM) && (r != -ENOENT))
735 /* Warning, early returns above */
736 log_err(ctx, _("No key available with this passphrase.\n"));
741 * Wipe patterns according to Gutmann's Paper
744 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
748 unsigned char write_modes[][3] = {
749 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
750 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
751 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
752 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
753 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
754 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
755 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
756 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
757 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
760 for(i = 0; i < buffer_size / 3; ++i) {
761 memcpy(buffer, write_modes[turn], 3);
766 static int wipe(const char *device, unsigned int from, unsigned int to)
770 unsigned int i, bufLen;
773 devfd = open(device, O_RDWR | O_DIRECT | O_SYNC);
777 bufLen = (to - from) * SECTOR_SIZE;
778 buffer = malloc(bufLen);
784 for(i = 0; i < 39; ++i) {
785 if (i < 5) crypt_random_get(NULL, buffer, bufLen,
787 else if(i >= 5 && i < 32) wipeSpecial(buffer, bufLen, i - 5);
788 else if(i >= 32 && i < 38) crypt_random_get(NULL, buffer, bufLen,
790 else if(i >= 38 && i < 39) memset(buffer, 0xFF, bufLen);
792 written = write_lseek_blockwise(devfd, buffer, bufLen,
794 if (written < 0 || written != bufLen) {
806 int LUKS_del_key(const char *device,
807 unsigned int keyIndex,
808 struct luks_phdr *hdr,
809 struct crypt_device *ctx)
811 unsigned int startOffset, endOffset, stripesLen;
814 r = LUKS_read_phdr(device, hdr, 1, ctx);
818 r = LUKS_keyslot_set(hdr, keyIndex, 0);
820 log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d.\n"),
821 keyIndex, LUKS_NUMKEYS - 1);
825 /* secure deletion of key material */
826 startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
827 stripesLen = hdr->keyBytes * hdr->keyblock[keyIndex].stripes;
828 endOffset = startOffset + div_round_up(stripesLen, SECTOR_SIZE);
830 r = wipe(device, startOffset, endOffset);
832 log_err(ctx, _("Cannot wipe device %s.\n"), device);
836 /* Wipe keyslot info */
837 memset(&hdr->keyblock[keyIndex].passwordSalt, 0, LUKS_SALTSIZE);
838 hdr->keyblock[keyIndex].passwordIterations = 0;
840 r = LUKS_write_phdr(device, hdr, ctx);
845 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot)
849 if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
850 return CRYPT_SLOT_INVALID;
852 if (hdr->keyblock[keyslot].active == LUKS_KEY_DISABLED)
853 return CRYPT_SLOT_INACTIVE;
855 if (hdr->keyblock[keyslot].active != LUKS_KEY_ENABLED)
856 return CRYPT_SLOT_INVALID;
858 for(i = 0; i < LUKS_NUMKEYS; i++)
859 if(i != keyslot && hdr->keyblock[i].active == LUKS_KEY_ENABLED)
860 return CRYPT_SLOT_ACTIVE;
862 return CRYPT_SLOT_ACTIVE_LAST;
865 int LUKS_keyslot_find_empty(struct luks_phdr *hdr)
869 for (i = 0; i < LUKS_NUMKEYS; i++)
870 if(hdr->keyblock[i].active == LUKS_KEY_DISABLED)
873 if (i == LUKS_NUMKEYS)
879 int LUKS_keyslot_active_count(struct luks_phdr *hdr)
883 for (i = 0; i < LUKS_NUMKEYS; i++)
884 if(hdr->keyblock[i].active == LUKS_KEY_ENABLED)
890 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
892 crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);
894 if (ki == CRYPT_SLOT_INVALID)
897 hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISABLED;
898 log_dbg("Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");
902 int LUKS1_activate(struct crypt_device *cd,
904 struct volume_key *vk,
908 char *dm_cipher = NULL;
909 struct crypt_dm_active_device dmd = {
910 .device = crypt_get_device_name(cd),
912 .uuid = crypt_get_uuid(cd),
914 .offset = crypt_get_data_offset(cd),
920 r = device_check_and_adjust(cd, dmd.device, DEV_EXCL,
921 &dmd.size, &dmd.offset, &flags);
925 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
929 dmd.cipher = dm_cipher;
930 r = dm_create_device(name, CRYPT_LUKS1, &dmd, 0);