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"
27 #include "crypto/hash.h"
28 #include "crypto/afsplit.h"
29 #include "crypto/pbkdf.h"
30 #include "crypto/secret.h"
31 #include "crypto/random.h"
32 #include "qemu/uuid.h"
34 #include "qemu/bitmap.h"
37 * Reference for the LUKS format implemented here is
39 * docs/on-disk-format.pdf
41 * in 'cryptsetup' package source code
43 * This file implements the 1.2.1 specification, dated
47 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader
;
48 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot
;
51 /* The following constants are all defined by the LUKS spec */
52 #define QCRYPTO_BLOCK_LUKS_VERSION 1
54 #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6
55 #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32
56 #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32
57 #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32
58 #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20
59 #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32
60 #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40
61 #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8
62 #define QCRYPTO_BLOCK_LUKS_STRIPES 4000
63 #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000
64 #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000
65 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096
67 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
68 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
70 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
72 #define QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS 2000
73 #define QCRYPTO_BLOCK_LUKS_ERASE_ITERATIONS 40
75 static const char qcrypto_block_luks_magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
] = {
76 'L', 'U', 'K', 'S', 0xBA, 0xBE
80 * This struct is written to disk in big-endian format,
81 * but operated upon in native-endian format.
83 struct QCryptoBlockLUKSKeySlot
{
84 /* state of keyslot, enabled/disable */
86 /* iterations for PBKDF2 */
89 uint8_t salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
90 /* start sector of key material */
91 uint32_t key_offset_sector
;
92 /* number of anti-forensic stripes */
97 * This struct is written to disk in big-endian format,
98 * but operated upon in native-endian format.
100 struct QCryptoBlockLUKSHeader
{
101 /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
102 char magic
[QCRYPTO_BLOCK_LUKS_MAGIC_LEN
];
104 /* LUKS version, currently 1 */
107 /* cipher name specification (aes, etc) */
108 char cipher_name
[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN
];
110 /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
111 char cipher_mode
[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN
];
113 /* hash specification (sha256, etc) */
114 char hash_spec
[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN
];
116 /* start offset of the volume data (in 512 byte sectors) */
117 uint32_t payload_offset_sector
;
119 /* Number of key bytes */
120 uint32_t master_key_len
;
122 /* master key checksum after PBKDF2 */
123 uint8_t master_key_digest
[QCRYPTO_BLOCK_LUKS_DIGEST_LEN
];
125 /* salt for master key PBKDF2 */
126 uint8_t master_key_salt
[QCRYPTO_BLOCK_LUKS_SALT_LEN
];
128 /* iterations for master key PBKDF2 */
129 uint32_t master_key_iterations
;
131 /* UUID of the partition in standard ASCII representation */
132 uint8_t uuid
[QCRYPTO_BLOCK_LUKS_UUID_LEN
];
135 QCryptoBlockLUKSKeySlot key_slots
[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS
];
140 qcrypto_block_luks_to_disk_endian(QCryptoBlockLUKSHeader
*hdr
);
142 qcrypto_block_luks_from_disk_endian(QCryptoBlockLUKSHeader
*hdr
);