keymanage.c 31 KB
Newer Older
1
/*
2
 * LUKS - Linux Unified Key Setup
3 4
 *
 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
Milan Broz's avatar
Milan Broz committed
5
 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
6
 * Copyright (C) 2013-2014, Milan Broz
7
 *
8 9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
10 11
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
12 13 14 15 16 17 18 19
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 22
 */

23 24
#include <sys/types.h>
#include <sys/stat.h>
25
#include <netinet/in.h>
26 27 28
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
29
#include <stdio.h>
30 31
#include <stdlib.h>
#include <string.h>
32
#include <ctype.h>
33
#include <assert.h>
34
#include <uuid/uuid.h>
35 36 37

#include "luks.h"
#include "af.h"
38
#include "internal.h"
39

40 41
/* Get size of struct luks_phdr with all keyslots material space */
static size_t LUKS_device_sectors(size_t keyLen)
42
{
43
	size_t keyslot_sectors, sector;
44 45
	int i;

46
	keyslot_sectors = AF_split_sectors(keyLen, LUKS_STRIPES);
47
	sector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
48 49

	for (i = 0; i < LUKS_NUMKEYS; i++) {
50
		sector = size_round_up(sector, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
51 52 53 54 55 56
		sector += keyslot_sectors;
	}

	return sector;
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70
int LUKS_keyslot_area(struct luks_phdr *hdr,
	int keyslot,
	uint64_t *offset,
	uint64_t *length)
{
	if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
		return -EINVAL;

	*offset = hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZE;
	*length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE;

	return 0;
}

Milan Broz's avatar
Milan Broz committed
71
static int LUKS_check_device_size(struct crypt_device *ctx, size_t keyLength)
72
{
Milan Broz's avatar
Milan Broz committed
73
	struct device *device = crypt_metadata_device(ctx);
74
	uint64_t dev_sectors, hdr_sectors;
75

76 77 78 79
	if (!keyLength)
		return -EINVAL;

	if(device_size(device, &dev_sectors)) {
Milan Broz's avatar
Milan Broz committed
80
		log_dbg("Cannot get device size for device %s.", device_path(device));
81 82 83
		return -EIO;
	}

84 85
	dev_sectors >>= SECTOR_SHIFT;
	hdr_sectors = LUKS_device_sectors(keyLength);
86
	log_dbg("Key length %zu, device size %" PRIu64 " sectors, header size %"
87 88 89
		PRIu64 " sectors.",keyLength, dev_sectors, hdr_sectors);

	if (hdr_sectors > dev_sectors) {
90 91
		log_err(ctx, _("Device %s is too small. (LUKS requires at least %" PRIu64 " bytes.)\n"),
			device_path(device), hdr_sectors * SECTOR_SIZE);
92 93 94 95
		return -EINVAL;
	}

	return 0;
96 97
}

98 99 100 101 102 103
/* Check keyslot to prevent access outside of header and keyslot area */
static int LUKS_check_keyslot_size(const struct luks_phdr *phdr, unsigned int keyIndex)
{
	uint32_t secs_per_stripes;

	/* First sectors is the header itself */
104
	if (phdr->keyblock[keyIndex].keyMaterialOffset * SECTOR_SIZE < sizeof(*phdr)) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
		log_dbg("Invalid offset %u in keyslot %u.",
			phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex);
		return 1;
	}

	/* Ignore following check for detached header where offset can be zero. */
	if (phdr->payloadOffset == 0)
		return 0;

	if (phdr->payloadOffset <= phdr->keyblock[keyIndex].keyMaterialOffset) {
		log_dbg("Invalid offset %u in keyslot %u (beyond data area offset %u).",
			phdr->keyblock[keyIndex].keyMaterialOffset, keyIndex,
			phdr->payloadOffset);
		return 1;
	}

121
	secs_per_stripes = AF_split_sectors(phdr->keyBytes, phdr->keyblock[keyIndex].stripes);
122 123 124 125 126 127 128 129 130 131 132 133 134 135

	if (phdr->payloadOffset < (phdr->keyblock[keyIndex].keyMaterialOffset + secs_per_stripes)) {
		log_dbg("Invalid keyslot size %u (offset %u, stripes %u) in "
			"keyslot %u (beyond data area offset %u).",
			secs_per_stripes,
			phdr->keyblock[keyIndex].keyMaterialOffset,
			phdr->keyblock[keyIndex].stripes,
			keyIndex, phdr->payloadOffset);
		return 1;
	}

	return 0;
}

136
static const char *dbg_slot_state(crypt_keyslot_info ki)
Milan Broz's avatar
Milan Broz committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
	switch(ki) {
	case CRYPT_SLOT_INACTIVE:
		return "INACTIVE";
	case CRYPT_SLOT_ACTIVE:
		return "ACTIVE";
	case CRYPT_SLOT_ACTIVE_LAST:
		return "ACTIVE_LAST";
	case CRYPT_SLOT_INVALID:
	default:
		return "INVALID";
	}
}

151
int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
152
{
Milan Broz's avatar
Milan Broz committed
153
	struct device *device = crypt_metadata_device(ctx);
154
	struct luks_phdr hdr;
155
	int r = 0, devfd = -1;
156
	ssize_t hdr_size;
157
	ssize_t buffer_size;
158
	char *buffer = NULL;
159

160
	r = LUKS_read_phdr(&hdr, 1, 0, ctx);
161 162 163
	if (r)
		return r;

164
	hdr_size = LUKS_device_sectors(hdr.keyBytes) << SECTOR_SHIFT;
165 166
	buffer_size = size_round_up(hdr_size, crypt_getpagesize());

167
	buffer = crypt_safe_alloc(buffer_size);
168
	if (!buffer || hdr_size < LUKS_ALIGN_KEYSLOTS || hdr_size > buffer_size) {
169 170 171 172
		r = -ENOMEM;
		goto out;
	}

173
	log_dbg("Storing backup of header (%zu bytes) and keyslot area (%zu bytes).",
174
		sizeof(hdr), hdr_size - LUKS_ALIGN_KEYSLOTS);
175

176
	log_dbg("Output backup file size: %zu bytes.", buffer_size);
177

178
	devfd = device_open(device, O_RDONLY);
179
	if(devfd == -1) {
Milan Broz's avatar
Milan Broz committed
180
		log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device_path(device));
181 182 183 184
		r = -EINVAL;
		goto out;
	}

