2 * LUKS - Linux Unified Key Setup v2
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 #ifndef _CRYPTSETUP_LUKS2_ONDISK_H
23 #define _CRYPTSETUP_LUKS2_ONDISK_H
27 #include "libcryptsetup.h"
29 #define LUKS2_MAGIC_1ST "LUKS\xba\xbe"
30 #define LUKS2_MAGIC_2ND "SKUL\xba\xbe"
31 #define LUKS2_MAGIC_L 6
32 #define LUKS2_UUID_L 40
33 #define LUKS2_LABEL_L 48
34 #define LUKS2_SALT_L 64
35 #define LUKS2_CHECKSUM_ALG_L 32
36 #define LUKS2_CHECKSUM_L 64
38 #define LUKS2_KEYSLOTS_MAX 32
39 #define LUKS2_TOKENS_MAX 32
40 #define LUKS2_SEGMENT_MAX 32
42 #define LUKS2_BUILTIN_TOKEN_PREFIX "luks2-"
43 #define LUKS2_BUILTIN_TOKEN_PREFIX_LEN 6
45 #define LUKS2_TOKEN_KEYRING LUKS2_BUILTIN_TOKEN_PREFIX "keyring"
47 #define LUKS2_DIGEST_MAX 8
49 #define CRYPT_ANY_SEGMENT -1
50 #define CRYPT_DEFAULT_SEGMENT -2
51 #define CRYPT_ONE_SEGMENT -3
53 #define CRYPT_ANY_DIGEST -1
56 #define LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH 0x1400000
59 #define LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH 0x40000000
64 * LUKS2 header on-disk.
66 * Binary header is followed by JSON area.
67 * JSON area is followed by keyslot area and data area,
68 * these are described in JSON metadata.
70 * Note: uuid, csum_alg are intentionally on the same offset as LUKS1
71 * (checksum alg replaces hash in LUKS1)
73 * String (char) should be zero terminated.
74 * Padding should be wiped.
75 * Checksum is calculated with csum zeroed (+ full JSON area).
77 struct luks2_hdr_disk {
78 char magic[LUKS2_MAGIC_L];
79 uint16_t version; /* Version 2 */
80 uint64_t hdr_size; /* in bytes, including JSON area */
81 uint64_t seqid; /* increased on every update */
82 char label[LUKS2_LABEL_L];
83 char checksum_alg[LUKS2_CHECKSUM_ALG_L];
84 uint8_t salt[LUKS2_SALT_L]; /* unique for every header/offset */
85 char uuid[LUKS2_UUID_L];
86 char subsystem[LUKS2_LABEL_L]; /* owner subsystem label */
87 uint64_t hdr_offset; /* offset from device start in bytes */
89 uint8_t csum[LUKS2_CHECKSUM_L];
90 char _padding4096[7*512];
91 /* JSON area starts here */
92 } __attribute__ ((packed));
95 * LUKS2 header in-memory.
97 typedef struct json_object json_object;
101 unsigned int version;
102 char label[LUKS2_LABEL_L];
103 char subsystem[LUKS2_LABEL_L];
104 char checksum_alg[LUKS2_CHECKSUM_ALG_L];
105 uint8_t salt1[LUKS2_SALT_L];
106 uint8_t salt2[LUKS2_SALT_L];
107 char uuid[LUKS2_UUID_L];
111 struct luks2_keyslot_params {
112 enum { LUKS2_KEYSLOT_AF_LUKS1 = 0 } af_type;
113 enum { LUKS2_KEYSLOT_AREA_RAW = 0 } area_type;
117 char hash[LUKS2_CHECKSUM_ALG_L]; // or include luks.h
118 unsigned int stripes;
124 char encryption[65]; // or include utils_crypt.h
130 struct reenc_protection {
131 enum { REENC_PROTECTION_NONE = 0, /* none should be 0 always */
132 REENC_PROTECTION_CHECKSUM,
133 REENC_PROTECTION_JOURNAL,
134 REENC_PROTECTION_DATASHIFT } type;
140 char hash[LUKS2_CHECKSUM_ALG_L]; // or include luks.h
141 struct crypt_hash *ch;
143 /* buffer for checksums */
145 size_t checksums_len;
152 struct luks2_reenc_context {
153 /* reencryption window attributes */
159 uint64_t device_size;
162 crypt_reencrypt_direction_info direction;
163 crypt_reencrypt_mode_info mode;
170 /* reencryption window persistence attributes */
171 struct reenc_protection rp;
175 /* already running reencryption */
176 json_object *jobj_segs_hot;
177 json_object *jobj_segs_post;
179 /* backup segments */
180 json_object *jobj_segment_new;
182 json_object *jobj_segment_old;
184 json_object *jobj_segment_moved;
186 struct volume_key *vks;
191 struct crypt_storage_wrapper *cw1;
192 struct crypt_storage_wrapper *cw2;
197 struct crypt_lock_handle *reenc_lock;
200 crypt_reencrypt_info LUKS2_reenc_status(struct luks2_hdr *hdr);
202 * Supportable header sizes (hdr_disk + JSON area)
203 * Also used as offset for the 2nd header.
205 #define LUKS2_HDR_16K_LEN 0x4000
207 #define LUKS2_HDR_BIN_LEN sizeof(struct luks2_hdr_disk)
209 //#define LUKS2_DEFAULT_HDR_SIZE 0x400000 /* 4 MiB */
210 #define LUKS2_DEFAULT_HDR_SIZE 0x1000000 /* 16 MiB */
212 #define LUKS2_MAX_KEYSLOTS_SIZE 0x8000000 /* 128 MiB */
214 #define LUKS2_HDR_OFFSET_MAX 0x400000 /* 4 MiB */
216 /* Offsets for secondary header (for scan if primary header is corrupted). */
217 #define LUKS2_HDR2_OFFSETS { 0x04000, 0x008000, 0x010000, 0x020000, \
218 0x40000, 0x080000, 0x100000, 0x200000, LUKS2_HDR_OFFSET_MAX }
220 int LUKS2_hdr_version_unlocked(struct crypt_device *cd,
221 const char *backup_file);
223 int LUKS2_device_write_lock(struct crypt_device *cd,
224 struct luks2_hdr *hdr, struct device *device);
226 int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr, int repair);
227 int LUKS2_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr);
228 int LUKS2_hdr_write_force(struct crypt_device *cd, struct luks2_hdr *hdr);
229 int LUKS2_hdr_dump(struct crypt_device *cd, struct luks2_hdr *hdr);
231 int LUKS2_hdr_uuid(struct crypt_device *cd,
232 struct luks2_hdr *hdr,
235 int LUKS2_hdr_labels(struct crypt_device *cd,
236 struct luks2_hdr *hdr,
238 const char *subsystem,
241 void LUKS2_hdr_free(struct crypt_device *cd, struct luks2_hdr *hdr);
243 int LUKS2_hdr_backup(struct crypt_device *cd,
244 struct luks2_hdr *hdr,
245 const char *backup_file);
246 int LUKS2_hdr_restore(struct crypt_device *cd,
247 struct luks2_hdr *hdr,
248 const char *backup_file);
250 uint64_t LUKS2_hdr_and_areas_size(json_object *jobj);
251 uint64_t LUKS2_keyslots_size(json_object *jobj);
252 uint64_t LUKS2_metadata_size(json_object *jobj);
254 int LUKS2_keyslot_cipher_incompatible(struct crypt_device *cd, const char *cipher_spec);
257 * Generic LUKS2 keyslot
259 int LUKS2_keyslot_open(struct crypt_device *cd,
262 const char *password,
264 struct volume_key **vk);
266 int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
269 const char *password,
271 struct volume_key **vks);
273 int LUKS2_keyslot_store(struct crypt_device *cd,
274 struct luks2_hdr *hdr,
276 const char *password,
278 const struct volume_key *vk,
279 const struct luks2_keyslot_params *params);
281 int LUKS2_keyslot_reencrypt_store(struct crypt_device *cd,
282 struct luks2_hdr *hdr,
285 size_t buffer_length);
287 int LUKS2_keyslot_reencrypt_create(struct crypt_device *cd,
288 struct luks2_hdr *hdr,
290 const struct crypt_params_reencrypt *params);
292 int reenc_keyslot_update(struct crypt_device *cd,
293 const struct luks2_reenc_context *rh);
295 int LUKS2_keyslot_wipe(struct crypt_device *cd,
296 struct luks2_hdr *hdr,
300 int LUKS2_keyslot_dump(struct crypt_device *cd,
303 crypt_keyslot_priority LUKS2_keyslot_priority_get(struct crypt_device *cd,
304 struct luks2_hdr *hdr,
307 int LUKS2_keyslot_priority_set(struct crypt_device *cd,
308 struct luks2_hdr *hdr,
310 crypt_keyslot_priority priority,
314 * Generic LUKS2 token
316 int LUKS2_token_json_get(struct crypt_device *cd,
317 struct luks2_hdr *hdr,
321 int LUKS2_token_assign(struct crypt_device *cd,
322 struct luks2_hdr *hdr,
328 int LUKS2_token_is_assigned(struct crypt_device *cd,
329 struct luks2_hdr *hdr,
333 int LUKS2_token_create(struct crypt_device *cd,
334 struct luks2_hdr *hdr,
339 crypt_token_info LUKS2_token_status(struct crypt_device *cd,
340 struct luks2_hdr *hdr,
344 int LUKS2_builtin_token_get(struct crypt_device *cd,
345 struct luks2_hdr *hdr,
350 int LUKS2_builtin_token_create(struct crypt_device *cd,
351 struct luks2_hdr *hdr,
357 int LUKS2_token_open_and_activate(struct crypt_device *cd,
358 struct luks2_hdr *hdr,
364 int LUKS2_token_open_and_activate_any(struct crypt_device *cd,
365 struct luks2_hdr *hdr,
369 int LUKS2_tokens_count(struct luks2_hdr *hdr);
372 * Generic LUKS2 segment
374 uint64_t json_segment_get_offset(json_object *jobj_segment, unsigned blockwise);
375 const char *json_segment_type(json_object *jobj_segment);
376 uint64_t json_segment_get_iv_offset(json_object *jobj_segment);
377 uint64_t json_segment_get_size(json_object *jobj_segment, unsigned blockwise);
378 const char *json_segment_get_cipher(json_object *jobj_segment);
379 int json_segment_get_sector_size(json_object *jobj_segment);
380 bool json_segment_is_backup(json_object *jobj_segment);
381 json_object *json_segments_get_segment(json_object *jobj_segments, int segment);
382 unsigned json_segments_count(json_object *jobj_segments);
383 void json_segment_remove_flag(json_object *jobj_segment, const char *flag);
384 uint64_t json_segments_get_minimal_offset(json_object *jobj_segments, unsigned blockwise);
385 json_object *json_segment_create_linear(uint64_t offset, const uint64_t *length, unsigned reencryption);
386 json_object *json_segment_create_crypt(uint64_t offset, uint64_t iv_offset, const uint64_t *length, const char *cipher, uint32_t sector_size, unsigned reencryption);
387 int json_segments_segment_in_reencrypt(json_object *jobj_segments);
389 int LUKS2_segments_count(struct luks2_hdr *hdr);
391 int LUKS2_segment_first_unused_id(struct luks2_hdr *hdr);
393 int LUKS2_segment_set_flag(json_object *jobj_segment, const char *flag);
395 json_object *LUKS2_get_segment_by_flag(struct luks2_hdr *hdr, const char *flag);
397 int LUKS2_get_segment_id_by_flag(struct luks2_hdr *hdr, const char *flag);
399 int LUKS2_segments_set(struct crypt_device *cd,
400 struct luks2_hdr *hdr,
401 json_object *jobj_segments,
404 uint64_t LUKS2_segment_offset(struct luks2_hdr *hdr,
408 uint64_t LUKS2_segment_size(struct luks2_hdr *hdr,
412 int LUKS2_segment_is_type(struct luks2_hdr *hdr,
416 int LUKS2_segment_by_type(struct luks2_hdr *hdr,
419 int LUKS2_last_segment_by_type(struct luks2_hdr *hdr,
422 int LUKS2_get_default_segment(struct luks2_hdr *hdr);
424 int LUKS2_reencrypt_digest_new(struct luks2_hdr *hdr);
425 int LUKS2_reencrypt_digest_old(struct luks2_hdr *hdr);
426 int LUKS2_reencrypt_data_offset(struct luks2_hdr *hdr, bool blockwise);
429 * Generic LUKS2 digest
431 int LUKS2_digest_any_matching(struct crypt_device *cd,
432 struct luks2_hdr *hdr,
433 const struct volume_key *vk);
435 int LUKS2_digest_by_segment(struct luks2_hdr *hdr, int segment);
437 int LUKS2_digest_verify_by_digest(struct crypt_device *cd,
438 struct luks2_hdr *hdr,
440 const struct volume_key *vk);
442 int LUKS2_digest_verify_by_segment(struct crypt_device *cd,
443 struct luks2_hdr *hdr,
445 const struct volume_key *vk);
447 void LUKS2_digests_erase_unused(struct crypt_device *cd,
448 struct luks2_hdr *hdr);
450 int LUKS2_digest_verify(struct crypt_device *cd,
451 struct luks2_hdr *hdr,
452 const struct volume_key *vk,
455 int LUKS2_digest_dump(struct crypt_device *cd,
458 int LUKS2_digest_assign(struct crypt_device *cd,
459 struct luks2_hdr *hdr,
465 int LUKS2_digest_segment_assign(struct crypt_device *cd,
466 struct luks2_hdr *hdr,
472 int LUKS2_digest_by_keyslot(struct luks2_hdr *hdr, int keyslot);
474 int LUKS2_digest_create(struct crypt_device *cd,
476 struct luks2_hdr *hdr,
477 const struct volume_key *vk);
482 int LUKS2_activate(struct crypt_device *cd,
484 struct volume_key *vk,
487 int LUKS2_activate_multi(struct crypt_device *cd,
489 struct volume_key *vks,
490 uint64_t device_size,
493 struct crypt_dm_active_device;
495 int LUKS2_deactivate(struct crypt_device *cd,
497 struct luks2_hdr *hdr,
498 struct crypt_dm_active_device *dmd,
501 int LUKS2_reload(struct crypt_device *cd,
503 struct volume_key *vks,
504 uint64_t device_size,
507 int LUKS2_generate_hdr(
508 struct crypt_device *cd,
509 struct luks2_hdr *hdr,
510 const struct volume_key *vk,
511 const char *cipherName,
512 const char *cipherMode,
513 const char *integrity,
515 unsigned int sector_size,
516 uint64_t data_offset,
517 uint64_t align_offset,
518 uint64_t required_alignment,
519 uint64_t metadata_size,
520 uint64_t keyslots_size);
522 int LUKS2_check_metadata_area_size(uint64_t metadata_size);
523 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size);
525 int LUKS2_wipe_header_areas(struct crypt_device *cd,
526 struct luks2_hdr *hdr);
528 uint64_t LUKS2_get_data_offset(struct luks2_hdr *hdr);
529 int LUKS2_get_data_size(struct luks2_hdr *hdr, uint64_t *size, bool *dynamic);
530 int LUKS2_get_sector_size(struct luks2_hdr *hdr);
531 const char *LUKS2_get_cipher(struct luks2_hdr *hdr, int segment);
532 const char *LUKS2_get_integrity(struct luks2_hdr *hdr, int segment);
533 int LUKS2_keyslot_params_default(struct crypt_device *cd, struct luks2_hdr *hdr,
534 struct luks2_keyslot_params *params);
535 int LUKS2_get_volume_key_size(struct luks2_hdr *hdr, int segment);
536 int LUKS2_get_keyslot_stored_key_size(struct luks2_hdr *hdr, int keyslot);
537 const char *LUKS2_get_keyslot_cipher(struct luks2_hdr *hdr, int keyslot, size_t *key_size);
538 int LUKS2_keyslot_find_empty(struct luks2_hdr *hdr);
539 int LUKS2_keyslot_active_count(struct luks2_hdr *hdr, int segment);
540 int LUKS2_keyslot_for_segment(struct luks2_hdr *hdr, int keyslot, int segment);
541 int LUKS2_find_keyslot(struct luks2_hdr *hdr, const char *type);
542 crypt_keyslot_info LUKS2_keyslot_info(struct luks2_hdr *hdr, int keyslot);
543 int LUKS2_keyslot_area(struct luks2_hdr *hdr,
547 int LUKS2_keyslot_pbkdf(struct luks2_hdr *hdr, int keyslot, struct crypt_pbkdf_type *pbkdf);
548 int LUKS2_set_keyslots_size(struct crypt_device *cd,
549 struct luks2_hdr *hdr,
550 uint64_t data_offset);
553 * Permanent activation flags stored in header
555 int LUKS2_config_get_flags(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t *flags);
556 int LUKS2_config_set_flags(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t flags);
559 * Requirements for device activation or header modification
561 int LUKS2_config_get_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t *reqs);
562 int LUKS2_config_set_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs, bool commit);
564 int LUKS2_unmet_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs_mask, int quiet);
566 int LUKS2_key_description_by_segment(struct crypt_device *cd,
567 struct luks2_hdr *hdr, struct volume_key *vk, int segment);
568 int LUKS2_volume_key_load_in_keyring_by_keyslot(struct crypt_device *cd,
569 struct luks2_hdr *hdr, struct volume_key *vk, int keyslot);
570 int LUKS2_volume_key_load_in_keyring_by_digest(struct crypt_device *cd,
571 struct luks2_hdr *hdr, struct volume_key *vk, int digest);
574 int LUKS2_luks1_to_luks2(struct crypt_device *cd,
575 struct luks_phdr *hdr1,
576 struct luks2_hdr *hdr2);
577 int LUKS2_luks2_to_luks1(struct crypt_device *cd,
578 struct luks2_hdr *hdr2,
579 struct luks_phdr *hdr1);
584 int LUKS2_reencrypt_locked_recovery_by_passphrase(struct crypt_device *cd,
587 const char *passphrase,
588 size_t passphrase_size,
590 struct volume_key **vks);
592 void LUKS2_reenc_context_free(struct crypt_device *cd, struct luks2_reenc_context *rh);
594 int LUKS2_assembly_multisegment_dmd(struct crypt_device *cd,
595 struct luks2_hdr *hdr,
596 struct volume_key *vks,
597 json_object *jobj_segments,
598 struct crypt_dm_active_device *dmd);
600 crypt_reencrypt_info LUKS2_reencrypt_status(struct crypt_device *cd,
601 struct crypt_params_reencrypt *params);
603 int crypt_reencrypt_lock(struct crypt_device *cd, struct crypt_lock_handle **reencrypt_lock);
604 int crypt_reencrypt_lock_by_dm_uuid(struct crypt_device *cd, const char *dm_uuid, struct crypt_lock_handle **reencrypt_lock);
605 void crypt_reencrypt_unlock(struct crypt_device *cd, struct crypt_lock_handle *reencrypt_lock);
607 int luks2_check_device_size(struct crypt_device *cd, struct luks2_hdr *hdr, uint64_t check_size, uint64_t *dev_size, bool activation, bool dynamic);