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"
34 #include <uuid/uuid.h>
37 #include "qemu/coroutine.h"
40 * Reference for the LUKS format implemented here is
42 * docs/on-disk-format.pdf
44 * in 'cryptsetup' package source code
46 * This file implements the 1.2.1 specification, dated
50 typedef struct QCryptoBlockLUKS QCryptoBlockLUKS
;
51 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader
;
52 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot
;
55 /* The following constants are all defined by the LUKS spec */
56 #define QCRYPTO_BLOCK_LUKS_VERSION 1
58 #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6
59 #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32
60 #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32
61 #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32
62 #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20
63 #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32
64 #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40
65 #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8
66 #define QCRYPTO_BLOCK_LUKS_STRIPES 4000
67 #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000
68 #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000
69 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096
71 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
72 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
74 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
76 static const char qcrypto_block_luks_magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
] = {
77 'L', 'U', 'K', 'S', 0xBA, 0xBE
80 typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap
;
81 struct QCryptoBlockLUKSNameMap
{
86 typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap
;
87 struct QCryptoBlockLUKSCipherSizeMap
{
91 typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap
;
92 struct QCryptoBlockLUKSCipherNameMap
{
94 const QCryptoBlockLUKSCipherSizeMap
*sizes
;
98 static const QCryptoBlockLUKSCipherSizeMap
99 qcrypto_block_luks_cipher_size_map_aes
[] = {
100 { 16, QCRYPTO_CIPHER_ALG_AES_128
},
101 { 24, QCRYPTO_CIPHER_ALG_AES_192
},
102 { 32, QCRYPTO_CIPHER_ALG_AES_256
},
106 static const QCryptoBlockLUKSCipherSizeMap
107 qcrypto_block_luks_cipher_size_map_cast5
[] = {
108 { 16, QCRYPTO_CIPHER_ALG_CAST5_128
},
112 static const QCryptoBlockLUKSCipherSizeMap
113 qcrypto_block_luks_cipher_size_map_serpent
[] = {
114 { 16, QCRYPTO_CIPHER_ALG_SERPENT_128
},
115 { 24, QCRYPTO_CIPHER_ALG_SERPENT_192
},
116 { 32, QCRYPTO_CIPHER_ALG_SERPENT_256
},
120 static const QCryptoBlockLUKSCipherSizeMap
121 qcrypto_block_luks_cipher_size_map_twofish
[] = {
122 { 16, QCRYPTO_CIPHER_ALG_TWOFISH_128
},
123 { 24, QCRYPTO_CIPHER_ALG_TWOFISH_192
},
124 { 32, QCRYPTO_CIPHER_ALG_TWOFISH_256
},
128 static const QCryptoBlockLUKSCipherNameMap
129 qcrypto_block_luks_cipher_name_map
[] = {
130 { "aes", qcrypto_block_luks_cipher_size_map_aes
},
131 { "cast5", qcrypto_block_luks_cipher_size_map_cast5
},
132 { "serpent", qcrypto_block_luks_cipher_size_map_serpent
},
133 { "twofish", qcrypto_block_luks_cipher_size_map_twofish
},
138 * This struct is written to disk in big-endian format,
139 * but operated upon in native-endian format.
141 struct QCryptoBlockLUKSKeySlot
{
142 /* state of keyslot, enabled/disable */
144 /* iterations for PBKDF2 */
146 /* salt for PBKDF2 */
147 uint8_t salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
148 /* start sector of key material */
150 /* number of anti-forensic stripes */
154 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot
) != 48);
158 * This struct is written to disk in big-endian format,
159 * but operated upon in native-endian format.
161 struct QCryptoBlockLUKSHeader
{
162 /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
163 char magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
];
165 /* LUKS version, currently 1 */
168 /* cipher name specification (aes, etc) */
169 char cipher_name
[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
];
171 /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
172 char cipher_mode
[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
];
174 /* hash specification (sha256, etc) */
175 char hash_spec
[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
];
177 /* start offset of the volume data (in 512 byte sectors) */
178 uint32_t payload_offset
;
180 /* Number of key bytes */
183 /* master key checksum after PBKDF2 */
184 uint8_t master_key_digest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
186 /* salt for master key PBKDF2 */
187 uint8_t master_key_salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
189 /* iterations for master key PBKDF2 */
190 uint32_t master_key_iterations
;
192 /* UUID of the partition in standard ASCII representation */
193 uint8_t uuid
[QCRYPTO_BLOCK_LUKS_UUID_LEN
];
196 QCryptoBlockLUKSKeySlot key_slots
[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
];
199 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader
) != 592);
202 struct QCryptoBlockLUKS
{
203 QCryptoBlockLUKSHeader header
;
205 /* Cache parsed versions of what's in header fields,
206 * as we can't rely on QCryptoBlock.cipher being
208 QCryptoCipherAlgorithm cipher_alg
;
209 QCryptoCipherMode cipher_mode
;
210 QCryptoIVGenAlgorithm ivgen_alg
;
211 QCryptoHashAlgorithm ivgen_hash_alg
;
212 QCryptoHashAlgorithm hash_alg
;
216 static int qcrypto_block_luks_cipher_name_lookup(const char *name
,
217 QCryptoCipherMode mode
,
221 const QCryptoBlockLUKSCipherNameMap
*map
=
222 qcrypto_block_luks_cipher_name_map
;
223 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
226 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
230 for (i
= 0; i
< maplen
; i
++) {
231 if (!g_str_equal(map
[i
].name
, name
)) {
234 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
235 if (map
[i
].sizes
[j
].key_bytes
== key_bytes
) {
236 return map
[i
].sizes
[j
].id
;
241 error_setg(errp
, "Algorithm %s with key size %d bytes not supported",
247 qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg
,
250 const QCryptoBlockLUKSCipherNameMap
*map
=
251 qcrypto_block_luks_cipher_name_map
;
252 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
254 for (i
= 0; i
< maplen
; i
++) {
255 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
256 if (map
[i
].sizes
[j
].id
== alg
) {
262 error_setg(errp
, "Algorithm '%s' not supported",
263 QCryptoCipherAlgorithm_lookup
[alg
]);
267 /* XXX replace with qapi_enum_parse() in future, when we can
268 * make that function emit a more friendly error message */
269 static int qcrypto_block_luks_name_lookup(const char *name
,
270 const char *const *map
,
276 for (i
= 0; i
< maplen
; i
++) {
277 if (g_str_equal(map
[i
], name
)) {
282 error_setg(errp
, "%s %s not supported", type
, name
);
286 #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
287 qcrypto_block_luks_name_lookup(name, \
288 QCryptoCipherMode_lookup, \
289 QCRYPTO_CIPHER_MODE__MAX, \
293 #define qcrypto_block_luks_hash_name_lookup(name, errp) \
294 qcrypto_block_luks_name_lookup(name, \
295 QCryptoHashAlgorithm_lookup, \
296 QCRYPTO_HASH_ALG__MAX, \
300 #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
301 qcrypto_block_luks_name_lookup(name, \
302 QCryptoIVGenAlgorithm_lookup, \
303 QCRYPTO_IVGEN_ALG__MAX, \
309 qcrypto_block_luks_has_format(const uint8_t *buf
,
312 const QCryptoBlockLUKSHeader
*luks_header
= (const void *)buf
;
314 if (buf_size
>= offsetof(QCryptoBlockLUKSHeader
, cipher_name
) &&
315 memcmp(luks_header
->magic
, qcrypto_block_luks_magic
,
316 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) == 0 &&
317 be16_to_cpu(luks_header
->version
) == QCRYPTO_BLOCK_LUKS_VERSION
) {
326 * Deal with a quirk of dm-crypt usage of ESSIV.
328 * When calculating ESSIV IVs, the cipher length used by ESSIV
329 * may be different from the cipher length used for the block
330 * encryption, becauses dm-crypt uses the hash digest length
331 * as the key size. ie, if you have AES 128 as the block cipher
332 * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
333 * the cipher since that gets a key length matching the digest
334 * size, not AES 128 with truncated digest as might be imagined
336 static QCryptoCipherAlgorithm
337 qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher
,
338 QCryptoHashAlgorithm hash
,
341 size_t digestlen
= qcrypto_hash_digest_len(hash
);
342 size_t keylen
= qcrypto_cipher_get_key_len(cipher
);
343 if (digestlen
== keylen
) {
348 case QCRYPTO_CIPHER_ALG_AES_128
:
349 case QCRYPTO_CIPHER_ALG_AES_192
:
350 case QCRYPTO_CIPHER_ALG_AES_256
:
351 if (digestlen
== qcrypto_cipher_get_key_len(
352 QCRYPTO_CIPHER_ALG_AES_128
)) {
353 return QCRYPTO_CIPHER_ALG_AES_128
;
354 } else if (digestlen
== qcrypto_cipher_get_key_len(
355 QCRYPTO_CIPHER_ALG_AES_192
)) {
356 return QCRYPTO_CIPHER_ALG_AES_192
;
357 } else if (digestlen
== qcrypto_cipher_get_key_len(
358 QCRYPTO_CIPHER_ALG_AES_256
)) {
359 return QCRYPTO_CIPHER_ALG_AES_256
;
361 error_setg(errp
, "No AES cipher with key size %zu available",
366 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
367 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
368 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
369 if (digestlen
== qcrypto_cipher_get_key_len(
370 QCRYPTO_CIPHER_ALG_SERPENT_128
)) {
371 return QCRYPTO_CIPHER_ALG_SERPENT_128
;
372 } else if (digestlen
== qcrypto_cipher_get_key_len(
373 QCRYPTO_CIPHER_ALG_SERPENT_192
)) {
374 return QCRYPTO_CIPHER_ALG_SERPENT_192
;
375 } else if (digestlen
== qcrypto_cipher_get_key_len(
376 QCRYPTO_CIPHER_ALG_SERPENT_256
)) {
377 return QCRYPTO_CIPHER_ALG_SERPENT_256
;
379 error_setg(errp
, "No Serpent cipher with key size %zu available",
384 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
385 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
386 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
387 if (digestlen
== qcrypto_cipher_get_key_len(
388 QCRYPTO_CIPHER_ALG_TWOFISH_128
)) {
389 return QCRYPTO_CIPHER_ALG_TWOFISH_128
;
390 } else if (digestlen
== qcrypto_cipher_get_key_len(
391 QCRYPTO_CIPHER_ALG_TWOFISH_192
)) {
392 return QCRYPTO_CIPHER_ALG_TWOFISH_192
;
393 } else if (digestlen
== qcrypto_cipher_get_key_len(
394 QCRYPTO_CIPHER_ALG_TWOFISH_256
)) {
395 return QCRYPTO_CIPHER_ALG_TWOFISH_256
;
397 error_setg(errp
, "No Twofish cipher with key size %zu available",
403 error_setg(errp
, "Cipher %s not supported with essiv",
404 QCryptoCipherAlgorithm_lookup
[cipher
]);
410 * Given a key slot, and user password, this will attempt to unlock
411 * the master encryption key from the key slot.
414 * 0 if the key slot is disabled, or key could not be decrypted
415 * with the provided password
416 * 1 if the key slot is enabled, and key decrypted successfully
417 * with the provided password
418 * -1 if a fatal error occurred loading the key
421 qcrypto_block_luks_load_key(QCryptoBlock
*block
,
422 QCryptoBlockLUKSKeySlot
*slot
,
423 const char *password
,
424 QCryptoCipherAlgorithm cipheralg
,
425 QCryptoCipherMode ciphermode
,
426 QCryptoHashAlgorithm hash
,
427 QCryptoIVGenAlgorithm ivalg
,
428 QCryptoCipherAlgorithm ivcipheralg
,
429 QCryptoHashAlgorithm ivhash
,
432 QCryptoBlockReadFunc readfunc
,
436 QCryptoBlockLUKS
*luks
= block
->opaque
;
439 uint8_t *possiblekey
;
442 QCryptoCipher
*cipher
= NULL
;
443 uint8_t keydigest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
444 QCryptoIVGen
*ivgen
= NULL
;
447 if (slot
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
) {
451 splitkeylen
= masterkeylen
* slot
->stripes
;
452 splitkey
= g_new0(uint8_t, splitkeylen
);
453 possiblekey
= g_new0(uint8_t, masterkeylen
);
456 * The user password is used to generate a (possible)
457 * decryption key. This may or may not successfully
458 * decrypt the master key - we just blindly assume
459 * the key is correct and validate the results of
462 if (qcrypto_pbkdf2(hash
,
463 (const uint8_t *)password
, strlen(password
),
464 slot
->salt
, QCRYPTO_BLOCK_LUKS_SALT_LEN
,
466 possiblekey
, masterkeylen
,
472 * We need to read the master key material from the
473 * LUKS key material header. What we're reading is
474 * not the raw master key, but rather the data after
475 * it has been passed through AFSplit and the result
479 slot
->key_offset
* QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
480 splitkey
, splitkeylen
,
488 /* Setup the cipher/ivgen that we'll use to try to decrypt
489 * the split master key material */
490 cipher
= qcrypto_cipher_new(cipheralg
, ciphermode
,
491 possiblekey
, masterkeylen
,
497 niv
= qcrypto_cipher_get_iv_len(cipheralg
,
499 ivgen
= qcrypto_ivgen_new(ivalg
,
502 possiblekey
, masterkeylen
,
510 * The master key needs to be decrypted in the same
511 * way that the block device payload will be decrypted
512 * later. In particular we'll be using the IV generator
513 * to reset the encryption cipher every time the master
514 * key crosses a sector boundary.
516 if (qcrypto_block_decrypt_helper(cipher
,
519 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
528 * Now we've decrypted the split master key, join
529 * it back together to get the actual master key.
531 if (qcrypto_afsplit_decode(hash
,
542 * We still don't know that the masterkey we got is valid,
543 * because we just blindly assumed the user's password
544 * was correct. This is where we now verify it. We are
545 * creating a hash of the master key using PBKDF and
546 * then comparing that to the hash stored in the key slot
549 if (qcrypto_pbkdf2(hash
,
550 masterkey
, masterkeylen
,
551 luks
->header
.master_key_salt
,
552 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
553 luks
->header
.master_key_iterations
,
554 keydigest
, G_N_ELEMENTS(keydigest
),
559 if (memcmp(keydigest
, luks
->header
.master_key_digest
,
560 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
) == 0) {
561 /* Success, we got the right master key */
566 /* Fail, user's password was not valid for this key slot,
567 * tell caller to try another slot */
571 qcrypto_ivgen_free(ivgen
);
572 qcrypto_cipher_free(cipher
);
580 * Given a user password, this will iterate over all key
581 * slots and try to unlock each active key slot using the
582 * password until it successfully obtains a master key.
584 * Returns 0 if a key was loaded, -1 if no keys could be loaded
587 qcrypto_block_luks_find_key(QCryptoBlock
*block
,
588 const char *password
,
589 QCryptoCipherAlgorithm cipheralg
,
590 QCryptoCipherMode ciphermode
,
591 QCryptoHashAlgorithm hash
,
592 QCryptoIVGenAlgorithm ivalg
,
593 QCryptoCipherAlgorithm ivcipheralg
,
594 QCryptoHashAlgorithm ivhash
,
596 size_t *masterkeylen
,
597 QCryptoBlockReadFunc readfunc
,
601 QCryptoBlockLUKS
*luks
= block
->opaque
;
605 *masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
606 *masterkeylen
= luks
->header
.key_bytes
;
608 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
609 rv
= qcrypto_block_luks_load_key(block
,
610 &luks
->header
.key_slots
[i
],
631 error_setg(errp
, "Invalid password, cannot unlock any keyslot");
642 qcrypto_block_luks_open(QCryptoBlock
*block
,
643 QCryptoBlockOpenOptions
*options
,
644 QCryptoBlockReadFunc readfunc
,
649 QCryptoBlockLUKS
*luks
;
650 Error
*local_err
= NULL
;
654 uint8_t *masterkey
= NULL
;
656 char *ivgen_name
, *ivhash_name
;
657 QCryptoCipherMode ciphermode
;
658 QCryptoCipherAlgorithm cipheralg
;
659 QCryptoIVGenAlgorithm ivalg
;
660 QCryptoCipherAlgorithm ivcipheralg
;
661 QCryptoHashAlgorithm hash
;
662 QCryptoHashAlgorithm ivhash
;
663 char *password
= NULL
;
665 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
666 if (!options
->u
.luks
.key_secret
) {
667 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
670 password
= qcrypto_secret_lookup_as_utf8(
671 options
->u
.luks
.key_secret
, errp
);
677 luks
= g_new0(QCryptoBlockLUKS
, 1);
678 block
->opaque
= luks
;
680 /* Read the entire LUKS header, minus the key material from
681 * the underlying device */
682 rv
= readfunc(block
, 0,
683 (uint8_t *)&luks
->header
,
684 sizeof(luks
->header
),
692 /* The header is always stored in big-endian format, so
693 * convert everything to native */
694 be16_to_cpus(&luks
->header
.version
);
695 be32_to_cpus(&luks
->header
.payload_offset
);
696 be32_to_cpus(&luks
->header
.key_bytes
);
697 be32_to_cpus(&luks
->header
.master_key_iterations
);
699 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
700 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
701 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
702 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
703 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
706 if (memcmp(luks
->header
.magic
, qcrypto_block_luks_magic
,
707 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) != 0) {
708 error_setg(errp
, "Volume is not in LUKS format");
712 if (luks
->header
.version
!= QCRYPTO_BLOCK_LUKS_VERSION
) {
713 error_setg(errp
, "LUKS version %" PRIu32
" is not supported",
714 luks
->header
.version
);
720 * The cipher_mode header contains a string that we have
721 * to further parse, of the format
723 * <cipher-mode>-<iv-generator>[:<iv-hash>]
725 * eg cbc-essiv:sha256, cbc-plain64
727 ivgen_name
= strchr(luks
->header
.cipher_mode
, '-');
730 error_setg(errp
, "Unexpected cipher mode string format %s",
731 luks
->header
.cipher_mode
);
737 ivhash_name
= strchr(ivgen_name
, ':');
744 ivhash
= qcrypto_block_luks_hash_name_lookup(ivhash_name
,
748 error_propagate(errp
, local_err
);
753 ciphermode
= qcrypto_block_luks_cipher_mode_lookup(luks
->header
.cipher_mode
,
757 error_propagate(errp
, local_err
);
761 cipheralg
= qcrypto_block_luks_cipher_name_lookup(luks
->header
.cipher_name
,
763 luks
->header
.key_bytes
,
767 error_propagate(errp
, local_err
);
771 hash
= qcrypto_block_luks_hash_name_lookup(luks
->header
.hash_spec
,
775 error_propagate(errp
, local_err
);
779 ivalg
= qcrypto_block_luks_ivgen_name_lookup(ivgen_name
,
783 error_propagate(errp
, local_err
);
787 if (ivalg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
790 error_setg(errp
, "Missing IV generator hash specification");
793 ivcipheralg
= qcrypto_block_luks_essiv_cipher(cipheralg
,
798 error_propagate(errp
, local_err
);
802 /* Note we parsed the ivhash_name earlier in the cipher_mode
803 * spec string even with plain/plain64 ivgens, but we
804 * will ignore it, since it is irrelevant for these ivgens.
805 * This is for compat with dm-crypt which will silently
806 * ignore hash names with these ivgens rather than report
807 * an error about the invalid usage
809 ivcipheralg
= cipheralg
;
812 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
813 /* Try to find which key slot our password is valid for
814 * and unlock the master key from that slot.
816 if (qcrypto_block_luks_find_key(block
,
818 cipheralg
, ciphermode
,
823 &masterkey
, &masterkeylen
,
830 /* We have a valid master key now, so can setup the
831 * block device payload decryption objects
833 block
->kdfhash
= hash
;
834 block
->niv
= qcrypto_cipher_get_iv_len(cipheralg
,
836 block
->ivgen
= qcrypto_ivgen_new(ivalg
,
839 masterkey
, masterkeylen
,
846 block
->cipher
= qcrypto_cipher_new(cipheralg
,
848 masterkey
, masterkeylen
,
850 if (!block
->cipher
) {
856 block
->payload_offset
= luks
->header
.payload_offset
*
857 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
859 luks
->cipher_alg
= cipheralg
;
860 luks
->cipher_mode
= ciphermode
;
861 luks
->ivgen_alg
= ivalg
;
862 luks
->ivgen_hash_alg
= ivhash
;
863 luks
->hash_alg
= hash
;
872 qcrypto_cipher_free(block
->cipher
);
873 qcrypto_ivgen_free(block
->ivgen
);
881 qcrypto_block_luks_uuid_gen(uint8_t *uuidstr
, Error
**errp
)
886 uuid_unparse(uuid
, (char *)uuidstr
);
889 error_setg(errp
, "Unable to generate uuids on this platform");
895 qcrypto_block_luks_create(QCryptoBlock
*block
,
896 QCryptoBlockCreateOptions
*options
,
897 QCryptoBlockInitFunc initfunc
,
898 QCryptoBlockWriteFunc writefunc
,
902 QCryptoBlockLUKS
*luks
;
903 QCryptoBlockCreateOptionsLUKS luks_opts
;
904 Error
*local_err
= NULL
;
905 uint8_t *masterkey
= NULL
;
906 uint8_t *slotkey
= NULL
;
907 uint8_t *splitkey
= NULL
;
908 size_t splitkeylen
= 0;
910 QCryptoCipher
*cipher
= NULL
;
911 QCryptoIVGen
*ivgen
= NULL
;
913 const char *cipher_alg
;
914 const char *cipher_mode
;
915 const char *ivgen_alg
;
916 const char *ivgen_hash_alg
= NULL
;
917 const char *hash_alg
;
918 char *cipher_mode_spec
= NULL
;
919 QCryptoCipherAlgorithm ivcipheralg
= 0;
922 memcpy(&luks_opts
, &options
->u
.luks
, sizeof(luks_opts
));
923 if (!luks_opts
.has_iter_time
) {
924 luks_opts
.iter_time
= 1000;
926 if (!luks_opts
.has_cipher_alg
) {
927 luks_opts
.cipher_alg
= QCRYPTO_CIPHER_ALG_AES_256
;
929 if (!luks_opts
.has_cipher_mode
) {
930 luks_opts
.cipher_mode
= QCRYPTO_CIPHER_MODE_XTS
;
932 if (!luks_opts
.has_ivgen_alg
) {
933 luks_opts
.ivgen_alg
= QCRYPTO_IVGEN_ALG_PLAIN64
;
935 if (!luks_opts
.has_hash_alg
) {
936 luks_opts
.hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
938 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
939 if (!luks_opts
.has_ivgen_hash_alg
) {
940 luks_opts
.ivgen_hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
941 luks_opts
.has_ivgen_hash_alg
= true;
944 /* Note we're allowing ivgen_hash_alg to be set even for
945 * non-essiv iv generators that don't need a hash. It will
946 * be silently ignored, for compatibility with dm-crypt */
948 if (!options
->u
.luks
.key_secret
) {
949 error_setg(errp
, "Parameter 'key-secret' is required for cipher");
952 password
= qcrypto_secret_lookup_as_utf8(luks_opts
.key_secret
, errp
);
957 luks
= g_new0(QCryptoBlockLUKS
, 1);
958 block
->opaque
= luks
;
960 memcpy(luks
->header
.magic
, qcrypto_block_luks_magic
,
961 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
);
963 /* We populate the header in native endianness initially and
964 * then convert everything to big endian just before writing
967 luks
->header
.version
= QCRYPTO_BLOCK_LUKS_VERSION
;
968 if (qcrypto_block_luks_uuid_gen(luks
->header
.uuid
,
973 cipher_alg
= qcrypto_block_luks_cipher_alg_lookup(luks_opts
.cipher_alg
,
979 cipher_mode
= QCryptoCipherMode_lookup
[luks_opts
.cipher_mode
];
980 ivgen_alg
= QCryptoIVGenAlgorithm_lookup
[luks_opts
.ivgen_alg
];
981 if (luks_opts
.has_ivgen_hash_alg
) {
982 ivgen_hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.ivgen_hash_alg
];
983 cipher_mode_spec
= g_strdup_printf("%s-%s:%s", cipher_mode
, ivgen_alg
,
986 cipher_mode_spec
= g_strdup_printf("%s-%s", cipher_mode
, ivgen_alg
);
988 hash_alg
= QCryptoHashAlgorithm_lookup
[luks_opts
.hash_alg
];
991 if (strlen(cipher_alg
) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
) {
992 error_setg(errp
, "Cipher name '%s' is too long for LUKS header",
996 if (strlen(cipher_mode_spec
) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
) {
997 error_setg(errp
, "Cipher mode '%s' is too long for LUKS header",
1001 if (strlen(hash_alg
) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
) {
1002 error_setg(errp
, "Hash name '%s' is too long for LUKS header",
1007 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
1008 ivcipheralg
= qcrypto_block_luks_essiv_cipher(luks_opts
.cipher_alg
,
1009 luks_opts
.ivgen_hash_alg
,
1012 error_propagate(errp
, local_err
);
1016 ivcipheralg
= luks_opts
.cipher_alg
;
1019 strcpy(luks
->header
.cipher_name
, cipher_alg
);
1020 strcpy(luks
->header
.cipher_mode
, cipher_mode_spec
);
1021 strcpy(luks
->header
.hash_spec
, hash_alg
);
1023 luks
->header
.key_bytes
= qcrypto_cipher_get_key_len(luks_opts
.cipher_alg
);
1024 if (luks_opts
.cipher_mode
== QCRYPTO_CIPHER_MODE_XTS
) {
1025 luks
->header
.key_bytes
*= 2;
1028 /* Generate the salt used for hashing the master key
1031 if (qcrypto_random_bytes(luks
->header
.master_key_salt
,
1032 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1037 /* Generate random master key */
1038 masterkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
1039 if (qcrypto_random_bytes(masterkey
,
1040 luks
->header
.key_bytes
, errp
) < 0) {
1045 /* Setup the block device payload encryption objects */
1046 block
->cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1047 luks_opts
.cipher_mode
,
1048 masterkey
, luks
->header
.key_bytes
,
1050 if (!block
->cipher
) {
1054 block
->kdfhash
= luks_opts
.hash_alg
;
1055 block
->niv
= qcrypto_cipher_get_iv_len(luks_opts
.cipher_alg
,
1056 luks_opts
.cipher_mode
);
1057 block
->ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1059 luks_opts
.ivgen_hash_alg
,
1060 masterkey
, luks
->header
.key_bytes
,
1063 if (!block
->ivgen
) {
1068 /* Determine how many iterations we need to hash the master
1069 * key, in order to have 1 second of compute time used
1071 iters
= qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1072 masterkey
, luks
->header
.key_bytes
,
1073 luks
->header
.master_key_salt
,
1074 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1075 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1078 error_propagate(errp
, local_err
);
1082 if (iters
> (ULLONG_MAX
/ luks_opts
.iter_time
)) {
1083 error_setg_errno(errp
, ERANGE
,
1084 "PBKDF iterations %llu too large to scale",
1085 (unsigned long long)iters
);
1089 /* iter_time was in millis, but count_iters reported for secs */
1090 iters
= iters
* luks_opts
.iter_time
/ 1000;
1092 /* Why /= 8 ? That matches cryptsetup, but there's no
1093 * explanation why they chose /= 8... Probably so that
1094 * if all 8 keyslots are active we only spend 1 second
1095 * in total time to check all keys */
1097 if (iters
> UINT32_MAX
) {
1098 error_setg_errno(errp
, ERANGE
,
1099 "PBKDF iterations %llu larger than %u",
1100 (unsigned long long)iters
, UINT32_MAX
);
1103 iters
= MAX(iters
, QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS
);
1104 luks
->header
.master_key_iterations
= iters
;
1106 /* Hash the master key, saving the result in the LUKS
1107 * header. This hash is used when opening the encrypted
1108 * device to verify that the user password unlocked a
1111 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1112 masterkey
, luks
->header
.key_bytes
,
1113 luks
->header
.master_key_salt
,
1114 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1115 luks
->header
.master_key_iterations
,
1116 luks
->header
.master_key_digest
,
1117 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1123 /* Although LUKS has multiple key slots, we're just going
1124 * to use the first key slot */
1125 splitkeylen
= luks
->header
.key_bytes
* QCRYPTO_BLOCK_LUKS_STRIPES
;
1126 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1127 luks
->header
.key_slots
[i
].active
= i
== 0 ?
1128 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
:
1129 QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
;
1130 luks
->header
.key_slots
[i
].stripes
= QCRYPTO_BLOCK_LUKS_STRIPES
;
1132 /* This calculation doesn't match that shown in the spec,
1133 * but instead follows the cryptsetup implementation.
1135 luks
->header
.key_slots
[i
].key_offset
=
1136 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1137 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1138 (ROUND_UP(DIV_ROUND_UP(splitkeylen
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1139 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1140 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) * i
);
1143 if (qcrypto_random_bytes(luks
->header
.key_slots
[0].salt
,
1144 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1149 /* Again we determine how many iterations are required to
1150 * hash the user password while consuming 1 second of compute
1152 iters
= qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1153 (uint8_t *)password
, strlen(password
),
1154 luks
->header
.key_slots
[0].salt
,
1155 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1156 luks
->header
.key_bytes
,
1159 error_propagate(errp
, local_err
);
1163 if (iters
> (ULLONG_MAX
/ luks_opts
.iter_time
)) {
1164 error_setg_errno(errp
, ERANGE
,
1165 "PBKDF iterations %llu too large to scale",
1166 (unsigned long long)iters
);
1170 /* iter_time was in millis, but count_iters reported for secs */
1171 iters
= iters
* luks_opts
.iter_time
/ 1000;
1173 /* Why /= 2 ? That matches cryptsetup, but there's no
1174 * explanation why they chose /= 2... */
1177 if (iters
> UINT32_MAX
) {
1178 error_setg_errno(errp
, ERANGE
,
1179 "PBKDF iterations %llu larger than %u",
1180 (unsigned long long)iters
, UINT32_MAX
);
1184 luks
->header
.key_slots
[0].iterations
=
1185 MAX(iters
, QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS
);
1188 /* Generate a key that we'll use to encrypt the master
1189 * key, from the user's password
1191 slotkey
= g_new0(uint8_t, luks
->header
.key_bytes
);
1192 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1193 (uint8_t *)password
, strlen(password
),
1194 luks
->header
.key_slots
[0].salt
,
1195 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1196 luks
->header
.key_slots
[0].iterations
,
1197 slotkey
, luks
->header
.key_bytes
,
1203 /* Setup the encryption objects needed to encrypt the
1204 * master key material
1206 cipher
= qcrypto_cipher_new(luks_opts
.cipher_alg
,
1207 luks_opts
.cipher_mode
,
1208 slotkey
, luks
->header
.key_bytes
,
1214 ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1216 luks_opts
.ivgen_hash_alg
,
1217 slotkey
, luks
->header
.key_bytes
,
1223 /* Before storing the master key, we need to vastly
1224 * increase its size, as protection against forensic
1225 * disk data recovery */
1226 splitkey
= g_new0(uint8_t, splitkeylen
);
1228 if (qcrypto_afsplit_encode(luks_opts
.hash_alg
,
1229 luks
->header
.key_bytes
,
1230 luks
->header
.key_slots
[0].stripes
,
1237 /* Now we encrypt the split master key with the key generated
1238 * from the user's password, before storing it */
1239 if (qcrypto_block_encrypt_helper(cipher
, block
->niv
, ivgen
,
1240 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1249 /* The total size of the LUKS headers is the partition header + key
1250 * slot headers, rounded up to the nearest sector, combined with
1251 * the size of each master key material region, also rounded up
1252 * to the nearest sector */
1253 luks
->header
.payload_offset
=
1254 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1255 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
) +
1256 (ROUND_UP(DIV_ROUND_UP(splitkeylen
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
),
1257 (QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1258 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) *
1259 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1261 block
->payload_offset
= luks
->header
.payload_offset
*
1262 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1264 /* Reserve header space to match payload offset */
1265 initfunc(block
, block
->payload_offset
, &local_err
, opaque
);
1267 error_propagate(errp
, local_err
);
1271 /* Everything on disk uses Big Endian, so flip header fields
1272 * before writing them */
1273 cpu_to_be16s(&luks
->header
.version
);
1274 cpu_to_be32s(&luks
->header
.payload_offset
);
1275 cpu_to_be32s(&luks
->header
.key_bytes
);
1276 cpu_to_be32s(&luks
->header
.master_key_iterations
);
1278 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1279 cpu_to_be32s(&luks
->header
.key_slots
[i
].active
);
1280 cpu_to_be32s(&luks
->header
.key_slots
[i
].iterations
);
1281 cpu_to_be32s(&luks
->header
.key_slots
[i
].key_offset
);
1282 cpu_to_be32s(&luks
->header
.key_slots
[i
].stripes
);
1286 /* Write out the partition header and key slot headers */
1288 (const uint8_t *)&luks
->header
,
1289 sizeof(luks
->header
),
1293 /* Delay checking local_err until we've byte-swapped */
1295 /* Byte swap the header back to native, in case we need
1296 * to read it again later */
1297 be16_to_cpus(&luks
->header
.version
);
1298 be32_to_cpus(&luks
->header
.payload_offset
);
1299 be32_to_cpus(&luks
->header
.key_bytes
);
1300 be32_to_cpus(&luks
->header
.master_key_iterations
);
1302 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1303 be32_to_cpus(&luks
->header
.key_slots
[i
].active
);
1304 be32_to_cpus(&luks
->header
.key_slots
[i
].iterations
);
1305 be32_to_cpus(&luks
->header
.key_slots
[i
].key_offset
);
1306 be32_to_cpus(&luks
->header
.key_slots
[i
].stripes
);
1310 error_propagate(errp
, local_err
);
1314 /* Write out the master key material, starting at the
1315 * sector immediately following the partition header. */
1316 if (writefunc(block
,
1317 luks
->header
.key_slots
[0].key_offset
*
1318 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1319 splitkey
, splitkeylen
,
1321 opaque
) != splitkeylen
) {
1325 luks
->cipher_alg
= luks_opts
.cipher_alg
;
1326 luks
->cipher_mode
= luks_opts
.cipher_mode
;
1327 luks
->ivgen_alg
= luks_opts
.ivgen_alg
;
1328 luks
->ivgen_hash_alg
= luks_opts
.ivgen_hash_alg
;
1329 luks
->hash_alg
= luks_opts
.hash_alg
;
1331 memset(masterkey
, 0, luks
->header
.key_bytes
);
1333 memset(slotkey
, 0, luks
->header
.key_bytes
);
1337 g_free(cipher_mode_spec
);
1339 qcrypto_ivgen_free(ivgen
);
1340 qcrypto_cipher_free(cipher
);
1346 memset(masterkey
, 0, luks
->header
.key_bytes
);
1350 memset(slotkey
, 0, luks
->header
.key_bytes
);
1355 g_free(cipher_mode_spec
);
1357 qcrypto_ivgen_free(ivgen
);
1358 qcrypto_cipher_free(cipher
);
1365 static int qcrypto_block_luks_get_info(QCryptoBlock
*block
,
1366 QCryptoBlockInfo
*info
,
1369 QCryptoBlockLUKS
*luks
= block
->opaque
;
1370 QCryptoBlockInfoLUKSSlot
*slot
;
1371 QCryptoBlockInfoLUKSSlotList
*slots
= NULL
, **prev
= &info
->u
.luks
.slots
;
1374 info
->u
.luks
.cipher_alg
= luks
->cipher_alg
;
1375 info
->u
.luks
.cipher_mode
= luks
->cipher_mode
;
1376 info
->u
.luks
.ivgen_alg
= luks
->ivgen_alg
;
1377 if (info
->u
.luks
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
1378 info
->u
.luks
.has_ivgen_hash_alg
= true;
1379 info
->u
.luks
.ivgen_hash_alg
= luks
->ivgen_hash_alg
;
1381 info
->u
.luks
.hash_alg
= luks
->hash_alg
;
1382 info
->u
.luks
.payload_offset
= block
->payload_offset
;
1383 info
->u
.luks
.master_key_iters
= luks
->header
.master_key_iterations
;
1384 info
->u
.luks
.uuid
= g_strndup((const char *)luks
->header
.uuid
,
1385 sizeof(luks
->header
.uuid
));
1387 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1388 slots
= g_new0(QCryptoBlockInfoLUKSSlotList
, 1);
1391 slots
->value
= slot
= g_new0(QCryptoBlockInfoLUKSSlot
, 1);
1392 slot
->active
= luks
->header
.key_slots
[i
].active
==
1393 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
;
1394 slot
->key_offset
= luks
->header
.key_slots
[i
].key_offset
1395 * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1397 slot
->has_iters
= true;
1398 slot
->iters
= luks
->header
.key_slots
[i
].iterations
;
1399 slot
->has_stripes
= true;
1400 slot
->stripes
= luks
->header
.key_slots
[i
].stripes
;
1403 prev
= &slots
->next
;
1410 static void qcrypto_block_luks_cleanup(QCryptoBlock
*block
)
1412 g_free(block
->opaque
);
1417 qcrypto_block_luks_decrypt(QCryptoBlock
*block
,
1418 uint64_t startsector
,
1423 return qcrypto_block_decrypt_helper(block
->cipher
,
1424 block
->niv
, block
->ivgen
,
1425 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1426 startsector
, buf
, len
, errp
);
1431 qcrypto_block_luks_encrypt(QCryptoBlock
*block
,
1432 uint64_t startsector
,
1437 return qcrypto_block_encrypt_helper(block
->cipher
,
1438 block
->niv
, block
->ivgen
,
1439 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1440 startsector
, buf
, len
, errp
);
1444 const QCryptoBlockDriver qcrypto_block_driver_luks
= {
1445 .open
= qcrypto_block_luks_open
,
1446 .create
= qcrypto_block_luks_create
,
1447 .get_info
= qcrypto_block_luks_get_info
,
1448 .cleanup
= qcrypto_block_luks_cleanup
,
1449 .decrypt
= qcrypto_block_luks_decrypt
,
1450 .encrypt
= qcrypto_block_luks_encrypt
,
1451 .has_format
= qcrypto_block_luks_has_format
,