xref: /qemu/crypto/block-luks-priv.h (revision e2c1c34f)
1 /*
2  * QEMU Crypto block device encryption LUKS format
3  *
4  * Copyright (c) 2015-2016 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/bswap.h"
24 
25 #include "block-luks.h"
26 
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"
33 
34 #include "qemu/bitmap.h"
35 
36 /*
37  * Reference for the LUKS format implemented here is
38  *
39  *   docs/on-disk-format.pdf
40  *
41  * in 'cryptsetup' package source code
42  *
43  * This file implements the 1.2.1 specification, dated
44  * Oct 16, 2011.
45  */
46 
47 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader;
48 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot;
49 
50 
51 /* The following constants are all defined by the LUKS spec */
52 #define QCRYPTO_BLOCK_LUKS_VERSION 1
53 
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
66 
67 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
68 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
69 
70 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
71 
72 #define QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS 2000
73 #define QCRYPTO_BLOCK_LUKS_ERASE_ITERATIONS 40
74 
75 static const char qcrypto_block_luks_magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN] = {
76     'L', 'U', 'K', 'S', 0xBA, 0xBE
77 };
78 
79 /*
80  * This struct is written to disk in big-endian format,
81  * but operated upon in native-endian format.
82  */
83 struct QCryptoBlockLUKSKeySlot {
84     /* state of keyslot, enabled/disable */
85     uint32_t active;
86     /* iterations for PBKDF2 */
87     uint32_t iterations;
88     /* salt 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 */
93     uint32_t stripes;
94 };
95 
96 /*
97  * This struct is written to disk in big-endian format,
98  * but operated upon in native-endian format.
99  */
100 struct QCryptoBlockLUKSHeader {
101     /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
102     char magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN];
103 
104     /* LUKS version, currently 1 */
105     uint16_t version;
106 
107     /* cipher name specification (aes, etc) */
108     char cipher_name[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN];
109 
110     /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
111     char cipher_mode[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN];
112 
113     /* hash specification (sha256, etc) */
114     char hash_spec[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN];
115 
116     /* start offset of the volume data (in 512 byte sectors) */
117     uint32_t payload_offset_sector;
118 
119     /* Number of key bytes */
120     uint32_t master_key_len;
121 
122     /* master key checksum after PBKDF2 */
123     uint8_t master_key_digest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
124 
125     /* salt for master key PBKDF2 */
126     uint8_t master_key_salt[QCRYPTO_BLOCK_LUKS_SALT_LEN];
127 
128     /* iterations for master key PBKDF2 */
129     uint32_t master_key_iterations;
130 
131     /* UUID of the partition in standard ASCII representation */
132     uint8_t uuid[QCRYPTO_BLOCK_LUKS_UUID_LEN];
133 
134     /* key slots */
135     QCryptoBlockLUKSKeySlot key_slots[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS];
136 };
137 
138 
139 void
140 qcrypto_block_luks_to_disk_endian(QCryptoBlockLUKSHeader *hdr);
141 void
142 qcrypto_block_luks_from_disk_endian(QCryptoBlockLUKSHeader *hdr);
143