2 * LUKS - Linux Unified Key Setup v2
4 * Copyright (C) 2015-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2015-2021 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 #ifndef _CRYPTSETUP_LUKS2_ONDISK_H
23 #define _CRYPTSETUP_LUKS2_ONDISK_H
27 #include <sys/types.h>
29 #include "libcryptsetup.h"
31 #define LUKS2_MAGIC_1ST "LUKS\xba\xbe"
32 #define LUKS2_MAGIC_2ND "SKUL\xba\xbe"
33 #define LUKS2_MAGIC_L 6
34 #define LUKS2_UUID_L 40
35 #define LUKS2_LABEL_L 48
36 #define LUKS2_SALT_L 64
37 #define LUKS2_CHECKSUM_ALG_L 32
38 #define LUKS2_CHECKSUM_L 64
40 #define LUKS2_KEYSLOTS_MAX 32
41 #define LUKS2_TOKENS_MAX 32
42 #define LUKS2_SEGMENT_MAX 32
44 #define LUKS2_BUILTIN_TOKEN_PREFIX "luks2-"
45 #define LUKS2_BUILTIN_TOKEN_PREFIX_LEN 6
47 #define LUKS2_TOKEN_KEYRING LUKS2_BUILTIN_TOKEN_PREFIX "keyring"
49 #define LUKS2_DIGEST_MAX 8
51 #define CRYPT_ANY_SEGMENT -1
52 #define CRYPT_DEFAULT_SEGMENT -2
53 #define CRYPT_ONE_SEGMENT -3
55 #define CRYPT_ANY_DIGEST -1
58 #define LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH 0x1400000
61 #define LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH 0x40000000
64 struct luks2_reencrypt;
65 struct crypt_lock_handle;
66 struct crypt_dm_active_device;
67 struct luks_phdr; /* LUKS1 for conversion */
70 * LUKS2 header on-disk.
72 * Binary header is followed by JSON area.
73 * JSON area is followed by keyslot area and data area,
74 * these are described in JSON metadata.
76 * Note: uuid, csum_alg are intentionally on the same offset as LUKS1
77 * (checksum alg replaces hash in LUKS1)
79 * String (char) should be zero terminated.
80 * Padding should be wiped.
81 * Checksum is calculated with csum zeroed (+ full JSON area).
83 struct luks2_hdr_disk {
84 char magic[LUKS2_MAGIC_L];
85 uint16_t version; /* Version 2 */
86 uint64_t hdr_size; /* in bytes, including JSON area */
87 uint64_t seqid; /* increased on every update */
88 char label[LUKS2_LABEL_L];
89 char checksum_alg[LUKS2_CHECKSUM_ALG_L];
90 uint8_t salt[LUKS2_SALT_L]; /* unique for every header/offset */
91 char uuid[LUKS2_UUID_L];
92 char subsystem[LUKS2_LABEL_L]; /* owner subsystem label */
93 uint64_t hdr_offset; /* offset from device start in bytes */
95 uint8_t csum[LUKS2_CHECKSUM_L];
96 char _padding4096[7*512];
97 /* JSON area starts here */
98 } __attribute__ ((packed));
101 * LUKS2 header in-memory.
106 unsigned int version;
107 char label[LUKS2_LABEL_L];
108 char subsystem[LUKS2_LABEL_L];
109 char checksum_alg[LUKS2_CHECKSUM_ALG_L];
110 uint8_t salt1[LUKS2_SALT_L];
111 uint8_t salt2[LUKS2_SALT_L];
112 char uuid[LUKS2_UUID_L];
116 struct luks2_keyslot_params {
117 enum { LUKS2_KEYSLOT_AF_LUKS1 = 0 } af_type;
118 enum { LUKS2_KEYSLOT_AREA_RAW = 0 } area_type;
122 char hash[LUKS2_CHECKSUM_ALG_L]; // or include luks.h
123 unsigned int stripes;
129 char encryption[65]; // or include utils_crypt.h
136 * Supportable header sizes (hdr_disk + JSON area)
137 * Also used as offset for the 2nd header.
139 #define LUKS2_HDR_16K_LEN 0x4000
141 #define LUKS2_HDR_BIN_LEN sizeof(struct luks2_hdr_disk)
143 //#define LUKS2_DEFAULT_HDR_SIZE 0x400000 /* 4 MiB */
144 #define LUKS2_DEFAULT_HDR_SIZE 0x1000000 /* 16 MiB */
146 #define LUKS2_MAX_KEYSLOTS_SIZE 0x8000000 /* 128 MiB */
148 #define LUKS2_HDR_OFFSET_MAX 0x400000 /* 4 MiB */
150 /* Offsets for secondary header (for scan if primary header is corrupted). */
151 #define LUKS2_HDR2_OFFSETS { 0x04000, 0x008000, 0x010000, 0x020000, \
152 0x40000, 0x080000, 0x100000, 0x200000, LUKS2_HDR_OFFSET_MAX }
154 int LUKS2_hdr_version_unlocked(struct crypt_device *cd,
155 const char *backup_file);
157 int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr, int repair);
158 int LUKS2_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr);
159 int LUKS2_hdr_write_force(struct crypt_device *cd, struct luks2_hdr *hdr);
160 int LUKS2_hdr_dump(struct crypt_device *cd, struct luks2_hdr *hdr);
162 int LUKS2_hdr_uuid(struct crypt_device *cd,
163 struct luks2_hdr *hdr,
166 int LUKS2_hdr_labels(struct crypt_device *cd,
167 struct luks2_hdr *hdr,
169 const char *subsystem,
172 void LUKS2_hdr_free(struct crypt_device *cd, struct luks2_hdr *hdr);
174 int LUKS2_hdr_backup(struct crypt_device *cd,
175 struct luks2_hdr *hdr,
176 const char *backup_file);
177 int LUKS2_hdr_restore(struct crypt_device *cd,
178 struct luks2_hdr *hdr,
179 const char *backup_file);
181 uint64_t LUKS2_hdr_and_areas_size(struct luks2_hdr *hdr);
182 uint64_t LUKS2_keyslots_size(struct luks2_hdr *hdr);
183 uint64_t LUKS2_metadata_size(struct luks2_hdr *hdr);
185 int LUKS2_keyslot_cipher_incompatible(struct crypt_device *cd, const char *cipher_spec);
188 * Generic LUKS2 keyslot
190 int LUKS2_keyslot_open(struct crypt_device *cd,
193 const char *password,
195 struct volume_key **vk);
197 int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
200 const char *password,
202 struct volume_key **vks);
204 int LUKS2_keyslot_store(struct crypt_device *cd,
205 struct luks2_hdr *hdr,
207 const char *password,
209 const struct volume_key *vk,
210 const struct luks2_keyslot_params *params);
212 int LUKS2_keyslot_wipe(struct crypt_device *cd,
213 struct luks2_hdr *hdr,
217 crypt_keyslot_priority LUKS2_keyslot_priority_get(struct crypt_device *cd,
218 struct luks2_hdr *hdr,
221 int LUKS2_keyslot_priority_set(struct crypt_device *cd,
222 struct luks2_hdr *hdr,
224 crypt_keyslot_priority priority,
228 * Generic LUKS2 token
230 int LUKS2_token_json_get(struct crypt_device *cd,
231 struct luks2_hdr *hdr,
235 int LUKS2_token_assign(struct crypt_device *cd,
236 struct luks2_hdr *hdr,
242 int LUKS2_token_is_assigned(struct crypt_device *cd,
243 struct luks2_hdr *hdr,
247 int LUKS2_token_assignment_copy(struct crypt_device *cd,
248 struct luks2_hdr *hdr,
253 int LUKS2_token_create(struct crypt_device *cd,
254 struct luks2_hdr *hdr,
259 crypt_token_info LUKS2_token_status(struct crypt_device *cd,
260 struct luks2_hdr *hdr,
264 int LUKS2_builtin_token_get(struct crypt_device *cd,
265 struct luks2_hdr *hdr,
270 int LUKS2_builtin_token_create(struct crypt_device *cd,
271 struct luks2_hdr *hdr,
277 int LUKS2_token_open_and_activate(struct crypt_device *cd,
278 struct luks2_hdr *hdr,
284 int LUKS2_token_open_and_activate_any(struct crypt_device *cd,
285 struct luks2_hdr *hdr,
290 * Generic LUKS2 digest
292 int LUKS2_digest_any_matching(struct crypt_device *cd,
293 struct luks2_hdr *hdr,
294 const struct volume_key *vk);
296 int LUKS2_digest_verify_by_segment(struct crypt_device *cd,
297 struct luks2_hdr *hdr,
299 const struct volume_key *vk);
301 int LUKS2_digest_verify(struct crypt_device *cd,
302 struct luks2_hdr *hdr,
303 const struct volume_key *vk,
306 int LUKS2_digest_assign(struct crypt_device *cd,
307 struct luks2_hdr *hdr,
313 int LUKS2_digest_segment_assign(struct crypt_device *cd,
314 struct luks2_hdr *hdr,
320 int LUKS2_digest_by_keyslot(struct luks2_hdr *hdr, int keyslot);
322 int LUKS2_digest_by_segment(struct luks2_hdr *hdr, int segment);
324 int LUKS2_digest_create(struct crypt_device *cd,
326 struct luks2_hdr *hdr,
327 const struct volume_key *vk);
332 int LUKS2_activate(struct crypt_device *cd,
334 struct volume_key *vk,
337 int LUKS2_activate_multi(struct crypt_device *cd,
339 struct volume_key *vks,
340 uint64_t device_size,
343 int LUKS2_deactivate(struct crypt_device *cd,
345 struct luks2_hdr *hdr,
346 struct crypt_dm_active_device *dmd,
349 int LUKS2_generate_hdr(
350 struct crypt_device *cd,
351 struct luks2_hdr *hdr,
352 const struct volume_key *vk,
353 const char *cipherName,
354 const char *cipherMode,
355 const char *integrity,
357 unsigned int sector_size,
358 uint64_t data_offset,
359 uint64_t align_offset,
360 uint64_t required_alignment,
361 uint64_t metadata_size,
362 uint64_t keyslots_size);
364 int LUKS2_check_metadata_area_size(uint64_t metadata_size);
365 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size);
367 int LUKS2_wipe_header_areas(struct crypt_device *cd,
368 struct luks2_hdr *hdr, bool detached_header);
370 uint64_t LUKS2_get_data_offset(struct luks2_hdr *hdr);
371 int LUKS2_get_data_size(struct luks2_hdr *hdr, uint64_t *size, bool *dynamic);
372 int LUKS2_get_sector_size(struct luks2_hdr *hdr);
373 const char *LUKS2_get_cipher(struct luks2_hdr *hdr, int segment);
374 const char *LUKS2_get_integrity(struct luks2_hdr *hdr, int segment);
375 int LUKS2_keyslot_params_default(struct crypt_device *cd, struct luks2_hdr *hdr,
376 struct luks2_keyslot_params *params);
377 int LUKS2_get_volume_key_size(struct luks2_hdr *hdr, int segment);
378 int LUKS2_get_keyslot_stored_key_size(struct luks2_hdr *hdr, int keyslot);
379 const char *LUKS2_get_keyslot_cipher(struct luks2_hdr *hdr, int keyslot, size_t *key_size);
380 int LUKS2_keyslot_find_empty(struct luks2_hdr *hdr);
381 int LUKS2_keyslot_active_count(struct luks2_hdr *hdr, int segment);
382 crypt_keyslot_info LUKS2_keyslot_info(struct luks2_hdr *hdr, int keyslot);
383 int LUKS2_keyslot_area(struct luks2_hdr *hdr,
387 int LUKS2_keyslot_pbkdf(struct luks2_hdr *hdr, int keyslot, struct crypt_pbkdf_type *pbkdf);
390 * Permanent activation flags stored in header
392 int LUKS2_config_get_flags(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t *flags);
393 int LUKS2_config_set_flags(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t flags);
396 * Requirements for device activation or header modification
398 int LUKS2_config_get_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t *reqs);
399 int LUKS2_config_set_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs, bool commit);
401 int LUKS2_config_get_reencrypt_version(struct luks2_hdr *hdr, uint32_t *version);
403 int LUKS2_unmet_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs_mask, int quiet);
405 int LUKS2_key_description_by_segment(struct crypt_device *cd,
406 struct luks2_hdr *hdr, struct volume_key *vk, int segment);
407 int LUKS2_volume_key_load_in_keyring_by_keyslot(struct crypt_device *cd,
408 struct luks2_hdr *hdr, struct volume_key *vk, int keyslot);
409 int LUKS2_volume_key_load_in_keyring_by_digest(struct crypt_device *cd,
410 struct luks2_hdr *hdr, struct volume_key *vk, int digest);
412 int LUKS2_luks1_to_luks2(struct crypt_device *cd,
413 struct luks_phdr *hdr1,
414 struct luks2_hdr *hdr2);
415 int LUKS2_luks2_to_luks1(struct crypt_device *cd,
416 struct luks2_hdr *hdr2,
417 struct luks_phdr *hdr1);
422 int LUKS2_reencrypt_locked_recovery_by_passphrase(struct crypt_device *cd,
425 const char *passphrase,
426 size_t passphrase_size,
428 struct volume_key **vks);
430 void LUKS2_reencrypt_free(struct crypt_device *cd,
431 struct luks2_reencrypt *rh);
433 crypt_reencrypt_info LUKS2_reencrypt_status(struct luks2_hdr *hdr);
435 crypt_reencrypt_info LUKS2_reencrypt_get_params(struct luks2_hdr *hdr,
436 struct crypt_params_reencrypt *params);
438 int LUKS2_reencrypt_lock(struct crypt_device *cd,
439 struct crypt_lock_handle **reencrypt_lock);
441 int LUKS2_reencrypt_lock_by_dm_uuid(struct crypt_device *cd,
443 struct crypt_lock_handle **reencrypt_lock);
445 void LUKS2_reencrypt_unlock(struct crypt_device *cd,
446 struct crypt_lock_handle *reencrypt_lock);
448 int LUKS2_reencrypt_check_device_size(struct crypt_device *cd,
449 struct luks2_hdr *hdr,
455 int LUKS2_reencrypt_digest_verify(struct crypt_device *cd,
456 struct luks2_hdr *hdr,
457 struct volume_key *vks);