2 * QEMU Crypto block device encryption LUKS format
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/bswap.h"
25 #include "block-luks.h"
26 #include "block-luks-priv.h"
28 #include "crypto/hash.h"
29 #include "crypto/afsplit.h"
30 #include "crypto/pbkdf.h"
31 #include "crypto/secret.h"
32 #include "crypto/random.h"
33 #include "qemu/uuid.h"
35 #include "qemu/bitmap.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
;
50 typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap
;
51 struct QCryptoBlockLUKSNameMap
{
56 typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap
;
57 struct QCryptoBlockLUKSCipherSizeMap
{
61 typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap
;
62 struct QCryptoBlockLUKSCipherNameMap
{
64 const QCryptoBlockLUKSCipherSizeMap
*sizes
;
68 static const QCryptoBlockLUKSCipherSizeMap
69 qcrypto_block_luks_cipher_size_map_aes
[] = {
70 { 16, QCRYPTO_CIPHER_ALG_AES_128
},
71 { 24, QCRYPTO_CIPHER_ALG_AES_192
},
72 { 32, QCRYPTO_CIPHER_ALG_AES_256
},
76 static const QCryptoBlockLUKSCipherSizeMap
77 qcrypto_block_luks_cipher_size_map_cast5
[] = {
78 { 16, QCRYPTO_CIPHER_ALG_CAST5_128
},
82 static const QCryptoBlockLUKSCipherSizeMap
83 qcrypto_block_luks_cipher_size_map_serpent
[] = {
84 { 16, QCRYPTO_CIPHER_ALG_SERPENT_128
},
85 { 24, QCRYPTO_CIPHER_ALG_SERPENT_192
},
86 { 32, QCRYPTO_CIPHER_ALG_SERPENT_256
},
90 static const QCryptoBlockLUKSCipherSizeMap
91 qcrypto_block_luks_cipher_size_map_twofish
[] = {
92 { 16, QCRYPTO_CIPHER_ALG_TWOFISH_128
},
93 { 24, QCRYPTO_CIPHER_ALG_TWOFISH_192
},
94 { 32, QCRYPTO_CIPHER_ALG_TWOFISH_256
},
98 static const QCryptoBlockLUKSCipherNameMap
99 qcrypto_block_luks_cipher_name_map
[] = {
100 { "aes", qcrypto_block_luks_cipher_size_map_aes
},
101 { "cast5", qcrypto_block_luks_cipher_size_map_cast5
},
102 { "serpent", qcrypto_block_luks_cipher_size_map_serpent
},
103 { "twofish", qcrypto_block_luks_cipher_size_map_twofish
},
106 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot
) != 48);
107 QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader
) != 592);
110 struct QCryptoBlockLUKS
{
111 QCryptoBlockLUKSHeader header
;
113 /* Main encryption algorithm used for encryption*/
114 QCryptoCipherAlgorithm cipher_alg
;
116 /* Mode of encryption for the selected encryption algorithm */
117 QCryptoCipherMode cipher_mode
;
119 /* Initialization vector generation algorithm */
120 QCryptoIVGenAlgorithm ivgen_alg
;
122 /* Hash algorithm used for IV generation*/
123 QCryptoHashAlgorithm ivgen_hash_alg
;
126 * Encryption algorithm used for IV generation.
127 * Usually the same as main encryption algorithm
129 QCryptoCipherAlgorithm ivgen_cipher_alg
;
131 /* Hash algorithm used in pbkdf2 function */
132 QCryptoHashAlgorithm hash_alg
;
134 /* Name of the secret that was used to open the image */
139 static int qcrypto_block_luks_cipher_name_lookup(const char *name
,
140 QCryptoCipherMode mode
,
144 const QCryptoBlockLUKSCipherNameMap
*map
=
145 qcrypto_block_luks_cipher_name_map
;
146 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
149 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
153 for (i
= 0; i
< maplen
; i
++) {
154 if (!g_str_equal(map
[i
].name
, name
)) {
157 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
158 if (map
[i
].sizes
[j
].key_bytes
== key_bytes
) {
159 return map
[i
].sizes
[j
].id
;
164 error_setg(errp
, "Algorithm '%s' with key size %d bytes not supported",
170 qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg
,
173 const QCryptoBlockLUKSCipherNameMap
*map
=
174 qcrypto_block_luks_cipher_name_map
;
175 size_t maplen
= G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map
);
177 for (i
= 0; i
< maplen
; i
++) {
178 for (j
= 0; j
< map
[i
].sizes
[j
].key_bytes
; j
++) {
179 if (map
[i
].sizes
[j
].id
== alg
) {
185 error_setg(errp
, "Algorithm '%s' not supported",
186 QCryptoCipherAlgorithm_str(alg
));
190 /* XXX replace with qapi_enum_parse() in future, when we can
191 * make that function emit a more friendly error message */
192 static int qcrypto_block_luks_name_lookup(const char *name
,
193 const QEnumLookup
*map
,
197 int ret
= qapi_enum_parse(map
, name
, -1, NULL
);
200 error_setg(errp
, "%s '%s' not supported", type
, name
);
206 #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
207 qcrypto_block_luks_name_lookup(name, \
208 &QCryptoCipherMode_lookup, \
212 #define qcrypto_block_luks_hash_name_lookup(name, errp) \
213 qcrypto_block_luks_name_lookup(name, \
214 &QCryptoHashAlgorithm_lookup, \
218 #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
219 qcrypto_block_luks_name_lookup(name, \
220 &QCryptoIVGenAlgorithm_lookup, \
226 qcrypto_block_luks_has_format(const uint8_t *buf
,
229 const QCryptoBlockLUKSHeader
*luks_header
= (const void *)buf
;
231 if (buf_size
>= offsetof(QCryptoBlockLUKSHeader
, cipher_name
) &&
232 memcmp(luks_header
->magic
, qcrypto_block_luks_magic
,
233 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) == 0 &&
234 be16_to_cpu(luks_header
->version
) == QCRYPTO_BLOCK_LUKS_VERSION
) {
243 * Deal with a quirk of dm-crypt usage of ESSIV.
245 * When calculating ESSIV IVs, the cipher length used by ESSIV
246 * may be different from the cipher length used for the block
247 * encryption, because dm-crypt uses the hash digest length
248 * as the key size. ie, if you have AES 128 as the block cipher
249 * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
250 * the cipher since that gets a key length matching the digest
251 * size, not AES 128 with truncated digest as might be imagined
253 static QCryptoCipherAlgorithm
254 qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher
,
255 QCryptoHashAlgorithm hash
,
258 size_t digestlen
= qcrypto_hash_digest_len(hash
);
259 size_t keylen
= qcrypto_cipher_get_key_len(cipher
);
260 if (digestlen
== keylen
) {
265 case QCRYPTO_CIPHER_ALG_AES_128
:
266 case QCRYPTO_CIPHER_ALG_AES_192
:
267 case QCRYPTO_CIPHER_ALG_AES_256
:
268 if (digestlen
== qcrypto_cipher_get_key_len(
269 QCRYPTO_CIPHER_ALG_AES_128
)) {
270 return QCRYPTO_CIPHER_ALG_AES_128
;
271 } else if (digestlen
== qcrypto_cipher_get_key_len(
272 QCRYPTO_CIPHER_ALG_AES_192
)) {
273 return QCRYPTO_CIPHER_ALG_AES_192
;
274 } else if (digestlen
== qcrypto_cipher_get_key_len(
275 QCRYPTO_CIPHER_ALG_AES_256
)) {
276 return QCRYPTO_CIPHER_ALG_AES_256
;
278 error_setg(errp
, "No AES cipher with key size %zu available",
283 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
284 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
285 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
286 if (digestlen
== qcrypto_cipher_get_key_len(
287 QCRYPTO_CIPHER_ALG_SERPENT_128
)) {
288 return QCRYPTO_CIPHER_ALG_SERPENT_128
;
289 } else if (digestlen
== qcrypto_cipher_get_key_len(
290 QCRYPTO_CIPHER_ALG_SERPENT_192
)) {
291 return QCRYPTO_CIPHER_ALG_SERPENT_192
;
292 } else if (digestlen
== qcrypto_cipher_get_key_len(
293 QCRYPTO_CIPHER_ALG_SERPENT_256
)) {
294 return QCRYPTO_CIPHER_ALG_SERPENT_256
;
296 error_setg(errp
, "No Serpent cipher with key size %zu available",
301 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
302 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
303 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
304 if (digestlen
== qcrypto_cipher_get_key_len(
305 QCRYPTO_CIPHER_ALG_TWOFISH_128
)) {
306 return QCRYPTO_CIPHER_ALG_TWOFISH_128
;
307 } else if (digestlen
== qcrypto_cipher_get_key_len(
308 QCRYPTO_CIPHER_ALG_TWOFISH_192
)) {
309 return QCRYPTO_CIPHER_ALG_TWOFISH_192
;
310 } else if (digestlen
== qcrypto_cipher_get_key_len(
311 QCRYPTO_CIPHER_ALG_TWOFISH_256
)) {
312 return QCRYPTO_CIPHER_ALG_TWOFISH_256
;
314 error_setg(errp
, "No Twofish cipher with key size %zu available",
320 error_setg(errp
, "Cipher %s not supported with essiv",
321 QCryptoCipherAlgorithm_str(cipher
));
327 * Returns number of sectors needed to store the key material
328 * given number of anti forensic stripes
331 qcrypto_block_luks_splitkeylen_sectors(const QCryptoBlockLUKS
*luks
,
332 unsigned int header_sectors
,
333 unsigned int stripes
)
336 * This calculation doesn't match that shown in the spec,
337 * but instead follows the cryptsetup implementation.
340 size_t splitkeylen
= luks
->header
.master_key_len
* stripes
;
342 /* First align the key material size to block size*/
343 size_t splitkeylen_sectors
=
344 DIV_ROUND_UP(splitkeylen
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
);
346 /* Then also align the key material size to the size of the header */
347 return ROUND_UP(splitkeylen_sectors
, header_sectors
);
352 qcrypto_block_luks_to_disk_endian(QCryptoBlockLUKSHeader
*hdr
)
357 * Everything on disk uses Big Endian (tm), so flip header fields
358 * before writing them
360 cpu_to_be16s(&hdr
->version
);
361 cpu_to_be32s(&hdr
->payload_offset_sector
);
362 cpu_to_be32s(&hdr
->master_key_len
);
363 cpu_to_be32s(&hdr
->master_key_iterations
);
365 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
366 cpu_to_be32s(&hdr
->key_slots
[i
].active
);
367 cpu_to_be32s(&hdr
->key_slots
[i
].iterations
);
368 cpu_to_be32s(&hdr
->key_slots
[i
].key_offset_sector
);
369 cpu_to_be32s(&hdr
->key_slots
[i
].stripes
);
374 qcrypto_block_luks_from_disk_endian(QCryptoBlockLUKSHeader
*hdr
)
379 * The header is always stored in big-endian format, so
380 * convert everything to native
382 be16_to_cpus(&hdr
->version
);
383 be32_to_cpus(&hdr
->payload_offset_sector
);
384 be32_to_cpus(&hdr
->master_key_len
);
385 be32_to_cpus(&hdr
->master_key_iterations
);
387 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
388 be32_to_cpus(&hdr
->key_slots
[i
].active
);
389 be32_to_cpus(&hdr
->key_slots
[i
].iterations
);
390 be32_to_cpus(&hdr
->key_slots
[i
].key_offset_sector
);
391 be32_to_cpus(&hdr
->key_slots
[i
].stripes
);
396 * Stores the main LUKS header, taking care of endianness
399 qcrypto_block_luks_store_header(QCryptoBlock
*block
,
400 QCryptoBlockWriteFunc writefunc
,
404 const QCryptoBlockLUKS
*luks
= block
->opaque
;
405 Error
*local_err
= NULL
;
406 g_autofree QCryptoBlockLUKSHeader
*hdr_copy
= NULL
;
408 /* Create a copy of the header */
409 hdr_copy
= g_new0(QCryptoBlockLUKSHeader
, 1);
410 memcpy(hdr_copy
, &luks
->header
, sizeof(QCryptoBlockLUKSHeader
));
412 qcrypto_block_luks_to_disk_endian(hdr_copy
);
414 /* Write out the partition header and key slot headers */
415 writefunc(block
, 0, (const uint8_t *)hdr_copy
, sizeof(*hdr_copy
),
419 error_propagate(errp
, local_err
);
426 * Loads the main LUKS header, and byteswaps it to native endianness
427 * And run basic sanity checks on it
430 qcrypto_block_luks_load_header(QCryptoBlock
*block
,
431 QCryptoBlockReadFunc readfunc
,
436 QCryptoBlockLUKS
*luks
= block
->opaque
;
439 * Read the entire LUKS header, minus the key material from
440 * the underlying device
442 rv
= readfunc(block
, 0,
443 (uint8_t *)&luks
->header
,
444 sizeof(luks
->header
),
451 qcrypto_block_luks_from_disk_endian(&luks
->header
);
457 * Does basic sanity checks on the LUKS header
460 qcrypto_block_luks_check_header(const QCryptoBlockLUKS
*luks
, Error
**errp
)
464 unsigned int header_sectors
= QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
465 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
467 if (memcmp(luks
->header
.magic
, qcrypto_block_luks_magic
,
468 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
) != 0) {
469 error_setg(errp
, "Volume is not in LUKS format");
473 if (luks
->header
.version
!= QCRYPTO_BLOCK_LUKS_VERSION
) {
474 error_setg(errp
, "LUKS version %" PRIu32
" is not supported",
475 luks
->header
.version
);
479 if (!memchr(luks
->header
.cipher_name
, '\0',
480 sizeof(luks
->header
.cipher_name
))) {
481 error_setg(errp
, "LUKS header cipher name is not NUL terminated");
485 if (!memchr(luks
->header
.cipher_mode
, '\0',
486 sizeof(luks
->header
.cipher_mode
))) {
487 error_setg(errp
, "LUKS header cipher mode is not NUL terminated");
491 if (!memchr(luks
->header
.hash_spec
, '\0',
492 sizeof(luks
->header
.hash_spec
))) {
493 error_setg(errp
, "LUKS header hash spec is not NUL terminated");
497 if (luks
->header
.payload_offset_sector
<
498 DIV_ROUND_UP(QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
,
499 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) {
500 error_setg(errp
, "LUKS payload is overlapping with the header");
504 if (luks
->header
.master_key_iterations
== 0) {
505 error_setg(errp
, "LUKS key iteration count is zero");
509 /* Check all keyslots for corruption */
510 for (i
= 0 ; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
512 const QCryptoBlockLUKSKeySlot
*slot1
= &luks
->header
.key_slots
[i
];
513 unsigned int start1
= slot1
->key_offset_sector
;
515 qcrypto_block_luks_splitkeylen_sectors(luks
,
519 if (slot1
->stripes
!= QCRYPTO_BLOCK_LUKS_STRIPES
) {
520 error_setg(errp
, "Keyslot %zu is corrupted (stripes %d != %d)",
521 i
, slot1
->stripes
, QCRYPTO_BLOCK_LUKS_STRIPES
);
525 if (slot1
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
&&
526 slot1
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
) {
528 "Keyslot %zu state (active/disable) is corrupted", i
);
532 if (slot1
->active
== QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
&&
533 slot1
->iterations
== 0) {
534 error_setg(errp
, "Keyslot %zu iteration count is zero", i
);
538 if (start1
< DIV_ROUND_UP(QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
,
539 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
)) {
541 "Keyslot %zu is overlapping with the LUKS header",
546 if (start1
+ len1
> luks
->header
.payload_offset_sector
) {
548 "Keyslot %zu is overlapping with the encrypted payload",
553 for (j
= i
+ 1 ; j
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; j
++) {
554 const QCryptoBlockLUKSKeySlot
*slot2
= &luks
->header
.key_slots
[j
];
555 unsigned int start2
= slot2
->key_offset_sector
;
557 qcrypto_block_luks_splitkeylen_sectors(luks
,
561 if (start1
+ len1
> start2
&& start2
+ len2
> start1
) {
563 "Keyslots %zu and %zu are overlapping in the header",
574 * Parses the crypto parameters that are stored in the LUKS header
578 qcrypto_block_luks_parse_header(QCryptoBlockLUKS
*luks
, Error
**errp
)
580 g_autofree
char *cipher_mode
= g_strdup(luks
->header
.cipher_mode
);
581 char *ivgen_name
, *ivhash_name
;
582 Error
*local_err
= NULL
;
585 * The cipher_mode header contains a string that we have
586 * to further parse, of the format
588 * <cipher-mode>-<iv-generator>[:<iv-hash>]
590 * eg cbc-essiv:sha256, cbc-plain64
592 ivgen_name
= strchr(cipher_mode
, '-');
594 error_setg(errp
, "Unexpected cipher mode string format '%s'",
595 luks
->header
.cipher_mode
);
601 ivhash_name
= strchr(ivgen_name
, ':');
603 luks
->ivgen_hash_alg
= 0;
608 luks
->ivgen_hash_alg
= qcrypto_block_luks_hash_name_lookup(ivhash_name
,
611 error_propagate(errp
, local_err
);
616 luks
->cipher_mode
= qcrypto_block_luks_cipher_mode_lookup(cipher_mode
,
619 error_propagate(errp
, local_err
);
624 qcrypto_block_luks_cipher_name_lookup(luks
->header
.cipher_name
,
626 luks
->header
.master_key_len
,
629 error_propagate(errp
, local_err
);
634 qcrypto_block_luks_hash_name_lookup(luks
->header
.hash_spec
,
637 error_propagate(errp
, local_err
);
641 luks
->ivgen_alg
= qcrypto_block_luks_ivgen_name_lookup(ivgen_name
,
644 error_propagate(errp
, local_err
);
648 if (luks
->ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
650 error_setg(errp
, "Missing IV generator hash specification");
653 luks
->ivgen_cipher_alg
=
654 qcrypto_block_luks_essiv_cipher(luks
->cipher_alg
,
655 luks
->ivgen_hash_alg
,
658 error_propagate(errp
, local_err
);
664 * Note we parsed the ivhash_name earlier in the cipher_mode
665 * spec string even with plain/plain64 ivgens, but we
666 * will ignore it, since it is irrelevant for these ivgens.
667 * This is for compat with dm-crypt which will silently
668 * ignore hash names with these ivgens rather than report
669 * an error about the invalid usage
671 luks
->ivgen_cipher_alg
= luks
->cipher_alg
;
677 * Given a key slot, user password, and the master key,
678 * will store the encrypted master key there, and update the
679 * in-memory header. User must then write the in-memory header
682 * 0 if the keyslot was written successfully
683 * with the provided password
684 * -1 if a fatal error occurred while storing the key
687 qcrypto_block_luks_store_key(QCryptoBlock
*block
,
688 unsigned int slot_idx
,
689 const char *password
,
692 QCryptoBlockWriteFunc writefunc
,
696 QCryptoBlockLUKS
*luks
= block
->opaque
;
697 QCryptoBlockLUKSKeySlot
*slot
;
698 g_autofree
uint8_t *splitkey
= NULL
;
700 g_autofree
uint8_t *slotkey
= NULL
;
701 g_autoptr(QCryptoCipher
) cipher
= NULL
;
702 g_autoptr(QCryptoIVGen
) ivgen
= NULL
;
703 Error
*local_err
= NULL
;
707 assert(slot_idx
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
708 slot
= &luks
->header
.key_slots
[slot_idx
];
709 splitkeylen
= luks
->header
.master_key_len
* slot
->stripes
;
711 if (qcrypto_random_bytes(slot
->salt
,
712 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
718 * Determine how many iterations are required to
719 * hash the user password while consuming 1 second of compute
722 iters
= qcrypto_pbkdf2_count_iters(luks
->hash_alg
,
723 (uint8_t *)password
, strlen(password
),
725 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
726 luks
->header
.master_key_len
,
729 error_propagate(errp
, local_err
);
733 if (iters
> (ULLONG_MAX
/ iter_time
)) {
734 error_setg_errno(errp
, ERANGE
,
735 "PBKDF iterations %llu too large to scale",
736 (unsigned long long)iters
);
740 /* iter_time was in millis, but count_iters reported for secs */
741 iters
= iters
* iter_time
/ 1000;
743 if (iters
> UINT32_MAX
) {
744 error_setg_errno(errp
, ERANGE
,
745 "PBKDF iterations %llu larger than %u",
746 (unsigned long long)iters
, UINT32_MAX
);
751 MAX(iters
, QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS
);
755 * Generate a key that we'll use to encrypt the master
756 * key, from the user's password
758 slotkey
= g_new0(uint8_t, luks
->header
.master_key_len
);
759 if (qcrypto_pbkdf2(luks
->hash_alg
,
760 (uint8_t *)password
, strlen(password
),
762 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
764 slotkey
, luks
->header
.master_key_len
,
771 * Setup the encryption objects needed to encrypt the
772 * master key material
774 cipher
= qcrypto_cipher_new(luks
->cipher_alg
,
776 slotkey
, luks
->header
.master_key_len
,
782 ivgen
= qcrypto_ivgen_new(luks
->ivgen_alg
,
783 luks
->ivgen_cipher_alg
,
784 luks
->ivgen_hash_alg
,
785 slotkey
, luks
->header
.master_key_len
,
792 * Before storing the master key, we need to vastly
793 * increase its size, as protection against forensic
796 splitkey
= g_new0(uint8_t, splitkeylen
);
798 if (qcrypto_afsplit_encode(luks
->hash_alg
,
799 luks
->header
.master_key_len
,
808 * Now we encrypt the split master key with the key generated
809 * from the user's password, before storing it
811 if (qcrypto_block_cipher_encrypt_helper(cipher
, block
->niv
, ivgen
,
812 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
820 /* Write out the slot's master key material. */
822 slot
->key_offset_sector
*
823 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
824 splitkey
, splitkeylen
,
830 slot
->active
= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
;
832 if (qcrypto_block_luks_store_header(block
, writefunc
, opaque
, errp
) < 0) {
840 memset(slotkey
, 0, luks
->header
.master_key_len
);
843 memset(splitkey
, 0, splitkeylen
);
849 * Given a key slot, and user password, this will attempt to unlock
850 * the master encryption key from the key slot.
853 * 0 if the key slot is disabled, or key could not be decrypted
854 * with the provided password
855 * 1 if the key slot is enabled, and key decrypted successfully
856 * with the provided password
857 * -1 if a fatal error occurred loading the key
860 qcrypto_block_luks_load_key(QCryptoBlock
*block
,
862 const char *password
,
864 QCryptoBlockReadFunc readfunc
,
868 QCryptoBlockLUKS
*luks
= block
->opaque
;
869 const QCryptoBlockLUKSKeySlot
*slot
;
870 g_autofree
uint8_t *splitkey
= NULL
;
872 g_autofree
uint8_t *possiblekey
= NULL
;
874 g_autoptr(QCryptoCipher
) cipher
= NULL
;
875 uint8_t keydigest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
876 g_autoptr(QCryptoIVGen
) ivgen
= NULL
;
879 assert(slot_idx
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
880 slot
= &luks
->header
.key_slots
[slot_idx
];
881 if (slot
->active
!= QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
) {
885 splitkeylen
= luks
->header
.master_key_len
* slot
->stripes
;
886 splitkey
= g_new0(uint8_t, splitkeylen
);
887 possiblekey
= g_new0(uint8_t, luks
->header
.master_key_len
);
890 * The user password is used to generate a (possible)
891 * decryption key. This may or may not successfully
892 * decrypt the master key - we just blindly assume
893 * the key is correct and validate the results of
896 if (qcrypto_pbkdf2(luks
->hash_alg
,
897 (const uint8_t *)password
, strlen(password
),
898 slot
->salt
, QCRYPTO_BLOCK_LUKS_SALT_LEN
,
900 possiblekey
, luks
->header
.master_key_len
,
906 * We need to read the master key material from the
907 * LUKS key material header. What we're reading is
908 * not the raw master key, but rather the data after
909 * it has been passed through AFSplit and the result
913 slot
->key_offset_sector
* QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
914 splitkey
, splitkeylen
,
922 /* Setup the cipher/ivgen that we'll use to try to decrypt
923 * the split master key material */
924 cipher
= qcrypto_cipher_new(luks
->cipher_alg
,
927 luks
->header
.master_key_len
,
933 niv
= qcrypto_cipher_get_iv_len(luks
->cipher_alg
,
936 ivgen
= qcrypto_ivgen_new(luks
->ivgen_alg
,
937 luks
->ivgen_cipher_alg
,
938 luks
->ivgen_hash_alg
,
940 luks
->header
.master_key_len
,
948 * The master key needs to be decrypted in the same
949 * way that the block device payload will be decrypted
950 * later. In particular we'll be using the IV generator
951 * to reset the encryption cipher every time the master
952 * key crosses a sector boundary.
954 if (qcrypto_block_cipher_decrypt_helper(cipher
,
957 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
966 * Now we've decrypted the split master key, join
967 * it back together to get the actual master key.
969 if (qcrypto_afsplit_decode(luks
->hash_alg
,
970 luks
->header
.master_key_len
,
980 * We still don't know that the masterkey we got is valid,
981 * because we just blindly assumed the user's password
982 * was correct. This is where we now verify it. We are
983 * creating a hash of the master key using PBKDF and
984 * then comparing that to the hash stored in the key slot
987 if (qcrypto_pbkdf2(luks
->hash_alg
,
989 luks
->header
.master_key_len
,
990 luks
->header
.master_key_salt
,
991 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
992 luks
->header
.master_key_iterations
,
994 G_N_ELEMENTS(keydigest
),
999 if (memcmp(keydigest
, luks
->header
.master_key_digest
,
1000 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
) == 0) {
1001 /* Success, we got the right master key */
1005 /* Fail, user's password was not valid for this key slot,
1006 * tell caller to try another slot */
1012 * Given a user password, this will iterate over all key
1013 * slots and try to unlock each active key slot using the
1014 * password until it successfully obtains a master key.
1016 * Returns 0 if a key was loaded, -1 if no keys could be loaded
1019 qcrypto_block_luks_find_key(QCryptoBlock
*block
,
1020 const char *password
,
1022 QCryptoBlockReadFunc readfunc
,
1029 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1030 rv
= qcrypto_block_luks_load_key(block
,
1045 error_setg(errp
, "Invalid password, cannot unlock any keyslot");
1051 * Returns true if a slot i is marked as active
1052 * (contains encrypted copy of the master key)
1055 qcrypto_block_luks_slot_active(const QCryptoBlockLUKS
*luks
,
1056 unsigned int slot_idx
)
1060 assert(slot_idx
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1061 val
= luks
->header
.key_slots
[slot_idx
].active
;
1062 return val
== QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
;
1066 * Returns the number of slots that are marked as active
1067 * (slots that contain encrypted copy of the master key)
1070 qcrypto_block_luks_count_active_slots(const QCryptoBlockLUKS
*luks
)
1073 unsigned int ret
= 0;
1075 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1076 if (qcrypto_block_luks_slot_active(luks
, i
)) {
1084 * Finds first key slot which is not active
1085 * Returns the key slot index, or -1 if it doesn't exist
1088 qcrypto_block_luks_find_free_keyslot(const QCryptoBlockLUKS
*luks
)
1092 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1093 if (!qcrypto_block_luks_slot_active(luks
, i
)) {
1101 * Erases an keyslot given its index
1103 * 0 if the keyslot was erased successfully
1104 * -1 if a error occurred while erasing the keyslot
1108 qcrypto_block_luks_erase_key(QCryptoBlock
*block
,
1109 unsigned int slot_idx
,
1110 QCryptoBlockWriteFunc writefunc
,
1114 QCryptoBlockLUKS
*luks
= block
->opaque
;
1115 QCryptoBlockLUKSKeySlot
*slot
;
1116 g_autofree
uint8_t *garbagesplitkey
= NULL
;
1119 Error
*local_err
= NULL
;
1122 assert(slot_idx
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1123 slot
= &luks
->header
.key_slots
[slot_idx
];
1125 splitkeylen
= luks
->header
.master_key_len
* slot
->stripes
;
1126 assert(splitkeylen
> 0);
1128 garbagesplitkey
= g_new0(uint8_t, splitkeylen
);
1130 /* Reset the key slot header */
1131 memset(slot
->salt
, 0, QCRYPTO_BLOCK_LUKS_SALT_LEN
);
1132 slot
->iterations
= 0;
1133 slot
->active
= QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
;
1135 ret
= qcrypto_block_luks_store_header(block
, writefunc
,
1136 opaque
, &local_err
);
1139 error_propagate(errp
, local_err
);
1142 * Now try to erase the key material, even if the header
1145 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_ERASE_ITERATIONS
; i
++) {
1146 if (qcrypto_random_bytes(garbagesplitkey
,
1147 splitkeylen
, &local_err
) < 0) {
1149 * If we failed to get the random data, still write
1150 * at least zeros to the key slot at least once
1152 error_propagate(errp
, local_err
);
1158 if (writefunc(block
,
1159 slot
->key_offset_sector
* QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1164 error_propagate(errp
, local_err
);
1172 qcrypto_block_luks_open(QCryptoBlock
*block
,
1173 QCryptoBlockOpenOptions
*options
,
1174 const char *optprefix
,
1175 QCryptoBlockReadFunc readfunc
,
1181 QCryptoBlockLUKS
*luks
= NULL
;
1182 g_autofree
uint8_t *masterkey
= NULL
;
1183 g_autofree
char *password
= NULL
;
1185 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
1186 if (!options
->u
.luks
.key_secret
) {
1187 error_setg(errp
, "Parameter '%skey-secret' is required for cipher",
1188 optprefix
? optprefix
: "");
1191 password
= qcrypto_secret_lookup_as_utf8(
1192 options
->u
.luks
.key_secret
, errp
);
1198 luks
= g_new0(QCryptoBlockLUKS
, 1);
1199 block
->opaque
= luks
;
1200 luks
->secret
= g_strdup(options
->u
.luks
.key_secret
);
1202 if (qcrypto_block_luks_load_header(block
, readfunc
, opaque
, errp
) < 0) {
1206 if (qcrypto_block_luks_check_header(luks
, errp
) < 0) {
1210 if (qcrypto_block_luks_parse_header(luks
, errp
) < 0) {
1214 if (!(flags
& QCRYPTO_BLOCK_OPEN_NO_IO
)) {
1215 /* Try to find which key slot our password is valid for
1216 * and unlock the master key from that slot.
1219 masterkey
= g_new0(uint8_t, luks
->header
.master_key_len
);
1221 if (qcrypto_block_luks_find_key(block
,
1229 /* We have a valid master key now, so can setup the
1230 * block device payload decryption objects
1232 block
->kdfhash
= luks
->hash_alg
;
1233 block
->niv
= qcrypto_cipher_get_iv_len(luks
->cipher_alg
,
1236 block
->ivgen
= qcrypto_ivgen_new(luks
->ivgen_alg
,
1237 luks
->ivgen_cipher_alg
,
1238 luks
->ivgen_hash_alg
,
1240 luks
->header
.master_key_len
,
1242 if (!block
->ivgen
) {
1246 if (qcrypto_block_init_cipher(block
,
1250 luks
->header
.master_key_len
,
1257 block
->sector_size
= QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1258 block
->payload_offset
= luks
->header
.payload_offset_sector
*
1264 qcrypto_block_free_cipher(block
);
1265 qcrypto_ivgen_free(block
->ivgen
);
1266 g_free(luks
->secret
);
1273 qcrypto_block_luks_uuid_gen(uint8_t *uuidstr
)
1276 qemu_uuid_generate(&uuid
);
1277 qemu_uuid_unparse(&uuid
, (char *)uuidstr
);
1281 qcrypto_block_luks_create(QCryptoBlock
*block
,
1282 QCryptoBlockCreateOptions
*options
,
1283 const char *optprefix
,
1284 QCryptoBlockInitFunc initfunc
,
1285 QCryptoBlockWriteFunc writefunc
,
1289 QCryptoBlockLUKS
*luks
;
1290 QCryptoBlockCreateOptionsLUKS luks_opts
;
1291 Error
*local_err
= NULL
;
1292 g_autofree
uint8_t *masterkey
= NULL
;
1293 size_t header_sectors
;
1294 size_t split_key_sectors
;
1296 g_autofree
char *password
= NULL
;
1297 const char *cipher_alg
;
1298 const char *cipher_mode
;
1299 const char *ivgen_alg
;
1300 const char *ivgen_hash_alg
= NULL
;
1301 const char *hash_alg
;
1302 g_autofree
char *cipher_mode_spec
= NULL
;
1305 memcpy(&luks_opts
, &options
->u
.luks
, sizeof(luks_opts
));
1306 if (!luks_opts
.has_iter_time
) {
1307 luks_opts
.iter_time
= QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS
;
1309 if (!luks_opts
.has_cipher_alg
) {
1310 luks_opts
.cipher_alg
= QCRYPTO_CIPHER_ALG_AES_256
;
1312 if (!luks_opts
.has_cipher_mode
) {
1313 luks_opts
.cipher_mode
= QCRYPTO_CIPHER_MODE_XTS
;
1315 if (!luks_opts
.has_ivgen_alg
) {
1316 luks_opts
.ivgen_alg
= QCRYPTO_IVGEN_ALG_PLAIN64
;
1318 if (!luks_opts
.has_hash_alg
) {
1319 luks_opts
.hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
1321 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
1322 if (!luks_opts
.has_ivgen_hash_alg
) {
1323 luks_opts
.ivgen_hash_alg
= QCRYPTO_HASH_ALG_SHA256
;
1324 luks_opts
.has_ivgen_hash_alg
= true;
1328 luks
= g_new0(QCryptoBlockLUKS
, 1);
1329 block
->opaque
= luks
;
1331 luks
->cipher_alg
= luks_opts
.cipher_alg
;
1332 luks
->cipher_mode
= luks_opts
.cipher_mode
;
1333 luks
->ivgen_alg
= luks_opts
.ivgen_alg
;
1334 luks
->ivgen_hash_alg
= luks_opts
.ivgen_hash_alg
;
1335 luks
->hash_alg
= luks_opts
.hash_alg
;
1338 /* Note we're allowing ivgen_hash_alg to be set even for
1339 * non-essiv iv generators that don't need a hash. It will
1340 * be silently ignored, for compatibility with dm-crypt */
1342 if (!options
->u
.luks
.key_secret
) {
1343 error_setg(errp
, "Parameter '%skey-secret' is required for cipher",
1344 optprefix
? optprefix
: "");
1347 luks
->secret
= g_strdup(options
->u
.luks
.key_secret
);
1349 password
= qcrypto_secret_lookup_as_utf8(luks_opts
.key_secret
, errp
);
1355 memcpy(luks
->header
.magic
, qcrypto_block_luks_magic
,
1356 QCRYPTO_BLOCK_LUKS_MAGIC_LEN
);
1358 /* We populate the header in native endianness initially and
1359 * then convert everything to big endian just before writing
1362 luks
->header
.version
= QCRYPTO_BLOCK_LUKS_VERSION
;
1363 qcrypto_block_luks_uuid_gen(luks
->header
.uuid
);
1365 cipher_alg
= qcrypto_block_luks_cipher_alg_lookup(luks_opts
.cipher_alg
,
1371 cipher_mode
= QCryptoCipherMode_str(luks_opts
.cipher_mode
);
1372 ivgen_alg
= QCryptoIVGenAlgorithm_str(luks_opts
.ivgen_alg
);
1373 if (luks_opts
.has_ivgen_hash_alg
) {
1374 ivgen_hash_alg
= QCryptoHashAlgorithm_str(luks_opts
.ivgen_hash_alg
);
1375 cipher_mode_spec
= g_strdup_printf("%s-%s:%s", cipher_mode
, ivgen_alg
,
1378 cipher_mode_spec
= g_strdup_printf("%s-%s", cipher_mode
, ivgen_alg
);
1380 hash_alg
= QCryptoHashAlgorithm_str(luks_opts
.hash_alg
);
1383 if (strlen(cipher_alg
) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
) {
1384 error_setg(errp
, "Cipher name '%s' is too long for LUKS header",
1388 if (strlen(cipher_mode_spec
) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
) {
1389 error_setg(errp
, "Cipher mode '%s' is too long for LUKS header",
1393 if (strlen(hash_alg
) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
) {
1394 error_setg(errp
, "Hash name '%s' is too long for LUKS header",
1399 if (luks_opts
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
1400 luks
->ivgen_cipher_alg
=
1401 qcrypto_block_luks_essiv_cipher(luks_opts
.cipher_alg
,
1402 luks_opts
.ivgen_hash_alg
,
1405 error_propagate(errp
, local_err
);
1409 luks
->ivgen_cipher_alg
= luks_opts
.cipher_alg
;
1412 strcpy(luks
->header
.cipher_name
, cipher_alg
);
1413 strcpy(luks
->header
.cipher_mode
, cipher_mode_spec
);
1414 strcpy(luks
->header
.hash_spec
, hash_alg
);
1416 luks
->header
.master_key_len
=
1417 qcrypto_cipher_get_key_len(luks_opts
.cipher_alg
);
1419 if (luks_opts
.cipher_mode
== QCRYPTO_CIPHER_MODE_XTS
) {
1420 luks
->header
.master_key_len
*= 2;
1423 /* Generate the salt used for hashing the master key
1426 if (qcrypto_random_bytes(luks
->header
.master_key_salt
,
1427 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1432 /* Generate random master key */
1433 masterkey
= g_new0(uint8_t, luks
->header
.master_key_len
);
1434 if (qcrypto_random_bytes(masterkey
,
1435 luks
->header
.master_key_len
, errp
) < 0) {
1440 /* Setup the block device payload encryption objects */
1441 if (qcrypto_block_init_cipher(block
, luks_opts
.cipher_alg
,
1442 luks_opts
.cipher_mode
, masterkey
,
1443 luks
->header
.master_key_len
, 1, errp
) < 0) {
1447 block
->kdfhash
= luks_opts
.hash_alg
;
1448 block
->niv
= qcrypto_cipher_get_iv_len(luks_opts
.cipher_alg
,
1449 luks_opts
.cipher_mode
);
1450 block
->ivgen
= qcrypto_ivgen_new(luks_opts
.ivgen_alg
,
1451 luks
->ivgen_cipher_alg
,
1452 luks_opts
.ivgen_hash_alg
,
1453 masterkey
, luks
->header
.master_key_len
,
1456 if (!block
->ivgen
) {
1461 /* Determine how many iterations we need to hash the master
1462 * key, in order to have 1 second of compute time used
1464 iters
= qcrypto_pbkdf2_count_iters(luks_opts
.hash_alg
,
1465 masterkey
, luks
->header
.master_key_len
,
1466 luks
->header
.master_key_salt
,
1467 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1468 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1471 error_propagate(errp
, local_err
);
1475 if (iters
> (ULLONG_MAX
/ luks_opts
.iter_time
)) {
1476 error_setg_errno(errp
, ERANGE
,
1477 "PBKDF iterations %llu too large to scale",
1478 (unsigned long long)iters
);
1482 /* iter_time was in millis, but count_iters reported for secs */
1483 iters
= iters
* luks_opts
.iter_time
/ 1000;
1485 /* Why /= 8 ? That matches cryptsetup, but there's no
1486 * explanation why they chose /= 8... Probably so that
1487 * if all 8 keyslots are active we only spend 1 second
1488 * in total time to check all keys */
1490 if (iters
> UINT32_MAX
) {
1491 error_setg_errno(errp
, ERANGE
,
1492 "PBKDF iterations %llu larger than %u",
1493 (unsigned long long)iters
, UINT32_MAX
);
1496 iters
= MAX(iters
, QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS
);
1497 luks
->header
.master_key_iterations
= iters
;
1499 /* Hash the master key, saving the result in the LUKS
1500 * header. This hash is used when opening the encrypted
1501 * device to verify that the user password unlocked a
1504 if (qcrypto_pbkdf2(luks_opts
.hash_alg
,
1505 masterkey
, luks
->header
.master_key_len
,
1506 luks
->header
.master_key_salt
,
1507 QCRYPTO_BLOCK_LUKS_SALT_LEN
,
1508 luks
->header
.master_key_iterations
,
1509 luks
->header
.master_key_digest
,
1510 QCRYPTO_BLOCK_LUKS_DIGEST_LEN
,
1515 /* start with the sector that follows the header*/
1516 header_sectors
= QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET
/
1517 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1520 qcrypto_block_luks_splitkeylen_sectors(luks
,
1522 QCRYPTO_BLOCK_LUKS_STRIPES
);
1524 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1525 QCryptoBlockLUKSKeySlot
*slot
= &luks
->header
.key_slots
[i
];
1526 slot
->active
= QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED
;
1528 slot
->key_offset_sector
= header_sectors
+ i
* split_key_sectors
;
1529 slot
->stripes
= QCRYPTO_BLOCK_LUKS_STRIPES
;
1532 /* The total size of the LUKS headers is the partition header + key
1533 * slot headers, rounded up to the nearest sector, combined with
1534 * the size of each master key material region, also rounded up
1535 * to the nearest sector */
1536 luks
->header
.payload_offset_sector
= header_sectors
+
1537 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
* split_key_sectors
;
1539 block
->sector_size
= QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1540 block
->payload_offset
= luks
->header
.payload_offset_sector
*
1543 /* Reserve header space to match payload offset */
1544 initfunc(block
, block
->payload_offset
, opaque
, &local_err
);
1546 error_propagate(errp
, local_err
);
1551 /* populate the slot 0 with the password encrypted master key*/
1552 /* This will also store the header */
1553 if (qcrypto_block_luks_store_key(block
,
1557 luks_opts
.iter_time
,
1564 memset(masterkey
, 0, luks
->header
.master_key_len
);
1570 memset(masterkey
, 0, luks
->header
.master_key_len
);
1573 qcrypto_block_free_cipher(block
);
1574 qcrypto_ivgen_free(block
->ivgen
);
1576 g_free(luks
->secret
);
1582 qcrypto_block_luks_amend_add_keyslot(QCryptoBlock
*block
,
1583 QCryptoBlockReadFunc readfunc
,
1584 QCryptoBlockWriteFunc writefunc
,
1586 QCryptoBlockAmendOptionsLUKS
*opts_luks
,
1590 QCryptoBlockLUKS
*luks
= block
->opaque
;
1591 uint64_t iter_time
= opts_luks
->has_iter_time
?
1592 opts_luks
->iter_time
:
1593 QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS
;
1595 g_autofree
char *old_password
= NULL
;
1596 g_autofree
char *new_password
= NULL
;
1597 g_autofree
uint8_t *master_key
= NULL
;
1599 char *secret
= opts_luks
->secret
?: luks
->secret
;
1601 if (!opts_luks
->new_secret
) {
1602 error_setg(errp
, "'new-secret' is required to activate a keyslot");
1605 if (opts_luks
->old_secret
) {
1607 "'old-secret' must not be given when activating keyslots");
1611 if (opts_luks
->has_keyslot
) {
1612 keyslot
= opts_luks
->keyslot
;
1613 if (keyslot
< 0 || keyslot
>= QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
) {
1615 "Invalid keyslot %u specified, must be between 0 and %u",
1616 keyslot
, QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
- 1);
1620 keyslot
= qcrypto_block_luks_find_free_keyslot(luks
);
1621 if (keyslot
== -1) {
1623 "Can't add a keyslot - all keyslots are in use");
1628 if (!force
&& qcrypto_block_luks_slot_active(luks
, keyslot
)) {
1630 "Refusing to overwrite active keyslot %i - "
1631 "please erase it first",
1636 /* Locate the password that will be used to retrieve the master key */
1637 old_password
= qcrypto_secret_lookup_as_utf8(secret
, errp
);
1638 if (!old_password
) {
1642 /* Retrieve the master key */
1643 master_key
= g_new0(uint8_t, luks
->header
.master_key_len
);
1645 if (qcrypto_block_luks_find_key(block
, old_password
, master_key
,
1646 readfunc
, opaque
, errp
) < 0) {
1647 error_append_hint(errp
, "Failed to retrieve the master key");
1651 /* Locate the new password*/
1652 new_password
= qcrypto_secret_lookup_as_utf8(opts_luks
->new_secret
, errp
);
1653 if (!new_password
) {
1657 /* Now set the new keyslots */
1658 if (qcrypto_block_luks_store_key(block
, keyslot
, new_password
, master_key
,
1659 iter_time
, writefunc
, opaque
, errp
)) {
1660 error_append_hint(errp
, "Failed to write to keyslot %i", keyslot
);
1667 qcrypto_block_luks_amend_erase_keyslots(QCryptoBlock
*block
,
1668 QCryptoBlockReadFunc readfunc
,
1669 QCryptoBlockWriteFunc writefunc
,
1671 QCryptoBlockAmendOptionsLUKS
*opts_luks
,
1675 QCryptoBlockLUKS
*luks
= block
->opaque
;
1676 g_autofree
uint8_t *tmpkey
= NULL
;
1677 g_autofree
char *old_password
= NULL
;
1679 if (opts_luks
->new_secret
) {
1681 "'new-secret' must not be given when erasing keyslots");
1684 if (opts_luks
->has_iter_time
) {
1686 "'iter-time' must not be given when erasing keyslots");
1689 if (opts_luks
->secret
) {
1691 "'secret' must not be given when erasing keyslots");
1695 /* Load the old password if given */
1696 if (opts_luks
->old_secret
) {
1697 old_password
= qcrypto_secret_lookup_as_utf8(opts_luks
->old_secret
,
1699 if (!old_password
) {
1704 * Allocate a temporary key buffer that we will need when
1705 * checking if slot matches the given old password
1707 tmpkey
= g_new0(uint8_t, luks
->header
.master_key_len
);
1710 /* Erase an explicitly given keyslot */
1711 if (opts_luks
->has_keyslot
) {
1712 int keyslot
= opts_luks
->keyslot
;
1714 if (keyslot
< 0 || keyslot
>= QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
) {
1716 "Invalid keyslot %i specified, must be between 0 and %i",
1717 keyslot
, QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
- 1);
1721 if (opts_luks
->old_secret
) {
1722 int rv
= qcrypto_block_luks_load_key(block
,
1731 } else if (rv
== 0) {
1733 "Given keyslot %i doesn't contain the given "
1734 "old password for erase operation",
1740 if (!force
&& !qcrypto_block_luks_slot_active(luks
, keyslot
)) {
1742 "Given keyslot %i is already erased (inactive) ",
1747 if (!force
&& qcrypto_block_luks_count_active_slots(luks
) == 1) {
1749 "Attempt to erase the only active keyslot %i "
1750 "which will erase all the data in the image "
1751 "irreversibly - refusing operation",
1756 if (qcrypto_block_luks_erase_key(block
, keyslot
,
1757 writefunc
, opaque
, errp
)) {
1758 error_append_hint(errp
, "Failed to erase keyslot %i", keyslot
);
1762 /* Erase all keyslots that match the given old password */
1763 } else if (opts_luks
->old_secret
) {
1765 unsigned long slots_to_erase_bitmap
= 0;
1769 assert(QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
<=
1770 sizeof(slots_to_erase_bitmap
) * 8);
1772 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1773 int rv
= qcrypto_block_luks_load_key(block
,
1782 } else if (rv
== 1) {
1783 bitmap_set(&slots_to_erase_bitmap
, i
, 1);
1787 slot_count
= bitmap_count_one(&slots_to_erase_bitmap
,
1788 QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
);
1789 if (slot_count
== 0) {
1791 "No keyslots match given (old) password for erase operation");
1796 slot_count
== qcrypto_block_luks_count_active_slots(luks
)) {
1798 "All the active keyslots match the (old) password that "
1799 "was given and erasing them will erase all the data in "
1800 "the image irreversibly - refusing operation");
1804 /* Now apply the update */
1805 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1806 if (!test_bit(i
, &slots_to_erase_bitmap
)) {
1809 if (qcrypto_block_luks_erase_key(block
, i
, writefunc
,
1811 error_append_hint(errp
, "Failed to erase keyslot %zu", i
);
1817 "To erase keyslot(s), either explicit keyslot index "
1818 "or the password currently contained in them must be given");
1825 qcrypto_block_luks_amend_options(QCryptoBlock
*block
,
1826 QCryptoBlockReadFunc readfunc
,
1827 QCryptoBlockWriteFunc writefunc
,
1829 QCryptoBlockAmendOptions
*options
,
1833 QCryptoBlockAmendOptionsLUKS
*opts_luks
= &options
->u
.luks
;
1835 switch (opts_luks
->state
) {
1836 case Q_CRYPTO_BLOCKLUKS_KEYSLOT_STATE_ACTIVE
:
1837 return qcrypto_block_luks_amend_add_keyslot(block
, readfunc
,
1839 opts_luks
, force
, errp
);
1840 case Q_CRYPTO_BLOCKLUKS_KEYSLOT_STATE_INACTIVE
:
1841 return qcrypto_block_luks_amend_erase_keyslots(block
, readfunc
,
1843 opts_luks
, force
, errp
);
1845 g_assert_not_reached();
1849 static int qcrypto_block_luks_get_info(QCryptoBlock
*block
,
1850 QCryptoBlockInfo
*info
,
1853 QCryptoBlockLUKS
*luks
= block
->opaque
;
1854 QCryptoBlockInfoLUKSSlot
*slot
;
1855 QCryptoBlockInfoLUKSSlotList
**tail
= &info
->u
.luks
.slots
;
1858 info
->u
.luks
.cipher_alg
= luks
->cipher_alg
;
1859 info
->u
.luks
.cipher_mode
= luks
->cipher_mode
;
1860 info
->u
.luks
.ivgen_alg
= luks
->ivgen_alg
;
1861 if (info
->u
.luks
.ivgen_alg
== QCRYPTO_IVGEN_ALG_ESSIV
) {
1862 info
->u
.luks
.has_ivgen_hash_alg
= true;
1863 info
->u
.luks
.ivgen_hash_alg
= luks
->ivgen_hash_alg
;
1865 info
->u
.luks
.hash_alg
= luks
->hash_alg
;
1866 info
->u
.luks
.payload_offset
= block
->payload_offset
;
1867 info
->u
.luks
.master_key_iters
= luks
->header
.master_key_iterations
;
1868 info
->u
.luks
.uuid
= g_strndup((const char *)luks
->header
.uuid
,
1869 sizeof(luks
->header
.uuid
));
1871 for (i
= 0; i
< QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
; i
++) {
1872 slot
= g_new0(QCryptoBlockInfoLUKSSlot
, 1);
1873 slot
->active
= luks
->header
.key_slots
[i
].active
==
1874 QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED
;
1875 slot
->key_offset
= luks
->header
.key_slots
[i
].key_offset_sector
1876 * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
;
1878 slot
->has_iters
= true;
1879 slot
->iters
= luks
->header
.key_slots
[i
].iterations
;
1880 slot
->has_stripes
= true;
1881 slot
->stripes
= luks
->header
.key_slots
[i
].stripes
;
1884 QAPI_LIST_APPEND(tail
, slot
);
1891 static void qcrypto_block_luks_cleanup(QCryptoBlock
*block
)
1893 QCryptoBlockLUKS
*luks
= block
->opaque
;
1895 g_free(luks
->secret
);
1902 qcrypto_block_luks_decrypt(QCryptoBlock
*block
,
1908 assert(QEMU_IS_ALIGNED(offset
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
));
1909 assert(QEMU_IS_ALIGNED(len
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
));
1910 return qcrypto_block_decrypt_helper(block
,
1911 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1912 offset
, buf
, len
, errp
);
1917 qcrypto_block_luks_encrypt(QCryptoBlock
*block
,
1923 assert(QEMU_IS_ALIGNED(offset
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
));
1924 assert(QEMU_IS_ALIGNED(len
, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
));
1925 return qcrypto_block_encrypt_helper(block
,
1926 QCRYPTO_BLOCK_LUKS_SECTOR_SIZE
,
1927 offset
, buf
, len
, errp
);
1931 const QCryptoBlockDriver qcrypto_block_driver_luks
= {
1932 .open
= qcrypto_block_luks_open
,
1933 .create
= qcrypto_block_luks_create
,
1934 .amend
= qcrypto_block_luks_amend_options
,
1935 .get_info
= qcrypto_block_luks_get_info
,
1936 .cleanup
= qcrypto_block_luks_cleanup
,
1937 .decrypt
= qcrypto_block_luks_decrypt
,
1938 .encrypt
= qcrypto_block_luks_encrypt
,
1939 .has_format
= qcrypto_block_luks_has_format
,