/src/qemu/crypto/block-luks.c
Line | Count | Source |
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 | | #include "block-luks-priv.h" |
27 | | |
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" |
34 | | |
35 | | #include "qemu/bitmap.h" |
36 | | #include "qemu/range.h" |
37 | | |
38 | | /* |
39 | | * Reference for the LUKS format implemented here is |
40 | | * |
41 | | * docs/on-disk-format.pdf |
42 | | * |
43 | | * in 'cryptsetup' package source code |
44 | | * |
45 | | * This file implements the 1.2.1 specification, dated |
46 | | * Oct 16, 2011. |
47 | | */ |
48 | | |
49 | | typedef struct QCryptoBlockLUKS QCryptoBlockLUKS; |
50 | | |
51 | | typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap; |
52 | | struct QCryptoBlockLUKSNameMap { |
53 | | const char *name; |
54 | | int id; |
55 | | }; |
56 | | |
57 | | typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap; |
58 | | struct QCryptoBlockLUKSCipherSizeMap { |
59 | | uint32_t key_bytes; |
60 | | int id; |
61 | | }; |
62 | | typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap; |
63 | | struct QCryptoBlockLUKSCipherNameMap { |
64 | | const char *name; |
65 | | const QCryptoBlockLUKSCipherSizeMap *sizes; |
66 | | }; |
67 | | |
68 | | |
69 | | static const QCryptoBlockLUKSCipherSizeMap |
70 | | qcrypto_block_luks_cipher_size_map_aes[] = { |
71 | | { 16, QCRYPTO_CIPHER_ALGO_AES_128 }, |
72 | | { 24, QCRYPTO_CIPHER_ALGO_AES_192 }, |
73 | | { 32, QCRYPTO_CIPHER_ALGO_AES_256 }, |
74 | | { 0, 0 }, |
75 | | }; |
76 | | |
77 | | static const QCryptoBlockLUKSCipherSizeMap |
78 | | qcrypto_block_luks_cipher_size_map_cast5[] = { |
79 | | { 16, QCRYPTO_CIPHER_ALGO_CAST5_128 }, |
80 | | { 0, 0 }, |
81 | | }; |
82 | | |
83 | | static const QCryptoBlockLUKSCipherSizeMap |
84 | | qcrypto_block_luks_cipher_size_map_serpent[] = { |
85 | | { 16, QCRYPTO_CIPHER_ALGO_SERPENT_128 }, |
86 | | { 24, QCRYPTO_CIPHER_ALGO_SERPENT_192 }, |
87 | | { 32, QCRYPTO_CIPHER_ALGO_SERPENT_256 }, |
88 | | { 0, 0 }, |
89 | | }; |
90 | | |
91 | | static const QCryptoBlockLUKSCipherSizeMap |
92 | | qcrypto_block_luks_cipher_size_map_twofish[] = { |
93 | | { 16, QCRYPTO_CIPHER_ALGO_TWOFISH_128 }, |
94 | | { 24, QCRYPTO_CIPHER_ALGO_TWOFISH_192 }, |
95 | | { 32, QCRYPTO_CIPHER_ALGO_TWOFISH_256 }, |
96 | | { 0, 0 }, |
97 | | }; |
98 | | |
99 | | #ifdef CONFIG_CRYPTO_SM4 |
100 | | static const QCryptoBlockLUKSCipherSizeMap |
101 | | qcrypto_block_luks_cipher_size_map_sm4[] = { |
102 | | { 16, QCRYPTO_CIPHER_ALGO_SM4}, |
103 | | { 0, 0 }, |
104 | | }; |
105 | | #endif |
106 | | |
107 | | static const QCryptoBlockLUKSCipherNameMap |
108 | | qcrypto_block_luks_cipher_name_map[] = { |
109 | | { "aes", qcrypto_block_luks_cipher_size_map_aes }, |
110 | | { "cast5", qcrypto_block_luks_cipher_size_map_cast5 }, |
111 | | { "serpent", qcrypto_block_luks_cipher_size_map_serpent }, |
112 | | { "twofish", qcrypto_block_luks_cipher_size_map_twofish }, |
113 | | #ifdef CONFIG_CRYPTO_SM4 |
114 | | { "sm4", qcrypto_block_luks_cipher_size_map_sm4}, |
115 | | #endif |
116 | | }; |
117 | | |
118 | | QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot) != 48); |
119 | | QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader) != 592); |
120 | | |
121 | | |
122 | | struct QCryptoBlockLUKS { |
123 | | QCryptoBlockLUKSHeader header; |
124 | | |
125 | | /* Main encryption algorithm used for encryption*/ |
126 | | QCryptoCipherAlgo cipher_alg; |
127 | | |
128 | | /* Mode of encryption for the selected encryption algorithm */ |
129 | | QCryptoCipherMode cipher_mode; |
130 | | |
131 | | /* Initialization vector generation algorithm */ |
132 | | QCryptoIVGenAlgo ivgen_alg; |
133 | | |
134 | | /* Hash algorithm used for IV generation*/ |
135 | | QCryptoHashAlgo ivgen_hash_alg; |
136 | | |
137 | | /* |
138 | | * Encryption algorithm used for IV generation. |
139 | | * Usually the same as main encryption algorithm |
140 | | */ |
141 | | QCryptoCipherAlgo ivgen_cipher_alg; |
142 | | |
143 | | /* Hash algorithm used in pbkdf2 function */ |
144 | | QCryptoHashAlgo hash_alg; |
145 | | |
146 | | /* Name of the secret that was used to open the image */ |
147 | | char *secret; |
148 | | }; |
149 | | |
150 | | |
151 | | static int qcrypto_block_luks_cipher_name_lookup(const char *name, |
152 | | QCryptoCipherMode mode, |
153 | | uint32_t key_bytes, |
154 | | Error **errp) |
155 | 0 | { |
156 | 0 | const QCryptoBlockLUKSCipherNameMap *map = |
157 | 0 | qcrypto_block_luks_cipher_name_map; |
158 | 0 | size_t maplen = G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map); |
159 | 0 | size_t i, j; |
160 | |
|
161 | 0 | if (mode == QCRYPTO_CIPHER_MODE_XTS) { |
162 | 0 | key_bytes /= 2; |
163 | 0 | } |
164 | |
|
165 | 0 | for (i = 0; i < maplen; i++) { |
166 | 0 | if (!g_str_equal(map[i].name, name)) { |
167 | 0 | continue; |
168 | 0 | } |
169 | 0 | for (j = 0; j < map[i].sizes[j].key_bytes; j++) { |
170 | 0 | if (map[i].sizes[j].key_bytes == key_bytes) { |
171 | 0 | return map[i].sizes[j].id; |
172 | 0 | } |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | 0 | error_setg(errp, "Algorithm '%s' with key size %d bytes not supported", |
177 | 0 | name, key_bytes); |
178 | 0 | return 0; |
179 | 0 | } |
180 | | |
181 | | static const char * |
182 | | qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgo alg, |
183 | | Error **errp) |
184 | 0 | { |
185 | 0 | const QCryptoBlockLUKSCipherNameMap *map = |
186 | 0 | qcrypto_block_luks_cipher_name_map; |
187 | 0 | size_t maplen = G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map); |
188 | 0 | size_t i, j; |
189 | 0 | for (i = 0; i < maplen; i++) { |
190 | 0 | for (j = 0; j < map[i].sizes[j].key_bytes; j++) { |
191 | 0 | if (map[i].sizes[j].id == alg) { |
192 | 0 | return map[i].name; |
193 | 0 | } |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | 0 | error_setg(errp, "Algorithm '%s' not supported", |
198 | 0 | QCryptoCipherAlgo_str(alg)); |
199 | 0 | return NULL; |
200 | 0 | } |
201 | | |
202 | | /* XXX replace with qapi_enum_parse() in future, when we can |
203 | | * make that function emit a more friendly error message */ |
204 | | static int qcrypto_block_luks_name_lookup(const char *name, |
205 | | const QEnumLookup *map, |
206 | | const char *type, |
207 | | Error **errp) |
208 | 0 | { |
209 | 0 | int ret = qapi_enum_parse(map, name, -1, NULL); |
210 | |
|
211 | 0 | if (ret < 0) { |
212 | 0 | error_setg(errp, "%s '%s' not supported", type, name); |
213 | 0 | return 0; |
214 | 0 | } |
215 | 0 | return ret; |
216 | 0 | } |
217 | | |
218 | | #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \ |
219 | 0 | qcrypto_block_luks_name_lookup(name, \ |
220 | 0 | &QCryptoCipherMode_lookup, \ |
221 | 0 | "Cipher mode", \ |
222 | 0 | errp) |
223 | | |
224 | | #define qcrypto_block_luks_hash_name_lookup(name, errp) \ |
225 | 0 | qcrypto_block_luks_name_lookup(name, \ |
226 | 0 | &QCryptoHashAlgo_lookup, \ |
227 | 0 | "Hash algorithm", \ |
228 | 0 | errp) |
229 | | |
230 | | #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \ |
231 | 0 | qcrypto_block_luks_name_lookup(name, \ |
232 | 0 | &QCryptoIVGenAlgo_lookup, \ |
233 | 0 | "IV generator", \ |
234 | 0 | errp) |
235 | | |
236 | | |
237 | | static bool |
238 | | qcrypto_block_luks_has_format(const uint8_t *buf, |
239 | | size_t buf_size) |
240 | 0 | { |
241 | 0 | const QCryptoBlockLUKSHeader *luks_header = (const void *)buf; |
242 | |
|
243 | 0 | if (buf_size >= offsetof(QCryptoBlockLUKSHeader, cipher_name) && |
244 | 0 | memcmp(luks_header->magic, qcrypto_block_luks_magic, |
245 | 0 | QCRYPTO_BLOCK_LUKS_MAGIC_LEN) == 0 && |
246 | 0 | be16_to_cpu(luks_header->version) == QCRYPTO_BLOCK_LUKS_VERSION) { |
247 | 0 | return true; |
248 | 0 | } else { |
249 | 0 | return false; |
250 | 0 | } |
251 | 0 | } |
252 | | |
253 | | |
254 | | /** |
255 | | * Deal with a quirk of dm-crypt usage of ESSIV. |
256 | | * |
257 | | * When calculating ESSIV IVs, the cipher length used by ESSIV |
258 | | * may be different from the cipher length used for the block |
259 | | * encryption, because dm-crypt uses the hash digest length |
260 | | * as the key size. ie, if you have AES 128 as the block cipher |
261 | | * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as |
262 | | * the cipher since that gets a key length matching the digest |
263 | | * size, not AES 128 with truncated digest as might be imagined |
264 | | */ |
265 | | static QCryptoCipherAlgo |
266 | | qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgo cipher, |
267 | | QCryptoHashAlgo hash, |
268 | | Error **errp) |
269 | 0 | { |
270 | 0 | size_t digestlen = qcrypto_hash_digest_len(hash); |
271 | 0 | size_t keylen = qcrypto_cipher_get_key_len(cipher); |
272 | 0 | if (digestlen == keylen) { |
273 | 0 | return cipher; |
274 | 0 | } |
275 | | |
276 | 0 | switch (cipher) { |
277 | 0 | case QCRYPTO_CIPHER_ALGO_AES_128: |
278 | 0 | case QCRYPTO_CIPHER_ALGO_AES_192: |
279 | 0 | case QCRYPTO_CIPHER_ALGO_AES_256: |
280 | 0 | if (digestlen == qcrypto_cipher_get_key_len( |
281 | 0 | QCRYPTO_CIPHER_ALGO_AES_128)) { |
282 | 0 | return QCRYPTO_CIPHER_ALGO_AES_128; |
283 | 0 | } else if (digestlen == qcrypto_cipher_get_key_len( |
284 | 0 | QCRYPTO_CIPHER_ALGO_AES_192)) { |
285 | 0 | return QCRYPTO_CIPHER_ALGO_AES_192; |
286 | 0 | } else if (digestlen == qcrypto_cipher_get_key_len( |
287 | 0 | QCRYPTO_CIPHER_ALGO_AES_256)) { |
288 | 0 | return QCRYPTO_CIPHER_ALGO_AES_256; |
289 | 0 | } else { |
290 | 0 | error_setg(errp, "No AES cipher with key size %zu available", |
291 | 0 | digestlen); |
292 | 0 | return 0; |
293 | 0 | } |
294 | 0 | break; |
295 | 0 | case QCRYPTO_CIPHER_ALGO_SERPENT_128: |
296 | 0 | case QCRYPTO_CIPHER_ALGO_SERPENT_192: |
297 | 0 | case QCRYPTO_CIPHER_ALGO_SERPENT_256: |
298 | 0 | if (digestlen == qcrypto_cipher_get_key_len( |
299 | 0 | QCRYPTO_CIPHER_ALGO_SERPENT_128)) { |
300 | 0 | return QCRYPTO_CIPHER_ALGO_SERPENT_128; |
301 | 0 | } else if (digestlen == qcrypto_cipher_get_key_len( |
302 | 0 | QCRYPTO_CIPHER_ALGO_SERPENT_192)) { |
303 | 0 | return QCRYPTO_CIPHER_ALGO_SERPENT_192; |
304 | 0 | } else if (digestlen == qcrypto_cipher_get_key_len( |
305 | 0 | QCRYPTO_CIPHER_ALGO_SERPENT_256)) { |
306 | 0 | return QCRYPTO_CIPHER_ALGO_SERPENT_256; |
307 | 0 | } else { |
308 | 0 | error_setg(errp, "No Serpent cipher with key size %zu available", |
309 | 0 | digestlen); |
310 | 0 | return 0; |
311 | 0 | } |
312 | 0 | break; |
313 | 0 | case QCRYPTO_CIPHER_ALGO_TWOFISH_128: |
314 | 0 | case QCRYPTO_CIPHER_ALGO_TWOFISH_192: |
315 | 0 | case QCRYPTO_CIPHER_ALGO_TWOFISH_256: |
316 | 0 | if (digestlen == qcrypto_cipher_get_key_len( |
317 | 0 | QCRYPTO_CIPHER_ALGO_TWOFISH_128)) { |
318 | 0 | return QCRYPTO_CIPHER_ALGO_TWOFISH_128; |
319 | 0 | } else if (digestlen == qcrypto_cipher_get_key_len( |
320 | 0 | QCRYPTO_CIPHER_ALGO_TWOFISH_192)) { |
321 | 0 | return QCRYPTO_CIPHER_ALGO_TWOFISH_192; |
322 | 0 | } else if (digestlen == qcrypto_cipher_get_key_len( |
323 | 0 | QCRYPTO_CIPHER_ALGO_TWOFISH_256)) { |
324 | 0 | return QCRYPTO_CIPHER_ALGO_TWOFISH_256; |
325 | 0 | } else { |
326 | 0 | error_setg(errp, "No Twofish cipher with key size %zu available", |
327 | 0 | digestlen); |
328 | 0 | return 0; |
329 | 0 | } |
330 | 0 | break; |
331 | 0 | default: |
332 | 0 | error_setg(errp, "Cipher %s not supported with essiv", |
333 | 0 | QCryptoCipherAlgo_str(cipher)); |
334 | 0 | return 0; |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | | /* |
339 | | * Returns number of sectors needed to store the key material |
340 | | * given number of anti forensic stripes |
341 | | */ |
342 | | static int |
343 | | qcrypto_block_luks_splitkeylen_sectors(const QCryptoBlockLUKS *luks, |
344 | | unsigned int header_sectors, |
345 | | unsigned int stripes) |
346 | 0 | { |
347 | | /* |
348 | | * This calculation doesn't match that shown in the spec, |
349 | | * but instead follows the cryptsetup implementation. |
350 | | */ |
351 | |
|
352 | 0 | size_t splitkeylen = luks->header.master_key_len * stripes; |
353 | | |
354 | | /* First align the key material size to block size*/ |
355 | 0 | size_t splitkeylen_sectors = |
356 | 0 | DIV_ROUND_UP(splitkeylen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE); |
357 | | |
358 | | /* Then also align the key material size to the size of the header */ |
359 | 0 | return ROUND_UP(splitkeylen_sectors, header_sectors); |
360 | 0 | } |
361 | | |
362 | | |
363 | | void |
364 | | qcrypto_block_luks_to_disk_endian(QCryptoBlockLUKSHeader *hdr) |
365 | 0 | { |
366 | 0 | size_t i; |
367 | | |
368 | | /* |
369 | | * Everything on disk uses Big Endian (tm), so flip header fields |
370 | | * before writing them |
371 | | */ |
372 | 0 | cpu_to_be16s(&hdr->version); |
373 | 0 | cpu_to_be32s(&hdr->payload_offset_sector); |
374 | 0 | cpu_to_be32s(&hdr->master_key_len); |
375 | 0 | cpu_to_be32s(&hdr->master_key_iterations); |
376 | |
|
377 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
378 | 0 | cpu_to_be32s(&hdr->key_slots[i].active); |
379 | 0 | cpu_to_be32s(&hdr->key_slots[i].iterations); |
380 | 0 | cpu_to_be32s(&hdr->key_slots[i].key_offset_sector); |
381 | 0 | cpu_to_be32s(&hdr->key_slots[i].stripes); |
382 | 0 | } |
383 | 0 | } |
384 | | |
385 | | void |
386 | | qcrypto_block_luks_from_disk_endian(QCryptoBlockLUKSHeader *hdr) |
387 | 0 | { |
388 | 0 | size_t i; |
389 | | |
390 | | /* |
391 | | * The header is always stored in big-endian format, so |
392 | | * convert everything to native |
393 | | */ |
394 | 0 | be16_to_cpus(&hdr->version); |
395 | 0 | be32_to_cpus(&hdr->payload_offset_sector); |
396 | 0 | be32_to_cpus(&hdr->master_key_len); |
397 | 0 | be32_to_cpus(&hdr->master_key_iterations); |
398 | |
|
399 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
400 | 0 | be32_to_cpus(&hdr->key_slots[i].active); |
401 | 0 | be32_to_cpus(&hdr->key_slots[i].iterations); |
402 | 0 | be32_to_cpus(&hdr->key_slots[i].key_offset_sector); |
403 | 0 | be32_to_cpus(&hdr->key_slots[i].stripes); |
404 | 0 | } |
405 | 0 | } |
406 | | |
407 | | /* |
408 | | * Stores the main LUKS header, taking care of endianness |
409 | | */ |
410 | | static int |
411 | | qcrypto_block_luks_store_header(QCryptoBlock *block, |
412 | | QCryptoBlockWriteFunc writefunc, |
413 | | void *opaque, |
414 | | Error **errp) |
415 | 0 | { |
416 | 0 | const QCryptoBlockLUKS *luks = block->opaque; |
417 | 0 | Error *local_err = NULL; |
418 | 0 | g_autofree QCryptoBlockLUKSHeader *hdr_copy = NULL; |
419 | | |
420 | | /* Create a copy of the header */ |
421 | 0 | hdr_copy = g_new0(QCryptoBlockLUKSHeader, 1); |
422 | 0 | memcpy(hdr_copy, &luks->header, sizeof(QCryptoBlockLUKSHeader)); |
423 | |
|
424 | 0 | qcrypto_block_luks_to_disk_endian(hdr_copy); |
425 | | |
426 | | /* Write out the partition header and key slot headers */ |
427 | 0 | writefunc(block, 0, (const uint8_t *)hdr_copy, sizeof(*hdr_copy), |
428 | 0 | opaque, &local_err); |
429 | |
|
430 | 0 | if (local_err) { |
431 | 0 | error_propagate(errp, local_err); |
432 | 0 | return -1; |
433 | 0 | } |
434 | 0 | return 0; |
435 | 0 | } |
436 | | |
437 | | /* |
438 | | * Loads the main LUKS header, and byteswaps it to native endianness |
439 | | * And run basic sanity checks on it |
440 | | */ |
441 | | static int |
442 | | qcrypto_block_luks_load_header(QCryptoBlock *block, |
443 | | QCryptoBlockReadFunc readfunc, |
444 | | void *opaque, |
445 | | Error **errp) |
446 | 0 | { |
447 | 0 | int rv; |
448 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
449 | | |
450 | | /* |
451 | | * Read the entire LUKS header, minus the key material from |
452 | | * the underlying device |
453 | | */ |
454 | 0 | rv = readfunc(block, 0, |
455 | 0 | (uint8_t *)&luks->header, |
456 | 0 | sizeof(luks->header), |
457 | 0 | opaque, |
458 | 0 | errp); |
459 | 0 | if (rv < 0) { |
460 | 0 | return rv; |
461 | 0 | } |
462 | | |
463 | 0 | qcrypto_block_luks_from_disk_endian(&luks->header); |
464 | |
|
465 | 0 | return 0; |
466 | 0 | } |
467 | | |
468 | | /* |
469 | | * Does basic sanity checks on the LUKS header |
470 | | */ |
471 | | static int |
472 | | qcrypto_block_luks_check_header(const QCryptoBlockLUKS *luks, |
473 | | unsigned int flags, |
474 | | Error **errp) |
475 | 0 | { |
476 | 0 | size_t i, j; |
477 | |
|
478 | 0 | unsigned int header_sectors = QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET / |
479 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; |
480 | 0 | bool detached = flags & QCRYPTO_BLOCK_OPEN_DETACHED; |
481 | |
|
482 | 0 | if (memcmp(luks->header.magic, qcrypto_block_luks_magic, |
483 | 0 | QCRYPTO_BLOCK_LUKS_MAGIC_LEN) != 0) { |
484 | 0 | error_setg(errp, "Volume is not in LUKS format"); |
485 | 0 | return -1; |
486 | 0 | } |
487 | | |
488 | 0 | if (luks->header.version != QCRYPTO_BLOCK_LUKS_VERSION) { |
489 | 0 | error_setg(errp, "LUKS version %" PRIu32 " is not supported", |
490 | 0 | luks->header.version); |
491 | 0 | return -1; |
492 | 0 | } |
493 | | |
494 | 0 | if (!memchr(luks->header.cipher_name, '\0', |
495 | 0 | sizeof(luks->header.cipher_name))) { |
496 | 0 | error_setg(errp, "LUKS header cipher name is not NUL terminated"); |
497 | 0 | return -1; |
498 | 0 | } |
499 | | |
500 | 0 | if (!memchr(luks->header.cipher_mode, '\0', |
501 | 0 | sizeof(luks->header.cipher_mode))) { |
502 | 0 | error_setg(errp, "LUKS header cipher mode is not NUL terminated"); |
503 | 0 | return -1; |
504 | 0 | } |
505 | | |
506 | 0 | if (!memchr(luks->header.hash_spec, '\0', |
507 | 0 | sizeof(luks->header.hash_spec))) { |
508 | 0 | error_setg(errp, "LUKS header hash spec is not NUL terminated"); |
509 | 0 | return -1; |
510 | 0 | } |
511 | | |
512 | 0 | if (!detached && luks->header.payload_offset_sector < |
513 | 0 | DIV_ROUND_UP(QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET, |
514 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)) { |
515 | 0 | error_setg(errp, "LUKS payload is overlapping with the header"); |
516 | 0 | return -1; |
517 | 0 | } |
518 | | |
519 | 0 | if (luks->header.master_key_iterations == 0) { |
520 | 0 | error_setg(errp, "LUKS key iteration count is zero"); |
521 | 0 | return -1; |
522 | 0 | } |
523 | | |
524 | | /* Check all keyslots for corruption */ |
525 | 0 | for (i = 0 ; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS ; i++) { |
526 | |
|
527 | 0 | const QCryptoBlockLUKSKeySlot *slot1 = &luks->header.key_slots[i]; |
528 | 0 | unsigned int start1 = slot1->key_offset_sector; |
529 | 0 | unsigned int len1 = |
530 | 0 | qcrypto_block_luks_splitkeylen_sectors(luks, |
531 | 0 | header_sectors, |
532 | 0 | slot1->stripes); |
533 | |
|
534 | 0 | if (slot1->stripes != QCRYPTO_BLOCK_LUKS_STRIPES) { |
535 | 0 | error_setg(errp, "Keyslot %zu is corrupted (stripes %d != %d)", |
536 | 0 | i, slot1->stripes, QCRYPTO_BLOCK_LUKS_STRIPES); |
537 | 0 | return -1; |
538 | 0 | } |
539 | | |
540 | 0 | if (slot1->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED && |
541 | 0 | slot1->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED) { |
542 | 0 | error_setg(errp, |
543 | 0 | "Keyslot %zu state (active/disable) is corrupted", i); |
544 | 0 | return -1; |
545 | 0 | } |
546 | | |
547 | 0 | if (slot1->active == QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED && |
548 | 0 | slot1->iterations == 0) { |
549 | 0 | error_setg(errp, "Keyslot %zu iteration count is zero", i); |
550 | 0 | return -1; |
551 | 0 | } |
552 | | |
553 | 0 | if (start1 < DIV_ROUND_UP(QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET, |
554 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)) { |
555 | 0 | error_setg(errp, |
556 | 0 | "Keyslot %zu is overlapping with the LUKS header", |
557 | 0 | i); |
558 | 0 | return -1; |
559 | 0 | } |
560 | | |
561 | 0 | if (!detached && start1 + len1 > luks->header.payload_offset_sector) { |
562 | 0 | error_setg(errp, |
563 | 0 | "Keyslot %zu is overlapping with the encrypted payload", |
564 | 0 | i); |
565 | 0 | return -1; |
566 | 0 | } |
567 | | |
568 | 0 | for (j = i + 1 ; j < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS ; j++) { |
569 | 0 | const QCryptoBlockLUKSKeySlot *slot2 = &luks->header.key_slots[j]; |
570 | 0 | unsigned int start2 = slot2->key_offset_sector; |
571 | 0 | unsigned int len2 = |
572 | 0 | qcrypto_block_luks_splitkeylen_sectors(luks, |
573 | 0 | header_sectors, |
574 | 0 | slot2->stripes); |
575 | |
|
576 | 0 | if (ranges_overlap(start1, len1, start2, len2)) { |
577 | 0 | error_setg(errp, |
578 | 0 | "Keyslots %zu and %zu are overlapping in the header", |
579 | 0 | i, j); |
580 | 0 | return -1; |
581 | 0 | } |
582 | 0 | } |
583 | |
|
584 | 0 | } |
585 | 0 | return 0; |
586 | 0 | } |
587 | | |
588 | | /* |
589 | | * Parses the crypto parameters that are stored in the LUKS header |
590 | | */ |
591 | | |
592 | | static int |
593 | | qcrypto_block_luks_parse_header(QCryptoBlockLUKS *luks, Error **errp) |
594 | 0 | { |
595 | 0 | g_autofree char *cipher_mode = g_strdup(luks->header.cipher_mode); |
596 | 0 | char *ivgen_name, *ivhash_name; |
597 | 0 | Error *local_err = NULL; |
598 | | |
599 | | /* |
600 | | * The cipher_mode header contains a string that we have |
601 | | * to further parse, of the format |
602 | | * |
603 | | * <cipher-mode>-<iv-generator>[:<iv-hash>] |
604 | | * |
605 | | * eg cbc-essiv:sha256, cbc-plain64 |
606 | | */ |
607 | 0 | ivgen_name = strchr(cipher_mode, '-'); |
608 | 0 | if (!ivgen_name) { |
609 | 0 | error_setg(errp, "Unexpected cipher mode string format '%s'", |
610 | 0 | luks->header.cipher_mode); |
611 | 0 | return -1; |
612 | 0 | } |
613 | 0 | *ivgen_name = '\0'; |
614 | 0 | ivgen_name++; |
615 | |
|
616 | 0 | ivhash_name = strchr(ivgen_name, ':'); |
617 | 0 | if (!ivhash_name) { |
618 | 0 | luks->ivgen_hash_alg = 0; |
619 | 0 | } else { |
620 | 0 | *ivhash_name = '\0'; |
621 | 0 | ivhash_name++; |
622 | |
|
623 | 0 | luks->ivgen_hash_alg = qcrypto_block_luks_hash_name_lookup(ivhash_name, |
624 | 0 | &local_err); |
625 | 0 | if (local_err) { |
626 | 0 | error_propagate(errp, local_err); |
627 | 0 | return -1; |
628 | 0 | } |
629 | 0 | } |
630 | | |
631 | 0 | luks->cipher_mode = qcrypto_block_luks_cipher_mode_lookup(cipher_mode, |
632 | 0 | &local_err); |
633 | 0 | if (local_err) { |
634 | 0 | error_propagate(errp, local_err); |
635 | 0 | return -1; |
636 | 0 | } |
637 | | |
638 | 0 | luks->cipher_alg = |
639 | 0 | qcrypto_block_luks_cipher_name_lookup(luks->header.cipher_name, |
640 | 0 | luks->cipher_mode, |
641 | 0 | luks->header.master_key_len, |
642 | 0 | &local_err); |
643 | 0 | if (local_err) { |
644 | 0 | error_propagate(errp, local_err); |
645 | 0 | return -1; |
646 | 0 | } |
647 | | |
648 | 0 | luks->hash_alg = |
649 | 0 | qcrypto_block_luks_hash_name_lookup(luks->header.hash_spec, |
650 | 0 | &local_err); |
651 | 0 | if (local_err) { |
652 | 0 | error_propagate(errp, local_err); |
653 | 0 | return -1; |
654 | 0 | } |
655 | | |
656 | 0 | luks->ivgen_alg = qcrypto_block_luks_ivgen_name_lookup(ivgen_name, |
657 | 0 | &local_err); |
658 | 0 | if (local_err) { |
659 | 0 | error_propagate(errp, local_err); |
660 | 0 | return -1; |
661 | 0 | } |
662 | | |
663 | 0 | if (luks->ivgen_alg == QCRYPTO_IV_GEN_ALGO_ESSIV) { |
664 | 0 | if (!ivhash_name) { |
665 | 0 | error_setg(errp, "Missing IV generator hash specification"); |
666 | 0 | return -1; |
667 | 0 | } |
668 | 0 | luks->ivgen_cipher_alg = |
669 | 0 | qcrypto_block_luks_essiv_cipher(luks->cipher_alg, |
670 | 0 | luks->ivgen_hash_alg, |
671 | 0 | &local_err); |
672 | 0 | if (local_err) { |
673 | 0 | error_propagate(errp, local_err); |
674 | 0 | return -1; |
675 | 0 | } |
676 | 0 | } else { |
677 | | |
678 | | /* |
679 | | * Note we parsed the ivhash_name earlier in the cipher_mode |
680 | | * spec string even with plain/plain64 ivgens, but we |
681 | | * will ignore it, since it is irrelevant for these ivgens. |
682 | | * This is for compat with dm-crypt which will silently |
683 | | * ignore hash names with these ivgens rather than report |
684 | | * an error about the invalid usage |
685 | | */ |
686 | 0 | luks->ivgen_cipher_alg = luks->cipher_alg; |
687 | 0 | } |
688 | 0 | return 0; |
689 | 0 | } |
690 | | |
691 | | /* |
692 | | * Given a key slot, user password, and the master key, |
693 | | * will store the encrypted master key there, and update the |
694 | | * in-memory header. User must then write the in-memory header |
695 | | * |
696 | | * Returns: |
697 | | * 0 if the keyslot was written successfully |
698 | | * with the provided password |
699 | | * -1 if a fatal error occurred while storing the key |
700 | | */ |
701 | | static int |
702 | | qcrypto_block_luks_store_key(QCryptoBlock *block, |
703 | | unsigned int slot_idx, |
704 | | const char *password, |
705 | | uint8_t *masterkey, |
706 | | uint64_t iter_time, |
707 | | QCryptoBlockWriteFunc writefunc, |
708 | | void *opaque, |
709 | | Error **errp) |
710 | 0 | { |
711 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
712 | 0 | QCryptoBlockLUKSKeySlot *slot; |
713 | 0 | g_autofree uint8_t *splitkey = NULL; |
714 | 0 | size_t splitkeylen; |
715 | 0 | g_autofree uint8_t *slotkey = NULL; |
716 | 0 | g_autoptr(QCryptoCipher) cipher = NULL; |
717 | 0 | g_autoptr(QCryptoIVGen) ivgen = NULL; |
718 | 0 | Error *local_err = NULL; |
719 | 0 | uint64_t iters; |
720 | 0 | int ret = -1; |
721 | |
|
722 | 0 | assert(slot_idx < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS); |
723 | 0 | slot = &luks->header.key_slots[slot_idx]; |
724 | 0 | splitkeylen = luks->header.master_key_len * slot->stripes; |
725 | |
|
726 | 0 | if (qcrypto_random_bytes(slot->salt, |
727 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
728 | 0 | errp) < 0) { |
729 | 0 | goto cleanup; |
730 | 0 | } |
731 | | |
732 | | /* |
733 | | * Determine how many iterations are required to |
734 | | * hash the user password while consuming 1 second of compute |
735 | | * time |
736 | | */ |
737 | 0 | iters = qcrypto_pbkdf2_count_iters(luks->hash_alg, |
738 | 0 | (uint8_t *)password, strlen(password), |
739 | 0 | slot->salt, |
740 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
741 | 0 | luks->header.master_key_len, |
742 | 0 | &local_err); |
743 | 0 | if (local_err) { |
744 | 0 | error_propagate(errp, local_err); |
745 | 0 | goto cleanup; |
746 | 0 | } |
747 | | |
748 | 0 | if (iters > (ULLONG_MAX / iter_time)) { |
749 | 0 | error_setg_errno(errp, ERANGE, |
750 | 0 | "PBKDF iterations %llu too large to scale", |
751 | 0 | (unsigned long long)iters); |
752 | 0 | goto cleanup; |
753 | 0 | } |
754 | | |
755 | | /* iter_time was in millis, but count_iters reported for secs */ |
756 | 0 | iters = iters * iter_time / 1000; |
757 | |
|
758 | 0 | if (iters > UINT32_MAX) { |
759 | 0 | error_setg_errno(errp, ERANGE, |
760 | 0 | "PBKDF iterations %llu larger than %u", |
761 | 0 | (unsigned long long)iters, UINT32_MAX); |
762 | 0 | goto cleanup; |
763 | 0 | } |
764 | | |
765 | 0 | slot->iterations = |
766 | 0 | MAX(iters, QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS); |
767 | | |
768 | | |
769 | | /* |
770 | | * Generate a key that we'll use to encrypt the master |
771 | | * key, from the user's password |
772 | | */ |
773 | 0 | slotkey = g_new0(uint8_t, luks->header.master_key_len); |
774 | 0 | if (qcrypto_pbkdf2(luks->hash_alg, |
775 | 0 | (uint8_t *)password, strlen(password), |
776 | 0 | slot->salt, |
777 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
778 | 0 | slot->iterations, |
779 | 0 | slotkey, luks->header.master_key_len, |
780 | 0 | errp) < 0) { |
781 | 0 | goto cleanup; |
782 | 0 | } |
783 | | |
784 | | |
785 | | /* |
786 | | * Setup the encryption objects needed to encrypt the |
787 | | * master key material |
788 | | */ |
789 | 0 | cipher = qcrypto_cipher_new(luks->cipher_alg, |
790 | 0 | luks->cipher_mode, |
791 | 0 | slotkey, luks->header.master_key_len, |
792 | 0 | errp); |
793 | 0 | if (!cipher) { |
794 | 0 | goto cleanup; |
795 | 0 | } |
796 | | |
797 | 0 | ivgen = qcrypto_ivgen_new(luks->ivgen_alg, |
798 | 0 | luks->ivgen_cipher_alg, |
799 | 0 | luks->ivgen_hash_alg, |
800 | 0 | slotkey, luks->header.master_key_len, |
801 | 0 | errp); |
802 | 0 | if (!ivgen) { |
803 | 0 | goto cleanup; |
804 | 0 | } |
805 | | |
806 | | /* |
807 | | * Before storing the master key, we need to vastly |
808 | | * increase its size, as protection against forensic |
809 | | * disk data recovery |
810 | | */ |
811 | 0 | splitkey = g_new0(uint8_t, splitkeylen); |
812 | |
|
813 | 0 | if (qcrypto_afsplit_encode(luks->hash_alg, |
814 | 0 | luks->header.master_key_len, |
815 | 0 | slot->stripes, |
816 | 0 | masterkey, |
817 | 0 | splitkey, |
818 | 0 | errp) < 0) { |
819 | 0 | goto cleanup; |
820 | 0 | } |
821 | | |
822 | | /* |
823 | | * Now we encrypt the split master key with the key generated |
824 | | * from the user's password, before storing it |
825 | | */ |
826 | 0 | if (qcrypto_block_cipher_encrypt_helper(cipher, block->niv, ivgen, |
827 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
828 | 0 | 0, |
829 | 0 | splitkey, |
830 | 0 | splitkeylen, |
831 | 0 | errp) < 0) { |
832 | 0 | goto cleanup; |
833 | 0 | } |
834 | | |
835 | | /* Write out the slot's master key material. */ |
836 | 0 | if (writefunc(block, |
837 | 0 | slot->key_offset_sector * |
838 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
839 | 0 | splitkey, splitkeylen, |
840 | 0 | opaque, |
841 | 0 | errp) < 0) { |
842 | 0 | goto cleanup; |
843 | 0 | } |
844 | | |
845 | 0 | slot->active = QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED; |
846 | |
|
847 | 0 | if (qcrypto_block_luks_store_header(block, writefunc, opaque, errp) < 0) { |
848 | 0 | goto cleanup; |
849 | 0 | } |
850 | | |
851 | 0 | ret = 0; |
852 | |
|
853 | 0 | cleanup: |
854 | 0 | if (slotkey) { |
855 | 0 | memset(slotkey, 0, luks->header.master_key_len); |
856 | 0 | } |
857 | 0 | if (splitkey) { |
858 | 0 | memset(splitkey, 0, splitkeylen); |
859 | 0 | } |
860 | 0 | return ret; |
861 | 0 | } |
862 | | |
863 | | /* |
864 | | * Given a key slot, and user password, this will attempt to unlock |
865 | | * the master encryption key from the key slot. |
866 | | * |
867 | | * Returns: |
868 | | * 0 if the key slot is disabled, or key could not be decrypted |
869 | | * with the provided password |
870 | | * 1 if the key slot is enabled, and key decrypted successfully |
871 | | * with the provided password |
872 | | * -1 if a fatal error occurred loading the key |
873 | | */ |
874 | | static int |
875 | | qcrypto_block_luks_load_key(QCryptoBlock *block, |
876 | | size_t slot_idx, |
877 | | const char *password, |
878 | | uint8_t *masterkey, |
879 | | QCryptoBlockReadFunc readfunc, |
880 | | void *opaque, |
881 | | Error **errp) |
882 | 0 | { |
883 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
884 | 0 | const QCryptoBlockLUKSKeySlot *slot; |
885 | 0 | g_autofree uint8_t *splitkey = NULL; |
886 | 0 | size_t splitkeylen; |
887 | 0 | g_autofree uint8_t *possiblekey = NULL; |
888 | 0 | int rv; |
889 | 0 | g_autoptr(QCryptoCipher) cipher = NULL; |
890 | 0 | uint8_t keydigest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN]; |
891 | 0 | g_autoptr(QCryptoIVGen) ivgen = NULL; |
892 | 0 | size_t niv; |
893 | |
|
894 | 0 | assert(slot_idx < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS); |
895 | 0 | slot = &luks->header.key_slots[slot_idx]; |
896 | 0 | if (slot->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED) { |
897 | 0 | return 0; |
898 | 0 | } |
899 | | |
900 | 0 | splitkeylen = luks->header.master_key_len * slot->stripes; |
901 | 0 | splitkey = g_new0(uint8_t, splitkeylen); |
902 | 0 | possiblekey = g_new0(uint8_t, luks->header.master_key_len); |
903 | | |
904 | | /* |
905 | | * The user password is used to generate a (possible) |
906 | | * decryption key. This may or may not successfully |
907 | | * decrypt the master key - we just blindly assume |
908 | | * the key is correct and validate the results of |
909 | | * decryption later. |
910 | | */ |
911 | 0 | if (qcrypto_pbkdf2(luks->hash_alg, |
912 | 0 | (const uint8_t *)password, strlen(password), |
913 | 0 | slot->salt, QCRYPTO_BLOCK_LUKS_SALT_LEN, |
914 | 0 | slot->iterations, |
915 | 0 | possiblekey, luks->header.master_key_len, |
916 | 0 | errp) < 0) { |
917 | 0 | return -1; |
918 | 0 | } |
919 | | |
920 | | /* |
921 | | * We need to read the master key material from the |
922 | | * LUKS key material header. What we're reading is |
923 | | * not the raw master key, but rather the data after |
924 | | * it has been passed through AFSplit and the result |
925 | | * then encrypted. |
926 | | */ |
927 | 0 | rv = readfunc(block, |
928 | 0 | slot->key_offset_sector * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
929 | 0 | splitkey, splitkeylen, |
930 | 0 | opaque, |
931 | 0 | errp); |
932 | 0 | if (rv < 0) { |
933 | 0 | return -1; |
934 | 0 | } |
935 | | |
936 | | |
937 | | /* Setup the cipher/ivgen that we'll use to try to decrypt |
938 | | * the split master key material */ |
939 | 0 | cipher = qcrypto_cipher_new(luks->cipher_alg, |
940 | 0 | luks->cipher_mode, |
941 | 0 | possiblekey, |
942 | 0 | luks->header.master_key_len, |
943 | 0 | errp); |
944 | 0 | if (!cipher) { |
945 | 0 | return -1; |
946 | 0 | } |
947 | | |
948 | 0 | niv = qcrypto_cipher_get_iv_len(luks->cipher_alg, |
949 | 0 | luks->cipher_mode); |
950 | |
|
951 | 0 | ivgen = qcrypto_ivgen_new(luks->ivgen_alg, |
952 | 0 | luks->ivgen_cipher_alg, |
953 | 0 | luks->ivgen_hash_alg, |
954 | 0 | possiblekey, |
955 | 0 | luks->header.master_key_len, |
956 | 0 | errp); |
957 | 0 | if (!ivgen) { |
958 | 0 | return -1; |
959 | 0 | } |
960 | | |
961 | | |
962 | | /* |
963 | | * The master key needs to be decrypted in the same |
964 | | * way that the block device payload will be decrypted |
965 | | * later. In particular we'll be using the IV generator |
966 | | * to reset the encryption cipher every time the master |
967 | | * key crosses a sector boundary. |
968 | | */ |
969 | 0 | if (qcrypto_block_cipher_decrypt_helper(cipher, |
970 | 0 | niv, |
971 | 0 | ivgen, |
972 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
973 | 0 | 0, |
974 | 0 | splitkey, |
975 | 0 | splitkeylen, |
976 | 0 | errp) < 0) { |
977 | 0 | return -1; |
978 | 0 | } |
979 | | |
980 | | /* |
981 | | * Now we've decrypted the split master key, join |
982 | | * it back together to get the actual master key. |
983 | | */ |
984 | 0 | if (qcrypto_afsplit_decode(luks->hash_alg, |
985 | 0 | luks->header.master_key_len, |
986 | 0 | slot->stripes, |
987 | 0 | splitkey, |
988 | 0 | masterkey, |
989 | 0 | errp) < 0) { |
990 | 0 | return -1; |
991 | 0 | } |
992 | | |
993 | | |
994 | | /* |
995 | | * We still don't know that the masterkey we got is valid, |
996 | | * because we just blindly assumed the user's password |
997 | | * was correct. This is where we now verify it. We are |
998 | | * creating a hash of the master key using PBKDF and |
999 | | * then comparing that to the hash stored in the key slot |
1000 | | * header |
1001 | | */ |
1002 | 0 | if (qcrypto_pbkdf2(luks->hash_alg, |
1003 | 0 | masterkey, |
1004 | 0 | luks->header.master_key_len, |
1005 | 0 | luks->header.master_key_salt, |
1006 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
1007 | 0 | luks->header.master_key_iterations, |
1008 | 0 | keydigest, |
1009 | 0 | G_N_ELEMENTS(keydigest), |
1010 | 0 | errp) < 0) { |
1011 | 0 | return -1; |
1012 | 0 | } |
1013 | | |
1014 | 0 | if (memcmp(keydigest, luks->header.master_key_digest, |
1015 | 0 | QCRYPTO_BLOCK_LUKS_DIGEST_LEN) == 0) { |
1016 | | /* Success, we got the right master key */ |
1017 | 0 | return 1; |
1018 | 0 | } |
1019 | | |
1020 | | /* Fail, user's password was not valid for this key slot, |
1021 | | * tell caller to try another slot */ |
1022 | 0 | return 0; |
1023 | 0 | } |
1024 | | |
1025 | | |
1026 | | /* |
1027 | | * Given a user password, this will iterate over all key |
1028 | | * slots and try to unlock each active key slot using the |
1029 | | * password until it successfully obtains a master key. |
1030 | | * |
1031 | | * Returns 0 if a key was loaded, -1 if no keys could be loaded |
1032 | | */ |
1033 | | static int |
1034 | | qcrypto_block_luks_find_key(QCryptoBlock *block, |
1035 | | const char *password, |
1036 | | uint8_t *masterkey, |
1037 | | QCryptoBlockReadFunc readfunc, |
1038 | | void *opaque, |
1039 | | Error **errp) |
1040 | 0 | { |
1041 | 0 | size_t i; |
1042 | 0 | int rv; |
1043 | |
|
1044 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1045 | 0 | rv = qcrypto_block_luks_load_key(block, |
1046 | 0 | i, |
1047 | 0 | password, |
1048 | 0 | masterkey, |
1049 | 0 | readfunc, |
1050 | 0 | opaque, |
1051 | 0 | errp); |
1052 | 0 | if (rv < 0) { |
1053 | 0 | goto error; |
1054 | 0 | } |
1055 | 0 | if (rv == 1) { |
1056 | 0 | return 0; |
1057 | 0 | } |
1058 | 0 | } |
1059 | | |
1060 | 0 | error_setg(errp, "Invalid password, cannot unlock any keyslot"); |
1061 | 0 | error: |
1062 | 0 | return -1; |
1063 | 0 | } |
1064 | | |
1065 | | /* |
1066 | | * Returns true if a slot i is marked as active |
1067 | | * (contains encrypted copy of the master key) |
1068 | | */ |
1069 | | static bool |
1070 | | qcrypto_block_luks_slot_active(const QCryptoBlockLUKS *luks, |
1071 | | unsigned int slot_idx) |
1072 | 0 | { |
1073 | 0 | uint32_t val; |
1074 | |
|
1075 | 0 | assert(slot_idx < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS); |
1076 | 0 | val = luks->header.key_slots[slot_idx].active; |
1077 | 0 | return val == QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED; |
1078 | 0 | } |
1079 | | |
1080 | | /* |
1081 | | * Returns the number of slots that are marked as active |
1082 | | * (slots that contain encrypted copy of the master key) |
1083 | | */ |
1084 | | static unsigned int |
1085 | | qcrypto_block_luks_count_active_slots(const QCryptoBlockLUKS *luks) |
1086 | 0 | { |
1087 | 0 | size_t i = 0; |
1088 | 0 | unsigned int ret = 0; |
1089 | |
|
1090 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1091 | 0 | if (qcrypto_block_luks_slot_active(luks, i)) { |
1092 | 0 | ret++; |
1093 | 0 | } |
1094 | 0 | } |
1095 | 0 | return ret; |
1096 | 0 | } |
1097 | | |
1098 | | /* |
1099 | | * Finds first key slot which is not active |
1100 | | * Returns the key slot index, or -1 if it doesn't exist |
1101 | | */ |
1102 | | static int |
1103 | | qcrypto_block_luks_find_free_keyslot(const QCryptoBlockLUKS *luks) |
1104 | 0 | { |
1105 | 0 | size_t i; |
1106 | |
|
1107 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1108 | 0 | if (!qcrypto_block_luks_slot_active(luks, i)) { |
1109 | 0 | return i; |
1110 | 0 | } |
1111 | 0 | } |
1112 | 0 | return -1; |
1113 | 0 | } |
1114 | | |
1115 | | /* |
1116 | | * Erases an keyslot given its index |
1117 | | * Returns: |
1118 | | * 0 if the keyslot was erased successfully |
1119 | | * -1 if a error occurred while erasing the keyslot |
1120 | | * |
1121 | | */ |
1122 | | static int |
1123 | | qcrypto_block_luks_erase_key(QCryptoBlock *block, |
1124 | | unsigned int slot_idx, |
1125 | | QCryptoBlockWriteFunc writefunc, |
1126 | | void *opaque, |
1127 | | Error **errp) |
1128 | 0 | { |
1129 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
1130 | 0 | QCryptoBlockLUKSKeySlot *slot; |
1131 | 0 | g_autofree uint8_t *garbagesplitkey = NULL; |
1132 | 0 | size_t splitkeylen; |
1133 | 0 | size_t i; |
1134 | 0 | Error *local_err = NULL; |
1135 | 0 | int ret; |
1136 | |
|
1137 | 0 | assert(slot_idx < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS); |
1138 | 0 | slot = &luks->header.key_slots[slot_idx]; |
1139 | |
|
1140 | 0 | splitkeylen = luks->header.master_key_len * slot->stripes; |
1141 | 0 | assert(splitkeylen > 0); |
1142 | |
|
1143 | 0 | garbagesplitkey = g_new0(uint8_t, splitkeylen); |
1144 | | |
1145 | | /* Reset the key slot header */ |
1146 | 0 | memset(slot->salt, 0, QCRYPTO_BLOCK_LUKS_SALT_LEN); |
1147 | 0 | slot->iterations = 0; |
1148 | 0 | slot->active = QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED; |
1149 | |
|
1150 | 0 | ret = qcrypto_block_luks_store_header(block, writefunc, |
1151 | 0 | opaque, &local_err); |
1152 | |
|
1153 | 0 | if (ret < 0) { |
1154 | 0 | error_propagate(errp, local_err); |
1155 | 0 | } |
1156 | | /* |
1157 | | * Now try to erase the key material, even if the header |
1158 | | * update failed |
1159 | | */ |
1160 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_ERASE_ITERATIONS; i++) { |
1161 | 0 | if (qcrypto_random_bytes(garbagesplitkey, |
1162 | 0 | splitkeylen, &local_err) < 0) { |
1163 | | /* |
1164 | | * If we failed to get the random data, still write |
1165 | | * at least zeros to the key slot at least once |
1166 | | */ |
1167 | 0 | error_propagate(errp, local_err); |
1168 | |
|
1169 | 0 | if (i > 0) { |
1170 | 0 | return -1; |
1171 | 0 | } |
1172 | 0 | } |
1173 | 0 | if (writefunc(block, |
1174 | 0 | slot->key_offset_sector * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
1175 | 0 | garbagesplitkey, |
1176 | 0 | splitkeylen, |
1177 | 0 | opaque, |
1178 | 0 | &local_err) < 0) { |
1179 | 0 | error_propagate(errp, local_err); |
1180 | 0 | return -1; |
1181 | 0 | } |
1182 | 0 | } |
1183 | 0 | return ret; |
1184 | 0 | } |
1185 | | |
1186 | | static int |
1187 | | qcrypto_block_luks_open(QCryptoBlock *block, |
1188 | | QCryptoBlockOpenOptions *options, |
1189 | | const char *optprefix, |
1190 | | QCryptoBlockReadFunc readfunc, |
1191 | | void *opaque, |
1192 | | unsigned int flags, |
1193 | | Error **errp) |
1194 | 0 | { |
1195 | 0 | QCryptoBlockLUKS *luks = NULL; |
1196 | 0 | g_autofree uint8_t *masterkey = NULL; |
1197 | 0 | g_autofree char *password = NULL; |
1198 | |
|
1199 | 0 | if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) { |
1200 | 0 | if (!options->u.luks.key_secret) { |
1201 | 0 | error_setg(errp, "Parameter '%skey-secret' is required for cipher", |
1202 | 0 | optprefix ? optprefix : ""); |
1203 | 0 | return -1; |
1204 | 0 | } |
1205 | 0 | password = qcrypto_secret_lookup_as_utf8( |
1206 | 0 | options->u.luks.key_secret, errp); |
1207 | 0 | if (!password) { |
1208 | 0 | return -1; |
1209 | 0 | } |
1210 | 0 | } |
1211 | | |
1212 | 0 | luks = g_new0(QCryptoBlockLUKS, 1); |
1213 | 0 | block->opaque = luks; |
1214 | 0 | luks->secret = g_strdup(options->u.luks.key_secret); |
1215 | |
|
1216 | 0 | if (qcrypto_block_luks_load_header(block, readfunc, opaque, errp) < 0) { |
1217 | 0 | goto fail; |
1218 | 0 | } |
1219 | | |
1220 | 0 | if (qcrypto_block_luks_check_header(luks, flags, errp) < 0) { |
1221 | 0 | goto fail; |
1222 | 0 | } |
1223 | | |
1224 | 0 | if (qcrypto_block_luks_parse_header(luks, errp) < 0) { |
1225 | 0 | goto fail; |
1226 | 0 | } |
1227 | | |
1228 | 0 | if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) { |
1229 | | /* Try to find which key slot our password is valid for |
1230 | | * and unlock the master key from that slot. |
1231 | | */ |
1232 | |
|
1233 | 0 | masterkey = g_new0(uint8_t, luks->header.master_key_len); |
1234 | |
|
1235 | 0 | if (qcrypto_block_luks_find_key(block, |
1236 | 0 | password, |
1237 | 0 | masterkey, |
1238 | 0 | readfunc, opaque, |
1239 | 0 | errp) < 0) { |
1240 | 0 | goto fail; |
1241 | 0 | } |
1242 | | |
1243 | | /* We have a valid master key now, so can setup the |
1244 | | * block device payload decryption objects |
1245 | | */ |
1246 | 0 | block->kdfhash = luks->hash_alg; |
1247 | 0 | block->niv = qcrypto_cipher_get_iv_len(luks->cipher_alg, |
1248 | 0 | luks->cipher_mode); |
1249 | |
|
1250 | 0 | block->ivgen = qcrypto_ivgen_new(luks->ivgen_alg, |
1251 | 0 | luks->ivgen_cipher_alg, |
1252 | 0 | luks->ivgen_hash_alg, |
1253 | 0 | masterkey, |
1254 | 0 | luks->header.master_key_len, |
1255 | 0 | errp); |
1256 | 0 | if (!block->ivgen) { |
1257 | 0 | goto fail; |
1258 | 0 | } |
1259 | | |
1260 | 0 | if (qcrypto_block_init_cipher(block, |
1261 | 0 | luks->cipher_alg, |
1262 | 0 | luks->cipher_mode, |
1263 | 0 | masterkey, |
1264 | 0 | luks->header.master_key_len, |
1265 | 0 | errp) < 0) { |
1266 | 0 | goto fail; |
1267 | 0 | } |
1268 | 0 | } |
1269 | | |
1270 | 0 | block->sector_size = QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; |
1271 | 0 | block->payload_offset = luks->header.payload_offset_sector * |
1272 | 0 | block->sector_size; |
1273 | 0 | block->detached_header = (block->payload_offset == 0) ? true : false; |
1274 | |
|
1275 | 0 | return 0; |
1276 | | |
1277 | 0 | fail: |
1278 | 0 | qcrypto_block_free_cipher(block); |
1279 | 0 | qcrypto_ivgen_free(block->ivgen); |
1280 | 0 | g_free(luks->secret); |
1281 | 0 | g_free(luks); |
1282 | 0 | return -1; |
1283 | 0 | } |
1284 | | |
1285 | | |
1286 | | static void |
1287 | | qcrypto_block_luks_uuid_gen(uint8_t *uuidstr) |
1288 | 0 | { |
1289 | 0 | QemuUUID uuid; |
1290 | 0 | qemu_uuid_generate(&uuid); |
1291 | 0 | qemu_uuid_unparse(&uuid, (char *)uuidstr); |
1292 | 0 | } |
1293 | | |
1294 | | static int |
1295 | | qcrypto_block_luks_create(QCryptoBlock *block, |
1296 | | QCryptoBlockCreateOptions *options, |
1297 | | const char *optprefix, |
1298 | | QCryptoBlockInitFunc initfunc, |
1299 | | QCryptoBlockWriteFunc writefunc, |
1300 | | void *opaque, |
1301 | | Error **errp) |
1302 | 0 | { |
1303 | 0 | QCryptoBlockLUKS *luks; |
1304 | 0 | QCryptoBlockCreateOptionsLUKS luks_opts; |
1305 | 0 | Error *local_err = NULL; |
1306 | 0 | g_autofree uint8_t *masterkey = NULL; |
1307 | 0 | size_t header_sectors; |
1308 | 0 | size_t split_key_sectors; |
1309 | 0 | size_t i; |
1310 | 0 | g_autofree char *password = NULL; |
1311 | 0 | const char *cipher_alg; |
1312 | 0 | const char *cipher_mode; |
1313 | 0 | const char *ivgen_alg; |
1314 | 0 | const char *ivgen_hash_alg = NULL; |
1315 | 0 | const char *hash_alg; |
1316 | 0 | g_autofree char *cipher_mode_spec = NULL; |
1317 | 0 | uint64_t iters; |
1318 | 0 | uint64_t detached_header_size; |
1319 | |
|
1320 | 0 | memcpy(&luks_opts, &options->u.luks, sizeof(luks_opts)); |
1321 | 0 | if (!luks_opts.has_iter_time) { |
1322 | 0 | luks_opts.iter_time = QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS; |
1323 | 0 | } |
1324 | 0 | if (!luks_opts.has_cipher_alg) { |
1325 | 0 | luks_opts.cipher_alg = QCRYPTO_CIPHER_ALGO_AES_256; |
1326 | 0 | } |
1327 | 0 | if (!luks_opts.has_cipher_mode) { |
1328 | 0 | luks_opts.cipher_mode = QCRYPTO_CIPHER_MODE_XTS; |
1329 | 0 | } |
1330 | 0 | if (!luks_opts.has_ivgen_alg) { |
1331 | 0 | luks_opts.ivgen_alg = QCRYPTO_IV_GEN_ALGO_PLAIN64; |
1332 | 0 | } |
1333 | 0 | if (!luks_opts.has_hash_alg) { |
1334 | 0 | luks_opts.hash_alg = QCRYPTO_HASH_ALGO_SHA256; |
1335 | 0 | } |
1336 | 0 | if (luks_opts.ivgen_alg == QCRYPTO_IV_GEN_ALGO_ESSIV) { |
1337 | 0 | if (!luks_opts.has_ivgen_hash_alg) { |
1338 | 0 | luks_opts.ivgen_hash_alg = QCRYPTO_HASH_ALGO_SHA256; |
1339 | 0 | luks_opts.has_ivgen_hash_alg = true; |
1340 | 0 | } |
1341 | 0 | } |
1342 | |
|
1343 | 0 | luks = g_new0(QCryptoBlockLUKS, 1); |
1344 | 0 | block->opaque = luks; |
1345 | |
|
1346 | 0 | luks->cipher_alg = luks_opts.cipher_alg; |
1347 | 0 | luks->cipher_mode = luks_opts.cipher_mode; |
1348 | 0 | luks->ivgen_alg = luks_opts.ivgen_alg; |
1349 | 0 | luks->ivgen_hash_alg = luks_opts.ivgen_hash_alg; |
1350 | 0 | luks->hash_alg = luks_opts.hash_alg; |
1351 | | |
1352 | | |
1353 | | /* Note we're allowing ivgen_hash_alg to be set even for |
1354 | | * non-essiv iv generators that don't need a hash. It will |
1355 | | * be silently ignored, for compatibility with dm-crypt */ |
1356 | |
|
1357 | 0 | if (!options->u.luks.key_secret) { |
1358 | 0 | error_setg(errp, "Parameter '%skey-secret' is required for cipher", |
1359 | 0 | optprefix ? optprefix : ""); |
1360 | 0 | goto error; |
1361 | 0 | } |
1362 | 0 | luks->secret = g_strdup(options->u.luks.key_secret); |
1363 | |
|
1364 | 0 | password = qcrypto_secret_lookup_as_utf8(luks_opts.key_secret, errp); |
1365 | 0 | if (!password) { |
1366 | 0 | goto error; |
1367 | 0 | } |
1368 | | |
1369 | | |
1370 | 0 | memcpy(luks->header.magic, qcrypto_block_luks_magic, |
1371 | 0 | QCRYPTO_BLOCK_LUKS_MAGIC_LEN); |
1372 | | |
1373 | | /* We populate the header in native endianness initially and |
1374 | | * then convert everything to big endian just before writing |
1375 | | * it out to disk |
1376 | | */ |
1377 | 0 | luks->header.version = QCRYPTO_BLOCK_LUKS_VERSION; |
1378 | 0 | qcrypto_block_luks_uuid_gen(luks->header.uuid); |
1379 | |
|
1380 | 0 | cipher_alg = qcrypto_block_luks_cipher_alg_lookup(luks_opts.cipher_alg, |
1381 | 0 | errp); |
1382 | 0 | if (!cipher_alg) { |
1383 | 0 | goto error; |
1384 | 0 | } |
1385 | | |
1386 | 0 | cipher_mode = QCryptoCipherMode_str(luks_opts.cipher_mode); |
1387 | 0 | ivgen_alg = QCryptoIVGenAlgo_str(luks_opts.ivgen_alg); |
1388 | 0 | if (luks_opts.has_ivgen_hash_alg) { |
1389 | 0 | ivgen_hash_alg = QCryptoHashAlgo_str(luks_opts.ivgen_hash_alg); |
1390 | 0 | cipher_mode_spec = g_strdup_printf("%s-%s:%s", cipher_mode, ivgen_alg, |
1391 | 0 | ivgen_hash_alg); |
1392 | 0 | } else { |
1393 | 0 | cipher_mode_spec = g_strdup_printf("%s-%s", cipher_mode, ivgen_alg); |
1394 | 0 | } |
1395 | 0 | hash_alg = QCryptoHashAlgo_str(luks_opts.hash_alg); |
1396 | | |
1397 | |
|
1398 | 0 | if (strlen(cipher_alg) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN) { |
1399 | 0 | error_setg(errp, "Cipher name '%s' is too long for LUKS header", |
1400 | 0 | cipher_alg); |
1401 | 0 | goto error; |
1402 | 0 | } |
1403 | 0 | if (strlen(cipher_mode_spec) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN) { |
1404 | 0 | error_setg(errp, "Cipher mode '%s' is too long for LUKS header", |
1405 | 0 | cipher_mode_spec); |
1406 | 0 | goto error; |
1407 | 0 | } |
1408 | 0 | if (strlen(hash_alg) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN) { |
1409 | 0 | error_setg(errp, "Hash name '%s' is too long for LUKS header", |
1410 | 0 | hash_alg); |
1411 | 0 | goto error; |
1412 | 0 | } |
1413 | | |
1414 | 0 | if (luks_opts.ivgen_alg == QCRYPTO_IV_GEN_ALGO_ESSIV) { |
1415 | 0 | luks->ivgen_cipher_alg = |
1416 | 0 | qcrypto_block_luks_essiv_cipher(luks_opts.cipher_alg, |
1417 | 0 | luks_opts.ivgen_hash_alg, |
1418 | 0 | &local_err); |
1419 | 0 | if (local_err) { |
1420 | 0 | error_propagate(errp, local_err); |
1421 | 0 | goto error; |
1422 | 0 | } |
1423 | 0 | } else { |
1424 | 0 | luks->ivgen_cipher_alg = luks_opts.cipher_alg; |
1425 | 0 | } |
1426 | | |
1427 | 0 | strcpy(luks->header.cipher_name, cipher_alg); |
1428 | 0 | strcpy(luks->header.cipher_mode, cipher_mode_spec); |
1429 | 0 | strcpy(luks->header.hash_spec, hash_alg); |
1430 | |
|
1431 | 0 | luks->header.master_key_len = |
1432 | 0 | qcrypto_cipher_get_key_len(luks_opts.cipher_alg); |
1433 | |
|
1434 | 0 | if (luks_opts.cipher_mode == QCRYPTO_CIPHER_MODE_XTS) { |
1435 | 0 | luks->header.master_key_len *= 2; |
1436 | 0 | } |
1437 | | |
1438 | | /* Generate the salt used for hashing the master key |
1439 | | * with PBKDF later |
1440 | | */ |
1441 | 0 | if (qcrypto_random_bytes(luks->header.master_key_salt, |
1442 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
1443 | 0 | errp) < 0) { |
1444 | 0 | goto error; |
1445 | 0 | } |
1446 | | |
1447 | | /* Generate random master key */ |
1448 | 0 | masterkey = g_new0(uint8_t, luks->header.master_key_len); |
1449 | 0 | if (qcrypto_random_bytes(masterkey, |
1450 | 0 | luks->header.master_key_len, errp) < 0) { |
1451 | 0 | goto error; |
1452 | 0 | } |
1453 | | |
1454 | | |
1455 | | /* Setup the block device payload encryption objects */ |
1456 | 0 | if (qcrypto_block_init_cipher(block, luks_opts.cipher_alg, |
1457 | 0 | luks_opts.cipher_mode, masterkey, |
1458 | 0 | luks->header.master_key_len, errp) < 0) { |
1459 | 0 | goto error; |
1460 | 0 | } |
1461 | | |
1462 | 0 | block->kdfhash = luks_opts.hash_alg; |
1463 | 0 | block->niv = qcrypto_cipher_get_iv_len(luks_opts.cipher_alg, |
1464 | 0 | luks_opts.cipher_mode); |
1465 | 0 | block->ivgen = qcrypto_ivgen_new(luks_opts.ivgen_alg, |
1466 | 0 | luks->ivgen_cipher_alg, |
1467 | 0 | luks_opts.ivgen_hash_alg, |
1468 | 0 | masterkey, luks->header.master_key_len, |
1469 | 0 | errp); |
1470 | |
|
1471 | 0 | if (!block->ivgen) { |
1472 | 0 | goto error; |
1473 | 0 | } |
1474 | | |
1475 | | |
1476 | | /* Determine how many iterations we need to hash the master |
1477 | | * key, in order to have 1 second of compute time used |
1478 | | */ |
1479 | 0 | iters = qcrypto_pbkdf2_count_iters(luks_opts.hash_alg, |
1480 | 0 | masterkey, luks->header.master_key_len, |
1481 | 0 | luks->header.master_key_salt, |
1482 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
1483 | 0 | QCRYPTO_BLOCK_LUKS_DIGEST_LEN, |
1484 | 0 | &local_err); |
1485 | 0 | if (local_err) { |
1486 | 0 | error_propagate(errp, local_err); |
1487 | 0 | goto error; |
1488 | 0 | } |
1489 | | |
1490 | 0 | if (iters > (ULLONG_MAX / luks_opts.iter_time)) { |
1491 | 0 | error_setg_errno(errp, ERANGE, |
1492 | 0 | "PBKDF iterations %llu too large to scale", |
1493 | 0 | (unsigned long long)iters); |
1494 | 0 | goto error; |
1495 | 0 | } |
1496 | | |
1497 | | /* iter_time was in millis, but count_iters reported for secs */ |
1498 | 0 | iters = iters * luks_opts.iter_time / 1000; |
1499 | | |
1500 | | /* Why /= 8 ? That matches cryptsetup, but there's no |
1501 | | * explanation why they chose /= 8... Probably so that |
1502 | | * if all 8 keyslots are active we only spend 1 second |
1503 | | * in total time to check all keys */ |
1504 | 0 | iters /= 8; |
1505 | 0 | if (iters > UINT32_MAX) { |
1506 | 0 | error_setg_errno(errp, ERANGE, |
1507 | 0 | "PBKDF iterations %llu larger than %u", |
1508 | 0 | (unsigned long long)iters, UINT32_MAX); |
1509 | 0 | goto error; |
1510 | 0 | } |
1511 | 0 | iters = MAX(iters, QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS); |
1512 | 0 | luks->header.master_key_iterations = iters; |
1513 | | |
1514 | | /* Hash the master key, saving the result in the LUKS |
1515 | | * header. This hash is used when opening the encrypted |
1516 | | * device to verify that the user password unlocked a |
1517 | | * valid master key |
1518 | | */ |
1519 | 0 | if (qcrypto_pbkdf2(luks_opts.hash_alg, |
1520 | 0 | masterkey, luks->header.master_key_len, |
1521 | 0 | luks->header.master_key_salt, |
1522 | 0 | QCRYPTO_BLOCK_LUKS_SALT_LEN, |
1523 | 0 | luks->header.master_key_iterations, |
1524 | 0 | luks->header.master_key_digest, |
1525 | 0 | QCRYPTO_BLOCK_LUKS_DIGEST_LEN, |
1526 | 0 | errp) < 0) { |
1527 | 0 | goto error; |
1528 | 0 | } |
1529 | | |
1530 | | /* start with the sector that follows the header*/ |
1531 | 0 | header_sectors = QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET / |
1532 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; |
1533 | |
|
1534 | 0 | split_key_sectors = |
1535 | 0 | qcrypto_block_luks_splitkeylen_sectors(luks, |
1536 | 0 | header_sectors, |
1537 | 0 | QCRYPTO_BLOCK_LUKS_STRIPES); |
1538 | |
|
1539 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1540 | 0 | QCryptoBlockLUKSKeySlot *slot = &luks->header.key_slots[i]; |
1541 | 0 | slot->active = QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED; |
1542 | |
|
1543 | 0 | slot->key_offset_sector = header_sectors + i * split_key_sectors; |
1544 | 0 | slot->stripes = QCRYPTO_BLOCK_LUKS_STRIPES; |
1545 | 0 | } |
1546 | |
|
1547 | 0 | if (block->detached_header) { |
1548 | | /* |
1549 | | * For a detached LUKS header image, set the payload_offset_sector |
1550 | | * to 0 to specify the starting point for read/write |
1551 | | */ |
1552 | 0 | luks->header.payload_offset_sector = 0; |
1553 | 0 | } else { |
1554 | | /* |
1555 | | * The total size of the LUKS headers is the partition header + key |
1556 | | * slot headers, rounded up to the nearest sector, combined with |
1557 | | * the size of each master key material region, also rounded up |
1558 | | * to the nearest sector |
1559 | | */ |
1560 | 0 | luks->header.payload_offset_sector = header_sectors + |
1561 | 0 | QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS * split_key_sectors; |
1562 | 0 | } |
1563 | |
|
1564 | 0 | block->sector_size = QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; |
1565 | 0 | block->payload_offset = luks->header.payload_offset_sector * |
1566 | 0 | block->sector_size; |
1567 | 0 | detached_header_size = |
1568 | 0 | (header_sectors + QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS * |
1569 | 0 | split_key_sectors) * block->sector_size; |
1570 | | |
1571 | | /* Reserve header space to match payload offset */ |
1572 | 0 | initfunc(block, detached_header_size, opaque, &local_err); |
1573 | 0 | if (local_err) { |
1574 | 0 | error_propagate(errp, local_err); |
1575 | 0 | goto error; |
1576 | 0 | } |
1577 | | |
1578 | | |
1579 | | /* populate the slot 0 with the password encrypted master key*/ |
1580 | | /* This will also store the header */ |
1581 | 0 | if (qcrypto_block_luks_store_key(block, |
1582 | 0 | 0, |
1583 | 0 | password, |
1584 | 0 | masterkey, |
1585 | 0 | luks_opts.iter_time, |
1586 | 0 | writefunc, |
1587 | 0 | opaque, |
1588 | 0 | errp) < 0) { |
1589 | 0 | goto error; |
1590 | 0 | } |
1591 | | |
1592 | 0 | memset(masterkey, 0, luks->header.master_key_len); |
1593 | |
|
1594 | 0 | return 0; |
1595 | | |
1596 | 0 | error: |
1597 | 0 | if (masterkey) { |
1598 | 0 | memset(masterkey, 0, luks->header.master_key_len); |
1599 | 0 | } |
1600 | |
|
1601 | 0 | qcrypto_block_free_cipher(block); |
1602 | 0 | qcrypto_ivgen_free(block->ivgen); |
1603 | |
|
1604 | 0 | g_free(luks->secret); |
1605 | 0 | g_free(luks); |
1606 | 0 | return -1; |
1607 | 0 | } |
1608 | | |
1609 | | static int |
1610 | | qcrypto_block_luks_amend_add_keyslot(QCryptoBlock *block, |
1611 | | QCryptoBlockReadFunc readfunc, |
1612 | | QCryptoBlockWriteFunc writefunc, |
1613 | | void *opaque, |
1614 | | QCryptoBlockAmendOptionsLUKS *opts_luks, |
1615 | | bool force, |
1616 | | Error **errp) |
1617 | 0 | { |
1618 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
1619 | 0 | uint64_t iter_time = opts_luks->has_iter_time ? |
1620 | 0 | opts_luks->iter_time : |
1621 | 0 | QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS; |
1622 | 0 | int keyslot; |
1623 | 0 | g_autofree char *old_password = NULL; |
1624 | 0 | g_autofree char *new_password = NULL; |
1625 | 0 | g_autofree uint8_t *master_key = NULL; |
1626 | |
|
1627 | 0 | char *secret = opts_luks->secret ?: luks->secret; |
1628 | |
|
1629 | 0 | if (!opts_luks->new_secret) { |
1630 | 0 | error_setg(errp, "'new-secret' is required to activate a keyslot"); |
1631 | 0 | return -1; |
1632 | 0 | } |
1633 | 0 | if (opts_luks->old_secret) { |
1634 | 0 | error_setg(errp, |
1635 | 0 | "'old-secret' must not be given when activating keyslots"); |
1636 | 0 | return -1; |
1637 | 0 | } |
1638 | | |
1639 | 0 | if (opts_luks->has_keyslot) { |
1640 | 0 | keyslot = opts_luks->keyslot; |
1641 | 0 | if (keyslot < 0 || keyslot >= QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS) { |
1642 | 0 | error_setg(errp, |
1643 | 0 | "Invalid keyslot %u specified, must be between 0 and %u", |
1644 | 0 | keyslot, QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS - 1); |
1645 | 0 | return -1; |
1646 | 0 | } |
1647 | 0 | } else { |
1648 | 0 | keyslot = qcrypto_block_luks_find_free_keyslot(luks); |
1649 | 0 | if (keyslot == -1) { |
1650 | 0 | error_setg(errp, |
1651 | 0 | "Can't add a keyslot - all keyslots are in use"); |
1652 | 0 | return -1; |
1653 | 0 | } |
1654 | 0 | } |
1655 | | |
1656 | 0 | if (!force && qcrypto_block_luks_slot_active(luks, keyslot)) { |
1657 | 0 | error_setg(errp, |
1658 | 0 | "Refusing to overwrite active keyslot %i - " |
1659 | 0 | "please erase it first", |
1660 | 0 | keyslot); |
1661 | 0 | return -1; |
1662 | 0 | } |
1663 | | |
1664 | | /* Locate the password that will be used to retrieve the master key */ |
1665 | 0 | old_password = qcrypto_secret_lookup_as_utf8(secret, errp); |
1666 | 0 | if (!old_password) { |
1667 | 0 | return -1; |
1668 | 0 | } |
1669 | | |
1670 | | /* Retrieve the master key */ |
1671 | 0 | master_key = g_new0(uint8_t, luks->header.master_key_len); |
1672 | |
|
1673 | 0 | if (qcrypto_block_luks_find_key(block, old_password, master_key, |
1674 | 0 | readfunc, opaque, errp) < 0) { |
1675 | 0 | error_append_hint(errp, "Failed to retrieve the master key"); |
1676 | 0 | return -1; |
1677 | 0 | } |
1678 | | |
1679 | | /* Locate the new password*/ |
1680 | 0 | new_password = qcrypto_secret_lookup_as_utf8(opts_luks->new_secret, errp); |
1681 | 0 | if (!new_password) { |
1682 | 0 | return -1; |
1683 | 0 | } |
1684 | | |
1685 | | /* Now set the new keyslots */ |
1686 | 0 | if (qcrypto_block_luks_store_key(block, keyslot, new_password, master_key, |
1687 | 0 | iter_time, writefunc, opaque, errp)) { |
1688 | 0 | error_append_hint(errp, "Failed to write to keyslot %i", keyslot); |
1689 | 0 | return -1; |
1690 | 0 | } |
1691 | 0 | return 0; |
1692 | 0 | } |
1693 | | |
1694 | | static int |
1695 | | qcrypto_block_luks_amend_erase_keyslots(QCryptoBlock *block, |
1696 | | QCryptoBlockReadFunc readfunc, |
1697 | | QCryptoBlockWriteFunc writefunc, |
1698 | | void *opaque, |
1699 | | QCryptoBlockAmendOptionsLUKS *opts_luks, |
1700 | | bool force, |
1701 | | Error **errp) |
1702 | 0 | { |
1703 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
1704 | 0 | g_autofree uint8_t *tmpkey = NULL; |
1705 | 0 | g_autofree char *old_password = NULL; |
1706 | |
|
1707 | 0 | if (opts_luks->new_secret) { |
1708 | 0 | error_setg(errp, |
1709 | 0 | "'new-secret' must not be given when erasing keyslots"); |
1710 | 0 | return -1; |
1711 | 0 | } |
1712 | 0 | if (opts_luks->has_iter_time) { |
1713 | 0 | error_setg(errp, |
1714 | 0 | "'iter-time' must not be given when erasing keyslots"); |
1715 | 0 | return -1; |
1716 | 0 | } |
1717 | 0 | if (opts_luks->secret) { |
1718 | 0 | error_setg(errp, |
1719 | 0 | "'secret' must not be given when erasing keyslots"); |
1720 | 0 | return -1; |
1721 | 0 | } |
1722 | | |
1723 | | /* Load the old password if given */ |
1724 | 0 | if (opts_luks->old_secret) { |
1725 | 0 | old_password = qcrypto_secret_lookup_as_utf8(opts_luks->old_secret, |
1726 | 0 | errp); |
1727 | 0 | if (!old_password) { |
1728 | 0 | return -1; |
1729 | 0 | } |
1730 | | |
1731 | | /* |
1732 | | * Allocate a temporary key buffer that we will need when |
1733 | | * checking if slot matches the given old password |
1734 | | */ |
1735 | 0 | tmpkey = g_new0(uint8_t, luks->header.master_key_len); |
1736 | 0 | } |
1737 | | |
1738 | | /* Erase an explicitly given keyslot */ |
1739 | 0 | if (opts_luks->has_keyslot) { |
1740 | 0 | int keyslot = opts_luks->keyslot; |
1741 | |
|
1742 | 0 | if (keyslot < 0 || keyslot >= QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS) { |
1743 | 0 | error_setg(errp, |
1744 | 0 | "Invalid keyslot %i specified, must be between 0 and %i", |
1745 | 0 | keyslot, QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS - 1); |
1746 | 0 | return -1; |
1747 | 0 | } |
1748 | | |
1749 | 0 | if (opts_luks->old_secret) { |
1750 | 0 | int rv = qcrypto_block_luks_load_key(block, |
1751 | 0 | keyslot, |
1752 | 0 | old_password, |
1753 | 0 | tmpkey, |
1754 | 0 | readfunc, |
1755 | 0 | opaque, |
1756 | 0 | errp); |
1757 | 0 | if (rv == -1) { |
1758 | 0 | return -1; |
1759 | 0 | } else if (rv == 0) { |
1760 | 0 | error_setg(errp, |
1761 | 0 | "Given keyslot %i doesn't contain the given " |
1762 | 0 | "old password for erase operation", |
1763 | 0 | keyslot); |
1764 | 0 | return -1; |
1765 | 0 | } |
1766 | 0 | } |
1767 | | |
1768 | 0 | if (!force && !qcrypto_block_luks_slot_active(luks, keyslot)) { |
1769 | 0 | error_setg(errp, |
1770 | 0 | "Given keyslot %i is already erased (inactive) ", |
1771 | 0 | keyslot); |
1772 | 0 | return -1; |
1773 | 0 | } |
1774 | | |
1775 | 0 | if (!force && qcrypto_block_luks_count_active_slots(luks) == 1) { |
1776 | 0 | error_setg(errp, |
1777 | 0 | "Attempt to erase the only active keyslot %i " |
1778 | 0 | "which will erase all the data in the image " |
1779 | 0 | "irreversibly - refusing operation", |
1780 | 0 | keyslot); |
1781 | 0 | return -1; |
1782 | 0 | } |
1783 | | |
1784 | 0 | if (qcrypto_block_luks_erase_key(block, keyslot, |
1785 | 0 | writefunc, opaque, errp)) { |
1786 | 0 | error_append_hint(errp, "Failed to erase keyslot %i", keyslot); |
1787 | 0 | return -1; |
1788 | 0 | } |
1789 | | |
1790 | | /* Erase all keyslots that match the given old password */ |
1791 | 0 | } else if (opts_luks->old_secret) { |
1792 | |
|
1793 | 0 | unsigned long slots_to_erase_bitmap = 0; |
1794 | 0 | size_t i; |
1795 | 0 | int slot_count; |
1796 | |
|
1797 | 0 | assert(QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS <= |
1798 | 0 | sizeof(slots_to_erase_bitmap) * 8); |
1799 | |
|
1800 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1801 | 0 | int rv = qcrypto_block_luks_load_key(block, |
1802 | 0 | i, |
1803 | 0 | old_password, |
1804 | 0 | tmpkey, |
1805 | 0 | readfunc, |
1806 | 0 | opaque, |
1807 | 0 | errp); |
1808 | 0 | if (rv == -1) { |
1809 | 0 | return -1; |
1810 | 0 | } else if (rv == 1) { |
1811 | 0 | bitmap_set(&slots_to_erase_bitmap, i, 1); |
1812 | 0 | } |
1813 | 0 | } |
1814 | | |
1815 | 0 | slot_count = bitmap_count_one(&slots_to_erase_bitmap, |
1816 | 0 | QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS); |
1817 | 0 | if (slot_count == 0) { |
1818 | 0 | error_setg(errp, |
1819 | 0 | "No keyslots match given (old) password for erase operation"); |
1820 | 0 | return -1; |
1821 | 0 | } |
1822 | | |
1823 | 0 | if (!force && |
1824 | 0 | slot_count == qcrypto_block_luks_count_active_slots(luks)) { |
1825 | 0 | error_setg(errp, |
1826 | 0 | "All the active keyslots match the (old) password that " |
1827 | 0 | "was given and erasing them will erase all the data in " |
1828 | 0 | "the image irreversibly - refusing operation"); |
1829 | 0 | return -1; |
1830 | 0 | } |
1831 | | |
1832 | | /* Now apply the update */ |
1833 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1834 | 0 | if (!test_bit(i, &slots_to_erase_bitmap)) { |
1835 | 0 | continue; |
1836 | 0 | } |
1837 | 0 | if (qcrypto_block_luks_erase_key(block, i, writefunc, |
1838 | 0 | opaque, errp)) { |
1839 | 0 | error_append_hint(errp, "Failed to erase keyslot %zu", i); |
1840 | 0 | return -1; |
1841 | 0 | } |
1842 | 0 | } |
1843 | 0 | } else { |
1844 | 0 | error_setg(errp, |
1845 | 0 | "To erase keyslot(s), either explicit keyslot index " |
1846 | 0 | "or the password currently contained in them must be given"); |
1847 | 0 | return -1; |
1848 | 0 | } |
1849 | 0 | return 0; |
1850 | 0 | } |
1851 | | |
1852 | | static int |
1853 | | qcrypto_block_luks_amend_options(QCryptoBlock *block, |
1854 | | QCryptoBlockReadFunc readfunc, |
1855 | | QCryptoBlockWriteFunc writefunc, |
1856 | | void *opaque, |
1857 | | QCryptoBlockAmendOptions *options, |
1858 | | bool force, |
1859 | | Error **errp) |
1860 | 0 | { |
1861 | 0 | QCryptoBlockAmendOptionsLUKS *opts_luks = &options->u.luks; |
1862 | |
|
1863 | 0 | switch (opts_luks->state) { |
1864 | 0 | case QCRYPTO_BLOCK_LUKS_KEYSLOT_STATE_ACTIVE: |
1865 | 0 | return qcrypto_block_luks_amend_add_keyslot(block, readfunc, |
1866 | 0 | writefunc, opaque, |
1867 | 0 | opts_luks, force, errp); |
1868 | 0 | case QCRYPTO_BLOCK_LUKS_KEYSLOT_STATE_INACTIVE: |
1869 | 0 | return qcrypto_block_luks_amend_erase_keyslots(block, readfunc, |
1870 | 0 | writefunc, opaque, |
1871 | 0 | opts_luks, force, errp); |
1872 | 0 | default: |
1873 | 0 | g_assert_not_reached(); |
1874 | 0 | } |
1875 | 0 | } |
1876 | | |
1877 | | static int qcrypto_block_luks_get_info(QCryptoBlock *block, |
1878 | | QCryptoBlockInfo *info, |
1879 | | Error **errp) |
1880 | 0 | { |
1881 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
1882 | 0 | QCryptoBlockInfoLUKSSlot *slot; |
1883 | 0 | QCryptoBlockInfoLUKSSlotList **tail = &info->u.luks.slots; |
1884 | 0 | size_t i; |
1885 | |
|
1886 | 0 | info->u.luks.cipher_alg = luks->cipher_alg; |
1887 | 0 | info->u.luks.cipher_mode = luks->cipher_mode; |
1888 | 0 | info->u.luks.ivgen_alg = luks->ivgen_alg; |
1889 | 0 | if (info->u.luks.ivgen_alg == QCRYPTO_IV_GEN_ALGO_ESSIV) { |
1890 | 0 | info->u.luks.has_ivgen_hash_alg = true; |
1891 | 0 | info->u.luks.ivgen_hash_alg = luks->ivgen_hash_alg; |
1892 | 0 | } |
1893 | 0 | info->u.luks.hash_alg = luks->hash_alg; |
1894 | 0 | info->u.luks.payload_offset = block->payload_offset; |
1895 | 0 | info->u.luks.master_key_iters = luks->header.master_key_iterations; |
1896 | 0 | info->u.luks.uuid = g_strndup((const char *)luks->header.uuid, |
1897 | 0 | sizeof(luks->header.uuid)); |
1898 | 0 | info->u.luks.detached_header = block->detached_header; |
1899 | |
|
1900 | 0 | for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) { |
1901 | 0 | slot = g_new0(QCryptoBlockInfoLUKSSlot, 1); |
1902 | 0 | slot->active = luks->header.key_slots[i].active == |
1903 | 0 | QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED; |
1904 | 0 | slot->key_offset = luks->header.key_slots[i].key_offset_sector |
1905 | 0 | * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; |
1906 | 0 | if (slot->active) { |
1907 | 0 | slot->has_iters = true; |
1908 | 0 | slot->iters = luks->header.key_slots[i].iterations; |
1909 | 0 | slot->has_stripes = true; |
1910 | 0 | slot->stripes = luks->header.key_slots[i].stripes; |
1911 | 0 | } |
1912 | |
|
1913 | 0 | QAPI_LIST_APPEND(tail, slot); |
1914 | 0 | } |
1915 | |
|
1916 | 0 | return 0; |
1917 | 0 | } |
1918 | | |
1919 | | |
1920 | | static void qcrypto_block_luks_cleanup(QCryptoBlock *block) |
1921 | 0 | { |
1922 | 0 | QCryptoBlockLUKS *luks = block->opaque; |
1923 | 0 | if (luks) { |
1924 | 0 | g_free(luks->secret); |
1925 | 0 | g_free(luks); |
1926 | 0 | } |
1927 | 0 | } |
1928 | | |
1929 | | |
1930 | | static int |
1931 | | qcrypto_block_luks_decrypt(QCryptoBlock *block, |
1932 | | uint64_t offset, |
1933 | | uint8_t *buf, |
1934 | | size_t len, |
1935 | | Error **errp) |
1936 | 0 | { |
1937 | 0 | assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); |
1938 | 0 | assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); |
1939 | 0 | return qcrypto_block_decrypt_helper(block, |
1940 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
1941 | 0 | offset, buf, len, errp); |
1942 | 0 | } |
1943 | | |
1944 | | |
1945 | | static int |
1946 | | qcrypto_block_luks_encrypt(QCryptoBlock *block, |
1947 | | uint64_t offset, |
1948 | | uint8_t *buf, |
1949 | | size_t len, |
1950 | | Error **errp) |
1951 | 0 | { |
1952 | 0 | assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); |
1953 | 0 | assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); |
1954 | 0 | return qcrypto_block_encrypt_helper(block, |
1955 | 0 | QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, |
1956 | 0 | offset, buf, len, errp); |
1957 | 0 | } |
1958 | | |
1959 | | |
1960 | | const QCryptoBlockDriver qcrypto_block_driver_luks = { |
1961 | | .open = qcrypto_block_luks_open, |
1962 | | .create = qcrypto_block_luks_create, |
1963 | | .amend = qcrypto_block_luks_amend_options, |
1964 | | .get_info = qcrypto_block_luks_get_info, |
1965 | | .cleanup = qcrypto_block_luks_cleanup, |
1966 | | .decrypt = qcrypto_block_luks_decrypt, |
1967 | | .encrypt = qcrypto_block_luks_encrypt, |
1968 | | .has_format = qcrypto_block_luks_has_format, |
1969 | | }; |