2 * LUKS - Linux Unified Key Setup v2
4 * Copyright (C) 2015-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2015-2023 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_NAME_MAX 64
49 #define LUKS2_TOKEN_KEYRING LUKS2_BUILTIN_TOKEN_PREFIX "keyring"
51 #define LUKS2_DIGEST_MAX 8
53 #define CRYPT_ANY_SEGMENT -1
54 #define CRYPT_DEFAULT_SEGMENT -2
55 #define CRYPT_ONE_SEGMENT -3
57 #define CRYPT_ANY_DIGEST -1
60 #define LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH 0x1400000
63 #define LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH 0x40000000
65 /* supported reencryption requirement versions */
66 #define LUKS2_REENCRYPT_REQ_VERSION UINT8_C(2)
67 #define LUKS2_DECRYPT_DATASHIFT_REQ_VERSION UINT8_C(3)
69 /* see reencrypt_assembly_verification_data() in luks2_reencrypt_digest.c */
70 /* LUKS2_REENCRYPT_MAX_VERSION UINT8_C(207) */
73 struct luks2_reencrypt;
74 struct reenc_protection;
75 struct crypt_lock_handle;
76 struct crypt_dm_active_device;
77 struct luks_phdr; /* LUKS1 for conversion */
80 * LUKS2 header on-disk.
82 * Binary header is followed by JSON area.
83 * JSON area is followed by keyslot area and data area,
84 * these are described in JSON metadata.
86 * Note: uuid, csum_alg are intentionally on the same offset as LUKS1
87 * (checksum alg replaces hash in LUKS1)
89 * String (char) should be zero terminated.
90 * Padding should be wiped.
91 * Checksum is calculated with csum zeroed (+ full JSON area).
93 struct luks2_hdr_disk {
94 char magic[LUKS2_MAGIC_L];
95 uint16_t version; /* Version 2 */
96 uint64_t hdr_size; /* in bytes, including JSON area */
97 uint64_t seqid; /* increased on every update */
98 char label[LUKS2_LABEL_L];
99 char checksum_alg[LUKS2_CHECKSUM_ALG_L];
100 uint8_t salt[LUKS2_SALT_L]; /* unique for every header/offset */
101 char uuid[LUKS2_UUID_L];
102 char subsystem[LUKS2_LABEL_L]; /* owner subsystem label */
103 uint64_t hdr_offset; /* offset from device start in bytes */
105 uint8_t csum[LUKS2_CHECKSUM_L];
106 char _padding4096[7*512];
107 /* JSON area starts here */
108 } __attribute__ ((packed));
111 * LUKS2 header in-memory.
116 unsigned int version;
117 char label[LUKS2_LABEL_L];
118 char subsystem[LUKS2_LABEL_L];
119 char checksum_alg[LUKS2_CHECKSUM_ALG_L];
120 uint8_t salt1[LUKS2_SALT_L];
121 uint8_t salt2[LUKS2_SALT_L];
122 char uuid[LUKS2_UUID_L];
127 struct luks2_keyslot_params {
128 enum { LUKS2_KEYSLOT_AF_LUKS1 = 0 } af_type;
129 enum { LUKS2_KEYSLOT_AREA_RAW = 0 } area_type;
133 char hash[LUKS2_CHECKSUM_ALG_L]; // or include luks.h
134 unsigned int stripes;
140 char encryption[65]; // or include utils_crypt.h
147 * Supportable header sizes (hdr_disk + JSON area)
148 * Also used as offset for the 2nd header.
150 #define LUKS2_HDR_16K_LEN 0x4000
152 #define LUKS2_HDR_BIN_LEN sizeof(struct luks2_hdr_disk)
154 //#define LUKS2_DEFAULT_HDR_SIZE 0x400000 /* 4 MiB */
155 #define LUKS2_DEFAULT_HDR_SIZE 0x1000000 /* 16 MiB */
157 #define LUKS2_MAX_KEYSLOTS_SIZE 0x8000000 /* 128 MiB */
159 #define LUKS2_HDR_OFFSET_MAX 0x400000 /* 4 MiB */
161 /* Offsets for secondary header (for scan if primary header is corrupted). */
162 #define LUKS2_HDR2_OFFSETS { 0x04000, 0x008000, 0x010000, 0x020000, \
163 0x40000, 0x080000, 0x100000, 0x200000, LUKS2_HDR_OFFSET_MAX }
165 int LUKS2_hdr_version_unlocked(struct crypt_device *cd,
166 const char *backup_file);
168 int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr, int repair);
169 int LUKS2_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr);
170 int LUKS2_hdr_write_force(struct crypt_device *cd, struct luks2_hdr *hdr);
171 int LUKS2_hdr_rollback(struct crypt_device *cd, struct luks2_hdr *hdr);
172 int LUKS2_hdr_dump(struct crypt_device *cd, struct luks2_hdr *hdr);
173 int LUKS2_hdr_dump_json(struct crypt_device *cd, struct luks2_hdr *hdr, const char **json);
175 int LUKS2_hdr_uuid(struct crypt_device *cd,
176 struct luks2_hdr *hdr,
179 int LUKS2_hdr_labels(struct crypt_device *cd,
180 struct luks2_hdr *hdr,
182 const char *subsystem,
185 void LUKS2_hdr_free(struct crypt_device *cd, struct luks2_hdr *hdr);
187 int LUKS2_hdr_backup(struct crypt_device *cd,
188 struct luks2_hdr *hdr,
189 const char *backup_file);
190 int LUKS2_hdr_restore(struct crypt_device *cd,
191 struct luks2_hdr *hdr,
192 const char *backup_file);
194 uint64_t LUKS2_hdr_and_areas_size(struct luks2_hdr *hdr);
195 uint64_t LUKS2_keyslots_size(struct luks2_hdr *hdr);
196 uint64_t LUKS2_metadata_size(struct luks2_hdr *hdr);
198 int LUKS2_keyslot_cipher_incompatible(struct crypt_device *cd, const char *cipher_spec);
201 * Generic LUKS2 keyslot
203 int LUKS2_keyslot_open(struct crypt_device *cd,
206 const char *password,
208 struct volume_key **vk);
210 int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
213 const char *password,
215 struct volume_key **vks);
217 int LUKS2_keyslot_store(struct crypt_device *cd,
218 struct luks2_hdr *hdr,
220 const char *password,
222 const struct volume_key *vk,
223 const struct luks2_keyslot_params *params);
225 int LUKS2_keyslot_wipe(struct crypt_device *cd,
226 struct luks2_hdr *hdr,
230 crypt_keyslot_priority LUKS2_keyslot_priority_get(struct luks2_hdr *hdr, int keyslot);
232 int LUKS2_keyslot_priority_set(struct crypt_device *cd,
233 struct luks2_hdr *hdr,
235 crypt_keyslot_priority priority,
238 int LUKS2_keyslot_swap(struct crypt_device *cd,
239 struct luks2_hdr *hdr,
244 * Generic LUKS2 token
246 int LUKS2_token_json_get(struct luks2_hdr *hdr,
250 int LUKS2_token_assign(struct crypt_device *cd,
251 struct luks2_hdr *hdr,
257 int LUKS2_token_is_assigned(struct luks2_hdr *hdr,
261 int LUKS2_token_assignment_copy(struct crypt_device *cd,
262 struct luks2_hdr *hdr,
267 int LUKS2_token_create(struct crypt_device *cd,
268 struct luks2_hdr *hdr,
273 crypt_token_info LUKS2_token_status(struct crypt_device *cd,
274 struct luks2_hdr *hdr,
278 int LUKS2_token_open_and_activate(struct crypt_device *cd,
279 struct luks2_hdr *hdr,
288 int LUKS2_token_unlock_key(struct crypt_device *cd,
289 struct luks2_hdr *hdr,
296 struct volume_key **vk);
298 int LUKS2_token_keyring_get(struct luks2_hdr *hdr,
300 struct crypt_token_params_luks2_keyring *keyring_params);
302 int LUKS2_token_keyring_json(char *buffer, size_t buffer_size,
303 const struct crypt_token_params_luks2_keyring *keyring_params);
305 int LUKS2_token_unlock_passphrase(struct crypt_device *cd,
306 struct luks2_hdr *hdr,
313 size_t *passphrase_size);
315 void crypt_token_unload_external_all(struct crypt_device *cd);
318 * Generic LUKS2 digest
320 int LUKS2_digest_any_matching(struct crypt_device *cd,
321 struct luks2_hdr *hdr,
322 const struct volume_key *vk);
324 int LUKS2_digest_verify_by_segment(struct crypt_device *cd,
325 struct luks2_hdr *hdr,
327 const struct volume_key *vk);
329 int LUKS2_digest_verify(struct crypt_device *cd,
330 struct luks2_hdr *hdr,
331 const struct volume_key *vk,
334 int LUKS2_digest_assign(struct crypt_device *cd,
335 struct luks2_hdr *hdr,
341 int LUKS2_digest_segment_assign(struct crypt_device *cd,
342 struct luks2_hdr *hdr,
348 int LUKS2_digest_by_keyslot(struct luks2_hdr *hdr, int keyslot);
350 int LUKS2_digest_by_segment(struct luks2_hdr *hdr, int segment);
352 int LUKS2_digest_create(struct crypt_device *cd,
354 struct luks2_hdr *hdr,
355 const struct volume_key *vk);
360 int LUKS2_activate(struct crypt_device *cd,
362 struct volume_key *vk,
365 int LUKS2_activate_multi(struct crypt_device *cd,
367 struct volume_key *vks,
368 uint64_t device_size,
371 int LUKS2_deactivate(struct crypt_device *cd,
373 struct luks2_hdr *hdr,
374 struct crypt_dm_active_device *dmd,
377 int LUKS2_generate_hdr(
378 struct crypt_device *cd,
379 struct luks2_hdr *hdr,
380 const struct volume_key *vk,
381 const char *cipherName,
382 const char *cipherMode,
383 const char *integrity,
385 unsigned int sector_size,
386 uint64_t data_offset,
387 uint64_t align_offset,
388 uint64_t required_alignment,
389 uint64_t metadata_size,
390 uint64_t keyslots_size);
392 int LUKS2_check_metadata_area_size(uint64_t metadata_size);
393 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size);
395 int LUKS2_wipe_header_areas(struct crypt_device *cd,
396 struct luks2_hdr *hdr, bool detached_header);
398 uint64_t LUKS2_get_data_offset(struct luks2_hdr *hdr);
399 int LUKS2_get_data_size(struct luks2_hdr *hdr, uint64_t *size, bool *dynamic);
400 uint32_t LUKS2_get_sector_size(struct luks2_hdr *hdr);
401 const char *LUKS2_get_cipher(struct luks2_hdr *hdr, int segment);
402 const char *LUKS2_get_integrity(struct luks2_hdr *hdr, int segment);
403 int LUKS2_keyslot_params_default(struct crypt_device *cd, struct luks2_hdr *hdr,
404 struct luks2_keyslot_params *params);
405 int LUKS2_get_volume_key_size(struct luks2_hdr *hdr, int segment);
406 int LUKS2_get_keyslot_stored_key_size(struct luks2_hdr *hdr, int keyslot);
407 const char *LUKS2_get_keyslot_cipher(struct luks2_hdr *hdr, int keyslot, size_t *key_size);
408 int LUKS2_keyslot_find_empty(struct crypt_device *cd, struct luks2_hdr *hdr, size_t keylength);
409 int LUKS2_keyslot_active_count(struct luks2_hdr *hdr, int segment);
410 crypt_keyslot_info LUKS2_keyslot_info(struct luks2_hdr *hdr, int keyslot);
411 int LUKS2_keyslot_area(struct luks2_hdr *hdr,
415 int LUKS2_keyslot_pbkdf(struct luks2_hdr *hdr, int keyslot, struct crypt_pbkdf_type *pbkdf);
418 * Permanent activation flags stored in header
420 int LUKS2_config_get_flags(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t *flags);
421 int LUKS2_config_set_flags(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t flags);
424 * Requirements for device activation or header modification
426 int LUKS2_config_get_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t *reqs);
427 int LUKS2_config_set_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs, bool commit);
428 int LUKS2_config_set_requirement_version(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t req_id, uint8_t req_version, bool commit);
430 int LUKS2_config_get_reencrypt_version(struct luks2_hdr *hdr, uint8_t *version);
432 bool LUKS2_reencrypt_requirement_candidate(struct luks2_hdr *hdr);
434 int LUKS2_unmet_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs_mask, int quiet);
436 int LUKS2_key_description_by_segment(struct crypt_device *cd,
437 struct luks2_hdr *hdr, struct volume_key *vk, int segment);
438 int LUKS2_volume_key_load_in_keyring_by_keyslot(struct crypt_device *cd,
439 struct luks2_hdr *hdr, struct volume_key *vk, int keyslot);
440 int LUKS2_volume_key_load_in_keyring_by_digest(struct crypt_device *cd,
441 struct volume_key *vk, int digest);
443 int LUKS2_luks1_to_luks2(struct crypt_device *cd,
444 struct luks_phdr *hdr1,
445 struct luks2_hdr *hdr2);
446 int LUKS2_luks2_to_luks1(struct crypt_device *cd,
447 struct luks2_hdr *hdr2,
448 struct luks_phdr *hdr1);
453 int LUKS2_reencrypt_locked_recovery_by_passphrase(struct crypt_device *cd,
456 const char *passphrase,
457 size_t passphrase_size,
458 struct volume_key **vks);
460 void LUKS2_reencrypt_free(struct crypt_device *cd,
461 struct luks2_reencrypt *rh);
463 crypt_reencrypt_info LUKS2_reencrypt_status(struct luks2_hdr *hdr);
465 crypt_reencrypt_info LUKS2_reencrypt_get_params(struct luks2_hdr *hdr,
466 struct crypt_params_reencrypt *params);
468 int LUKS2_reencrypt_lock(struct crypt_device *cd,
469 struct crypt_lock_handle **reencrypt_lock);
471 int LUKS2_reencrypt_lock_by_dm_uuid(struct crypt_device *cd,
473 struct crypt_lock_handle **reencrypt_lock);
475 void LUKS2_reencrypt_unlock(struct crypt_device *cd,
476 struct crypt_lock_handle *reencrypt_lock);
478 int LUKS2_reencrypt_check_device_size(struct crypt_device *cd,
479 struct luks2_hdr *hdr,
485 int LUKS2_reencrypt_digest_verify(struct crypt_device *cd,
486 struct luks2_hdr *hdr,
487 struct volume_key *vks);
489 int LUKS2_reencrypt_max_hotzone_size(struct crypt_device *cd,
490 struct luks2_hdr *hdr,
491 const struct reenc_protection *rp,
492 int reencrypt_keyslot,
495 void LUKS2_reencrypt_protection_erase(struct reenc_protection *rp);