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"
24 #include "crypto/block-luks.h"
26 #include "crypto/hash.h"
27 #include "crypto/afsplit.h"
28 #include "crypto/pbkdf.h"
29 #include "crypto/secret.h"
30 #include "crypto/random.h"
33 #include <uuid/uuid.h>
36 #include "qemu/coroutine.h"
39 * Reference for the LUKS format implemented here is
41 * docs/on-disk-format.pdf
43 * in 'cryptsetup' package source code
45 * This file implements the 1.2.1 specification, dated
49 typedef struct QCryptoBlockLUKS QCryptoBlockLUKS
;
50 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader
;
51 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot
;
54 /* The following constants are all defined by the LUKS spec */
55 #define QCRYPTO_BLOCK_LUKS_VERSION 1
57 #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6
58 #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32
59 #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32
60 #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32
61 #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20
62 #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32
63 #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40
64 #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8
65 #define QCRYPTO_BLOCK_LUKS_STRIPES 4000
66 #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000
67 #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000
68 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096
70 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
71 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
73 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
75 static const char qcrypto_block_luks_magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
] = {
76 'L', 'U', 'K', 'S', 0xBA, 0xBE
79 typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap
;
80 struct QCryptoBlockLUKSNameMap
{
85 typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap
;
86 struct QCryptoBlockLUKSCipherSizeMap
{
90 typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap
;
91 struct QCryptoBlockLUKSCipherNameMap
{
93 const QCryptoBlockLUKSCipherSizeMap
*sizes
;
97 static const QCryptoBlockLUKSCipherSizeMap
98 qcrypto_block_luks_cipher_size_map_aes
[] = {
99 { 16, QCRYPTO_CIPHER_ALG_AES_128
},
100 { 24, QCRYPTO_CIPHER_ALG_AES_192
},
101 { 32, QCRYPTO_CIPHER_ALG_AES_256
},
105 static const QCryptoBlockLUKSCipherSizeMap
106 qcrypto_block_luks_cipher_size_map_cast5
[] = {
107 { 16, QCRYPTO_CIPHER_ALG_CAST5_128
},
111 static const QCryptoBlockLUKSCipherSizeMap
112 qcrypto_block_luks_cipher_size_map_serpent
[] = {
113 { 16, QCRYPTO_CIPHER_ALG_SERPENT_128
},
114 { 24, QCRYPTO_CIPHER_ALG_SERPENT_192
},
115 { 32, QCRYPTO_CIPHER_ALG_SERPENT_256
},
119 static const QCryptoBlockLUKSCipherSizeMap
120 qcrypto_block_luks_cipher_size_map_twofish
[] = {
121 { 16, QCRYPTO_CIPHER_ALG_TWOFISH_128
},
122 { 24, QCRYPTO_CIPHER_ALG_TWOFISH_192
},
123 { 32, QCRYPTO_CIPHER_ALG_TWOFISH_256
},
127 static const QCryptoBlockLUKSCipherNameMap
128 qcrypto_block_luks_cipher_name_map
[] = {
129 { "aes", qcrypto_block_luks_cipher_size_map_aes
},
130 { "cast5", qcrypto_block_luks_cipher_size_map_cast5
},
131 { "serpent", qcrypto_block_luks_cipher_size_map_serpent
},
132 { "twofish", qcrypto_block_luks_cipher_size_map_twofish
},
137 * This struct is written to disk in big-endian format,
138 * but operated upon in native-endian format.
140 struct QCryptoBlockLUKSKeySlot
{
141 /* state of keyslot, enabled/disable */
143 /* iterations for PBKDF2 */
145 /* salt for PBKDF2 */
146 uint8_t salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
147 /* start sector of key material */
149 /* number of anti-forensic stripes */
153 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot
) != 48);
157 * This struct is written to disk in big-endian format,
158 * but operated upon in native-endian format.
160 struct QCryptoBlockLUKSHeader
{
161 /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
162 char magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
];
164 /* LUKS version, currently 1 */
167 /* cipher name specification (aes, etc) */
168 char cipher_name
[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
];
170 /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
171 char cipher_mode
[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
];
173 /* hash specification (sha256, etc) */
174 char hash_spec
[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
];
176 /* start offset of the volume data (in 512 byte sectors) */
177 uint32_t payload_offset
;
179 /* Number of key bytes */
182 /* master key checksum after PBKDF2 */
183 uint8_t master_key_digest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
185 /* salt for master key PBKDF2 */
186 uint8_t master_key_salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
188 /* iterations for master key PBKDF2 */
189 uint32_t master_key_iterations
;
191 /* UUID of the partition in standard ASCII representation */
192 uint8_t uuid
[QCRYPTO_BLOCK_LUKS_UUID_LEN
];
195 QCryptoBlockLUKSKeySlot key_slots
[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
];
198 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader
) != 592);
201 struct QCryptoBlockLUKS
{
202 QCryptoBlockLUKSHeader header
;
206 static int qcrypto_block_luks_cipher_name_lookup(const char *name
,
207 QCryptoCipherMode mode
,
211 const QCryptoBlockLUKSCipherNameMap
*map
=
212 qcrypto_block_luks_cipher_name_map
;
213 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
216 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
220 for (i
= 0; i
< maplen
; i
++) {
221 if (!g_str_equal(map
[i
].name
, name
)) {
224 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
225 if (map
[i
].sizes
[j
].key_bytes
== key_bytes
) {
226 return map
[i
].sizes
[j
].id
;
231 error_setg(errp
, "Algorithm %s with key size %d bytes not supported",
237 qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg
,
240 const QCryptoBlockLUKSCipherNameMap
*map
=
241 qcrypto_block_luks_cipher_name_map
;
242 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
244 for (i
= 0; i
< maplen
; i
++) {
245 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
246 if (map
[i
].sizes
[j
].id
== alg
) {
252 error_setg(errp
, "Algorithm '%s' not supported",
253 QCryptoCipherAlgorithm_lookup
[alg
]);
257 /* XXX replace with qapi_enum_parse() in future, when we can
258 * make that function emit a more friendly error message */
259 static int qcrypto_block_luks_name_lookup(const char *name
,
260 const char *const *map
,
266 for (i
= 0; i
< maplen
; i
++) {
267 if (g_str_equal(map
[i
], name
)) {
272 error_setg(errp
, "%s %s not supported", type
, name
);
276 #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
277 qcrypto_block_luks_name_lookup(name, \
278 QCryptoCipherMode_lookup, \
279 QCRYPTO_CIPHER_MODE__MAX, \
283 #define qcrypto_block_luks_hash_name_lookup(name, errp) \
284 qcrypto_block_luks_name_lookup(name, \
285 QCryptoHashAlgorithm_lookup, \
286 QCRYPTO_HASH_ALG__MAX, \
290 #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
291 qcrypto_block_luks_name_lookup(name, \
292 QCryptoIVGenAlgorithm_lookup, \
293 QCRYPTO_IVGEN_ALG__MAX, \
299 qcrypto_block_luks_has_format(const uint8_t *buf
,
302 const QCryptoBlockLUKSHeader
*luks_header
= (const void *)buf
;
304 if (buf_size
>= offsetof(QCryptoBlockLUKSHeader
, cipher_name
) &&
305 memcmp(luks_header
->magic
, qcrypto_block_luks_magic
,
306 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) == 0 &&
307 be16_to_cpu(luks_header
->version
) == QCRYPTO_BLOCK_LUKS_VERSION
) {
316 * Deal with a quirk of dm-crypt usage of ESSIV.
318 * When calculating ESSIV IVs, the cipher length used by ESSIV
319 * may be different from the cipher length used for the block
320 * encryption, becauses dm-crypt uses the hash digest length
321 * as the key size. ie, if you have AES 128 as the block cipher
322 * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
323 * the cipher since that gets a key length matching the digest
324 * size, not AES 128 with truncated digest as might be imagined
326 static QCryptoCipherAlgorithm
327 qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher
,
328 QCryptoHashAlgorithm hash
,
331 size_t digestlen
= qcrypto_hash_digest_len(hash
);
332 size_t keylen
= qcrypto_cipher_get_key_len(cipher
);
333 if (digestlen
== keylen
) {
338 case QCRYPTO_CIPHER_ALG_AES_128
:
339 case QCRYPTO_CIPHER_ALG_AES_192
:
340 case QCRYPTO_CIPHER_ALG_AES_256
:
341 if (digestlen
== qcrypto_cipher_get_key_len(
342 QCRYPTO_CIPHER_ALG_AES_128
)) {
343 return QCRYPTO_CIPHER_ALG_AES_128
;
344 } else if (digestlen
== qcrypto_cipher_get_key_len(
345 QCRYPTO_CIPHER_ALG_AES_192
)) {
346 return QCRYPTO_CIPHER_ALG_AES_192
;
347 } else if (digestlen
== qcrypto_cipher_get_key_len(
348 QCRYPTO_CIPHER_ALG_AES_256
)) {
349 return QCRYPTO_CIPHER_ALG_AES_256
;
351 error_setg(errp
, "No AES cipher with key size %zu available",
356 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
357 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
358 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
359 if (digestlen
== qcrypto_cipher_get_key_len(
360 QCRYPTO_CIPHER_ALG_SERPENT_128
)) {
361 return QCRYPTO_CIPHER_ALG_SERPENT_128
;
362 } else if (digestlen
== qcrypto_cipher_get_key_len(
363 QCRYPTO_CIPHER_ALG_SERPENT_192
)) {
364 return QCRYPTO_CIPHER_ALG_SERPENT_192
;
365 } else if (digestlen
== qcrypto_cipher_get_key_len(
366 QCRYPTO_CIPHER_ALG_SERPENT_256
)) {
367 return QCRYPTO_CIPHER_ALG_SERPENT_256
;
369 error_setg(errp
, "No Serpent cipher with key size %zu available",
374 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
375 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
376 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
377 if (digestlen
== qcrypto_cipher_get_key_len(
378 QCRYPTO_CIPHER_ALG_TWOFISH_128
)) {
379 return QCRYPTO_CIPHER_ALG_TWOFISH_128
;
380 } else if (digestlen
== qcrypto_cipher_get_key_len(
381 QCRYPTO_CIPHER_ALG_TWOFISH_192
)) {
382 return QCRYPTO_CIPHER_ALG_TWOFISH_192
;
383 } else if (digestlen
== qcrypto_cipher_get_key_len(
384 QCRYPTO_CIPHER_ALG_TWOFISH_256
)) {
385 return QCRYPTO_CIPHER_ALG_TWOFISH_256
;
387 error_setg(errp
, "No Twofish cipher with key size %zu available",
393 error_setg(errp
, "Cipher %s not supported with essiv",
394 QCryptoCipherAlgorithm_lookup
[cipher
]);
400 * Given a key slot, and user password, this will attempt to unlock
401 * the master encryption key from the key slot.
404 * 0 if the key slot is disabled, or key could not be decrypted
405 * with the provided password
406 * 1 if the key slot is enabled, and key decrypted successfully
407 * with the provided password
408 * -1 if a fatal error occurred loading the key
411 qcrypto_block_luks_load_key(QCryptoBlock
*block
,
412 QCryptoBlockLUKSKeySlot
*slot
,
413 const char *password
,
414 QCryptoCipherAlgorithm cipheralg
,
415 QCryptoCipherMode ciphermode
,
416 QCryptoHashAlgorithm hash
,
417 QCryptoIVGenAlgorithm ivalg
,
418 QCryptoCipherAlgorithm ivcipheralg
,
419 QCryptoHashAlgorithm ivhash
,
422 QCryptoBlockReadFunc readfunc
,
426 QCryptoBlockLUKS
*luks
= block
->opaque
;
429 uint8_t *possiblekey
;
432 QCryptoCipher
*cipher
= NULL
;
433 uint8_t keydigest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
434 QCryptoIVGen
*ivgen
= NULL
;
437 if (slot
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
) {
441 splitkeylen
= masterkeylen
* slot
->stripes
;
442 splitkey
= g_new0(uint8_t, splitkeylen
);
443 possiblekey
= g_new0(uint8_t, masterkeylen
);
446 * The user password is used to generate a (possible)
447 * decryption key. This may or may not successfully
448 * decrypt the master key - we just blindly assume
449 * the key is correct and validate the results of
452 if (qcrypto_pbkdf2(hash
,
453 (const uint8_t *)password
, strlen(password
),
454 slot
->salt
, QCRYPTO_BLOCK_LUKS_SALT_LEN
,
456 possiblekey
, masterkeylen
,
462 * We need to read the master key material from the
463 * LUKS key material header. What we're reading is
464 * not the raw master key, but rather the data after
465 * it has been passed through AFSplit and the result
469 slot
->key_offset
* QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
470 splitkey
, splitkeylen
,
478 /* Setup the cipher/ivgen that we'll use to try to decrypt
479 * the split master key material */
480 cipher
= qcrypto_cipher_new(cipheralg
, ciphermode
,
481 possiblekey
, masterkeylen
,
487 niv
= qcrypto_cipher_get_iv_len(cipheralg
,
489 ivgen
= qcrypto_ivgen_new(ivalg
,
492 possiblekey
, masterkeylen
,
500 * The master key needs to be decrypted in the same
501 * way that the block device payload will be decrypted
502 * later. In particular we'll be using the IV generator
503 * to reset the encryption cipher every time the master
504 * key crosses a sector boundary.
506 if (qcrypto_block_decrypt_helper(cipher
,
509 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
518 * Now we've decrypted the split master key, join
519 * it back together to get the actual master key.
521 if (qcrypto_afsplit_decode(hash
,
532 * We still don't know that the masterkey we got is valid,
533 * because we just blindly assumed the user's password
534 * was correct. This is where we now verify it. We are
535 * creating a hash of the master key using PBKDF and
536 * then comparing that to the hash stored in the key slot
539 if (qcrypto_pbkdf2(hash
,
540 masterkey
, masterkeylen
,
541 luks
->header
.master_key_salt
,
542 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
543 luks
->header
.master_key_iterations
,
544 keydigest
, G_N_ELEMENTS(keydigest
),
549 if (memcmp(keydigest
, luks
->header
.master_key_digest
,
550 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
) == 0) {
551 /* Success, we got the right master key */
556 /* Fail, user's password was not valid for this key slot,
557 * tell caller to try another slot */
561 qcrypto_ivgen_free(ivgen
);
562 qcrypto_cipher_free(cipher
);
570 * Given a user password, this will iterate over all key
571 * slots and try to unlock each active key slot using the
572 * password until it successfully obtains a master key.
574 * Returns 0 if a key was loaded, -1 if no keys could be loaded
577 qcrypto_block_luks_find_key(QCryptoBlock
*block
,
578 const char *password
,
579 QCryptoCipherAlgorithm cipheralg
,
580 QCryptoCipherMode ciphermode
,
581 QCryptoHashAlgorithm hash
,
582 QCryptoIVGenAlgorithm ivalg
,
583 QCryptoCipherAlgorithm ivcipheralg
,
584 QCryptoHashAlgorithm ivhash
,
586 size_t *masterkeylen
,
587 QCryptoBlockReadFunc readfunc
,
591 QCryptoBlockLUKS
*luks
= block
->opaque
;
595 *masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
596 *masterkeylen
= luks
->header
.key_bytes
;
598 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
599 rv
= qcrypto_block_luks_load_key(block
,
600 &luks
->header
.key_slots
[i
],
621 error_setg(errp
, "Invalid password, cannot unlock any keyslot");
632 qcrypto_block_luks_open(QCryptoBlock
*block
,
633 QCryptoBlockOpenOptions
*options
,
634 QCryptoBlockReadFunc readfunc
,
639 QCryptoBlockLUKS
*luks
;
640 Error
*local_err
= NULL
;
644 uint8_t *masterkey
= NULL
;
646 char *ivgen_name
, *ivhash_name
;
647 QCryptoCipherMode ciphermode
;
648 QCryptoCipherAlgorithm cipheralg
;
649 QCryptoIVGenAlgorithm ivalg
;
650 QCryptoCipherAlgorithm ivcipheralg
;
651 QCryptoHashAlgorithm hash
;
652 QCryptoHashAlgorithm ivhash
;
653 char *password
= NULL
;
655 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
656 if (!options
->u
.luks
.key_secret
) {
657 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
660 password
= qcrypto_secret_lookup_as_utf8(
661 options
->u
.luks
.key_secret
, errp
);
667 luks
= g_new0(QCryptoBlockLUKS
, 1);
668 block
->opaque
= luks
;
670 /* Read the entire LUKS header, minus the key material from
671 * the underlying device */
672 rv
= readfunc(block
, 0,
673 (uint8_t *)&luks
->header
,
674 sizeof(luks
->header
),
682 /* The header is always stored in big-endian format, so
683 * convert everything to native */
684 be16_to_cpus(&luks
->header
.version
);
685 be32_to_cpus(&luks
->header
.payload_offset
);
686 be32_to_cpus(&luks
->header
.key_bytes
);
687 be32_to_cpus(&luks
->header
.master_key_iterations
);
689 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
690 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
691 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
692 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
693 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
696 if (memcmp(luks
->header
.magic
, qcrypto_block_luks_magic
,
697 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) != 0) {
698 error_setg(errp
, "Volume is not in LUKS format");
702 if (luks
->header
.version
!= QCRYPTO_BLOCK_LUKS_VERSION
) {
703 error_setg(errp
, "LUKS version %" PRIu32
" is not supported",
704 luks
->header
.version
);
710 * The cipher_mode header contains a string that we have
711 * to further parse, of the format
713 * <cipher-mode>-<iv-generator>[:<iv-hash>]
715 * eg cbc-essiv:sha256, cbc-plain64
717 ivgen_name
= strchr(luks
->header
.cipher_mode
, '-');
720 error_setg(errp
, "Unexpected cipher mode string format %s",
721 luks
->header
.cipher_mode
);
727 ivhash_name
= strchr(ivgen_name
, ':');
734 ivhash
= qcrypto_block_luks_hash_name_lookup(ivhash_name
,
738 error_propagate(errp
, local_err
);
743 ciphermode
= qcrypto_block_luks_cipher_mode_lookup(luks
->header
.cipher_mode
,
747 error_propagate(errp
, local_err
);
751 cipheralg
= qcrypto_block_luks_cipher_name_lookup(luks
->header
.cipher_name
,
753 luks
->header
.key_bytes
,
757 error_propagate(errp
, local_err
);
761 hash
= qcrypto_block_luks_hash_name_lookup(luks
->header
.hash_spec
,
765 error_propagate(errp
, local_err
);
769 ivalg
= qcrypto_block_luks_ivgen_name_lookup(ivgen_name
,
773 error_propagate(errp
, local_err
);
777 if (ivalg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
778 ivcipheralg
= qcrypto_block_luks_essiv_cipher(cipheralg
,
783 error_propagate(errp
, local_err
);
787 ivcipheralg
= cipheralg
;
790 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
791 /* Try to find which key slot our password is valid for
792 * and unlock the master key from that slot.
794 if (qcrypto_block_luks_find_key(block
,
796 cipheralg
, ciphermode
,
801 &masterkey
, &masterkeylen
,
808 /* We have a valid master key now, so can setup the
809 * block device payload decryption objects
811 block
->kdfhash
= hash
;
812 block
->niv
= qcrypto_cipher_get_iv_len(cipheralg
,
814 block
->ivgen
= qcrypto_ivgen_new(ivalg
,
817 masterkey
, masterkeylen
,
824 block
->cipher
= qcrypto_cipher_new(cipheralg
,
826 masterkey
, masterkeylen
,
828 if (!block
->cipher
) {
834 block
->payload_offset
= luks
->header
.payload_offset
*
835 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
844 qcrypto_cipher_free(block
->cipher
);
845 qcrypto_ivgen_free(block
->ivgen
);
853 qcrypto_block_luks_uuid_gen(uint8_t *uuidstr
, Error
**errp
)
858 uuid_unparse(uuid
, (char *)uuidstr
);
861 error_setg(errp
, "Unable to generate uuids on this platform");
867 qcrypto_block_luks_create(QCryptoBlock
*block
,
868 QCryptoBlockCreateOptions
*options
,
869 QCryptoBlockInitFunc initfunc
,
870 QCryptoBlockWriteFunc writefunc
,
874 QCryptoBlockLUKS
*luks
;
875 QCryptoBlockCreateOptionsLUKS luks_opts
;
876 Error
*local_err
= NULL
;
877 uint8_t *masterkey
= NULL
;
878 uint8_t *slotkey
= NULL
;
879 uint8_t *splitkey
= NULL
;
880 size_t splitkeylen
= 0;
882 QCryptoCipher
*cipher
= NULL
;
883 QCryptoIVGen
*ivgen
= NULL
;
885 const char *cipher_alg
;
886 const char *cipher_mode
;
887 const char *ivgen_alg
;
888 const char *ivgen_hash_alg
= NULL
;
889 const char *hash_alg
;
890 char *cipher_mode_spec
= NULL
;
891 QCryptoCipherAlgorithm ivcipheralg
= 0;
893 memcpy(&luks_opts
, &options
->u
.luks
, sizeof(luks_opts
));
894 if (!luks_opts
.has_cipher_alg
) {
895 luks_opts
.cipher_alg
= QCRYPTO_CIPHER_ALG_AES_256
;
897 if (!luks_opts
.has_cipher_mode
) {
898 luks_opts
.cipher_mode
= QCRYPTO_CIPHER_MODE_XTS
;
900 if (!luks_opts
.has_ivgen_alg
) {
901 luks_opts
.ivgen_alg
= QCRYPTO_IVGEN_ALG_PLAIN64
;
903 if (!luks_opts
.has_hash_alg
) {
904 luks_opts
.hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
907 if (!options
->u
.luks
.key_secret
) {
908 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
911 password
= qcrypto_secret_lookup_as_utf8(luks_opts
.key_secret
, errp
);
916 luks
= g_new0(QCryptoBlockLUKS
, 1);
917 block
->opaque
= luks
;
919 memcpy(luks
->header
.magic
, qcrypto_block_luks_magic
,
920 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
);
922 /* We populate the header in native endianness initially and
923 * then convert everything to big endian just before writing
926 luks
->header
.version
= QCRYPTO_BLOCK_LUKS_VERSION
;
927 if (qcrypto_block_luks_uuid_gen(luks
->header
.uuid
,
932 cipher_alg
= qcrypto_block_luks_cipher_alg_lookup(luks_opts
.cipher_alg
,
938 cipher_mode
= QCryptoCipherMode_lookup
[luks_opts
.cipher_mode
];
939 ivgen_alg
= QCryptoIVGenAlgorithm_lookup
[luks_opts
.ivgen_alg
];
940 if (luks_opts
.has_ivgen_hash_alg
) {
941 ivgen_hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.ivgen_hash_alg
];
942 cipher_mode_spec
= g_strdup_printf("%s-%s:%s", cipher_mode
, ivgen_alg
,
945 cipher_mode_spec
= g_strdup_printf("%s-%s", cipher_mode
, ivgen_alg
);
947 hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.hash_alg
];
950 if (strlen(cipher_alg
) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
) {
951 error_setg(errp
, "Cipher name '%s' is too long for LUKS header",
955 if (strlen(cipher_mode_spec
) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
) {
956 error_setg(errp
, "Cipher mode '%s' is too long for LUKS header",
960 if (strlen(hash_alg
) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
) {
961 error_setg(errp
, "Hash name '%s' is too long for LUKS header",
966 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
967 ivcipheralg
= qcrypto_block_luks_essiv_cipher(luks_opts
.cipher_alg
,
968 luks_opts
.ivgen_hash_alg
,
971 error_propagate(errp
, local_err
);
975 ivcipheralg
= luks_opts
.cipher_alg
;
978 strcpy(luks
->header
.cipher_name
, cipher_alg
);
979 strcpy(luks
->header
.cipher_mode
, cipher_mode_spec
);
980 strcpy(luks
->header
.hash_spec
, hash_alg
);
982 luks
->header
.key_bytes
= qcrypto_cipher_get_key_len(luks_opts
.cipher_alg
);
983 if (luks_opts
.cipher_mode
== QCRYPTO_CIPHER_MODE_XTS
) {
984 luks
->header
.key_bytes
*= 2;
987 /* Generate the salt used for hashing the master key
990 if (qcrypto_random_bytes(luks
->header
.master_key_salt
,
991 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
996 /* Generate random master key */
997 masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
998 if (qcrypto_random_bytes(masterkey
,
999 luks
->header
.key_bytes
, errp
) < 0) {
1004 /* Setup the block device payload encryption objects */
1005 block
->cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1006 luks_opts
.cipher_mode
,
1007 masterkey
, luks
->header
.key_bytes
,
1009 if (!block
->cipher
) {
1013 block
->kdfhash
= luks_opts
.hash_alg
;
1014 block
->niv
= qcrypto_cipher_get_iv_len(luks_opts
.cipher_alg
,
1015 luks_opts
.cipher_mode
);
1016 block
->ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1018 luks_opts
.ivgen_hash_alg
,
1019 masterkey
, luks
->header
.key_bytes
,
1022 if (!block
->ivgen
) {
1027 /* Determine how many iterations we need to hash the master
1028 * key, in order to have 1 second of compute time used
1030 luks
->header
.master_key_iterations
=
1031 qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1032 masterkey
, luks
->header
.key_bytes
,
1033 luks
->header
.master_key_salt
,
1034 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1037 error_propagate(errp
, local_err
);
1041 /* Why /= 8 ? That matches cryptsetup, but there's no
1042 * explanation why they chose /= 8... Probably so that
1043 * if all 8 keyslots are active we only spend 1 second
1044 * in total time to check all keys */
1045 luks
->header
.master_key_iterations
/= 8;
1046 luks
->header
.master_key_iterations
= MAX(
1047 luks
->header
.master_key_iterations
,
1048 QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS
);
1051 /* Hash the master key, saving the result in the LUKS
1052 * header. This hash is used when opening the encrypted
1053 * device to verify that the user password unlocked a
1056 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1057 masterkey
, luks
->header
.key_bytes
,
1058 luks
->header
.master_key_salt
,
1059 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1060 luks
->header
.master_key_iterations
,
1061 luks
->header
.master_key_digest
,
1062 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1068 /* Although LUKS has multiple key slots, we're just going
1069 * to use the first key slot */
1070 splitkeylen
= luks
->header
.key_bytes
* QCRYPTO_BLOCK_LUKS_STRIPES
;
1071 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1072 luks
->header
.key_slots
[i
].active
= i
== 0 ?
1073 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
:
1074 QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
;
1075 luks
->header
.key_slots
[i
].stripes
= QCRYPTO_BLOCK_LUKS_STRIPES
;
1077 /* This calculation doesn't match that shown in the spec,
1078 * but instead follows the cryptsetup implementation.
1080 luks
->header
.key_slots
[i
].key_offset
=
1081 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1082 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1083 (ROUND_UP(((splitkeylen
+ (QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
- 1)) /
1084 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1085 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1086 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) * i
);
1089 if (qcrypto_random_bytes(luks
->header
.key_slots
[0].salt
,
1090 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1095 /* Again we determine how many iterations are required to
1096 * hash the user password while consuming 1 second of compute
1098 luks
->header
.key_slots
[0].iterations
=
1099 qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1100 (uint8_t *)password
, strlen(password
),
1101 luks
->header
.key_slots
[0].salt
,
1102 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1105 error_propagate(errp
, local_err
);
1108 /* Why /= 2 ? That matches cryptsetup, but there's no
1109 * explanation why they chose /= 2... */
1110 luks
->header
.key_slots
[0].iterations
/= 2;
1111 luks
->header
.key_slots
[0].iterations
= MAX(
1112 luks
->header
.key_slots
[0].iterations
,
1113 QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS
);
1116 /* Generate a key that we'll use to encrypt the master
1117 * key, from the user's password
1119 slotkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
1120 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1121 (uint8_t *)password
, strlen(password
),
1122 luks
->header
.key_slots
[0].salt
,
1123 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1124 luks
->header
.key_slots
[0].iterations
,
1125 slotkey
, luks
->header
.key_bytes
,
1131 /* Setup the encryption objects needed to encrypt the
1132 * master key material
1134 cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1135 luks_opts
.cipher_mode
,
1136 slotkey
, luks
->header
.key_bytes
,
1142 ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1144 luks_opts
.ivgen_hash_alg
,
1145 slotkey
, luks
->header
.key_bytes
,
1151 /* Before storing the master key, we need to vastly
1152 * increase its size, as protection against forensic
1153 * disk data recovery */
1154 splitkey
= g_new0(uint8_t, splitkeylen
);
1156 if (qcrypto_afsplit_encode(luks_opts
.hash_alg
,
1157 luks
->header
.key_bytes
,
1158 luks
->header
.key_slots
[0].stripes
,
1165 /* Now we encrypt the split master key with the key generated
1166 * from the user's password, before storing it */
1167 if (qcrypto_block_encrypt_helper(cipher
, block
->niv
, ivgen
,
1168 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1177 /* The total size of the LUKS headers is the partition header + key
1178 * slot headers, rounded up to the nearest sector, combined with
1179 * the size of each master key material region, also rounded up
1180 * to the nearest sector */
1181 luks
->header
.payload_offset
=
1182 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1183 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1184 (ROUND_UP(((splitkeylen
+ (QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
- 1)) /
1185 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1186 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1187 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) *
1188 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1190 block
->payload_offset
= luks
->header
.payload_offset
*
1191 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1193 /* Reserve header space to match payload offset */
1194 initfunc(block
, block
->payload_offset
, &local_err
, opaque
);
1196 error_propagate(errp
, local_err
);
1200 /* Everything on disk uses Big Endian, so flip header fields
1201 * before writing them */
1202 cpu_to_be16s(&luks
->header
.version
);
1203 cpu_to_be32s(&luks
->header
.payload_offset
);
1204 cpu_to_be32s(&luks
->header
.key_bytes
);
1205 cpu_to_be32s(&luks
->header
.master_key_iterations
);
1207 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1208 cpu_to_be32s(&luks
->header
.key_slots
[i
].active
);
1209 cpu_to_be32s(&luks
->header
.key_slots
[i
].iterations
);
1210 cpu_to_be32s(&luks
->header
.key_slots
[i
].key_offset
);
1211 cpu_to_be32s(&luks
->header
.key_slots
[i
].stripes
);
1215 /* Write out the partition header and key slot headers */
1217 (const uint8_t *)&luks
->header
,
1218 sizeof(luks
->header
),
1222 /* Delay checking local_err until we've byte-swapped */
1224 /* Byte swap the header back to native, in case we need
1225 * to read it again later */
1226 be16_to_cpus(&luks
->header
.version
);
1227 be32_to_cpus(&luks
->header
.payload_offset
);
1228 be32_to_cpus(&luks
->header
.key_bytes
);
1229 be32_to_cpus(&luks
->header
.master_key_iterations
);
1231 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1232 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
1233 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
1234 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
1235 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
1239 error_propagate(errp
, local_err
);
1243 /* Write out the master key material, starting at the
1244 * sector immediately following the partition header. */
1245 if (writefunc(block
,
1246 luks
->header
.key_slots
[0].key_offset
*
1247 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1248 splitkey
, splitkeylen
,
1250 opaque
) != splitkeylen
) {
1254 memset(masterkey
, 0, luks
->header
.key_bytes
);
1256 memset(slotkey
, 0, luks
->header
.key_bytes
);
1260 g_free(cipher_mode_spec
);
1262 qcrypto_ivgen_free(ivgen
);
1263 qcrypto_cipher_free(cipher
);
1269 memset(masterkey
, 0, luks
->header
.key_bytes
);
1273 memset(slotkey
, 0, luks
->header
.key_bytes
);
1278 g_free(cipher_mode_spec
);
1280 qcrypto_ivgen_free(ivgen
);
1281 qcrypto_cipher_free(cipher
);
1288 static void qcrypto_block_luks_cleanup(QCryptoBlock
*block
)
1290 g_free(block
->opaque
);
1295 qcrypto_block_luks_decrypt(QCryptoBlock
*block
,
1296 uint64_t startsector
,
1301 return qcrypto_block_decrypt_helper(block
->cipher
,
1302 block
->niv
, block
->ivgen
,
1303 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1304 startsector
, buf
, len
, errp
);
1309 qcrypto_block_luks_encrypt(QCryptoBlock
*block
,
1310 uint64_t startsector
,
1315 return qcrypto_block_encrypt_helper(block
->cipher
,
1316 block
->niv
, block
->ivgen
,
1317 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1318 startsector
, buf
, len
, errp
);
1322 const QCryptoBlockDriver qcrypto_block_driver_luks
= {
1323 .open
= qcrypto_block_luks_open
,
1324 .create
= qcrypto_block_luks_create
,
1325 .cleanup
= qcrypto_block_luks_cleanup
,
1326 .decrypt
= qcrypto_block_luks_decrypt
,
1327 .encrypt
= qcrypto_block_luks_encrypt
,
1328 .has_format
= qcrypto_block_luks_has_format
,