qcrypto-luks: extract check and parse header
[qemu/ar7.git] / crypto / block-luks.c
blobfa799fd21d97c5b789ecd2b2dcc3ee08ab856d7d
1 /*
2 * QEMU Crypto block device encryption LUKS format
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/bswap.h"
25 #include "block-luks.h"
27 #include "crypto/hash.h"
28 #include "crypto/afsplit.h"
29 #include "crypto/pbkdf.h"
30 #include "crypto/secret.h"
31 #include "crypto/random.h"
32 #include "qemu/uuid.h"
34 #include "qemu/coroutine.h"
37 * Reference for the LUKS format implemented here is
39 * docs/on-disk-format.pdf
41 * in 'cryptsetup' package source code
43 * This file implements the 1.2.1 specification, dated
44 * Oct 16, 2011.
47 typedef struct QCryptoBlockLUKS QCryptoBlockLUKS;
48 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader;
49 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot;
52 /* The following constants are all defined by the LUKS spec */
53 #define QCRYPTO_BLOCK_LUKS_VERSION 1
55 #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6
56 #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32
57 #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32
58 #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32
59 #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20
60 #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32
61 #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40
62 #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8
63 #define QCRYPTO_BLOCK_LUKS_STRIPES 4000
64 #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000
65 #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000
66 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096
68 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
69 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
71 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
73 static const char qcrypto_block_luks_magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN] = {
74 'L', 'U', 'K', 'S', 0xBA, 0xBE
77 typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap;
78 struct QCryptoBlockLUKSNameMap {
79 const char *name;
80 int id;
83 typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap;
84 struct QCryptoBlockLUKSCipherSizeMap {
85 uint32_t key_bytes;
86 int id;
88 typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap;
89 struct QCryptoBlockLUKSCipherNameMap {
90 const char *name;
91 const QCryptoBlockLUKSCipherSizeMap *sizes;
95 static const QCryptoBlockLUKSCipherSizeMap
96 qcrypto_block_luks_cipher_size_map_aes[] = {
97 { 16, QCRYPTO_CIPHER_ALG_AES_128 },
98 { 24, QCRYPTO_CIPHER_ALG_AES_192 },
99 { 32, QCRYPTO_CIPHER_ALG_AES_256 },
100 { 0, 0 },
103 static const QCryptoBlockLUKSCipherSizeMap
104 qcrypto_block_luks_cipher_size_map_cast5[] = {
105 { 16, QCRYPTO_CIPHER_ALG_CAST5_128 },
106 { 0, 0 },
109 static const QCryptoBlockLUKSCipherSizeMap
110 qcrypto_block_luks_cipher_size_map_serpent[] = {
111 { 16, QCRYPTO_CIPHER_ALG_SERPENT_128 },
112 { 24, QCRYPTO_CIPHER_ALG_SERPENT_192 },
113 { 32, QCRYPTO_CIPHER_ALG_SERPENT_256 },
114 { 0, 0 },
117 static const QCryptoBlockLUKSCipherSizeMap
118 qcrypto_block_luks_cipher_size_map_twofish[] = {
119 { 16, QCRYPTO_CIPHER_ALG_TWOFISH_128 },
120 { 24, QCRYPTO_CIPHER_ALG_TWOFISH_192 },
121 { 32, QCRYPTO_CIPHER_ALG_TWOFISH_256 },
122 { 0, 0 },
125 static const QCryptoBlockLUKSCipherNameMap
126 qcrypto_block_luks_cipher_name_map[] = {
127 { "aes", qcrypto_block_luks_cipher_size_map_aes },
128 { "cast5", qcrypto_block_luks_cipher_size_map_cast5 },
129 { "serpent", qcrypto_block_luks_cipher_size_map_serpent },
130 { "twofish", qcrypto_block_luks_cipher_size_map_twofish },
135 * This struct is written to disk in big-endian format,
136 * but operated upon in native-endian format.
138 struct QCryptoBlockLUKSKeySlot {
139 /* state of keyslot, enabled/disable */
140 uint32_t active;
141 /* iterations for PBKDF2 */
142 uint32_t iterations;
143 /* salt for PBKDF2 */
144 uint8_t salt[QCRYPTO_BLOCK_LUKS_SALT_LEN];
145 /* start sector of key material */
146 uint32_t key_offset_sector;
147 /* number of anti-forensic stripes */
148 uint32_t stripes;
151 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot) != 48);
155 * This struct is written to disk in big-endian format,
156 * but operated upon in native-endian format.
158 struct QCryptoBlockLUKSHeader {
159 /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
160 char magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN];
162 /* LUKS version, currently 1 */
163 uint16_t version;
165 /* cipher name specification (aes, etc) */
166 char cipher_name[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN];
168 /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
169 char cipher_mode[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN];
171 /* hash specification (sha256, etc) */
172 char hash_spec[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN];
174 /* start offset of the volume data (in 512 byte sectors) */
175 uint32_t payload_offset_sector;
177 /* Number of key bytes */
178 uint32_t master_key_len;
180 /* master key checksum after PBKDF2 */
181 uint8_t master_key_digest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
183 /* salt for master key PBKDF2 */
184 uint8_t master_key_salt[QCRYPTO_BLOCK_LUKS_SALT_LEN];
186 /* iterations for master key PBKDF2 */
187 uint32_t master_key_iterations;
189 /* UUID of the partition in standard ASCII representation */
190 uint8_t uuid[QCRYPTO_BLOCK_LUKS_UUID_LEN];
192 /* key slots */
193 QCryptoBlockLUKSKeySlot key_slots[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS];
196 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader) != 592);
199 struct QCryptoBlockLUKS {
200 QCryptoBlockLUKSHeader header;
202 /* Main encryption algorithm used for encryption*/
203 QCryptoCipherAlgorithm cipher_alg;
205 /* Mode of encryption for the selected encryption algorithm */
206 QCryptoCipherMode cipher_mode;
208 /* Initialization vector generation algorithm */
209 QCryptoIVGenAlgorithm ivgen_alg;
211 /* Hash algorithm used for IV generation*/
212 QCryptoHashAlgorithm ivgen_hash_alg;
215 * Encryption algorithm used for IV generation.
216 * Usually the same as main encryption algorithm
218 QCryptoCipherAlgorithm ivgen_cipher_alg;
220 /* Hash algorithm used in pbkdf2 function */
221 QCryptoHashAlgorithm hash_alg;
225 static int qcrypto_block_luks_cipher_name_lookup(const char *name,
226 QCryptoCipherMode mode,
227 uint32_t key_bytes,
228 Error **errp)
230 const QCryptoBlockLUKSCipherNameMap *map =
231 qcrypto_block_luks_cipher_name_map;
232 size_t maplen = G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map);
233 size_t i, j;
235 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
236 key_bytes /= 2;
239 for (i = 0; i < maplen; i++) {
240 if (!g_str_equal(map[i].name, name)) {
241 continue;
243 for (j = 0; j < map[i].sizes[j].key_bytes; j++) {
244 if (map[i].sizes[j].key_bytes == key_bytes) {
245 return map[i].sizes[j].id;
250 error_setg(errp, "Algorithm %s with key size %d bytes not supported",
251 name, key_bytes);
252 return 0;
255 static const char *
256 qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg,
257 Error **errp)
259 const QCryptoBlockLUKSCipherNameMap *map =
260 qcrypto_block_luks_cipher_name_map;
261 size_t maplen = G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map);
262 size_t i, j;
263 for (i = 0; i < maplen; i++) {
264 for (j = 0; j < map[i].sizes[j].key_bytes; j++) {
265 if (map[i].sizes[j].id == alg) {
266 return map[i].name;
271 error_setg(errp, "Algorithm '%s' not supported",
272 QCryptoCipherAlgorithm_str(alg));
273 return NULL;
276 /* XXX replace with qapi_enum_parse() in future, when we can
277 * make that function emit a more friendly error message */
278 static int qcrypto_block_luks_name_lookup(const char *name,
279 const QEnumLookup *map,
280 const char *type,
281 Error **errp)
283 int ret = qapi_enum_parse(map, name, -1, NULL);
285 if (ret < 0) {
286 error_setg(errp, "%s %s not supported", type, name);
287 return 0;
289 return ret;
292 #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
293 qcrypto_block_luks_name_lookup(name, \
294 &QCryptoCipherMode_lookup, \
295 "Cipher mode", \
296 errp)
298 #define qcrypto_block_luks_hash_name_lookup(name, errp) \
299 qcrypto_block_luks_name_lookup(name, \
300 &QCryptoHashAlgorithm_lookup, \
301 "Hash algorithm", \
302 errp)
304 #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
305 qcrypto_block_luks_name_lookup(name, \
306 &QCryptoIVGenAlgorithm_lookup, \
307 "IV generator", \
308 errp)
311 static bool
312 qcrypto_block_luks_has_format(const uint8_t *buf,
313 size_t buf_size)
315 const QCryptoBlockLUKSHeader *luks_header = (const void *)buf;
317 if (buf_size >= offsetof(QCryptoBlockLUKSHeader, cipher_name) &&
318 memcmp(luks_header->magic, qcrypto_block_luks_magic,
319 QCRYPTO_BLOCK_LUKS_MAGIC_LEN) == 0 &&
320 be16_to_cpu(luks_header->version) == QCRYPTO_BLOCK_LUKS_VERSION) {
321 return true;
322 } else {
323 return false;
329 * Deal with a quirk of dm-crypt usage of ESSIV.
331 * When calculating ESSIV IVs, the cipher length used by ESSIV
332 * may be different from the cipher length used for the block
333 * encryption, becauses dm-crypt uses the hash digest length
334 * as the key size. ie, if you have AES 128 as the block cipher
335 * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
336 * the cipher since that gets a key length matching the digest
337 * size, not AES 128 with truncated digest as might be imagined
339 static QCryptoCipherAlgorithm
340 qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher,
341 QCryptoHashAlgorithm hash,
342 Error **errp)
344 size_t digestlen = qcrypto_hash_digest_len(hash);
345 size_t keylen = qcrypto_cipher_get_key_len(cipher);
346 if (digestlen == keylen) {
347 return cipher;
350 switch (cipher) {
351 case QCRYPTO_CIPHER_ALG_AES_128:
352 case QCRYPTO_CIPHER_ALG_AES_192:
353 case QCRYPTO_CIPHER_ALG_AES_256:
354 if (digestlen == qcrypto_cipher_get_key_len(
355 QCRYPTO_CIPHER_ALG_AES_128)) {
356 return QCRYPTO_CIPHER_ALG_AES_128;
357 } else if (digestlen == qcrypto_cipher_get_key_len(
358 QCRYPTO_CIPHER_ALG_AES_192)) {
359 return QCRYPTO_CIPHER_ALG_AES_192;
360 } else if (digestlen == qcrypto_cipher_get_key_len(
361 QCRYPTO_CIPHER_ALG_AES_256)) {
362 return QCRYPTO_CIPHER_ALG_AES_256;
363 } else {
364 error_setg(errp, "No AES cipher with key size %zu available",
365 digestlen);
366 return 0;
368 break;
369 case QCRYPTO_CIPHER_ALG_SERPENT_128:
370 case QCRYPTO_CIPHER_ALG_SERPENT_192:
371 case QCRYPTO_CIPHER_ALG_SERPENT_256:
372 if (digestlen == qcrypto_cipher_get_key_len(
373 QCRYPTO_CIPHER_ALG_SERPENT_128)) {
374 return QCRYPTO_CIPHER_ALG_SERPENT_128;
375 } else if (digestlen == qcrypto_cipher_get_key_len(
376 QCRYPTO_CIPHER_ALG_SERPENT_192)) {
377 return QCRYPTO_CIPHER_ALG_SERPENT_192;
378 } else if (digestlen == qcrypto_cipher_get_key_len(
379 QCRYPTO_CIPHER_ALG_SERPENT_256)) {
380 return QCRYPTO_CIPHER_ALG_SERPENT_256;
381 } else {
382 error_setg(errp, "No Serpent cipher with key size %zu available",
383 digestlen);
384 return 0;
386 break;
387 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
388 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
389 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
390 if (digestlen == qcrypto_cipher_get_key_len(
391 QCRYPTO_CIPHER_ALG_TWOFISH_128)) {
392 return QCRYPTO_CIPHER_ALG_TWOFISH_128;
393 } else if (digestlen == qcrypto_cipher_get_key_len(
394 QCRYPTO_CIPHER_ALG_TWOFISH_192)) {
395 return QCRYPTO_CIPHER_ALG_TWOFISH_192;
396 } else if (digestlen == qcrypto_cipher_get_key_len(
397 QCRYPTO_CIPHER_ALG_TWOFISH_256)) {
398 return QCRYPTO_CIPHER_ALG_TWOFISH_256;
399 } else {
400 error_setg(errp, "No Twofish cipher with key size %zu available",
401 digestlen);
402 return 0;
404 break;
405 default:
406 error_setg(errp, "Cipher %s not supported with essiv",
407 QCryptoCipherAlgorithm_str(cipher));
408 return 0;
413 * Stores the main LUKS header, taking care of endianess
415 static int
416 qcrypto_block_luks_store_header(QCryptoBlock *block,
417 QCryptoBlockWriteFunc writefunc,
418 void *opaque,
419 Error **errp)
421 const QCryptoBlockLUKS *luks = block->opaque;
422 Error *local_err = NULL;
423 size_t i;
424 g_autofree QCryptoBlockLUKSHeader *hdr_copy = NULL;
426 /* Create a copy of the header */
427 hdr_copy = g_new0(QCryptoBlockLUKSHeader, 1);
428 memcpy(hdr_copy, &luks->header, sizeof(QCryptoBlockLUKSHeader));
431 * Everything on disk uses Big Endian (tm), so flip header fields
432 * before writing them
434 cpu_to_be16s(&hdr_copy->version);
435 cpu_to_be32s(&hdr_copy->payload_offset_sector);
436 cpu_to_be32s(&hdr_copy->master_key_len);
437 cpu_to_be32s(&hdr_copy->master_key_iterations);
439 for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
440 cpu_to_be32s(&hdr_copy->key_slots[i].active);
441 cpu_to_be32s(&hdr_copy->key_slots[i].iterations);
442 cpu_to_be32s(&hdr_copy->key_slots[i].key_offset_sector);
443 cpu_to_be32s(&hdr_copy->key_slots[i].stripes);
446 /* Write out the partition header and key slot headers */
447 writefunc(block, 0, (const uint8_t *)hdr_copy, sizeof(*hdr_copy),
448 opaque, &local_err);
450 if (local_err) {
451 error_propagate(errp, local_err);
452 return -1;
454 return 0;
458 * Loads the main LUKS header,and byteswaps it to native endianess
459 * And run basic sanity checks on it
461 static int
462 qcrypto_block_luks_load_header(QCryptoBlock *block,
463 QCryptoBlockReadFunc readfunc,
464 void *opaque,
465 Error **errp)
467 ssize_t rv;
468 size_t i;
469 QCryptoBlockLUKS *luks = block->opaque;
472 * Read the entire LUKS header, minus the key material from
473 * the underlying device
475 rv = readfunc(block, 0,
476 (uint8_t *)&luks->header,
477 sizeof(luks->header),
478 opaque,
479 errp);
480 if (rv < 0) {
481 return rv;
485 * The header is always stored in big-endian format, so
486 * convert everything to native
488 be16_to_cpus(&luks->header.version);
489 be32_to_cpus(&luks->header.payload_offset_sector);
490 be32_to_cpus(&luks->header.master_key_len);
491 be32_to_cpus(&luks->header.master_key_iterations);
493 for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
494 be32_to_cpus(&luks->header.key_slots[i].active);
495 be32_to_cpus(&luks->header.key_slots[i].iterations);
496 be32_to_cpus(&luks->header.key_slots[i].key_offset_sector);
497 be32_to_cpus(&luks->header.key_slots[i].stripes);
500 return 0;
504 * Does basic sanity checks on the LUKS header
506 static int
507 qcrypto_block_luks_check_header(const QCryptoBlockLUKS *luks, Error **errp)
509 if (memcmp(luks->header.magic, qcrypto_block_luks_magic,
510 QCRYPTO_BLOCK_LUKS_MAGIC_LEN) != 0) {
511 error_setg(errp, "Volume is not in LUKS format");
512 return -1;
515 if (luks->header.version != QCRYPTO_BLOCK_LUKS_VERSION) {
516 error_setg(errp, "LUKS version %" PRIu32 " is not supported",
517 luks->header.version);
518 return -1;
520 return 0;
524 * Parses the crypto parameters that are stored in the LUKS header
527 static int
528 qcrypto_block_luks_parse_header(QCryptoBlockLUKS *luks, Error **errp)
530 g_autofree char *cipher_mode = g_strdup(luks->header.cipher_mode);
531 char *ivgen_name, *ivhash_name;
532 Error *local_err = NULL;
535 * The cipher_mode header contains a string that we have
536 * to further parse, of the format
538 * <cipher-mode>-<iv-generator>[:<iv-hash>]
540 * eg cbc-essiv:sha256, cbc-plain64
542 ivgen_name = strchr(cipher_mode, '-');
543 if (!ivgen_name) {
544 error_setg(errp, "Unexpected cipher mode string format %s",
545 luks->header.cipher_mode);
546 return -1;
548 *ivgen_name = '\0';
549 ivgen_name++;
551 ivhash_name = strchr(ivgen_name, ':');
552 if (!ivhash_name) {
553 luks->ivgen_hash_alg = 0;
554 } else {
555 *ivhash_name = '\0';
556 ivhash_name++;
558 luks->ivgen_hash_alg = qcrypto_block_luks_hash_name_lookup(ivhash_name,
559 &local_err);
560 if (local_err) {
561 error_propagate(errp, local_err);
562 return -1;
566 luks->cipher_mode = qcrypto_block_luks_cipher_mode_lookup(cipher_mode,
567 &local_err);
568 if (local_err) {
569 error_propagate(errp, local_err);
570 return -1;
573 luks->cipher_alg =
574 qcrypto_block_luks_cipher_name_lookup(luks->header.cipher_name,
575 luks->cipher_mode,
576 luks->header.master_key_len,
577 &local_err);
578 if (local_err) {
579 error_propagate(errp, local_err);
580 return -1;
583 luks->hash_alg =
584 qcrypto_block_luks_hash_name_lookup(luks->header.hash_spec,
585 &local_err);
586 if (local_err) {
587 error_propagate(errp, local_err);
588 return -1;
591 luks->ivgen_alg = qcrypto_block_luks_ivgen_name_lookup(ivgen_name,
592 &local_err);
593 if (local_err) {
594 error_propagate(errp, local_err);
595 return -1;
598 if (luks->ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
599 if (!ivhash_name) {
600 error_setg(errp, "Missing IV generator hash specification");
601 return -1;
603 luks->ivgen_cipher_alg =
604 qcrypto_block_luks_essiv_cipher(luks->cipher_alg,
605 luks->ivgen_hash_alg,
606 &local_err);
607 if (local_err) {
608 error_propagate(errp, local_err);
609 return -1;
611 } else {
614 * Note we parsed the ivhash_name earlier in the cipher_mode
615 * spec string even with plain/plain64 ivgens, but we
616 * will ignore it, since it is irrelevant for these ivgens.
617 * This is for compat with dm-crypt which will silently
618 * ignore hash names with these ivgens rather than report
619 * an error about the invalid usage
621 luks->ivgen_cipher_alg = luks->cipher_alg;
623 return 0;
627 * Given a key slot, and user password, this will attempt to unlock
628 * the master encryption key from the key slot.
630 * Returns:
631 * 0 if the key slot is disabled, or key could not be decrypted
632 * with the provided password
633 * 1 if the key slot is enabled, and key decrypted successfully
634 * with the provided password
635 * -1 if a fatal error occurred loading the key
637 static int
638 qcrypto_block_luks_load_key(QCryptoBlock *block,
639 size_t slot_idx,
640 const char *password,
641 uint8_t *masterkey,
642 QCryptoBlockReadFunc readfunc,
643 void *opaque,
644 Error **errp)
646 QCryptoBlockLUKS *luks = block->opaque;
647 const QCryptoBlockLUKSKeySlot *slot = &luks->header.key_slots[slot_idx];
648 g_autofree uint8_t *splitkey = NULL;
649 size_t splitkeylen;
650 g_autofree uint8_t *possiblekey = NULL;
651 ssize_t rv;
652 g_autoptr(QCryptoCipher) cipher = NULL;
653 uint8_t keydigest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
654 g_autoptr(QCryptoIVGen) ivgen = NULL;
655 size_t niv;
657 if (slot->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED) {
658 return 0;
661 splitkeylen = luks->header.master_key_len * slot->stripes;
662 splitkey = g_new0(uint8_t, splitkeylen);
663 possiblekey = g_new0(uint8_t, luks->header.master_key_len);
666 * The user password is used to generate a (possible)
667 * decryption key. This may or may not successfully
668 * decrypt the master key - we just blindly assume
669 * the key is correct and validate the results of
670 * decryption later.
672 if (qcrypto_pbkdf2(luks->hash_alg,
673 (const uint8_t *)password, strlen(password),
674 slot->salt, QCRYPTO_BLOCK_LUKS_SALT_LEN,
675 slot->iterations,
676 possiblekey, luks->header.master_key_len,
677 errp) < 0) {
678 return -1;
682 * We need to read the master key material from the
683 * LUKS key material header. What we're reading is
684 * not the raw master key, but rather the data after
685 * it has been passed through AFSplit and the result
686 * then encrypted.
688 rv = readfunc(block,
689 slot->key_offset_sector * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
690 splitkey, splitkeylen,
691 opaque,
692 errp);
693 if (rv < 0) {
694 return -1;
698 /* Setup the cipher/ivgen that we'll use to try to decrypt
699 * the split master key material */
700 cipher = qcrypto_cipher_new(luks->cipher_alg,
701 luks->cipher_mode,
702 possiblekey,
703 luks->header.master_key_len,
704 errp);
705 if (!cipher) {
706 return -1;
709 niv = qcrypto_cipher_get_iv_len(luks->cipher_alg,
710 luks->cipher_mode);
712 ivgen = qcrypto_ivgen_new(luks->ivgen_alg,
713 luks->ivgen_cipher_alg,
714 luks->ivgen_hash_alg,
715 possiblekey,
716 luks->header.master_key_len,
717 errp);
718 if (!ivgen) {
719 return -1;
724 * The master key needs to be decrypted in the same
725 * way that the block device payload will be decrypted
726 * later. In particular we'll be using the IV generator
727 * to reset the encryption cipher every time the master
728 * key crosses a sector boundary.
730 if (qcrypto_block_cipher_decrypt_helper(cipher,
731 niv,
732 ivgen,
733 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
735 splitkey,
736 splitkeylen,
737 errp) < 0) {
738 return -1;
742 * Now we've decrypted the split master key, join
743 * it back together to get the actual master key.
745 if (qcrypto_afsplit_decode(luks->hash_alg,
746 luks->header.master_key_len,
747 slot->stripes,
748 splitkey,
749 masterkey,
750 errp) < 0) {
751 return -1;
756 * We still don't know that the masterkey we got is valid,
757 * because we just blindly assumed the user's password
758 * was correct. This is where we now verify it. We are
759 * creating a hash of the master key using PBKDF and
760 * then comparing that to the hash stored in the key slot
761 * header
763 if (qcrypto_pbkdf2(luks->hash_alg,
764 masterkey,
765 luks->header.master_key_len,
766 luks->header.master_key_salt,
767 QCRYPTO_BLOCK_LUKS_SALT_LEN,
768 luks->header.master_key_iterations,
769 keydigest,
770 G_N_ELEMENTS(keydigest),
771 errp) < 0) {
772 return -1;
775 if (memcmp(keydigest, luks->header.master_key_digest,
776 QCRYPTO_BLOCK_LUKS_DIGEST_LEN) == 0) {
777 /* Success, we got the right master key */
778 return 1;
781 /* Fail, user's password was not valid for this key slot,
782 * tell caller to try another slot */
783 return 0;
788 * Given a user password, this will iterate over all key
789 * slots and try to unlock each active key slot using the
790 * password until it successfully obtains a master key.
792 * Returns 0 if a key was loaded, -1 if no keys could be loaded
794 static int
795 qcrypto_block_luks_find_key(QCryptoBlock *block,
796 const char *password,
797 uint8_t *masterkey,
798 QCryptoBlockReadFunc readfunc,
799 void *opaque,
800 Error **errp)
802 size_t i;
803 int rv;
805 for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
806 rv = qcrypto_block_luks_load_key(block,
808 password,
809 masterkey,
810 readfunc,
811 opaque,
812 errp);
813 if (rv < 0) {
814 goto error;
816 if (rv == 1) {
817 return 0;
821 error_setg(errp, "Invalid password, cannot unlock any keyslot");
822 error:
823 return -1;
827 static int
828 qcrypto_block_luks_open(QCryptoBlock *block,
829 QCryptoBlockOpenOptions *options,
830 const char *optprefix,
831 QCryptoBlockReadFunc readfunc,
832 void *opaque,
833 unsigned int flags,
834 size_t n_threads,
835 Error **errp)
837 QCryptoBlockLUKS *luks = NULL;
838 g_autofree uint8_t *masterkey = NULL;
839 g_autofree char *password = NULL;
841 if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) {
842 if (!options->u.luks.key_secret) {
843 error_setg(errp, "Parameter '%skey-secret' is required for cipher",
844 optprefix ? optprefix : "");
845 return -1;
847 password = qcrypto_secret_lookup_as_utf8(
848 options->u.luks.key_secret, errp);
849 if (!password) {
850 return -1;
854 luks = g_new0(QCryptoBlockLUKS, 1);
855 block->opaque = luks;
857 if (qcrypto_block_luks_load_header(block, readfunc, opaque, errp) < 0) {
858 goto fail;
861 if (qcrypto_block_luks_check_header(luks, errp) < 0) {
862 goto fail;
865 if (qcrypto_block_luks_parse_header(luks, errp) < 0) {
866 goto fail;
869 if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) {
870 /* Try to find which key slot our password is valid for
871 * and unlock the master key from that slot.
874 masterkey = g_new0(uint8_t, luks->header.master_key_len);
876 if (qcrypto_block_luks_find_key(block,
877 password,
878 masterkey,
879 readfunc, opaque,
880 errp) < 0) {
881 goto fail;
884 /* We have a valid master key now, so can setup the
885 * block device payload decryption objects
887 block->kdfhash = luks->hash_alg;
888 block->niv = qcrypto_cipher_get_iv_len(luks->cipher_alg,
889 luks->cipher_mode);
891 block->ivgen = qcrypto_ivgen_new(luks->ivgen_alg,
892 luks->ivgen_cipher_alg,
893 luks->ivgen_hash_alg,
894 masterkey,
895 luks->header.master_key_len,
896 errp);
897 if (!block->ivgen) {
898 goto fail;
901 if (qcrypto_block_init_cipher(block,
902 luks->cipher_alg,
903 luks->cipher_mode,
904 masterkey,
905 luks->header.master_key_len,
906 n_threads,
907 errp) < 0) {
908 goto fail;
912 block->sector_size = QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
913 block->payload_offset = luks->header.payload_offset_sector *
914 block->sector_size;
916 return 0;
918 fail:
919 qcrypto_block_free_cipher(block);
920 qcrypto_ivgen_free(block->ivgen);
921 g_free(luks);
922 return -1;
926 static void
927 qcrypto_block_luks_uuid_gen(uint8_t *uuidstr)
929 QemuUUID uuid;
930 qemu_uuid_generate(&uuid);
931 qemu_uuid_unparse(&uuid, (char *)uuidstr);
934 static int
935 qcrypto_block_luks_create(QCryptoBlock *block,
936 QCryptoBlockCreateOptions *options,
937 const char *optprefix,
938 QCryptoBlockInitFunc initfunc,
939 QCryptoBlockWriteFunc writefunc,
940 void *opaque,
941 Error **errp)
943 QCryptoBlockLUKS *luks;
944 QCryptoBlockCreateOptionsLUKS luks_opts;
945 Error *local_err = NULL;
946 g_autofree uint8_t *masterkey = NULL;
947 g_autofree uint8_t *slotkey = NULL;
948 g_autofree uint8_t *splitkey = NULL;
949 size_t splitkeylen = 0;
950 size_t i;
951 g_autoptr(QCryptoCipher) cipher = NULL;
952 g_autoptr(QCryptoIVGen) ivgen = NULL;
953 g_autofree char *password = NULL;
954 const char *cipher_alg;
955 const char *cipher_mode;
956 const char *ivgen_alg;
957 const char *ivgen_hash_alg = NULL;
958 const char *hash_alg;
959 g_autofree char *cipher_mode_spec = NULL;
960 uint64_t iters;
962 memcpy(&luks_opts, &options->u.luks, sizeof(luks_opts));
963 if (!luks_opts.has_iter_time) {
964 luks_opts.iter_time = 2000;
966 if (!luks_opts.has_cipher_alg) {
967 luks_opts.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256;
969 if (!luks_opts.has_cipher_mode) {
970 luks_opts.cipher_mode = QCRYPTO_CIPHER_MODE_XTS;
972 if (!luks_opts.has_ivgen_alg) {
973 luks_opts.ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64;
975 if (!luks_opts.has_hash_alg) {
976 luks_opts.hash_alg = QCRYPTO_HASH_ALG_SHA256;
978 if (luks_opts.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
979 if (!luks_opts.has_ivgen_hash_alg) {
980 luks_opts.ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256;
981 luks_opts.has_ivgen_hash_alg = true;
985 luks = g_new0(QCryptoBlockLUKS, 1);
986 block->opaque = luks;
988 luks->cipher_alg = luks_opts.cipher_alg;
989 luks->cipher_mode = luks_opts.cipher_mode;
990 luks->ivgen_alg = luks_opts.ivgen_alg;
991 luks->ivgen_hash_alg = luks_opts.ivgen_hash_alg;
992 luks->hash_alg = luks_opts.hash_alg;
995 /* Note we're allowing ivgen_hash_alg to be set even for
996 * non-essiv iv generators that don't need a hash. It will
997 * be silently ignored, for compatibility with dm-crypt */
999 if (!options->u.luks.key_secret) {
1000 error_setg(errp, "Parameter '%skey-secret' is required for cipher",
1001 optprefix ? optprefix : "");
1002 goto error;
1004 password = qcrypto_secret_lookup_as_utf8(luks_opts.key_secret, errp);
1005 if (!password) {
1006 goto error;
1010 memcpy(luks->header.magic, qcrypto_block_luks_magic,
1011 QCRYPTO_BLOCK_LUKS_MAGIC_LEN);
1013 /* We populate the header in native endianness initially and
1014 * then convert everything to big endian just before writing
1015 * it out to disk
1017 luks->header.version = QCRYPTO_BLOCK_LUKS_VERSION;
1018 qcrypto_block_luks_uuid_gen(luks->header.uuid);
1020 cipher_alg = qcrypto_block_luks_cipher_alg_lookup(luks_opts.cipher_alg,
1021 errp);
1022 if (!cipher_alg) {
1023 goto error;
1026 cipher_mode = QCryptoCipherMode_str(luks_opts.cipher_mode);
1027 ivgen_alg = QCryptoIVGenAlgorithm_str(luks_opts.ivgen_alg);
1028 if (luks_opts.has_ivgen_hash_alg) {
1029 ivgen_hash_alg = QCryptoHashAlgorithm_str(luks_opts.ivgen_hash_alg);
1030 cipher_mode_spec = g_strdup_printf("%s-%s:%s", cipher_mode, ivgen_alg,
1031 ivgen_hash_alg);
1032 } else {
1033 cipher_mode_spec = g_strdup_printf("%s-%s", cipher_mode, ivgen_alg);
1035 hash_alg = QCryptoHashAlgorithm_str(luks_opts.hash_alg);
1038 if (strlen(cipher_alg) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN) {
1039 error_setg(errp, "Cipher name '%s' is too long for LUKS header",
1040 cipher_alg);
1041 goto error;
1043 if (strlen(cipher_mode_spec) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN) {
1044 error_setg(errp, "Cipher mode '%s' is too long for LUKS header",
1045 cipher_mode_spec);
1046 goto error;
1048 if (strlen(hash_alg) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN) {
1049 error_setg(errp, "Hash name '%s' is too long for LUKS header",
1050 hash_alg);
1051 goto error;
1054 if (luks_opts.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
1055 luks->ivgen_cipher_alg =
1056 qcrypto_block_luks_essiv_cipher(luks_opts.cipher_alg,
1057 luks_opts.ivgen_hash_alg,
1058 &local_err);
1059 if (local_err) {
1060 error_propagate(errp, local_err);
1061 goto error;
1063 } else {
1064 luks->ivgen_cipher_alg = luks_opts.cipher_alg;
1067 strcpy(luks->header.cipher_name, cipher_alg);
1068 strcpy(luks->header.cipher_mode, cipher_mode_spec);
1069 strcpy(luks->header.hash_spec, hash_alg);
1071 luks->header.master_key_len =
1072 qcrypto_cipher_get_key_len(luks_opts.cipher_alg);
1074 if (luks_opts.cipher_mode == QCRYPTO_CIPHER_MODE_XTS) {
1075 luks->header.master_key_len *= 2;
1078 /* Generate the salt used for hashing the master key
1079 * with PBKDF later
1081 if (qcrypto_random_bytes(luks->header.master_key_salt,
1082 QCRYPTO_BLOCK_LUKS_SALT_LEN,
1083 errp) < 0) {
1084 goto error;
1087 /* Generate random master key */
1088 masterkey = g_new0(uint8_t, luks->header.master_key_len);
1089 if (qcrypto_random_bytes(masterkey,
1090 luks->header.master_key_len, errp) < 0) {
1091 goto error;
1095 /* Setup the block device payload encryption objects */
1096 if (qcrypto_block_init_cipher(block, luks_opts.cipher_alg,
1097 luks_opts.cipher_mode, masterkey,
1098 luks->header.master_key_len, 1, errp) < 0) {
1099 goto error;
1102 block->kdfhash = luks_opts.hash_alg;
1103 block->niv = qcrypto_cipher_get_iv_len(luks_opts.cipher_alg,
1104 luks_opts.cipher_mode);
1105 block->ivgen = qcrypto_ivgen_new(luks_opts.ivgen_alg,
1106 luks->ivgen_cipher_alg,
1107 luks_opts.ivgen_hash_alg,
1108 masterkey, luks->header.master_key_len,
1109 errp);
1111 if (!block->ivgen) {
1112 goto error;
1116 /* Determine how many iterations we need to hash the master
1117 * key, in order to have 1 second of compute time used
1119 iters = qcrypto_pbkdf2_count_iters(luks_opts.hash_alg,
1120 masterkey, luks->header.master_key_len,
1121 luks->header.master_key_salt,
1122 QCRYPTO_BLOCK_LUKS_SALT_LEN,
1123 QCRYPTO_BLOCK_LUKS_DIGEST_LEN,
1124 &local_err);
1125 if (local_err) {
1126 error_propagate(errp, local_err);
1127 goto error;
1130 if (iters > (ULLONG_MAX / luks_opts.iter_time)) {
1131 error_setg_errno(errp, ERANGE,
1132 "PBKDF iterations %llu too large to scale",
1133 (unsigned long long)iters);
1134 goto error;
1137 /* iter_time was in millis, but count_iters reported for secs */
1138 iters = iters * luks_opts.iter_time / 1000;
1140 /* Why /= 8 ? That matches cryptsetup, but there's no
1141 * explanation why they chose /= 8... Probably so that
1142 * if all 8 keyslots are active we only spend 1 second
1143 * in total time to check all keys */
1144 iters /= 8;
1145 if (iters > UINT32_MAX) {
1146 error_setg_errno(errp, ERANGE,
1147 "PBKDF iterations %llu larger than %u",
1148 (unsigned long long)iters, UINT32_MAX);
1149 goto error;
1151 iters = MAX(iters, QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS);
1152 luks->header.master_key_iterations = iters;
1154 /* Hash the master key, saving the result in the LUKS
1155 * header. This hash is used when opening the encrypted
1156 * device to verify that the user password unlocked a
1157 * valid master key
1159 if (qcrypto_pbkdf2(luks_opts.hash_alg,
1160 masterkey, luks->header.master_key_len,
1161 luks->header.master_key_salt,
1162 QCRYPTO_BLOCK_LUKS_SALT_LEN,
1163 luks->header.master_key_iterations,
1164 luks->header.master_key_digest,
1165 QCRYPTO_BLOCK_LUKS_DIGEST_LEN,
1166 errp) < 0) {
1167 goto error;
1171 /* Although LUKS has multiple key slots, we're just going
1172 * to use the first key slot */
1173 splitkeylen = luks->header.master_key_len * QCRYPTO_BLOCK_LUKS_STRIPES;
1174 for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
1175 luks->header.key_slots[i].active = i == 0 ?
1176 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED :
1177 QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED;
1178 luks->header.key_slots[i].stripes = QCRYPTO_BLOCK_LUKS_STRIPES;
1180 /* This calculation doesn't match that shown in the spec,
1181 * but instead follows the cryptsetup implementation.
1183 luks->header.key_slots[i].key_offset_sector =
1184 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET /
1185 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE) +
1186 (ROUND_UP(DIV_ROUND_UP(splitkeylen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE),
1187 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET /
1188 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)) * i);
1191 if (qcrypto_random_bytes(luks->header.key_slots[0].salt,
1192 QCRYPTO_BLOCK_LUKS_SALT_LEN,
1193 errp) < 0) {
1194 goto error;
1197 /* Again we determine how many iterations are required to
1198 * hash the user password while consuming 1 second of compute
1199 * time */
1200 iters = qcrypto_pbkdf2_count_iters(luks_opts.hash_alg,
1201 (uint8_t *)password, strlen(password),
1202 luks->header.key_slots[0].salt,
1203 QCRYPTO_BLOCK_LUKS_SALT_LEN,
1204 luks->header.master_key_len,
1205 &local_err);
1206 if (local_err) {
1207 error_propagate(errp, local_err);
1208 goto error;
1211 if (iters > (ULLONG_MAX / luks_opts.iter_time)) {
1212 error_setg_errno(errp, ERANGE,
1213 "PBKDF iterations %llu too large to scale",
1214 (unsigned long long)iters);
1215 goto error;
1218 /* iter_time was in millis, but count_iters reported for secs */
1219 iters = iters * luks_opts.iter_time / 1000;
1221 if (iters > UINT32_MAX) {
1222 error_setg_errno(errp, ERANGE,
1223 "PBKDF iterations %llu larger than %u",
1224 (unsigned long long)iters, UINT32_MAX);
1225 goto error;
1228 luks->header.key_slots[0].iterations =
1229 MAX(iters, QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS);
1232 /* Generate a key that we'll use to encrypt the master
1233 * key, from the user's password
1235 slotkey = g_new0(uint8_t, luks->header.master_key_len);
1236 if (qcrypto_pbkdf2(luks_opts.hash_alg,
1237 (uint8_t *)password, strlen(password),
1238 luks->header.key_slots[0].salt,
1239 QCRYPTO_BLOCK_LUKS_SALT_LEN,
1240 luks->header.key_slots[0].iterations,
1241 slotkey, luks->header.master_key_len,
1242 errp) < 0) {
1243 goto error;
1247 /* Setup the encryption objects needed to encrypt the
1248 * master key material
1250 cipher = qcrypto_cipher_new(luks_opts.cipher_alg,
1251 luks_opts.cipher_mode,
1252 slotkey, luks->header.master_key_len,
1253 errp);
1254 if (!cipher) {
1255 goto error;
1258 ivgen = qcrypto_ivgen_new(luks_opts.ivgen_alg,
1259 luks->ivgen_cipher_alg,
1260 luks_opts.ivgen_hash_alg,
1261 slotkey, luks->header.master_key_len,
1262 errp);
1263 if (!ivgen) {
1264 goto error;
1267 /* Before storing the master key, we need to vastly
1268 * increase its size, as protection against forensic
1269 * disk data recovery */
1270 splitkey = g_new0(uint8_t, splitkeylen);
1272 if (qcrypto_afsplit_encode(luks_opts.hash_alg,
1273 luks->header.master_key_len,
1274 luks->header.key_slots[0].stripes,
1275 masterkey,
1276 splitkey,
1277 errp) < 0) {
1278 goto error;
1281 /* Now we encrypt the split master key with the key generated
1282 * from the user's password, before storing it */
1283 if (qcrypto_block_cipher_encrypt_helper(cipher, block->niv, ivgen,
1284 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
1286 splitkey,
1287 splitkeylen,
1288 errp) < 0) {
1289 goto error;
1293 /* The total size of the LUKS headers is the partition header + key
1294 * slot headers, rounded up to the nearest sector, combined with
1295 * the size of each master key material region, also rounded up
1296 * to the nearest sector */
1297 luks->header.payload_offset_sector =
1298 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET /
1299 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE) +
1300 (ROUND_UP(DIV_ROUND_UP(splitkeylen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE),
1301 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET /
1302 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)) *
1303 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS);
1305 block->sector_size = QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
1306 block->payload_offset = luks->header.payload_offset_sector *
1307 block->sector_size;
1309 /* Reserve header space to match payload offset */
1310 initfunc(block, block->payload_offset, opaque, &local_err);
1311 if (local_err) {
1312 error_propagate(errp, local_err);
1313 goto error;
1316 if (qcrypto_block_luks_store_header(block, writefunc, opaque, errp) < 0) {
1317 goto error;
1320 /* Write out the master key material, starting at the
1321 * sector immediately following the partition header. */
1322 if (writefunc(block,
1323 luks->header.key_slots[0].key_offset_sector *
1324 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
1325 splitkey, splitkeylen,
1326 opaque,
1327 errp) != splitkeylen) {
1328 goto error;
1331 memset(masterkey, 0, luks->header.master_key_len);
1332 memset(slotkey, 0, luks->header.master_key_len);
1334 return 0;
1336 error:
1337 if (masterkey) {
1338 memset(masterkey, 0, luks->header.master_key_len);
1340 if (slotkey) {
1341 memset(slotkey, 0, luks->header.master_key_len);
1344 qcrypto_block_free_cipher(block);
1345 qcrypto_ivgen_free(block->ivgen);
1347 g_free(luks);
1348 return -1;
1352 static int qcrypto_block_luks_get_info(QCryptoBlock *block,
1353 QCryptoBlockInfo *info,
1354 Error **errp)
1356 QCryptoBlockLUKS *luks = block->opaque;
1357 QCryptoBlockInfoLUKSSlot *slot;
1358 QCryptoBlockInfoLUKSSlotList *slots = NULL, **prev = &info->u.luks.slots;
1359 size_t i;
1361 info->u.luks.cipher_alg = luks->cipher_alg;
1362 info->u.luks.cipher_mode = luks->cipher_mode;
1363 info->u.luks.ivgen_alg = luks->ivgen_alg;
1364 if (info->u.luks.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
1365 info->u.luks.has_ivgen_hash_alg = true;
1366 info->u.luks.ivgen_hash_alg = luks->ivgen_hash_alg;
1368 info->u.luks.hash_alg = luks->hash_alg;
1369 info->u.luks.payload_offset = block->payload_offset;
1370 info->u.luks.master_key_iters = luks->header.master_key_iterations;
1371 info->u.luks.uuid = g_strndup((const char *)luks->header.uuid,
1372 sizeof(luks->header.uuid));
1374 for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
1375 slots = g_new0(QCryptoBlockInfoLUKSSlotList, 1);
1376 *prev = slots;
1378 slots->value = slot = g_new0(QCryptoBlockInfoLUKSSlot, 1);
1379 slot->active = luks->header.key_slots[i].active ==
1380 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED;
1381 slot->key_offset = luks->header.key_slots[i].key_offset_sector
1382 * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
1383 if (slot->active) {
1384 slot->has_iters = true;
1385 slot->iters = luks->header.key_slots[i].iterations;
1386 slot->has_stripes = true;
1387 slot->stripes = luks->header.key_slots[i].stripes;
1390 prev = &slots->next;
1393 return 0;
1397 static void qcrypto_block_luks_cleanup(QCryptoBlock *block)
1399 g_free(block->opaque);
1403 static int
1404 qcrypto_block_luks_decrypt(QCryptoBlock *block,
1405 uint64_t offset,
1406 uint8_t *buf,
1407 size_t len,
1408 Error **errp)
1410 assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
1411 assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
1412 return qcrypto_block_decrypt_helper(block,
1413 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
1414 offset, buf, len, errp);
1418 static int
1419 qcrypto_block_luks_encrypt(QCryptoBlock *block,
1420 uint64_t offset,
1421 uint8_t *buf,
1422 size_t len,
1423 Error **errp)
1425 assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
1426 assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
1427 return qcrypto_block_encrypt_helper(block,
1428 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
1429 offset, buf, len, errp);
1433 const QCryptoBlockDriver qcrypto_block_driver_luks = {
1434 .open = qcrypto_block_luks_open,
1435 .create = qcrypto_block_luks_create,
1436 .get_info = qcrypto_block_luks_get_info,
1437 .cleanup = qcrypto_block_luks_cleanup,
1438 .decrypt = qcrypto_block_luks_decrypt,
1439 .encrypt = qcrypto_block_luks_encrypt,
1440 .has_format = qcrypto_block_luks_has_format,