185
	if (read_blockwise(devfd, device_block_size(device), buffer, hdr_size) < hdr_size) {
186 187 188 189 190 191
		r = -EIO;
		goto out;
	}
	close(devfd);

	/* Wipe unused area, so backup cannot contain old signatures */
192 193
	if (hdr.keyblock[0].keyMaterialOffset * SECTOR_SIZE == LUKS_ALIGN_KEYSLOTS)
		memset(buffer + sizeof(hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(hdr));
194

195 196 197 198 199 200
	devfd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
	if (devfd == -1) {
		if (errno == EEXIST)
			log_err(ctx, _("Requested header backup file %s already exists.\n"), backup_file);
		else
			log_err(ctx, _("Cannot create header backup file %s.\n"), backup_file);
201 202 203
		r = -EINVAL;
		goto out;
	}
204
	if (write(devfd, buffer, buffer_size) < buffer_size) {
205 206 207 208 209 210 211 212 213 214
		log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
		r = -EIO;
		goto out;
	}
	close(devfd);

	r = 0;
out:
	if (devfd != -1)
		close(devfd);
215
	memset(&hdr, 0, sizeof(hdr));
216
	crypt_safe_free(buffer);
217 218 219 220 221 222 223 224
	return r;
}

int LUKS_hdr_restore(
	const char *backup_file,
	struct luks_phdr *hdr,
	struct crypt_device *ctx)
{
Milan Broz's avatar
Milan Broz committed
225
	struct device *device = crypt_metadata_device(ctx);
226
	int r = 0, devfd = -1, diff_uuid = 0;
227
	ssize_t buffer_size = 0;
228 229 230
	char *buffer = NULL, msg[200];
	struct luks_phdr hdr_file;

Milan Broz's avatar
Milan Broz committed
231
	r = LUKS_read_phdr_backup(backup_file, &hdr_file, 0, ctx);
232 233 234
	if (r == -ENOENT)
		return r;

Milan Broz's avatar
Milan Broz committed
235
	if (!r)
236
		buffer_size = LUKS_device_sectors(hdr_file.keyBytes) << SECTOR_SHIFT;
237 238

	if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
Milan Broz's avatar
Milan Broz committed
239
		log_err(ctx, _("Backup file doesn't contain valid LUKS header.\n"));
240 241 242 243
		r = -EINVAL;
		goto out;
	}

244
	buffer = crypt_safe_alloc(buffer_size);
245 246 247 248 249 250
	if (!buffer) {
		r = -ENOMEM;
		goto out;
	}

	devfd = open(backup_file, O_RDONLY);
251
	if (devfd == -1) {
252 253 254 255 256
		log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
		r = -EINVAL;
		goto out;
	}

257
	if (read(devfd, buffer, buffer_size) < buffer_size) {
258 259 260 261 262 263
		log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
		r = -EIO;
		goto out;
	}
	close(devfd);

Milan Broz's avatar
Milan Broz committed
264
	r = LUKS_read_phdr(hdr, 0, 0, ctx);
265
	if (r == 0) {
Milan Broz's avatar
Milan Broz committed
266
		log_dbg("Device %s already contains LUKS header, checking UUID and offset.", device_path(device));
267 268 269 270 271 272 273 274 275 276
		if(hdr->payloadOffset != hdr_file.payloadOffset ||
		   hdr->keyBytes != hdr_file.keyBytes) {
			log_err(ctx, _("Data offset or key size differs on device and backup, restore failed.\n"));
			r = -EINVAL;
			goto out;
		}
		if (memcmp(hdr->uuid, hdr_file.uuid, UUID_STRING_L))
			diff_uuid = 1;
	}

Milan Broz's avatar
Milan Broz committed
277
	if (snprintf(msg, sizeof(msg), _("Device %s %s%s"), device_path(device),
278 279 280 281 282 283 284 285 286 287 288 289
		 r ? _("does not contain LUKS header. Replacing header can destroy data on that device.") :
		     _("already contains LUKS header. Replacing header will destroy existing keyslots."),
		     diff_uuid ? _("\nWARNING: real device header has different UUID than backup!") : "") < 0) {
		r = -ENOMEM;
		goto out;
	}

	if (!crypt_confirm(ctx, msg)) {
		r = -EINVAL;
		goto out;
	}

290
	log_dbg("Storing backup of header (%zu bytes) and keyslot area (%zu bytes) to device %s.",
Milan Broz's avatar
Milan Broz committed
291
		sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(device));
292

293
	devfd = device_open(device, O_RDWR);
294
	if (devfd == -1) {
295 296 297 298 299
		if (errno == EACCES)
			log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
				device_path(device));
		else
			log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
300 301
		r = -EINVAL;
		goto out;
302 303
	}

Milan Broz's avatar
Milan Broz committed
304
	if (write_blockwise(devfd, device_block_size(device), buffer, buffer_size) < buffer_size) {
305
		r = -EIO;
306 307 308 309 310
		goto out;
	}
	close(devfd);

	/* Be sure to reload new data */
Milan Broz's avatar
Milan Broz committed
311
	r = LUKS_read_phdr(hdr, 1, 0, ctx);
312 313 314
out:
	if (devfd != -1)
		close(devfd);
