Makefile: Add security compiling option (RELRO, SC, and FORTIFY)
[platform/upstream/cryptsetup.git] / lib / setup.c
1 /*
2  * libcryptsetup - cryptsetup library
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2023 Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <sys/utsname.h>
29 #include <errno.h>
30
31 #include "libcryptsetup.h"
32 #include "luks1/luks.h"
33 #include "luks2/luks2.h"
34 #include "loopaes/loopaes.h"
35 #include "verity/verity.h"
36 #include "tcrypt/tcrypt.h"
37 #include "integrity/integrity.h"
38 #include "bitlk/bitlk.h"
39 #include "fvault2/fvault2.h"
40 #include "utils_device_locking.h"
41 #include "internal.h"
42 #include "keyslot_context.h"
43
44 #define CRYPT_CD_UNRESTRICTED   (1 << 0)
45 #define CRYPT_CD_QUIET          (1 << 1)
46
47 struct crypt_device {
48         char *type;
49
50         struct device *device;
51         struct device *metadata_device;
52
53         struct volume_key *volume_key;
54         int rng_type;
55         uint32_t compatibility;
56         struct crypt_pbkdf_type pbkdf;
57
58         /* global context scope settings */
59         unsigned key_in_keyring:1;
60
61         uint64_t data_offset;
62         uint64_t metadata_size; /* Used in LUKS2 format */
63         uint64_t keyslots_size; /* Used in LUKS2 format */
64
65         /* Workaround for OOM during parallel activation (like in systemd) */
66         bool memory_hard_pbkdf_lock_enabled;
67         struct crypt_lock_handle *pbkdf_memory_hard_lock;
68
69         union {
70         struct { /* used in CRYPT_LUKS1 */
71                 struct luks_phdr hdr;
72                 char *cipher_spec;
73         } luks1;
74         struct { /* used in CRYPT_LUKS2 */
75                 struct luks2_hdr hdr;
76                 char cipher[MAX_CIPHER_LEN];      /* only for compatibility */
77                 char cipher_mode[MAX_CIPHER_LEN]; /* only for compatibility */
78                 char *keyslot_cipher;
79                 unsigned int keyslot_key_size;
80                 struct luks2_reencrypt *rh;
81         } luks2;
82         struct { /* used in CRYPT_PLAIN */
83                 struct crypt_params_plain hdr;
84                 char *cipher_spec;
85                 char *cipher;
86                 const char *cipher_mode;
87                 unsigned int key_size;
88         } plain;
89         struct { /* used in CRYPT_LOOPAES */
90                 struct crypt_params_loopaes hdr;
91                 char *cipher_spec;
92                 char *cipher;
93                 const char *cipher_mode;
94                 unsigned int key_size;
95         } loopaes;
96         struct { /* used in CRYPT_VERITY */
97                 struct crypt_params_verity hdr;
98                 const char *root_hash;
99                 unsigned int root_hash_size;
100                 char *uuid;
101                 struct device *fec_device;
102         } verity;
103         struct { /* used in CRYPT_TCRYPT */
104                 struct crypt_params_tcrypt params;
105                 struct tcrypt_phdr hdr;
106         } tcrypt;
107         struct { /* used in CRYPT_INTEGRITY */
108                 struct crypt_params_integrity params;
109                 struct volume_key *journal_mac_key;
110                 struct volume_key *journal_crypt_key;
111                 uint32_t sb_flags;
112         } integrity;
113         struct { /* used in CRYPT_BITLK */
114                 struct bitlk_metadata params;
115                 char *cipher_spec;
116         } bitlk;
117         struct { /* used in CRYPT_FVAULT2 */
118                 struct fvault2_params params;
119         } fvault2;
120         struct { /* used if initialized without header by name */
121                 char *active_name;
122                 /* buffers, must refresh from kernel on every query */
123                 char cipher_spec[MAX_CIPHER_LEN*2+1];
124                 char cipher[MAX_CIPHER_LEN];
125                 const char *cipher_mode;
126                 unsigned int key_size;
127         } none;
128         } u;
129
130         /* callbacks definitions */
131         void (*log)(int level, const char *msg, void *usrptr);
132         void *log_usrptr;
133         int (*confirm)(const char *msg, void *usrptr);
134         void *confirm_usrptr;
135 };
136
137 /* Just to suppress redundant messages about crypto backend */
138 static int _crypto_logged = 0;
139
140 /* Log helper */
141 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
142 static void *_default_log_usrptr = NULL;
143 static int _debug_level = 0;
144
145 /* Library can do metadata locking  */
146 static int _metadata_locking = 1;
147
148 /* Library scope detection for kernel keyring support */
149 static int _kernel_keyring_supported;
150
151 /* Library allowed to use kernel keyring for loading VK in kernel crypto layer */
152 static int _vk_via_keyring = 1;
153
154 void crypt_set_debug_level(int level)
155 {
156         _debug_level = level;
157 }
158
159 int crypt_get_debug_level(void)
160 {
161         return _debug_level;
162 }
163
164 void crypt_log(struct crypt_device *cd, int level, const char *msg)
165 {
166         if (!msg)
167                 return;
168
169         if (level < _debug_level)
170                 return;
171
172         if (cd && cd->log)
173                 cd->log(level, msg, cd->log_usrptr);
174         else if (_default_log)
175                 _default_log(level, msg, _default_log_usrptr);
176         /* Default to stdout/stderr if there is no callback. */
177         else
178                 fprintf(level == CRYPT_LOG_ERROR ? stderr : stdout, "%s", msg);
179 }
180
181 __attribute__((format(printf, 3, 4)))
182 void crypt_logf(struct crypt_device *cd, int level, const char *format, ...)
183 {
184         va_list argp;
185         char target[LOG_MAX_LEN + 2];
186         int len;
187
188         va_start(argp, format);
189
190         len = vsnprintf(&target[0], LOG_MAX_LEN, format, argp);
191         if (len > 0 && len < LOG_MAX_LEN) {
192                 /* All verbose and error messages in tools end with EOL. */
193                 if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR ||
194                     level == CRYPT_LOG_DEBUG || level == CRYPT_LOG_DEBUG_JSON)
195                         strncat(target, "\n", LOG_MAX_LEN);
196
197                 crypt_log(cd, level, target);
198         }
199
200         va_end(argp);
201 }
202
203 static const char *mdata_device_path(struct crypt_device *cd)
204 {
205         return device_path(cd->metadata_device ?: cd->device);
206 }
207
208 static const char *data_device_path(struct crypt_device *cd)
209 {
210         return device_path(cd->device);
211 }
212
213 /* internal only */
214 struct device *crypt_metadata_device(struct crypt_device *cd)
215 {
216         return cd->metadata_device ?: cd->device;
217 }
218
219 struct device *crypt_data_device(struct crypt_device *cd)
220 {
221         return cd->device;
222 }
223
224 int init_crypto(struct crypt_device *ctx)
225 {
226         struct utsname uts;
227         int r;
228
229         r = crypt_random_init(ctx);
230         if (r < 0) {
231                 log_err(ctx, _("Cannot initialize crypto RNG backend."));
232                 return r;
233         }
234
235         r = crypt_backend_init(crypt_fips_mode());
236         if (r < 0)
237                 log_err(ctx, _("Cannot initialize crypto backend."));
238
239         if (!r && !_crypto_logged) {
240                 log_dbg(ctx, "Crypto backend (%s) initialized in cryptsetup library version %s.",
241                         crypt_backend_version(), PACKAGE_VERSION);
242                 if (!uname(&uts))
243                         log_dbg(ctx, "Detected kernel %s %s %s.",
244                                 uts.sysname, uts.release, uts.machine);
245                 _crypto_logged = 1;
246         }
247
248         return r;
249 }
250
251 static int process_key(struct crypt_device *cd, const char *hash_name,
252                        size_t key_size, const char *pass, size_t passLen,
253                        struct volume_key **vk)
254 {
255         int r;
256
257         if (!key_size)
258                 return -EINVAL;
259
260         *vk = crypt_alloc_volume_key(key_size, NULL);
261         if (!*vk)
262                 return -ENOMEM;
263
264         if (hash_name) {
265                 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
266                 if (r < 0) {
267                         if (r == -ENOENT)
268                                 log_err(cd, _("Hash algorithm %s not supported."),
269                                         hash_name);
270                         else
271                                 log_err(cd, _("Key processing error (using hash %s)."),
272                                         hash_name);
273                         crypt_free_volume_key(*vk);
274                         *vk = NULL;
275                         return -EINVAL;
276                 }
277         } else if (passLen > key_size) {
278                 memcpy((*vk)->key, pass, key_size);
279         } else {
280                 memcpy((*vk)->key, pass, passLen);
281         }
282
283         return 0;
284 }
285
286 static int isPLAIN(const char *type)
287 {
288         return (type && !strcmp(CRYPT_PLAIN, type));
289 }
290
291 static int isLUKS1(const char *type)
292 {
293         return (type && !strcmp(CRYPT_LUKS1, type));
294 }
295
296 static int isLUKS2(const char *type)
297 {
298         return (type && !strcmp(CRYPT_LUKS2, type));
299 }
300
301 static int isLUKS(const char *type)
302 {
303         return (isLUKS2(type) || isLUKS1(type));
304 }
305
306 static int isLOOPAES(const char *type)
307 {
308         return (type && !strcmp(CRYPT_LOOPAES, type));
309 }
310
311 static int isVERITY(const char *type)
312 {
313         return (type && !strcmp(CRYPT_VERITY, type));
314 }
315
316 static int isTCRYPT(const char *type)
317 {
318         return (type && !strcmp(CRYPT_TCRYPT, type));
319 }
320
321 static int isINTEGRITY(const char *type)
322 {
323         return (type && !strcmp(CRYPT_INTEGRITY, type));
324 }
325
326 static int isBITLK(const char *type)
327 {
328         return (type && !strcmp(CRYPT_BITLK, type));
329 }
330
331 static int isFVAULT2(const char *type)
332 {
333         return (type && !strcmp(CRYPT_FVAULT2, type));
334 }
335
336 static int _onlyLUKS(struct crypt_device *cd, uint32_t cdflags)
337 {
338         int r = 0;
339
340         if (cd && !cd->type) {
341                 if (!(cdflags & CRYPT_CD_QUIET))
342                         log_err(cd, _("Cannot determine device type. Incompatible activation of device?"));
343                 r = -EINVAL;
344         }
345
346         if (!cd || !isLUKS(cd->type)) {
347                 if (!(cdflags & CRYPT_CD_QUIET))
348                         log_err(cd, _("This operation is supported only for LUKS device."));
349                 r = -EINVAL;
350         }
351
352         if (r || (cdflags & CRYPT_CD_UNRESTRICTED) || isLUKS1(cd->type))
353                 return r;
354
355         return LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, 0, cdflags & CRYPT_CD_QUIET);
356 }
357
358 static int onlyLUKS(struct crypt_device *cd)
359 {
360         return _onlyLUKS(cd, 0);
361 }
362
363 static int _onlyLUKS2(struct crypt_device *cd, uint32_t cdflags, uint32_t mask)
364 {
365         int r = 0;
366
367         if (cd && !cd->type) {
368                 if (!(cdflags & CRYPT_CD_QUIET))
369                         log_err(cd, _("Cannot determine device type. Incompatible activation of device?"));
370                 r = -EINVAL;
371         }
372
373         if (!cd || !isLUKS2(cd->type)) {
374                 if (!(cdflags & CRYPT_CD_QUIET))
375                         log_err(cd, _("This operation is supported only for LUKS2 device."));
376                 r = -EINVAL;
377         }
378
379         if (r || (cdflags & CRYPT_CD_UNRESTRICTED))
380                 return r;
381
382         return LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, mask, cdflags & CRYPT_CD_QUIET);
383 }
384
385 /* Internal only */
386 int onlyLUKS2(struct crypt_device *cd)
387 {
388         return _onlyLUKS2(cd, 0, 0);
389 }
390
391 /* Internal only */
392 int onlyLUKS2mask(struct crypt_device *cd, uint32_t mask)
393 {
394         return _onlyLUKS2(cd, 0, mask);
395 }
396
397 static void crypt_set_null_type(struct crypt_device *cd)
398 {
399         free(cd->type);
400         cd->type = NULL;
401         cd->data_offset = 0;
402         cd->metadata_size = 0;
403         cd->keyslots_size = 0;
404         crypt_safe_memzero(&cd->u, sizeof(cd->u));
405 }
406
407 static void crypt_reset_null_type(struct crypt_device *cd)
408 {
409         if (cd->type)
410                 return;
411
412         free(cd->u.none.active_name);
413         cd->u.none.active_name = NULL;
414 }
415
416 /* keyslot helpers */
417 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
418 {
419         crypt_keyslot_info ki;
420
421         if (*keyslot == CRYPT_ANY_SLOT) {
422                 if (isLUKS1(cd->type))
423                         *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
424                 else
425                         *keyslot = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, 0);
426                 if (*keyslot < 0) {
427                         log_err(cd, _("All key slots full."));
428                         return -EINVAL;
429                 }
430         }
431
432         if (isLUKS1(cd->type))
433                 ki = LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot);
434         else
435                 ki = LUKS2_keyslot_info(&cd->u.luks2.hdr, *keyslot);
436         switch (ki) {
437                 case CRYPT_SLOT_INVALID:
438                         log_err(cd, _("Key slot %d is invalid, please select between 0 and %d."),
439                                 *keyslot, crypt_keyslot_max(cd->type) - 1);
440                         return -EINVAL;
441                 case CRYPT_SLOT_INACTIVE:
442                         break;
443                 default:
444                         log_err(cd, _("Key slot %d is full, please select another one."),
445                                 *keyslot);
446                         return -EINVAL;
447         }
448
449         log_dbg(cd, "Selected keyslot %d.", *keyslot);
450         return 0;
451 }
452
453 /*
454  * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
455  */
456 int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
457 {
458         int i, j;
459         char *str;
460
461         if (!dm_uuid || !hdr_uuid)
462                 return -EINVAL;
463
464         str = strchr(dm_uuid, '-');
465         if (!str)
466                 return -EINVAL;
467
468         for (i = 0, j = 1; hdr_uuid[i]; i++) {
469                 if (hdr_uuid[i] == '-')
470                         continue;
471
472                 if (!str[j] || str[j] == '-')
473                         return -EINVAL;
474
475                 if (str[j] != hdr_uuid[i])
476                         return -EINVAL;
477                 j++;
478         }
479
480         return 0;
481 }
482
483 /*
484  * compares type of active device to provided string (only if there is no explicit type)
485  */
486 static int crypt_uuid_type_cmp(struct crypt_device *cd, const char *type)
487 {
488         struct crypt_dm_active_device dmd;
489         size_t len;
490         int r;
491
492         /* Must use header-on-disk if we know the type here */
493         if (cd->type || !cd->u.none.active_name)
494                 return -EINVAL;
495
496         log_dbg(cd, "Checking if active device %s without header has UUID type %s.",
497                 cd->u.none.active_name, type);
498
499         r = dm_query_device(cd, cd->u.none.active_name, DM_ACTIVE_UUID, &dmd);
500         if (r < 0)
501                 return r;
502
503         r = -ENODEV;
504         len = strlen(type);
505         if (dmd.uuid && strlen(dmd.uuid) > len &&
506             !strncmp(dmd.uuid, type, len) && dmd.uuid[len] == '-')
507                 r = 0;
508
509         free(CONST_CAST(void*)dmd.uuid);
510         return r;
511 }
512
513 int PLAIN_activate(struct crypt_device *cd,
514                      const char *name,
515                      struct volume_key *vk,
516                      uint64_t size,
517                      uint32_t flags)
518 {
519         int r;
520         struct crypt_dm_active_device dmd = {
521                 .flags = flags,
522                 .size = size,
523         };
524
525         log_dbg(cd, "Trying to activate PLAIN device %s using cipher %s.",
526                 name, crypt_get_cipher_spec(cd));
527
528         if (MISALIGNED(size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) {
529                 log_err(cd, _("Device size is not aligned to device logical block size."));
530                 return -EINVAL;
531         }
532
533         r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
534                         vk, crypt_get_cipher_spec(cd), crypt_get_iv_offset(cd),
535                         crypt_get_data_offset(cd), crypt_get_integrity(cd),
536                         crypt_get_integrity_tag_size(cd), crypt_get_sector_size(cd));
537         if (r < 0)
538                 return r;
539
540         r = create_or_reload_device(cd, name, CRYPT_PLAIN, &dmd);
541
542         dm_targets_free(cd, &dmd);
543         return r;
544 }
545
546 int crypt_confirm(struct crypt_device *cd, const char *msg)
547 {
548         if (!cd || !cd->confirm)
549                 return 1;
550         else
551                 return cd->confirm(msg, cd->confirm_usrptr);
552 }
553
554 void crypt_set_log_callback(struct crypt_device *cd,
555         void (*log)(int level, const char *msg, void *usrptr),
556         void *usrptr)
557 {
558         if (!cd) {
559                 _default_log = log;
560                 _default_log_usrptr = usrptr;
561         } else {
562                 cd->log = log;
563                 cd->log_usrptr = usrptr;
564         }
565 }
566
567 void crypt_set_confirm_callback(struct crypt_device *cd,
568         int (*confirm)(const char *msg, void *usrptr),
569         void *usrptr)
570 {
571         if (cd) {
572                 cd->confirm = confirm;
573                 cd->confirm_usrptr = usrptr;
574         }
575 }
576
577 const char *crypt_get_dir(void)
578 {
579         return dm_get_dir();
580 }
581
582 int crypt_init(struct crypt_device **cd, const char *device)
583 {
584         struct crypt_device *h = NULL;
585         int r;
586
587         if (!cd)
588                 return -EINVAL;
589
590         log_dbg(NULL, "Allocating context for crypt device %s.", device ?: "(none)");
591 #if !HAVE_DECL_O_CLOEXEC
592         log_dbg(NULL, "Running without O_CLOEXEC.");
593 #endif
594
595         if (!(h = malloc(sizeof(struct crypt_device))))
596                 return -ENOMEM;
597
598         memset(h, 0, sizeof(*h));
599
600         r = device_alloc(NULL, &h->device, device);
601         if (r < 0) {
602                 free(h);
603                 return r;
604         }
605
606         dm_backend_init(NULL);
607
608         h->rng_type = crypt_random_default_key_rng();
609
610         *cd = h;
611         return 0;
612 }
613
614 static int crypt_check_data_device_size(struct crypt_device *cd)
615 {
616         int r;
617         uint64_t size, size_min;
618
619         /* Check data device size, require at least header or one sector */
620         size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
621
622         r = device_size(cd->device, &size);
623         if (r < 0)
624                 return r;
625
626         if (size < size_min) {
627                 log_err(cd, _("Header detected but device %s is too small."),
628                         device_path(cd->device));
629                 return -EINVAL;
630         }
631
632         return r;
633 }
634
635 static int _crypt_set_data_device(struct crypt_device *cd, const char *device)
636 {
637         struct device *dev = NULL;
638         int r;
639
640         r = device_alloc(cd, &dev, device);
641         if (r < 0)
642                 return r;
643
644         if (!cd->metadata_device) {
645                 cd->metadata_device = cd->device;
646         } else
647                 device_free(cd, cd->device);
648
649         cd->device = dev;
650
651         r = crypt_check_data_device_size(cd);
652         if (!r && isLUKS2(cd->type))
653                 device_set_block_size(crypt_data_device(cd), LUKS2_get_sector_size(&cd->u.luks2.hdr));
654
655         return r;
656 }
657
658 int crypt_set_data_device(struct crypt_device *cd, const char *device)
659 {
660         /* metadata device must be set */
661         if (!cd || !cd->device || !device)
662                 return -EINVAL;
663
664         log_dbg(cd, "Setting ciphertext data device to %s.", device ?: "(none)");
665
666         if (!isLUKS1(cd->type) && !isLUKS2(cd->type) && !isVERITY(cd->type) &&
667             !isINTEGRITY(cd->type) && !isTCRYPT(cd->type)) {
668                 log_err(cd, _("This operation is not supported for this device type."));
669                 return -EINVAL;
670         }
671
672         if (isLUKS2(cd->type) && crypt_get_luks2_reencrypt(cd)) {
673                 log_err(cd, _("Illegal operation with reencryption in-progress."));
674                 return -EINVAL;
675         }
676
677         return _crypt_set_data_device(cd, device);
678 }
679
680 int crypt_init_data_device(struct crypt_device **cd, const char *device, const char *data_device)
681 {
682         int r;
683
684         if (!cd)
685                 return -EINVAL;
686
687         r = crypt_init(cd, device);
688         if (r || !data_device || !strcmp(device, data_device))
689                 return r;
690
691         log_dbg(NULL, "Setting ciphertext data device to %s.", data_device);
692         r = _crypt_set_data_device(*cd, data_device);
693         if (r) {
694                 crypt_free(*cd);
695                 *cd = NULL;
696         }
697
698         return r;
699 }
700
701 static void crypt_free_type(struct crypt_device *cd, const char *force_type)
702 {
703         const char *type = force_type ?: cd->type;
704
705         if (isPLAIN(type)) {
706                 free(CONST_CAST(void*)cd->u.plain.hdr.hash);
707                 free(cd->u.plain.cipher);
708                 free(cd->u.plain.cipher_spec);
709         } else if (isLUKS2(type)) {
710                 LUKS2_reencrypt_free(cd, cd->u.luks2.rh);
711                 LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
712                 free(cd->u.luks2.keyslot_cipher);
713         } else if (isLUKS1(type)) {
714                 free(cd->u.luks1.cipher_spec);
715         } else if (isLOOPAES(type)) {
716                 free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
717                 free(cd->u.loopaes.cipher);
718                 free(cd->u.loopaes.cipher_spec);
719         } else if (isVERITY(type)) {
720                 free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
721                 free(CONST_CAST(void*)cd->u.verity.hdr.data_device);
722                 free(CONST_CAST(void*)cd->u.verity.hdr.hash_device);
723                 free(CONST_CAST(void*)cd->u.verity.hdr.fec_device);
724                 free(CONST_CAST(void*)cd->u.verity.hdr.salt);
725                 free(CONST_CAST(void*)cd->u.verity.root_hash);
726                 free(cd->u.verity.uuid);
727                 device_free(cd, cd->u.verity.fec_device);
728         } else if (isINTEGRITY(type)) {
729                 free(CONST_CAST(void*)cd->u.integrity.params.integrity);
730                 free(CONST_CAST(void*)cd->u.integrity.params.journal_integrity);
731                 free(CONST_CAST(void*)cd->u.integrity.params.journal_crypt);
732                 crypt_free_volume_key(cd->u.integrity.journal_crypt_key);
733                 crypt_free_volume_key(cd->u.integrity.journal_mac_key);
734         } else if (isBITLK(type)) {
735                 free(cd->u.bitlk.cipher_spec);
736                 BITLK_bitlk_metadata_free(&cd->u.bitlk.params);
737         } else if (!type) {
738                 free(cd->u.none.active_name);
739                 cd->u.none.active_name = NULL;
740         }
741
742         crypt_set_null_type(cd);
743 }
744
745 /* internal only */
746 struct crypt_pbkdf_type *crypt_get_pbkdf(struct crypt_device *cd)
747 {
748         return &cd->pbkdf;
749 }
750
751 /*
752  * crypt_load() helpers
753  */
754 static int _crypt_load_luks2(struct crypt_device *cd, int reload, int repair)
755 {
756         int r;
757         char *type = NULL;
758         struct luks2_hdr hdr2 = {};
759
760         log_dbg(cd, "%soading LUKS2 header (repair %sabled).", reload ? "Rel" : "L", repair ? "en" : "dis");
761
762         r = LUKS2_hdr_read(cd, &hdr2, repair);
763         if (r)
764                 return r;
765
766         if (!reload && !(type = strdup(CRYPT_LUKS2))) {
767                 r = -ENOMEM;
768                 goto out;
769         }
770
771         if (verify_pbkdf_params(cd, &cd->pbkdf)) {
772                 r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2);
773                 if (r)
774                         goto out;
775         }
776
777         if (reload) {
778                 LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
779                 free(cd->u.luks2.keyslot_cipher);
780         } else
781                 cd->type = type;
782
783         r = 0;
784         memcpy(&cd->u.luks2.hdr, &hdr2, sizeof(hdr2));
785         cd->u.luks2.keyslot_cipher = NULL;
786         cd->u.luks2.rh = NULL;
787
788 out:
789         if (r) {
790                 free(type);
791                 LUKS2_hdr_free(cd, &hdr2);
792         }
793         return r;
794 }
795
796 static void _luks2_rollback(struct crypt_device *cd)
797 {
798         if (!cd || !isLUKS2(cd->type))
799                 return;
800
801         if (LUKS2_hdr_rollback(cd, &cd->u.luks2.hdr)) {
802                 log_err(cd, _("Failed to rollback LUKS2 metadata in memory."));
803                 return;
804         }
805
806         free(cd->u.luks2.keyslot_cipher);
807         cd->u.luks2.keyslot_cipher = NULL;
808 }
809
810 static int _crypt_load_luks(struct crypt_device *cd, const char *requested_type,
811                             bool quiet, bool repair)
812 {
813         char *cipher_spec;
814         struct luks_phdr hdr = {};
815         int r, version;
816
817         r = init_crypto(cd);
818         if (r < 0)
819                 return r;
820
821         /* This will return 0 if primary LUKS2 header is damaged */
822         version = LUKS2_hdr_version_unlocked(cd, NULL);
823
824         if ((isLUKS1(requested_type) && version == 2) ||
825             (isLUKS2(requested_type) && version == 1))
826                 return -EINVAL;
827
828         if (requested_type)
829                 version = 0;
830
831         if (isLUKS1(requested_type) || version == 1) {
832                 if (isLUKS2(cd->type)) {
833                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
834                         return -EINVAL;
835                 }
836
837                 if (verify_pbkdf_params(cd, &cd->pbkdf)) {
838                         r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1);
839                         if (r)
840                                 return r;
841                 }
842
843                 r = LUKS_read_phdr(&hdr, !quiet, repair, cd);
844                 if (r)
845                         goto out;
846
847                 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1))) {
848                         r = -ENOMEM;
849                         goto out;
850                 }
851
852                 /* Set hash to the same as in the loaded header */
853                 if (!cd->pbkdf.hash || strcmp(cd->pbkdf.hash, hdr.hashSpec)) {
854                         free(CONST_CAST(void*)cd->pbkdf.hash);
855                         cd->pbkdf.hash = strdup(hdr.hashSpec);
856                         if (!cd->pbkdf.hash) {
857                                 r = -ENOMEM;
858                                 goto out;
859                         }
860                 }
861
862                 if (asprintf(&cipher_spec, "%s-%s", hdr.cipherName, hdr.cipherMode) < 0) {
863                         r = -ENOMEM;
864                         goto out;
865                 }
866
867                 free(cd->u.luks1.cipher_spec);
868                 cd->u.luks1.cipher_spec = cipher_spec;
869
870                 memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
871         } else if (isLUKS2(requested_type) || version == 2 || version == 0) {
872                 if (isLUKS1(cd->type)) {
873                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
874                         return -EINVAL;
875                 }
876
877                 /*
878                  * Current LUKS2 repair just overrides blkid probes
879                  * and perform auto-recovery if possible. This is safe
880                  * unless future LUKS2 repair code do something more
881                  * sophisticated. In such case we would need to check
882                  * for LUKS2 requirements and decide if it's safe to
883                  * perform repair.
884                  */
885                 r =  _crypt_load_luks2(cd, cd->type != NULL, repair);
886                 if (!r)
887                         device_set_block_size(crypt_data_device(cd), LUKS2_get_sector_size(&cd->u.luks2.hdr));
888                 else if (!quiet)
889                         log_err(cd, _("Device %s is not a valid LUKS device."), mdata_device_path(cd));
890         } else {
891                 if (version > 2)
892                         log_err(cd, _("Unsupported LUKS version %d."), version);
893                 r = -EINVAL;
894         }
895 out:
896         crypt_safe_memzero(&hdr, sizeof(hdr));
897
898         return r;
899 }
900
901 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
902 {
903         int r;
904
905         if (!params)
906                 return -EINVAL;
907
908         r = init_crypto(cd);
909         if (r < 0)
910                 return r;
911
912         memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
913
914         r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
915
916         cd->u.tcrypt.params.passphrase = NULL;
917         cd->u.tcrypt.params.passphrase_size = 0;
918         cd->u.tcrypt.params.keyfiles = NULL;
919         cd->u.tcrypt.params.keyfiles_count = 0;
920         cd->u.tcrypt.params.veracrypt_pim = 0;
921
922         if (r < 0)
923                 goto out;
924
925         if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
926                 r = -ENOMEM;
927 out:
928         if (r < 0)
929                 crypt_free_type(cd, CRYPT_TCRYPT);
930         return r;
931 }
932
933 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
934 {
935         int r;
936         uint64_t sb_offset = 0;
937
938         r = init_crypto(cd);
939         if (r < 0)
940                 return r;
941
942         if (params && params->flags & CRYPT_VERITY_NO_HEADER)
943                 return -EINVAL;
944
945         if (params)
946                 sb_offset = params->hash_area_offset;
947
948         r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
949         if (r < 0)
950                 goto out;
951
952         if (!cd->type && !(cd->type = strdup(CRYPT_VERITY))) {
953                 r = -ENOMEM;
954                 goto out;
955         }
956
957         if (params)
958                 cd->u.verity.hdr.flags = params->flags;
959
960         /* Hash availability checked in sb load */
961         cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
962         if (cd->u.verity.root_hash_size > 4096) {
963                 r = -EINVAL;
964                 goto out;
965         }
966
967         if (params && params->data_device &&
968             (r = crypt_set_data_device(cd, params->data_device)) < 0)
969                 goto out;
970
971         if (params && params->fec_device) {
972                 r = device_alloc(cd, &cd->u.verity.fec_device, params->fec_device);
973                 if (r < 0)
974                         goto out;
975                 cd->u.verity.hdr.fec_area_offset = params->fec_area_offset;
976                 cd->u.verity.hdr.fec_roots = params->fec_roots;
977         }
978 out:
979         if (r < 0)
980                 crypt_free_type(cd, CRYPT_VERITY);
981         return r;
982 }
983
984 static int _crypt_load_integrity(struct crypt_device *cd,
985                                  struct crypt_params_integrity *params)
986 {
987         int r;
988
989         r = init_crypto(cd);
990         if (r < 0)
991                 return r;
992
993         r = INTEGRITY_read_sb(cd, &cd->u.integrity.params, &cd->u.integrity.sb_flags);
994         if (r < 0)
995                 goto out;
996
997         // FIXME: add checks for fields in integrity sb vs params
998
999         r = -ENOMEM;
1000         if (params) {
1001                 cd->u.integrity.params.journal_watermark = params->journal_watermark;
1002                 cd->u.integrity.params.journal_commit_time = params->journal_commit_time;
1003                 cd->u.integrity.params.buffer_sectors = params->buffer_sectors;
1004                 if (params->integrity &&
1005                     !(cd->u.integrity.params.integrity = strdup(params->integrity)))
1006                         goto out;
1007                 cd->u.integrity.params.integrity_key_size = params->integrity_key_size;
1008                 if (params->journal_integrity &&
1009                     !(cd->u.integrity.params.journal_integrity = strdup(params->journal_integrity)))
1010                         goto out;
1011                 if (params->journal_crypt &&
1012                     !(cd->u.integrity.params.journal_crypt = strdup(params->journal_crypt)))
1013                         goto out;
1014
1015                 if (params->journal_crypt_key) {
1016                         cd->u.integrity.journal_crypt_key =
1017                                 crypt_alloc_volume_key(params->journal_crypt_key_size,
1018                                                        params->journal_crypt_key);
1019                         if (!cd->u.integrity.journal_crypt_key)
1020                                 goto out;
1021                 }
1022                 if (params->journal_integrity_key) {
1023                         cd->u.integrity.journal_mac_key =
1024                                 crypt_alloc_volume_key(params->journal_integrity_key_size,
1025                                                        params->journal_integrity_key);
1026                         if (!cd->u.integrity.journal_mac_key)
1027                                 goto out;
1028                 }
1029         }
1030
1031         if (!cd->type && !(cd->type = strdup(CRYPT_INTEGRITY)))
1032                 goto out;
1033         r = 0;
1034 out:
1035         if (r < 0)
1036                 crypt_free_type(cd, CRYPT_INTEGRITY);
1037         return r;
1038 }
1039
1040 static int _crypt_load_bitlk(struct crypt_device *cd)
1041 {
1042         int r;
1043
1044         r = init_crypto(cd);
1045         if (r < 0)
1046                 return r;
1047
1048         r = BITLK_read_sb(cd, &cd->u.bitlk.params);
1049         if (r < 0)
1050                 goto out;
1051
1052         if (asprintf(&cd->u.bitlk.cipher_spec, "%s-%s",
1053                      cd->u.bitlk.params.cipher, cd->u.bitlk.params.cipher_mode) < 0) {
1054                 cd->u.bitlk.cipher_spec = NULL;
1055                 r = -ENOMEM;
1056                 goto out;
1057         }
1058
1059         if (!cd->type && !(cd->type = strdup(CRYPT_BITLK))) {
1060                 r = -ENOMEM;
1061                 goto out;
1062         }
1063
1064         device_set_block_size(crypt_data_device(cd), cd->u.bitlk.params.sector_size);
1065 out:
1066         if (r < 0)
1067                 crypt_free_type(cd, CRYPT_BITLK);
1068         return r;
1069 }
1070
1071 static int _crypt_load_fvault2(struct crypt_device *cd)
1072 {
1073         int r;
1074
1075         r = init_crypto(cd);
1076         if (r < 0)
1077                 return r;
1078
1079         r = FVAULT2_read_metadata(cd, &cd->u.fvault2.params);
1080         if (r < 0)
1081                 goto out;
1082
1083         if (!cd->type && !(cd->type = strdup(CRYPT_FVAULT2)))
1084                 r = -ENOMEM;
1085 out:
1086         if (r < 0)
1087                 crypt_free_type(cd, CRYPT_FVAULT2);
1088         return r;
1089 }
1090
1091 int crypt_load(struct crypt_device *cd,
1092                const char *requested_type,
1093                void *params)
1094 {
1095         int r;
1096
1097         if (!cd)
1098                 return -EINVAL;
1099
1100         log_dbg(cd, "Trying to load %s crypt type from device %s.",
1101                 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1102
1103         if (!crypt_metadata_device(cd))
1104                 return -EINVAL;
1105
1106         crypt_reset_null_type(cd);
1107         cd->data_offset = 0;
1108         cd->metadata_size = 0;
1109         cd->keyslots_size = 0;
1110
1111         if (!requested_type || isLUKS1(requested_type) || isLUKS2(requested_type)) {
1112                 if (cd->type && !isLUKS1(cd->type) && !isLUKS2(cd->type)) {
1113                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
1114                         return -EINVAL;
1115                 }
1116
1117                 r = _crypt_load_luks(cd, requested_type, true, false);
1118         } else if (isVERITY(requested_type)) {
1119                 if (cd->type && !isVERITY(cd->type)) {
1120                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
1121                         return -EINVAL;
1122                 }
1123                 r = _crypt_load_verity(cd, params);
1124         } else if (isTCRYPT(requested_type)) {
1125                 if (cd->type && !isTCRYPT(cd->type)) {
1126                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
1127                         return -EINVAL;
1128                 }
1129                 r = _crypt_load_tcrypt(cd, params);
1130         } else if (isINTEGRITY(requested_type)) {
1131                 if (cd->type && !isINTEGRITY(cd->type)) {
1132                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
1133                         return -EINVAL;
1134                 }
1135                 r = _crypt_load_integrity(cd, params);
1136         } else if (isBITLK(requested_type)) {
1137                 if (cd->type && !isBITLK(cd->type)) {
1138                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
1139                         return -EINVAL;
1140                 }
1141                 r = _crypt_load_bitlk(cd);
1142         } else if (isFVAULT2(requested_type)) {
1143                 if (cd->type && !isFVAULT2(cd->type)) {
1144                         log_dbg(cd, "Context is already initialized to type %s", cd->type);
1145                         return -EINVAL;
1146                 }
1147                 r = _crypt_load_fvault2(cd);
1148         } else
1149                 return -EINVAL;
1150
1151         return r;
1152 }
1153
1154 /*
1155  * crypt_init() helpers
1156  */
1157 static int _init_by_name_crypt_none(struct crypt_device *cd)
1158 {
1159         int r;
1160         char _mode[MAX_CIPHER_LEN];
1161         struct crypt_dm_active_device dmd;
1162         struct dm_target *tgt = &dmd.segment;
1163
1164         if (cd->type || !cd->u.none.active_name)
1165                 return -EINVAL;
1166
1167         r = dm_query_device(cd, cd->u.none.active_name,
1168                         DM_ACTIVE_CRYPT_CIPHER |
1169                         DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
1170         if (r < 0)
1171                 return r;
1172         if (!single_segment(&dmd) || tgt->type != DM_CRYPT)
1173                 r = -EINVAL;
1174         if (r >= 0)
1175                 r = crypt_parse_name_and_mode(tgt->u.crypt.cipher,
1176                                               cd->u.none.cipher, NULL,
1177                                               _mode);
1178
1179         if (!r) {
1180                 r = snprintf(cd->u.none.cipher_spec, sizeof(cd->u.none.cipher_spec),
1181                          "%s-%s", cd->u.none.cipher, _mode);
1182                 if (r < 0 || (size_t)r >= sizeof(cd->u.none.cipher_spec))
1183                         r = -EINVAL;
1184                 else {
1185                         cd->u.none.cipher_mode = cd->u.none.cipher_spec + strlen(cd->u.none.cipher) + 1;
1186                         cd->u.none.key_size = tgt->u.crypt.vk->keylength;
1187                         r = 0;
1188                 }
1189         }
1190
1191         dm_targets_free(cd, &dmd);
1192         return r;
1193 }
1194
1195 static const char *LUKS_UUID(struct crypt_device *cd)
1196 {
1197         if (!cd)
1198                 return NULL;
1199         else if (isLUKS1(cd->type))
1200                 return cd->u.luks1.hdr.uuid;
1201         else if (isLUKS2(cd->type))
1202                 return cd->u.luks2.hdr.uuid;
1203
1204         return NULL;
1205 }
1206
1207 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
1208 {
1209         bool found = false;
1210         char **dep, *cipher_spec = NULL, cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1211         char deps_uuid_prefix[40], *deps[MAX_DM_DEPS+1] = {};
1212         const char *dev, *namei;
1213         int key_nums, r;
1214         struct crypt_dm_active_device dmd, dmdi = {}, dmdep = {};
1215         struct dm_target *tgt = &dmd.segment, *tgti = &dmdi.segment;
1216
1217         r = dm_query_device(cd, name,
1218                         DM_ACTIVE_DEVICE |
1219                         DM_ACTIVE_UUID |
1220                         DM_ACTIVE_CRYPT_CIPHER |
1221                         DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
1222         if (r < 0)
1223                 return r;
1224
1225         if (tgt->type != DM_CRYPT && tgt->type != DM_LINEAR) {
1226                 log_dbg(cd, "Unsupported device table detected in %s.", name);
1227                 r = -EINVAL;
1228                 goto out;
1229         }
1230
1231         r = -EINVAL;
1232
1233         if (dmd.uuid) {
1234                 r = snprintf(deps_uuid_prefix, sizeof(deps_uuid_prefix), CRYPT_SUBDEV "-%.32s", dmd.uuid + 6);
1235                 if (r < 0 || (size_t)r != (sizeof(deps_uuid_prefix) - 1))
1236                         r = -EINVAL;
1237         }
1238
1239         if (r >= 0) {
1240                 r = dm_device_deps(cd, name, deps_uuid_prefix, deps, ARRAY_SIZE(deps));
1241                 if (r)
1242                         goto out;
1243         }
1244
1245         r = crypt_parse_name_and_mode(tgt->type == DM_LINEAR ? "null" : tgt->u.crypt.cipher, cipher,
1246                                       &key_nums, cipher_mode);
1247         if (r < 0) {
1248                 log_dbg(cd, "Cannot parse cipher and mode from active device.");
1249                 goto out;
1250         }
1251
1252         dep = deps;
1253
1254         if (tgt->type == DM_CRYPT && tgt->u.crypt.integrity && (namei = device_dm_name(tgt->data_device))) {
1255                 r = dm_query_device(cd, namei, DM_ACTIVE_DEVICE, &dmdi);
1256                 if (r < 0)
1257                         goto out;
1258                 if (!single_segment(&dmdi) || tgti->type != DM_INTEGRITY) {
1259                         log_dbg(cd, "Unsupported device table detected in %s.", namei);
1260                         r = -EINVAL;
1261                         goto out;
1262                 }
1263                 if (!cd->metadata_device) {
1264                         device_free(cd, cd->device);
1265                         MOVE_REF(cd->device, tgti->data_device);
1266                 }
1267         }
1268
1269         /* do not try to lookup LUKS2 header in detached header mode */
1270         if (dmd.uuid && !cd->metadata_device && !found) {
1271                 while (*dep && !found) {
1272                         r = dm_query_device(cd, *dep, DM_ACTIVE_DEVICE, &dmdep);
1273                         if (r < 0)
1274                                 goto out;
1275
1276                         tgt = &dmdep.segment;
1277
1278                         while (tgt && !found) {
1279                                 dev = device_path(tgt->data_device);
1280                                 if (!dev) {
1281                                         tgt = tgt->next;
1282                                         continue;
1283                                 }
1284                                 if (!strstr(dev, dm_get_dir()) ||
1285                                     !crypt_string_in(dev + strlen(dm_get_dir()) + 1, deps, ARRAY_SIZE(deps))) {
1286                                         device_free(cd, cd->device);
1287                                         MOVE_REF(cd->device, tgt->data_device);
1288                                         found = true;
1289                                 }
1290                                 tgt = tgt->next;
1291                         }
1292                         dep++;
1293                         dm_targets_free(cd, &dmdep);
1294                 }
1295         }
1296
1297         if (asprintf(&cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
1298                 cipher_spec = NULL;
1299                 r = -ENOMEM;
1300                 goto out;
1301         }
1302
1303         tgt = &dmd.segment;
1304         r = 0;
1305
1306         if (isPLAIN(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) {
1307                 cd->u.plain.hdr.hash = NULL; /* no way to get this */
1308                 cd->u.plain.hdr.offset = tgt->u.crypt.offset;
1309                 cd->u.plain.hdr.skip = tgt->u.crypt.iv_offset;
1310                 cd->u.plain.hdr.sector_size = tgt->u.crypt.sector_size;
1311                 cd->u.plain.key_size = tgt->u.crypt.vk->keylength;
1312                 cd->u.plain.cipher = strdup(cipher);
1313                 MOVE_REF(cd->u.plain.cipher_spec, cipher_spec);
1314                 cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1;
1315         } else if (isLOOPAES(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) {
1316                 cd->u.loopaes.hdr.offset = tgt->u.crypt.offset;
1317                 cd->u.loopaes.cipher = strdup(cipher);
1318                 MOVE_REF(cd->u.loopaes.cipher_spec, cipher_spec);
1319                 cd->u.loopaes.cipher_mode = cd->u.loopaes.cipher_spec + strlen(cipher) + 1;
1320                 /* version 3 uses last key for IV */
1321                 if (tgt->u.crypt.vk->keylength % key_nums)
1322                         key_nums++;
1323                 cd->u.loopaes.key_size = tgt->u.crypt.vk->keylength / key_nums;
1324         } else if (isLUKS1(cd->type) || isLUKS2(cd->type)) {
1325                 if (crypt_metadata_device(cd)) {
1326                         r = _crypt_load_luks(cd, cd->type, true, false);
1327                         if (r < 0) {
1328                                 log_dbg(cd, "LUKS device header does not match active device.");
1329                                 crypt_set_null_type(cd);
1330                                 device_close(cd, cd->metadata_device);
1331                                 device_close(cd, cd->device);
1332                                 r = 0;
1333                                 goto out;
1334                         }
1335                         /* check whether UUIDs match each other */
1336                         r = crypt_uuid_cmp(dmd.uuid, LUKS_UUID(cd));
1337                         if (r < 0) {
1338                                 log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s",
1339                                         LUKS_UUID(cd), dmd.uuid);
1340                                 crypt_free_type(cd, NULL);
1341                                 r = 0;
1342                                 goto out;
1343                         }
1344                 } else {
1345                         log_dbg(cd, "LUKS device header not available.");
1346                         crypt_set_null_type(cd);
1347                         r = 0;
1348                 }
1349         } else if (isTCRYPT(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) {
1350                 r = TCRYPT_init_by_name(cd, name, dmd.uuid, tgt, &cd->device,
1351                                         &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
1352         } else if (isBITLK(cd->type)) {
1353                 r = _crypt_load_bitlk(cd);
1354                 if (r < 0) {
1355                         log_dbg(cd, "BITLK device header not available.");
1356                         crypt_set_null_type(cd);
1357                         r = 0;
1358                 }
1359         } else if (isFVAULT2(cd->type)) {
1360                 r = _crypt_load_fvault2(cd);
1361                 if (r < 0) {
1362                         log_dbg(cd, "FVAULT2 device header not available.");
1363                         crypt_set_null_type(cd);
1364                         r = 0;
1365                 }
1366         }
1367 out:
1368         dm_targets_free(cd, &dmd);
1369         dm_targets_free(cd, &dmdi);
1370         dm_targets_free(cd, &dmdep);
1371         free(CONST_CAST(void*)dmd.uuid);
1372         free(cipher_spec);
1373         dep = deps;
1374         while (*dep)
1375                 free(*dep++);
1376         return r;
1377 }
1378
1379 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
1380 {
1381         struct crypt_dm_active_device dmd;
1382         struct dm_target *tgt = &dmd.segment;
1383         int r;
1384
1385         r = dm_query_device(cd, name,
1386                                 DM_ACTIVE_DEVICE |
1387                                 DM_ACTIVE_VERITY_HASH_DEVICE |
1388                                 DM_ACTIVE_VERITY_ROOT_HASH |
1389                                 DM_ACTIVE_VERITY_PARAMS, &dmd);
1390         if (r < 0)
1391                 return r;
1392         if (!single_segment(&dmd) || tgt->type != DM_VERITY) {
1393                 log_dbg(cd, "Unsupported device table detected in %s.", name);
1394                 r = -EINVAL;
1395                 goto out;
1396         }
1397         if (r > 0)
1398                 r = 0;
1399
1400         if (isVERITY(cd->type)) {
1401                 cd->u.verity.uuid = NULL; // FIXME
1402                 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
1403                 cd->u.verity.hdr.data_size = tgt->u.verity.vp->data_size;
1404                 cd->u.verity.root_hash_size = tgt->u.verity.root_hash_size;
1405                 MOVE_REF(cd->u.verity.hdr.hash_name, tgt->u.verity.vp->hash_name);
1406                 cd->u.verity.hdr.data_device = NULL;
1407                 cd->u.verity.hdr.hash_device = NULL;
1408                 cd->u.verity.hdr.data_block_size = tgt->u.verity.vp->data_block_size;
1409                 cd->u.verity.hdr.hash_block_size = tgt->u.verity.vp->hash_block_size;
1410                 cd->u.verity.hdr.hash_area_offset = tgt->u.verity.hash_offset;
1411                 cd->u.verity.hdr.fec_area_offset = tgt->u.verity.fec_offset;
1412                 cd->u.verity.hdr.hash_type = tgt->u.verity.vp->hash_type;
1413                 cd->u.verity.hdr.flags = tgt->u.verity.vp->flags;
1414                 cd->u.verity.hdr.salt_size = tgt->u.verity.vp->salt_size;
1415                 MOVE_REF(cd->u.verity.hdr.salt, tgt->u.verity.vp->salt);
1416                 MOVE_REF(cd->u.verity.hdr.fec_device, tgt->u.verity.vp->fec_device);
1417                 cd->u.verity.hdr.fec_roots = tgt->u.verity.vp->fec_roots;
1418                 MOVE_REF(cd->u.verity.fec_device, tgt->u.verity.fec_device);
1419                 MOVE_REF(cd->metadata_device, tgt->u.verity.hash_device);
1420                 MOVE_REF(cd->u.verity.root_hash, tgt->u.verity.root_hash);
1421         }
1422 out:
1423         dm_targets_free(cd, &dmd);
1424         return r;
1425 }
1426
1427 static int _init_by_name_integrity(struct crypt_device *cd, const char *name)
1428 {
1429         struct crypt_dm_active_device dmd;
1430         struct dm_target *tgt = &dmd.segment;
1431         int r;
1432
1433         r = dm_query_device(cd, name, DM_ACTIVE_DEVICE |
1434                                       DM_ACTIVE_CRYPT_KEY |
1435                                       DM_ACTIVE_CRYPT_KEYSIZE |
1436                                       DM_ACTIVE_INTEGRITY_PARAMS, &dmd);
1437         if (r < 0)
1438                 return r;
1439         if (!single_segment(&dmd) || tgt->type != DM_INTEGRITY) {
1440                 log_dbg(cd, "Unsupported device table detected in %s.", name);
1441                 r = -EINVAL;
1442                 goto out;
1443         }
1444         if (r > 0)
1445                 r = 0;
1446
1447         if (isINTEGRITY(cd->type)) {
1448                 cd->u.integrity.params.tag_size = tgt->u.integrity.tag_size;
1449                 cd->u.integrity.params.sector_size = tgt->u.integrity.sector_size;
1450                 cd->u.integrity.params.journal_size = tgt->u.integrity.journal_size;
1451                 cd->u.integrity.params.journal_watermark = tgt->u.integrity.journal_watermark;
1452                 cd->u.integrity.params.journal_commit_time = tgt->u.integrity.journal_commit_time;
1453                 cd->u.integrity.params.interleave_sectors = tgt->u.integrity.interleave_sectors;
1454                 cd->u.integrity.params.buffer_sectors = tgt->u.integrity.buffer_sectors;
1455                 MOVE_REF(cd->u.integrity.params.integrity, tgt->u.integrity.integrity);
1456                 MOVE_REF(cd->u.integrity.params.journal_integrity, tgt->u.integrity.journal_integrity);
1457                 MOVE_REF(cd->u.integrity.params.journal_crypt, tgt->u.integrity.journal_crypt);
1458
1459                 if (tgt->u.integrity.vk)
1460                         cd->u.integrity.params.integrity_key_size = tgt->u.integrity.vk->keylength;
1461                 if (tgt->u.integrity.journal_integrity_key)
1462                         cd->u.integrity.params.journal_integrity_key_size = tgt->u.integrity.journal_integrity_key->keylength;
1463                 if (tgt->u.integrity.journal_crypt_key)
1464                         cd->u.integrity.params.integrity_key_size = tgt->u.integrity.journal_crypt_key->keylength;
1465                 MOVE_REF(cd->metadata_device, tgt->u.integrity.meta_device);
1466         }
1467 out:
1468         dm_targets_free(cd, &dmd);
1469         return r;
1470 }
1471
1472 int crypt_init_by_name_and_header(struct crypt_device **cd,
1473                                   const char *name,
1474                                   const char *header_device)
1475 {
1476         crypt_status_info ci;
1477         struct crypt_dm_active_device dmd;
1478         struct dm_target *tgt = &dmd.segment;
1479         int r;
1480
1481         if (!cd || !name)
1482                 return -EINVAL;
1483
1484         log_dbg(NULL, "Allocating crypt device context by device %s.", name);
1485
1486         ci = crypt_status(NULL, name);
1487         if (ci == CRYPT_INVALID)
1488                 return -ENODEV;
1489
1490         if (ci < CRYPT_ACTIVE) {
1491                 log_err(NULL, _("Device %s is not active."), name);
1492                 return -ENODEV;
1493         }
1494
1495         r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
1496         if (r < 0)
1497                 return r;
1498
1499         *cd = NULL;
1500
1501         if (header_device) {
1502                 r = crypt_init(cd, header_device);
1503         } else {
1504                 r = crypt_init(cd, device_path(tgt->data_device));
1505
1506                 /* Underlying device disappeared but mapping still active */
1507                 if (!tgt->data_device || r == -ENOTBLK)
1508                         log_verbose(NULL, _("Underlying device for crypt device %s disappeared."),
1509                                     name);
1510
1511                 /* Underlying device is not readable but crypt mapping exists */
1512                 if (r == -ENOTBLK)
1513                         r = crypt_init(cd, NULL);
1514         }
1515
1516         if (r < 0)
1517                 goto out;
1518
1519         if (dmd.uuid) {
1520                 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
1521                         (*cd)->type = strdup(CRYPT_PLAIN);
1522                 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
1523                         (*cd)->type = strdup(CRYPT_LOOPAES);
1524                 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
1525                         (*cd)->type = strdup(CRYPT_LUKS1);
1526                 else if (!strncmp(CRYPT_LUKS2, dmd.uuid, sizeof(CRYPT_LUKS2)-1))
1527                         (*cd)->type = strdup(CRYPT_LUKS2);
1528                 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
1529                         (*cd)->type = strdup(CRYPT_VERITY);
1530                 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
1531                         (*cd)->type = strdup(CRYPT_TCRYPT);
1532                 else if (!strncmp(CRYPT_INTEGRITY, dmd.uuid, sizeof(CRYPT_INTEGRITY)-1))
1533                         (*cd)->type = strdup(CRYPT_INTEGRITY);
1534                 else if (!strncmp(CRYPT_BITLK, dmd.uuid, sizeof(CRYPT_BITLK)-1))
1535                         (*cd)->type = strdup(CRYPT_BITLK);
1536                 else if (!strncmp(CRYPT_FVAULT2, dmd.uuid, sizeof(CRYPT_FVAULT2)-1))
1537                         (*cd)->type = strdup(CRYPT_FVAULT2);
1538                 else
1539                         log_dbg(NULL, "Unknown UUID set, some parameters are not set.");
1540         } else
1541                 log_dbg(NULL, "Active device has no UUID set, some parameters are not set.");
1542
1543         if (header_device) {
1544                 r = crypt_set_data_device(*cd, device_path(tgt->data_device));
1545                 if (r < 0)
1546                         goto out;
1547         }
1548
1549         /* Try to initialize basic parameters from active device */
1550
1551         if (tgt->type == DM_CRYPT || tgt->type == DM_LINEAR)
1552                 r = _init_by_name_crypt(*cd, name);
1553         else if (tgt->type == DM_VERITY)
1554                 r = _init_by_name_verity(*cd, name);
1555         else if (tgt->type == DM_INTEGRITY)
1556                 r = _init_by_name_integrity(*cd, name);
1557 out:
1558         if (r < 0) {
1559                 crypt_free(*cd);
1560                 *cd = NULL;
1561         } else if (!(*cd)->type) {
1562                 /* For anonymous device (no header found) remember initialized name */
1563                 (*cd)->u.none.active_name = strdup(name);
1564         }
1565
1566         free(CONST_CAST(void*)dmd.uuid);
1567         dm_targets_free(NULL, &dmd);
1568         return r;
1569 }
1570
1571 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1572 {
1573         return crypt_init_by_name_and_header(cd, name, NULL);
1574 }
1575
1576 /*
1577  * crypt_format() helpers
1578  */
1579 static int _crypt_format_plain(struct crypt_device *cd,
1580                                const char *cipher,
1581                                const char *cipher_mode,
1582                                const char *uuid,
1583                                size_t volume_key_size,
1584                                struct crypt_params_plain *params)
1585 {
1586         unsigned int sector_size = params ? params->sector_size : SECTOR_SIZE;
1587         uint64_t dev_size;
1588
1589         if (!cipher || !cipher_mode) {
1590                 log_err(cd, _("Invalid plain crypt parameters."));
1591                 return -EINVAL;
1592         }
1593
1594         if (volume_key_size > 1024) {
1595                 log_err(cd, _("Invalid key size."));
1596                 return -EINVAL;
1597         }
1598
1599         if (uuid) {
1600                 log_err(cd, _("UUID is not supported for this crypt type."));
1601                 return -EINVAL;
1602         }
1603
1604         if (cd->metadata_device) {
1605                 log_err(cd, _("Detached metadata device is not supported for this crypt type."));
1606                 return -EINVAL;
1607         }
1608
1609         /* For compatibility with old params structure */
1610         if (!sector_size)
1611                 sector_size = SECTOR_SIZE;
1612
1613         if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE ||
1614             NOTPOW2(sector_size)) {
1615                 log_err(cd, _("Unsupported encryption sector size."));
1616                 return -EINVAL;
1617         }
1618
1619         if (sector_size > SECTOR_SIZE && !device_size(cd->device, &dev_size)) {
1620                 if (params && params->offset)
1621                         dev_size -= (params->offset * SECTOR_SIZE);
1622                 if (dev_size % sector_size) {
1623                         log_err(cd, _("Device size is not aligned to requested sector size."));
1624                         return -EINVAL;
1625                 }
1626                 device_set_block_size(crypt_data_device(cd), sector_size);
1627         }
1628
1629         if (!(cd->type = strdup(CRYPT_PLAIN)))
1630                 return -ENOMEM;
1631
1632         cd->u.plain.key_size = volume_key_size;
1633         cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1634         if (!cd->volume_key)
1635                 return -ENOMEM;
1636
1637         if (asprintf(&cd->u.plain.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
1638                 cd->u.plain.cipher_spec = NULL;
1639                 return -ENOMEM;
1640         }
1641         cd->u.plain.cipher = strdup(cipher);
1642         cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1;
1643
1644         if (params && params->hash)
1645                 cd->u.plain.hdr.hash = strdup(params->hash);
1646
1647         cd->u.plain.hdr.offset = params ? params->offset : 0;
1648         cd->u.plain.hdr.skip = params ? params->skip : 0;
1649         cd->u.plain.hdr.size = params ? params->size : 0;
1650         cd->u.plain.hdr.sector_size = sector_size;
1651
1652         if (!cd->u.plain.cipher)
1653                 return -ENOMEM;
1654
1655         return 0;
1656 }
1657
1658 static int _crypt_format_luks1(struct crypt_device *cd,
1659                                const char *cipher,
1660                                const char *cipher_mode,
1661                                const char *uuid,
1662                                const char *volume_key,
1663                                size_t volume_key_size,
1664                                struct crypt_params_luks1 *params)
1665 {
1666         int r;
1667         unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1668         unsigned long alignment_offset = 0;
1669         uint64_t dev_size;
1670
1671         if (!cipher || !cipher_mode)
1672                 return -EINVAL;
1673
1674         if (!crypt_metadata_device(cd)) {
1675                 log_err(cd, _("Can't format LUKS without device."));
1676                 return -EINVAL;
1677         }
1678
1679         if (params && cd->data_offset && params->data_alignment &&
1680            (cd->data_offset % params->data_alignment)) {
1681                 log_err(cd, _("Requested data alignment is not compatible with data offset."));
1682                 return -EINVAL;
1683         }
1684
1685         if (!(cd->type = strdup(CRYPT_LUKS1)))
1686                 return -ENOMEM;
1687
1688         if (volume_key)
1689                 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1690                                                       volume_key);
1691         else
1692                 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1693
1694         if (!cd->volume_key)
1695                 return -ENOMEM;
1696
1697         if (verify_pbkdf_params(cd, &cd->pbkdf)) {
1698                 r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1);
1699                 if (r)
1700                         return r;
1701         }
1702
1703         if (params && params->hash && strcmp(params->hash, cd->pbkdf.hash)) {
1704                 free(CONST_CAST(void*)cd->pbkdf.hash);
1705                 cd->pbkdf.hash = strdup(params->hash);
1706                 if (!cd->pbkdf.hash)
1707                         return -ENOMEM;
1708         }
1709
1710         if (params && params->data_device) {
1711                 if (!cd->metadata_device)
1712                         cd->metadata_device = cd->device;
1713                 else
1714                         device_free(cd, cd->device);
1715                 cd->device = NULL;
1716                 if (device_alloc(cd, &cd->device, params->data_device) < 0)
1717                         return -ENOMEM;
1718         }
1719
1720         if (params && cd->metadata_device) {
1721                 /* For detached header the alignment is used directly as data offset */
1722                 if (!cd->data_offset)
1723                         cd->data_offset = params->data_alignment;
1724                 required_alignment = params->data_alignment * SECTOR_SIZE;
1725         } else if (params && params->data_alignment) {
1726                 required_alignment = params->data_alignment * SECTOR_SIZE;
1727         } else
1728                 device_topology_alignment(cd, cd->device,
1729                                        &required_alignment,
1730                                        &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1731
1732         r = LUKS_check_cipher(cd, volume_key_size, cipher, cipher_mode);
1733         if (r < 0)
1734                 return r;
1735
1736         r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
1737                                cd->pbkdf.hash, uuid,
1738                                cd->data_offset * SECTOR_SIZE,
1739                                alignment_offset, required_alignment, cd);
1740         if (r < 0)
1741                 return r;
1742
1743         r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
1744         if (r < 0)
1745                 return r;
1746
1747
1748         if (asprintf(&cd->u.luks1.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
1749                 cd->u.luks1.cipher_spec = NULL;
1750                 return -ENOMEM;
1751         }
1752
1753         r = LUKS_wipe_header_areas(&cd->u.luks1.hdr, cd);
1754         if (r < 0) {
1755                 free(cd->u.luks1.cipher_spec);
1756                 log_err(cd, _("Cannot wipe header on device %s."),
1757                         mdata_device_path(cd));
1758                 return r;
1759         }
1760
1761         r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1762         if (r) {
1763                 free(cd->u.luks1.cipher_spec);
1764                 return r;
1765         }
1766
1767         if (!device_size(crypt_data_device(cd), &dev_size) &&
1768             dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
1769                 log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
1770                               device_path(crypt_data_device(cd)));
1771
1772         return 0;
1773 }
1774
1775 static int _crypt_format_luks2(struct crypt_device *cd,
1776                                const char *cipher,
1777                                const char *cipher_mode,
1778                                const char *uuid,
1779                                const char *volume_key,
1780                                size_t volume_key_size,
1781                                struct crypt_params_luks2 *params,
1782                                bool sector_size_autodetect)
1783 {
1784         int r, integrity_key_size = 0;
1785         unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1786         unsigned long alignment_offset = 0;
1787         unsigned int sector_size;
1788         const char *integrity = params ? params->integrity : NULL;
1789         uint64_t dev_size;
1790         uint32_t dmc_flags;
1791
1792         cd->u.luks2.hdr.jobj = NULL;
1793         cd->u.luks2.keyslot_cipher = NULL;
1794
1795         if (!cipher || !cipher_mode)
1796                 return -EINVAL;
1797
1798         if (!crypt_metadata_device(cd)) {
1799                 log_err(cd, _("Can't format LUKS without device."));
1800                 return -EINVAL;
1801         }
1802
1803         if (params && cd->data_offset && params->data_alignment &&
1804            (cd->data_offset % params->data_alignment)) {
1805                 log_err(cd, _("Requested data alignment is not compatible with data offset."));
1806                 return -EINVAL;
1807         }
1808
1809         if (params && params->sector_size)
1810                 sector_size_autodetect = false;
1811
1812         if (params && params->data_device) {
1813                 if (!cd->metadata_device)
1814                         cd->metadata_device = cd->device;
1815                 else
1816                         device_free(cd, cd->device);
1817                 cd->device = NULL;
1818                 if (device_alloc(cd, &cd->device, params->data_device) < 0)
1819                         return -ENOMEM;
1820         }
1821
1822         if (sector_size_autodetect) {
1823                 sector_size = device_optimal_encryption_sector_size(cd, crypt_data_device(cd));
1824                 log_dbg(cd, "Auto-detected optimal encryption sector size for device %s is %d bytes.",
1825                         device_path(crypt_data_device(cd)), sector_size);
1826         } else
1827                 sector_size = params ? params->sector_size : SECTOR_SIZE;
1828
1829         if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE ||
1830             NOTPOW2(sector_size)) {
1831                 log_err(cd, _("Unsupported encryption sector size."));
1832                 return -EINVAL;
1833         }
1834         if (sector_size != SECTOR_SIZE && !dm_flags(cd, DM_CRYPT, &dmc_flags) &&
1835             !(dmc_flags & DM_SECTOR_SIZE_SUPPORTED)) {
1836                 if (sector_size_autodetect) {
1837                         log_dbg(cd, "dm-crypt does not support encryption sector size option. Reverting to 512 bytes.");
1838                         sector_size = SECTOR_SIZE;
1839                 } else
1840                         log_std(cd, _("WARNING: The device activation will fail, dm-crypt is missing "
1841                                       "support for requested encryption sector size.\n"));
1842         }
1843
1844         if (integrity) {
1845                 if (params->integrity_params) {
1846                         /* Standalone dm-integrity must not be used */
1847                         if (params->integrity_params->integrity ||
1848                             params->integrity_params->integrity_key_size)
1849                                 return -EINVAL;
1850                         /* FIXME: journal encryption and MAC is here not yet supported */
1851                         if (params->integrity_params->journal_crypt ||
1852                         params->integrity_params->journal_integrity)
1853                                 return -ENOTSUP;
1854                 }
1855                 if (!INTEGRITY_tag_size(integrity, cipher, cipher_mode)) {
1856                         if (!strcmp(integrity, "none"))
1857                                 integrity = NULL;
1858                         else
1859                                 return -EINVAL;
1860                 }
1861                 integrity_key_size = INTEGRITY_key_size(integrity);
1862                 if ((integrity_key_size < 0) || (integrity_key_size >= (int)volume_key_size)) {
1863                         log_err(cd, _("Volume key is too small for encryption with integrity extensions."));
1864                         return -EINVAL;
1865                 }
1866         }
1867
1868         r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
1869         if (r < 0)
1870                 return r;
1871
1872         if (!(cd->type = strdup(CRYPT_LUKS2)))
1873                 return -ENOMEM;
1874
1875         if (volume_key)
1876                 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1877                                                       volume_key);
1878         else
1879                 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1880
1881         if (!cd->volume_key)
1882                 return -ENOMEM;
1883
1884         if (params && params->pbkdf)
1885                 r = crypt_set_pbkdf_type(cd, params->pbkdf);
1886         else if (verify_pbkdf_params(cd, &cd->pbkdf))
1887                 r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2);
1888
1889         if (r < 0)
1890                 return r;
1891
1892         if (params && cd->metadata_device) {
1893                 /* For detached header the alignment is used directly as data offset */
1894                 if (!cd->data_offset)
1895                         cd->data_offset = params->data_alignment;
1896                 required_alignment = params->data_alignment * SECTOR_SIZE;
1897         } else if (params && params->data_alignment) {
1898                 required_alignment = params->data_alignment * SECTOR_SIZE;
1899         } else
1900                 device_topology_alignment(cd, cd->device,
1901                                        &required_alignment,
1902                                        &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1903
1904         r = device_size(crypt_data_device(cd), &dev_size);
1905         if (r < 0)
1906                 goto out;
1907
1908         if (sector_size_autodetect) {
1909                 if (cd->data_offset && MISALIGNED(cd->data_offset, sector_size)) {
1910                         log_dbg(cd, "Data offset not aligned to sector size. Reverting to 512 bytes.");
1911                         sector_size = SECTOR_SIZE;
1912                 } else if (MISALIGNED(dev_size - (uint64_t)required_alignment - (uint64_t)alignment_offset, sector_size)) {
1913                         /* underflow does not affect misalignment checks */
1914                         log_dbg(cd, "Device size is not aligned to sector size. Reverting to 512 bytes.");
1915                         sector_size = SECTOR_SIZE;
1916                 }
1917         }
1918
1919         /* FIXME: allow this later also for normal ciphers (check AF_ALG availability. */
1920         if (integrity && !integrity_key_size) {
1921                 r = crypt_cipher_check_kernel(cipher, cipher_mode, integrity, volume_key_size);
1922                 if (r < 0) {
1923                         log_err(cd, _("Cipher %s-%s (key size %zd bits) is not available."),
1924                                 cipher, cipher_mode, volume_key_size * 8);
1925                         goto out;
1926                 }
1927         }
1928
1929         if ((!integrity || integrity_key_size) && !crypt_cipher_wrapped_key(cipher, cipher_mode) &&
1930             !INTEGRITY_tag_size(NULL, cipher, cipher_mode)) {
1931                 r = LUKS_check_cipher(cd, volume_key_size - integrity_key_size,
1932                                       cipher, cipher_mode);
1933                 if (r < 0)
1934                         goto out;
1935         }
1936
1937         r = LUKS2_generate_hdr(cd, &cd->u.luks2.hdr, cd->volume_key,
1938                                cipher, cipher_mode,
1939                                integrity, uuid,
1940                                sector_size,
1941                                cd->data_offset * SECTOR_SIZE,
1942                                alignment_offset,
1943                                required_alignment,
1944                                cd->metadata_size, cd->keyslots_size);
1945         if (r < 0)
1946                 goto out;
1947
1948         if (cd->metadata_size && (cd->metadata_size != LUKS2_metadata_size(&cd->u.luks2.hdr)))
1949                 log_std(cd, _("WARNING: LUKS2 metadata size changed to %" PRIu64 " bytes.\n"),
1950                         LUKS2_metadata_size(&cd->u.luks2.hdr));
1951
1952         if (cd->keyslots_size && (cd->keyslots_size != LUKS2_keyslots_size(&cd->u.luks2.hdr)))
1953                 log_std(cd, _("WARNING: LUKS2 keyslots area size changed to %" PRIu64 " bytes.\n"),
1954                         LUKS2_keyslots_size(&cd->u.luks2.hdr));
1955
1956         if (!integrity && sector_size > SECTOR_SIZE) {
1957                 dev_size -= (crypt_get_data_offset(cd) * SECTOR_SIZE);
1958                 if (dev_size % sector_size) {
1959                         log_err(cd, _("Device size is not aligned to requested sector size."));
1960                         r = -EINVAL;
1961                         goto out;
1962                 }
1963         }
1964
1965         if (params && (params->label || params->subsystem)) {
1966                 r = LUKS2_hdr_labels(cd, &cd->u.luks2.hdr,
1967                                      params->label, params->subsystem, 0);
1968                 if (r < 0)
1969                         goto out;
1970         }
1971
1972         device_set_block_size(crypt_data_device(cd), sector_size);
1973
1974         r = LUKS2_wipe_header_areas(cd, &cd->u.luks2.hdr, cd->metadata_device != NULL);
1975         if (r < 0) {
1976                 log_err(cd, _("Cannot wipe header on device %s."),
1977                         mdata_device_path(cd));
1978                 if (dev_size < LUKS2_hdr_and_areas_size(&cd->u.luks2.hdr))
1979                         log_err(cd, _("Device %s is too small."), device_path(crypt_metadata_device(cd)));
1980                 goto out;
1981         }
1982
1983         /* Wipe integrity superblock and create integrity superblock */
1984         if (crypt_get_integrity_tag_size(cd)) {
1985                 r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_ZERO,
1986                                       crypt_get_data_offset(cd) * SECTOR_SIZE,
1987                                       8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
1988                 if (r < 0) {
1989                         if (r == -EBUSY)
1990                                 log_err(cd, _("Cannot format device %s in use."),
1991                                         data_device_path(cd));
1992                         else if (r == -EACCES) {
1993                                 log_err(cd, _("Cannot format device %s, permission denied."),
1994                                         data_device_path(cd));
1995                                 r = -EINVAL;
1996                         } else
1997                                 log_err(cd, _("Cannot wipe header on device %s."),
1998                                         data_device_path(cd));
1999
2000                         goto out;
2001                 }
2002
2003                 r = INTEGRITY_format(cd, params ? params->integrity_params : NULL, NULL, NULL);
2004                 if (r)
2005                         log_err(cd, _("Cannot format integrity for device %s."),
2006                                 data_device_path(cd));
2007         }
2008
2009         if (r < 0)
2010                 goto out;
2011
2012         /* override sequence id check with format */
2013         r = LUKS2_hdr_write_force(cd, &cd->u.luks2.hdr);
2014         if (r < 0) {
2015                 if (r == -EBUSY)
2016                         log_err(cd, _("Cannot format device %s in use."),
2017                                 mdata_device_path(cd));
2018                 else if (r == -EACCES) {
2019                         log_err(cd, _("Cannot format device %s, permission denied."),
2020                                 mdata_device_path(cd));
2021                         r = -EINVAL;
2022                 } else
2023                         log_err(cd, _("Cannot format device %s."),
2024                                 mdata_device_path(cd));
2025         }
2026
2027 out:
2028         if (r) {
2029                 LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
2030                 return r;
2031         }
2032
2033         /* Device size can be larger now if it is a file container */
2034         if (!device_size(crypt_data_device(cd), &dev_size) &&
2035             dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
2036                 log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
2037                               device_path(crypt_data_device(cd)));
2038
2039         return 0;
2040 }
2041
2042 static int _crypt_format_loopaes(struct crypt_device *cd,
2043                                  const char *cipher,
2044                                  const char *uuid,
2045                                  size_t volume_key_size,
2046                                  struct crypt_params_loopaes *params)
2047 {
2048         if (!crypt_metadata_device(cd)) {
2049                 log_err(cd, _("Can't format LOOPAES without device."));
2050                 return -EINVAL;
2051         }
2052
2053         if (volume_key_size > 1024) {
2054                 log_err(cd, _("Invalid key size."));
2055                 return -EINVAL;
2056         }
2057
2058         if (uuid) {
2059                 log_err(cd, _("UUID is not supported for this crypt type."));
2060                 return -EINVAL;
2061         }
2062
2063         if (cd->metadata_device) {
2064                 log_err(cd, _("Detached metadata device is not supported for this crypt type."));
2065                 return -EINVAL;
2066         }
2067
2068         if (!(cd->type = strdup(CRYPT_LOOPAES)))
2069                 return -ENOMEM;
2070
2071         cd->u.loopaes.key_size = volume_key_size;
2072
2073         cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
2074
2075         if (params && params->hash)
2076                 cd->u.loopaes.hdr.hash = strdup(params->hash);
2077
2078         cd->u.loopaes.hdr.offset = params ? params->offset : 0;
2079         cd->u.loopaes.hdr.skip = params ? params->skip : 0;
2080
2081         return 0;
2082 }
2083
2084 static int _crypt_format_verity(struct crypt_device *cd,
2085                                  const char *uuid,
2086                                  struct crypt_params_verity *params)
2087 {
2088         int r = 0, hash_size;
2089         uint64_t data_device_size, hash_blocks_size;
2090         struct device *fec_device = NULL;
2091         char *fec_device_path = NULL, *hash_name = NULL, *root_hash = NULL, *salt = NULL;
2092
2093         if (!crypt_metadata_device(cd)) {
2094                 log_err(cd, _("Can't format VERITY without device."));
2095                 return -EINVAL;
2096         }
2097
2098         if (!params)
2099                 return -EINVAL;
2100
2101         if (!params->data_device && !cd->metadata_device)
2102                 return -EINVAL;
2103
2104         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
2105                 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
2106                 return -EINVAL;
2107         }
2108
2109         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
2110             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
2111                 log_err(cd, _("Unsupported VERITY block size."));
2112                 return -EINVAL;
2113         }
2114
2115         if (MISALIGNED_512(params->hash_area_offset)) {
2116                 log_err(cd, _("Unsupported VERITY hash offset."));
2117                 return -EINVAL;
2118         }
2119
2120         if (MISALIGNED_512(params->fec_area_offset)) {
2121                 log_err(cd, _("Unsupported VERITY FEC offset."));
2122                 return -EINVAL;
2123         }
2124
2125         if (!(cd->type = strdup(CRYPT_VERITY)))
2126                 return -ENOMEM;
2127
2128         if (params->data_device) {
2129                 r = crypt_set_data_device(cd, params->data_device);
2130                 if (r)
2131                         return r;
2132         }
2133
2134         if (!params->data_size) {
2135                 r = device_size(cd->device, &data_device_size);
2136                 if (r < 0)
2137                         return r;
2138
2139                 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
2140         } else
2141                 cd->u.verity.hdr.data_size = params->data_size;
2142
2143         if (device_is_identical(crypt_metadata_device(cd), crypt_data_device(cd)) > 0 &&
2144            (cd->u.verity.hdr.data_size * params->data_block_size) > params->hash_area_offset) {
2145                 log_err(cd, _("Data area overlaps with hash area."));
2146                 return -EINVAL;
2147         }
2148
2149         hash_size = crypt_hash_size(params->hash_name);
2150         if (hash_size <= 0) {
2151                 log_err(cd, _("Hash algorithm %s not supported."),
2152                         params->hash_name);
2153                 return -EINVAL;
2154         }
2155         cd->u.verity.root_hash_size = hash_size;
2156
2157         if (params->fec_device) {
2158                 fec_device_path = strdup(params->fec_device);
2159                 if (!fec_device_path)
2160                         return -ENOMEM;
2161                 r = device_alloc(cd, &fec_device, params->fec_device);
2162                 if (r < 0) {
2163                         r = -ENOMEM;
2164                         goto out;
2165                 }
2166
2167                 hash_blocks_size = VERITY_hash_blocks(cd, params) * params->hash_block_size;
2168                 if (device_is_identical(crypt_metadata_device(cd), fec_device) > 0 &&
2169                     (params->hash_area_offset + hash_blocks_size) > params->fec_area_offset) {
2170                         log_err(cd, _("Hash area overlaps with FEC area."));
2171                         r = -EINVAL;
2172                         goto out;
2173                 }
2174
2175                 if (device_is_identical(crypt_data_device(cd), fec_device) > 0 &&
2176                     (cd->u.verity.hdr.data_size * params->data_block_size) > params->fec_area_offset) {
2177                         log_err(cd, _("Data area overlaps with FEC area."));
2178                         r = -EINVAL;
2179                         goto out;
2180                 }
2181         }
2182
2183         root_hash = malloc(cd->u.verity.root_hash_size);
2184         hash_name = strdup(params->hash_name);
2185         salt = malloc(params->salt_size);
2186
2187         if (!root_hash || !hash_name || !salt) {
2188                 r = -ENOMEM;
2189                 goto out;
2190         }
2191
2192         cd->u.verity.hdr.flags = params->flags;
2193         cd->u.verity.root_hash = root_hash;
2194         cd->u.verity.hdr.hash_name = hash_name;
2195         cd->u.verity.hdr.data_device = NULL;
2196         cd->u.verity.fec_device = fec_device;
2197         cd->u.verity.hdr.fec_device = fec_device_path;
2198         cd->u.verity.hdr.fec_roots = params->fec_roots;
2199         cd->u.verity.hdr.data_block_size = params->data_block_size;
2200         cd->u.verity.hdr.hash_block_size = params->hash_block_size;
2201         cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
2202         cd->u.verity.hdr.fec_area_offset = params->fec_area_offset;
2203         cd->u.verity.hdr.hash_type = params->hash_type;
2204         cd->u.verity.hdr.flags = params->flags;
2205         cd->u.verity.hdr.salt_size = params->salt_size;
2206         cd->u.verity.hdr.salt = salt;
2207
2208         if (params->salt)
2209                 memcpy(salt, params->salt, params->salt_size);
2210         else
2211                 r = crypt_random_get(cd, salt, params->salt_size, CRYPT_RND_SALT);
2212         if (r)
2213                 goto out;
2214
2215         if (params->flags & CRYPT_VERITY_CREATE_HASH) {
2216                 r = VERITY_create(cd, &cd->u.verity.hdr,
2217                                   cd->u.verity.root_hash, cd->u.verity.root_hash_size);
2218                 if (!r && params->fec_device)
2219                         r = VERITY_FEC_process(cd, &cd->u.verity.hdr, cd->u.verity.fec_device, 0, NULL);
2220                 if (r)
2221                         goto out;
2222         }
2223
2224         if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
2225                 if (uuid) {
2226                         if (!(cd->u.verity.uuid = strdup(uuid)))
2227                                 r = -ENOMEM;
2228                 } else
2229                         r = VERITY_UUID_generate(&cd->u.verity.uuid);
2230
2231                 if (!r)
2232                         r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
2233                                             cd->u.verity.uuid,
2234                                             &cd->u.verity.hdr);
2235         }
2236
2237 out:
2238         if (r) {
2239                 device_free(cd, fec_device);
2240                 free(root_hash);
2241                 free(hash_name);
2242                 free(fec_device_path);
2243                 free(salt);
2244         }
2245
2246         return r;
2247 }
2248
2249 static int _crypt_format_integrity(struct crypt_device *cd,
2250                                    const char *uuid,
2251                                    struct crypt_params_integrity *params)
2252 {
2253         int r;
2254         uint32_t integrity_tag_size;
2255         char *integrity = NULL, *journal_integrity = NULL, *journal_crypt = NULL;
2256         struct volume_key *journal_crypt_key = NULL, *journal_mac_key = NULL;
2257
2258         if (!params)
2259                 return -EINVAL;
2260
2261         if (uuid) {
2262                 log_err(cd, _("UUID is not supported for this crypt type."));
2263                 return -EINVAL;
2264         }
2265
2266         r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
2267         if (r < 0)
2268                 return r;
2269
2270         /* Wipe first 8 sectors - fs magic numbers etc. */
2271         r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0,
2272                               8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
2273         if (r < 0) {
2274                 log_err(cd, _("Cannot wipe header on device %s."),
2275                         mdata_device_path(cd));
2276                 return r;
2277         }
2278
2279         if (!(cd->type = strdup(CRYPT_INTEGRITY)))
2280                 return -ENOMEM;
2281
2282         if (params->journal_crypt_key) {
2283                 journal_crypt_key = crypt_alloc_volume_key(params->journal_crypt_key_size,
2284                                                            params->journal_crypt_key);
2285                 if (!journal_crypt_key)
2286                         return -ENOMEM;
2287         }
2288
2289         if (params->journal_integrity_key) {
2290                 journal_mac_key = crypt_alloc_volume_key(params->journal_integrity_key_size,
2291                                                          params->journal_integrity_key);
2292                 if (!journal_mac_key) {
2293                         r = -ENOMEM;
2294                         goto out;
2295                 }
2296         }
2297
2298         if (params->integrity && !(integrity = strdup(params->integrity))) {
2299                 r = -ENOMEM;
2300                 goto out;
2301         }
2302         if (params->journal_integrity && !(journal_integrity = strdup(params->journal_integrity))) {
2303                 r = -ENOMEM;
2304                 goto out;
2305         }
2306         if (params->journal_crypt && !(journal_crypt = strdup(params->journal_crypt))) {
2307                 r = -ENOMEM;
2308                 goto out;
2309         }
2310
2311         integrity_tag_size = INTEGRITY_hash_tag_size(integrity);
2312         if (integrity_tag_size > 0 && params->tag_size && integrity_tag_size != params->tag_size)
2313                 log_std(cd, _("WARNING: Requested tag size %d bytes differs from %s size output (%d bytes).\n"),
2314                         params->tag_size, integrity, integrity_tag_size);
2315
2316         if (params->tag_size)
2317                 integrity_tag_size = params->tag_size;
2318
2319         cd->u.integrity.journal_crypt_key = journal_crypt_key;
2320         cd->u.integrity.journal_mac_key = journal_mac_key;
2321         cd->u.integrity.params.journal_size = params->journal_size;
2322         cd->u.integrity.params.journal_watermark = params->journal_watermark;
2323         cd->u.integrity.params.journal_commit_time = params->journal_commit_time;
2324         cd->u.integrity.params.interleave_sectors = params->interleave_sectors;
2325         cd->u.integrity.params.buffer_sectors = params->buffer_sectors;
2326         cd->u.integrity.params.sector_size = params->sector_size;
2327         cd->u.integrity.params.tag_size = integrity_tag_size;
2328         cd->u.integrity.params.integrity = integrity;
2329         cd->u.integrity.params.journal_integrity = journal_integrity;
2330         cd->u.integrity.params.journal_crypt = journal_crypt;
2331
2332         r = INTEGRITY_format(cd, params, cd->u.integrity.journal_crypt_key, cd->u.integrity.journal_mac_key);
2333         if (r)
2334                 log_err(cd, _("Cannot format integrity for device %s."),
2335                         mdata_device_path(cd));
2336 out:
2337         if (r) {
2338                 crypt_free_volume_key(journal_crypt_key);
2339                 crypt_free_volume_key(journal_mac_key);
2340                 free(integrity);
2341                 free(journal_integrity);
2342                 free(journal_crypt);
2343         }
2344
2345         return r;
2346 }
2347
2348 static int _crypt_format(struct crypt_device *cd,
2349         const char *type,
2350         const char *cipher,
2351         const char *cipher_mode,
2352         const char *uuid,
2353         const char *volume_key,
2354         size_t volume_key_size,
2355         void *params,
2356         bool sector_size_autodetect)
2357 {
2358         int r;
2359
2360         if (!cd || !type)
2361                 return -EINVAL;
2362
2363         if (cd->type) {
2364                 log_dbg(cd, "Context already formatted as %s.", cd->type);
2365                 return -EINVAL;
2366         }
2367
2368         log_dbg(cd, "Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
2369
2370         crypt_reset_null_type(cd);
2371
2372         r = init_crypto(cd);
2373         if (r < 0)
2374                 return r;
2375
2376         if (isPLAIN(type))
2377                 r = _crypt_format_plain(cd, cipher, cipher_mode,
2378                                         uuid, volume_key_size, params);
2379         else if (isLUKS1(type))
2380                 r = _crypt_format_luks1(cd, cipher, cipher_mode,
2381                                         uuid, volume_key, volume_key_size, params);
2382         else if (isLUKS2(type))
2383                 r = _crypt_format_luks2(cd, cipher, cipher_mode,
2384                                         uuid, volume_key, volume_key_size, params, sector_size_autodetect);
2385         else if (isLOOPAES(type))
2386                 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
2387         else if (isVERITY(type))
2388                 r = _crypt_format_verity(cd, uuid, params);
2389         else if (isINTEGRITY(type))
2390                 r = _crypt_format_integrity(cd, uuid, params);
2391         else {
2392                 log_err(cd, _("Unknown crypt device type %s requested."), type);
2393                 r = -EINVAL;
2394         }
2395
2396         if (r < 0) {
2397                 crypt_set_null_type(cd);
2398                 crypt_free_volume_key(cd->volume_key);
2399                 cd->volume_key = NULL;
2400         }
2401
2402         return r;
2403 }
2404
2405 CRYPT_SYMBOL_EXPORT_NEW(int, crypt_format, 2, 4,
2406         /* crypt_format parameters follows */
2407         struct crypt_device *cd,
2408         const char *type,
2409         const char *cipher,
2410         const char *cipher_mode,
2411         const char *uuid,
2412         const char *volume_key,
2413         size_t volume_key_size,
2414         void *params)
2415 {
2416         return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, true);
2417 }
2418
2419
2420 CRYPT_SYMBOL_EXPORT_OLD(int, crypt_format, 2, 0,
2421         /* crypt_format parameters follows */
2422         struct crypt_device *cd,
2423         const char *type,
2424         const char *cipher,
2425         const char *cipher_mode,
2426         const char *uuid,
2427         const char *volume_key,
2428         size_t volume_key_size,
2429         void *params)
2430 {
2431         return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, false);
2432 }
2433
2434 int crypt_repair(struct crypt_device *cd,
2435                  const char *requested_type,
2436                  void *params __attribute__((unused)))
2437 {
2438         int r;
2439
2440         if (!cd)
2441                 return -EINVAL;
2442
2443         log_dbg(cd, "Trying to repair %s crypt type from device %s.",
2444                 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
2445
2446         if (!crypt_metadata_device(cd))
2447                 return -EINVAL;
2448
2449         if (requested_type && !isLUKS(requested_type))
2450                 return -EINVAL;
2451
2452         /* Load with repair */
2453         r = _crypt_load_luks(cd, requested_type, false, true);
2454         if (r < 0)
2455                 return r;
2456
2457         /* cd->type and header must be set in context */
2458         r = crypt_check_data_device_size(cd);
2459         if (r < 0)
2460                 crypt_set_null_type(cd);
2461
2462         return r;
2463 }
2464
2465 /* compare volume keys */
2466 static int _compare_volume_keys(struct volume_key *svk, unsigned skeyring_only,
2467                                 struct volume_key *tvk, unsigned tkeyring_only)
2468 {
2469         if (!svk && !tvk)
2470                 return 0;
2471         else if (!svk || !tvk)
2472                 return 1;
2473
2474         if (svk->keylength != tvk->keylength)
2475                 return 1;
2476
2477         if (!skeyring_only && !tkeyring_only)
2478                 return crypt_backend_memeq(svk->key, tvk->key, svk->keylength);
2479
2480         if (svk->key_description && tvk->key_description)
2481                 return strcmp(svk->key_description, tvk->key_description);
2482
2483         return 0;
2484 }
2485
2486 static int _compare_device_types(struct crypt_device *cd,
2487                                const struct crypt_dm_active_device *src,
2488                                const struct crypt_dm_active_device *tgt)
2489 {
2490         if (!tgt->uuid) {
2491                 log_dbg(cd, "Missing device uuid in target device.");
2492                 return -EINVAL;
2493         }
2494
2495         if (isLUKS2(cd->type) && !strncmp("INTEGRITY-", tgt->uuid, strlen("INTEGRITY-"))) {
2496                 if (crypt_uuid_cmp(tgt->uuid, src->uuid)) {
2497                         log_dbg(cd, "LUKS UUID mismatch.");
2498                         return -EINVAL;
2499                 }
2500         } else if (isLUKS(cd->type)) {
2501                 if (!src->uuid || strncmp(cd->type, tgt->uuid, strlen(cd->type)) ||
2502                     crypt_uuid_cmp(tgt->uuid, src->uuid)) {
2503                         log_dbg(cd, "LUKS UUID mismatch.");
2504                         return -EINVAL;
2505                 }
2506         } else if (isPLAIN(cd->type) || isLOOPAES(cd->type)) {
2507                 if (strncmp(cd->type, tgt->uuid, strlen(cd->type))) {
2508                         log_dbg(cd, "Unexpected uuid prefix %s in target device.", tgt->uuid);
2509                         return -EINVAL;
2510                 }
2511         } else if (!isINTEGRITY(cd->type)) {
2512                 log_dbg(cd, "Unsupported device type %s for reload.", cd->type ?: "<empty>");
2513                 return -ENOTSUP;
2514         }
2515
2516         return 0;
2517 }
2518
2519 static int _compare_crypt_devices(struct crypt_device *cd,
2520                                const struct dm_target *src,
2521                                const struct dm_target *tgt)
2522 {
2523         char *src_cipher = NULL, *src_integrity = NULL;
2524         int r = -EINVAL;
2525
2526         /* for crypt devices keys are mandatory */
2527         if (!src->u.crypt.vk || !tgt->u.crypt.vk)
2528                 return -EINVAL;
2529
2530         /* CIPHER checks */
2531         if (!src->u.crypt.cipher || !tgt->u.crypt.cipher)
2532                 return -EINVAL;
2533
2534         /*
2535          * dm_query_target converts capi cipher specification to dm-crypt format.
2536          * We need to do same for cipher specification requested in source
2537          * device.
2538          */
2539         if (crypt_capi_to_cipher(&src_cipher, &src_integrity, src->u.crypt.cipher, src->u.crypt.integrity))
2540                 return -EINVAL;
2541
2542         if (strcmp(src_cipher, tgt->u.crypt.cipher)) {
2543                 log_dbg(cd, "Cipher specs do not match.");
2544                 goto out;
2545         }
2546
2547         if (tgt->u.crypt.vk->keylength == 0 && crypt_is_cipher_null(tgt->u.crypt.cipher))
2548                 log_dbg(cd, "Existing device uses cipher null. Skipping key comparison.");
2549         else if (_compare_volume_keys(src->u.crypt.vk, 0, tgt->u.crypt.vk, tgt->u.crypt.vk->key_description != NULL)) {
2550                 log_dbg(cd, "Keys in context and target device do not match.");
2551                 goto out;
2552         }
2553
2554         if (crypt_strcmp(src_integrity, tgt->u.crypt.integrity)) {
2555                 log_dbg(cd, "Integrity parameters do not match.");
2556                 goto out;
2557         }
2558
2559         if (src->u.crypt.offset      != tgt->u.crypt.offset ||
2560             src->u.crypt.sector_size != tgt->u.crypt.sector_size ||
2561             src->u.crypt.iv_offset   != tgt->u.crypt.iv_offset ||
2562             src->u.crypt.tag_size    != tgt->u.crypt.tag_size) {
2563                 log_dbg(cd, "Integer parameters do not match.");
2564                 goto out;
2565         }
2566
2567         if (device_is_identical(src->data_device, tgt->data_device) <= 0)
2568                 log_dbg(cd, "Data devices do not match.");
2569         else
2570                 r = 0;
2571
2572 out:
2573         free(src_cipher);
2574         free(src_integrity);
2575
2576         return r;
2577 }
2578
2579 static int _compare_integrity_devices(struct crypt_device *cd,
2580                                const struct dm_target *src,
2581                                const struct dm_target *tgt)
2582 {
2583         /*
2584          * some parameters may be implicit (and set in dm-integrity ctor)
2585          *
2586          *      journal_size
2587          *      journal_watermark
2588          *      journal_commit_time
2589          *      buffer_sectors
2590          *      interleave_sectors
2591          */
2592
2593         /* check remaining integer values that makes sense */
2594         if (src->u.integrity.tag_size     != tgt->u.integrity.tag_size ||
2595             src->u.integrity.offset       != tgt->u.integrity.offset   ||
2596             src->u.integrity.sector_size  != tgt->u.integrity.sector_size) {
2597                 log_dbg(cd, "Integer parameters do not match.");
2598                 return -EINVAL;
2599         }
2600
2601         if (crypt_strcmp(src->u.integrity.integrity,         tgt->u.integrity.integrity) ||
2602             crypt_strcmp(src->u.integrity.journal_integrity, tgt->u.integrity.journal_integrity) ||
2603             crypt_strcmp(src->u.integrity.journal_crypt,     tgt->u.integrity.journal_crypt)) {
2604                 log_dbg(cd, "Journal parameters do not match.");
2605                 return -EINVAL;
2606         }
2607
2608         /* unfortunately dm-integrity doesn't support keyring */
2609         if (_compare_volume_keys(src->u.integrity.vk, 0, tgt->u.integrity.vk, 0) ||
2610             _compare_volume_keys(src->u.integrity.journal_integrity_key, 0, tgt->u.integrity.journal_integrity_key, 0) ||
2611             _compare_volume_keys(src->u.integrity.journal_crypt_key, 0, tgt->u.integrity.journal_crypt_key, 0)) {
2612                 log_dbg(cd, "Journal keys do not match.");
2613                 return -EINVAL;
2614         }
2615
2616         if (device_is_identical(src->data_device, tgt->data_device) <= 0) {
2617                 log_dbg(cd, "Data devices do not match.");
2618                 return -EINVAL;
2619         }
2620
2621         return 0;
2622 }
2623
2624 int crypt_compare_dm_devices(struct crypt_device *cd,
2625                                const struct crypt_dm_active_device *src,
2626                                const struct crypt_dm_active_device *tgt)
2627 {
2628         int r;
2629         const struct dm_target *s, *t;
2630
2631         if (!src || !tgt)
2632                 return -EINVAL;
2633
2634         r = _compare_device_types(cd, src, tgt);
2635         if (r)
2636                 return r;
2637
2638         s = &src->segment;
2639         t = &tgt->segment;
2640
2641         while (s || t) {
2642                 if (!s || !t) {
2643                         log_dbg(cd, "segments count mismatch.");
2644                         return -EINVAL;
2645                 }
2646                 if (s->type != t->type) {
2647                         log_dbg(cd, "segment type mismatch.");
2648                         r = -EINVAL;
2649                         break;
2650                 }
2651
2652                 switch (s->type) {
2653                 case DM_CRYPT:
2654                         r = _compare_crypt_devices(cd, s, t);
2655                         break;
2656                 case DM_INTEGRITY:
2657                         r = _compare_integrity_devices(cd, s, t);
2658                         break;
2659                 case DM_LINEAR:
2660                         r = (s->u.linear.offset == t->u.linear.offset) ? 0 : -EINVAL;
2661                         break;
2662                 default:
2663                         r = -ENOTSUP;
2664                 }
2665
2666                 if (r)
2667                         break;
2668
2669                 s = s->next;
2670                 t = t->next;
2671         }
2672
2673         return r;
2674 }
2675
2676 static int _reload_device(struct crypt_device *cd, const char *name,
2677                           struct crypt_dm_active_device *sdmd)
2678 {
2679         int r;
2680         struct crypt_dm_active_device tdmd;
2681         struct dm_target *src, *tgt = &tdmd.segment;
2682
2683         if (!cd || !cd->type || !name || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH))
2684                 return -EINVAL;
2685
2686         r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
2687                                   DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
2688                                   DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_INTEGRITY_PARAMS |
2689                                   DM_ACTIVE_JOURNAL_CRYPT_KEY | DM_ACTIVE_JOURNAL_MAC_KEY, &tdmd);
2690         if (r < 0) {
2691                 log_err(cd, _("Device %s is not active."), name);
2692                 return -EINVAL;
2693         }
2694
2695         if (!single_segment(&tdmd) ||
2696             (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY) ||
2697             (tgt->type == DM_CRYPT && tgt->u.crypt.tag_size)) {
2698                 r = -ENOTSUP;
2699                 log_err(cd, _("Unsupported parameters on device %s."), name);
2700                 goto out;
2701         }
2702
2703         r = crypt_compare_dm_devices(cd, sdmd, &tdmd);
2704         if (r) {
2705                 log_err(cd, _("Mismatching parameters on device %s."), name);
2706                 goto out;
2707         }
2708
2709         src = &sdmd->segment;
2710
2711         /* Changing read only flag for active device makes no sense */
2712         if (tdmd.flags & CRYPT_ACTIVATE_READONLY)
2713                 sdmd->flags |= CRYPT_ACTIVATE_READONLY;
2714         else
2715                 sdmd->flags &= ~CRYPT_ACTIVATE_READONLY;
2716
2717         if (tgt->type == DM_CRYPT && sdmd->flags & CRYPT_ACTIVATE_KEYRING_KEY) {
2718                 r = crypt_volume_key_set_description(tgt->u.crypt.vk, src->u.crypt.vk->key_description);
2719                 if (r)
2720                         goto out;
2721         } else if (tgt->type == DM_CRYPT) {
2722                 crypt_free_volume_key(tgt->u.crypt.vk);
2723                 tgt->u.crypt.vk = crypt_alloc_volume_key(src->u.crypt.vk->keylength, src->u.crypt.vk->key);
2724                 if (!tgt->u.crypt.vk) {
2725                         r = -ENOMEM;
2726                         goto out;
2727                 }
2728         }
2729
2730         if (tgt->type == DM_CRYPT)
2731                 r = device_block_adjust(cd, src->data_device, DEV_OK,
2732                                         src->u.crypt.offset, &sdmd->size, NULL);
2733         else if (tgt->type == DM_INTEGRITY)
2734                 r = device_block_adjust(cd, src->data_device, DEV_OK,
2735                                         src->u.integrity.offset, &sdmd->size, NULL);
2736         else
2737                 r = -EINVAL;
2738
2739         if (r)
2740                 goto out;
2741
2742         tdmd.flags = sdmd->flags;
2743         tgt->size = tdmd.size = sdmd->size;
2744
2745         r = dm_reload_device(cd, name, &tdmd, 0, 1);
2746 out:
2747         dm_targets_free(cd, &tdmd);
2748         free(CONST_CAST(void*)tdmd.uuid);
2749
2750         return r;
2751 }
2752
2753 static int _reload_device_with_integrity(struct crypt_device *cd,
2754         const char *name,
2755         const char *iname,
2756         const char *ipath,
2757         struct crypt_dm_active_device *sdmd,
2758         struct crypt_dm_active_device *sdmdi)
2759 {
2760         int r;
2761         struct crypt_dm_active_device tdmd, tdmdi = {};
2762         struct dm_target *src, *srci, *tgt = &tdmd.segment, *tgti = &tdmdi.segment;
2763         struct device *data_device = NULL;
2764         bool clear = false;
2765
2766         if (!cd || !cd->type || !name || !iname || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH))
2767                 return -EINVAL;
2768
2769         r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
2770                                   DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
2771                                   DM_ACTIVE_CRYPT_KEY, &tdmd);
2772         if (r < 0) {
2773                 log_err(cd, _("Device %s is not active."), name);
2774                 return -EINVAL;
2775         }
2776
2777         if (!single_segment(&tdmd) || tgt->type != DM_CRYPT || !tgt->u.crypt.tag_size) {
2778                 log_err(cd, _("Unsupported parameters on device %s."), name);
2779                 r = -ENOTSUP;
2780                 goto out;
2781         }
2782
2783         r = dm_query_device(cd, iname, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &tdmdi);
2784         if (r < 0) {
2785                 log_err(cd, _("Device %s is not active."), iname);
2786                 r = -EINVAL;
2787                 goto out;
2788         }
2789
2790         if (!single_segment(&tdmdi) || tgti->type != DM_INTEGRITY) {
2791                 log_err(cd, _("Unsupported parameters on device %s."), iname);
2792                 r = -ENOTSUP;
2793                 goto out;
2794         }
2795
2796         r = crypt_compare_dm_devices(cd, sdmdi, &tdmdi);
2797         if (r) {
2798                 log_err(cd, _("Mismatching parameters on device %s."), iname);
2799                 goto out;
2800         }
2801
2802         /* unsupported underneath dm-crypt with auth. encryption */
2803         if (sdmdi->segment.u.integrity.meta_device || tdmdi.segment.u.integrity.meta_device)
2804                 return -ENOTSUP;
2805
2806         src = &sdmd->segment;
2807         srci = &sdmdi->segment;
2808
2809         r = device_alloc(cd, &data_device, ipath);
2810         if (r < 0)
2811                 goto out;
2812
2813         r = device_block_adjust(cd, srci->data_device, DEV_OK,
2814                                 srci->u.integrity.offset, &sdmdi->size, NULL);
2815         if (r)
2816                 goto out;
2817
2818         src->data_device = data_device;
2819
2820         r = crypt_compare_dm_devices(cd, sdmd, &tdmd);
2821         if (r) {
2822                 log_err(cd, _("Crypt devices mismatch."));
2823                 goto out;
2824         }
2825
2826         /* Changing read only flag for active device makes no sense */
2827         if (tdmd.flags & CRYPT_ACTIVATE_READONLY)
2828                 sdmd->flags |= CRYPT_ACTIVATE_READONLY;
2829         else
2830                 sdmd->flags &= ~CRYPT_ACTIVATE_READONLY;
2831
2832         if (tdmdi.flags & CRYPT_ACTIVATE_READONLY)
2833                 sdmdi->flags |= CRYPT_ACTIVATE_READONLY;
2834         else
2835                 sdmdi->flags &= ~CRYPT_ACTIVATE_READONLY;
2836
2837         if (sdmd->flags & CRYPT_ACTIVATE_KEYRING_KEY) {
2838                 r = crypt_volume_key_set_description(tgt->u.crypt.vk, src->u.crypt.vk->key_description);
2839                 if (r)
2840                         goto out;
2841         } else {
2842                 crypt_free_volume_key(tgt->u.crypt.vk);
2843                 tgt->u.crypt.vk = crypt_alloc_volume_key(src->u.crypt.vk->keylength, src->u.crypt.vk->key);
2844                 if (!tgt->u.crypt.vk) {
2845                         r = -ENOMEM;
2846                         goto out;
2847                 }
2848         }
2849
2850         r = device_block_adjust(cd, src->data_device, DEV_OK,
2851                                 src->u.crypt.offset, &sdmd->size, NULL);
2852         if (r)
2853                 goto out;
2854
2855         tdmd.flags = sdmd->flags;
2856         tdmd.size = sdmd->size;
2857
2858         if ((r = dm_reload_device(cd, iname, sdmdi, 0, 0))) {
2859                 log_err(cd, _("Failed to reload device %s."), iname);
2860                 goto out;
2861         }
2862
2863         if ((r = dm_reload_device(cd, name, &tdmd, 0, 0))) {
2864                 log_err(cd, _("Failed to reload device %s."), name);
2865                 clear = true;
2866                 goto out;
2867         }
2868
2869         if ((r = dm_suspend_device(cd, name, 0))) {
2870                 log_err(cd, _("Failed to suspend device %s."), name);
2871                 clear = true;
2872                 goto out;
2873         }
2874
2875         if ((r = dm_suspend_device(cd, iname, 0))) {
2876                 log_err(cd, _("Failed to suspend device %s."), iname);
2877                 clear = true;
2878                 goto out;
2879         }
2880
2881         if ((r = dm_resume_device(cd, iname, act2dmflags(sdmdi->flags)))) {
2882                 log_err(cd, _("Failed to resume device %s."), iname);
2883                 clear = true;
2884                 goto out;
2885         }
2886
2887         r = dm_resume_device(cd, name, act2dmflags(tdmd.flags));
2888         if (!r)
2889                 goto out;
2890
2891         /*
2892          * This is worst case scenario. We have active underlying dm-integrity device with
2893          * new table but dm-crypt resume failed for some reason. Tear everything down and
2894          * burn it for good.
2895          */
2896
2897         log_err(cd, _("Fatal error while reloading device %s (on top of device %s)."), name, iname);
2898
2899         if (dm_error_device(cd, name))
2900                 log_err(cd, _("Failed to switch device %s to dm-error."), name);
2901         if (dm_error_device(cd, iname))
2902                 log_err(cd, _("Failed to switch device %s to dm-error."), iname);
2903 out:
2904         if (clear) {
2905                 dm_clear_device(cd, name);
2906                 dm_clear_device(cd, iname);
2907
2908                 if (dm_status_suspended(cd, name) > 0)
2909                         dm_resume_device(cd, name, 0);
2910                 if (dm_status_suspended(cd, iname) > 0)
2911                         dm_resume_device(cd, iname, 0);
2912         }
2913
2914         dm_targets_free(cd, &tdmd);
2915         dm_targets_free(cd, &tdmdi);
2916         free(CONST_CAST(void*)tdmdi.uuid);
2917         free(CONST_CAST(void*)tdmd.uuid);
2918         device_free(cd, data_device);
2919
2920         return r;
2921 }
2922
2923 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
2924 {
2925         struct crypt_dm_active_device dmdq, dmd = {};
2926         struct dm_target *tgt = &dmdq.segment;
2927         struct crypt_params_integrity params = {};
2928         uint32_t supported_flags = 0;
2929         uint64_t old_size;
2930         int r;
2931
2932         /*
2933          * FIXME: Also with LUKS2 we must not allow resize when there's
2934          *        explicit size stored in metadata (length != "dynamic")
2935          */
2936
2937         /* Device context type must be initialized */
2938         if (!cd || !cd->type || !name)
2939                 return -EINVAL;
2940
2941         if (isTCRYPT(cd->type) || isBITLK(cd->type)) {
2942                 log_err(cd, _("This operation is not supported for this device type."));
2943                 return -ENOTSUP;
2944         }
2945
2946         log_dbg(cd, "Resizing device %s to %" PRIu64 " sectors.", name, new_size);
2947
2948         r = dm_query_device(cd, name, DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY |
2949                             DM_ACTIVE_INTEGRITY_PARAMS | DM_ACTIVE_JOURNAL_CRYPT_KEY |
2950                             DM_ACTIVE_JOURNAL_MAC_KEY, &dmdq);
2951         if (r < 0) {
2952                 log_err(cd, _("Device %s is not active."), name);
2953                 return -EINVAL;
2954         }
2955         if (!single_segment(&dmdq) || (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY)) {
2956                 log_dbg(cd, "Unsupported device table detected in %s.", name);
2957                 r = -EINVAL;
2958                 goto out;
2959         }
2960
2961         if ((dmdq.flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_key_in_keyring(cd)) {
2962                 r = -EPERM;
2963                 goto out;
2964         }
2965
2966         if (crypt_key_in_keyring(cd)) {
2967                 if (!isLUKS2(cd->type)) {
2968                         r = -EINVAL;
2969                         goto out;
2970                 }
2971                 r = LUKS2_key_description_by_segment(cd, &cd->u.luks2.hdr,
2972                                         tgt->u.crypt.vk, CRYPT_DEFAULT_SEGMENT);
2973                 if (r)
2974                         goto out;
2975
2976                 dmdq.flags |= CRYPT_ACTIVATE_KEYRING_KEY;
2977         }
2978
2979         if (crypt_loop_device(crypt_get_device_name(cd))) {
2980                 log_dbg(cd, "Trying to resize underlying loop device %s.",
2981                         crypt_get_device_name(cd));
2982                 /* Here we always use default size not new_size */
2983                 if (crypt_loop_resize(crypt_get_device_name(cd)))
2984                         log_err(cd, _("Cannot resize loop device."));
2985         }
2986
2987
2988         /*
2989          * Integrity device metadata are maintained by the kernel. We need to
2990          * reload the device (with the same parameters) and let the kernel
2991          * calculate the maximum size of integrity device and store it in the
2992          * superblock.
2993          */
2994         if (!new_size && tgt->type == DM_INTEGRITY) {
2995                 r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd),
2996                                            crypt_get_data_offset(cd) * SECTOR_SIZE, &old_size);
2997                 if (r < 0)
2998                         return r;
2999
3000                 dmd.size = dmdq.size;
3001                 dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH | CRYPT_ACTIVATE_PRIVATE;
3002
3003                 r = crypt_get_integrity_info(cd, &params);
3004                 if (r)
3005                         goto out;
3006
3007                 r = dm_integrity_target_set(cd, &dmd.segment, 0, dmdq.segment.size,
3008                                 crypt_metadata_device(cd), crypt_data_device(cd),
3009                                 crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd),
3010                                 crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key,
3011                                 tgt->u.integrity.journal_integrity_key, &params);
3012                 if (r)
3013                         goto out;
3014                 r = _reload_device(cd, name, &dmd);
3015                 if (r)
3016                         goto out;
3017
3018                 r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd),
3019                                 crypt_get_data_offset(cd) * SECTOR_SIZE, &new_size);
3020                 if (r < 0)
3021                         return r;
3022                 log_dbg(cd, "Maximum integrity device size from kernel %" PRIu64, new_size);
3023
3024                 if (old_size == new_size && new_size == dmdq.size &&
3025                     !dm_flags(cd, tgt->type, &supported_flags) &&
3026                     !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED))
3027                         log_std(cd, _("WARNING: Maximum size already set or kernel doesn't support resize.\n"));
3028         }
3029
3030         r = device_block_adjust(cd, crypt_data_device(cd), DEV_OK,
3031                         crypt_get_data_offset(cd), &new_size, &dmdq.flags);
3032         if (r)
3033                 goto out;
3034
3035         if (MISALIGNED(new_size, (tgt->type == DM_CRYPT ? tgt->u.crypt.sector_size : tgt->u.integrity.sector_size) >> SECTOR_SHIFT)) {
3036                 log_err(cd, _("Device size is not aligned to requested sector size."));
3037                 r = -EINVAL;
3038                 goto out;
3039         }
3040
3041         if (MISALIGNED(new_size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) {
3042                 log_err(cd, _("Device size is not aligned to device logical block size."));
3043                 r = -EINVAL;
3044                 goto out;
3045         }
3046
3047         dmd.uuid = crypt_get_uuid(cd);
3048         dmd.size = new_size;
3049         dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH;
3050
3051         if (tgt->type == DM_CRYPT) {
3052                 r = dm_crypt_target_set(&dmd.segment, 0, new_size, crypt_data_device(cd),
3053                                 tgt->u.crypt.vk, crypt_get_cipher_spec(cd),
3054                                 crypt_get_iv_offset(cd), crypt_get_data_offset(cd),
3055                                 crypt_get_integrity(cd), crypt_get_integrity_tag_size(cd),
3056                                 crypt_get_sector_size(cd));
3057                 if (r < 0)
3058                         goto out;
3059         } else if (tgt->type == DM_INTEGRITY) {
3060                 r = crypt_get_integrity_info(cd, &params);
3061                 if (r)
3062                         goto out;
3063
3064                 r = dm_integrity_target_set(cd, &dmd.segment, 0, new_size,
3065                                 crypt_metadata_device(cd), crypt_data_device(cd),
3066                                 crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd),
3067                                 crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key,
3068                                 tgt->u.integrity.journal_integrity_key, &params);
3069                 if (r)
3070                         goto out;
3071         }
3072
3073         if (new_size == dmdq.size) {
3074                 log_dbg(cd, "Device has already requested size %" PRIu64
3075                         " sectors.", dmdq.size);
3076                 r = 0;
3077         } else {
3078                 if (isTCRYPT(cd->type))
3079                         r = -ENOTSUP;
3080                 else if (isLUKS2(cd->type))
3081                         r = LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, 0, 0);
3082                 if (!r)
3083                         r = _reload_device(cd, name, &dmd);
3084
3085                 if (r && tgt->type == DM_INTEGRITY &&
3086                     !dm_flags(cd, tgt->type, &supported_flags) &&
3087                     !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED))
3088                         log_err(cd, _("Resize failed, the kernel doesn't support it."));
3089         }
3090 out:
3091         dm_targets_free(cd, &dmd);
3092         dm_targets_free(cd, &dmdq);
3093
3094         return r;
3095 }
3096
3097 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
3098 {
3099         const char *active_uuid;
3100         int r;
3101
3102         log_dbg(cd, "%s device uuid.", uuid ? "Setting new" : "Refreshing");
3103
3104         if ((r = onlyLUKS(cd)))
3105                 return r;
3106
3107         active_uuid = crypt_get_uuid(cd);
3108
3109         if (uuid && active_uuid && !strncmp(uuid, active_uuid, UUID_STRING_L)) {
3110                 log_dbg(cd, "UUID is the same as requested (%s) for device %s.",
3111                         uuid, mdata_device_path(cd));
3112                 return 0;
3113         }
3114
3115         if (uuid)
3116                 log_dbg(cd, "Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
3117         else
3118                 log_dbg(cd, "Requested new UUID refresh for %s.", mdata_device_path(cd));
3119
3120         if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
3121                 return -EPERM;
3122
3123         if (isLUKS1(cd->type))
3124                 return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
3125         else
3126                 return LUKS2_hdr_uuid(cd, &cd->u.luks2.hdr, uuid);
3127 }
3128
3129 int crypt_set_label(struct crypt_device *cd, const char *label, const char *subsystem)
3130 {
3131         int r;
3132
3133         log_dbg(cd, "Setting new labels.");
3134
3135         if ((r = onlyLUKS2(cd)))
3136                 return r;
3137
3138         return LUKS2_hdr_labels(cd, &cd->u.luks2.hdr, label, subsystem, 1);
3139 }
3140
3141 const char *crypt_get_label(struct crypt_device *cd)
3142 {
3143         if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
3144                 return NULL;
3145
3146         return cd->u.luks2.hdr.label;
3147 }
3148
3149 const char *crypt_get_subsystem(struct crypt_device *cd)
3150 {
3151         if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
3152                 return NULL;
3153
3154         return cd->u.luks2.hdr.subsystem;
3155 }
3156
3157 int crypt_header_backup(struct crypt_device *cd,
3158                         const char *requested_type,
3159                         const char *backup_file)
3160 {
3161         int r;
3162
3163         if (requested_type && !isLUKS(requested_type))
3164                 return -EINVAL;
3165
3166         if (!backup_file)
3167                 return -EINVAL;
3168
3169         /* Load with repair */
3170         r = _crypt_load_luks(cd, requested_type, false, false);
3171         if (r < 0)
3172                 return r;
3173
3174         log_dbg(cd, "Requested header backup of device %s (%s) to "
3175                 "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file);
3176
3177         if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type)))
3178                 r = LUKS_hdr_backup(backup_file, cd);
3179         else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type)))
3180                 r = LUKS2_hdr_backup(cd, &cd->u.luks2.hdr, backup_file);
3181         else
3182                 r = -EINVAL;
3183
3184         return r;
3185 }
3186
3187 int crypt_header_restore(struct crypt_device *cd,
3188                          const char *requested_type,
3189                          const char *backup_file)
3190 {
3191         struct luks_phdr hdr1;
3192         struct luks2_hdr hdr2;
3193         int r, version;
3194
3195         if (requested_type && !isLUKS(requested_type))
3196                 return -EINVAL;
3197
3198         if (!cd || (cd->type && !isLUKS(cd->type)) || !backup_file)
3199                 return -EINVAL;
3200
3201         r = init_crypto(cd);
3202         if (r < 0)
3203                 return r;
3204
3205         log_dbg(cd, "Requested header restore to device %s (%s) from "
3206                 "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file);
3207
3208         version = LUKS2_hdr_version_unlocked(cd, backup_file);
3209         if (!version ||
3210            (requested_type && version == 1 && !isLUKS1(requested_type)) ||
3211            (requested_type && version == 2 && !isLUKS2(requested_type))) {
3212                 log_err(cd, _("Header backup file does not contain compatible LUKS header."));
3213                 return -EINVAL;
3214         }
3215
3216         memset(&hdr2, 0, sizeof(hdr2));
3217
3218         if (!cd->type) {
3219                 if (version == 1)
3220                         r = LUKS_hdr_restore(backup_file, &hdr1, cd);
3221                 else
3222                         r = LUKS2_hdr_restore(cd, &hdr2, backup_file);
3223
3224                 crypt_safe_memzero(&hdr1, sizeof(hdr1));
3225                 crypt_safe_memzero(&hdr2, sizeof(hdr2));
3226         } else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type))) {
3227                 r = LUKS2_hdr_restore(cd, &cd->u.luks2.hdr, backup_file);
3228                 if (r)
3229                         (void) _crypt_load_luks2(cd, 1, 0);
3230         } else if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type)))
3231                 r = LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
3232         else
3233                 r = -EINVAL;
3234
3235         if (!r)
3236                 r = _crypt_load_luks(cd, version == 1 ? CRYPT_LUKS1 : CRYPT_LUKS2, false, true);
3237
3238         return r;
3239 }
3240
3241 int crypt_header_is_detached(struct crypt_device *cd)
3242 {
3243         int r;
3244
3245         if (!cd || (cd->type && !isLUKS(cd->type)))
3246                 return -EINVAL;
3247
3248         r = device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd));
3249         if (r < 0) {
3250                 log_dbg(cd, "Failed to compare data and metadata devices path.");
3251                 return r;
3252         }
3253
3254         return r ? 0 : 1;
3255 }
3256
3257 void crypt_free(struct crypt_device *cd)
3258 {
3259         if (!cd)
3260                 return;
3261
3262         log_dbg(cd, "Releasing crypt device %s context.", mdata_device_path(cd) ?: "empty");
3263
3264         dm_backend_exit(cd);
3265         crypt_free_volume_key(cd->volume_key);
3266
3267         crypt_free_type(cd, NULL);
3268
3269         device_free(cd, cd->device);
3270         device_free(cd, cd->metadata_device);
3271
3272         free(CONST_CAST(void*)cd->pbkdf.type);
3273         free(CONST_CAST(void*)cd->pbkdf.hash);
3274
3275         /* Some structures can contain keys (TCRYPT), wipe it */
3276         crypt_safe_memzero(cd, sizeof(*cd));
3277         free(cd);
3278 }
3279
3280 static char *crypt_get_device_key_description(struct crypt_device *cd, const char *name)
3281 {
3282         char *desc = NULL;
3283         struct crypt_dm_active_device dmd;
3284         struct dm_target *tgt = &dmd.segment;
3285
3286         if (dm_query_device(cd, name, DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_CRYPT_KEYSIZE, &dmd) < 0)
3287                 return NULL;
3288
3289         if (single_segment(&dmd) && tgt->type == DM_CRYPT &&
3290             (dmd.flags & CRYPT_ACTIVATE_KEYRING_KEY) && tgt->u.crypt.vk->key_description)
3291                 desc = strdup(tgt->u.crypt.vk->key_description);
3292
3293         dm_targets_free(cd, &dmd);
3294
3295         return desc;
3296 }
3297
3298 int crypt_suspend(struct crypt_device *cd,
3299                   const char *name)
3300 {
3301         char *key_desc;
3302         crypt_status_info ci;
3303         int r;
3304         uint32_t dmflags = DM_SUSPEND_WIPE_KEY;
3305
3306         /* FIXME: check context uuid matches the dm-crypt device uuid (onlyLUKS branching) */
3307
3308         if (!cd || !name)
3309                 return -EINVAL;
3310
3311         log_dbg(cd, "Suspending volume %s.", name);
3312
3313         if (cd->type)
3314                 r = onlyLUKS(cd);
3315         else {
3316                 r = crypt_uuid_type_cmp(cd, CRYPT_LUKS1);
3317                 if (r < 0)
3318                         r = crypt_uuid_type_cmp(cd, CRYPT_LUKS2);
3319                 if (r < 0)
3320                         log_err(cd, _("This operation is supported only for LUKS device."));
3321         }
3322
3323         if (r < 0)
3324                 return r;
3325
3326         ci = crypt_status(NULL, name);
3327         if (ci < CRYPT_ACTIVE) {
3328                 log_err(cd, _("Volume %s is not active."), name);
3329                 return -EINVAL;
3330         }
3331
3332         dm_backend_init(cd);
3333
3334         r = dm_status_suspended(cd, name);
3335         if (r < 0)
3336                 goto out;
3337
3338         if (r) {
3339                 log_err(cd, _("Volume %s is already suspended."), name);
3340                 r = -EINVAL;
3341                 goto out;
3342         }
3343
3344         key_desc = crypt_get_device_key_description(cd, name);
3345
3346         /* we can't simply wipe wrapped keys */
3347         if (crypt_cipher_wrapped_key(crypt_get_cipher(cd), crypt_get_cipher_mode(cd)))
3348                 dmflags &= ~DM_SUSPEND_WIPE_KEY;
3349
3350         r = dm_suspend_device(cd, name, dmflags);
3351         if (r == -ENOTSUP)
3352                 log_err(cd, _("Suspend is not supported for device %s."), name);
3353         else if (r)
3354                 log_err(cd, _("Error during suspending device %s."), name);
3355         else
3356                 crypt_drop_keyring_key_by_description(cd, key_desc, LOGON_KEY);
3357         free(key_desc);
3358 out:
3359         dm_backend_exit(cd);
3360         return r;
3361 }
3362
3363 /* key must be properly verified */
3364 static int resume_by_volume_key(struct crypt_device *cd,
3365                 struct volume_key *vk,
3366                 const char *name)
3367 {
3368         int digest, r;
3369         struct volume_key *zerokey = NULL;
3370
3371         if (crypt_is_cipher_null(crypt_get_cipher_spec(cd))) {
3372                 zerokey = crypt_alloc_volume_key(0, NULL);
3373                 if (!zerokey)
3374                         return -ENOMEM;
3375                 vk = zerokey;
3376         } else if (crypt_use_keyring_for_vk(cd)) {
3377                 /* LUKS2 path only */
3378                 digest = LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
3379                 if (digest < 0)
3380                         return -EINVAL;
3381                 r = LUKS2_volume_key_load_in_keyring_by_digest(cd, vk, digest);
3382                 if (r < 0)
3383                         return r;
3384         }
3385
3386         r = dm_resume_and_reinstate_key(cd, name, vk);
3387
3388         if (r == -ENOTSUP)
3389                 log_err(cd, _("Resume is not supported for device %s."), name);
3390         else if (r)
3391                 log_err(cd, _("Error during resuming device %s."), name);
3392
3393         if (r < 0)
3394                 crypt_drop_keyring_key(cd, vk);
3395
3396         crypt_free_volume_key(zerokey);
3397
3398         return r;
3399 }
3400
3401 int crypt_resume_by_passphrase(struct crypt_device *cd,
3402                                const char *name,
3403                                int keyslot,
3404                                const char *passphrase,
3405                                size_t passphrase_size)
3406 {
3407         struct volume_key *vk = NULL;
3408         int r;
3409
3410         /* FIXME: check context uuid matches the dm-crypt device uuid */
3411
3412         if (!passphrase || !name)
3413                 return -EINVAL;
3414
3415         log_dbg(cd, "Resuming volume %s.", name);
3416
3417         if ((r = onlyLUKS(cd)))
3418                 return r;
3419
3420         r = dm_status_suspended(cd, name);
3421         if (r < 0)
3422                 return r;
3423
3424         if (!r) {
3425                 log_err(cd, _("Volume %s is not suspended."), name);
3426                 return -EINVAL;
3427         }
3428
3429         if (isLUKS1(cd->type))
3430                 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
3431                                            &cd->u.luks1.hdr, &vk, cd);
3432         else
3433                 r = LUKS2_keyslot_open(cd, keyslot, CRYPT_DEFAULT_SEGMENT, passphrase, passphrase_size, &vk);
3434
3435         if  (r < 0)
3436                 return r;
3437
3438         keyslot = r;
3439
3440         r = resume_by_volume_key(cd, vk, name);
3441
3442         crypt_free_volume_key(vk);
3443         return r < 0 ? r : keyslot;
3444 }
3445
3446 int crypt_resume_by_keyfile_device_offset(struct crypt_device *cd,
3447                                           const char *name,
3448                                           int keyslot,
3449                                           const char *keyfile,
3450                                           size_t keyfile_size,
3451                                           uint64_t keyfile_offset)
3452 {
3453         struct volume_key *vk = NULL;
3454         char *passphrase_read = NULL;
3455         size_t passphrase_size_read;
3456         int r;
3457
3458         /* FIXME: check context uuid matches the dm-crypt device uuid */
3459
3460         if (!name || !keyfile)
3461                 return -EINVAL;
3462
3463         log_dbg(cd, "Resuming volume %s.", name);
3464
3465         if ((r = onlyLUKS(cd)))
3466                 return r;
3467
3468         r = dm_status_suspended(cd, name);
3469         if (r < 0)
3470                 return r;
3471
3472         if (!r) {
3473                 log_err(cd, _("Volume %s is not suspended."), name);
3474                 return -EINVAL;
3475         }
3476
3477         r = crypt_keyfile_device_read(cd, keyfile,
3478                                       &passphrase_read, &passphrase_size_read,
3479                                       keyfile_offset, keyfile_size, 0);
3480         if (r < 0)
3481                 return r;
3482
3483         if (isLUKS1(cd->type))
3484                 r = LUKS_open_key_with_hdr(keyslot, passphrase_read, passphrase_size_read,
3485                                            &cd->u.luks1.hdr, &vk, cd);
3486         else
3487                 r = LUKS2_keyslot_open(cd, keyslot, CRYPT_DEFAULT_SEGMENT,
3488                                        passphrase_read, passphrase_size_read, &vk);
3489
3490         crypt_safe_free(passphrase_read);
3491         if (r < 0)
3492                 return r;
3493
3494         keyslot = r;
3495
3496         r = resume_by_volume_key(cd, vk, name);
3497
3498         crypt_free_volume_key(vk);
3499         return r < 0 ? r : keyslot;
3500 }
3501
3502 int crypt_resume_by_keyfile(struct crypt_device *cd,
3503                             const char *name,
3504                             int keyslot,
3505                             const char *keyfile,
3506                             size_t keyfile_size)
3507 {
3508         return crypt_resume_by_keyfile_device_offset(cd, name, keyslot,
3509                                               keyfile, keyfile_size, 0);
3510 }
3511
3512 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
3513                                    const char *name,
3514                                    int keyslot,
3515                                    const char *keyfile,
3516                                    size_t keyfile_size,
3517                                    size_t keyfile_offset)
3518 {
3519         return crypt_resume_by_keyfile_device_offset(cd, name, keyslot,
3520                                       keyfile, keyfile_size, keyfile_offset);
3521 }
3522
3523 int crypt_resume_by_volume_key(struct crypt_device *cd,
3524         const char *name,
3525         const char *volume_key,
3526         size_t volume_key_size)
3527 {
3528         struct volume_key *vk = NULL;
3529         int r;
3530
3531         if (!name || !volume_key)
3532                 return -EINVAL;
3533
3534         log_dbg(cd, "Resuming volume %s by volume key.", name);
3535
3536         if ((r = onlyLUKS(cd)))
3537                 return r;
3538
3539         r = dm_status_suspended(cd, name);
3540         if (r < 0)
3541                 return r;
3542
3543         if (!r) {
3544                 log_err(cd, _("Volume %s is not suspended."), name);
3545                 return -EINVAL;
3546         }
3547
3548         vk = crypt_alloc_volume_key(volume_key_size, volume_key);
3549         if (!vk)
3550                 return -ENOMEM;
3551
3552         if (isLUKS1(cd->type))
3553                 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
3554         else if (isLUKS2(cd->type))
3555                 r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
3556         else
3557                 r = -EINVAL;
3558         if (r == -EPERM || r == -ENOENT)
3559                 log_err(cd, _("Volume key does not match the volume."));
3560
3561         if (r >= 0)
3562                 r = resume_by_volume_key(cd, vk, name);
3563
3564         crypt_free_volume_key(vk);
3565         return r;
3566 }
3567
3568 int crypt_resume_by_token_pin(struct crypt_device *cd, const char *name,
3569         const char *type, int token, const char *pin, size_t pin_size,
3570         void *usrptr)
3571 {
3572         struct volume_key *vk = NULL;
3573         int r, keyslot;
3574
3575         if (!name)
3576                 return -EINVAL;
3577
3578         log_dbg(cd, "Resuming volume %s by token (%s type) %d.",
3579                 name, type ?: "any", token);
3580
3581         if ((r = _onlyLUKS2(cd, CRYPT_CD_QUIET, 0)))
3582                 return r;
3583
3584         r = dm_status_suspended(cd, name);
3585         if (r < 0)
3586                 return r;
3587
3588         if (!r) {
3589                 log_err(cd, _("Volume %s is not suspended."), name);
3590                 return -EINVAL;
3591         }
3592
3593         r = LUKS2_token_unlock_key(cd, &cd->u.luks2.hdr, token, type,
3594                                    pin, pin_size, CRYPT_DEFAULT_SEGMENT, usrptr, &vk);
3595         keyslot = r;
3596         if (r >= 0)
3597                 r = resume_by_volume_key(cd, vk, name);
3598
3599         crypt_free_volume_key(vk);
3600         return r < 0 ? r : keyslot;
3601 }
3602
3603 /*
3604  * Keyslot manipulation
3605  */
3606 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
3607         int keyslot, // -1 any
3608         const char *passphrase,
3609         size_t passphrase_size,
3610         const char *new_passphrase,
3611         size_t new_passphrase_size)
3612 {
3613         int r;
3614         struct crypt_keyslot_context kc, new_kc;
3615
3616         if (!passphrase || !new_passphrase)
3617                 return -EINVAL;
3618
3619         crypt_keyslot_unlock_by_passphrase_init_internal(&kc, passphrase, passphrase_size);
3620         crypt_keyslot_unlock_by_passphrase_init_internal(&new_kc, new_passphrase, new_passphrase_size);
3621
3622         r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
3623
3624         crypt_keyslot_context_destroy_internal(&kc);
3625         crypt_keyslot_context_destroy_internal(&new_kc);
3626
3627         return r;
3628 }
3629
3630 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
3631         int keyslot_old,
3632         int keyslot_new,
3633         const char *passphrase,
3634         size_t passphrase_size,
3635         const char *new_passphrase,
3636         size_t new_passphrase_size)
3637 {
3638         int digest = -1, r, keyslot_new_orig = keyslot_new;
3639         struct luks2_keyslot_params params;
3640         struct volume_key *vk = NULL;
3641
3642         if (!passphrase || !new_passphrase)
3643                 return -EINVAL;
3644
3645         log_dbg(cd, "Changing passphrase from old keyslot %d to new %d.",
3646                 keyslot_old, keyslot_new);
3647
3648         if ((r = onlyLUKS(cd)))
3649                 return r;
3650
3651         if (isLUKS1(cd->type))
3652                 r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
3653                                            &cd->u.luks1.hdr, &vk, cd);
3654         else if (isLUKS2(cd->type)) {
3655                 r = LUKS2_keyslot_open(cd, keyslot_old, CRYPT_ANY_SEGMENT, passphrase, passphrase_size, &vk);
3656                 /* will fail for keyslots w/o digest. fix if supported in a future */
3657                 if (r >= 0) {
3658                         digest = LUKS2_digest_by_keyslot(&cd->u.luks2.hdr, r);
3659                         if (digest < 0)
3660                                 r = -EINVAL;
3661                 }
3662         } else
3663                 r = -EINVAL;
3664         if (r < 0)
3665                 goto out;
3666
3667         if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
3668                 log_dbg(cd, "Keyslot mismatch.");
3669                 goto out;
3670         }
3671         keyslot_old = r;
3672
3673         if (keyslot_new == CRYPT_ANY_SLOT) {
3674                 if (isLUKS1(cd->type))
3675                         keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
3676                 else if (isLUKS2(cd->type))
3677                         keyslot_new = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, vk->keylength);
3678                 if (keyslot_new < 0)
3679                         keyslot_new = keyslot_old;
3680         }
3681         log_dbg(cd, "Key change, old slot %d, new slot %d.", keyslot_old, keyslot_new);
3682
3683         if (isLUKS1(cd->type)) {
3684                 if (keyslot_old == keyslot_new) {
3685                         log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old);
3686                         (void)crypt_keyslot_destroy(cd, keyslot_old);
3687                 }
3688                 r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
3689                                  &cd->u.luks1.hdr, vk, cd);
3690         } else if (isLUKS2(cd->type)) {
3691                 r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, &params);
3692                 if (r)
3693                         goto out;
3694
3695                 if (keyslot_old != keyslot_new) {
3696                         r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, digest, 1, 0);
3697                         if (r < 0)
3698                                 goto out;
3699                         r = LUKS2_token_assignment_copy(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new, 0);
3700                         if (r < 0)
3701                                 goto out;
3702                 } else {
3703                         log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old);
3704                         /* FIXME: improve return code so that we can detect area is damaged */
3705                         r = LUKS2_keyslot_wipe(cd, &cd->u.luks2.hdr, keyslot_old, 1);
3706                         if (r) {
3707                                 /* (void)crypt_keyslot_destroy(cd, keyslot_old); */
3708                                 r = -EINVAL;
3709                                 goto out;
3710                         }
3711                 }
3712
3713                 r = LUKS2_keyslot_store(cd,  &cd->u.luks2.hdr,
3714                                         keyslot_new, new_passphrase,
3715                                         new_passphrase_size, vk, &params);
3716                 if (r < 0)
3717                         goto out;
3718
3719                 /* Swap old & new so the final keyslot number remains */
3720                 if (keyslot_new_orig == CRYPT_ANY_SLOT && keyslot_old != keyslot_new) {
3721                         r = LUKS2_keyslot_swap(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new);
3722                         if (r < 0)
3723                                 goto out;
3724
3725                         /* Swap slot id */
3726                         r = keyslot_old;
3727                         keyslot_old = keyslot_new;
3728                         keyslot_new = r;
3729                 }
3730         } else
3731                 r = -EINVAL;
3732
3733         if (r >= 0 && keyslot_old != keyslot_new)
3734                 r = crypt_keyslot_destroy(cd, keyslot_old);
3735
3736         if (r < 0)
3737                 log_err(cd, _("Failed to swap new key slot."));
3738 out:
3739         crypt_free_volume_key(vk);
3740         if (r < 0) {
3741                 _luks2_rollback(cd);
3742                 return r;
3743         }
3744         return keyslot_new;
3745 }
3746
3747 int crypt_keyslot_add_by_keyfile_device_offset(struct crypt_device *cd,
3748         int keyslot,
3749         const char *keyfile,
3750         size_t keyfile_size,
3751         uint64_t keyfile_offset,
3752         const char *new_keyfile,
3753         size_t new_keyfile_size,
3754         uint64_t new_keyfile_offset)
3755 {
3756         int r;
3757         struct crypt_keyslot_context kc, new_kc;
3758
3759         if (!keyfile || !new_keyfile)
3760                 return -EINVAL;
3761
3762         crypt_keyslot_unlock_by_keyfile_init_internal(&kc, keyfile, keyfile_size, keyfile_offset);
3763         crypt_keyslot_unlock_by_keyfile_init_internal(&new_kc, new_keyfile, new_keyfile_size, new_keyfile_offset);
3764
3765         r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
3766
3767         crypt_keyslot_context_destroy_internal(&kc);
3768         crypt_keyslot_context_destroy_internal(&new_kc);
3769
3770         return r;
3771 }
3772
3773 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
3774         int keyslot,
3775         const char *keyfile,
3776         size_t keyfile_size,
3777         const char *new_keyfile,
3778         size_t new_keyfile_size)
3779 {
3780         return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot,
3781                                 keyfile, keyfile_size, 0,
3782                                 new_keyfile, new_keyfile_size, 0);
3783 }
3784
3785 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
3786         int keyslot,
3787         const char *keyfile,
3788         size_t keyfile_size,
3789         size_t keyfile_offset,
3790         const char *new_keyfile,
3791         size_t new_keyfile_size,
3792         size_t new_keyfile_offset)
3793 {
3794         return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot,
3795                                 keyfile, keyfile_size, keyfile_offset,
3796                                 new_keyfile, new_keyfile_size, new_keyfile_offset);
3797 }
3798
3799 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
3800         int keyslot,
3801         const char *volume_key,
3802         size_t volume_key_size,
3803         const char *passphrase,
3804         size_t passphrase_size)
3805 {
3806         int r;
3807         struct crypt_keyslot_context kc, new_kc;
3808
3809         if (!passphrase)
3810                 return -EINVAL;
3811
3812         crypt_keyslot_unlock_by_key_init_internal(&kc, volume_key, volume_key_size);
3813         crypt_keyslot_unlock_by_passphrase_init_internal(&new_kc, passphrase, passphrase_size);
3814
3815         r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
3816
3817         crypt_keyslot_context_destroy_internal(&kc);
3818         crypt_keyslot_context_destroy_internal(&new_kc);
3819
3820         return r;
3821 }
3822
3823 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
3824 {
3825         crypt_keyslot_info ki;
3826         int r;
3827
3828         log_dbg(cd, "Destroying keyslot %d.", keyslot);
3829
3830         if ((r = _onlyLUKS(cd, CRYPT_CD_UNRESTRICTED)))
3831                 return r;
3832
3833         ki = crypt_keyslot_status(cd, keyslot);
3834         if (ki == CRYPT_SLOT_INVALID) {
3835                 log_err(cd, _("Key slot %d is invalid."), keyslot);
3836                 return -EINVAL;
3837         }
3838
3839         if (isLUKS1(cd->type)) {
3840                 if (ki == CRYPT_SLOT_INACTIVE) {
3841                         log_err(cd, _("Keyslot %d is not active."), keyslot);
3842                         return -EINVAL;
3843                 }
3844                 return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
3845         }
3846
3847         return LUKS2_keyslot_wipe(cd, &cd->u.luks2.hdr, keyslot, 0);
3848 }
3849
3850 static int _check_header_data_overlap(struct crypt_device *cd, const char *name)
3851 {
3852         if (!name || !isLUKS(cd->type))
3853                 return 0;
3854
3855         if (device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd)) <= 0)
3856                 return 0;
3857
3858         /* FIXME: check real header size */
3859         if (crypt_get_data_offset(cd) == 0) {
3860                 log_err(cd, _("Device header overlaps with data area."));
3861                 return -EINVAL;
3862         }
3863
3864         return 0;
3865 }
3866
3867 static int check_devices(struct crypt_device *cd, const char *name, const char *iname, uint32_t *flags)
3868 {
3869         int r;
3870
3871         if (!flags || !name)
3872                 return -EINVAL;
3873
3874         if (iname) {
3875                 r = dm_status_device(cd, iname);
3876                 if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH))
3877                         return -EBUSY;
3878                 if (r < 0 && r != -ENODEV)
3879                         return r;
3880                 if (r == -ENODEV)
3881                         *flags &= ~CRYPT_ACTIVATE_REFRESH;
3882         }
3883
3884         r = dm_status_device(cd, name);
3885         if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH))
3886                 return -EBUSY;
3887         if (r < 0 && r != -ENODEV)
3888                 return r;
3889         if (r == -ENODEV)
3890                 *flags &= ~CRYPT_ACTIVATE_REFRESH;
3891
3892         return 0;
3893 }
3894
3895 static int _create_device_with_integrity(struct crypt_device *cd,
3896         const char *type, const char *name, const char *iname,
3897         const char *ipath, struct crypt_dm_active_device *dmd,
3898         struct crypt_dm_active_device *dmdi)
3899 {
3900         int r;
3901         enum devcheck device_check;
3902         struct dm_target *tgt;
3903         struct device *device = NULL;
3904
3905         if (!single_segment(dmd))
3906                 return -EINVAL;
3907
3908         tgt = &dmd->segment;
3909         if (tgt->type != DM_CRYPT)
3910                 return -EINVAL;
3911
3912         device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;
3913
3914         r = INTEGRITY_activate_dmd_device(cd, iname, CRYPT_INTEGRITY, dmdi, 0);
3915         if (r)
3916                 return r;
3917
3918         r = device_alloc(cd, &device, ipath);
3919         if (r < 0)
3920                 goto out;
3921         tgt->data_device = device;
3922
3923         r = device_block_adjust(cd, tgt->data_device, device_check,
3924                                 tgt->u.crypt.offset, &dmd->size, &dmd->flags);
3925
3926         if (!r)
3927                 r = dm_create_device(cd, name, type, dmd);
3928 out:
3929         if (r < 0)
3930                 dm_remove_device(cd, iname, 0);
3931
3932         device_free(cd, device);
3933         return r;
3934 }
3935
3936 static int kernel_keyring_support(void)
3937 {
3938         static unsigned _checked = 0;
3939
3940         if (!_checked) {
3941                 _kernel_keyring_supported = keyring_check();
3942                 _checked = 1;
3943         }
3944
3945         return _kernel_keyring_supported;
3946 }
3947
3948 static int dmcrypt_keyring_bug(void)
3949 {
3950         uint64_t kversion;
3951
3952         if (kernel_version(&kversion))
3953                 return 1;
3954         return kversion < compact_version(4,15,0,0);
3955 }
3956
3957 int create_or_reload_device(struct crypt_device *cd, const char *name,
3958                      const char *type, struct crypt_dm_active_device *dmd)
3959 {
3960         int r;
3961         enum devcheck device_check;
3962         struct dm_target *tgt;
3963
3964         if (!type || !name || !single_segment(dmd))
3965                 return -EINVAL;
3966
3967         tgt = &dmd->segment;
3968         if (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY)
3969                 return -EINVAL;
3970
3971         /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
3972         r = check_devices(cd, name, NULL, &dmd->flags);
3973         if (r)
3974                 return r;
3975
3976         if (dmd->flags & CRYPT_ACTIVATE_REFRESH)
3977                 r = _reload_device(cd, name, dmd);
3978         else {
3979                 if (tgt->type == DM_CRYPT) {
3980                         device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;
3981
3982                         r = device_block_adjust(cd, tgt->data_device, device_check,
3983                                         tgt->u.crypt.offset, &dmd->size, &dmd->flags);
3984                         if (!r) {
3985                                 tgt->size = dmd->size;
3986                                 r = dm_create_device(cd, name, type, dmd);
3987                         }
3988                 } else if (tgt->type == DM_INTEGRITY) {
3989                         r = device_block_adjust(cd, tgt->data_device, DEV_EXCL,
3990                                         tgt->u.integrity.offset, NULL, &dmd->flags);
3991                         if (r)
3992                                 return r;
3993
3994                         if (tgt->u.integrity.meta_device) {
3995                                 r = device_block_adjust(cd, tgt->u.integrity.meta_device, DEV_EXCL, 0, NULL, NULL);
3996                                 if (r)
3997                                         return r;
3998                         }
3999
4000                         r = dm_create_device(cd, name, type, dmd);
4001                 }
4002         }
4003
4004         return r;
4005 }
4006
4007 int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name,
4008                      const char *type, struct crypt_dm_active_device *dmd,
4009                      struct crypt_dm_active_device *dmdi)
4010 {
4011         int r;
4012         const char *iname = NULL;
4013         char *ipath = NULL;
4014
4015         if (!type || !name || !dmd || !dmdi)
4016                 return -EINVAL;
4017
4018         if (asprintf(&ipath, "%s/%s_dif", dm_get_dir(), name) < 0)
4019                 return -ENOMEM;
4020         iname = ipath + strlen(dm_get_dir()) + 1;
4021
4022         /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
4023         r = check_devices(cd, name, iname, &dmd->flags);
4024         if (r)
4025                 goto out;
4026
4027         if (dmd->flags & CRYPT_ACTIVATE_REFRESH)
4028                 r = _reload_device_with_integrity(cd, name, iname, ipath, dmd, dmdi);
4029         else
4030                 r = _create_device_with_integrity(cd, type, name, iname, ipath, dmd, dmdi);
4031 out:
4032         free(ipath);
4033
4034         return r;
4035 }
4036
4037 static int _open_and_activate(struct crypt_device *cd,
4038         int keyslot,
4039         const char *name,
4040         const char *passphrase,
4041         size_t passphrase_size,
4042         uint32_t flags)
4043 {
4044         bool use_keyring;
4045         int r;
4046         struct volume_key *vk = NULL;
4047
4048         r = LUKS2_keyslot_open(cd, keyslot,
4049                                (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ?
4050                                CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT,
4051                                passphrase, passphrase_size, &vk);
4052         if (r < 0)
4053                 return r;
4054         keyslot = r;
4055
4056         if (!crypt_use_keyring_for_vk(cd))
4057                 use_keyring = false;
4058         else
4059                 use_keyring = ((name && !crypt_is_cipher_null(crypt_get_cipher(cd))) ||
4060                                (flags & CRYPT_ACTIVATE_KEYRING_KEY));
4061
4062         if (use_keyring) {
4063                 r = LUKS2_volume_key_load_in_keyring_by_keyslot(cd,
4064                                 &cd->u.luks2.hdr, vk, keyslot);
4065                 if (r < 0)
4066                         goto out;
4067                 flags |= CRYPT_ACTIVATE_KEYRING_KEY;
4068         }
4069
4070         if (name)
4071                 r = LUKS2_activate(cd, name, vk, flags);
4072 out:
4073         if (r < 0)
4074                 crypt_drop_keyring_key(cd, vk);
4075         crypt_free_volume_key(vk);
4076
4077         return r < 0 ? r : keyslot;
4078 }
4079
4080 #if USE_LUKS2_REENCRYPTION
4081 static int load_all_keys(struct crypt_device *cd, struct luks2_hdr *hdr, struct volume_key *vks)
4082 {
4083         int r;
4084         struct volume_key *vk = vks;
4085
4086         while (vk) {
4087                 r = LUKS2_volume_key_load_in_keyring_by_digest(cd, vk, crypt_volume_key_get_id(vk));
4088                 if (r < 0)
4089                         return r;
4090                 vk = crypt_volume_key_next(vk);
4091         }
4092
4093         return 0;
4094 }
4095
4096 static int _open_all_keys(struct crypt_device *cd,
4097         struct luks2_hdr *hdr,
4098         int keyslot,
4099         const char *passphrase,
4100         size_t passphrase_size,
4101         uint32_t flags,
4102         struct volume_key **vks)
4103 {
4104         int r, segment;
4105         struct volume_key *_vks = NULL;
4106         crypt_reencrypt_info ri = LUKS2_reencrypt_status(hdr);
4107
4108         segment = (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ? CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT;
4109
4110         switch (ri) {
4111         case CRYPT_REENCRYPT_NONE:
4112                 r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase, passphrase_size, &_vks);
4113                 break;
4114         case CRYPT_REENCRYPT_CLEAN:
4115         case CRYPT_REENCRYPT_CRASH:
4116                 if (segment == CRYPT_ANY_SEGMENT)
4117                         r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase,
4118                                                passphrase_size, &_vks);
4119                 else
4120                         r = LUKS2_keyslot_open_all_segments(cd, keyslot,
4121                                         keyslot, passphrase, passphrase_size,
4122                                         &_vks);
4123                 break;
4124         default:
4125                 r = -EINVAL;
4126         }
4127
4128         if (keyslot == CRYPT_ANY_SLOT)
4129                 keyslot = r;
4130
4131         if (r >= 0 && (flags & CRYPT_ACTIVATE_KEYRING_KEY))
4132                 r = load_all_keys(cd, hdr, _vks);
4133
4134         if (r >= 0 && vks)
4135                 MOVE_REF(*vks, _vks);
4136
4137         if (r < 0)
4138                 crypt_drop_keyring_key(cd, _vks);
4139         crypt_free_volume_key(_vks);
4140
4141         return r < 0 ? r : keyslot;
4142 }
4143
4144 static int _open_and_activate_reencrypt_device(struct crypt_device *cd,
4145         struct luks2_hdr *hdr,
4146         int keyslot,
4147         const char *name,
4148         const char *passphrase,
4149         size_t passphrase_size,
4150         uint32_t flags)
4151 {
4152         bool dynamic_size;
4153         crypt_reencrypt_info ri;
4154         uint64_t minimal_size, device_size;
4155         struct volume_key *vks = NULL;
4156         int r = 0;
4157         struct crypt_lock_handle *reencrypt_lock = NULL;
4158
4159         if (crypt_use_keyring_for_vk(cd))
4160                 flags |= CRYPT_ACTIVATE_KEYRING_KEY;
4161
4162         r = LUKS2_reencrypt_lock(cd, &reencrypt_lock);
4163         if (r) {
4164                 if (r == -EBUSY)
4165                         log_err(cd, _("Reencryption in-progress. Cannot activate device."));
4166                 else
4167                         log_err(cd, _("Failed to get reencryption lock."));
4168                 return r;
4169         }
4170
4171         if ((r = crypt_load(cd, CRYPT_LUKS2, NULL)))
4172                 goto out;
4173
4174         ri = LUKS2_reencrypt_status(hdr);
4175
4176         if (ri == CRYPT_REENCRYPT_CRASH) {
4177                 r = LUKS2_reencrypt_locked_recovery_by_passphrase(cd, keyslot,
4178                                 keyslot, passphrase, passphrase_size, &vks);
4179                 if (r < 0) {
4180                         log_err(cd, _("LUKS2 reencryption recovery failed."));
4181                         goto out;
4182                 }
4183                 keyslot = r;
4184
4185                 ri = LUKS2_reencrypt_status(hdr);
4186         }
4187
4188         /* recovery finished reencryption or it's already finished */
4189         if (ri == CRYPT_REENCRYPT_NONE) {
4190                 crypt_drop_keyring_key(cd, vks);
4191                 crypt_free_volume_key(vks);
4192                 LUKS2_reencrypt_unlock(cd, reencrypt_lock);
4193                 return _open_and_activate(cd, keyslot, name, passphrase, passphrase_size, flags);
4194         }
4195
4196         if (ri > CRYPT_REENCRYPT_CLEAN) {
4197                 r = -EINVAL;
4198                 goto out;
4199         }
4200
4201         if (LUKS2_get_data_size(hdr, &minimal_size, &dynamic_size))
4202                 goto out;
4203
4204         if (!vks) {
4205                 r = _open_all_keys(cd, hdr, keyslot, passphrase, passphrase_size, flags, &vks);
4206                 if (r >= 0)
4207                         keyslot = r;
4208         }
4209
4210         if (r >= 0) {
4211                 r = LUKS2_reencrypt_digest_verify(cd, hdr, vks);
4212                 if (r < 0)
4213                         goto out;
4214         }
4215
4216         log_dbg(cd, "Entering clean reencryption state mode.");
4217
4218         if (r >= 0)
4219                 r = LUKS2_reencrypt_check_device_size(cd, hdr, minimal_size, &device_size, true, dynamic_size);
4220
4221         if (r >= 0)
4222                 r = LUKS2_activate_multi(cd, name, vks, device_size >> SECTOR_SHIFT, flags);
4223 out:
4224         LUKS2_reencrypt_unlock(cd, reencrypt_lock);
4225         if (r < 0)
4226                 crypt_drop_keyring_key(cd, vks);
4227         crypt_free_volume_key(vks);
4228
4229         return r < 0 ? r : keyslot;
4230 }
4231
4232 /*
4233  * Activation/deactivation of a device
4234  */
4235 static int _open_and_activate_luks2(struct crypt_device *cd,
4236         int keyslot,
4237         const char *name,
4238         const char *passphrase,
4239         size_t passphrase_size,
4240         uint32_t flags)
4241 {
4242         crypt_reencrypt_info ri;
4243         int r, rv;
4244         struct luks2_hdr *hdr = &cd->u.luks2.hdr;
4245         struct volume_key *vks = NULL;
4246
4247         ri = LUKS2_reencrypt_status(hdr);
4248         if (ri == CRYPT_REENCRYPT_INVALID)
4249                 return -EINVAL;
4250
4251         if (ri > CRYPT_REENCRYPT_NONE) {
4252                 if (name)
4253                         r = _open_and_activate_reencrypt_device(cd, hdr, keyslot, name, passphrase,
4254                                         passphrase_size, flags);
4255                 else {
4256                         r = _open_all_keys(cd, hdr, keyslot, passphrase,
4257                                            passphrase_size, flags, &vks);
4258                         if (r < 0)
4259                                 return r;
4260
4261                         rv = LUKS2_reencrypt_digest_verify(cd, hdr, vks);
4262                         crypt_free_volume_key(vks);
4263                         if (rv < 0)
4264                                 return rv;
4265                 }
4266         } else
4267                 r = _open_and_activate(cd, keyslot, name, passphrase,
4268                                 passphrase_size, flags);
4269
4270         return r;
4271 }
4272 #else
4273 static int _open_and_activate_luks2(struct crypt_device *cd,
4274         int keyslot,
4275         const char *name,
4276         const char *passphrase,
4277         size_t passphrase_size,
4278         uint32_t flags)
4279 {
4280         crypt_reencrypt_info ri;
4281
4282         ri = LUKS2_reencrypt_status(&cd->u.luks2.hdr);
4283         if (ri == CRYPT_REENCRYPT_INVALID)
4284                 return -EINVAL;
4285
4286         if (ri > CRYPT_REENCRYPT_NONE) {
4287                 log_err(cd, _("This operation is not supported for this device type."));
4288                 return -ENOTSUP;
4289         }
4290
4291         return _open_and_activate(cd, keyslot, name, passphrase, passphrase_size, flags);
4292 }
4293 #endif
4294
4295 static int _activate_by_passphrase(struct crypt_device *cd,
4296         const char *name,
4297         int keyslot,
4298         const char *passphrase,
4299         size_t passphrase_size,
4300         uint32_t flags)
4301 {
4302         int r;
4303         struct volume_key *vk = NULL;
4304
4305         if ((flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_use_keyring_for_vk(cd))
4306                 return -EINVAL;
4307
4308         if ((flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) && name)
4309                 return -EINVAL;
4310
4311         r = _check_header_data_overlap(cd, name);
4312         if (r < 0)
4313                 return r;
4314
4315         if (flags & CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF)
4316                 cd->memory_hard_pbkdf_lock_enabled = true;
4317
4318         /* plain, use hashed passphrase */
4319         if (isPLAIN(cd->type)) {
4320                 r = -EINVAL;
4321                 if (!name)
4322                         goto out;
4323
4324                 r = process_key(cd, cd->u.plain.hdr.hash,
4325                                 cd->u.plain.key_size,
4326                                 passphrase, passphrase_size, &vk);
4327                 if (r < 0)
4328                         goto out;
4329
4330                 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
4331                 keyslot = 0;
4332         } else if (isLUKS1(cd->type)) {
4333                 r = LUKS_open_key_with_hdr(keyslot, passphrase,
4334                                            passphrase_size, &cd->u.luks1.hdr, &vk, cd);
4335                 if (r >= 0) {
4336                         keyslot = r;
4337                         if (name)
4338                                 r = LUKS1_activate(cd, name, vk, flags);
4339                 }
4340         } else if (isLUKS2(cd->type)) {
4341                 r = _open_and_activate_luks2(cd, keyslot, name, passphrase, passphrase_size, flags);
4342                 keyslot = r;
4343         } else if (isBITLK(cd->type)) {
4344                 r = BITLK_activate_by_passphrase(cd, name, passphrase, passphrase_size,
4345                                                  &cd->u.bitlk.params, flags);
4346                 keyslot = 0;
4347         } else if (isFVAULT2(cd->type)) {
4348                 r = FVAULT2_activate_by_passphrase(cd, name, passphrase, passphrase_size,
4349                         &cd->u.fvault2.params, flags);
4350                 keyslot = 0;
4351         } else {
4352                 log_err(cd, _("Device type is not properly initialized."));
4353                 r = -EINVAL;
4354         }
4355 out:
4356         if (r < 0)
4357                 crypt_drop_keyring_key(cd, vk);
4358         crypt_free_volume_key(vk);
4359
4360         cd->memory_hard_pbkdf_lock_enabled = false;
4361
4362         return r < 0 ? r : keyslot;
4363 }
4364
4365 static int _activate_loopaes(struct crypt_device *cd,
4366         const char *name,
4367         char *buffer,
4368         size_t buffer_size,
4369         uint32_t flags)
4370 {
4371         int r;
4372         unsigned int key_count = 0;
4373         struct volume_key *vk = NULL;
4374
4375         r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
4376                                   buffer, buffer_size);
4377
4378         if (!r && name)
4379                 r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher, key_count,
4380                                      vk, flags);
4381
4382         crypt_free_volume_key(vk);
4383
4384         return r;
4385 }
4386
4387 static int _activate_check_status(struct crypt_device *cd, const char *name, unsigned reload)
4388 {
4389         int r;
4390
4391         if (!name)
4392                 return 0;
4393
4394         r = dm_status_device(cd, name);
4395
4396         if (r >= 0 && reload)
4397                 return 0;
4398
4399         if (r >= 0 || r == -EEXIST) {
4400                 log_err(cd, _("Device %s already exists."), name);
4401                 return -EEXIST;
4402         }
4403
4404         if (r == -ENODEV)
4405                 return 0;
4406
4407         log_err(cd, _("Cannot use device %s, name is invalid or still in use."), name);
4408         return r;
4409 }
4410
4411 // activation/deactivation of device mapping
4412 int crypt_activate_by_passphrase(struct crypt_device *cd,
4413         const char *name,
4414         int keyslot,
4415         const char *passphrase,
4416         size_t passphrase_size,
4417         uint32_t flags)
4418 {
4419         int r;
4420
4421         if (!cd || !passphrase || (!name && (flags & CRYPT_ACTIVATE_REFRESH)))
4422                 return -EINVAL;
4423
4424         log_dbg(cd, "%s volume %s [keyslot %d] using passphrase.",
4425                 name ? "Activating" : "Checking", name ?: "passphrase",
4426                 keyslot);
4427
4428         r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
4429         if (r < 0)
4430                 return r;
4431
4432         return _activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);
4433 }
4434
4435 int crypt_activate_by_keyfile_device_offset(struct crypt_device *cd,
4436         const char *name,
4437         int keyslot,
4438         const char *keyfile,
4439         size_t keyfile_size,
4440         uint64_t keyfile_offset,
4441         uint32_t flags)
4442 {
4443         char *passphrase_read = NULL;
4444         size_t passphrase_size_read;
4445         int r;
4446
4447         if (!cd || !keyfile ||
4448             ((flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_use_keyring_for_vk(cd)))
4449                 return -EINVAL;
4450
4451         log_dbg(cd, "%s volume %s [keyslot %d] using keyfile %s.",
4452                 name ? "Activating" : "Checking", name ?: "passphrase", keyslot, keyfile);
4453
4454         r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
4455         if (r < 0)
4456                 return r;
4457
4458         r = crypt_keyfile_device_read(cd, keyfile,
4459                                 &passphrase_read, &passphrase_size_read,
4460                                 keyfile_offset, keyfile_size, 0);
4461         if (r < 0)
4462                 goto out;
4463
4464         if (isLOOPAES(cd->type))
4465                 r = _activate_loopaes(cd, name, passphrase_read, passphrase_size_read, flags);
4466         else
4467                 r = _activate_by_passphrase(cd, name, keyslot, passphrase_read, passphrase_size_read, flags);
4468
4469 out:
4470         crypt_safe_free(passphrase_read);
4471         return r;
4472 }
4473
4474 int crypt_activate_by_keyfile(struct crypt_device *cd,
4475         const char *name,
4476         int keyslot,
4477         const char *keyfile,
4478         size_t keyfile_size,
4479         uint32_t flags)
4480 {
4481         return crypt_activate_by_keyfile_device_offset(cd, name, keyslot, keyfile,
4482                                         keyfile_size, 0, flags);
4483 }
4484
4485 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
4486         const char *name,
4487         int keyslot,
4488         const char *keyfile,
4489         size_t keyfile_size,
4490         size_t keyfile_offset,
4491         uint32_t flags)
4492 {
4493         return crypt_activate_by_keyfile_device_offset(cd, name, keyslot, keyfile,
4494                                         keyfile_size, keyfile_offset, flags);
4495 }
4496 int crypt_activate_by_volume_key(struct crypt_device *cd,
4497         const char *name,
4498         const char *volume_key,
4499         size_t volume_key_size,
4500         uint32_t flags)
4501 {
4502         bool use_keyring;
4503         struct volume_key *vk = NULL;
4504         int r;
4505
4506         if (!cd ||
4507             ((flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_use_keyring_for_vk(cd)))
4508                 return -EINVAL;
4509
4510         log_dbg(cd, "%s volume %s by volume key.", name ? "Activating" : "Checking",
4511                 name ?: "");
4512
4513         r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
4514         if (r < 0)
4515                 return r;
4516
4517         r = _check_header_data_overlap(cd, name);
4518         if (r < 0)
4519                 return r;
4520
4521         /* use key directly, no hash */
4522         if (isPLAIN(cd->type)) {
4523                 if (!name)
4524                         return -EINVAL;
4525
4526                 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
4527                         log_err(cd, _("Incorrect volume key specified for plain device."));
4528                         return -EINVAL;
4529                 }
4530
4531                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
4532                 if (!vk)
4533                         return -ENOMEM;
4534
4535                 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
4536         } else if (isLUKS1(cd->type)) {
4537                 /* If key is not provided, try to use internal key */
4538                 if (!volume_key) {
4539                         if (!cd->volume_key) {
4540                                 log_err(cd, _("Volume key does not match the volume."));
4541                                 return -EINVAL;
4542                         }
4543                         volume_key_size = cd->volume_key->keylength;
4544                         volume_key = cd->volume_key->key;
4545                 }
4546
4547                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
4548                 if (!vk)
4549                         return -ENOMEM;
4550                 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
4551
4552                 if (r == -EPERM)
4553                         log_err(cd, _("Volume key does not match the volume."));
4554
4555                 if (!r && name)
4556                         r = LUKS1_activate(cd, name, vk, flags);
4557         } else if (isLUKS2(cd->type)) {
4558                 /* If key is not provided, try to use internal key */
4559                 if (!volume_key) {
4560                         if (!cd->volume_key) {
4561                                 log_err(cd, _("Volume key does not match the volume."));
4562                                 return -EINVAL;
4563                         }
4564                         volume_key_size = cd->volume_key->keylength;
4565                         volume_key = cd->volume_key->key;
4566                 }
4567
4568                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
4569                 if (!vk)
4570                         return -ENOMEM;
4571
4572                 r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
4573                 if (r == -EPERM || r == -ENOENT)
4574                         log_err(cd, _("Volume key does not match the volume."));
4575                 if (r > 0)
4576                         r = 0;
4577
4578                 if (!crypt_use_keyring_for_vk(cd))
4579                         use_keyring = false;
4580                 else
4581                         use_keyring = (name && !crypt_is_cipher_null(crypt_get_cipher(cd))) ||
4582                                       (flags & CRYPT_ACTIVATE_KEYRING_KEY);
4583
4584                 if (!r && use_keyring) {
4585                         r = LUKS2_key_description_by_segment(cd,
4586                                 &cd->u.luks2.hdr, vk, CRYPT_DEFAULT_SEGMENT);
4587                         if (!r)
4588                                 r = crypt_volume_key_load_in_keyring(cd, vk);
4589                         if (!r)
4590                                 flags |= CRYPT_ACTIVATE_KEYRING_KEY;
4591                 }
4592
4593                 if (!r && name)
4594                         r = LUKS2_activate(cd, name, vk, flags);
4595         } else if (isVERITY(cd->type)) {
4596                 r = crypt_activate_by_signed_key(cd, name, volume_key, volume_key_size, NULL, 0, flags);
4597         } else if (isTCRYPT(cd->type)) {
4598                 if (!name)
4599                         return 0;
4600                 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
4601                                     &cd->u.tcrypt.params, flags);
4602         } else if (isINTEGRITY(cd->type)) {
4603                 if (!name)
4604                         return 0;
4605                 if (volume_key) {
4606                         vk = crypt_alloc_volume_key(volume_key_size, volume_key);
4607                         if (!vk)
4608                                 return -ENOMEM;
4609                 }
4610                 r = INTEGRITY_activate(cd, name, &cd->u.integrity.params, vk,
4611                                        cd->u.integrity.journal_crypt_key,
4612                                        cd->u.integrity.journal_mac_key, flags,
4613                                        cd->u.integrity.sb_flags);
4614         } else if (isBITLK(cd->type)) {
4615                 r = BITLK_activate_by_volume_key(cd, name, volume_key, volume_key_size,
4616                                                  &cd->u.bitlk.params, flags);
4617         } else {
4618                 log_err(cd, _("Device type is not properly initialized."));
4619                 r = -EINVAL;
4620         }
4621
4622         if (r < 0)
4623                 crypt_drop_keyring_key(cd, vk);
4624         crypt_free_volume_key(vk);
4625
4626         return r;
4627 }
4628
4629 int crypt_activate_by_signed_key(struct crypt_device *cd,
4630         const char *name,
4631         const char *volume_key,
4632         size_t volume_key_size,
4633         const char *signature,
4634         size_t signature_size,
4635         uint32_t flags)
4636 {
4637         char description[512];
4638         int r;
4639
4640         if (!cd || !isVERITY(cd->type))
4641                 return -EINVAL;
4642
4643         if (!volume_key || !volume_key_size || (!name && signature)) {
4644                 log_err(cd, _("Incorrect root hash specified for verity device."));
4645                 return -EINVAL;
4646         }
4647
4648         if (name)
4649                 log_dbg(cd, "Activating volume %s by %skey.", name, signature ? "signed " : "");
4650         else
4651                 log_dbg(cd, "Checking volume by key.");
4652
4653         if (cd->u.verity.hdr.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE && !signature) {
4654                 log_err(cd, _("Root hash signature required."));
4655                 return -EINVAL;
4656         }
4657
4658         r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
4659         if (r < 0)
4660                 return r;
4661
4662         if (signature && !kernel_keyring_support()) {
4663                 log_err(cd, _("Kernel keyring missing: required for passing signature to kernel."));
4664                 return -EINVAL;
4665         }
4666
4667         /* volume_key == root hash */
4668         free(CONST_CAST(void*)cd->u.verity.root_hash);
4669         cd->u.verity.root_hash = NULL;
4670
4671         if (signature) {
4672                 r = snprintf(description, sizeof(description)-1, "cryptsetup:%s%s%s",
4673                              crypt_get_uuid(cd) ?: "", crypt_get_uuid(cd) ? "-" : "", name);
4674                 if (r < 0)
4675                         return -EINVAL;
4676
4677                 log_dbg(cd, "Adding signature into keyring %s", description);
4678                 r = keyring_add_key_in_thread_keyring(USER_KEY, description, signature, signature_size);
4679                 if (r) {
4680                         log_err(cd, _("Failed to load key in kernel keyring."));
4681                         return r;
4682                 }
4683         }
4684
4685         r = VERITY_activate(cd, name, volume_key, volume_key_size,
4686                             signature ? description : NULL,
4687                             cd->u.verity.fec_device,
4688                             &cd->u.verity.hdr, flags | CRYPT_ACTIVATE_READONLY);
4689
4690         if (!r) {
4691                 cd->u.verity.root_hash_size = volume_key_size;
4692                 cd->u.verity.root_hash = malloc(volume_key_size);
4693                 if (cd->u.verity.root_hash)
4694                         memcpy(CONST_CAST(void*)cd->u.verity.root_hash, volume_key, volume_key_size);
4695         }
4696
4697         if (signature)
4698                 crypt_drop_keyring_key_by_description(cd, description, USER_KEY);
4699
4700         return r;
4701 }
4702
4703 int crypt_deactivate_by_name(struct crypt_device *cd, const char *name, uint32_t flags)
4704 {
4705         struct crypt_device *fake_cd = NULL;
4706         struct luks2_hdr *hdr2 = NULL;
4707         struct crypt_dm_active_device dmd = {};
4708         int r;
4709         uint32_t get_flags = DM_ACTIVE_DEVICE | DM_ACTIVE_UUID | DM_ACTIVE_HOLDERS;
4710
4711         if (!name)
4712                 return -EINVAL;
4713
4714         if ((flags & CRYPT_DEACTIVATE_DEFERRED) && (flags & CRYPT_DEACTIVATE_DEFERRED_CANCEL))
4715                 return -EINVAL;
4716
4717         log_dbg(cd, "Deactivating volume %s.", name);
4718
4719         if (!cd) {
4720                 r = crypt_init_by_name(&fake_cd, name);
4721                 if (r < 0)
4722                         return r;
4723                 cd = fake_cd;
4724         }
4725
4726         /* skip holders detection and early abort when some flags raised */
4727         if (flags & (CRYPT_DEACTIVATE_FORCE | CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL))
4728                 get_flags &= ~DM_ACTIVE_HOLDERS;
4729
4730         switch (crypt_status(cd, name)) {
4731                 case CRYPT_ACTIVE:
4732                 case CRYPT_BUSY:
4733                         if (flags & CRYPT_DEACTIVATE_DEFERRED_CANCEL) {
4734                                 r = dm_cancel_deferred_removal(name);
4735                                 if (r < 0)
4736                                         log_err(cd, _("Could not cancel deferred remove from device %s."), name);
4737                                 break;
4738                         }
4739
4740                         r = dm_query_device(cd, name, get_flags, &dmd);
4741                         if (r >= 0) {
4742                                 if (dmd.holders) {
4743                                         log_err(cd, _("Device %s is still in use."), name);
4744                                         r = -EBUSY;
4745                                         break;
4746                                 }
4747                         }
4748
4749                         if (isLUKS2(cd->type))
4750                                 hdr2 = crypt_get_hdr(cd, CRYPT_LUKS2);
4751
4752                         if ((dmd.uuid && !strncmp(CRYPT_LUKS2, dmd.uuid, sizeof(CRYPT_LUKS2)-1)) || hdr2)
4753                                 r = LUKS2_deactivate(cd, name, hdr2, &dmd, flags);
4754                         else if (isTCRYPT(cd->type))
4755                                 r = TCRYPT_deactivate(cd, name, flags);
4756                         else
4757                                 r = dm_remove_device(cd, name, flags);
4758                         if (r < 0 && crypt_status(cd, name) == CRYPT_BUSY) {
4759                                 log_err(cd, _("Device %s is still in use."), name);
4760                                 r = -EBUSY;
4761                         }
4762                         break;
4763                 case CRYPT_INACTIVE:
4764                         log_err(cd, _("Device %s is not active."), name);
4765                         r = -ENODEV;
4766                         break;
4767                 default:
4768                         log_err(cd, _("Invalid device %s."), name);
4769                         r = -EINVAL;
4770         }
4771
4772         dm_targets_free(cd, &dmd);
4773         free(CONST_CAST(void*)dmd.uuid);
4774         crypt_free(fake_cd);
4775
4776         return r;
4777 }
4778
4779 int crypt_deactivate(struct crypt_device *cd, const char *name)
4780 {
4781         return crypt_deactivate_by_name(cd, name, 0);
4782 }
4783
4784 int crypt_get_active_device(struct crypt_device *cd, const char *name,
4785                             struct crypt_active_device *cad)
4786 {
4787         int r;
4788         struct crypt_dm_active_device dmd, dmdi = {};
4789         const char *namei = NULL;
4790         struct dm_target *tgt = &dmd.segment;
4791         uint64_t min_offset = UINT64_MAX;
4792
4793         if (!cd || !name || !cad)
4794                 return -EINVAL;
4795
4796         r = dm_query_device(cd, name, DM_ACTIVE_DEVICE, &dmd);
4797         if (r < 0)
4798                 return r;
4799
4800         /* For LUKS2 with integrity we need flags from underlying dm-integrity */
4801         if (isLUKS2(cd->type) && crypt_get_integrity_tag_size(cd) && single_segment(&dmd)) {
4802                 namei = device_dm_name(tgt->data_device);
4803                 if (namei && dm_query_device(cd, namei, 0, &dmdi) >= 0)
4804                         dmd.flags |= dmdi.flags;
4805         }
4806
4807         if (cd && isTCRYPT(cd->type)) {
4808                 cad->offset     = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
4809                 cad->iv_offset  = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
4810         } else {
4811                 while (tgt) {
4812                         if (tgt->type == DM_CRYPT && (min_offset > tgt->u.crypt.offset)) {
4813                                 min_offset = tgt->u.crypt.offset;
4814                                 cad->iv_offset = tgt->u.crypt.iv_offset;
4815                         } else if (tgt->type == DM_INTEGRITY && (min_offset > tgt->u.integrity.offset)) {
4816                                 min_offset = tgt->u.integrity.offset;
4817                                 cad->iv_offset = 0;
4818                         } else if (tgt->type == DM_LINEAR && (min_offset > tgt->u.linear.offset)) {
4819                                 min_offset = tgt->u.linear.offset;
4820                                 cad->iv_offset = 0;
4821                         }
4822                         tgt = tgt->next;
4823                 }
4824         }
4825
4826         if (min_offset != UINT64_MAX)
4827                 cad->offset = min_offset;
4828
4829         cad->size       = dmd.size;
4830         cad->flags      = dmd.flags;
4831
4832         r = 0;
4833         dm_targets_free(cd, &dmd);
4834         dm_targets_free(cd, &dmdi);
4835
4836         return r;
4837 }
4838
4839 uint64_t crypt_get_active_integrity_failures(struct crypt_device *cd, const char *name)
4840 {
4841         struct crypt_dm_active_device dmd;
4842         uint64_t failures = 0;
4843
4844         if (!name)
4845                 return 0;
4846
4847         /* LUKS2 / dm-crypt does not provide this count. */
4848         if (dm_query_device(cd, name, 0, &dmd) < 0)
4849                 return 0;
4850
4851         if (single_segment(&dmd) && dmd.segment.type == DM_INTEGRITY)
4852                 (void)dm_status_integrity_failures(cd, name, &failures);
4853
4854         dm_targets_free(cd, &dmd);
4855
4856         return failures;
4857 }
4858
4859 /*
4860  * Volume key handling
4861  */
4862 int crypt_volume_key_get(struct crypt_device *cd,
4863         int keyslot,
4864         char *volume_key,
4865         size_t *volume_key_size,
4866         const char *passphrase,
4867         size_t passphrase_size)
4868 {
4869         int r;
4870         struct crypt_keyslot_context kc;
4871
4872         if (!passphrase)
4873                 return crypt_volume_key_get_by_keyslot_context(cd, keyslot, volume_key, volume_key_size, NULL);
4874
4875         crypt_keyslot_unlock_by_passphrase_init_internal(&kc, passphrase, passphrase_size);
4876
4877         r = crypt_volume_key_get_by_keyslot_context(cd, keyslot, volume_key, volume_key_size, &kc);
4878
4879         crypt_keyslot_context_destroy_internal(&kc);
4880
4881         return r;
4882 }
4883
4884 int crypt_volume_key_get_by_keyslot_context(struct crypt_device *cd,
4885         int keyslot,
4886         char *volume_key,
4887         size_t *volume_key_size,
4888         struct crypt_keyslot_context *kc)
4889 {
4890         size_t passphrase_size;
4891         int key_len, r;
4892         const char *passphrase = NULL;
4893         struct volume_key *vk = NULL;
4894
4895         if (!cd || !volume_key || !volume_key_size ||
4896             (!kc && !isLUKS(cd->type) && !isTCRYPT(cd->type) && !isVERITY(cd->type)))
4897                 return -EINVAL;
4898
4899         if (isLUKS2(cd->type) && keyslot != CRYPT_ANY_SLOT)
4900                 key_len = LUKS2_get_keyslot_stored_key_size(&cd->u.luks2.hdr, keyslot);
4901         else
4902                 key_len = crypt_get_volume_key_size(cd);
4903
4904         if (key_len < 0)
4905                 return -EINVAL;
4906
4907         if (key_len > (int)*volume_key_size) {
4908                 log_err(cd, _("Volume key buffer too small."));
4909                 return -ENOMEM;
4910         }
4911
4912         if (kc && (!kc->get_passphrase || kc->type == CRYPT_KC_TYPE_KEY))
4913                 return -EINVAL;
4914
4915         if (kc) {
4916                 r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size);
4917                 if (r < 0)
4918                         return r;
4919         }
4920
4921         r = -EINVAL;
4922
4923         if (isLUKS2(cd->type)) {
4924                 if (kc && !kc->get_luks2_key)
4925                         log_err(cd, _("Cannot retrieve volume key for LUKS2 device."));
4926                 else if (!kc)
4927                         r = -ENOENT;
4928                 else
4929                         r = kc->get_luks2_key(cd, kc, keyslot,
4930                                         keyslot == CRYPT_ANY_SLOT ? CRYPT_DEFAULT_SEGMENT : CRYPT_ANY_SEGMENT,
4931                                         &vk);
4932         } else if (isLUKS1(cd->type)) {
4933                 if (kc && !kc->get_luks1_volume_key)
4934                         log_err(cd, _("Cannot retrieve volume key for LUKS1 device."));
4935                 else if (!kc)
4936                         r = -ENOENT;
4937                 else
4938                         r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk);
4939         } else if (isPLAIN(cd->type)) {
4940                 if (passphrase && cd->u.plain.hdr.hash)
4941                         r = process_key(cd, cd->u.plain.hdr.hash, key_len,
4942                                         passphrase, passphrase_size, &vk);
4943                 if (r < 0)
4944                         log_err(cd, _("Cannot retrieve volume key for plain device."));
4945         } else if (isVERITY(cd->type)) {
4946                 /* volume_key == root hash */
4947                 if (cd->u.verity.root_hash) {
4948                         memcpy(volume_key, cd->u.verity.root_hash, cd->u.verity.root_hash_size);
4949                         *volume_key_size = cd->u.verity.root_hash_size;
4950                         r = 0;
4951                 } else
4952                         log_err(cd, _("Cannot retrieve root hash for verity device."));
4953         } else if (isTCRYPT(cd->type)) {
4954                 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
4955         } else if (isBITLK(cd->type)) {
4956                 if (passphrase)
4957                         r = BITLK_get_volume_key(cd, passphrase, passphrase_size, &cd->u.bitlk.params, &vk);
4958                 if (r < 0)
4959                         log_err(cd, _("Cannot retrieve volume key for BITLK device."));
4960         } else if (isFVAULT2(cd->type)) {
4961                 if (passphrase)
4962                         r = FVAULT2_get_volume_key(cd, passphrase, passphrase_size, &cd->u.fvault2.params, &vk);
4963                 if (r < 0)
4964                         log_err(cd, _("Cannot retrieve volume key for FVAULT2 device."));
4965         } else
4966                 log_err(cd, _("This operation is not supported for %s crypt device."), cd->type ?: "(none)");
4967
4968         if (r == -ENOENT && isLUKS(cd->type) && cd->volume_key) {
4969                 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
4970                 r = vk ? 0 : -ENOMEM;
4971         }
4972
4973         if (r >= 0 && vk) {
4974                 memcpy(volume_key, vk->key, vk->keylength);
4975                 *volume_key_size = vk->keylength;
4976         }
4977
4978         crypt_free_volume_key(vk);
4979         return r;
4980 }
4981
4982 int crypt_volume_key_verify(struct crypt_device *cd,
4983         const char *volume_key,
4984         size_t volume_key_size)
4985 {
4986         struct volume_key *vk;
4987         int r;
4988
4989         if ((r = _onlyLUKS(cd, CRYPT_CD_UNRESTRICTED)))
4990                 return r;
4991
4992         vk = crypt_alloc_volume_key(volume_key_size, volume_key);
4993         if (!vk)
4994                 return -ENOMEM;
4995
4996         if (isLUKS1(cd->type))
4997                 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
4998         else if (isLUKS2(cd->type))
4999                 r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
5000         else
5001                 r = -EINVAL;
5002
5003         crypt_free_volume_key(vk);
5004
5005         return r >= 0 ? 0 : r;
5006 }
5007
5008 /*
5009  * RNG and memory locking
5010  */
5011 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
5012 {
5013         if (!cd)
5014                 return;
5015
5016         switch (rng_type) {
5017         case CRYPT_RNG_URANDOM:
5018         case CRYPT_RNG_RANDOM:
5019                 log_dbg(cd, "RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
5020                 cd->rng_type = rng_type;
5021         }
5022 }
5023
5024 int crypt_get_rng_type(struct crypt_device *cd)
5025 {
5026         if (!cd)
5027                 return -EINVAL;
5028
5029         return cd->rng_type;
5030 }
5031
5032 int crypt_memory_lock(struct crypt_device *cd, int lock)
5033 {
5034         return 0;
5035 }
5036
5037 void crypt_set_compatibility(struct crypt_device *cd, uint32_t flags)
5038 {
5039         if (cd)
5040                 cd->compatibility = flags;
5041 }
5042
5043 uint32_t crypt_get_compatibility(struct crypt_device *cd)
5044 {
5045         if (cd)
5046                 return cd->compatibility;
5047
5048         return 0;
5049 }
5050
5051 /*
5052  * Reporting
5053  */
5054 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
5055 {
5056         int r;
5057
5058         if (!name)
5059                 return CRYPT_INVALID;
5060
5061         if (!cd)
5062                 dm_backend_init(cd);
5063
5064         r = dm_status_device(cd, name);
5065
5066         if (!cd)
5067                 dm_backend_exit(cd);
5068
5069         if (r < 0 && r != -ENODEV)
5070                 return CRYPT_INVALID;
5071
5072         if (r == 0)
5073                 return CRYPT_ACTIVE;
5074
5075         if (r > 0)
5076                 return CRYPT_BUSY;
5077
5078         return CRYPT_INACTIVE;
5079 }
5080
5081 static int _luks_dump(struct crypt_device *cd)
5082 {
5083         int i;
5084
5085         log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
5086         log_std(cd, "Version:       \t%" PRIu16 "\n", cd->u.luks1.hdr.version);
5087         log_std(cd, "Cipher name:   \t%s\n", cd->u.luks1.hdr.cipherName);
5088         log_std(cd, "Cipher mode:   \t%s\n", cd->u.luks1.hdr.cipherMode);
5089         log_std(cd, "Hash spec:     \t%s\n", cd->u.luks1.hdr.hashSpec);
5090         log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset);
5091         log_std(cd, "MK bits:       \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8);
5092         log_std(cd, "MK digest:     \t");
5093         crypt_log_hex(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ", 0, NULL);
5094         log_std(cd, "\n");
5095         log_std(cd, "MK salt:       \t");
5096         crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ", 0, NULL);
5097         log_std(cd, "\n               \t");
5098         crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL);
5099         log_std(cd, "\n");
5100         log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations);
5101         log_std(cd, "UUID:          \t%s\n\n", cd->u.luks1.hdr.uuid);
5102         for(i = 0; i < LUKS_NUMKEYS; i++) {
5103                 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
5104                         log_std(cd, "Key Slot %d: ENABLED\n",i);
5105                         log_std(cd, "\tIterations:         \t%" PRIu32 "\n",
5106                                 cd->u.luks1.hdr.keyblock[i].passwordIterations);
5107                         log_std(cd, "\tSalt:               \t");
5108                         crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
5109                                  LUKS_SALTSIZE/2, " ", 0, NULL);
5110                         log_std(cd, "\n\t                      \t");
5111                         crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
5112                                  LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL);
5113                         log_std(cd, "\n");
5114
5115                         log_std(cd, "\tKey material offset:\t%" PRIu32 "\n",
5116                                 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
5117                         log_std(cd, "\tAF stripes:            \t%" PRIu32 "\n",
5118                                 cd->u.luks1.hdr.keyblock[i].stripes);
5119                 }
5120                 else
5121                         log_std(cd, "Key Slot %d: DISABLED\n", i);
5122         }
5123         return 0;
5124 }
5125
5126 int crypt_dump(struct crypt_device *cd)
5127 {
5128         if (!cd)
5129                 return -EINVAL;
5130         if (isLUKS1(cd->type))
5131                 return _luks_dump(cd);
5132         else if (isLUKS2(cd->type))
5133                 return LUKS2_hdr_dump(cd, &cd->u.luks2.hdr);
5134         else if (isVERITY(cd->type))
5135                 return VERITY_dump(cd, &cd->u.verity.hdr,
5136                                    cd->u.verity.root_hash, cd->u.verity.root_hash_size,
5137                                    cd->u.verity.fec_device);
5138         else if (isTCRYPT(cd->type))
5139                 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
5140         else if (isINTEGRITY(cd->type))
5141                 return INTEGRITY_dump(cd, crypt_data_device(cd), 0);
5142         else if (isBITLK(cd->type))
5143                 return BITLK_dump(cd, crypt_data_device(cd), &cd->u.bitlk.params);
5144         else if (isFVAULT2(cd->type))
5145                 return FVAULT2_dump(cd, crypt_data_device(cd), &cd->u.fvault2.params);
5146
5147         log_err(cd, _("Dump operation is not supported for this device type."));
5148         return -EINVAL;
5149 }
5150
5151 int crypt_dump_json(struct crypt_device *cd, const char **json, uint32_t flags)
5152 {
5153         if (!cd || flags)
5154                 return -EINVAL;
5155         if (isLUKS2(cd->type))
5156                 return LUKS2_hdr_dump_json(cd, &cd->u.luks2.hdr, json);
5157
5158         log_err(cd, _("Dump operation is not supported for this device type."));
5159         return -EINVAL;
5160 }
5161
5162 /* internal only */
5163 const char *crypt_get_cipher_spec(struct crypt_device *cd)
5164 {
5165         if (!cd)
5166                 return NULL;
5167         else if (isLUKS2(cd->type))
5168                 return LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
5169         else if (isLUKS1(cd->type))
5170                 return cd->u.luks1.cipher_spec;
5171         else if (isPLAIN(cd->type))
5172                 return cd->u.plain.cipher_spec;
5173         else if (isLOOPAES(cd->type))
5174                 return cd->u.loopaes.cipher_spec;
5175         else if (isBITLK(cd->type))
5176                 return cd->u.bitlk.cipher_spec;
5177         else if (!cd->type && !_init_by_name_crypt_none(cd))
5178                 return cd->u.none.cipher_spec;
5179
5180         return NULL;
5181 }
5182
5183 const char *crypt_get_cipher(struct crypt_device *cd)
5184 {
5185         if (!cd)
5186                 return NULL;
5187
5188         if (isPLAIN(cd->type))
5189                 return cd->u.plain.cipher;
5190
5191         if (isLUKS1(cd->type))
5192                 return cd->u.luks1.hdr.cipherName;
5193
5194         if (isLUKS2(cd->type)) {
5195                 if (crypt_parse_name_and_mode(LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT),
5196                                               cd->u.luks2.cipher, NULL, cd->u.luks2.cipher_mode))
5197                         return NULL;
5198                 return cd->u.luks2.cipher;
5199         }
5200
5201         if (isLOOPAES(cd->type))
5202                 return cd->u.loopaes.cipher;
5203
5204         if (isTCRYPT(cd->type))
5205                 return cd->u.tcrypt.params.cipher;
5206
5207         if (isBITLK(cd->type))
5208                 return cd->u.bitlk.params.cipher;
5209
5210         if (isFVAULT2(cd->type))
5211                 return cd->u.fvault2.params.cipher;
5212
5213         if (!cd->type && !_init_by_name_crypt_none(cd))
5214                 return cd->u.none.cipher;
5215
5216         return NULL;
5217 }
5218
5219 const char *crypt_get_cipher_mode(struct crypt_device *cd)
5220 {
5221         if (!cd)
5222                 return NULL;
5223
5224         if (isPLAIN(cd->type))
5225                 return cd->u.plain.cipher_mode;
5226
5227         if (isLUKS1(cd->type))
5228                 return cd->u.luks1.hdr.cipherMode;
5229
5230         if (isLUKS2(cd->type)) {
5231                 if (crypt_parse_name_and_mode(LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT),
5232                                               cd->u.luks2.cipher, NULL, cd->u.luks2.cipher_mode))
5233                         return NULL;
5234                 return cd->u.luks2.cipher_mode;
5235         }
5236
5237         if (isLOOPAES(cd->type))
5238                 return cd->u.loopaes.cipher_mode;
5239
5240         if (isTCRYPT(cd->type))
5241                 return cd->u.tcrypt.params.mode;
5242
5243         if (isBITLK(cd->type))
5244                 return cd->u.bitlk.params.cipher_mode;
5245
5246         if (isFVAULT2(cd->type))
5247                 return cd->u.fvault2.params.cipher_mode;
5248
5249         if (!cd->type && !_init_by_name_crypt_none(cd))
5250                 return cd->u.none.cipher_mode;
5251
5252         return NULL;
5253 }
5254
5255 /* INTERNAL only */
5256 const char *crypt_get_integrity(struct crypt_device *cd)
5257 {
5258         if (!cd)
5259                 return NULL;
5260
5261         if (isINTEGRITY(cd->type))
5262                 return cd->u.integrity.params.integrity;
5263
5264         if (isLUKS2(cd->type))
5265                 return LUKS2_get_integrity(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
5266
5267         return NULL;
5268 }
5269
5270 /* INTERNAL only */
5271 int crypt_get_integrity_key_size(struct crypt_device *cd)
5272 {
5273         int key_size = 0;
5274
5275         if (isINTEGRITY(cd->type))
5276                 key_size = INTEGRITY_key_size(crypt_get_integrity(cd));
5277
5278         if (isLUKS2(cd->type))
5279                 key_size = INTEGRITY_key_size(crypt_get_integrity(cd));
5280
5281         return key_size > 0 ? key_size : 0;
5282 }
5283
5284 /* INTERNAL only */
5285 int crypt_get_integrity_tag_size(struct crypt_device *cd)
5286 {
5287         if (isINTEGRITY(cd->type))
5288                 return cd->u.integrity.params.tag_size;
5289
5290         if (isLUKS2(cd->type))
5291                 return INTEGRITY_tag_size(crypt_get_integrity(cd),
5292                                           crypt_get_cipher(cd),
5293                                           crypt_get_cipher_mode(cd));
5294         return 0;
5295 }
5296
5297 int crypt_get_sector_size(struct crypt_device *cd)
5298 {
5299         if (!cd)
5300                 return SECTOR_SIZE;
5301
5302         if (isPLAIN(cd->type))
5303                 return cd->u.plain.hdr.sector_size;
5304
5305         if (isINTEGRITY(cd->type))
5306                 return cd->u.integrity.params.sector_size;
5307
5308         if (isLUKS2(cd->type))
5309                 return LUKS2_get_sector_size(&cd->u.luks2.hdr);
5310
5311         return SECTOR_SIZE;
5312 }
5313
5314 const char *crypt_get_uuid(struct crypt_device *cd)
5315 {
5316         if (!cd)
5317                 return NULL;
5318
5319         if (isLUKS1(cd->type))
5320                 return cd->u.luks1.hdr.uuid;
5321
5322         if (isLUKS2(cd->type))
5323                 return cd->u.luks2.hdr.uuid;
5324
5325         if (isVERITY(cd->type))
5326                 return cd->u.verity.uuid;
5327
5328         if (isBITLK(cd->type))
5329                 return cd->u.bitlk.params.guid;
5330
5331         if (isFVAULT2(cd->type))
5332                 return cd->u.fvault2.params.family_uuid;
5333
5334         return NULL;
5335 }
5336
5337 const char *crypt_get_device_name(struct crypt_device *cd)
5338 {
5339         const char *path;
5340
5341         if (!cd)
5342                 return NULL;
5343
5344         path = device_block_path(cd->device);
5345         if (!path)
5346                 path = device_path(cd->device);
5347
5348         return path;
5349 }
5350
5351 const char *crypt_get_metadata_device_name(struct crypt_device *cd)
5352 {
5353         const char *path;
5354
5355         if (!cd || !cd->metadata_device)
5356                 return NULL;
5357
5358         path = device_block_path(cd->metadata_device);
5359         if (!path)
5360                 path = device_path(cd->metadata_device);
5361
5362         return path;
5363 }
5364
5365 int crypt_get_volume_key_size(struct crypt_device *cd)
5366 {
5367         int r;
5368
5369         if (!cd)
5370                 return 0;
5371
5372         if (isPLAIN(cd->type))
5373                 return cd->u.plain.key_size;
5374
5375         if (isLUKS1(cd->type))
5376                 return cd->u.luks1.hdr.keyBytes;
5377
5378         if (isLUKS2(cd->type)) {
5379                 r = LUKS2_get_volume_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
5380                 if (r < 0 && cd->volume_key)
5381                         r = cd->volume_key->keylength;
5382                 return r < 0 ? 0 : r;
5383         }
5384
5385         if (isLOOPAES(cd->type))
5386                 return cd->u.loopaes.key_size;
5387
5388         if (isVERITY(cd->type))
5389                 return cd->u.verity.root_hash_size;
5390
5391         if (isTCRYPT(cd->type))
5392                 return cd->u.tcrypt.params.key_size;
5393
5394         if (isBITLK(cd->type))
5395                 return cd->u.bitlk.params.key_size / 8;
5396
5397         if (isFVAULT2(cd->type))
5398                 return cd->u.fvault2.params.key_size;
5399
5400         if (!cd->type && !_init_by_name_crypt_none(cd))
5401                 return cd->u.none.key_size;
5402
5403         return 0;
5404 }
5405
5406 int crypt_keyslot_get_key_size(struct crypt_device *cd, int keyslot)
5407 {
5408         if (!cd || !isLUKS(cd->type))
5409                 return -EINVAL;
5410
5411         if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type))
5412                 return -EINVAL;
5413
5414         if (isLUKS1(cd->type))
5415                 return cd->u.luks1.hdr.keyBytes;
5416
5417         if (isLUKS2(cd->type))
5418                 return LUKS2_get_keyslot_stored_key_size(&cd->u.luks2.hdr, keyslot);
5419
5420         return -EINVAL;
5421 }
5422
5423 int crypt_keyslot_set_encryption(struct crypt_device *cd,
5424         const char *cipher,
5425         size_t key_size)
5426 {
5427         char *tmp;
5428
5429         if (!cd || !cipher || !key_size || !isLUKS2(cd->type))
5430                 return -EINVAL;
5431
5432         if (LUKS2_keyslot_cipher_incompatible(cd, cipher))
5433                 return -EINVAL;
5434
5435         if (!(tmp = strdup(cipher)))
5436                 return -ENOMEM;
5437
5438         free(cd->u.luks2.keyslot_cipher);
5439         cd->u.luks2.keyslot_cipher = tmp;
5440         cd->u.luks2.keyslot_key_size = key_size;
5441
5442         return 0;
5443 }
5444
5445 const char *crypt_keyslot_get_encryption(struct crypt_device *cd, int keyslot, size_t *key_size)
5446 {
5447         const char *cipher;
5448
5449         if (!cd || !isLUKS(cd->type) || !key_size)
5450                 return NULL;
5451
5452         if (isLUKS1(cd->type)) {
5453                 if (keyslot != CRYPT_ANY_SLOT &&
5454                     LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot) < CRYPT_SLOT_ACTIVE)
5455                         return NULL;
5456                 *key_size = crypt_get_volume_key_size(cd);
5457                 return cd->u.luks1.cipher_spec;
5458         }
5459
5460         if (keyslot != CRYPT_ANY_SLOT)
5461                 return LUKS2_get_keyslot_cipher(&cd->u.luks2.hdr, keyslot, key_size);
5462
5463         /* Keyslot encryption was set through crypt_keyslot_set_encryption() */
5464         if (cd->u.luks2.keyslot_cipher) {
5465                 *key_size = cd->u.luks2.keyslot_key_size;
5466                 return cd->u.luks2.keyslot_cipher;
5467         }
5468
5469         /* Try to reuse volume encryption parameters */
5470         cipher =  LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
5471         if (!LUKS2_keyslot_cipher_incompatible(cd, cipher)) {
5472                 *key_size = crypt_get_volume_key_size(cd);
5473                 if (*key_size)
5474                         return cipher;
5475         }
5476
5477         /* Fallback to default LUKS2 keyslot encryption */
5478         *key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8;
5479         return DEFAULT_LUKS2_KEYSLOT_CIPHER;
5480 }
5481
5482 int crypt_keyslot_get_pbkdf(struct crypt_device *cd, int keyslot, struct crypt_pbkdf_type *pbkdf)
5483 {
5484         if (!cd || !pbkdf || keyslot == CRYPT_ANY_SLOT)
5485                 return -EINVAL;
5486
5487         if (isLUKS1(cd->type))
5488                 return LUKS_keyslot_pbkdf(&cd->u.luks1.hdr, keyslot, pbkdf);
5489         else if (isLUKS2(cd->type))
5490                 return LUKS2_keyslot_pbkdf(&cd->u.luks2.hdr, keyslot, pbkdf);
5491
5492         return -EINVAL;
5493 }
5494
5495 int crypt_set_data_offset(struct crypt_device *cd, uint64_t data_offset)
5496 {
5497         if (!cd)
5498                 return -EINVAL;
5499         if (data_offset % (MAX_SECTOR_SIZE >> SECTOR_SHIFT)) {
5500                 log_err(cd, _("Data offset is not multiple of %u bytes."), MAX_SECTOR_SIZE);
5501                 return -EINVAL;
5502         }
5503
5504         cd->data_offset = data_offset;
5505         log_dbg(cd, "Data offset set to %" PRIu64 " (512-byte) sectors.", data_offset);
5506
5507         return 0;
5508 }
5509
5510 int crypt_set_metadata_size(struct crypt_device *cd,
5511         uint64_t metadata_size,
5512         uint64_t keyslots_size)
5513 {
5514         if (!cd)
5515                 return -EINVAL;
5516
5517         if (cd->type && !isLUKS2(cd->type))
5518                 return -EINVAL;
5519
5520         if (metadata_size && LUKS2_check_metadata_area_size(metadata_size))
5521                 return -EINVAL;
5522
5523         if (keyslots_size && LUKS2_check_keyslots_area_size(keyslots_size))
5524                 return -EINVAL;
5525
5526         cd->metadata_size = metadata_size;
5527         cd->keyslots_size = keyslots_size;
5528
5529         return 0;
5530 }
5531
5532 int crypt_get_metadata_size(struct crypt_device *cd,
5533         uint64_t *metadata_size,
5534         uint64_t *keyslots_size)
5535 {
5536         uint64_t msize, ksize;
5537
5538         if (!cd)
5539                 return -EINVAL;
5540
5541         if (!cd->type) {
5542                 msize = cd->metadata_size;
5543                 ksize = cd->keyslots_size;
5544         } else if (isLUKS1(cd->type)) {
5545                 msize = LUKS_ALIGN_KEYSLOTS;
5546                 ksize = LUKS_device_sectors(&cd->u.luks1.hdr) * SECTOR_SIZE - msize;
5547         } else if (isLUKS2(cd->type)) {
5548                 msize = LUKS2_metadata_size(&cd->u.luks2.hdr);
5549                 ksize = LUKS2_keyslots_size(&cd->u.luks2.hdr);
5550         } else
5551                 return -EINVAL;
5552
5553         if (metadata_size)
5554                 *metadata_size = msize;
5555         if (keyslots_size)
5556                 *keyslots_size = ksize;
5557
5558         return 0;
5559 }
5560
5561 uint64_t crypt_get_data_offset(struct crypt_device *cd)
5562 {
5563         if (!cd)
5564                 return 0;
5565
5566         if (isPLAIN(cd->type))
5567                 return cd->u.plain.hdr.offset;
5568
5569         if (isLUKS1(cd->type))
5570                 return cd->u.luks1.hdr.payloadOffset;
5571
5572         if (isLUKS2(cd->type))
5573                 return LUKS2_get_data_offset(&cd->u.luks2.hdr);
5574
5575         if (isLOOPAES(cd->type))
5576                 return cd->u.loopaes.hdr.offset;
5577
5578         if (isTCRYPT(cd->type))
5579                 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
5580
5581         if (isBITLK(cd->type))
5582                 return cd->u.bitlk.params.volume_header_size / SECTOR_SIZE;
5583
5584         if (isFVAULT2(cd->type))
5585                 return cd->u.fvault2.params.log_vol_off / SECTOR_SIZE;
5586
5587         return cd->data_offset;
5588 }
5589
5590 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
5591 {
5592         if (!cd)
5593                 return 0;
5594
5595         if (isPLAIN(cd->type))
5596                 return cd->u.plain.hdr.skip;
5597
5598         if (isLOOPAES(cd->type))
5599                 return cd->u.loopaes.hdr.skip;
5600
5601         if (isTCRYPT(cd->type))
5602                 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
5603
5604         return 0;
5605 }
5606
5607 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
5608 {
5609         if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED) < 0)
5610                 return CRYPT_SLOT_INVALID;
5611
5612         if (isLUKS1(cd->type))
5613                 return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
5614         else if(isLUKS2(cd->type))
5615                 return LUKS2_keyslot_info(&cd->u.luks2.hdr, keyslot);
5616
5617         return CRYPT_SLOT_INVALID;
5618 }
5619
5620 int crypt_keyslot_max(const char *type)
5621 {
5622         if (isLUKS1(type))
5623                 return LUKS_NUMKEYS;
5624
5625         if (isLUKS2(type))
5626                 return LUKS2_KEYSLOTS_MAX;
5627
5628         return -EINVAL;
5629 }
5630
5631 int crypt_keyslot_area(struct crypt_device *cd,
5632         int keyslot,
5633         uint64_t *offset,
5634         uint64_t *length)
5635 {
5636         if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED) || !offset || !length)
5637                 return -EINVAL;
5638
5639         if (isLUKS2(cd->type))
5640                 return LUKS2_keyslot_area(&cd->u.luks2.hdr, keyslot, offset, length);
5641
5642         return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
5643 }
5644
5645 crypt_keyslot_priority crypt_keyslot_get_priority(struct crypt_device *cd, int keyslot)
5646 {
5647         if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED))
5648                 return CRYPT_SLOT_PRIORITY_INVALID;
5649
5650         if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type))
5651                 return CRYPT_SLOT_PRIORITY_INVALID;
5652
5653         if (isLUKS2(cd->type))
5654                 return LUKS2_keyslot_priority_get(&cd->u.luks2.hdr, keyslot);
5655
5656         return CRYPT_SLOT_PRIORITY_NORMAL;
5657 }
5658
5659 int crypt_keyslot_set_priority(struct crypt_device *cd, int keyslot, crypt_keyslot_priority priority)
5660 {
5661         int r;
5662
5663         log_dbg(cd, "Setting keyslot %d to priority %d.", keyslot, priority);
5664
5665         if (priority == CRYPT_SLOT_PRIORITY_INVALID)
5666                 return -EINVAL;
5667
5668         if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type))
5669                 return -EINVAL;
5670
5671         if ((r = onlyLUKS2(cd)))
5672                 return r;
5673
5674         return LUKS2_keyslot_priority_set(cd, &cd->u.luks2.hdr, keyslot, priority, 1);
5675 }
5676
5677 const char *crypt_get_type(struct crypt_device *cd)
5678 {
5679         return cd ? cd->type : NULL;
5680 }
5681
5682 const char *crypt_get_default_type(void)
5683 {
5684         return DEFAULT_LUKS_FORMAT;
5685 }
5686
5687 int crypt_get_verity_info(struct crypt_device *cd,
5688         struct crypt_params_verity *vp)
5689 {
5690         if (!cd || !isVERITY(cd->type) || !vp)
5691                 return -EINVAL;
5692
5693         vp->data_device = device_path(cd->device);
5694         vp->hash_device = mdata_device_path(cd);
5695         vp->fec_device  = device_path(cd->u.verity.fec_device);
5696         vp->fec_area_offset = cd->u.verity.hdr.fec_area_offset;
5697         vp->fec_roots = cd->u.verity.hdr.fec_roots;
5698         vp->hash_name = cd->u.verity.hdr.hash_name;
5699         vp->salt = cd->u.verity.hdr.salt;
5700         vp->salt_size = cd->u.verity.hdr.salt_size;
5701         vp->data_block_size = cd->u.verity.hdr.data_block_size;
5702         vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
5703         vp->data_size = cd->u.verity.hdr.data_size;
5704         vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
5705         vp->hash_type = cd->u.verity.hdr.hash_type;
5706         vp->flags = cd->u.verity.hdr.flags & (CRYPT_VERITY_NO_HEADER | CRYPT_VERITY_ROOT_HASH_SIGNATURE);
5707         return 0;
5708 }
5709
5710 int crypt_get_integrity_info(struct crypt_device *cd,
5711         struct crypt_params_integrity *ip)
5712 {
5713         if (!cd || !ip)
5714                 return -EINVAL;
5715
5716         if (isINTEGRITY(cd->type)) {
5717                 ip->journal_size = cd->u.integrity.params.journal_size;
5718                 ip->journal_watermark = cd->u.integrity.params.journal_watermark;
5719                 ip->journal_commit_time = cd->u.integrity.params.journal_commit_time;
5720                 ip->interleave_sectors = cd->u.integrity.params.interleave_sectors;
5721                 ip->tag_size = cd->u.integrity.params.tag_size;
5722                 ip->sector_size = cd->u.integrity.params.sector_size;
5723                 ip->buffer_sectors = cd->u.integrity.params.buffer_sectors;
5724
5725                 ip->integrity = cd->u.integrity.params.integrity;
5726                 ip->integrity_key_size = crypt_get_integrity_key_size(cd);
5727
5728                 ip->journal_integrity = cd->u.integrity.params.journal_integrity;
5729                 ip->journal_integrity_key_size = cd->u.integrity.params.journal_integrity_key_size;
5730                 ip->journal_integrity_key = NULL;
5731
5732                 ip->journal_crypt = cd->u.integrity.params.journal_crypt;
5733                 ip->journal_crypt_key_size = cd->u.integrity.params.journal_crypt_key_size;
5734                 ip->journal_crypt_key = NULL;
5735                 return 0;
5736         } else if (isLUKS2(cd->type)) {
5737                 ip->journal_size = 0; // FIXME
5738                 ip->journal_watermark = 0; // FIXME
5739                 ip->journal_commit_time = 0; // FIXME
5740                 ip->interleave_sectors = 0; // FIXME
5741                 ip->sector_size = crypt_get_sector_size(cd);
5742                 ip->buffer_sectors = 0; // FIXME
5743
5744                 ip->integrity = LUKS2_get_integrity(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
5745                 ip->integrity_key_size = crypt_get_integrity_key_size(cd);
5746                 ip->tag_size = INTEGRITY_tag_size(ip->integrity, crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
5747
5748                 ip->journal_integrity = NULL;
5749                 ip->journal_integrity_key_size = 0;
5750                 ip->journal_integrity_key = NULL;
5751
5752                 ip->journal_crypt = NULL;
5753                 ip->journal_crypt_key_size = 0;
5754                 ip->journal_crypt_key = NULL;
5755                 return 0;
5756         }
5757
5758         return -ENOTSUP;
5759 }
5760
5761 int crypt_convert(struct crypt_device *cd,
5762                   const char *type,
5763                   void *params)
5764 {
5765         struct luks_phdr hdr1;
5766         struct luks2_hdr hdr2;
5767         int r;
5768
5769         if (!type)
5770                 return -EINVAL;
5771
5772         log_dbg(cd, "Converting LUKS device to type %s", type);
5773
5774         if ((r = onlyLUKS(cd)))
5775                 return r;
5776
5777         if (isLUKS1(cd->type) && isLUKS2(type))
5778                 r = LUKS2_luks1_to_luks2(cd, &cd->u.luks1.hdr, &hdr2);
5779         else if (isLUKS2(cd->type) && isLUKS1(type))
5780                 r = LUKS2_luks2_to_luks1(cd, &cd->u.luks2.hdr, &hdr1);
5781         else
5782                 return -EINVAL;
5783
5784         if (r < 0) {
5785                 /* in-memory header may be invalid after failed conversion */
5786                 _luks2_rollback(cd);
5787                 if (r == -EBUSY)
5788                         log_err(cd, _("Cannot convert device %s which is still in use."), mdata_device_path(cd));
5789                 return r;
5790         }
5791
5792         crypt_free_type(cd, NULL);
5793
5794         return crypt_load(cd, type, params);
5795 }
5796
5797 /* Internal access function to header pointer */
5798 void *crypt_get_hdr(struct crypt_device *cd, const char *type)
5799 {
5800         /* If requested type differs, ignore it */
5801         if (strcmp(cd->type, type))
5802                 return NULL;
5803
5804         if (isPLAIN(cd->type))
5805                 return &cd->u.plain;
5806
5807         if (isLUKS1(cd->type))
5808                 return &cd->u.luks1.hdr;
5809
5810         if (isLUKS2(cd->type))
5811                 return &cd->u.luks2.hdr;
5812
5813         if (isLOOPAES(cd->type))
5814                 return &cd->u.loopaes;
5815
5816         if (isVERITY(cd->type))
5817                 return &cd->u.verity;
5818
5819         if (isTCRYPT(cd->type))
5820                 return &cd->u.tcrypt;
5821
5822         return NULL;
5823 }
5824
5825 /* internal only */
5826 struct luks2_reencrypt *crypt_get_luks2_reencrypt(struct crypt_device *cd)
5827 {
5828         return cd->u.luks2.rh;
5829 }
5830
5831 /* internal only */
5832 void crypt_set_luks2_reencrypt(struct crypt_device *cd, struct luks2_reencrypt *rh)
5833 {
5834         cd->u.luks2.rh = rh;
5835 }
5836
5837 /*
5838  * Token handling
5839  */
5840 int crypt_activate_by_token_pin(struct crypt_device *cd, const char *name,
5841         const char *type, int token, const char *pin, size_t pin_size,
5842         void *usrptr, uint32_t flags)
5843 {
5844         int r;
5845
5846         log_dbg(cd, "%s volume %s using token (%s type) %d.",
5847                 name ? "Activating" : "Checking", name ?: "passphrase",
5848                 type ?: "any", token);
5849
5850         if ((r = _onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)))
5851                 return r;
5852
5853         if ((flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_use_keyring_for_vk(cd))
5854                 return -EINVAL;
5855
5856         if ((flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) && name)
5857                 return -EINVAL;
5858
5859         r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
5860         if (r < 0)
5861                 return r;
5862
5863         return LUKS2_token_open_and_activate(cd, &cd->u.luks2.hdr, token, name, type,
5864                                              pin, pin_size, flags, usrptr);
5865 }
5866
5867 int crypt_activate_by_token(struct crypt_device *cd,
5868         const char *name, int token, void *usrptr, uint32_t flags)
5869 {
5870         return crypt_activate_by_token_pin(cd, name, NULL, token, NULL, 0, usrptr, flags);
5871 }
5872
5873 int crypt_token_json_get(struct crypt_device *cd, int token, const char **json)
5874 {
5875         int r;
5876
5877         if (!json)
5878                 return -EINVAL;
5879
5880         log_dbg(cd, "Requesting JSON for token %d.", token);
5881
5882         if ((r = _onlyLUKS2(cd, CRYPT_CD_UNRESTRICTED, 0)))
5883                 return r;
5884
5885         return LUKS2_token_json_get(&cd->u.luks2.hdr, token, json) ?: token;
5886 }
5887
5888 int crypt_token_json_set(struct crypt_device *cd, int token, const char *json)
5889 {
5890         int r;
5891
5892         log_dbg(cd, "Updating JSON for token %d.", token);
5893
5894         if ((r = onlyLUKS2(cd)))
5895                 return r;
5896
5897         return LUKS2_token_create(cd, &cd->u.luks2.hdr, token, json, 1);
5898 }
5899
5900 crypt_token_info crypt_token_status(struct crypt_device *cd, int token, const char **type)
5901 {
5902         if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
5903                 return CRYPT_TOKEN_INVALID;
5904
5905         return LUKS2_token_status(cd, &cd->u.luks2.hdr, token, type);
5906 }
5907
5908 int crypt_token_max(const char *type)
5909 {
5910         if (isLUKS2(type))
5911                 return LUKS2_TOKENS_MAX;
5912
5913         return -EINVAL;
5914 }
5915
5916 int crypt_token_luks2_keyring_get(struct crypt_device *cd,
5917         int token,
5918         struct crypt_token_params_luks2_keyring *params)
5919 {
5920         crypt_token_info token_info;
5921         const char *type;
5922         int r;
5923
5924         if (!params)
5925                 return -EINVAL;
5926
5927         log_dbg(cd, "Requesting LUKS2 keyring token %d.", token);
5928
5929         if ((r = _onlyLUKS2(cd, CRYPT_CD_UNRESTRICTED, 0)))
5930                 return r;
5931
5932         token_info = LUKS2_token_status(cd, &cd->u.luks2.hdr, token, &type);
5933         switch (token_info) {
5934         case CRYPT_TOKEN_INVALID:
5935                 log_dbg(cd, "Token %d is invalid.", token);
5936                 return -EINVAL;
5937         case CRYPT_TOKEN_INACTIVE:
5938                 log_dbg(cd, "Token %d is inactive.", token);
5939                 return -EINVAL;
5940         case CRYPT_TOKEN_INTERNAL:
5941                 if (!strcmp(type, LUKS2_TOKEN_KEYRING))
5942                         break;
5943                 /* Fall through */
5944         case CRYPT_TOKEN_INTERNAL_UNKNOWN:
5945         case CRYPT_TOKEN_EXTERNAL:
5946         case CRYPT_TOKEN_EXTERNAL_UNKNOWN:
5947                 log_dbg(cd, "Token %d has unexpected type %s.", token, type);
5948                 return -EINVAL;
5949         }
5950
5951         return LUKS2_token_keyring_get(&cd->u.luks2.hdr, token, params);
5952 }
5953
5954 int crypt_token_luks2_keyring_set(struct crypt_device *cd,
5955         int token,
5956         const struct crypt_token_params_luks2_keyring *params)
5957 {
5958         int r;
5959         char json[4096];
5960
5961         if (!params || !params->key_description)
5962                 return -EINVAL;
5963
5964         log_dbg(cd, "Creating new LUKS2 keyring token (%d).", token);
5965
5966         if ((r = onlyLUKS2(cd)))
5967                 return r;
5968
5969         r = LUKS2_token_keyring_json(json, sizeof(json), params);
5970         if (r < 0)
5971                 return r;
5972
5973         return LUKS2_token_create(cd, &cd->u.luks2.hdr, token, json, 1);
5974 }
5975
5976 int crypt_token_assign_keyslot(struct crypt_device *cd, int token, int keyslot)
5977 {
5978         int r;
5979
5980         if ((r = onlyLUKS2(cd)))
5981                 return r;
5982
5983         return LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot, token, 1, 1);
5984 }
5985
5986 int crypt_token_unassign_keyslot(struct crypt_device *cd, int token, int keyslot)
5987 {
5988         int r;
5989
5990         if ((r = onlyLUKS2(cd)))
5991                 return r;
5992
5993         return LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot, token, 0, 1);
5994 }
5995
5996 int crypt_token_is_assigned(struct crypt_device *cd, int token, int keyslot)
5997 {
5998         int r;
5999
6000         if ((r = _onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)))
6001                 return r;
6002
6003         return LUKS2_token_is_assigned(&cd->u.luks2.hdr, keyslot, token);
6004 }
6005
6006 /* Internal only */
6007 int crypt_metadata_locking_enabled(void)
6008 {
6009         return _metadata_locking;
6010 }
6011
6012 int crypt_metadata_locking(struct crypt_device *cd __attribute__((unused)), int enable)
6013 {
6014         if (enable && !_metadata_locking)
6015                 return -EPERM;
6016
6017         _metadata_locking = enable ? 1 : 0;
6018         return 0;
6019 }
6020
6021 int crypt_persistent_flags_set(struct crypt_device *cd, crypt_flags_type type, uint32_t flags)
6022 {
6023         int r;
6024
6025         if ((r = onlyLUKS2(cd)))
6026                 return r;
6027
6028         if (type == CRYPT_FLAGS_ACTIVATION)
6029                 return LUKS2_config_set_flags(cd, &cd->u.luks2.hdr, flags);
6030
6031         if (type == CRYPT_FLAGS_REQUIREMENTS)
6032                 return LUKS2_config_set_requirements(cd, &cd->u.luks2.hdr, flags, true);
6033
6034         return -EINVAL;
6035 }
6036
6037 int crypt_persistent_flags_get(struct crypt_device *cd, crypt_flags_type type, uint32_t *flags)
6038 {
6039         int r;
6040
6041         if (!flags)
6042                 return -EINVAL;
6043
6044         if ((r = _onlyLUKS2(cd, CRYPT_CD_UNRESTRICTED, 0)))
6045                 return r;
6046
6047         if (type == CRYPT_FLAGS_ACTIVATION)
6048                 return LUKS2_config_get_flags(cd, &cd->u.luks2.hdr, flags);
6049
6050         if (type == CRYPT_FLAGS_REQUIREMENTS)
6051                 return LUKS2_config_get_requirements(cd, &cd->u.luks2.hdr, flags);
6052
6053         return -EINVAL;
6054 }
6055
6056 static int update_volume_key_segment_digest(struct crypt_device *cd, struct luks2_hdr *hdr, int digest, int commit)
6057 {
6058         int r;
6059
6060         /* Remove any assignments in memory */
6061         r = LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, CRYPT_ANY_DIGEST, 0, 0);
6062         if (r)
6063                 return r;
6064
6065         /* Assign it to the specific digest */
6066         return LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, digest, 1, commit);
6067 }
6068
6069 static int verify_and_update_segment_digest(struct crypt_device *cd,
6070                 struct luks2_hdr *hdr, int keyslot, struct crypt_keyslot_context *kc)
6071 {
6072         int digest, r;
6073         struct volume_key *vk = NULL;
6074
6075         assert(kc);
6076         assert(kc->get_luks2_key);
6077         assert(keyslot >= 0);
6078
6079         r = kc->get_luks2_key(cd, kc, keyslot, CRYPT_ANY_SEGMENT, &vk);
6080         if (r < 0)
6081                 return r;
6082
6083         /* check volume_key (param) digest matches keyslot digest */
6084         r = LUKS2_digest_verify(cd, hdr, vk, keyslot);
6085         if (r < 0)
6086                 goto out;
6087         digest = r;
6088
6089         /* nothing to do, volume key in keyslot is already assigned to default segment */
6090         r = LUKS2_digest_verify_by_segment(cd, hdr, CRYPT_DEFAULT_SEGMENT, vk);
6091         if (r >= 0)
6092                 goto out;
6093
6094         /* FIXME: check new volume key is usable with current default segment */
6095
6096         r = update_volume_key_segment_digest(cd, &cd->u.luks2.hdr, digest, 1);
6097         if (r)
6098                 log_err(cd, _("Failed to assign keyslot %u as the new volume key."), keyslot);
6099 out:
6100         crypt_free_volume_key(vk);
6101
6102         return r < 0 ? r : keyslot;
6103 }
6104
6105 static int luks2_keyslot_add_by_verified_volume_key(struct crypt_device *cd,
6106         int keyslot_new,
6107         const char *new_passphrase,
6108         size_t new_passphrase_size,
6109         struct volume_key *vk)
6110 {
6111         int r;
6112         struct luks2_keyslot_params params;
6113
6114         assert(cd);
6115         assert(keyslot_new >= 0);
6116         assert(new_passphrase);
6117         assert(vk);
6118         assert(crypt_volume_key_get_id(vk) >= 0);
6119
6120         r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, &params);
6121         if (r < 0) {
6122                 log_err(cd, _("Failed to initialize default LUKS2 keyslot parameters."));
6123                 return r;
6124         }
6125
6126         r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, crypt_volume_key_get_id(vk), 1, 0);
6127         if (r < 0) {
6128                 log_err(cd, _("Failed to assign keyslot %d to digest."), keyslot_new);
6129                 return r;
6130         }
6131
6132         r = LUKS2_keyslot_store(cd,  &cd->u.luks2.hdr, keyslot_new,
6133                                 CONST_CAST(char*)new_passphrase,
6134                                 new_passphrase_size, vk, &params);
6135
6136         return r < 0 ? r : keyslot_new;
6137 }
6138
6139 static int luks2_keyslot_add_by_volume_key(struct crypt_device *cd,
6140         int keyslot_new,
6141         const char *new_passphrase,
6142         size_t new_passphrase_size,
6143         struct volume_key *vk)
6144 {
6145         int r;
6146
6147         assert(cd);
6148         assert(keyslot_new >= 0);
6149         assert(new_passphrase);
6150         assert(vk);
6151
6152         r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
6153         if (r >= 0)
6154                 crypt_volume_key_set_id(vk, r);
6155
6156         if (r < 0) {
6157                 log_err(cd, _("Volume key does not match the volume."));
6158                 return r;
6159         }
6160
6161         return luks2_keyslot_add_by_verified_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
6162 }
6163
6164 static int luks1_keyslot_add_by_volume_key(struct crypt_device *cd,
6165         int keyslot_new,
6166         const char *new_passphrase,
6167         size_t new_passphrase_size,
6168         struct volume_key *vk)
6169 {
6170         int r;
6171
6172         assert(cd);
6173         assert(keyslot_new >= 0);
6174         assert(new_passphrase);
6175         assert(vk);
6176
6177         r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
6178         if (r < 0) {
6179                 log_err(cd, _("Volume key does not match the volume."));
6180                 return r;
6181         }
6182
6183         r = LUKS_set_key(keyslot_new, CONST_CAST(char*)new_passphrase,
6184                          new_passphrase_size, &cd->u.luks1.hdr, vk, cd);
6185
6186         return r < 0 ? r : keyslot_new;
6187 }
6188
6189 static int keyslot_add_by_key(struct crypt_device *cd,
6190         bool is_luks1,
6191         int keyslot_new,
6192         const char *new_passphrase,
6193         size_t new_passphrase_size,
6194         struct volume_key *vk,
6195         uint32_t flags)
6196 {
6197         int r, digest;
6198
6199         assert(cd);
6200         assert(keyslot_new >= 0);
6201         assert(new_passphrase);
6202         assert(vk);
6203
6204         if (!flags)
6205                 return is_luks1 ? luks1_keyslot_add_by_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk) :
6206                                   luks2_keyslot_add_by_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
6207
6208         if (is_luks1)
6209                 return -EINVAL;
6210
6211         digest = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
6212         if (digest >= 0) /* if key matches volume key digest tear down new vk flag */
6213                 flags &= ~CRYPT_VOLUME_KEY_SET;
6214         else {
6215                 /* if key matches any existing digest, do not create new digest */
6216                 if ((flags & CRYPT_VOLUME_KEY_DIGEST_REUSE))
6217                         digest = LUKS2_digest_any_matching(cd, &cd->u.luks2.hdr, vk);
6218
6219                 /* no segment flag or new vk flag requires new key digest */
6220                 if (flags & (CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_SET)) {
6221                         if (digest < 0 || !(flags & CRYPT_VOLUME_KEY_DIGEST_REUSE))
6222                                 digest = LUKS2_digest_create(cd, "pbkdf2", &cd->u.luks2.hdr, vk);
6223                 }
6224         }
6225
6226         r = digest;
6227         if (r < 0) {
6228                 log_err(cd, _("Volume key does not match the volume."));
6229                 return r;
6230         }
6231
6232         crypt_volume_key_set_id(vk, digest);
6233
6234         if (flags & CRYPT_VOLUME_KEY_SET) {
6235                 r = update_volume_key_segment_digest(cd, &cd->u.luks2.hdr, digest, 0);
6236                 if (r < 0)
6237                         log_err(cd, _("Failed to assign keyslot %u as the new volume key."), keyslot_new);
6238         }
6239
6240         if (r >= 0)
6241                 r = luks2_keyslot_add_by_verified_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
6242
6243         return r < 0 ? r : keyslot_new;
6244 }
6245
6246 int crypt_keyslot_add_by_key(struct crypt_device *cd,
6247         int keyslot,
6248         const char *volume_key,
6249         size_t volume_key_size,
6250         const char *passphrase,
6251         size_t passphrase_size,
6252         uint32_t flags)
6253 {
6254         int r;
6255         struct crypt_keyslot_context kc, new_kc;
6256
6257         if (!passphrase || ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) &&
6258                             (flags & CRYPT_VOLUME_KEY_SET)))
6259                 return -EINVAL;
6260
6261         if ((r = onlyLUKS(cd)) < 0)
6262                 return r;
6263
6264         if ((flags & CRYPT_VOLUME_KEY_SET) && crypt_keyslot_status(cd, keyslot) > CRYPT_SLOT_INACTIVE &&
6265             isLUKS2(cd->type)) {
6266                 if (volume_key)
6267                         crypt_keyslot_unlock_by_key_init_internal(&kc, volume_key, volume_key_size);
6268                 else
6269                         crypt_keyslot_unlock_by_passphrase_init_internal(&kc, passphrase, passphrase_size);
6270
6271                 r = verify_and_update_segment_digest(cd, &cd->u.luks2.hdr, keyslot, &kc);
6272
6273                 crypt_keyslot_context_destroy_internal(&kc);
6274
6275                 return r;
6276         }
6277
6278         crypt_keyslot_unlock_by_key_init_internal(&kc, volume_key, volume_key_size);
6279         crypt_keyslot_unlock_by_passphrase_init_internal(&new_kc, passphrase, passphrase_size);
6280
6281         r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, flags);
6282
6283         crypt_keyslot_context_destroy_internal(&kc);
6284         crypt_keyslot_context_destroy_internal(&new_kc);
6285
6286         return r;
6287 }
6288
6289 int crypt_keyslot_add_by_keyslot_context(struct crypt_device *cd,
6290         int keyslot_existing,
6291         struct crypt_keyslot_context *kc,
6292         int keyslot_new,
6293         struct crypt_keyslot_context *new_kc,
6294         uint32_t flags)
6295 {
6296         bool is_luks1;
6297         int active_slots, r;
6298         const char *new_passphrase;
6299         size_t new_passphrase_size;
6300         struct volume_key *vk = NULL;
6301
6302         if (!kc || ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) &&
6303                     (flags & CRYPT_VOLUME_KEY_SET)))
6304                 return -EINVAL;
6305
6306         r = flags ? onlyLUKS2(cd) : onlyLUKS(cd);
6307         if (r)
6308                 return r;
6309
6310         if ((flags & CRYPT_VOLUME_KEY_SET) && crypt_keyslot_status(cd, keyslot_existing) > CRYPT_SLOT_INACTIVE)
6311                 return verify_and_update_segment_digest(cd, &cd->u.luks2.hdr, keyslot_existing, kc);
6312
6313         if (!new_kc || !new_kc->get_passphrase)
6314                 return -EINVAL;
6315
6316         log_dbg(cd, "Adding new keyslot %d by %s%s, volume key provided by %s (%d).",
6317                 keyslot_new, keyslot_context_type_string(new_kc),
6318                 (flags & CRYPT_VOLUME_KEY_NO_SEGMENT) ? " unassigned to a crypt segment" : "",
6319                 keyslot_context_type_string(kc), keyslot_existing);
6320
6321         r = keyslot_verify_or_find_empty(cd, &keyslot_new);
6322         if (r < 0)
6323                 return r;
6324
6325         is_luks1 = isLUKS1(cd->type);
6326         if (is_luks1)
6327                 active_slots = LUKS_keyslot_active_count(&cd->u.luks1.hdr);
6328         else
6329                 active_slots = LUKS2_keyslot_active_count(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6330
6331         if (active_slots < 0)
6332                 return -EINVAL;
6333
6334         if (active_slots == 0 && kc->type != CRYPT_KC_TYPE_KEY)
6335                 r = -ENOENT;
6336         else if (is_luks1 && kc->get_luks1_volume_key)
6337                 r = kc->get_luks1_volume_key(cd, kc, keyslot_existing, &vk);
6338         else if (!is_luks1 && kc->get_luks2_volume_key)
6339                 r = kc->get_luks2_volume_key(cd, kc, keyslot_existing, &vk);
6340         else
6341                 return -EINVAL;
6342
6343         if (r == -ENOENT) {
6344                 if ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) && kc->type == CRYPT_KC_TYPE_KEY) {
6345                         if (!(vk = crypt_generate_volume_key(cd, kc->u.k.volume_key_size)))
6346                                 return -ENOMEM;
6347                         r = 0;
6348                 } else if (cd->volume_key) {
6349                         if (!(vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key)))
6350                                 return -ENOMEM;
6351                         r = 0;
6352                 } else if (active_slots == 0) {
6353                         log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided."));
6354                         r = -EINVAL;
6355                 }
6356         }
6357
6358         if (r < 0)
6359                 return r;
6360
6361         r = new_kc->get_passphrase(cd, new_kc, &new_passphrase, &new_passphrase_size);
6362         /* If new keyslot context is token just assign it to new keyslot */
6363         if (r >= 0 && new_kc->type == CRYPT_KC_TYPE_TOKEN && !is_luks1)
6364                 r = LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot_new, new_kc->u.t.id, 1, 0);
6365         if (r >= 0)
6366                 r = keyslot_add_by_key(cd, is_luks1, keyslot_new, new_passphrase, new_passphrase_size, vk, flags);
6367
6368         crypt_free_volume_key(vk);
6369
6370         if (r < 0) {
6371                 _luks2_rollback(cd);
6372                 return r;
6373         }
6374
6375         return keyslot_new;
6376 }
6377
6378 /*
6379  * Keyring handling
6380  */
6381 int crypt_use_keyring_for_vk(struct crypt_device *cd)
6382 {
6383         uint32_t dmc_flags;
6384
6385         /* dm backend must be initialized */
6386         if (!cd || !isLUKS2(cd->type))
6387                 return 0;
6388
6389         if (!_vk_via_keyring || !kernel_keyring_support())
6390                 return 0;
6391
6392         if (dm_flags(cd, DM_CRYPT, &dmc_flags))
6393                 return dmcrypt_keyring_bug() ? 0 : 1;
6394
6395         return (dmc_flags & DM_KERNEL_KEYRING_SUPPORTED);
6396 }
6397
6398 int crypt_volume_key_keyring(struct crypt_device *cd __attribute__((unused)), int enable)
6399 {
6400         _vk_via_keyring = enable ? 1 : 0;
6401         return 0;
6402 }
6403
6404 /* internal only */
6405 int crypt_volume_key_load_in_keyring(struct crypt_device *cd, struct volume_key *vk)
6406 {
6407         int r;
6408         const char *type_name = key_type_name(LOGON_KEY);
6409
6410         if (!vk || !cd || !type_name)
6411                 return -EINVAL;
6412
6413         if (!vk->key_description) {
6414                 log_dbg(cd, "Invalid key description");
6415                 return -EINVAL;
6416         }
6417
6418         log_dbg(cd, "Loading key (%zu bytes, type %s) in thread keyring.", vk->keylength, type_name);
6419
6420         r = keyring_add_key_in_thread_keyring(LOGON_KEY, vk->key_description, vk->key, vk->keylength);
6421         if (r) {
6422                 log_dbg(cd, "keyring_add_key_in_thread_keyring failed (error %d)", r);
6423                 log_err(cd, _("Failed to load key in kernel keyring."));
6424         } else
6425                 crypt_set_key_in_keyring(cd, 1);
6426
6427         return r;
6428 }
6429
6430 /* internal only */
6431 int crypt_key_in_keyring(struct crypt_device *cd)
6432 {
6433         return cd ? cd->key_in_keyring : 0;
6434 }
6435
6436 /* internal only */
6437 void crypt_set_key_in_keyring(struct crypt_device *cd, unsigned key_in_keyring)
6438 {
6439         if (!cd)
6440                 return;
6441
6442         cd->key_in_keyring = key_in_keyring;
6443 }
6444
6445 /* internal only */
6446 void crypt_drop_keyring_key_by_description(struct crypt_device *cd, const char *key_description, key_type_t ktype)
6447 {
6448         int r;
6449         const char *type_name = key_type_name(ktype);
6450
6451         if (!key_description || !type_name)
6452                 return;
6453
6454         log_dbg(cd, "Requesting keyring %s key for revoke and unlink.", type_name);
6455
6456         r = keyring_revoke_and_unlink_key(ktype, key_description);
6457         if (r)
6458                 log_dbg(cd, "keyring_revoke_and_unlink_key failed (error %d)", r);
6459         crypt_set_key_in_keyring(cd, 0);
6460 }
6461
6462 /* internal only */
6463 void crypt_drop_keyring_key(struct crypt_device *cd, struct volume_key *vks)
6464 {
6465         struct volume_key *vk = vks;
6466
6467         while (vk) {
6468                 crypt_drop_keyring_key_by_description(cd, vk->key_description, LOGON_KEY);
6469                 vk = crypt_volume_key_next(vk);
6470         }
6471 }
6472
6473 int crypt_activate_by_keyring(struct crypt_device *cd,
6474                               const char *name,
6475                               const char *key_description,
6476                               int keyslot,
6477                               uint32_t flags)
6478 {
6479         char *passphrase;
6480         size_t passphrase_size;
6481         int r;
6482
6483         if (!cd || !key_description)
6484                 return -EINVAL;
6485
6486         log_dbg(cd, "%s volume %s [keyslot %d] using passphrase in keyring.",
6487                 name ? "Activating" : "Checking", name ?: "passphrase", keyslot);
6488
6489         if (!kernel_keyring_support()) {
6490                 log_err(cd, _("Kernel keyring is not supported by the kernel."));
6491                 return -EINVAL;
6492         }
6493
6494         r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
6495         if (r < 0)
6496                 return r;
6497
6498         r = keyring_get_passphrase(key_description, &passphrase, &passphrase_size);
6499         if (r < 0) {
6500                 log_err(cd, _("Failed to read passphrase from keyring (error %d)."), r);
6501                 return -EINVAL;
6502         }
6503
6504         r = _activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);
6505
6506         crypt_safe_free(passphrase);
6507
6508         return r;
6509 }
6510
6511 /*
6512  * Workaround for serialization of parallel activation and memory-hard PBKDF
6513  * In specific situation (systemd activation) this causes OOM killer activation.
6514  * For now, let's provide this ugly way to serialize unlocking of devices.
6515  */
6516 int crypt_serialize_lock(struct crypt_device *cd)
6517 {
6518         if (!cd->memory_hard_pbkdf_lock_enabled)
6519                 return 0;
6520
6521         log_dbg(cd, "Taking global memory-hard access serialization lock.");
6522         if (crypt_write_lock(cd, "memory-hard-access", true, &cd->pbkdf_memory_hard_lock)) {
6523                 log_err(cd, _("Failed to acquire global memory-hard access serialization lock."));
6524                 cd->pbkdf_memory_hard_lock = NULL;
6525                 return -EINVAL;
6526         }
6527
6528         return 0;
6529 }
6530
6531 void crypt_serialize_unlock(struct crypt_device *cd)
6532 {
6533         if (!cd->memory_hard_pbkdf_lock_enabled)
6534                 return;
6535
6536         crypt_unlock_internal(cd, cd->pbkdf_memory_hard_lock);
6537         cd->pbkdf_memory_hard_lock = NULL;
6538 }
6539
6540 crypt_reencrypt_info crypt_reencrypt_status(struct crypt_device *cd,
6541                 struct crypt_params_reencrypt *params)
6542 {
6543         if (params)
6544                 memset(params, 0, sizeof(*params));
6545
6546         if (!cd || !isLUKS(cd->type))
6547                 return CRYPT_REENCRYPT_INVALID;
6548
6549         if (isLUKS1(cd->type))
6550                 return CRYPT_REENCRYPT_NONE;
6551
6552         if (_onlyLUKS2(cd, CRYPT_CD_QUIET, CRYPT_REQUIREMENT_ONLINE_REENCRYPT))
6553                 return CRYPT_REENCRYPT_INVALID;
6554
6555         return LUKS2_reencrypt_get_params(&cd->u.luks2.hdr, params);
6556 }
6557
6558 static void __attribute__((destructor)) libcryptsetup_exit(void)
6559 {
6560         crypt_token_unload_external_all(NULL);
6561
6562         crypt_backend_destroy();
6563         crypt_random_exit();
6564 }