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"
23 #include "crypto/block-luks.h"
25 #include "crypto/hash.h"
26 #include "crypto/afsplit.h"
27 #include "crypto/pbkdf.h"
28 #include "crypto/secret.h"
29 #include "crypto/random.h"
32 #include <uuid/uuid.h>
35 #include "qemu/coroutine.h"
38 * Reference for the LUKS format implemented here is
40 * docs/on-disk-format.pdf
42 * in 'cryptsetup' package source code
44 * This file implements the 1.2.1 specification, dated
48 typedef struct QCryptoBlockLUKS QCryptoBlockLUKS
;
49 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader
;
50 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot
;
53 /* The following constants are all defined by the LUKS spec */
54 #define QCRYPTO_BLOCK_LUKS_VERSION 1
56 #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6
57 #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32
58 #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32
59 #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32
60 #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20
61 #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32
62 #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40
63 #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8
64 #define QCRYPTO_BLOCK_LUKS_STRIPES 4000
65 #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000
66 #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000
67 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096
69 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
70 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
72 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
74 static const char qcrypto_block_luks_magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
] = {
75 'L', 'U', 'K', 'S', 0xBA, 0xBE
78 typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap
;
79 struct QCryptoBlockLUKSNameMap
{
84 typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap
;
85 struct QCryptoBlockLUKSCipherSizeMap
{
89 typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap
;
90 struct QCryptoBlockLUKSCipherNameMap
{
92 const QCryptoBlockLUKSCipherSizeMap
*sizes
;
96 static const QCryptoBlockLUKSCipherSizeMap
97 qcrypto_block_luks_cipher_size_map_aes
[] = {
98 { 16, QCRYPTO_CIPHER_ALG_AES_128
},
99 { 24, QCRYPTO_CIPHER_ALG_AES_192
},
100 { 32, QCRYPTO_CIPHER_ALG_AES_256
},
104 static const QCryptoBlockLUKSCipherSizeMap
105 qcrypto_block_luks_cipher_size_map_cast5
[] = {
106 { 16, QCRYPTO_CIPHER_ALG_CAST5_128
},
110 static const QCryptoBlockLUKSCipherSizeMap
111 qcrypto_block_luks_cipher_size_map_serpent
[] = {
112 { 16, QCRYPTO_CIPHER_ALG_SERPENT_128
},
113 { 24, QCRYPTO_CIPHER_ALG_SERPENT_192
},
114 { 32, QCRYPTO_CIPHER_ALG_SERPENT_256
},
118 static const QCryptoBlockLUKSCipherSizeMap
119 qcrypto_block_luks_cipher_size_map_twofish
[] = {
120 { 16, QCRYPTO_CIPHER_ALG_TWOFISH_128
},
121 { 24, QCRYPTO_CIPHER_ALG_TWOFISH_192
},
122 { 32, QCRYPTO_CIPHER_ALG_TWOFISH_256
},
126 static const QCryptoBlockLUKSCipherNameMap
127 qcrypto_block_luks_cipher_name_map
[] = {
128 { "aes", qcrypto_block_luks_cipher_size_map_aes
},
129 { "cast5", qcrypto_block_luks_cipher_size_map_cast5
},
130 { "serpent", qcrypto_block_luks_cipher_size_map_serpent
},
131 { "twofish", qcrypto_block_luks_cipher_size_map_twofish
},
136 * This struct is written to disk in big-endian format,
137 * but operated upon in native-endian format.
139 struct QCryptoBlockLUKSKeySlot
{
140 /* state of keyslot, enabled/disable */
142 /* iterations for PBKDF2 */
144 /* salt for PBKDF2 */
145 uint8_t salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
146 /* start sector of key material */
148 /* number of anti-forensic stripes */
152 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot
) != 48);
156 * This struct is written to disk in big-endian format,
157 * but operated upon in native-endian format.
159 struct QCryptoBlockLUKSHeader
{
160 /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
161 char magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
];
163 /* LUKS version, currently 1 */
166 /* cipher name specification (aes, etc) */
167 char cipher_name
[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
];
169 /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
170 char cipher_mode
[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
];
172 /* hash specification (sha256, etc) */
173 char hash_spec
[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
];
175 /* start offset of the volume data (in 512 byte sectors) */
176 uint32_t payload_offset
;
178 /* Number of key bytes */
181 /* master key checksum after PBKDF2 */
182 uint8_t master_key_digest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
184 /* salt for master key PBKDF2 */
185 uint8_t master_key_salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
187 /* iterations for master key PBKDF2 */
188 uint32_t master_key_iterations
;
190 /* UUID of the partition in standard ASCII representation */
191 uint8_t uuid
[QCRYPTO_BLOCK_LUKS_UUID_LEN
];
194 QCryptoBlockLUKSKeySlot key_slots
[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
];
197 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader
) != 592);
200 struct QCryptoBlockLUKS
{
201 QCryptoBlockLUKSHeader header
;
205 static int qcrypto_block_luks_cipher_name_lookup(const char *name
,
206 QCryptoCipherMode mode
,
210 const QCryptoBlockLUKSCipherNameMap
*map
=
211 qcrypto_block_luks_cipher_name_map
;
212 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
215 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
219 for (i
= 0; i
< maplen
; i
++) {
220 if (!g_str_equal(map
[i
].name
, name
)) {
223 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
224 if (map
[i
].sizes
[j
].key_bytes
== key_bytes
) {
225 return map
[i
].sizes
[j
].id
;
230 error_setg(errp
, "Algorithm %s with key size %d bytes not supported",
236 qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg
,
239 const QCryptoBlockLUKSCipherNameMap
*map
=
240 qcrypto_block_luks_cipher_name_map
;
241 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
243 for (i
= 0; i
< maplen
; i
++) {
244 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
245 if (map
[i
].sizes
[j
].id
== alg
) {
251 error_setg(errp
, "Algorithm '%s' not supported",
252 QCryptoCipherAlgorithm_lookup
[alg
]);
256 /* XXX replace with qapi_enum_parse() in future, when we can
257 * make that function emit a more friendly error message */
258 static int qcrypto_block_luks_name_lookup(const char *name
,
259 const char *const *map
,
265 for (i
= 0; i
< maplen
; i
++) {
266 if (g_str_equal(map
[i
], name
)) {
271 error_setg(errp
, "%s %s not supported", type
, name
);
275 #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
276 qcrypto_block_luks_name_lookup(name, \
277 QCryptoCipherMode_lookup, \
278 QCRYPTO_CIPHER_MODE__MAX, \
282 #define qcrypto_block_luks_hash_name_lookup(name, errp) \
283 qcrypto_block_luks_name_lookup(name, \
284 QCryptoHashAlgorithm_lookup, \
285 QCRYPTO_HASH_ALG__MAX, \
289 #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
290 qcrypto_block_luks_name_lookup(name, \
291 QCryptoIVGenAlgorithm_lookup, \
292 QCRYPTO_IVGEN_ALG__MAX, \
298 qcrypto_block_luks_has_format(const uint8_t *buf
,
301 const QCryptoBlockLUKSHeader
*luks_header
= (const void *)buf
;
303 if (buf_size
>= offsetof(QCryptoBlockLUKSHeader
, cipher_name
) &&
304 memcmp(luks_header
->magic
, qcrypto_block_luks_magic
,
305 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) == 0 &&
306 be16_to_cpu(luks_header
->version
) == QCRYPTO_BLOCK_LUKS_VERSION
) {
315 * Deal with a quirk of dm-crypt usage of ESSIV.
317 * When calculating ESSIV IVs, the cipher length used by ESSIV
318 * may be different from the cipher length used for the block
319 * encryption, becauses dm-crypt uses the hash digest length
320 * as the key size. ie, if you have AES 128 as the block cipher
321 * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
322 * the cipher since that gets a key length matching the digest
323 * size, not AES 128 with truncated digest as might be imagined
325 static QCryptoCipherAlgorithm
326 qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher
,
327 QCryptoHashAlgorithm hash
,
330 size_t digestlen
= qcrypto_hash_digest_len(hash
);
331 size_t keylen
= qcrypto_cipher_get_key_len(cipher
);
332 if (digestlen
== keylen
) {
337 case QCRYPTO_CIPHER_ALG_AES_128
:
338 case QCRYPTO_CIPHER_ALG_AES_192
:
339 case QCRYPTO_CIPHER_ALG_AES_256
:
340 if (digestlen
== qcrypto_cipher_get_key_len(
341 QCRYPTO_CIPHER_ALG_AES_128
)) {
342 return QCRYPTO_CIPHER_ALG_AES_128
;
343 } else if (digestlen
== qcrypto_cipher_get_key_len(
344 QCRYPTO_CIPHER_ALG_AES_192
)) {
345 return QCRYPTO_CIPHER_ALG_AES_192
;
346 } else if (digestlen
== qcrypto_cipher_get_key_len(
347 QCRYPTO_CIPHER_ALG_AES_256
)) {
348 return QCRYPTO_CIPHER_ALG_AES_256
;
350 error_setg(errp
, "No AES cipher with key size %zu available",
355 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
356 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
357 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
358 if (digestlen
== qcrypto_cipher_get_key_len(
359 QCRYPTO_CIPHER_ALG_SERPENT_128
)) {
360 return QCRYPTO_CIPHER_ALG_SERPENT_128
;
361 } else if (digestlen
== qcrypto_cipher_get_key_len(
362 QCRYPTO_CIPHER_ALG_SERPENT_192
)) {
363 return QCRYPTO_CIPHER_ALG_SERPENT_192
;
364 } else if (digestlen
== qcrypto_cipher_get_key_len(
365 QCRYPTO_CIPHER_ALG_SERPENT_256
)) {
366 return QCRYPTO_CIPHER_ALG_SERPENT_256
;
368 error_setg(errp
, "No Serpent cipher with key size %zu available",
373 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
374 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
375 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
376 if (digestlen
== qcrypto_cipher_get_key_len(
377 QCRYPTO_CIPHER_ALG_TWOFISH_128
)) {
378 return QCRYPTO_CIPHER_ALG_TWOFISH_128
;
379 } else if (digestlen
== qcrypto_cipher_get_key_len(
380 QCRYPTO_CIPHER_ALG_TWOFISH_192
)) {
381 return QCRYPTO_CIPHER_ALG_TWOFISH_192
;
382 } else if (digestlen
== qcrypto_cipher_get_key_len(
383 QCRYPTO_CIPHER_ALG_TWOFISH_256
)) {
384 return QCRYPTO_CIPHER_ALG_TWOFISH_256
;
386 error_setg(errp
, "No Twofish cipher with key size %zu available",
392 error_setg(errp
, "Cipher %s not supported with essiv",
393 QCryptoCipherAlgorithm_lookup
[cipher
]);
399 * Given a key slot, and user password, this will attempt to unlock
400 * the master encryption key from the key slot.
403 * 0 if the key slot is disabled, or key could not be decrypted
404 * with the provided password
405 * 1 if the key slot is enabled, and key decrypted successfully
406 * with the provided password
407 * -1 if a fatal error occurred loading the key
410 qcrypto_block_luks_load_key(QCryptoBlock
*block
,
411 QCryptoBlockLUKSKeySlot
*slot
,
412 const char *password
,
413 QCryptoCipherAlgorithm cipheralg
,
414 QCryptoCipherMode ciphermode
,
415 QCryptoHashAlgorithm hash
,
416 QCryptoIVGenAlgorithm ivalg
,
417 QCryptoCipherAlgorithm ivcipheralg
,
418 QCryptoHashAlgorithm ivhash
,
421 QCryptoBlockReadFunc readfunc
,
425 QCryptoBlockLUKS
*luks
= block
->opaque
;
428 uint8_t *possiblekey
;
431 QCryptoCipher
*cipher
= NULL
;
432 uint8_t keydigest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
433 QCryptoIVGen
*ivgen
= NULL
;
436 if (slot
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
) {
440 splitkeylen
= masterkeylen
* slot
->stripes
;
441 splitkey
= g_new0(uint8_t, splitkeylen
);
442 possiblekey
= g_new0(uint8_t, masterkeylen
);
445 * The user password is used to generate a (possible)
446 * decryption key. This may or may not successfully
447 * decrypt the master key - we just blindly assume
448 * the key is correct and validate the results of
451 if (qcrypto_pbkdf2(hash
,
452 (const uint8_t *)password
, strlen(password
),
453 slot
->salt
, QCRYPTO_BLOCK_LUKS_SALT_LEN
,
455 possiblekey
, masterkeylen
,
461 * We need to read the master key material from the
462 * LUKS key material header. What we're reading is
463 * not the raw master key, but rather the data after
464 * it has been passed through AFSplit and the result
468 slot
->key_offset
* QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
469 splitkey
, splitkeylen
,
477 /* Setup the cipher/ivgen that we'll use to try to decrypt
478 * the split master key material */
479 cipher
= qcrypto_cipher_new(cipheralg
, ciphermode
,
480 possiblekey
, masterkeylen
,
486 niv
= qcrypto_cipher_get_iv_len(cipheralg
,
488 ivgen
= qcrypto_ivgen_new(ivalg
,
491 possiblekey
, masterkeylen
,
499 * The master key needs to be decrypted in the same
500 * way that the block device payload will be decrypted
501 * later. In particular we'll be using the IV generator
502 * to reset the encryption cipher every time the master
503 * key crosses a sector boundary.
505 if (qcrypto_block_decrypt_helper(cipher
,
508 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
517 * Now we've decrypted the split master key, join
518 * it back together to get the actual master key.
520 if (qcrypto_afsplit_decode(hash
,
531 * We still don't know that the masterkey we got is valid,
532 * because we just blindly assumed the user's password
533 * was correct. This is where we now verify it. We are
534 * creating a hash of the master key using PBKDF and
535 * then comparing that to the hash stored in the key slot
538 if (qcrypto_pbkdf2(hash
,
539 masterkey
, masterkeylen
,
540 luks
->header
.master_key_salt
,
541 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
542 luks
->header
.master_key_iterations
,
543 keydigest
, G_N_ELEMENTS(keydigest
),
548 if (memcmp(keydigest
, luks
->header
.master_key_digest
,
549 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
) == 0) {
550 /* Success, we got the right master key */
555 /* Fail, user's password was not valid for this key slot,
556 * tell caller to try another slot */
560 qcrypto_ivgen_free(ivgen
);
561 qcrypto_cipher_free(cipher
);
569 * Given a user password, this will iterate over all key
570 * slots and try to unlock each active key slot using the
571 * password until it successfully obtains a master key.
573 * Returns 0 if a key was loaded, -1 if no keys could be loaded
576 qcrypto_block_luks_find_key(QCryptoBlock
*block
,
577 const char *password
,
578 QCryptoCipherAlgorithm cipheralg
,
579 QCryptoCipherMode ciphermode
,
580 QCryptoHashAlgorithm hash
,
581 QCryptoIVGenAlgorithm ivalg
,
582 QCryptoCipherAlgorithm ivcipheralg
,
583 QCryptoHashAlgorithm ivhash
,
585 size_t *masterkeylen
,
586 QCryptoBlockReadFunc readfunc
,
590 QCryptoBlockLUKS
*luks
= block
->opaque
;
594 *masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
595 *masterkeylen
= luks
->header
.key_bytes
;
597 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
598 rv
= qcrypto_block_luks_load_key(block
,
599 &luks
->header
.key_slots
[i
],
620 error_setg(errp
, "Invalid password, cannot unlock any keyslot");
631 qcrypto_block_luks_open(QCryptoBlock
*block
,
632 QCryptoBlockOpenOptions
*options
,
633 QCryptoBlockReadFunc readfunc
,
638 QCryptoBlockLUKS
*luks
;
639 Error
*local_err
= NULL
;
643 uint8_t *masterkey
= NULL
;
645 char *ivgen_name
, *ivhash_name
;
646 QCryptoCipherMode ciphermode
;
647 QCryptoCipherAlgorithm cipheralg
;
648 QCryptoIVGenAlgorithm ivalg
;
649 QCryptoCipherAlgorithm ivcipheralg
;
650 QCryptoHashAlgorithm hash
;
651 QCryptoHashAlgorithm ivhash
;
652 char *password
= NULL
;
654 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
655 if (!options
->u
.luks
.key_secret
) {
656 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
659 password
= qcrypto_secret_lookup_as_utf8(
660 options
->u
.luks
.key_secret
, errp
);
666 luks
= g_new0(QCryptoBlockLUKS
, 1);
667 block
->opaque
= luks
;
669 /* Read the entire LUKS header, minus the key material from
670 * the underlying device */
671 rv
= readfunc(block
, 0,
672 (uint8_t *)&luks
->header
,
673 sizeof(luks
->header
),
681 /* The header is always stored in big-endian format, so
682 * convert everything to native */
683 be16_to_cpus(&luks
->header
.version
);
684 be32_to_cpus(&luks
->header
.payload_offset
);
685 be32_to_cpus(&luks
->header
.key_bytes
);
686 be32_to_cpus(&luks
->header
.master_key_iterations
);
688 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
689 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
690 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
691 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
692 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
695 if (memcmp(luks
->header
.magic
, qcrypto_block_luks_magic
,
696 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) != 0) {
697 error_setg(errp
, "Volume is not in LUKS format");
701 if (luks
->header
.version
!= QCRYPTO_BLOCK_LUKS_VERSION
) {
702 error_setg(errp
, "LUKS version %" PRIu32
" is not supported",
703 luks
->header
.version
);
709 * The cipher_mode header contains a string that we have
710 * to further parse, of the format
712 * <cipher-mode>-<iv-generator>[:<iv-hash>]
714 * eg cbc-essiv:sha256, cbc-plain64
716 ivgen_name
= strchr(luks
->header
.cipher_mode
, '-');
719 error_setg(errp
, "Unexpected cipher mode string format %s",
720 luks
->header
.cipher_mode
);
726 ivhash_name
= strchr(ivgen_name
, ':');
733 ivhash
= qcrypto_block_luks_hash_name_lookup(ivhash_name
,
737 error_propagate(errp
, local_err
);
742 ciphermode
= qcrypto_block_luks_cipher_mode_lookup(luks
->header
.cipher_mode
,
746 error_propagate(errp
, local_err
);
750 cipheralg
= qcrypto_block_luks_cipher_name_lookup(luks
->header
.cipher_name
,
752 luks
->header
.key_bytes
,
756 error_propagate(errp
, local_err
);
760 hash
= qcrypto_block_luks_hash_name_lookup(luks
->header
.hash_spec
,
764 error_propagate(errp
, local_err
);
768 ivalg
= qcrypto_block_luks_ivgen_name_lookup(ivgen_name
,
772 error_propagate(errp
, local_err
);
776 if (ivalg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
777 ivcipheralg
= qcrypto_block_luks_essiv_cipher(cipheralg
,
782 error_propagate(errp
, local_err
);
786 ivcipheralg
= cipheralg
;
789 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
790 /* Try to find which key slot our password is valid for
791 * and unlock the master key from that slot.
793 if (qcrypto_block_luks_find_key(block
,
795 cipheralg
, ciphermode
,
800 &masterkey
, &masterkeylen
,
807 /* We have a valid master key now, so can setup the
808 * block device payload decryption objects
810 block
->kdfhash
= hash
;
811 block
->niv
= qcrypto_cipher_get_iv_len(cipheralg
,
813 block
->ivgen
= qcrypto_ivgen_new(ivalg
,
816 masterkey
, masterkeylen
,
823 block
->cipher
= qcrypto_cipher_new(cipheralg
,
825 masterkey
, masterkeylen
,
827 if (!block
->cipher
) {
833 block
->payload_offset
= luks
->header
.payload_offset
*
834 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
843 qcrypto_cipher_free(block
->cipher
);
844 qcrypto_ivgen_free(block
->ivgen
);
852 qcrypto_block_luks_uuid_gen(uint8_t *uuidstr
, Error
**errp
)
857 uuid_unparse(uuid
, (char *)uuidstr
);
860 error_setg(errp
, "Unable to generate uuids on this platform");
866 qcrypto_block_luks_create(QCryptoBlock
*block
,
867 QCryptoBlockCreateOptions
*options
,
868 QCryptoBlockInitFunc initfunc
,
869 QCryptoBlockWriteFunc writefunc
,
873 QCryptoBlockLUKS
*luks
;
874 QCryptoBlockCreateOptionsLUKS luks_opts
;
875 Error
*local_err
= NULL
;
876 uint8_t *masterkey
= NULL
;
877 uint8_t *slotkey
= NULL
;
878 uint8_t *splitkey
= NULL
;
879 size_t splitkeylen
= 0;
881 QCryptoCipher
*cipher
= NULL
;
882 QCryptoIVGen
*ivgen
= NULL
;
884 const char *cipher_alg
;
885 const char *cipher_mode
;
886 const char *ivgen_alg
;
887 const char *ivgen_hash_alg
= NULL
;
888 const char *hash_alg
;
889 char *cipher_mode_spec
= NULL
;
890 QCryptoCipherAlgorithm ivcipheralg
= 0;
892 memcpy(&luks_opts
, &options
->u
.luks
, sizeof(luks_opts
));
893 if (!luks_opts
.has_cipher_alg
) {
894 luks_opts
.cipher_alg
= QCRYPTO_CIPHER_ALG_AES_256
;
896 if (!luks_opts
.has_cipher_mode
) {
897 luks_opts
.cipher_mode
= QCRYPTO_CIPHER_MODE_XTS
;
899 if (!luks_opts
.has_ivgen_alg
) {
900 luks_opts
.ivgen_alg
= QCRYPTO_IVGEN_ALG_PLAIN64
;
902 if (!luks_opts
.has_hash_alg
) {
903 luks_opts
.hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
906 if (!options
->u
.luks
.key_secret
) {
907 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
910 password
= qcrypto_secret_lookup_as_utf8(luks_opts
.key_secret
, errp
);
915 luks
= g_new0(QCryptoBlockLUKS
, 1);
916 block
->opaque
= luks
;
918 memcpy(luks
->header
.magic
, qcrypto_block_luks_magic
,
919 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
);
921 /* We populate the header in native endianness initially and
922 * then convert everything to big endian just before writing
925 luks
->header
.version
= QCRYPTO_BLOCK_LUKS_VERSION
;
926 if (qcrypto_block_luks_uuid_gen(luks
->header
.uuid
,
931 cipher_alg
= qcrypto_block_luks_cipher_alg_lookup(luks_opts
.cipher_alg
,
937 cipher_mode
= QCryptoCipherMode_lookup
[luks_opts
.cipher_mode
];
938 ivgen_alg
= QCryptoIVGenAlgorithm_lookup
[luks_opts
.ivgen_alg
];
939 if (luks_opts
.has_ivgen_hash_alg
) {
940 ivgen_hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.ivgen_hash_alg
];
941 cipher_mode_spec
= g_strdup_printf("%s-%s:%s", cipher_mode
, ivgen_alg
,
944 cipher_mode_spec
= g_strdup_printf("%s-%s", cipher_mode
, ivgen_alg
);
946 hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.hash_alg
];
949 if (strlen(cipher_alg
) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
) {
950 error_setg(errp
, "Cipher name '%s' is too long for LUKS header",
954 if (strlen(cipher_mode_spec
) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
) {
955 error_setg(errp
, "Cipher mode '%s' is too long for LUKS header",
959 if (strlen(hash_alg
) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
) {
960 error_setg(errp
, "Hash name '%s' is too long for LUKS header",
965 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
966 ivcipheralg
= qcrypto_block_luks_essiv_cipher(luks_opts
.cipher_alg
,
967 luks_opts
.ivgen_hash_alg
,
970 error_propagate(errp
, local_err
);
974 ivcipheralg
= luks_opts
.cipher_alg
;
977 strcpy(luks
->header
.cipher_name
, cipher_alg
);
978 strcpy(luks
->header
.cipher_mode
, cipher_mode_spec
);
979 strcpy(luks
->header
.hash_spec
, hash_alg
);
981 luks
->header
.key_bytes
= qcrypto_cipher_get_key_len(luks_opts
.cipher_alg
);
982 if (luks_opts
.cipher_mode
== QCRYPTO_CIPHER_MODE_XTS
) {
983 luks
->header
.key_bytes
*= 2;
986 /* Generate the salt used for hashing the master key
989 if (qcrypto_random_bytes(luks
->header
.master_key_salt
,
990 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
995 /* Generate random master key */
996 masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
997 if (qcrypto_random_bytes(masterkey
,
998 luks
->header
.key_bytes
, errp
) < 0) {
1003 /* Setup the block device payload encryption objects */
1004 block
->cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1005 luks_opts
.cipher_mode
,
1006 masterkey
, luks
->header
.key_bytes
,
1008 if (!block
->cipher
) {
1012 block
->kdfhash
= luks_opts
.hash_alg
;
1013 block
->niv
= qcrypto_cipher_get_iv_len(luks_opts
.cipher_alg
,
1014 luks_opts
.cipher_mode
);
1015 block
->ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1017 luks_opts
.ivgen_hash_alg
,
1018 masterkey
, luks
->header
.key_bytes
,
1021 if (!block
->ivgen
) {
1026 /* Determine how many iterations we need to hash the master
1027 * key, in order to have 1 second of compute time used
1029 luks
->header
.master_key_iterations
=
1030 qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1031 masterkey
, luks
->header
.key_bytes
,
1032 luks
->header
.master_key_salt
,
1033 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1036 error_propagate(errp
, local_err
);
1040 /* Why /= 8 ? That matches cryptsetup, but there's no
1041 * explanation why they chose /= 8... Probably so that
1042 * if all 8 keyslots are active we only spend 1 second
1043 * in total time to check all keys */
1044 luks
->header
.master_key_iterations
/= 8;
1045 luks
->header
.master_key_iterations
= MAX(
1046 luks
->header
.master_key_iterations
,
1047 QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS
);
1050 /* Hash the master key, saving the result in the LUKS
1051 * header. This hash is used when opening the encrypted
1052 * device to verify that the user password unlocked a
1055 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1056 masterkey
, luks
->header
.key_bytes
,
1057 luks
->header
.master_key_salt
,
1058 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1059 luks
->header
.master_key_iterations
,
1060 luks
->header
.master_key_digest
,
1061 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1067 /* Although LUKS has multiple key slots, we're just going
1068 * to use the first key slot */
1069 splitkeylen
= luks
->header
.key_bytes
* QCRYPTO_BLOCK_LUKS_STRIPES
;
1070 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1071 luks
->header
.key_slots
[i
].active
= i
== 0 ?
1072 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
:
1073 QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
;
1074 luks
->header
.key_slots
[i
].stripes
= QCRYPTO_BLOCK_LUKS_STRIPES
;
1076 /* This calculation doesn't match that shown in the spec,
1077 * but instead follows the cryptsetup implementation.
1079 luks
->header
.key_slots
[i
].key_offset
=
1080 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1081 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1082 (ROUND_UP(((splitkeylen
+ (QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
- 1)) /
1083 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1084 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1085 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) * i
);
1088 if (qcrypto_random_bytes(luks
->header
.key_slots
[0].salt
,
1089 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1094 /* Again we determine how many iterations are required to
1095 * hash the user password while consuming 1 second of compute
1097 luks
->header
.key_slots
[0].iterations
=
1098 qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1099 (uint8_t *)password
, strlen(password
),
1100 luks
->header
.key_slots
[0].salt
,
1101 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1104 error_propagate(errp
, local_err
);
1107 /* Why /= 2 ? That matches cryptsetup, but there's no
1108 * explanation why they chose /= 2... */
1109 luks
->header
.key_slots
[0].iterations
/= 2;
1110 luks
->header
.key_slots
[0].iterations
= MAX(
1111 luks
->header
.key_slots
[0].iterations
,
1112 QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS
);
1115 /* Generate a key that we'll use to encrypt the master
1116 * key, from the user's password
1118 slotkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
1119 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1120 (uint8_t *)password
, strlen(password
),
1121 luks
->header
.key_slots
[0].salt
,
1122 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1123 luks
->header
.key_slots
[0].iterations
,
1124 slotkey
, luks
->header
.key_bytes
,
1130 /* Setup the encryption objects needed to encrypt the
1131 * master key material
1133 cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1134 luks_opts
.cipher_mode
,
1135 slotkey
, luks
->header
.key_bytes
,
1141 ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1143 luks_opts
.ivgen_hash_alg
,
1144 slotkey
, luks
->header
.key_bytes
,
1150 /* Before storing the master key, we need to vastly
1151 * increase its size, as protection against forensic
1152 * disk data recovery */
1153 splitkey
= g_new0(uint8_t, splitkeylen
);
1155 if (qcrypto_afsplit_encode(luks_opts
.hash_alg
,
1156 luks
->header
.key_bytes
,
1157 luks
->header
.key_slots
[0].stripes
,
1164 /* Now we encrypt the split master key with the key generated
1165 * from the user's password, before storing it */
1166 if (qcrypto_block_encrypt_helper(cipher
, block
->niv
, ivgen
,
1167 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1176 /* The total size of the LUKS headers is the partition header + key
1177 * slot headers, rounded up to the nearest sector, combined with
1178 * the size of each master key material region, also rounded up
1179 * to the nearest sector */
1180 luks
->header
.payload_offset
=
1181 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1182 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1183 (ROUND_UP(((splitkeylen
+ (QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
- 1)) /
1184 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1185 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1186 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) *
1187 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1189 block
->payload_offset
= luks
->header
.payload_offset
*
1190 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1192 /* Reserve header space to match payload offset */
1193 initfunc(block
, block
->payload_offset
, &local_err
, opaque
);
1195 error_propagate(errp
, local_err
);
1199 /* Everything on disk uses Big Endian, so flip header fields
1200 * before writing them */
1201 cpu_to_be16s(&luks
->header
.version
);
1202 cpu_to_be32s(&luks
->header
.payload_offset
);
1203 cpu_to_be32s(&luks
->header
.key_bytes
);
1204 cpu_to_be32s(&luks
->header
.master_key_iterations
);
1206 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1207 cpu_to_be32s(&luks
->header
.key_slots
[i
].active
);
1208 cpu_to_be32s(&luks
->header
.key_slots
[i
].iterations
);
1209 cpu_to_be32s(&luks
->header
.key_slots
[i
].key_offset
);
1210 cpu_to_be32s(&luks
->header
.key_slots
[i
].stripes
);
1214 /* Write out the partition header and key slot headers */
1216 (const uint8_t *)&luks
->header
,
1217 sizeof(luks
->header
),
1221 /* Delay checking local_err until we've byte-swapped */
1223 /* Byte swap the header back to native, in case we need
1224 * to read it again later */
1225 be16_to_cpus(&luks
->header
.version
);
1226 be32_to_cpus(&luks
->header
.payload_offset
);
1227 be32_to_cpus(&luks
->header
.key_bytes
);
1228 be32_to_cpus(&luks
->header
.master_key_iterations
);
1230 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1231 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
1232 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
1233 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
1234 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
1238 error_propagate(errp
, local_err
);
1242 /* Write out the master key material, starting at the
1243 * sector immediately following the partition header. */
1244 if (writefunc(block
,
1245 luks
->header
.key_slots
[0].key_offset
*
1246 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1247 splitkey
, splitkeylen
,
1249 opaque
) != splitkeylen
) {
1253 memset(masterkey
, 0, luks
->header
.key_bytes
);
1255 memset(slotkey
, 0, luks
->header
.key_bytes
);
1259 g_free(cipher_mode_spec
);
1261 qcrypto_ivgen_free(ivgen
);
1262 qcrypto_cipher_free(cipher
);
1268 memset(masterkey
, 0, luks
->header
.key_bytes
);
1272 memset(slotkey
, 0, luks
->header
.key_bytes
);
1277 g_free(cipher_mode_spec
);
1279 qcrypto_ivgen_free(ivgen
);
1280 qcrypto_cipher_free(cipher
);
1287 static void qcrypto_block_luks_cleanup(QCryptoBlock
*block
)
1289 g_free(block
->opaque
);
1294 qcrypto_block_luks_decrypt(QCryptoBlock
*block
,
1295 uint64_t startsector
,
1300 return qcrypto_block_decrypt_helper(block
->cipher
,
1301 block
->niv
, block
->ivgen
,
1302 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1303 startsector
, buf
, len
, errp
);
1308 qcrypto_block_luks_encrypt(QCryptoBlock
*block
,
1309 uint64_t startsector
,
1314 return qcrypto_block_encrypt_helper(block
->cipher
,
1315 block
->niv
, block
->ivgen
,
1316 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1317 startsector
, buf
, len
, errp
);
1321 const QCryptoBlockDriver qcrypto_block_driver_luks
= {
1322 .open
= qcrypto_block_luks_open
,
1323 .create
= qcrypto_block_luks_create
,
1324 .cleanup
= qcrypto_block_luks_cleanup
,
1325 .decrypt
= qcrypto_block_luks_decrypt
,
1326 .encrypt
= qcrypto_block_luks_encrypt
,
1327 .has_format
= qcrypto_block_luks_has_format
,