315
	crypt_safe_free(buffer);
316 317 318
	return r;
}

319
/* This routine should do some just basic recovery for known problems. */
Milan Broz's avatar
Milan Broz committed
320
static int _keyslot_repair(struct luks_phdr *phdr, struct crypt_device *ctx)
321 322 323 324 325
{
	struct luks_phdr temp_phdr;
	const unsigned char *sector = (const unsigned char*)phdr;
	struct volume_key *vk;
	uint64_t PBKDF2_per_sec = 1;
326
	int i, bad, r, need_write = 0;
327

328
	if (phdr->keyBytes != 16 && phdr->keyBytes != 32 && phdr->keyBytes != 64) {
329 330 331
		log_err(ctx, _("Non standard key size, manual repair required.\n"));
		return -EINVAL;
	}
332 333 334 335 336 337
	/* cryptsetup 1.0 did not align to 4k, cannot repair this one */
	if (phdr->keyblock[0].keyMaterialOffset < (LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE)) {
		log_err(ctx, _("Non standard keyslots alignment, manual repair required.\n"));
		return -EINVAL;
	}

338 339
	vk = crypt_alloc_volume_key(phdr->keyBytes, NULL);

340 341 342 343 344 345
	log_verbose(ctx, _("Repairing keyslots.\n"));

	log_dbg("Generating second header with the same parameters for check.");
	/* cipherName, cipherMode, hashSpec, uuid are already null terminated */
	/* payloadOffset - cannot check */
	r = LUKS_generate_phdr(&temp_phdr, vk, phdr->cipherName, phdr->cipherMode,
346 347 348
			       phdr->hashSpec,phdr->uuid, LUKS_STRIPES,
			       phdr->payloadOffset, 0,
			       1, &PBKDF2_per_sec,
Milan Broz's avatar
Milan Broz committed
349
			       1, ctx);
350 351 352 353
	if (r < 0) {
		log_err(ctx, _("Repair failed."));
		goto out;
	}
354 355

	for(i = 0; i < LUKS_NUMKEYS; ++i) {
356 357 358 359 360
		if (phdr->keyblock[i].active == LUKS_KEY_ENABLED)  {
			log_dbg("Skipping repair for active keyslot %i.", i);
			continue;
		}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
		bad = 0;
		if (phdr->keyblock[i].keyMaterialOffset != temp_phdr.keyblock[i].keyMaterialOffset) {
			log_err(ctx, _("Keyslot %i: offset repaired (%u -> %u).\n"), i,
				(unsigned)phdr->keyblock[i].keyMaterialOffset,
				(unsigned)temp_phdr.keyblock[i].keyMaterialOffset);
			phdr->keyblock[i].keyMaterialOffset = temp_phdr.keyblock[i].keyMaterialOffset;
			bad = 1;
		}

		if (phdr->keyblock[i].stripes != temp_phdr.keyblock[i].stripes) {
			log_err(ctx, _("Keyslot %i: stripes repaired (%u -> %u).\n"), i,
				(unsigned)phdr->keyblock[i].stripes,
				(unsigned)temp_phdr.keyblock[i].stripes);
			phdr->keyblock[i].stripes = temp_phdr.keyblock[i].stripes;
			bad = 1;
		}

378 379 380 381 382 383 384 385 386 387 388
		/* Known case - MSDOS partition table signature */
		if (i == 6 && sector[0x1fe] == 0x55 && sector[0x1ff] == 0xaa) {
			log_err(ctx, _("Keyslot %i: bogus partition signature.\n"), i);
			bad = 1;
		}

		if(bad) {
			log_err(ctx, _("Keyslot %i: salt wiped.\n"), i);
			phdr->keyblock[i].active = LUKS_KEY_DISABLED;
			memset(&phdr->keyblock[i].passwordSalt, 0x00, LUKS_SALTSIZE);
			phdr->keyblock[i].passwordIterations = 0;
389
		}
390 391 392

		if (bad)
			need_write = 1;
393 394
	}

395 396
	if (need_write) {
		log_verbose(ctx, _("Writing LUKS header to disk.\n"));
Milan Broz's avatar
Milan Broz committed
397
		r = LUKS_write_phdr(phdr, ctx);
398 399
	}
out:
400 401
	crypt_free_volume_key(vk);
	memset(&temp_phdr, 0, sizeof(temp_phdr));
402
	return r;
403 404
}

405 406 407
static int _check_and_convert_hdr(const char *device,
				  struct luks_phdr *hdr,
				  int require_luks_device,
408
				  int repair,
409 410 411 412 413 414 415
				  struct crypt_device *ctx)
{
	int r = 0;
	unsigned int i;
	char luksMagic[] = LUKS_MAGIC;

	if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */
416 417
		log_dbg("LUKS header not detected.");
		if (require_luks_device)
418
			log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
419
		return -EINVAL;
420
	} else if((hdr->version = ntohs(hdr->version)) != 1) {	/* Convert every uint16/32_t item from network byte order */
421
		log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
422 423 424 425
		return -EINVAL;
	}

	hdr->hashSpec[LUKS_HASHSPEC_L - 1] = '\0';
