2 * LUKS - Linux Unified Key Setup v2, LUKS2 header format code
4 * Copyright (C) 2015-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2015-2020 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "luks2_internal.h"
23 #include <uuid/uuid.h>
31 static size_t get_area_size(size_t keylength)
33 //FIXME: calculate this properly, for now it is AF_split_sectors
34 return size_round_up(keylength * 4000, 4096);
37 static size_t get_min_offset(struct luks2_hdr *hdr)
39 return 2 * hdr->hdr_size;
42 static size_t get_max_offset(struct luks2_hdr *hdr)
44 return LUKS2_hdr_and_areas_size(hdr->jobj);
47 int LUKS2_find_area_max_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
48 uint64_t *area_offset, uint64_t *area_length)
50 struct area areas[LUKS2_KEYSLOTS_MAX], sorted_areas[LUKS2_KEYSLOTS_MAX+1] = {};
52 size_t valid_offset, offset, length;
54 /* fill area offset + length table */
55 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
56 if (!LUKS2_keyslot_area(hdr, i, &areas[i].offset, &areas[i].length))
63 k = 0; /* index in sorted table */
64 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
65 offset = get_max_offset(hdr) ?: UINT64_MAX;
67 /* search for the smallest offset in table */
68 for (j = 0; j < LUKS2_KEYSLOTS_MAX; j++)
69 if (areas[j].offset && areas[j].offset <= offset) {
71 offset = areas[j].offset;
75 sorted_areas[k].length = areas[area_i].length;
76 sorted_areas[k].offset = areas[area_i].offset;
77 areas[area_i].length = 0;
78 areas[area_i].offset = 0;
83 sorted_areas[LUKS2_KEYSLOTS_MAX].offset = get_max_offset(hdr);
84 sorted_areas[LUKS2_KEYSLOTS_MAX].length = 1;
86 /* search for the gap we can use */
87 length = valid_offset = 0;
88 offset = get_min_offset(hdr);
89 for (i = 0; i < LUKS2_KEYSLOTS_MAX+1; i++) {
91 if (sorted_areas[i].offset == 0 || sorted_areas[i].length == 0)
94 /* found bigger gap than the last one */
95 if ((offset < sorted_areas[i].offset) && (sorted_areas[i].offset - offset) > length) {
96 length = sorted_areas[i].offset - offset;
97 valid_offset = offset;
100 /* move beyond allocated area */
101 offset = sorted_areas[i].offset + sorted_areas[i].length;
104 /* this search 'algorithm' does not work with unaligned areas */
105 assert(length == size_round_up(length, 4096));
106 assert(valid_offset == size_round_up(valid_offset, 4096));
109 log_dbg(cd, "Not enough space in header keyslot area.");
113 log_dbg(cd, "Found largest free area %zu -> %zu", valid_offset, length + valid_offset);
115 *area_offset = valid_offset;
116 *area_length = length;
121 int LUKS2_find_area_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
122 size_t keylength, uint64_t *area_offset, uint64_t *area_length)
124 struct area areas[LUKS2_KEYSLOTS_MAX], sorted_areas[LUKS2_KEYSLOTS_MAX] = {};
126 size_t offset, length;
128 /* fill area offset + length table */
129 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
130 if (!LUKS2_keyslot_area(hdr, i, &areas[i].offset, &areas[i].length))
137 k = 0; /* index in sorted table */
138 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
139 offset = get_max_offset(hdr) ?: UINT64_MAX;
141 /* search for the smallest offset in table */
142 for (j = 0; j < LUKS2_KEYSLOTS_MAX; j++)
143 if (areas[j].offset && areas[j].offset <= offset) {
145 offset = areas[j].offset;
149 sorted_areas[k].length = areas[area_i].length;
150 sorted_areas[k].offset = areas[area_i].offset;
151 areas[area_i].length = 0;
152 areas[area_i].offset = 0;
157 /* search for the gap we can use */
158 offset = get_min_offset(hdr);
159 length = get_area_size(keylength);
160 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
162 if (sorted_areas[i].offset == 0 || sorted_areas[i].length == 0)
165 /* enough space before the used area */
166 if ((offset < sorted_areas[i].offset) && ((offset + length) <= sorted_areas[i].offset))
169 /* both offset and length are already aligned to 4096 bytes */
170 offset = sorted_areas[i].offset + sorted_areas[i].length;
173 if ((offset + length) > get_max_offset(hdr)) {
174 log_dbg(cd, "Not enough space in header keyslot area.");
178 log_dbg(cd, "Found area %zu -> %zu", offset, length + offset);
180 *area_offset = offset;
181 *area_length = length;
185 int LUKS2_check_metadata_area_size(uint64_t metadata_size)
187 /* see LUKS2_HDR2_OFFSETS */
188 return (metadata_size != 0x004000 &&
189 metadata_size != 0x008000 && metadata_size != 0x010000 &&
190 metadata_size != 0x020000 && metadata_size != 0x040000 &&
191 metadata_size != 0x080000 && metadata_size != 0x100000 &&
192 metadata_size != 0x200000 && metadata_size != 0x400000);
195 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size)
197 return (MISALIGNED_4K(keyslots_size) ||
198 keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE);
201 int LUKS2_generate_hdr(
202 struct crypt_device *cd,
203 struct luks2_hdr *hdr,
204 const struct volume_key *vk,
205 const char *cipherName,
206 const char *cipherMode,
207 const char *integrity,
209 unsigned int sector_size, /* in bytes */
210 uint64_t data_offset, /* in bytes */
211 uint64_t align_offset, /* in bytes */
212 uint64_t required_alignment,
213 uint64_t metadata_size,
214 uint64_t keyslots_size)
216 struct json_object *jobj_segment, *jobj_integrity, *jobj_keyslots, *jobj_segments, *jobj_config;
218 uuid_t partitionUuid;
223 metadata_size = LUKS2_HDR_16K_LEN;
224 hdr->hdr_size = metadata_size;
226 if (data_offset && data_offset < get_min_offset(hdr)) {
227 log_err(cd, _("Requested data offset is too small."));
231 /* Increase keyslot size according to data offset */
232 if (!keyslots_size && data_offset)
233 keyslots_size = data_offset - get_min_offset(hdr);
235 /* keyslots size has to be 4 KiB aligned */
236 keyslots_size -= (keyslots_size % 4096);
238 if (keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE)
239 keyslots_size = LUKS2_MAX_KEYSLOTS_SIZE;
241 if (!keyslots_size) {
242 assert(LUKS2_DEFAULT_HDR_SIZE > 2 * LUKS2_HDR_OFFSET_MAX);
243 keyslots_size = LUKS2_DEFAULT_HDR_SIZE - get_min_offset(hdr);
244 /* Decrease keyslots_size due to metadata device being too small */
245 if (!device_size(crypt_metadata_device(cd), &mdev_size) &&
246 ((keyslots_size + get_min_offset(hdr)) > mdev_size) &&
247 device_fallocate(crypt_metadata_device(cd), keyslots_size + get_min_offset(hdr)))
248 keyslots_size = mdev_size - get_min_offset(hdr);
251 /* Decrease keyslots_size if we have smaller data_offset */
252 if (data_offset && (keyslots_size + get_min_offset(hdr)) > data_offset) {
253 keyslots_size = data_offset - get_min_offset(hdr);
254 log_dbg(cd, "Decreasing keyslot area size to %" PRIu64
255 " bytes due to the requested data offset %"
256 PRIu64 " bytes.", keyslots_size, data_offset);
259 /* Data offset has priority */
260 if (!data_offset && required_alignment) {
261 data_offset = size_round_up(get_min_offset(hdr) + keyslots_size,
262 (size_t)required_alignment);
263 data_offset += align_offset;
266 log_dbg(cd, "Formatting LUKS2 with JSON metadata area %" PRIu64
267 " bytes and keyslots area %" PRIu64 " bytes.",
268 metadata_size - LUKS2_HDR_BIN_LEN, keyslots_size);
270 if (keyslots_size < (LUKS2_HDR_OFFSET_MAX - 2*LUKS2_HDR_16K_LEN))
271 log_std(cd, _("WARNING: keyslots area (%" PRIu64 " bytes) is very small,"
272 " available LUKS2 keyslot count is very limited.\n"),
277 memset(hdr->label, 0, LUKS2_LABEL_L);
278 strcpy(hdr->checksum_alg, "sha256");
279 crypt_random_get(cd, (char*)hdr->salt1, LUKS2_SALT_L, CRYPT_RND_SALT);
280 crypt_random_get(cd, (char*)hdr->salt2, LUKS2_SALT_L, CRYPT_RND_SALT);
282 if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
283 log_err(cd, _("Wrong LUKS UUID format provided."));
287 uuid_generate(partitionUuid);
289 uuid_unparse(partitionUuid, hdr->uuid);
291 if (*cipherMode != '\0')
292 snprintf(cipher, sizeof(cipher), "%s-%s", cipherName, cipherMode);
294 snprintf(cipher, sizeof(cipher), "%s", cipherName);
296 hdr->jobj = json_object_new_object();
298 jobj_keyslots = json_object_new_object();
299 json_object_object_add(hdr->jobj, "keyslots", jobj_keyslots);
300 json_object_object_add(hdr->jobj, "tokens", json_object_new_object());
301 jobj_segments = json_object_new_object();
302 json_object_object_add(hdr->jobj, "segments", jobj_segments);
303 json_object_object_add(hdr->jobj, "digests", json_object_new_object());
304 jobj_config = json_object_new_object();
305 json_object_object_add(hdr->jobj, "config", jobj_config);
307 digest = LUKS2_digest_create(cd, "pbkdf2", hdr, vk);
311 if (LUKS2_digest_segment_assign(cd, hdr, 0, digest, 1, 0) < 0)
314 jobj_segment = json_segment_create_crypt(data_offset, 0, NULL, cipher, sector_size, 0);
319 jobj_integrity = json_object_new_object();
320 json_object_object_add(jobj_integrity, "type", json_object_new_string(integrity));
321 json_object_object_add(jobj_integrity, "journal_encryption", json_object_new_string("none"));
322 json_object_object_add(jobj_integrity, "journal_integrity", json_object_new_string("none"));
323 json_object_object_add(jobj_segment, "integrity", jobj_integrity);
326 json_object_object_add_by_uint(jobj_segments, 0, jobj_segment);
328 json_object_object_add(jobj_config, "json_size", crypt_jobj_new_uint64(metadata_size - LUKS2_HDR_BIN_LEN));
329 json_object_object_add(jobj_config, "keyslots_size", crypt_jobj_new_uint64(keyslots_size));
331 JSON_DBG(cd, hdr->jobj, "Header JSON:");
334 json_object_put(hdr->jobj);
339 int LUKS2_wipe_header_areas(struct crypt_device *cd,
340 struct luks2_hdr *hdr)
343 uint64_t offset, length;
346 /* Wipe complete header, keyslots and padding areas with zeroes. */
348 length = LUKS2_get_data_offset(hdr) * SECTOR_SIZE;
349 wipe_block = 1024 * 1024;
351 if (LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN))
354 /* On detached header wipe at least the first 4k */
360 log_dbg(cd, "Wiping LUKS areas (0x%06" PRIx64 " - 0x%06" PRIx64") with zeroes.",
361 offset, length + offset);
363 r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO,
364 offset, length, wipe_block, NULL, NULL);
368 /* Wipe keyslot area */
369 wipe_block = 1024 * 1024;
370 offset = get_min_offset(hdr);
371 length = LUKS2_keyslots_size(hdr->jobj);
373 log_dbg(cd, "Wiping keyslots area (0x%06" PRIx64 " - 0x%06" PRIx64") with random data.",
374 offset, length + offset);
376 return crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_RANDOM,
377 offset, length, wipe_block, NULL, NULL);
380 /* FIXME: what if user wanted to keep original keyslots size? */
381 int LUKS2_set_keyslots_size(struct crypt_device *cd,
382 struct luks2_hdr *hdr,
383 uint64_t data_offset)
385 json_object *jobj_config;
386 uint64_t keyslots_size;
388 if (data_offset < get_min_offset(hdr))
391 keyslots_size = data_offset - get_min_offset(hdr);
393 /* keep keyslots_size reasonable for custom data alignments */
394 if (keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE)
395 keyslots_size = LUKS2_MAX_KEYSLOTS_SIZE;
397 /* keyslots size has to be 4 KiB aligned */
398 keyslots_size -= (keyslots_size % 4096);
400 if (!json_object_object_get_ex(hdr->jobj, "config", &jobj_config))
403 json_object_object_add(jobj_config, "keyslots_size", crypt_jobj_new_uint64(keyslots_size));