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 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 "crypto/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
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
{
83 typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap
;
84 struct QCryptoBlockLUKSCipherSizeMap
{
88 typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap
;
89 struct QCryptoBlockLUKSCipherNameMap
{
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
},
103 static const QCryptoBlockLUKSCipherSizeMap
104 qcrypto_block_luks_cipher_size_map_cast5
[] = {
105 { 16, QCRYPTO_CIPHER_ALG_CAST5_128
},
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
},
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
},
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 */
141 /* iterations for PBKDF2 */
143 /* salt for PBKDF2 */
144 uint8_t salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
145 /* start sector of key material */
147 /* number of anti-forensic 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 */
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
;
177 /* Number of key bytes */
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
];
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 /* Cache parsed versions of what's in header fields,
203 * as we can't rely on QCryptoBlock.cipher being
205 QCryptoCipherAlgorithm cipher_alg
;
206 QCryptoCipherMode cipher_mode
;
207 QCryptoIVGenAlgorithm ivgen_alg
;
208 QCryptoHashAlgorithm ivgen_hash_alg
;
209 QCryptoHashAlgorithm hash_alg
;
213 static int qcrypto_block_luks_cipher_name_lookup(const char *name
,
214 QCryptoCipherMode mode
,
218 const QCryptoBlockLUKSCipherNameMap
*map
=
219 qcrypto_block_luks_cipher_name_map
;
220 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
223 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
227 for (i
= 0; i
< maplen
; i
++) {
228 if (!g_str_equal(map
[i
].name
, name
)) {
231 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
232 if (map
[i
].sizes
[j
].key_bytes
== key_bytes
) {
233 return map
[i
].sizes
[j
].id
;
238 error_setg(errp
, "Algorithm %s with key size %d bytes not supported",
244 qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg
,
247 const QCryptoBlockLUKSCipherNameMap
*map
=
248 qcrypto_block_luks_cipher_name_map
;
249 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
251 for (i
= 0; i
< maplen
; i
++) {
252 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
253 if (map
[i
].sizes
[j
].id
== alg
) {
259 error_setg(errp
, "Algorithm '%s' not supported",
260 QCryptoCipherAlgorithm_lookup
[alg
]);
264 /* XXX replace with qapi_enum_parse() in future, when we can
265 * make that function emit a more friendly error message */
266 static int qcrypto_block_luks_name_lookup(const char *name
,
267 const char *const *map
,
273 for (i
= 0; i
< maplen
; i
++) {
274 if (g_str_equal(map
[i
], name
)) {
279 error_setg(errp
, "%s %s not supported", type
, name
);
283 #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
284 qcrypto_block_luks_name_lookup(name, \
285 QCryptoCipherMode_lookup, \
286 QCRYPTO_CIPHER_MODE__MAX, \
290 #define qcrypto_block_luks_hash_name_lookup(name, errp) \
291 qcrypto_block_luks_name_lookup(name, \
292 QCryptoHashAlgorithm_lookup, \
293 QCRYPTO_HASH_ALG__MAX, \
297 #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
298 qcrypto_block_luks_name_lookup(name, \
299 QCryptoIVGenAlgorithm_lookup, \
300 QCRYPTO_IVGEN_ALG__MAX, \
306 qcrypto_block_luks_has_format(const uint8_t *buf
,
309 const QCryptoBlockLUKSHeader
*luks_header
= (const void *)buf
;
311 if (buf_size
>= offsetof(QCryptoBlockLUKSHeader
, cipher_name
) &&
312 memcmp(luks_header
->magic
, qcrypto_block_luks_magic
,
313 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) == 0 &&
314 be16_to_cpu(luks_header
->version
) == QCRYPTO_BLOCK_LUKS_VERSION
) {
323 * Deal with a quirk of dm-crypt usage of ESSIV.
325 * When calculating ESSIV IVs, the cipher length used by ESSIV
326 * may be different from the cipher length used for the block
327 * encryption, becauses dm-crypt uses the hash digest length
328 * as the key size. ie, if you have AES 128 as the block cipher
329 * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
330 * the cipher since that gets a key length matching the digest
331 * size, not AES 128 with truncated digest as might be imagined
333 static QCryptoCipherAlgorithm
334 qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher
,
335 QCryptoHashAlgorithm hash
,
338 size_t digestlen
= qcrypto_hash_digest_len(hash
);
339 size_t keylen
= qcrypto_cipher_get_key_len(cipher
);
340 if (digestlen
== keylen
) {
345 case QCRYPTO_CIPHER_ALG_AES_128
:
346 case QCRYPTO_CIPHER_ALG_AES_192
:
347 case QCRYPTO_CIPHER_ALG_AES_256
:
348 if (digestlen
== qcrypto_cipher_get_key_len(
349 QCRYPTO_CIPHER_ALG_AES_128
)) {
350 return QCRYPTO_CIPHER_ALG_AES_128
;
351 } else if (digestlen
== qcrypto_cipher_get_key_len(
352 QCRYPTO_CIPHER_ALG_AES_192
)) {
353 return QCRYPTO_CIPHER_ALG_AES_192
;
354 } else if (digestlen
== qcrypto_cipher_get_key_len(
355 QCRYPTO_CIPHER_ALG_AES_256
)) {
356 return QCRYPTO_CIPHER_ALG_AES_256
;
358 error_setg(errp
, "No AES cipher with key size %zu available",
363 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
364 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
365 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
366 if (digestlen
== qcrypto_cipher_get_key_len(
367 QCRYPTO_CIPHER_ALG_SERPENT_128
)) {
368 return QCRYPTO_CIPHER_ALG_SERPENT_128
;
369 } else if (digestlen
== qcrypto_cipher_get_key_len(
370 QCRYPTO_CIPHER_ALG_SERPENT_192
)) {
371 return QCRYPTO_CIPHER_ALG_SERPENT_192
;
372 } else if (digestlen
== qcrypto_cipher_get_key_len(
373 QCRYPTO_CIPHER_ALG_SERPENT_256
)) {
374 return QCRYPTO_CIPHER_ALG_SERPENT_256
;
376 error_setg(errp
, "No Serpent cipher with key size %zu available",
381 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
382 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
383 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
384 if (digestlen
== qcrypto_cipher_get_key_len(
385 QCRYPTO_CIPHER_ALG_TWOFISH_128
)) {
386 return QCRYPTO_CIPHER_ALG_TWOFISH_128
;
387 } else if (digestlen
== qcrypto_cipher_get_key_len(
388 QCRYPTO_CIPHER_ALG_TWOFISH_192
)) {
389 return QCRYPTO_CIPHER_ALG_TWOFISH_192
;
390 } else if (digestlen
== qcrypto_cipher_get_key_len(
391 QCRYPTO_CIPHER_ALG_TWOFISH_256
)) {
392 return QCRYPTO_CIPHER_ALG_TWOFISH_256
;
394 error_setg(errp
, "No Twofish cipher with key size %zu available",
400 error_setg(errp
, "Cipher %s not supported with essiv",
401 QCryptoCipherAlgorithm_lookup
[cipher
]);
407 * Given a key slot, and user password, this will attempt to unlock
408 * the master encryption key from the key slot.
411 * 0 if the key slot is disabled, or key could not be decrypted
412 * with the provided password
413 * 1 if the key slot is enabled, and key decrypted successfully
414 * with the provided password
415 * -1 if a fatal error occurred loading the key
418 qcrypto_block_luks_load_key(QCryptoBlock
*block
,
419 QCryptoBlockLUKSKeySlot
*slot
,
420 const char *password
,
421 QCryptoCipherAlgorithm cipheralg
,
422 QCryptoCipherMode ciphermode
,
423 QCryptoHashAlgorithm hash
,
424 QCryptoIVGenAlgorithm ivalg
,
425 QCryptoCipherAlgorithm ivcipheralg
,
426 QCryptoHashAlgorithm ivhash
,
429 QCryptoBlockReadFunc readfunc
,
433 QCryptoBlockLUKS
*luks
= block
->opaque
;
436 uint8_t *possiblekey
;
439 QCryptoCipher
*cipher
= NULL
;
440 uint8_t keydigest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
441 QCryptoIVGen
*ivgen
= NULL
;
444 if (slot
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
) {
448 splitkeylen
= masterkeylen
* slot
->stripes
;
449 splitkey
= g_new0(uint8_t, splitkeylen
);
450 possiblekey
= g_new0(uint8_t, masterkeylen
);
453 * The user password is used to generate a (possible)
454 * decryption key. This may or may not successfully
455 * decrypt the master key - we just blindly assume
456 * the key is correct and validate the results of
459 if (qcrypto_pbkdf2(hash
,
460 (const uint8_t *)password
, strlen(password
),
461 slot
->salt
, QCRYPTO_BLOCK_LUKS_SALT_LEN
,
463 possiblekey
, masterkeylen
,
469 * We need to read the master key material from the
470 * LUKS key material header. What we're reading is
471 * not the raw master key, but rather the data after
472 * it has been passed through AFSplit and the result
477 slot
->key_offset
* QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
478 splitkey
, splitkeylen
,
485 /* Setup the cipher/ivgen that we'll use to try to decrypt
486 * the split master key material */
487 cipher
= qcrypto_cipher_new(cipheralg
, ciphermode
,
488 possiblekey
, masterkeylen
,
494 niv
= qcrypto_cipher_get_iv_len(cipheralg
,
496 ivgen
= qcrypto_ivgen_new(ivalg
,
499 possiblekey
, masterkeylen
,
507 * The master key needs to be decrypted in the same
508 * way that the block device payload will be decrypted
509 * later. In particular we'll be using the IV generator
510 * to reset the encryption cipher every time the master
511 * key crosses a sector boundary.
513 if (qcrypto_block_decrypt_helper(cipher
,
516 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
525 * Now we've decrypted the split master key, join
526 * it back together to get the actual master key.
528 if (qcrypto_afsplit_decode(hash
,
539 * We still don't know that the masterkey we got is valid,
540 * because we just blindly assumed the user's password
541 * was correct. This is where we now verify it. We are
542 * creating a hash of the master key using PBKDF and
543 * then comparing that to the hash stored in the key slot
546 if (qcrypto_pbkdf2(hash
,
547 masterkey
, masterkeylen
,
548 luks
->header
.master_key_salt
,
549 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
550 luks
->header
.master_key_iterations
,
551 keydigest
, G_N_ELEMENTS(keydigest
),
556 if (memcmp(keydigest
, luks
->header
.master_key_digest
,
557 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
) == 0) {
558 /* Success, we got the right master key */
563 /* Fail, user's password was not valid for this key slot,
564 * tell caller to try another slot */
568 qcrypto_ivgen_free(ivgen
);
569 qcrypto_cipher_free(cipher
);
577 * Given a user password, this will iterate over all key
578 * slots and try to unlock each active key slot using the
579 * password until it successfully obtains a master key.
581 * Returns 0 if a key was loaded, -1 if no keys could be loaded
584 qcrypto_block_luks_find_key(QCryptoBlock
*block
,
585 const char *password
,
586 QCryptoCipherAlgorithm cipheralg
,
587 QCryptoCipherMode ciphermode
,
588 QCryptoHashAlgorithm hash
,
589 QCryptoIVGenAlgorithm ivalg
,
590 QCryptoCipherAlgorithm ivcipheralg
,
591 QCryptoHashAlgorithm ivhash
,
593 size_t *masterkeylen
,
594 QCryptoBlockReadFunc readfunc
,
598 QCryptoBlockLUKS
*luks
= block
->opaque
;
602 *masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
603 *masterkeylen
= luks
->header
.key_bytes
;
605 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
606 rv
= qcrypto_block_luks_load_key(block
,
607 &luks
->header
.key_slots
[i
],
628 error_setg(errp
, "Invalid password, cannot unlock any keyslot");
639 qcrypto_block_luks_open(QCryptoBlock
*block
,
640 QCryptoBlockOpenOptions
*options
,
641 QCryptoBlockReadFunc readfunc
,
646 QCryptoBlockLUKS
*luks
;
647 Error
*local_err
= NULL
;
651 uint8_t *masterkey
= NULL
;
653 char *ivgen_name
, *ivhash_name
;
654 QCryptoCipherMode ciphermode
;
655 QCryptoCipherAlgorithm cipheralg
;
656 QCryptoIVGenAlgorithm ivalg
;
657 QCryptoCipherAlgorithm ivcipheralg
;
658 QCryptoHashAlgorithm hash
;
659 QCryptoHashAlgorithm ivhash
;
660 char *password
= NULL
;
662 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
663 if (!options
->u
.luks
.key_secret
) {
664 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
667 password
= qcrypto_secret_lookup_as_utf8(
668 options
->u
.luks
.key_secret
, errp
);
674 luks
= g_new0(QCryptoBlockLUKS
, 1);
675 block
->opaque
= luks
;
677 /* Read the entire LUKS header, minus the key material from
678 * the underlying device */
679 rv
= readfunc(block
, opaque
, 0,
680 (uint8_t *)&luks
->header
,
681 sizeof(luks
->header
),
688 /* The header is always stored in big-endian format, so
689 * convert everything to native */
690 be16_to_cpus(&luks
->header
.version
);
691 be32_to_cpus(&luks
->header
.payload_offset
);
692 be32_to_cpus(&luks
->header
.key_bytes
);
693 be32_to_cpus(&luks
->header
.master_key_iterations
);
695 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
696 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
697 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
698 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
699 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
702 if (memcmp(luks
->header
.magic
, qcrypto_block_luks_magic
,
703 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) != 0) {
704 error_setg(errp
, "Volume is not in LUKS format");
708 if (luks
->header
.version
!= QCRYPTO_BLOCK_LUKS_VERSION
) {
709 error_setg(errp
, "LUKS version %" PRIu32
" is not supported",
710 luks
->header
.version
);
716 * The cipher_mode header contains a string that we have
717 * to further parse, of the format
719 * <cipher-mode>-<iv-generator>[:<iv-hash>]
721 * eg cbc-essiv:sha256, cbc-plain64
723 ivgen_name
= strchr(luks
->header
.cipher_mode
, '-');
726 error_setg(errp
, "Unexpected cipher mode string format %s",
727 luks
->header
.cipher_mode
);
733 ivhash_name
= strchr(ivgen_name
, ':');
740 ivhash
= qcrypto_block_luks_hash_name_lookup(ivhash_name
,
744 error_propagate(errp
, local_err
);
749 ciphermode
= qcrypto_block_luks_cipher_mode_lookup(luks
->header
.cipher_mode
,
753 error_propagate(errp
, local_err
);
757 cipheralg
= qcrypto_block_luks_cipher_name_lookup(luks
->header
.cipher_name
,
759 luks
->header
.key_bytes
,
763 error_propagate(errp
, local_err
);
767 hash
= qcrypto_block_luks_hash_name_lookup(luks
->header
.hash_spec
,
771 error_propagate(errp
, local_err
);
775 ivalg
= qcrypto_block_luks_ivgen_name_lookup(ivgen_name
,
779 error_propagate(errp
, local_err
);
783 if (ivalg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
786 error_setg(errp
, "Missing IV generator hash specification");
789 ivcipheralg
= qcrypto_block_luks_essiv_cipher(cipheralg
,
794 error_propagate(errp
, local_err
);
798 /* Note we parsed the ivhash_name earlier in the cipher_mode
799 * spec string even with plain/plain64 ivgens, but we
800 * will ignore it, since it is irrelevant for these ivgens.
801 * This is for compat with dm-crypt which will silently
802 * ignore hash names with these ivgens rather than report
803 * an error about the invalid usage
805 ivcipheralg
= cipheralg
;
808 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
809 /* Try to find which key slot our password is valid for
810 * and unlock the master key from that slot.
812 if (qcrypto_block_luks_find_key(block
,
814 cipheralg
, ciphermode
,
819 &masterkey
, &masterkeylen
,
826 /* We have a valid master key now, so can setup the
827 * block device payload decryption objects
829 block
->kdfhash
= hash
;
830 block
->niv
= qcrypto_cipher_get_iv_len(cipheralg
,
832 block
->ivgen
= qcrypto_ivgen_new(ivalg
,
835 masterkey
, masterkeylen
,
842 block
->cipher
= qcrypto_cipher_new(cipheralg
,
844 masterkey
, masterkeylen
,
846 if (!block
->cipher
) {
852 block
->payload_offset
= luks
->header
.payload_offset
*
853 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
855 luks
->cipher_alg
= cipheralg
;
856 luks
->cipher_mode
= ciphermode
;
857 luks
->ivgen_alg
= ivalg
;
858 luks
->ivgen_hash_alg
= ivhash
;
859 luks
->hash_alg
= hash
;
868 qcrypto_cipher_free(block
->cipher
);
869 qcrypto_ivgen_free(block
->ivgen
);
877 qcrypto_block_luks_uuid_gen(uint8_t *uuidstr
)
880 qemu_uuid_generate(&uuid
);
881 qemu_uuid_unparse(&uuid
, (char *)uuidstr
);
885 qcrypto_block_luks_create(QCryptoBlock
*block
,
886 QCryptoBlockCreateOptions
*options
,
887 QCryptoBlockInitFunc initfunc
,
888 QCryptoBlockWriteFunc writefunc
,
892 QCryptoBlockLUKS
*luks
;
893 QCryptoBlockCreateOptionsLUKS luks_opts
;
894 Error
*local_err
= NULL
;
895 uint8_t *masterkey
= NULL
;
896 uint8_t *slotkey
= NULL
;
897 uint8_t *splitkey
= NULL
;
898 size_t splitkeylen
= 0;
900 QCryptoCipher
*cipher
= NULL
;
901 QCryptoIVGen
*ivgen
= NULL
;
903 const char *cipher_alg
;
904 const char *cipher_mode
;
905 const char *ivgen_alg
;
906 const char *ivgen_hash_alg
= NULL
;
907 const char *hash_alg
;
908 char *cipher_mode_spec
= NULL
;
909 QCryptoCipherAlgorithm ivcipheralg
= 0;
912 memcpy(&luks_opts
, &options
->u
.luks
, sizeof(luks_opts
));
913 if (!luks_opts
.has_iter_time
) {
914 luks_opts
.iter_time
= 2000;
916 if (!luks_opts
.has_cipher_alg
) {
917 luks_opts
.cipher_alg
= QCRYPTO_CIPHER_ALG_AES_256
;
919 if (!luks_opts
.has_cipher_mode
) {
920 luks_opts
.cipher_mode
= QCRYPTO_CIPHER_MODE_XTS
;
922 if (!luks_opts
.has_ivgen_alg
) {
923 luks_opts
.ivgen_alg
= QCRYPTO_IVGEN_ALG_PLAIN64
;
925 if (!luks_opts
.has_hash_alg
) {
926 luks_opts
.hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
928 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
929 if (!luks_opts
.has_ivgen_hash_alg
) {
930 luks_opts
.ivgen_hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
931 luks_opts
.has_ivgen_hash_alg
= true;
934 /* Note we're allowing ivgen_hash_alg to be set even for
935 * non-essiv iv generators that don't need a hash. It will
936 * be silently ignored, for compatibility with dm-crypt */
938 if (!options
->u
.luks
.key_secret
) {
939 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
942 password
= qcrypto_secret_lookup_as_utf8(luks_opts
.key_secret
, errp
);
947 luks
= g_new0(QCryptoBlockLUKS
, 1);
948 block
->opaque
= luks
;
950 memcpy(luks
->header
.magic
, qcrypto_block_luks_magic
,
951 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
);
953 /* We populate the header in native endianness initially and
954 * then convert everything to big endian just before writing
957 luks
->header
.version
= QCRYPTO_BLOCK_LUKS_VERSION
;
958 qcrypto_block_luks_uuid_gen(luks
->header
.uuid
);
960 cipher_alg
= qcrypto_block_luks_cipher_alg_lookup(luks_opts
.cipher_alg
,
966 cipher_mode
= QCryptoCipherMode_lookup
[luks_opts
.cipher_mode
];
967 ivgen_alg
= QCryptoIVGenAlgorithm_lookup
[luks_opts
.ivgen_alg
];
968 if (luks_opts
.has_ivgen_hash_alg
) {
969 ivgen_hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.ivgen_hash_alg
];
970 cipher_mode_spec
= g_strdup_printf("%s-%s:%s", cipher_mode
, ivgen_alg
,
973 cipher_mode_spec
= g_strdup_printf("%s-%s", cipher_mode
, ivgen_alg
);
975 hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.hash_alg
];
978 if (strlen(cipher_alg
) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
) {
979 error_setg(errp
, "Cipher name '%s' is too long for LUKS header",
983 if (strlen(cipher_mode_spec
) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
) {
984 error_setg(errp
, "Cipher mode '%s' is too long for LUKS header",
988 if (strlen(hash_alg
) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
) {
989 error_setg(errp
, "Hash name '%s' is too long for LUKS header",
994 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
995 ivcipheralg
= qcrypto_block_luks_essiv_cipher(luks_opts
.cipher_alg
,
996 luks_opts
.ivgen_hash_alg
,
999 error_propagate(errp
, local_err
);
1003 ivcipheralg
= luks_opts
.cipher_alg
;
1006 strcpy(luks
->header
.cipher_name
, cipher_alg
);
1007 strcpy(luks
->header
.cipher_mode
, cipher_mode_spec
);
1008 strcpy(luks
->header
.hash_spec
, hash_alg
);
1010 luks
->header
.key_bytes
= qcrypto_cipher_get_key_len(luks_opts
.cipher_alg
);
1011 if (luks_opts
.cipher_mode
== QCRYPTO_CIPHER_MODE_XTS
) {
1012 luks
->header
.key_bytes
*= 2;
1015 /* Generate the salt used for hashing the master key
1018 if (qcrypto_random_bytes(luks
->header
.master_key_salt
,
1019 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1024 /* Generate random master key */
1025 masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
1026 if (qcrypto_random_bytes(masterkey
,
1027 luks
->header
.key_bytes
, errp
) < 0) {
1032 /* Setup the block device payload encryption objects */
1033 block
->cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1034 luks_opts
.cipher_mode
,
1035 masterkey
, luks
->header
.key_bytes
,
1037 if (!block
->cipher
) {
1041 block
->kdfhash
= luks_opts
.hash_alg
;
1042 block
->niv
= qcrypto_cipher_get_iv_len(luks_opts
.cipher_alg
,
1043 luks_opts
.cipher_mode
);
1044 block
->ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1046 luks_opts
.ivgen_hash_alg
,
1047 masterkey
, luks
->header
.key_bytes
,
1050 if (!block
->ivgen
) {
1055 /* Determine how many iterations we need to hash the master
1056 * key, in order to have 1 second of compute time used
1058 iters
= qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1059 masterkey
, luks
->header
.key_bytes
,
1060 luks
->header
.master_key_salt
,
1061 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1062 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1065 error_propagate(errp
, local_err
);
1069 if (iters
> (ULLONG_MAX
/ luks_opts
.iter_time
)) {
1070 error_setg_errno(errp
, ERANGE
,
1071 "PBKDF iterations %llu too large to scale",
1072 (unsigned long long)iters
);
1076 /* iter_time was in millis, but count_iters reported for secs */
1077 iters
= iters
* luks_opts
.iter_time
/ 1000;
1079 /* Why /= 8 ? That matches cryptsetup, but there's no
1080 * explanation why they chose /= 8... Probably so that
1081 * if all 8 keyslots are active we only spend 1 second
1082 * in total time to check all keys */
1084 if (iters
> UINT32_MAX
) {
1085 error_setg_errno(errp
, ERANGE
,
1086 "PBKDF iterations %llu larger than %u",
1087 (unsigned long long)iters
, UINT32_MAX
);
1090 iters
= MAX(iters
, QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS
);
1091 luks
->header
.master_key_iterations
= iters
;
1093 /* Hash the master key, saving the result in the LUKS
1094 * header. This hash is used when opening the encrypted
1095 * device to verify that the user password unlocked a
1098 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1099 masterkey
, luks
->header
.key_bytes
,
1100 luks
->header
.master_key_salt
,
1101 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1102 luks
->header
.master_key_iterations
,
1103 luks
->header
.master_key_digest
,
1104 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1110 /* Although LUKS has multiple key slots, we're just going
1111 * to use the first key slot */
1112 splitkeylen
= luks
->header
.key_bytes
* QCRYPTO_BLOCK_LUKS_STRIPES
;
1113 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1114 luks
->header
.key_slots
[i
].active
= i
== 0 ?
1115 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
:
1116 QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
;
1117 luks
->header
.key_slots
[i
].stripes
= QCRYPTO_BLOCK_LUKS_STRIPES
;
1119 /* This calculation doesn't match that shown in the spec,
1120 * but instead follows the cryptsetup implementation.
1122 luks
->header
.key_slots
[i
].key_offset
=
1123 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1124 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1125 (ROUND_UP(DIV_ROUND_UP(splitkeylen
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1126 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1127 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) * i
);
1130 if (qcrypto_random_bytes(luks
->header
.key_slots
[0].salt
,
1131 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1136 /* Again we determine how many iterations are required to
1137 * hash the user password while consuming 1 second of compute
1139 iters
= qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1140 (uint8_t *)password
, strlen(password
),
1141 luks
->header
.key_slots
[0].salt
,
1142 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1143 luks
->header
.key_bytes
,
1146 error_propagate(errp
, local_err
);
1150 if (iters
> (ULLONG_MAX
/ luks_opts
.iter_time
)) {
1151 error_setg_errno(errp
, ERANGE
,
1152 "PBKDF iterations %llu too large to scale",
1153 (unsigned long long)iters
);
1157 /* iter_time was in millis, but count_iters reported for secs */
1158 iters
= iters
* luks_opts
.iter_time
/ 1000;
1160 if (iters
> UINT32_MAX
) {
1161 error_setg_errno(errp
, ERANGE
,
1162 "PBKDF iterations %llu larger than %u",
1163 (unsigned long long)iters
, UINT32_MAX
);
1167 luks
->header
.key_slots
[0].iterations
=
1168 MAX(iters
, QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS
);
1171 /* Generate a key that we'll use to encrypt the master
1172 * key, from the user's password
1174 slotkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
1175 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1176 (uint8_t *)password
, strlen(password
),
1177 luks
->header
.key_slots
[0].salt
,
1178 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1179 luks
->header
.key_slots
[0].iterations
,
1180 slotkey
, luks
->header
.key_bytes
,
1186 /* Setup the encryption objects needed to encrypt the
1187 * master key material
1189 cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1190 luks_opts
.cipher_mode
,
1191 slotkey
, luks
->header
.key_bytes
,
1197 ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1199 luks_opts
.ivgen_hash_alg
,
1200 slotkey
, luks
->header
.key_bytes
,
1206 /* Before storing the master key, we need to vastly
1207 * increase its size, as protection against forensic
1208 * disk data recovery */
1209 splitkey
= g_new0(uint8_t, splitkeylen
);
1211 if (qcrypto_afsplit_encode(luks_opts
.hash_alg
,
1212 luks
->header
.key_bytes
,
1213 luks
->header
.key_slots
[0].stripes
,
1220 /* Now we encrypt the split master key with the key generated
1221 * from the user's password, before storing it */
1222 if (qcrypto_block_encrypt_helper(cipher
, block
->niv
, ivgen
,
1223 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1232 /* The total size of the LUKS headers is the partition header + key
1233 * slot headers, rounded up to the nearest sector, combined with
1234 * the size of each master key material region, also rounded up
1235 * to the nearest sector */
1236 luks
->header
.payload_offset
=
1237 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1238 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1239 (ROUND_UP(DIV_ROUND_UP(splitkeylen
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1240 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1241 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) *
1242 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1244 block
->payload_offset
= luks
->header
.payload_offset
*
1245 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1247 /* Reserve header space to match payload offset */
1248 initfunc(block
, opaque
, block
->payload_offset
, &local_err
);
1250 error_propagate(errp
, local_err
);
1254 /* Everything on disk uses Big Endian, so flip header fields
1255 * before writing them */
1256 cpu_to_be16s(&luks
->header
.version
);
1257 cpu_to_be32s(&luks
->header
.payload_offset
);
1258 cpu_to_be32s(&luks
->header
.key_bytes
);
1259 cpu_to_be32s(&luks
->header
.master_key_iterations
);
1261 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1262 cpu_to_be32s(&luks
->header
.key_slots
[i
].active
);
1263 cpu_to_be32s(&luks
->header
.key_slots
[i
].iterations
);
1264 cpu_to_be32s(&luks
->header
.key_slots
[i
].key_offset
);
1265 cpu_to_be32s(&luks
->header
.key_slots
[i
].stripes
);
1269 /* Write out the partition header and key slot headers */
1270 writefunc(block
, opaque
, 0,
1271 (const uint8_t *)&luks
->header
,
1272 sizeof(luks
->header
),
1275 /* Delay checking local_err until we've byte-swapped */
1277 /* Byte swap the header back to native, in case we need
1278 * to read it again later */
1279 be16_to_cpus(&luks
->header
.version
);
1280 be32_to_cpus(&luks
->header
.payload_offset
);
1281 be32_to_cpus(&luks
->header
.key_bytes
);
1282 be32_to_cpus(&luks
->header
.master_key_iterations
);
1284 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1285 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
1286 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
1287 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
1288 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
1292 error_propagate(errp
, local_err
);
1296 /* Write out the master key material, starting at the
1297 * sector immediately following the partition header. */
1298 if (writefunc(block
, opaque
,
1299 luks
->header
.key_slots
[0].key_offset
*
1300 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1301 splitkey
, splitkeylen
,
1302 errp
) != splitkeylen
) {
1306 luks
->cipher_alg
= luks_opts
.cipher_alg
;
1307 luks
->cipher_mode
= luks_opts
.cipher_mode
;
1308 luks
->ivgen_alg
= luks_opts
.ivgen_alg
;
1309 luks
->ivgen_hash_alg
= luks_opts
.ivgen_hash_alg
;
1310 luks
->hash_alg
= luks_opts
.hash_alg
;
1312 memset(masterkey
, 0, luks
->header
.key_bytes
);
1314 memset(slotkey
, 0, luks
->header
.key_bytes
);
1318 g_free(cipher_mode_spec
);
1320 qcrypto_ivgen_free(ivgen
);
1321 qcrypto_cipher_free(cipher
);
1327 memset(masterkey
, 0, luks
->header
.key_bytes
);
1331 memset(slotkey
, 0, luks
->header
.key_bytes
);
1336 g_free(cipher_mode_spec
);
1338 qcrypto_ivgen_free(ivgen
);
1339 qcrypto_cipher_free(cipher
);
1346 static int qcrypto_block_luks_get_info(QCryptoBlock
*block
,
1347 QCryptoBlockInfo
*info
,
1350 QCryptoBlockLUKS
*luks
= block
->opaque
;
1351 QCryptoBlockInfoLUKSSlot
*slot
;
1352 QCryptoBlockInfoLUKSSlotList
*slots
= NULL
, **prev
= &info
->u
.luks
.slots
;
1355 info
->u
.luks
.cipher_alg
= luks
->cipher_alg
;
1356 info
->u
.luks
.cipher_mode
= luks
->cipher_mode
;
1357 info
->u
.luks
.ivgen_alg
= luks
->ivgen_alg
;
1358 if (info
->u
.luks
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
1359 info
->u
.luks
.has_ivgen_hash_alg
= true;
1360 info
->u
.luks
.ivgen_hash_alg
= luks
->ivgen_hash_alg
;
1362 info
->u
.luks
.hash_alg
= luks
->hash_alg
;
1363 info
->u
.luks
.payload_offset
= block
->payload_offset
;
1364 info
->u
.luks
.master_key_iters
= luks
->header
.master_key_iterations
;
1365 info
->u
.luks
.uuid
= g_strndup((const char *)luks
->header
.uuid
,
1366 sizeof(luks
->header
.uuid
));
1368 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1369 slots
= g_new0(QCryptoBlockInfoLUKSSlotList
, 1);
1372 slots
->value
= slot
= g_new0(QCryptoBlockInfoLUKSSlot
, 1);
1373 slot
->active
= luks
->header
.key_slots
[i
].active
==
1374 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
;
1375 slot
->key_offset
= luks
->header
.key_slots
[i
].key_offset
1376 * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1378 slot
->has_iters
= true;
1379 slot
->iters
= luks
->header
.key_slots
[i
].iterations
;
1380 slot
->has_stripes
= true;
1381 slot
->stripes
= luks
->header
.key_slots
[i
].stripes
;
1384 prev
= &slots
->next
;
1391 static void qcrypto_block_luks_cleanup(QCryptoBlock
*block
)
1393 g_free(block
->opaque
);
1398 qcrypto_block_luks_decrypt(QCryptoBlock
*block
,
1399 uint64_t startsector
,
1404 return qcrypto_block_decrypt_helper(block
->cipher
,
1405 block
->niv
, block
->ivgen
,
1406 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1407 startsector
, buf
, len
, errp
);
1412 qcrypto_block_luks_encrypt(QCryptoBlock
*block
,
1413 uint64_t startsector
,
1418 return qcrypto_block_encrypt_helper(block
->cipher
,
1419 block
->niv
, block
->ivgen
,
1420 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1421 startsector
, buf
, len
, errp
);
1425 const QCryptoBlockDriver qcrypto_block_driver_luks
= {
1426 .open
= qcrypto_block_luks_open
,
1427 .create
= qcrypto_block_luks_create
,
1428 .get_info
= qcrypto_block_luks_get_info
,
1429 .cleanup
= qcrypto_block_luks_cleanup
,
1430 .decrypt
= qcrypto_block_luks_decrypt
,
1431 .encrypt
= qcrypto_block_luks_encrypt
,
1432 .has_format
= qcrypto_block_luks_has_format
,