426
	if (crypt_hmac_size(hdr->hashSpec) < LUKS_DIGESTSIZE) {
427
		log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hdr->hashSpec);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		return -EINVAL;
	}

	/* Header detected */
	hdr->payloadOffset      = ntohl(hdr->payloadOffset);
	hdr->keyBytes           = ntohl(hdr->keyBytes);
	hdr->mkDigestIterations = ntohl(hdr->mkDigestIterations);

	for(i = 0; i < LUKS_NUMKEYS; ++i) {
		hdr->keyblock[i].active             = ntohl(hdr->keyblock[i].active);
		hdr->keyblock[i].passwordIterations = ntohl(hdr->keyblock[i].passwordIterations);
		hdr->keyblock[i].keyMaterialOffset  = ntohl(hdr->keyblock[i].keyMaterialOffset);
		hdr->keyblock[i].stripes            = ntohl(hdr->keyblock[i].stripes);
		if (LUKS_check_keyslot_size(hdr, i)) {
			log_err(ctx, _("LUKS keyslot %u is invalid.\n"), i);
			r = -EINVAL;
444
		}
445 446 447 448 449 450
	}

	/* Avoid unterminated strings */
	hdr->cipherName[LUKS_CIPHERNAME_L - 1] = '\0';
	hdr->cipherMode[LUKS_CIPHERMODE_L - 1] = '\0';
	hdr->uuid[UUID_STRING_L - 1] = '\0';
451

452 453
	if (repair) {
		if (r == -EINVAL)
Milan Broz's avatar
Milan Broz committed
454
			r = _keyslot_repair(hdr, ctx);
455 456
		else
			log_verbose(ctx, _("No known problems detected for LUKS header.\n"));
457 458
	}

459 460 461
	return r;
}

462
static void _to_lower(char *str, unsigned max_len)
463
{
464
	for(; *str && max_len; str++, max_len--)
465 466 467 468 469 470 471 472
		if (isupper(*str))
			*str = tolower(*str);
}

static void LUKS_fix_header_compatible(struct luks_phdr *header)
{
	/* Old cryptsetup expects "sha1", gcrypt allows case insensistive names,
	 * so always convert hash to lower case in header */
473
	_to_lower(header->hashSpec, LUKS_HASHSPEC_L);
474 475
}

476 477 478 479 480
int LUKS_read_phdr_backup(const char *backup_file,
			  struct luks_phdr *hdr,
			  int require_luks_device,
			  struct crypt_device *ctx)
{
481
	ssize_t hdr_size = sizeof(struct luks_phdr);
482 483 484
	int devfd = 0, r = 0;

	log_dbg("Reading LUKS header of size %d from backup file %s",
485
		(int)hdr_size, backup_file);
486 487 488

	devfd = open(backup_file, O_RDONLY);
	if(-1 == devfd) {
489 490
		log_err(ctx, _("Cannot open header backup file %s.\n"), backup_file);
		return -ENOENT;
491 492
	}

493
	if (read(devfd, hdr, hdr_size) < hdr_size)
494
		r = -EIO;
495 496
	else {
		LUKS_fix_header_compatible(hdr);
497 498
		r = _check_and_convert_hdr(backup_file, hdr,
					   require_luks_device, 0, ctx);
499
	}
500 501 502 503 504

	close(devfd);
	return r;
}

Milan Broz's avatar
Milan Broz committed
505
int LUKS_read_phdr(struct luks_phdr *hdr,
506
		   int require_luks_device,
507
		   int repair,
508 509
		   struct crypt_device *ctx)
{
Milan Broz's avatar
Milan Broz committed
510
	struct device *device = crypt_metadata_device(ctx);
511
	ssize_t hdr_size = sizeof(struct luks_phdr);
512 513
	int devfd = 0, r = 0;

514 515 516
	/* LUKS header starts at offset 0, first keyslot on LUKS_ALIGN_KEYSLOTS */
	assert(sizeof(struct luks_phdr) <= LUKS_ALIGN_KEYSLOTS);

517 518 519
	/* Stripes count cannot be changed without additional code fixes yet */
	assert(LUKS_STRIPES == 4000);

520 521 522
	if (repair && !require_luks_device)
		return -EINVAL;

523
	log_dbg("Reading LUKS header of size %zu from device %s",
Milan Broz's avatar
Milan Broz committed
524
		hdr_size, device_path(device));
525

526
	devfd = device_open(device, O_RDONLY);
Milan Broz's avatar
Milan Broz committed
527 528
	if (devfd == -1) {
		log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
529 530 531
		return -EINVAL;
	}

Milan Broz's avatar
Milan Broz committed
532
	if (read_blockwise(devfd, device_block_size(device), hdr, hdr_size) < hdr_size)
533 534
		r = -EIO;
	else
Milan Broz's avatar
Milan Broz committed
535
		r = _check_and_convert_hdr(device_path(device), hdr, require_luks_device,
536
					   repair, ctx);
537

538
	if (!r)
Milan Broz's avatar
Milan Broz committed
539
		r = LUKS_check_device_size(ctx, hdr->keyBytes);
540

541 542 543 544
	close(devfd);
	return r;
}

Milan Broz's avatar
Milan Broz committed
545
int LUKS_write_phdr(struct luks_phdr *hdr,
546
		    struct crypt_device *ctx)
547
{
Milan Broz's avatar
Milan Broz committed
548
	struct device *device = crypt_metadata_device(ctx);
549
	ssize_t hdr_size = sizeof(struct luks_phdr);
550 551
	int devfd = 0;
	unsigned int i;
552 553
	struct luks_phdr convHdr;
	int r;
554

555
	log_dbg("Updating LUKS header of size %zu on device %s",
Milan Broz's avatar
Milan Broz committed
556
		sizeof(struct luks_phdr), device_path(device));
557

Milan Broz's avatar
Milan Broz committed
558
	r = LUKS_check_device_size(ctx, hdr->keyBytes);
559 560
	if (r)
		return r;
561

562
	devfd = device_open(device, O_RDWR);
563
	if(-1 == devfd) {
564 565 566 567 568
		if (errno == EACCES)
			log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
				device_path(device));
		else
			log_err(ctx, _("Cannot open device %s.\n"), device_path(device));
569 570 571
		return -EINVAL;
	}

572
	memcpy(&convHdr, hdr, hdr_size);
573
	memset(&convHdr._padding, 0, sizeof(convHdr._padding));
