Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / luks2 / luks2.h
1 /*
2  * LUKS - Linux Unified Key Setup v2
3  *
4  * Copyright (C) 2015-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2015-2020 Milan Broz
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef _CRYPTSETUP_LUKS2_ONDISK_H
23 #define _CRYPTSETUP_LUKS2_ONDISK_H
24
25 #include <stdbool.h>
26
27 #include "libcryptsetup.h"
28
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
37
38 #define LUKS2_KEYSLOTS_MAX       32
39 #define LUKS2_TOKENS_MAX         32
40 #define LUKS2_SEGMENT_MAX        32
41
42 #define LUKS2_BUILTIN_TOKEN_PREFIX "luks2-"
43 #define LUKS2_BUILTIN_TOKEN_PREFIX_LEN 6
44
45 #define LUKS2_TOKEN_KEYRING LUKS2_BUILTIN_TOKEN_PREFIX "keyring"
46
47 #define LUKS2_DIGEST_MAX 8
48
49 #define CRYPT_ANY_SEGMENT -1
50 #define CRYPT_DEFAULT_SEGMENT -2
51 #define CRYPT_ONE_SEGMENT -3
52
53 #define CRYPT_ANY_DIGEST -1
54
55 /* 20 MiBs */
56 #define LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH 0x1400000
57
58 /* 1 GiB */
59 #define LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH 0x40000000
60
61 struct device;
62
63 /*
64  * LUKS2 header on-disk.
65  *
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.
69  *
70  * Note: uuid, csum_alg are intentionally on the same offset as LUKS1
71  * (checksum alg replaces hash in LUKS1)
72  *
73  * String (char) should be zero terminated.
74  * Padding should be wiped.
75  * Checksum is calculated with csum zeroed (+ full JSON area).
76  */
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 */
88         char            _padding[184];
89         uint8_t         csum[LUKS2_CHECKSUM_L];
90         char            _padding4096[7*512];
91         /* JSON area starts here */
92 } __attribute__ ((packed));
93
94 /*
95  * LUKS2 header in-memory.
96  */
97 typedef struct json_object json_object;
98 struct luks2_hdr {
99         size_t          hdr_size;
100         uint64_t        seqid;
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];
108         json_object     *jobj;
109 };
110
111 struct luks2_keyslot_params {
112         enum { LUKS2_KEYSLOT_AF_LUKS1 = 0 } af_type;
113         enum { LUKS2_KEYSLOT_AREA_RAW = 0 } area_type;
114
115         union {
116         struct {
117                 char hash[LUKS2_CHECKSUM_ALG_L]; // or include luks.h
118                 unsigned int stripes;
119         } luks1;
120         } af;
121
122         union {
123         struct {
124                 char encryption[65]; // or include utils_crypt.h
125                 size_t key_size;
126         } raw;
127         } area;
128 };
129
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;
135
136         union {
137         struct {
138         } none;
139         struct {
140                 char hash[LUKS2_CHECKSUM_ALG_L]; // or include luks.h
141                 struct crypt_hash *ch;
142                 size_t hash_size;
143                 /* buffer for checksums */
144                 void *checksums;
145                 size_t checksums_len;
146         } csum;
147         struct {
148         } ds;
149         } p;
150 };
151
152 struct luks2_reenc_context {
153         /* reencryption window attributes */
154         uint64_t offset;
155         uint64_t progress;
156         uint64_t length;
157         uint64_t data_shift;
158         size_t alignment;
159         uint64_t device_size;
160         bool online;
161         bool fixed_length;
162         crypt_reencrypt_direction_info direction;
163         crypt_reencrypt_mode_info mode;
164
165         char *device_name;
166         char *hotzone_name;
167         char *overlay_name;
168         uint32_t flags;
169
170         /* reencryption window persistence attributes */
171         struct reenc_protection rp;
172
173         int reenc_keyslot;
174
175         /* already running reencryption */
176         json_object *jobj_segs_hot;
177         json_object *jobj_segs_post;
178
179         /* backup segments */
180         json_object *jobj_segment_new;
181         int digest_new;
182         json_object *jobj_segment_old;
183         int digest_old;
184         json_object *jobj_segment_moved;
185
186         struct volume_key *vks;
187
188         void *reenc_buffer;
189         ssize_t read;
190
191         struct crypt_storage_wrapper *cw1;
192         struct crypt_storage_wrapper *cw2;
193
194         uint32_t wflags1;
195         uint32_t wflags2;
196
197         struct crypt_lock_handle *reenc_lock;
198 };
199
200 crypt_reencrypt_info LUKS2_reenc_status(struct luks2_hdr *hdr);
201 /*
202  * Supportable header sizes (hdr_disk + JSON area)
203  * Also used as offset for the 2nd header.
204  */
205 #define LUKS2_HDR_16K_LEN 0x4000
206
207 #define LUKS2_HDR_BIN_LEN sizeof(struct luks2_hdr_disk)
208
209 //#define LUKS2_DEFAULT_HDR_SIZE 0x400000  /* 4 MiB */
210 #define LUKS2_DEFAULT_HDR_SIZE 0x1000000 /* 16 MiB */
211
212 #define LUKS2_MAX_KEYSLOTS_SIZE 0x8000000 /* 128 MiB */
213
214 #define LUKS2_HDR_OFFSET_MAX 0x400000 /* 4 MiB */
215
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 }
219
220 int LUKS2_hdr_version_unlocked(struct crypt_device *cd,
221         const char *backup_file);
222
223 int LUKS2_device_write_lock(struct crypt_device *cd,
224         struct luks2_hdr *hdr, struct device *device);
225
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);
230
231 int LUKS2_hdr_uuid(struct crypt_device *cd,
232         struct luks2_hdr *hdr,
233         const char *uuid);
234
235 int LUKS2_hdr_labels(struct crypt_device *cd,
236         struct luks2_hdr *hdr,
237         const char *label,
238         const char *subsystem,
239         int commit);
240
241 void LUKS2_hdr_free(struct crypt_device *cd, struct luks2_hdr *hdr);
242
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);
249
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);
253
254 int LUKS2_keyslot_cipher_incompatible(struct crypt_device *cd, const char *cipher_spec);
255
256 /*
257  * Generic LUKS2 keyslot
258  */
259 int LUKS2_keyslot_open(struct crypt_device *cd,
260         int keyslot,
261         int segment,
262         const char *password,
263         size_t password_len,
264         struct volume_key **vk);
265
266 int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
267         int keyslot_old,
268         int keyslot_new,
269         const char *password,
270         size_t password_len,
271         struct volume_key **vks);
272
273 int LUKS2_keyslot_store(struct crypt_device *cd,
274         struct luks2_hdr *hdr,
275         int keyslot,
276         const char *password,
277         size_t password_len,
278         const struct volume_key *vk,
279         const struct luks2_keyslot_params *params);
280
281 int LUKS2_keyslot_reencrypt_store(struct crypt_device *cd,
282         struct luks2_hdr *hdr,
283         int keyslot,
284         const void *buffer,
285         size_t buffer_length);
286
287 int LUKS2_keyslot_reencrypt_create(struct crypt_device *cd,
288         struct luks2_hdr *hdr,
289         int keyslot,
290         const struct crypt_params_reencrypt *params);
291
292 int reenc_keyslot_update(struct crypt_device *cd,
293         const struct luks2_reenc_context *rh);
294
295 int LUKS2_keyslot_wipe(struct crypt_device *cd,
296         struct luks2_hdr *hdr,
297         int keyslot,
298         int wipe_area_only);
299
300 int LUKS2_keyslot_dump(struct crypt_device *cd,
301         int keyslot);
302
303 crypt_keyslot_priority LUKS2_keyslot_priority_get(struct crypt_device *cd,
304         struct luks2_hdr *hdr,
305         int keyslot);
306
307 int LUKS2_keyslot_priority_set(struct crypt_device *cd,
308         struct luks2_hdr *hdr,
309         int keyslot,
310         crypt_keyslot_priority priority,
311         int commit);
312
313 /*
314  * Generic LUKS2 token
315  */
316 int LUKS2_token_json_get(struct crypt_device *cd,
317         struct luks2_hdr *hdr,
318         int token,
319         const char **json);
320
321 int LUKS2_token_assign(struct crypt_device *cd,
322         struct luks2_hdr *hdr,
323         int keyslot,
324         int token,
325         int assign,
326         int commit);
327
328 int LUKS2_token_is_assigned(struct crypt_device *cd,
329         struct luks2_hdr *hdr,
330         int keyslot,
331         int token);
332
333 int LUKS2_token_create(struct crypt_device *cd,
334         struct luks2_hdr *hdr,
335         int token,
336         const char *json,
337         int commit);
338
339 crypt_token_info LUKS2_token_status(struct crypt_device *cd,
340         struct luks2_hdr *hdr,
341         int token,
342         const char **type);
343
344 int LUKS2_builtin_token_get(struct crypt_device *cd,
345         struct luks2_hdr *hdr,
346         int token,
347         const char *type,
348         void *params);
349
350 int LUKS2_builtin_token_create(struct crypt_device *cd,
351         struct luks2_hdr *hdr,
352         int token,
353         const char *type,
354         const void *params,
355         int commit);
356
357 int LUKS2_token_open_and_activate(struct crypt_device *cd,
358                 struct luks2_hdr *hdr,
359                 int token,
360                 const char *name,
361                 uint32_t flags,
362                 void *usrptr);
363
364 int LUKS2_token_open_and_activate_any(struct crypt_device *cd,
365         struct luks2_hdr *hdr,
366         const char *name,
367         uint32_t flags);
368
369 int LUKS2_tokens_count(struct luks2_hdr *hdr);
370
371 /*
372  * Generic LUKS2 segment
373  */
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);
388
389 int LUKS2_segments_count(struct luks2_hdr *hdr);
390
391 int LUKS2_segment_first_unused_id(struct luks2_hdr *hdr);
392
393 int LUKS2_segment_set_flag(json_object *jobj_segment, const char *flag);
394
395 json_object *LUKS2_get_segment_by_flag(struct luks2_hdr *hdr, const char *flag);
396
397 int LUKS2_get_segment_id_by_flag(struct luks2_hdr *hdr, const char *flag);
398
399 int LUKS2_segments_set(struct crypt_device *cd,
400         struct luks2_hdr *hdr,
401         json_object *jobj_segments,
402         int commit);
403
404 uint64_t LUKS2_segment_offset(struct luks2_hdr *hdr,
405         int segment,
406         unsigned blockwise);
407
408 uint64_t LUKS2_segment_size(struct luks2_hdr *hdr,
409         int segment,
410         unsigned blockwise);
411
412 int LUKS2_segment_is_type(struct luks2_hdr *hdr,
413         int segment,
414         const char *type);
415
416 int LUKS2_segment_by_type(struct luks2_hdr *hdr,
417         const char *type);
418
419 int LUKS2_last_segment_by_type(struct luks2_hdr *hdr,
420         const char *type);
421
422 int LUKS2_get_default_segment(struct luks2_hdr *hdr);
423
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);
427
428 /*
429  * Generic LUKS2 digest
430  */
431 int LUKS2_digest_any_matching(struct crypt_device *cd,
432                 struct luks2_hdr *hdr,
433                 const struct volume_key *vk);
434
435 int LUKS2_digest_by_segment(struct luks2_hdr *hdr, int segment);
436
437 int LUKS2_digest_verify_by_digest(struct crypt_device *cd,
438         struct luks2_hdr *hdr,
439         int digest,
440         const struct volume_key *vk);
441
442 int LUKS2_digest_verify_by_segment(struct crypt_device *cd,
443         struct luks2_hdr *hdr,
444         int segment,
445         const struct volume_key *vk);
446
447 void LUKS2_digests_erase_unused(struct crypt_device *cd,
448         struct luks2_hdr *hdr);
449
450 int LUKS2_digest_verify(struct crypt_device *cd,
451         struct luks2_hdr *hdr,
452         const struct volume_key *vk,
453         int keyslot);
454
455 int LUKS2_digest_dump(struct crypt_device *cd,
456         int digest);
457
458 int LUKS2_digest_assign(struct crypt_device *cd,
459         struct luks2_hdr *hdr,
460         int keyslot,
461         int digest,
462         int assign,
463         int commit);
464
465 int LUKS2_digest_segment_assign(struct crypt_device *cd,
466         struct luks2_hdr *hdr,
467         int segment,
468         int digest,
469         int assign,
470         int commit);
471
472 int LUKS2_digest_by_keyslot(struct luks2_hdr *hdr, int keyslot);
473
474 int LUKS2_digest_create(struct crypt_device *cd,
475         const char *type,
476         struct luks2_hdr *hdr,
477         const struct volume_key *vk);
478
479 /*
480  * LUKS2 generic
481  */
482 int LUKS2_activate(struct crypt_device *cd,
483         const char *name,
484         struct volume_key *vk,
485         uint32_t flags);
486
487 int LUKS2_activate_multi(struct crypt_device *cd,
488         const char *name,
489         struct volume_key *vks,
490         uint64_t device_size,
491         uint32_t flags);
492
493 struct crypt_dm_active_device;
494
495 int LUKS2_deactivate(struct crypt_device *cd,
496         const char *name,
497         struct luks2_hdr *hdr,
498         struct crypt_dm_active_device *dmd,
499         uint32_t flags);
500
501 int LUKS2_reload(struct crypt_device *cd,
502         const char *name,
503         struct volume_key *vks,
504         uint64_t device_size,
505         uint32_t flags);
506
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,
514         const char *uuid,
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);
521
522 int LUKS2_check_metadata_area_size(uint64_t metadata_size);
523 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size);
524
525 int LUKS2_wipe_header_areas(struct crypt_device *cd,
526         struct luks2_hdr *hdr);
527
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,
544         int keyslot,
545         uint64_t *offset,
546         uint64_t *length);
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);
551
552 /*
553  * Permanent activation flags stored in header
554  */
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);
557
558 /*
559  * Requirements for device activation or header modification
560  */
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);
563
564 int LUKS2_unmet_requirements(struct crypt_device *cd, struct luks2_hdr *hdr, uint32_t reqs_mask, int quiet);
565
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);
572
573 struct luks_phdr;
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);
580
581 /*
582  * LUKS2 reencryption
583  */
584 int LUKS2_reencrypt_locked_recovery_by_passphrase(struct crypt_device *cd,
585         int keyslot_old,
586         int keyslot_new,
587         const char *passphrase,
588         size_t passphrase_size,
589         uint32_t flags,
590         struct volume_key **vks);
591
592 void LUKS2_reenc_context_free(struct crypt_device *cd, struct luks2_reenc_context *rh);
593
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);
599
600 crypt_reencrypt_info LUKS2_reencrypt_status(struct crypt_device *cd,
601         struct crypt_params_reencrypt *params);
602
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);
606
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);
608
609 #endif