574 575 576 577 578 579 580 581 582 583 584 585 586

	/* Convert every uint16/32_t item to network byte order */
	convHdr.version            = htons(hdr->version);
	convHdr.payloadOffset      = htonl(hdr->payloadOffset);
	convHdr.keyBytes           = htonl(hdr->keyBytes);
	convHdr.mkDigestIterations = htonl(hdr->mkDigestIterations);
	for(i = 0; i < LUKS_NUMKEYS; ++i) {
		convHdr.keyblock[i].active             = htonl(hdr->keyblock[i].active);
		convHdr.keyblock[i].passwordIterations = htonl(hdr->keyblock[i].passwordIterations);
		convHdr.keyblock[i].keyMaterialOffset  = htonl(hdr->keyblock[i].keyMaterialOffset);
		convHdr.keyblock[i].stripes            = htonl(hdr->keyblock[i].stripes);
	}

Milan Broz's avatar
Milan Broz committed
587
	r = write_blockwise(devfd, device_block_size(device), &convHdr, hdr_size) < hdr_size ? -EIO : 0;
588
	if (r)
Milan Broz's avatar
Milan Broz committed
589
		log_err(ctx, _("Error during update of LUKS header on device %s.\n"), device_path(device));
590
	close(devfd);
591

592 593
	/* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
	if (!r) {
Milan Broz's avatar
Milan Broz committed
594
		r = LUKS_read_phdr(hdr, 1, 0, ctx);
595
		if (r)
Milan Broz's avatar
Milan Broz committed
596 597
			log_err(ctx, _("Error re-reading LUKS header after update on device %s.\n"),
				device_path(device));
598 599
	}

600
	return r;
601
}
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
/* Check that kernel supports requested cipher by decryption of one sector */
static int LUKS_check_cipher(struct luks_phdr *hdr, struct crypt_device *ctx)
{
	int r;
	struct volume_key *empty_key;
	char buf[SECTOR_SIZE];

	log_dbg("Checking if cipher %s-%s is usable.", hdr->cipherName, hdr->cipherMode);

	empty_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
	if (!empty_key)
		return -ENOMEM;

	r = LUKS_decrypt_from_storage(buf, sizeof(buf),
				      hdr->cipherName, hdr->cipherMode,
				      empty_key, 0, ctx);

	crypt_free_volume_key(empty_key);
	memset(buf, 0, sizeof(buf));
	return r;
}

625
int LUKS_generate_phdr(struct luks_phdr *header,
626
		       const struct volume_key *vk,
627 628 629
		       const char *cipherName, const char *cipherMode, const char *hashSpec,
		       const char *uuid, unsigned int stripes,
		       unsigned int alignPayload,
630
		       unsigned int alignOffset,
631 632
		       uint32_t iteration_time_ms,
		       uint64_t *PBKDF2_per_sec,
Milan Broz's avatar
Milan Broz committed
633
		       int detached_metadata_device,
634
		       struct crypt_device *ctx)
635
{
636
	unsigned int i = 0, hdr_sectors = LUKS_device_sectors(vk->keylength);
637
	size_t blocksPerStripeSet, currentSector;
638 639
	int r;
	uuid_t partitionUuid;
640
	char luksMagic[] = LUKS_MAGIC;
641

642
	/* For separate metadata device allow zero alignment */
Milan Broz's avatar
Milan Broz committed
643
	if (alignPayload == 0 && !detached_metadata_device)
644
		alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
645

646 647 648 649 650 651 652
	if (alignPayload && detached_metadata_device && alignPayload < hdr_sectors) {
		log_err(ctx, _("Data offset for detached LUKS header must be "
			       "either 0 or higher than header size (%d sectors).\n"),
			       hdr_sectors);
		return -EINVAL;
	}

653
	if (crypt_hmac_size(hashSpec) < LUKS_DIGESTSIZE) {
654 655 656 657
		log_err(ctx, _("Requested LUKS hash %s is not supported.\n"), hashSpec);
		return -EINVAL;
	}

658 659 660 661 662 663 664
	if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
		log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
		return -EINVAL;
	}
	if (!uuid)
		uuid_generate(partitionUuid);

665 666 667 668 669 670 671
	memset(header,0,sizeof(struct luks_phdr));

	/* Set Magic */
	memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
	header->version=1;
	strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L);
	strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L);
672
	strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L);
673

674
	header->keyBytes=vk->keylength;
675

676 677
	LUKS_fix_header_compatible(header);

678 679 680 681
	r = LUKS_check_cipher(header, ctx);
	if (r < 0)
		return r;

682 683 684 685
	log_dbg("Generating LUKS header version %d using hash %s, %s, %s, MK %d bytes",
		header->version, header->hashSpec ,header->cipherName, header->cipherMode,
		header->keyBytes);

686
	r = crypt_random_get(ctx, header->mkDigestSalt, LUKS_SALTSIZE, CRYPT_RND_SALT);
687
	if(r < 0) {
688
		log_err(ctx, _("Cannot create LUKS header: reading random salt failed.\n"));
689 690
		return r;
	}
691

Milan Broz's avatar
Milan Broz committed
692 693 694 695 696
	r = crypt_benchmark_kdf(ctx, "pbkdf2", header->hashSpec,
				"foo", 3, "bar", 3, PBKDF2_per_sec);
	if (r < 0) {
		log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"),
			header->hashSpec);
697
		return r;
Milan Broz's avatar
Milan Broz committed
698
	}
699

700
	/* Compute master key digest */
701 702 703 704
	iteration_time_ms /= 8;
	header->mkDigestIterations = at_least((uint32_t)(*PBKDF2_per_sec/1024) * iteration_time_ms,
					      LUKS_MKD_ITERATIONS_MIN);

705 706 707 708
	r = crypt_pbkdf("pbkdf2", header->hashSpec, vk->key,vk->keylength,
			header->mkDigestSalt, LUKS_SALTSIZE,
			header->mkDigest,LUKS_DIGESTSIZE,
			header->mkDigestIterations);
709
	if(r < 0) {
710
		log_err(ctx, _("Cannot create LUKS header: header digest failed (using hash %s).\n"),
711
			header->hashSpec);
712 713
		return r;
	}
714

715
	currentSector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
716
	blocksPerStripeSet = AF_split_sectors(vk->keylength, stripes);
717 718 719 720
	for(i = 0; i < LUKS_NUMKEYS; ++i) {
		header->keyblock[i].active = LUKS_KEY_DISABLED;
		header->keyblock[i].keyMaterialOffset = currentSector;
		header->keyblock[i].stripes = stripes;
721
		currentSector = size_round_up(currentSector + blocksPerStripeSet,
722
						LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
723 724
	}

Milan Broz's avatar
Milan Broz committed
725
	if (detached_metadata_device) {
726 727 728 729
		/* for separate metadata device use alignPayload directly */
		header->payloadOffset = alignPayload;
	} else {
		/* alignOffset - offset from natural device alignment provided by topology info */
730
		currentSector = size_round_up(currentSector, alignPayload);
731 732
		header->payloadOffset = currentSector + alignOffset;
	}
733 734 735

        uuid_unparse(partitionUuid, header->uuid);

736 737
	log_dbg("Data offset %d, UUID %s, digest iterations %" PRIu32,
		header->payloadOffset, header->uuid, header->mkDigestIterations);
738

739 740 741
	return 0;
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
int LUKS_hdr_uuid_set(
	struct luks_phdr *hdr,
	const char *uuid,
	struct crypt_device *ctx)
{
	uuid_t partitionUuid;

	if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
		log_err(ctx, _("Wrong LUKS UUID format provided.\n"));
		return -EINVAL;
	}
	if (!uuid)
		uuid_generate(partitionUuid);

	uuid_unparse(partitionUuid, hdr->uuid);

Milan Broz's avatar
Milan Broz committed
758
	return LUKS_write_phdr(hdr, ctx);
759 760
}

Milan Broz's avatar
Milan Broz committed
761
int LUKS_set_key(unsigned int keyIndex,
762
		 const char *password, size_t passwordLen,
763
		 struct luks_phdr *hdr, struct volume_key *vk,
764 765 766
		 uint32_t iteration_time_ms,
		 uint64_t *PBKDF2_per_sec,
		 struct crypt_device *ctx)
767
{
768
	struct volume_key *derived_key;
769
	char *AfKey = NULL;
770
	size_t AFEKSize;
771
	uint64_t PBKDF2_temp;
772
	int r;
773

774
	if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) {
775
		log_err(ctx, _("Key slot %d active, purge first.\n"), keyIndex);
776 777
		return -EINVAL;
	}
778

779 780
	/* LUKS keyslot has always at least 4000 stripes accoding to specification */
	if(hdr->keyblock[keyIndex].stripes < 4000) {
781 782
	        log_err(ctx, _("Key slot %d material includes too few stripes. Header manipulation?\n"),
			keyIndex);
783 784
	         return -EINVAL;
	}
785

786 787
	log_dbg("Calculating data for key slot %d", keyIndex);

Milan Broz's avatar
Milan Broz committed
788 789 790 791 792
	r = crypt_benchmark_kdf(ctx, "pbkdf2", hdr->hashSpec,
				"foo", 3, "bar", 3, PBKDF2_per_sec);
	if (r < 0) {
		log_err(ctx, _("Not compatible PBKDF2 options (using hash algorithm %s).\n"),
			hdr->hashSpec);
793
		return r;
Milan Broz's avatar
Milan Broz committed
794
	}
795 796 797 798 799 800 801 802 803 804 805

	/*
	 * Avoid floating point operation
	 * Final iteration count is at least LUKS_SLOT_ITERATIONS_MIN
	 */
	PBKDF2_temp = (*PBKDF2_per_sec / 2) * (uint64_t)iteration_time_ms;
	PBKDF2_temp /= 1024;
	if (PBKDF2_temp > UINT32_MAX)
		PBKDF2_temp = UINT32_MAX;
	hdr->keyblock[keyIndex].passwordIterations = at_least((uint32_t)PBKDF2_temp,
							      LUKS_SLOT_ITERATIONS_MIN);
806 807 808

	log_dbg("Key slot %d use %d password iterations.", keyIndex, hdr->keyblock[keyIndex].passwordIterations);

809 810 811 812
	derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
	if (!derived_key)
		return -ENOMEM;

813
	r = crypt_random_get(ctx, hdr->keyblock[keyIndex].passwordSalt,
814
		       LUKS_SALTSIZE, CRYPT_RND_SALT);
815
	if (r < 0)
816
		goto out;
817

818 819 820 821
	r = crypt_pbkdf("pbkdf2", hdr->hashSpec, password, passwordLen,
			hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
			derived_key->key, hdr->keyBytes,
			hdr->keyblock[keyIndex].passwordIterations);
822 823
	if (r < 0)
		goto out;
824

825
	/*
Milan Broz's avatar
Milan Broz committed
826
	 * AF splitting, the masterkey stored in vk->key is split to AfKey
827
	 */
828
	assert(vk->keylength == hdr->keyBytes);
829
	AFEKSize = AF_split_sectors(vk->keylength, hdr->keyblock[keyIndex].stripes) * SECTOR_SIZE;
830 831 832 833 834
	AfKey = crypt_safe_alloc(AFEKSize);
	if (!AfKey) {
		r = -ENOMEM;
		goto out;
	}
835

836 837
	log_dbg("Using hash %s for AF in key slot %d, %d stripes",
		hdr->hashSpec, keyIndex, hdr->keyblock[keyIndex].stripes);
838
	r = AF_split(vk->key,AfKey,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
839 840
	if (r < 0)
		goto out;
841

Milan Broz's avatar
Milan Broz committed
842 843
	log_dbg("Updating key slot %d [0x%04x] area.", keyIndex,
		hdr->keyblock[keyIndex].keyMaterialOffset << 9);
844 845 846
	/* Encryption via dm */
	r = LUKS_encrypt_to_storage(AfKey,
				    AFEKSize,
847
				    hdr->cipherName, hdr->cipherMode,
848
				    derived_key,
849 850
				    hdr->keyblock[keyIndex].keyMaterialOffset,
				    ctx);
851
	if (r < 0)
852 853 854
		goto out;

	/* Mark the key as active in phdr */
855
	r = LUKS_keyslot_set(hdr, (int)keyIndex, 1);
856 857
	if (r < 0)
		goto out;
858

Milan Broz's avatar
Milan Broz committed
859
	r = LUKS_write_phdr(hdr, ctx);
860 861
	if (r < 0)
		goto out;
862 863 864

	r = 0;
out:
865
	crypt_safe_free(AfKey);
866
	crypt_free_volume_key(derived_key);
867 868 869
	return r;
}

870 871 872
/* Check whether a volume key is invalid. */
int LUKS_verify_volume_key(const struct luks_phdr *hdr,
			   const struct volume_key *vk)
873 874 875
{
	char checkHashBuf[LUKS_DIGESTSIZE];

876
	if (crypt_pbkdf("pbkdf2", hdr->hashSpec, vk->key, vk->keylength,
877
			hdr->mkDigestSalt, LUKS_SALTSIZE,
878 879
			checkHashBuf, LUKS_DIGESTSIZE,
			hdr->mkDigestIterations) < 0)
880 881 882 883 884 885 886 887
		return -EINVAL;

	if (memcmp(checkHashBuf, hdr->mkDigest, LUKS_DIGESTSIZE))
		return -EPERM;

	return 0;
}

888
/* Try to open a particular key slot */
Milan Broz's avatar
Milan Broz committed
889
static int LUKS_open_key(unsigned int keyIndex,
890 891 892
		  const char *password,
		  size_t passwordLen,
		  struct luks_phdr *hdr,
893
		  struct volume_key *vk,
894
		  struct crypt_device *ctx)
895
{
896
	crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyIndex);
897
	struct volume_key *derived_key;
898 899 900
	char *AfKey;
	size_t AFEKSize;
	int r;
901

Milan Broz's avatar
Milan Broz committed
902 903
	log_dbg("Trying to open key slot %d [%s].", keyIndex,
		dbg_slot_state(ki));
904

905
	if (ki < CRYPT_SLOT_ACTIVE)
906 907
		return -ENOENT;

908 909 910
	derived_key = crypt_alloc_volume_key(hdr->keyBytes, NULL);
	if (!derived_key)
		return -ENOMEM;
911

912
	assert(vk->keylength == hdr->keyBytes);
913
	AFEKSize = AF_split_sectors(vk->keylength, hdr->keyblock[keyIndex].stripes) * SECTOR_SIZE;
914
	AfKey = crypt_safe_alloc(AFEKSize);
915 916 917 918
	if (!AfKey) {
		r = -ENOMEM;
		goto out;
	}
919

920 921 922 923
	r = crypt_pbkdf("pbkdf2", hdr->hashSpec, password, passwordLen,
			hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE,
			derived_key->key, hdr->keyBytes,
			hdr->keyblock[keyIndex].passwordIterations);
924 925
	if (r < 0)
		goto out;
926

927
	log_dbg("Reading key slot %d area.", keyIndex);
928 929
	r = LUKS_decrypt_from_storage(AfKey,
				      AFEKSize,
930
				      hdr->cipherName, hdr->cipherMode,
931
				      derived_key,
932 933
				      hdr->keyblock[keyIndex].keyMaterialOffset,
				      ctx);
934
	if (r < 0)
935
		goto out;
936

937
	r = AF_merge(AfKey,vk->key,vk->keylength,hdr->keyblock[keyIndex].stripes,hdr->hashSpec);
938 939
	if (r < 0)
		goto out;
940

941
	r = LUKS_verify_volume_key(hdr, vk);
942
	if (!r)
943
		log_verbose(ctx, _("Key slot %d unlocked.\n"), keyIndex);
944
out:
945
	crypt_safe_free(AfKey);
946
	crypt_free_volume_key(derived_key);
947 948 949
	return r;
}

Milan Broz's avatar
Milan Broz committed
950
int LUKS_open_key_with_hdr(int keyIndex,
951 952 953
			   const char *password,
			   size_t passwordLen,
			   struct luks_phdr *hdr,
954
			   struct volume_key **vk,
955
			   struct crypt_device *ctx)
956
{
957
	unsigned int i;
958 959
	int r;

960
	*vk = crypt_alloc_volume_key(hdr->keyBytes, NULL);
961

962
	if (keyIndex >= 0) {
Milan Broz's avatar
Milan Broz committed
963
		r = LUKS_open_key(keyIndex, password, passwordLen, hdr, *vk, ctx);
964 965
		return (r < 0) ? r : keyIndex;
	}
966

967
	for(i = 0; i < LUKS_NUMKEYS; i++) {
Milan Broz's avatar
Milan Broz committed
968
		r = LUKS_open_key(i, password, passwordLen, hdr, *vk, ctx);
969 970 971
		if(r == 0)
			return i;

972 973
		/* Do not retry for errors that are no -EPERM or -ENOENT,
		   former meaning password wrong, latter key slot inactive */
974
		if ((r != -EPERM) && (r != -ENOENT))
975
			return r;
976 977
	}
	/* Warning, early returns above */
978
	log_err(ctx, _("No key available with this passphrase.\n"));
979 980 981
	return -EPERM;
}

Milan Broz's avatar
Milan Broz committed
982
int LUKS_del_key(unsigned int keyIndex,
983 984
		 struct luks_phdr *hdr,
		 struct crypt_device *ctx)
985
{
Milan Broz's avatar
Milan Broz committed
986
	struct device *device = crypt_metadata_device(ctx);
987
	unsigned int startOffset, endOffset;
988
	int r;
989

Milan Broz's avatar
Milan Broz committed
990
	r = LUKS_read_phdr(hdr, 1, 0, ctx);
991 992 993
	if (r)
		return r;

994
	r = LUKS_keyslot_set(hdr, keyIndex, 0);
995
	if (r) {
996 997
		log_err(ctx, _("Key slot %d is invalid, please select keyslot between 0 and %d.\n"),
			keyIndex, LUKS_NUMKEYS - 1);
998
		return r;
999 1000
	}

1001
	/* secure deletion of key material */
1002
	startOffset = hdr->keyblock[keyIndex].keyMaterialOffset;
1003
	endOffset = startOffset + AF_split_sectors(hdr->keyBytes, hdr->keyblock[keyIndex].stripes);
1004

1005 1006 1007
	r = crypt_wipe(device, startOffset * SECTOR_SIZE,
		       (endOffset - startOffset) * SECTOR_SIZE,
		       CRYPT_WIPE_DISK, 0);
1008
	if (r) {
1009 1010 1011 1012 1013 1014 1015
		if (r == -EACCES) {
			log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
				device_path(device));
			r = -EINVAL;
		} else
			log_err(ctx, _("Cannot wipe device %s.\n"),
				device_path(device));
1016 1017 1018
		return r;
	}

1019 1020 1021 1022
	/* Wipe keyslot info */
	memset(&hdr->keyblock[keyIndex].passwordSalt, 0, LUKS_SALTSIZE);
	hdr->keyblock[keyIndex].passwordIterations = 0;

Milan Broz's avatar
Milan Broz committed
1023
	r = LUKS_write_phdr(hdr, ctx);
1024

1025 1026 1027
	return r;
}

1028
crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot)
1029
{
1030
	int i;
1031

1032
	if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
1033
		return CRYPT_SLOT_INVALID;
1034

1035
	if (hdr->keyblock[keyslot].active == LUKS_KEY_DISABLED)
1036
		return CRYPT_SLOT_INACTIVE;
1037 1038

	if (hdr->keyblock[keyslot].active != LUKS_KEY_ENABLED)
1039
		return CRYPT_SLOT_INVALID;
1040 1041 1042

	for(i = 0; i < LUKS_NUMKEYS; i++)
		if(i != keyslot && hdr->keyblock[i].active == LUKS_KEY_ENABLED)
1043
			return CRYPT_SLOT_ACTIVE;
1044

1045
	return CRYPT_SLOT_ACTIVE_LAST;
1046 1047
}

1048
int LUKS_keyslot_find_empty(struct luks_phdr *hdr)
1049
{
1050 1051 1052 1053 1054 1055 1056
	int i;

	for (i = 0; i < LUKS_NUMKEYS; i++)
		if(hdr->keyblock[i].active == LUKS_KEY_DISABLED)
			break;

	if (i == LUKS_NUMKEYS)
1057 1058
		return -EINVAL;

1059
	return i;
1060 1061
}

1062
int LUKS_keyslot_active_count(struct luks_phdr *hdr)
1063
{
1064
	int i, num = 0;
1065

1066 1067 1068
	for (i = 0; i < LUKS_NUMKEYS; i++)
		if(hdr->keyblock[i].active == LUKS_KEY_ENABLED)
			num++;
1069

1070 1071 1072 1073 1074 1075 1076
	return num;
}

int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable)
{
	crypt_keyslot_info ki = LUKS_keyslot_info(hdr, keyslot);

1077
	if (ki == CRYPT_SLOT_INVALID)
1078
		return -EINVAL;
1079

1080 1081 1082
	hdr->keyblock[keyslot].active = enable ? LUKS_KEY_ENABLED : LUKS_KEY_DISABLED;
	log_dbg("Key slot %d was %s in LUKS header.", keyslot, enable ? "enabled" : "disabled");
	return 0;
1083
}
1084 1085 1086 1087 1088 1089 1090 1091

int LUKS1_activate(struct crypt_device *cd,
		   const char *name,
		   struct volume_key *vk,
		   uint32_t flags)
{
	int r;
	char *dm_cipher = NULL;
1092
	enum devcheck device_check;
1093
	struct crypt_dm_active_device dmd = {
1094
		.target = DM_CRYPT,
1095
		.uuid   = crypt_get_uuid(cd),
1096
		.flags  = flags,
1097
		.size   = 0,
Milan Broz's avatar
Milan Broz committed
1098
		.data_device = crypt_data_device(cd),
1099 1100 1101 1102 1103 1104
		.u.crypt = {
			.cipher = NULL,
			.vk     = vk,
			.offset = crypt_get_data_offset(cd),
			.iv_offset = 0,
		}
1105 1106
	};

1107 1108 1109 1110 1111
	if (dmd.flags & CRYPT_ACTIVATE_SHARED)
		device_check = DEV_SHARED;
	else
		device_check = DEV_EXCL;

Milan Broz's avatar
Milan Broz committed
1112 1113
	r = device_block_adjust(cd, dmd.data_device, device_check,
				 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
1114 1115 1116 1117 1118 1119 1120
	if (r)
		return r;

	r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
	if (r < 0)
		return -ENOMEM;

1121
	dmd.u.crypt.cipher = dm_cipher;
Milan Broz's avatar
Milan Broz committed
1122
	r = dm_create_device(cd, name, CRYPT_LUKS1, &dmd, 0);
1123 1124 1125 1126

	free(dm_cipher);
	return r;
}