Imported Upstream version 1.6.7
[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-2012, Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2014, 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 <fcntl.h>
30 #include <errno.h>
31
32 #include "libcryptsetup.h"
33 #include "luks.h"
34 #include "loopaes.h"
35 #include "verity.h"
36 #include "tcrypt.h"
37 #include "internal.h"
38
39 struct crypt_device {
40         char *type;
41
42         struct device *device;
43         struct device *metadata_device;
44
45         struct volume_key *volume_key;
46         uint64_t timeout;
47         uint64_t iteration_time;
48         int tries;
49         int password_verify;
50         int rng_type;
51
52         // FIXME: private binary headers and access it properly
53         // through sub-library (LUKS1, TCRYPT)
54
55         union {
56         struct { /* used in CRYPT_LUKS1 */
57                 struct luks_phdr hdr;
58                 uint64_t PBKDF2_per_sec;
59         } luks1;
60         struct { /* used in CRYPT_PLAIN */
61                 struct crypt_params_plain hdr;
62                 char *cipher;
63                 char *cipher_mode;
64                 unsigned int key_size;
65         } plain;
66         struct { /* used in CRYPT_LOOPAES */
67                 struct crypt_params_loopaes hdr;
68                 char *cipher;
69                 char *cipher_mode;
70                 unsigned int key_size;
71         } loopaes;
72         struct { /* used in CRYPT_VERITY */
73                 struct crypt_params_verity hdr;
74                 char *root_hash;
75                 unsigned int root_hash_size;
76                 char *uuid;
77         } verity;
78         struct { /* used in CRYPT_TCRYPT */
79                 struct crypt_params_tcrypt params;
80                 struct tcrypt_phdr hdr;
81         } tcrypt;
82         struct { /* used if initialized without header by name */
83                 char *active_name;
84                 /* buffers, must refresh from kernel on every query */
85                 char cipher[MAX_CIPHER_LEN];
86                 char cipher_mode[MAX_CIPHER_LEN];
87                 unsigned int key_size;
88         } none;
89         } u;
90
91         /* callbacks definitions */
92         void (*log)(int level, const char *msg, void *usrptr);
93         void *log_usrptr;
94         int (*confirm)(const char *msg, void *usrptr);
95         void *confirm_usrptr;
96         int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
97         void *password_usrptr;
98
99         /* last error message */
100         char error[MAX_ERROR_LENGTH];
101 };
102
103 /* Just to suppress redundant messages about crypto backend */
104 static int _crypto_logged = 0;
105
106 /* Global error */
107 /* FIXME: not thread safe, remove this later */
108 static char global_error[MAX_ERROR_LENGTH] = {0};
109
110 /* Log helper */
111 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
112 static int _debug_level = 0;
113
114 void crypt_set_debug_level(int level)
115 {
116         _debug_level = level;
117 }
118
119 int crypt_get_debug_level(void)
120 {
121         return _debug_level;
122 }
123
124 static void crypt_set_error(struct crypt_device *cd, const char *error)
125 {
126         size_t size = strlen(error);
127
128         /* Set global error, ugly hack... */
129         strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
130         if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
131                 global_error[size - 1] = '\0';
132
133         /* Set error string per context */
134         if (cd) {
135                 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
136                 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
137                         cd->error[size - 1] = '\0';
138         }
139 }
140
141 void crypt_log(struct crypt_device *cd, int level, const char *msg)
142 {
143         if (cd && cd->log)
144                 cd->log(level, msg, cd->log_usrptr);
145         else if (_default_log)
146                 _default_log(level, msg, NULL);
147
148         if (level == CRYPT_LOG_ERROR)
149                 crypt_set_error(cd, msg);
150 }
151
152 __attribute__((format(printf, 5, 6)))
153 void logger(struct crypt_device *cd, int level, const char *file,
154             int line, const char *format, ...)
155 {
156         va_list argp;
157         char *target = NULL;
158
159         va_start(argp, format);
160
161         if (vasprintf(&target, format, argp) > 0 ) {
162                 if (level >= 0) {
163                         crypt_log(cd, level, target);
164 #ifdef CRYPT_DEBUG
165                 } else if (_debug_level)
166                         printf("# %s:%d %s\n", file ?: "?", line, target);
167 #else
168                 } else if (_debug_level)
169                         printf("# %s\n", target);
170 #endif
171         }
172
173         va_end(argp);
174         free(target);
175 }
176
177 static const char *mdata_device_path(struct crypt_device *cd)
178 {
179         return device_path(cd->metadata_device ?: cd->device);
180 }
181
182 /* internal only */
183 struct device *crypt_metadata_device(struct crypt_device *cd)
184 {
185         return cd->metadata_device ?: cd->device;
186 }
187
188 struct device *crypt_data_device(struct crypt_device *cd)
189 {
190         return cd->device;
191 }
192
193 int init_crypto(struct crypt_device *ctx)
194 {
195         struct utsname uts;
196         int r;
197
198         r = crypt_random_init(ctx);
199         if (r < 0) {
200                 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
201                 return r;
202         }
203
204         r = crypt_backend_init(ctx);
205         if (r < 0)
206                 log_err(ctx, _("Cannot initialize crypto backend.\n"));
207
208         if (!r && !_crypto_logged) {
209                 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
210                 if (!uname(&uts))
211                         log_dbg("Detected kernel %s %s %s.",
212                                 uts.sysname, uts.release, uts.machine);
213                 _crypto_logged = 1;
214         }
215
216         return r;
217 }
218
219 static int process_key(struct crypt_device *cd, const char *hash_name,
220                        size_t key_size, const char *pass, size_t passLen,
221                        struct volume_key **vk)
222 {
223         int r;
224
225         if (!key_size)
226                 return -EINVAL;
227
228         *vk = crypt_alloc_volume_key(key_size, NULL);
229         if (!*vk)
230                 return -ENOMEM;
231
232         if (hash_name) {
233                 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
234                 if (r < 0) {
235                         if (r == -ENOENT)
236                                 log_err(cd, _("Hash algorithm %s not supported.\n"),
237                                         hash_name);
238                         else
239                                 log_err(cd, _("Key processing error (using hash %s).\n"),
240                                         hash_name);
241                         crypt_free_volume_key(*vk);
242                         *vk = NULL;
243                         return -EINVAL;
244                 }
245         } else if (passLen > key_size) {
246                 memcpy((*vk)->key, pass, key_size);
247         } else {
248                 memcpy((*vk)->key, pass, passLen);
249         }
250
251         return 0;
252 }
253
254 static int isPLAIN(const char *type)
255 {
256         return (type && !strcmp(CRYPT_PLAIN, type));
257 }
258
259 static int isLUKS(const char *type)
260 {
261         return (type && !strcmp(CRYPT_LUKS1, type));
262 }
263
264 static int isLOOPAES(const char *type)
265 {
266         return (type && !strcmp(CRYPT_LOOPAES, type));
267 }
268
269 static int isVERITY(const char *type)
270 {
271         return (type && !strcmp(CRYPT_VERITY, type));
272 }
273
274 static int isTCRYPT(const char *type)
275 {
276         return (type && !strcmp(CRYPT_TCRYPT, type));
277 }
278
279 static int onlyLUKS(struct crypt_device *cd)
280 {
281         int r = 0;
282
283         if (cd && !cd->type) {
284                 log_err(cd, _("Cannot determine device type. Incompatible activation of device?\n"));
285                 r = -EINVAL;
286         }
287         if (!cd || !isLUKS(cd->type)) {
288                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
289                 r = -EINVAL;
290         }
291
292         return r;
293 }
294
295 static void crypt_set_null_type(struct crypt_device *cd)
296 {
297         if (!cd->type)
298                 return;
299
300         free(cd->type);
301         cd->type = NULL;
302         cd->u.none.active_name = NULL;
303 }
304
305 static void crypt_reset_null_type(struct crypt_device *cd)
306 {
307         if (cd->type)
308                 return;
309
310         free(cd->u.none.active_name);
311         cd->u.none.active_name = NULL;
312 }
313
314 /* keyslot helpers */
315 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
316 {
317         if (*keyslot == CRYPT_ANY_SLOT) {
318                 *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
319                 if (*keyslot < 0) {
320                         log_err(cd, _("All key slots full.\n"));
321                         return -EINVAL;
322                 }
323         }
324
325         switch (LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot)) {
326                 case CRYPT_SLOT_INVALID:
327                         log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
328                                 *keyslot, LUKS_NUMKEYS - 1);
329                         return -EINVAL;
330                 case CRYPT_SLOT_INACTIVE:
331                         break;
332                 default:
333                         log_err(cd, _("Key slot %d is full, please select another one.\n"),
334                                 *keyslot);
335                         return -EINVAL;
336         }
337
338         return 0;
339 }
340
341 /*
342  * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
343  */
344 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
345 {
346         int i, j;
347         char *str;
348
349         if (!dm_uuid || !hdr_uuid)
350                 return -EINVAL;
351
352         str = strchr(dm_uuid, '-');
353         if (!str)
354                 return -EINVAL;
355
356         for (i = 0, j = 1; hdr_uuid[i]; i++) {
357                 if (hdr_uuid[i] == '-')
358                         continue;
359
360                 if (!str[j] || str[j] == '-')
361                         return -EINVAL;
362
363                 if (str[j] != hdr_uuid[i])
364                         return -EINVAL;
365                 j++;
366         }
367
368         return 0;
369 }
370
371 /*
372  * compares type of active device to provided string (only if there is no explicit type)
373  */
374 static int crypt_uuid_type_cmp(struct crypt_device *cd, const char *type)
375 {
376         struct crypt_dm_active_device dmd = {};
377         size_t len;
378         int r;
379
380         /* Must user header-on-disk if we know type here */
381         if (cd->type || !cd->u.none.active_name)
382                 return -EINVAL;
383
384         log_dbg("Checking if active device %s without header has UUID type %s.",
385                 cd->u.none.active_name, type);
386
387         r = dm_query_device(cd, cd->u.none.active_name, DM_ACTIVE_UUID, &dmd);
388         if (r < 0)
389                 return r;
390
391         r = -ENODEV;
392         len = strlen(type);
393         if (dmd.uuid && strlen(dmd.uuid) > len &&
394             !strncmp(dmd.uuid, type, len) && dmd.uuid[len] == '-')
395                 r = 0;
396
397         free(CONST_CAST(void*)dmd.uuid);
398         return r;
399 }
400
401 int PLAIN_activate(struct crypt_device *cd,
402                      const char *name,
403                      struct volume_key *vk,
404                      uint64_t size,
405                      uint32_t flags)
406 {
407         int r;
408         char *dm_cipher = NULL;
409         enum devcheck device_check;
410         struct crypt_dm_active_device dmd = {
411                 .target = DM_CRYPT,
412                 .size   = size,
413                 .flags  = flags,
414                 .data_device = crypt_data_device(cd),
415                 .u.crypt  = {
416                         .cipher = NULL,
417                         .vk     = vk,
418                         .offset = crypt_get_data_offset(cd),
419                         .iv_offset = crypt_get_iv_offset(cd),
420                 }
421         };
422
423         if (dmd.flags & CRYPT_ACTIVATE_SHARED)
424                 device_check = DEV_SHARED;
425         else
426                 device_check = DEV_EXCL;
427
428         r = device_block_adjust(cd, dmd.data_device, device_check,
429                                 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
430         if (r)
431                 return r;
432
433         if (crypt_get_cipher_mode(cd))
434                 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
435         else
436                 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
437         if (r < 0)
438                 return -ENOMEM;
439
440         dmd.u.crypt.cipher = dm_cipher;
441         log_dbg("Trying to activate PLAIN device %s using cipher %s.",
442                 name, dmd.u.crypt.cipher);
443
444         r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
445
446         free(dm_cipher);
447         return r;
448 }
449
450 int crypt_confirm(struct crypt_device *cd, const char *msg)
451 {
452         if (!cd || !cd->confirm)
453                 return 1;
454         else
455                 return cd->confirm(msg, cd->confirm_usrptr);
456 }
457
458 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
459                               size_t *key_len, int force_verify)
460 {
461         char *prompt = NULL, *device_name;
462         int r;
463
464         *key = NULL;
465         if(!msg) {
466                 if (crypt_loop_device(crypt_get_device_name(cd)))
467                         device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
468                 else
469                         device_name = strdup(crypt_get_device_name(cd));
470                 if (!device_name)
471                         return -ENOMEM;
472                 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
473                 free(device_name);
474                 if (r < 0)
475                         return -ENOMEM;
476                 msg = prompt;
477         }
478
479         if (cd->password) {
480                 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
481                 if (!*key) {
482                         r = -ENOMEM;
483                         goto out;
484                 }
485                 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
486                                  cd->password_usrptr);
487                 if (r < 0) {
488                         crypt_safe_free(*key);
489                         *key = NULL;
490                 } else
491                         *key_len = r;
492         } else
493                 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
494                                   (force_verify || cd->password_verify), cd);
495 out:
496         free(prompt);
497         return (r < 0) ? r: 0;
498 }
499
500 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
501                                              struct volume_key **vk)
502 {
503         char *passphrase_read = NULL;
504         size_t passphrase_size_read;
505         int r = -EINVAL, eperm = 0, tries = cd->tries;
506
507         *vk = NULL;
508         do {
509                 crypt_free_volume_key(*vk);
510                 *vk = NULL;
511
512                 r = key_from_terminal(cd, NULL, &passphrase_read,
513                                       &passphrase_size_read, 0);
514                 /* Continue if it is just passphrase verify mismatch */
515                 if (r == -EPERM)
516                         continue;
517                 if(r < 0)
518                         goto out;
519
520                 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
521                                            passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
522                 if (r == -EPERM)
523                         eperm = 1;
524                 crypt_safe_free(passphrase_read);
525                 passphrase_read = NULL;
526         } while (r == -EPERM && (--tries > 0));
527 out:
528         if (r < 0) {
529                 crypt_free_volume_key(*vk);
530                 *vk = NULL;
531
532                 /* Report wrong passphrase if at least one try failed */
533                 if (eperm && r == -EPIPE)
534                         r = -EPERM;
535         }
536
537         crypt_safe_free(passphrase_read);
538         return r;
539 }
540
541 static int key_from_file(struct crypt_device *cd, char *msg,
542                           char **key, size_t *key_len,
543                           const char *key_file, size_t key_offset,
544                           size_t key_size)
545 {
546         return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
547                              cd->timeout, 0, cd);
548 }
549
550 void crypt_set_log_callback(struct crypt_device *cd,
551         void (*log)(int level, const char *msg, void *usrptr),
552         void *usrptr)
553 {
554         if (!cd)
555                 _default_log = log;
556         else {
557                 cd->log = log;
558                 cd->log_usrptr = usrptr;
559         }
560 }
561
562 void crypt_set_confirm_callback(struct crypt_device *cd,
563         int (*confirm)(const char *msg, void *usrptr),
564         void *usrptr)
565 {
566         cd->confirm = confirm;
567         cd->confirm_usrptr = usrptr;
568 }
569
570 void crypt_set_password_callback(struct crypt_device *cd,
571         int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
572         void *usrptr)
573 {
574         cd->password = password;
575         cd->password_usrptr = usrptr;
576 }
577
578 static void _get_error(char *error, char *buf, size_t size)
579 {
580         if (!buf || size < 1)
581                 error[0] = '\0';
582         else if (*error) {
583                 strncpy(buf, error, size - 1);
584                 buf[size - 1] = '\0';
585                 error[0] = '\0';
586         } else
587                 buf[0] = '\0';
588 }
589
590 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
591 {
592         if (cd)
593                 return _get_error(cd->error, buf, size);
594 }
595
596 /* Deprecated global error interface */
597 void crypt_get_error(char *buf, size_t size)
598 {
599         return _get_error(global_error, buf, size);
600 }
601
602 const char *crypt_get_dir(void)
603 {
604         return dm_get_dir();
605 }
606
607 int crypt_init(struct crypt_device **cd, const char *device)
608 {
609         struct crypt_device *h = NULL;
610         int r;
611
612         if (!cd)
613                 return -EINVAL;
614
615         log_dbg("Allocating crypt device %s context.", device);
616
617         if (!(h = malloc(sizeof(struct crypt_device))))
618                 return -ENOMEM;
619
620         memset(h, 0, sizeof(*h));
621
622         r = device_alloc(&h->device, device);
623         if (r < 0)
624                 goto bad;
625
626         dm_backend_init();
627
628         h->iteration_time = 1000;
629         h->password_verify = 0;
630         h->tries = 3;
631         h->rng_type = crypt_random_default_key_rng();
632         *cd = h;
633         return 0;
634 bad:
635         device_free(h->device);
636         free(h);
637         return r;
638 }
639
640 static int crypt_check_data_device_size(struct crypt_device *cd)
641 {
642         int r;
643         uint64_t size, size_min;
644
645         /* Check data device size, require at least one sector */
646         size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
647
648         r = device_size(cd->device, &size);
649         if (r < 0)
650                 return r;
651
652         if (size < size_min) {
653                 log_err(cd, _("Header detected but device %s is too small.\n"),
654                         device_path(cd->device));
655                 return -EINVAL;
656         }
657
658         return r;
659 }
660
661 int crypt_set_data_device(struct crypt_device *cd, const char *device)
662 {
663         struct device *dev = NULL;
664         int r;
665
666         log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
667
668         if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
669                 log_err(cd, _("This operation is not supported for this device type.\n"));
670                 return  -EINVAL;
671         }
672
673         /* metadata device must be set */
674         if (!cd->device || !device)
675                 return -EINVAL;
676
677         r = device_alloc(&dev, device);
678         if (r < 0)
679                 return r;
680
681         if (!cd->metadata_device) {
682                 cd->metadata_device = cd->device;
683         } else
684                 device_free(cd->device);
685
686         cd->device = dev;
687
688         return crypt_check_data_device_size(cd);
689 }
690
691 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
692 {
693         struct luks_phdr hdr;
694         int r;
695
696         r = init_crypto(cd);
697         if (r < 0)
698                 return r;
699
700         r = LUKS_read_phdr(&hdr, require_header, repair, cd);
701         if (r < 0)
702                 return r;
703
704         if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
705                 return -ENOMEM;
706
707         memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
708
709         return r;
710 }
711
712 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
713 {
714         int r;
715
716         if (!params)
717                 return -EINVAL;
718
719         r = init_crypto(cd);
720         if (r < 0)
721                 return r;
722
723         memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
724
725         r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
726
727         cd->u.tcrypt.params.passphrase = NULL;
728         cd->u.tcrypt.params.passphrase_size = 0;
729         cd->u.tcrypt.params.keyfiles = NULL;
730         cd->u.tcrypt.params.keyfiles_count = 0;
731
732         if (r < 0)
733                 return r;
734
735         if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
736                 return -ENOMEM;
737
738         return r;
739 }
740
741 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
742 {
743         int r;
744         size_t sb_offset = 0;
745
746         r = init_crypto(cd);
747         if (r < 0)
748                 return r;
749
750         if (params && params->flags & CRYPT_VERITY_NO_HEADER)
751                 return -EINVAL;
752
753         if (params)
754                 sb_offset = params->hash_area_offset;
755
756         r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
757         if (r < 0)
758                 return r;
759
760         if (params)
761                 cd->u.verity.hdr.flags = params->flags;
762
763         /* Hash availability checked in sb load */
764         cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
765         if (cd->u.verity.root_hash_size > 4096)
766                 return -EINVAL;
767
768         if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
769                 return -ENOMEM;
770
771         if (params && params->data_device &&
772             (r = crypt_set_data_device(cd, params->data_device)) < 0)
773                 return r;
774
775         return r;
776 }
777
778 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
779 {
780         struct crypt_dm_active_device dmd = {};
781         char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
782         int key_nums, r;
783
784         r = dm_query_device(cd, name,
785                         DM_ACTIVE_DEVICE |
786                         DM_ACTIVE_UUID |
787                         DM_ACTIVE_CRYPT_CIPHER |
788                         DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
789         if (r < 0)
790                 goto out;
791         if (r > 0)
792                 r = 0;
793
794         if (isPLAIN(cd->type)) {
795                 cd->u.plain.hdr.hash = NULL; /* no way to get this */
796                 cd->u.plain.hdr.offset = dmd.u.crypt.offset;
797                 cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
798                 cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
799
800                 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
801                 if (!r) {
802                         cd->u.plain.cipher = strdup(cipher);
803                         cd->u.plain.cipher_mode = strdup(cipher_mode);
804                 }
805         } else if (isLOOPAES(cd->type)) {
806                 cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
807
808                 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
809                                               &key_nums, cipher_mode);
810                 if (!r) {
811                         cd->u.loopaes.cipher = strdup(cipher);
812                         cd->u.loopaes.cipher_mode = strdup(cipher_mode);
813                         /* version 3 uses last key for IV */
814                         if (dmd.u.crypt.vk->keylength % key_nums)
815                                 key_nums++;
816                         cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
817                 }
818         } else if (isLUKS(cd->type)) {
819                 if (crypt_metadata_device(cd)) {
820                         r = _crypt_load_luks1(cd, 0, 0);
821                         if (r < 0) {
822                                 log_dbg("LUKS device header does not match active device.");
823                                 crypt_set_null_type(cd);
824                                 r = 0;
825                                 goto out;
826                         }
827                         /* check whether UUIDs match each other */
828                         r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
829                         if (r < 0) {
830                                 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
831                                         cd->u.luks1.hdr.uuid, dmd.uuid);
832                                 crypt_set_null_type(cd);
833                                 r = 0;
834                         }
835                 } else {
836                         log_dbg("LUKS device header not available.");
837                         crypt_set_null_type(cd);
838                         r = 0;
839                 }
840         } else if (isTCRYPT(cd->type)) {
841                 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
842                                         &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
843         }
844 out:
845         crypt_free_volume_key(dmd.u.crypt.vk);
846         device_free(dmd.data_device);
847         free(CONST_CAST(void*)dmd.u.crypt.cipher);
848         free(CONST_CAST(void*)dmd.uuid);
849         return r;
850 }
851
852 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
853 {
854         struct crypt_params_verity params = {};
855         struct crypt_dm_active_device dmd = {
856                 .target = DM_VERITY,
857                 .u.verity.vp = &params,
858         };
859         int r;
860
861         r = dm_query_device(cd, name,
862                                 DM_ACTIVE_DEVICE |
863                                 DM_ACTIVE_VERITY_HASH_DEVICE |
864                                 DM_ACTIVE_VERITY_PARAMS, &dmd);
865         if (r < 0)
866                 goto out;
867         if (r > 0)
868                 r = 0;
869
870         if (isVERITY(cd->type)) {
871                 cd->u.verity.uuid = NULL; // FIXME
872                 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
873                 cd->u.verity.hdr.data_size = params.data_size;
874                 cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
875                 cd->u.verity.root_hash = NULL;
876                 cd->u.verity.hdr.hash_name = params.hash_name;
877                 cd->u.verity.hdr.data_device = NULL;
878                 cd->u.verity.hdr.hash_device = NULL;
879                 cd->u.verity.hdr.data_block_size = params.data_block_size;
880                 cd->u.verity.hdr.hash_block_size = params.hash_block_size;
881                 cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
882                 cd->u.verity.hdr.hash_type = params.hash_type;
883                 cd->u.verity.hdr.flags = params.flags;
884                 cd->u.verity.hdr.salt_size = params.salt_size;
885                 cd->u.verity.hdr.salt = params.salt;
886                 cd->metadata_device = dmd.u.verity.hash_device;
887         }
888 out:
889         device_free(dmd.data_device);
890         return r;
891 }
892
893 int crypt_init_by_name_and_header(struct crypt_device **cd,
894                                   const char *name,
895                                   const char *header_device)
896 {
897         crypt_status_info ci;
898         struct crypt_dm_active_device dmd;
899         int r;
900
901         log_dbg("Allocating crypt device context by device %s.", name);
902
903         ci = crypt_status(NULL, name);
904         if (ci == CRYPT_INVALID)
905                 return -ENODEV;
906
907         if (ci < CRYPT_ACTIVE) {
908                 log_err(NULL, _("Device %s is not active.\n"), name);
909                 return -ENODEV;
910         }
911
912         r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
913         if (r < 0)
914                 goto out;
915
916         *cd = NULL;
917
918         if (header_device) {
919                 r = crypt_init(cd, header_device);
920         } else {
921                 r = crypt_init(cd, device_path(dmd.data_device));
922
923                 /* Underlying device disappeared but mapping still active */
924                 if (!dmd.data_device || r == -ENOTBLK)
925                         log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
926                                     name);
927
928                 /* Underlying device is not readable but crypt mapping exists */
929                 if (r == -ENOTBLK) {
930                         device_free(dmd.data_device);
931                         dmd.data_device = NULL;
932                         r = crypt_init(cd, NULL);
933                 }
934         }
935
936         if (r < 0)
937                 goto out;
938
939         if (dmd.uuid) {
940                 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
941                         (*cd)->type = strdup(CRYPT_PLAIN);
942                 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
943                         (*cd)->type = strdup(CRYPT_LOOPAES);
944                 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
945                         (*cd)->type = strdup(CRYPT_LUKS1);
946                 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
947                         (*cd)->type = strdup(CRYPT_VERITY);
948                 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
949                         (*cd)->type = strdup(CRYPT_TCRYPT);
950                 else
951                         log_dbg("Unknown UUID set, some parameters are not set.");
952         } else
953                 log_dbg("Active device has no UUID set, some parameters are not set.");
954
955         if (header_device) {
956                 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
957                 if (r < 0)
958                         goto out;
959         }
960
961         /* Try to initialise basic parameters from active device */
962
963         if (dmd.target == DM_CRYPT)
964                 r = _init_by_name_crypt(*cd, name);
965         else if (dmd.target == DM_VERITY)
966                 r = _init_by_name_verity(*cd, name);
967 out:
968         if (r < 0) {
969                 crypt_free(*cd);
970                 *cd = NULL;
971         } else if (!(*cd)->type && name) {
972                 /* For anonymous device (no header found) remember initialized name */
973                 (*cd)->u.none.active_name = strdup(name);
974         }
975
976         device_free(dmd.data_device);
977         free(CONST_CAST(void*)dmd.uuid);
978         return r;
979 }
980
981 int crypt_init_by_name(struct crypt_device **cd, const char *name)
982 {
983         return crypt_init_by_name_and_header(cd, name, NULL);
984 }
985
986 static int _crypt_format_plain(struct crypt_device *cd,
987                                const char *cipher,
988                                const char *cipher_mode,
989                                const char *uuid,
990                                size_t volume_key_size,
991                                struct crypt_params_plain *params)
992 {
993         if (!cipher || !cipher_mode) {
994                 log_err(cd, _("Invalid plain crypt parameters.\n"));
995                 return -EINVAL;
996         }
997
998         if (volume_key_size > 1024) {
999                 log_err(cd, _("Invalid key size.\n"));
1000                 return -EINVAL;
1001         }
1002
1003         if (uuid) {
1004                 log_err(cd, _("UUID is not supported for this crypt type.\n"));
1005                 return -EINVAL;
1006         }
1007
1008         if (!(cd->type = strdup(CRYPT_PLAIN)))
1009                 return -ENOMEM;
1010
1011         cd->u.plain.key_size = volume_key_size;
1012         cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1013         if (!cd->volume_key)
1014                 return -ENOMEM;
1015
1016         cd->u.plain.cipher = strdup(cipher);
1017         cd->u.plain.cipher_mode = strdup(cipher_mode);
1018
1019
1020         if (params && params->hash)
1021                 cd->u.plain.hdr.hash = strdup(params->hash);
1022
1023         cd->u.plain.hdr.offset = params ? params->offset : 0;
1024         cd->u.plain.hdr.skip = params ? params->skip : 0;
1025         cd->u.plain.hdr.size = params ? params->size : 0;
1026
1027         if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
1028                 return -ENOMEM;
1029
1030         return 0;
1031 }
1032
1033 static int _crypt_format_luks1(struct crypt_device *cd,
1034                                const char *cipher,
1035                                const char *cipher_mode,
1036                                const char *uuid,
1037                                const char *volume_key,
1038                                size_t volume_key_size,
1039                                struct crypt_params_luks1 *params)
1040 {
1041         int r;
1042         unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1043         unsigned long alignment_offset = 0;
1044
1045         if (!crypt_metadata_device(cd)) {
1046                 log_err(cd, _("Can't format LUKS without device.\n"));
1047                 return -EINVAL;
1048         }
1049
1050         if (!(cd->type = strdup(CRYPT_LUKS1)))
1051                 return -ENOMEM;
1052
1053         if (volume_key)
1054                 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1055                                                       volume_key);
1056         else
1057                 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1058
1059         if(!cd->volume_key)
1060                 return -ENOMEM;
1061
1062         if (params && params->data_device) {
1063                 cd->metadata_device = cd->device;
1064                 cd->device = NULL;
1065                 if (device_alloc(&cd->device, params->data_device) < 0)
1066                         return -ENOMEM;
1067                 required_alignment = params->data_alignment * SECTOR_SIZE;
1068         } else if (params && params->data_alignment) {
1069                 required_alignment = params->data_alignment * SECTOR_SIZE;
1070         } else
1071                 device_topology_alignment(cd->device,
1072                                        &required_alignment,
1073                                        &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1074
1075         r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
1076                                (params && params->hash) ? params->hash : "sha1",
1077                                uuid, LUKS_STRIPES,
1078                                required_alignment / SECTOR_SIZE,
1079                                alignment_offset / SECTOR_SIZE,
1080                                cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
1081                                cd->metadata_device ? 1 : 0, cd);
1082         if(r < 0)
1083                 return r;
1084
1085         /* Wipe first 8 sectors - fs magic numbers etc. */
1086         r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1087         if(r < 0) {
1088                 if (r == -EBUSY)
1089                         log_err(cd, _("Cannot format device %s which is still in use.\n"),
1090                                 mdata_device_path(cd));
1091                 else if (r == -EACCES) {
1092                         log_err(cd, _("Cannot format device %s, permission denied.\n"),
1093                                 mdata_device_path(cd));
1094                         r = -EINVAL;
1095                 } else
1096                         log_err(cd, _("Cannot wipe header on device %s.\n"),
1097                                 mdata_device_path(cd));
1098
1099                 return r;
1100         }
1101
1102         r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1103
1104         return r;
1105 }
1106
1107 static int _crypt_format_loopaes(struct crypt_device *cd,
1108                                  const char *cipher,
1109                                  const char *uuid,
1110                                  size_t volume_key_size,
1111                                  struct crypt_params_loopaes *params)
1112 {
1113         if (!crypt_metadata_device(cd)) {
1114                 log_err(cd, _("Can't format LOOPAES without device.\n"));
1115                 return -EINVAL;
1116         }
1117
1118         if (volume_key_size > 1024) {
1119                 log_err(cd, _("Invalid key size.\n"));
1120                 return -EINVAL;
1121         }
1122
1123         if (uuid) {
1124                 log_err(cd, _("UUID is not supported for this crypt type.\n"));
1125                 return -EINVAL;
1126         }
1127
1128         if (!(cd->type = strdup(CRYPT_LOOPAES)))
1129                 return -ENOMEM;
1130
1131         cd->u.loopaes.key_size = volume_key_size;
1132
1133         cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1134
1135         if (params && params->hash)
1136                 cd->u.loopaes.hdr.hash = strdup(params->hash);
1137
1138         cd->u.loopaes.hdr.offset = params ? params->offset : 0;
1139         cd->u.loopaes.hdr.skip = params ? params->skip : 0;
1140
1141         return 0;
1142 }
1143
1144 static int _crypt_format_verity(struct crypt_device *cd,
1145                                  const char *uuid,
1146                                  struct crypt_params_verity *params)
1147 {
1148         int r = 0, hash_size;
1149         uint64_t data_device_size;
1150
1151         if (!crypt_metadata_device(cd)) {
1152                 log_err(cd, _("Can't format VERITY without device.\n"));
1153                 return -EINVAL;
1154         }
1155
1156         if (!params || !params->data_device)
1157                 return -EINVAL;
1158
1159         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1160                 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1161                 return -EINVAL;
1162         }
1163
1164         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1165             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1166                 log_err(cd, _("Unsupported VERITY block size.\n"));
1167                 return -EINVAL;
1168         }
1169
1170         if (params->hash_area_offset % 512) {
1171                 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1172                 return -EINVAL;
1173         }
1174
1175         if (!(cd->type = strdup(CRYPT_VERITY)))
1176                 return -ENOMEM;
1177
1178         r = crypt_set_data_device(cd, params->data_device);
1179         if (r)
1180                 return r;
1181         if (!params->data_size) {
1182                 r = device_size(cd->device, &data_device_size);
1183                 if (r < 0)
1184                         return r;
1185
1186                 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
1187         } else
1188                 cd->u.verity.hdr.data_size = params->data_size;
1189
1190         hash_size = crypt_hash_size(params->hash_name);
1191         if (hash_size <= 0) {
1192                 log_err(cd, _("Hash algorithm %s not supported.\n"),
1193                         params->hash_name);
1194                 return -EINVAL;
1195         }
1196         cd->u.verity.root_hash_size = hash_size;
1197
1198         cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
1199         if (!cd->u.verity.root_hash)
1200                 return -ENOMEM;
1201
1202         cd->u.verity.hdr.flags = params->flags;
1203         if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
1204                 return -ENOMEM;
1205         cd->u.verity.hdr.data_device = NULL;
1206         cd->u.verity.hdr.data_block_size = params->data_block_size;
1207         cd->u.verity.hdr.hash_block_size = params->hash_block_size;
1208         cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
1209         cd->u.verity.hdr.hash_type = params->hash_type;
1210         cd->u.verity.hdr.flags = params->flags;
1211         cd->u.verity.hdr.salt_size = params->salt_size;
1212         if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
1213                 return -ENOMEM;
1214
1215         if (params->salt)
1216                 memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
1217                        params->salt_size);
1218         else
1219                 r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
1220                                      params->salt_size, CRYPT_RND_SALT);
1221         if (r)
1222                 return r;
1223
1224         if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1225                 r = VERITY_create(cd, &cd->u.verity.hdr,
1226                                   cd->u.verity.root_hash, cd->u.verity.root_hash_size);
1227                 if (r)
1228                         return r;
1229         }
1230
1231         if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1232                 if (uuid)
1233                         cd->u.verity.uuid = strdup(uuid);
1234                 else {
1235                         r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
1236                         if (r)
1237                                 return r;
1238                 }
1239
1240                 r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
1241                                     cd->u.verity.uuid,
1242                                     &cd->u.verity.hdr);
1243         }
1244         return r;
1245 }
1246
1247 int crypt_format(struct crypt_device *cd,
1248         const char *type,
1249         const char *cipher,
1250         const char *cipher_mode,
1251         const char *uuid,
1252         const char *volume_key,
1253         size_t volume_key_size,
1254         void *params)
1255 {
1256         int r;
1257
1258         if (!type)
1259                 return -EINVAL;
1260
1261         if (cd->type) {
1262                 log_dbg("Context already formatted as %s.", cd->type);
1263                 return -EINVAL;
1264         }
1265
1266         log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1267
1268         crypt_reset_null_type(cd);
1269
1270         r = init_crypto(cd);
1271         if (r < 0)
1272                 return r;
1273
1274         if (isPLAIN(type))
1275                 r = _crypt_format_plain(cd, cipher, cipher_mode,
1276                                         uuid, volume_key_size, params);
1277         else if (isLUKS(type))
1278                 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1279                                         uuid, volume_key, volume_key_size, params);
1280         else if (isLOOPAES(type))
1281                 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1282         else if (isVERITY(type))
1283                 r = _crypt_format_verity(cd, uuid, params);
1284         else {
1285                 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1286                 r = -EINVAL;
1287         }
1288
1289         if (r < 0) {
1290                 crypt_set_null_type(cd);
1291                 crypt_free_volume_key(cd->volume_key);
1292                 cd->volume_key = NULL;
1293         }
1294
1295         return r;
1296 }
1297
1298 int crypt_load(struct crypt_device *cd,
1299                const char *requested_type,
1300                void *params)
1301 {
1302         int r;
1303
1304         log_dbg("Trying to load %s crypt type from device %s.",
1305                 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1306
1307         if (!crypt_metadata_device(cd))
1308                 return -EINVAL;
1309
1310         crypt_reset_null_type(cd);
1311
1312         if (!requested_type || isLUKS(requested_type)) {
1313                 if (cd->type && !isLUKS(cd->type)) {
1314                         log_dbg("Context is already initialised to type %s", cd->type);
1315                         return -EINVAL;
1316                 }
1317
1318                 r = _crypt_load_luks1(cd, 1, 0);
1319         } else if (isVERITY(requested_type)) {
1320                 if (cd->type && !isVERITY(cd->type)) {
1321                         log_dbg("Context is already initialised to type %s", cd->type);
1322                         return -EINVAL;
1323                 }
1324                 r = _crypt_load_verity(cd, params);
1325         } else if (isTCRYPT(requested_type)) {
1326                 if (cd->type && !isTCRYPT(cd->type)) {
1327                         log_dbg("Context is already initialised to type %s", cd->type);
1328                         return -EINVAL;
1329                 }
1330                 r = _crypt_load_tcrypt(cd, params);
1331         } else
1332                 return -EINVAL;
1333
1334         return r;
1335 }
1336
1337 int crypt_repair(struct crypt_device *cd,
1338                  const char *requested_type,
1339                  void *params __attribute__((unused)))
1340 {
1341         int r;
1342
1343         log_dbg("Trying to repair %s crypt type from device %s.",
1344                 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1345
1346         if (!crypt_metadata_device(cd))
1347                 return -EINVAL;
1348
1349         if (requested_type && !isLUKS(requested_type))
1350                 return -EINVAL;
1351
1352
1353         /* Load with repair */
1354         r = _crypt_load_luks1(cd, 1, 1);
1355         if (r < 0)
1356                 return r;
1357
1358         /* cd->type and header must be set in context */
1359         r = crypt_check_data_device_size(cd);
1360         if (r < 0)
1361                 crypt_set_null_type(cd);
1362
1363         return r;
1364 }
1365
1366 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1367 {
1368         struct crypt_dm_active_device dmd;
1369         int r;
1370
1371         /* Device context type must be initialised */
1372         if (!cd->type)
1373                 return -EINVAL;
1374
1375         log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1376
1377         r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1378                                   DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1379                                   DM_ACTIVE_CRYPT_KEY, &dmd);
1380         if (r < 0) {
1381                 log_err(NULL, _("Device %s is not active.\n"), name);
1382                 return -EINVAL;
1383         }
1384
1385         if (!dmd.uuid || dmd.target != DM_CRYPT) {
1386                 r = -EINVAL;
1387                 goto out;
1388         }
1389
1390         r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1391                                 dmd.u.crypt.offset, &new_size, &dmd.flags);
1392         if (r)
1393                 goto out;
1394
1395         if (new_size == dmd.size) {
1396                 log_dbg("Device has already requested size %" PRIu64
1397                         " sectors.", dmd.size);
1398                 r = 0;
1399         } else {
1400                 dmd.size = new_size;
1401                 if (isTCRYPT(cd->type))
1402                         r = -ENOTSUP;
1403                 else
1404                         r = dm_create_device(cd, name, cd->type, &dmd, 1);
1405         }
1406 out:
1407         if (dmd.target == DM_CRYPT) {
1408                 crypt_free_volume_key(dmd.u.crypt.vk);
1409                 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1410         }
1411         free(CONST_CAST(void*)dmd.data_device);
1412         free(CONST_CAST(void*)dmd.uuid);
1413
1414         return r;
1415 }
1416
1417 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1418 {
1419         if (!isLUKS(cd->type)) {
1420                 log_err(cd, _("This operation is not supported for this device type.\n"));
1421                 return  -EINVAL;
1422         }
1423
1424         if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
1425                 log_dbg("UUID is the same as requested (%s) for device %s.",
1426                         uuid, mdata_device_path(cd));
1427                 return 0;
1428         }
1429
1430         if (uuid)
1431                 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1432         else
1433                 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1434
1435         if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1436                 return -EPERM;
1437
1438         return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
1439 }
1440
1441 int crypt_header_backup(struct crypt_device *cd,
1442                         const char *requested_type,
1443                         const char *backup_file)
1444 {
1445         int r;
1446
1447         if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1448                 return -EINVAL;
1449
1450         if (cd->type && !isLUKS(cd->type))
1451                 return -EINVAL;
1452
1453         r = init_crypto(cd);
1454         if (r < 0)
1455                 return r;
1456
1457         log_dbg("Requested header backup of device %s (%s) to "
1458                 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1459
1460         r = LUKS_hdr_backup(backup_file, cd);
1461         return r;
1462 }
1463
1464 int crypt_header_restore(struct crypt_device *cd,
1465                          const char *requested_type,
1466                          const char *backup_file)
1467 {
1468         struct luks_phdr hdr;
1469         int r;
1470
1471         if (requested_type && !isLUKS(requested_type))
1472                 return -EINVAL;
1473
1474         if (cd->type && !isLUKS(cd->type))
1475                 return -EINVAL;
1476
1477         r = init_crypto(cd);
1478         if (r < 0)
1479                 return r;
1480
1481         log_dbg("Requested header restore to device %s (%s) from "
1482                 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1483
1484         r = LUKS_hdr_restore(backup_file, isLUKS(cd->type) ? &cd->u.luks1.hdr : &hdr, cd);
1485
1486         crypt_memzero(&hdr, sizeof(hdr));
1487         return r;
1488 }
1489
1490 void crypt_free(struct crypt_device *cd)
1491 {
1492         if (cd) {
1493                 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1494
1495                 dm_backend_exit();
1496                 crypt_free_volume_key(cd->volume_key);
1497
1498                 device_free(cd->device);
1499                 device_free(cd->metadata_device);
1500
1501                 if (isPLAIN(cd->type)) {
1502                         free(CONST_CAST(void*)cd->u.plain.hdr.hash);
1503                         free(cd->u.plain.cipher);
1504                         free(cd->u.plain.cipher_mode);
1505                 } else if (isLOOPAES(cd->type)) {
1506                         free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
1507                         free(cd->u.loopaes.cipher);
1508                 } else if (isVERITY(cd->type)) {
1509                         free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
1510                         free(CONST_CAST(void*)cd->u.verity.hdr.salt);
1511                         free(cd->u.verity.root_hash);
1512                         free(cd->u.verity.uuid);
1513                 } else if (!cd->type) {
1514                         free(cd->u.none.active_name);
1515                 }
1516
1517                 free(cd->type);
1518                 /* Some structures can contain keys (TCRYPT), wipe it */
1519                 crypt_memzero(cd, sizeof(*cd));
1520                 free(cd);
1521         }
1522 }
1523
1524 int crypt_suspend(struct crypt_device *cd,
1525                   const char *name)
1526 {
1527         crypt_status_info ci;
1528         int r;
1529
1530         log_dbg("Suspending volume %s.", name);
1531
1532         if (cd->type) {
1533                 r = onlyLUKS(cd);
1534         } else {
1535                 r = crypt_uuid_type_cmp(cd, CRYPT_LUKS1);
1536                 if (r < 0)
1537                         log_err(cd, _("This operation is supported only for LUKS device.\n"));
1538         }
1539
1540         if (r < 0)
1541                 return r;
1542
1543         ci = crypt_status(NULL, name);
1544         if (ci < CRYPT_ACTIVE) {
1545                 log_err(cd, _("Volume %s is not active.\n"), name);
1546                 return -EINVAL;
1547         }
1548
1549         dm_backend_init();
1550
1551         r = dm_status_suspended(cd, name);
1552         if (r < 0)
1553                 goto out;
1554
1555         if (r) {
1556                 log_err(cd, _("Volume %s is already suspended.\n"), name);
1557                 r = -EINVAL;
1558                 goto out;
1559         }
1560
1561         r = dm_suspend_and_wipe_key(cd, name);
1562         if (r == -ENOTSUP)
1563                 log_err(cd, _("Suspend is not supported for device %s.\n"), name);
1564         else if (r)
1565                 log_err(cd, _("Error during suspending device %s.\n"), name);
1566 out:
1567         dm_backend_exit();
1568         return r;
1569 }
1570
1571 int crypt_resume_by_passphrase(struct crypt_device *cd,
1572                                const char *name,
1573                                int keyslot,
1574                                const char *passphrase,
1575                                size_t passphrase_size)
1576 {
1577         struct volume_key *vk = NULL;
1578         int r;
1579
1580         log_dbg("Resuming volume %s.", name);
1581
1582         r = onlyLUKS(cd);
1583         if (r < 0)
1584                 return r;
1585
1586         r = dm_status_suspended(cd, name);
1587         if (r < 0)
1588                 return r;
1589
1590         if (!r) {
1591                 log_err(cd, _("Volume %s is not suspended.\n"), name);
1592                 return -EINVAL;
1593         }
1594
1595         if (passphrase) {
1596                 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1597                                            &cd->u.luks1.hdr, &vk, cd);
1598         } else
1599                 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1600
1601         if (r >= 0) {
1602                 keyslot = r;
1603                 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1604                 if (r == -ENOTSUP)
1605                         log_err(cd, _("Resume is not supported for device %s.\n"), name);
1606                 else if (r)
1607                         log_err(cd, _("Error during resuming device %s.\n"), name);
1608         } else
1609                 r = keyslot;
1610
1611         crypt_free_volume_key(vk);
1612         return r < 0 ? r : keyslot;
1613 }
1614
1615 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1616                                    const char *name,
1617                                    int keyslot,
1618                                    const char *keyfile,
1619                                    size_t keyfile_size,
1620                                    size_t keyfile_offset)
1621 {
1622         struct volume_key *vk = NULL;
1623         char *passphrase_read = NULL;
1624         size_t passphrase_size_read;
1625         int r;
1626
1627         log_dbg("Resuming volume %s.", name);
1628
1629         r = onlyLUKS(cd);
1630         if (r < 0)
1631                 return r;
1632
1633         r = dm_status_suspended(cd, name);
1634         if (r < 0)
1635                 return r;
1636
1637         if (!r) {
1638                 log_err(cd, _("Volume %s is not suspended.\n"), name);
1639                 return -EINVAL;
1640         }
1641
1642         if (!keyfile)
1643                 return -EINVAL;
1644
1645         r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1646                           &passphrase_size_read, keyfile, keyfile_offset,
1647                           keyfile_size);
1648         if (r < 0)
1649                 goto out;
1650
1651         r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1652                                    passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
1653         if (r < 0)
1654                 goto out;
1655
1656         keyslot = r;
1657         r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1658         if (r)
1659                 log_err(cd, _("Error during resuming device %s.\n"), name);
1660 out:
1661         crypt_safe_free(passphrase_read);
1662         crypt_free_volume_key(vk);
1663         return r < 0 ? r : keyslot;
1664 }
1665
1666 int crypt_resume_by_keyfile(struct crypt_device *cd,
1667                             const char *name,
1668                             int keyslot,
1669                             const char *keyfile,
1670                             size_t keyfile_size)
1671 {
1672         return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1673                                               keyfile, keyfile_size, 0);
1674 }
1675
1676 // slot manipulation
1677 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1678         int keyslot, // -1 any
1679         const char *passphrase, // NULL -> terminal
1680         size_t passphrase_size,
1681         const char *new_passphrase, // NULL -> terminal
1682         size_t new_passphrase_size)
1683 {
1684         struct volume_key *vk = NULL;
1685         char *password = NULL, *new_password = NULL;
1686         size_t passwordLen, new_passwordLen;
1687         int r;
1688
1689         log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1690                 "new passphrase %sprovided.",
1691                 passphrase ? "" : "not ", new_passphrase  ? "" : "not ");
1692
1693         r = onlyLUKS(cd);
1694         if (r < 0)
1695                 return r;
1696
1697         r = keyslot_verify_or_find_empty(cd, &keyslot);
1698         if (r)
1699                 return r;
1700
1701         if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1702                 /* No slots used, try to use pre-generated key in header */
1703                 if (cd->volume_key) {
1704                         vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1705                         r = vk ? 0 : -ENOMEM;
1706                 } else {
1707                         log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1708                         return -EINVAL;
1709                 }
1710         } else if (passphrase) {
1711                 /* Passphrase provided, use it to unlock existing keyslot */
1712                 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1713                                            passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1714         } else {
1715                 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1716                 r = key_from_terminal(cd, _("Enter any passphrase: "),
1717                                       &password, &passwordLen, 0);
1718                 if (r < 0)
1719                         goto out;
1720
1721                 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1722                                            passwordLen, &cd->u.luks1.hdr, &vk, cd);
1723                 crypt_safe_free(password);
1724         }
1725
1726         if(r < 0)
1727                 goto out;
1728
1729         if (new_passphrase) {
1730                 new_password = CONST_CAST(char*)new_passphrase;
1731                 new_passwordLen = new_passphrase_size;
1732         } else {
1733                 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1734                                       &new_password, &new_passwordLen, 1);
1735                 if(r < 0)
1736                         goto out;
1737         }
1738
1739         r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1740                          &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1741         if(r < 0)
1742                 goto out;
1743
1744         r = 0;
1745 out:
1746         if (!new_passphrase)
1747                 crypt_safe_free(new_password);
1748         crypt_free_volume_key(vk);
1749         return r < 0 ? r : keyslot;
1750 }
1751
1752 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
1753         int keyslot_old,
1754         int keyslot_new,
1755         const char *passphrase,
1756         size_t passphrase_size,
1757         const char *new_passphrase,
1758         size_t new_passphrase_size)
1759 {
1760         struct volume_key *vk = NULL;
1761         int r;
1762
1763         log_dbg("Changing passphrase from old keyslot %d to new %d.",
1764                 keyslot_old, keyslot_new);
1765
1766         r = onlyLUKS(cd);
1767         if (r < 0)
1768                 return r;
1769
1770         r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
1771                                    &cd->u.luks1.hdr, &vk, cd);
1772         if (r < 0)
1773                 goto out;
1774
1775         if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
1776                 log_dbg("Keyslot mismatch.");
1777                 goto out;
1778         }
1779         keyslot_old = r;
1780
1781         if (keyslot_new == CRYPT_ANY_SLOT) {
1782                 keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
1783                 if (keyslot_new < 0)
1784                         keyslot_new = keyslot_old;
1785         }
1786
1787         if (keyslot_old == keyslot_new) {
1788                 log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
1789                 (void)crypt_keyslot_destroy(cd, keyslot_old);
1790         }
1791
1792         r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
1793                          &cd->u.luks1.hdr, vk, cd->iteration_time,
1794                          &cd->u.luks1.PBKDF2_per_sec, cd);
1795
1796         if (keyslot_old == keyslot_new) {
1797                 if (r >= 0)
1798                         log_verbose(cd, _("Key slot %d changed.\n"), keyslot_new);
1799         } else {
1800                 if (r >= 0) {
1801                         log_verbose(cd, _("Replaced with key slot %d.\n"), keyslot_new);
1802                         r = crypt_keyslot_destroy(cd, keyslot_old);
1803                 }
1804         }
1805         if (r < 0)
1806                 log_err(cd, _("Failed to swap new key slot.\n"));
1807 out:
1808         crypt_free_volume_key(vk);
1809         return r < 0 ? r : keyslot_new;
1810 }
1811
1812 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1813         int keyslot,
1814         const char *keyfile,
1815         size_t keyfile_size,
1816         size_t keyfile_offset,
1817         const char *new_keyfile,
1818         size_t new_keyfile_size,
1819         size_t new_keyfile_offset)
1820 {
1821         struct volume_key *vk = NULL;
1822         char *password = NULL; size_t passwordLen;
1823         char *new_password = NULL; size_t new_passwordLen;
1824         int r;
1825
1826         log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1827                 keyfile ?: "[none]", new_keyfile ?: "[none]");
1828
1829         r = onlyLUKS(cd);
1830         if (r < 0)
1831                 return r;
1832
1833         r = keyslot_verify_or_find_empty(cd, &keyslot);
1834         if (r)
1835                 return r;
1836
1837         if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1838                 /* No slots used, try to use pre-generated key in header */
1839                 if (cd->volume_key) {
1840                         vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1841                         r = vk ? 0 : -ENOMEM;
1842                 } else {
1843                         log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1844                         return -EINVAL;
1845                 }
1846         } else {
1847                 /* Read password from file of (if NULL) from terminal */
1848                 if (keyfile)
1849                         r = key_from_file(cd, _("Enter any passphrase: "),
1850                                           &password, &passwordLen,
1851                                           keyfile, keyfile_offset, keyfile_size);
1852                 else
1853                         r = key_from_terminal(cd, _("Enter any passphrase: "),
1854                                               &password, &passwordLen, 0);
1855                 if (r < 0)
1856                         goto out;
1857
1858                 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1859                                            &cd->u.luks1.hdr, &vk, cd);
1860         }
1861
1862         if(r < 0)
1863                 goto out;
1864
1865         if (new_keyfile)
1866                 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1867                                   &new_password, &new_passwordLen, new_keyfile,
1868                                   new_keyfile_offset, new_keyfile_size);
1869         else
1870                 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1871                                       &new_password, &new_passwordLen, 1);
1872         if (r < 0)
1873                 goto out;
1874
1875         r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1876                          &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1877 out:
1878         crypt_safe_free(password);
1879         crypt_safe_free(new_password);
1880         crypt_free_volume_key(vk);
1881         return r < 0 ? r : keyslot;
1882 }
1883
1884 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1885         int keyslot,
1886         const char *keyfile,
1887         size_t keyfile_size,
1888         const char *new_keyfile,
1889         size_t new_keyfile_size)
1890 {
1891         return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1892                                 keyfile, keyfile_size, 0,
1893                                 new_keyfile, new_keyfile_size, 0);
1894 }
1895
1896 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1897         int keyslot,
1898         const char *volume_key,
1899         size_t volume_key_size,
1900         const char *passphrase,
1901         size_t passphrase_size)
1902 {
1903         struct volume_key *vk = NULL;
1904         int r;
1905         char *new_password = NULL; size_t new_passwordLen;
1906
1907         log_dbg("Adding new keyslot %d using volume key.", keyslot);
1908
1909         r = onlyLUKS(cd);
1910         if (r < 0)
1911                 return r;
1912
1913         if (volume_key)
1914                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1915         else if (cd->volume_key)
1916                 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1917
1918         if (!vk)
1919                 return -ENOMEM;
1920
1921         r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
1922         if (r < 0) {
1923                 log_err(cd, _("Volume key does not match the volume.\n"));
1924                 goto out;
1925         }
1926
1927         r = keyslot_verify_or_find_empty(cd, &keyslot);
1928         if (r)
1929                 goto out;
1930
1931         if (!passphrase) {
1932                 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1933                                       &new_password, &new_passwordLen, 1);
1934                 if (r < 0)
1935                         goto out;
1936                 passphrase = new_password;
1937                 passphrase_size = new_passwordLen;
1938         }
1939
1940         r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1941                          &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1942 out:
1943         crypt_safe_free(new_password);
1944         crypt_free_volume_key(vk);
1945         return (r < 0) ? r : keyslot;
1946 }
1947
1948 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1949 {
1950         crypt_keyslot_info ki;
1951         int r;
1952
1953         log_dbg("Destroying keyslot %d.", keyslot);
1954
1955         r = onlyLUKS(cd);
1956         if (r < 0)
1957                 return r;
1958
1959         ki = crypt_keyslot_status(cd, keyslot);
1960         if (ki == CRYPT_SLOT_INVALID) {
1961                 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1962                 return -EINVAL;
1963         }
1964
1965         if (ki == CRYPT_SLOT_INACTIVE) {
1966                 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1967                 return -EINVAL;
1968         }
1969
1970         return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
1971 }
1972
1973 // activation/deactivation of device mapping
1974 int crypt_activate_by_passphrase(struct crypt_device *cd,
1975         const char *name,
1976         int keyslot,
1977         const char *passphrase,
1978         size_t passphrase_size,
1979         uint32_t flags)
1980 {
1981         crypt_status_info ci;
1982         struct volume_key *vk = NULL;
1983         char *read_passphrase = NULL;
1984         size_t passphraseLen = 0;
1985         int r;
1986
1987         log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1988                 name ? "Activating" : "Checking", name ?: "",
1989                 keyslot, passphrase ? "" : "[none] ");
1990
1991         if (name) {
1992                 ci = crypt_status(NULL, name);
1993                 if (ci == CRYPT_INVALID)
1994                         return -EINVAL;
1995                 else if (ci >= CRYPT_ACTIVE) {
1996                         log_err(cd, _("Device %s already exists.\n"), name);
1997                         return -EEXIST;
1998                 }
1999         }
2000
2001         /* plain, use hashed passphrase */
2002         if (isPLAIN(cd->type)) {
2003                 if (!name)
2004                         return -EINVAL;
2005
2006                 if (!passphrase) {
2007                         r = key_from_terminal(cd, NULL, &read_passphrase,
2008                                               &passphraseLen, 0);
2009                         if (r < 0)
2010                                 goto out;
2011                         passphrase = read_passphrase;
2012                         passphrase_size = passphraseLen;
2013                 }
2014
2015                 r = process_key(cd, cd->u.plain.hdr.hash,
2016                                 cd->u.plain.key_size,
2017                                 passphrase, passphrase_size, &vk);
2018                 if (r < 0)
2019                         goto out;
2020
2021                 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2022                 keyslot = 0;
2023         } else if (isLUKS(cd->type)) {
2024                 /* provided passphrase, do not retry */
2025                 if (passphrase) {
2026                         r = LUKS_open_key_with_hdr(keyslot, passphrase,
2027                                                    passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2028                 } else
2029                         r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
2030
2031                 if (r >= 0) {
2032                         keyslot = r;
2033                         if (name)
2034                                 r = LUKS1_activate(cd, name, vk, flags);
2035                 }
2036         } else
2037                 r = -EINVAL;
2038 out:
2039         crypt_safe_free(read_passphrase);
2040         crypt_free_volume_key(vk);
2041
2042         return r < 0  ? r : keyslot;
2043 }
2044
2045 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
2046         const char *name,
2047         int keyslot,
2048         const char *keyfile,
2049         size_t keyfile_size,
2050         size_t keyfile_offset,
2051         uint32_t flags)
2052 {
2053         crypt_status_info ci;
2054         struct volume_key *vk = NULL;
2055         char *passphrase_read = NULL;
2056         size_t passphrase_size_read;
2057         unsigned int key_count = 0;
2058         int r;
2059
2060         log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
2061                 name ?: "", keyslot, keyfile ?: "[none]");
2062
2063         if (name) {
2064                 ci = crypt_status(NULL, name);
2065                 if (ci == CRYPT_INVALID)
2066                         return -EINVAL;
2067                 else if (ci >= CRYPT_ACTIVE) {
2068                         log_err(cd, _("Device %s already exists.\n"), name);
2069                         return -EEXIST;
2070                 }
2071         }
2072
2073         if (!keyfile)
2074                 return -EINVAL;
2075
2076         if (isPLAIN(cd->type)) {
2077                 if (!name)
2078                         return -EINVAL;
2079
2080                 r = key_from_file(cd, _("Enter passphrase: "),
2081                                   &passphrase_read, &passphrase_size_read,
2082                                   keyfile, keyfile_offset, keyfile_size);
2083                 if (r < 0)
2084                         goto out;
2085
2086                 r = process_key(cd, cd->u.plain.hdr.hash,
2087                                 cd->u.plain.key_size,
2088                                 passphrase_read, passphrase_size_read, &vk);
2089                 if (r < 0)
2090                         goto out;
2091
2092                 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2093         } else if (isLUKS(cd->type)) {
2094                 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2095                           &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
2096                 if (r < 0)
2097                         goto out;
2098                 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
2099                                            passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
2100                 if (r < 0)
2101                         goto out;
2102                 keyslot = r;
2103
2104                 if (name) {
2105                         r = LUKS1_activate(cd, name, vk, flags);
2106                         if (r < 0)
2107                                 goto out;
2108                 }
2109                 r = keyslot;
2110         } else if (isLOOPAES(cd->type)) {
2111                 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2112                                   keyfile, keyfile_offset, keyfile_size);
2113                 if (r < 0)
2114                         goto out;
2115                 r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
2116                                           passphrase_read, passphrase_size_read);
2117                 if (r < 0)
2118                         goto out;
2119                 if (name)
2120                         r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
2121                                              key_count, vk, flags);
2122         } else
2123                 r = -EINVAL;
2124
2125 out:
2126         crypt_safe_free(passphrase_read);
2127         crypt_free_volume_key(vk);
2128
2129         return r;
2130 }
2131
2132 int crypt_activate_by_keyfile(struct crypt_device *cd,
2133         const char *name,
2134         int keyslot,
2135         const char *keyfile,
2136         size_t keyfile_size,
2137         uint32_t flags)
2138 {
2139         return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
2140                                                 keyfile_size, 0, flags);
2141 }
2142
2143 int crypt_activate_by_volume_key(struct crypt_device *cd,
2144         const char *name,
2145         const char *volume_key,
2146         size_t volume_key_size,
2147         uint32_t flags)
2148 {
2149         crypt_status_info ci;
2150         struct volume_key *vk = NULL;
2151         int r = -EINVAL;
2152
2153         log_dbg("Activating volume %s by volume key.", name ?: "[none]");
2154
2155         if (name) {
2156                 ci = crypt_status(NULL, name);
2157                 if (ci == CRYPT_INVALID)
2158                         return -EINVAL;
2159                 else if (ci >= CRYPT_ACTIVE) {
2160                         log_err(cd, _("Device %s already exists.\n"), name);
2161                         return -EEXIST;
2162                 }
2163         }
2164
2165         /* use key directly, no hash */
2166         if (isPLAIN(cd->type)) {
2167                 if (!name)
2168                         return -EINVAL;
2169
2170                 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
2171                         log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2172                         return -EINVAL;
2173                 }
2174
2175                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2176                 if (!vk)
2177                         return -ENOMEM;
2178
2179                 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2180         } else if (isLUKS(cd->type)) {
2181                 /* If key is not provided, try to use internal key */
2182                 if (!volume_key) {
2183                         if (!cd->volume_key) {
2184                                 log_err(cd, _("Volume key does not match the volume.\n"));
2185                                 return -EINVAL;
2186                         }
2187                         volume_key_size = cd->volume_key->keylength;
2188                         volume_key = cd->volume_key->key;
2189                 }
2190
2191                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2192                 if (!vk)
2193                         return -ENOMEM;
2194                 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2195
2196                 if (r == -EPERM)
2197                         log_err(cd, _("Volume key does not match the volume.\n"));
2198
2199                 if (!r && name)
2200                         r = LUKS1_activate(cd, name, vk, flags);
2201         } else if (isVERITY(cd->type)) {
2202                 /* volume_key == root hash */
2203                 if (!volume_key || !volume_key_size) {
2204                         log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2205                         return -EINVAL;
2206                 }
2207
2208                 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2209                                     &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
2210
2211                 if (r == -EPERM) {
2212                         free(cd->u.verity.root_hash);
2213                         cd->u.verity.root_hash = NULL;
2214                 } if (!r) {
2215                         cd->u.verity.root_hash_size = volume_key_size;
2216                         if (!cd->u.verity.root_hash)
2217                                 cd->u.verity.root_hash = malloc(volume_key_size);
2218                         if (cd->u.verity.root_hash)
2219                                 memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
2220                 }
2221         } else if (isTCRYPT(cd->type)) {
2222                 if (!name)
2223                         return 0;
2224                 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
2225                                     &cd->u.tcrypt.params, flags);
2226         } else
2227                 log_err(cd, _("Device type is not properly initialised.\n"));
2228
2229         crypt_free_volume_key(vk);
2230
2231         return r;
2232 }
2233
2234 int crypt_deactivate(struct crypt_device *cd, const char *name)
2235 {
2236         struct crypt_device *fake_cd = NULL;
2237         int r;
2238
2239         if (!name)
2240                 return -EINVAL;
2241
2242         log_dbg("Deactivating volume %s.", name);
2243
2244         if (!cd) {
2245                 r = crypt_init_by_name(&fake_cd, name);
2246                 if (r < 0)
2247                         return r;
2248                 cd = fake_cd;
2249         }
2250
2251         switch (crypt_status(cd, name)) {
2252                 case CRYPT_ACTIVE:
2253                 case CRYPT_BUSY:
2254                         if (isTCRYPT(cd->type))
2255                                 r = TCRYPT_deactivate(cd, name);
2256                         else
2257                                 r = dm_remove_device(cd, name, 0, 0);
2258                         if (r < 0 && crypt_status(cd, name) == CRYPT_BUSY) {
2259                                 log_err(cd, _("Device %s is still in use.\n"), name);
2260                                 r = -EBUSY;
2261                         }
2262                         break;
2263                 case CRYPT_INACTIVE:
2264                         log_err(cd, _("Device %s is not active.\n"), name);
2265                         r = -ENODEV;
2266                         break;
2267                 default:
2268                         log_err(cd, _("Invalid device %s.\n"), name);
2269                         r = -EINVAL;
2270         }
2271
2272         crypt_free(fake_cd);
2273
2274         return r;
2275 }
2276
2277 int crypt_volume_key_get(struct crypt_device *cd,
2278         int keyslot,
2279         char *volume_key,
2280         size_t *volume_key_size,
2281         const char *passphrase,
2282         size_t passphrase_size)
2283 {
2284         struct volume_key *vk = NULL;
2285         unsigned key_len;
2286         int r = -EINVAL;
2287
2288         if (crypt_fips_mode()) {
2289                 log_err(cd, _("Function not available in FIPS mode.\n"));
2290                 return -EACCES;
2291         }
2292
2293         key_len = crypt_get_volume_key_size(cd);
2294         if (key_len > *volume_key_size) {
2295                 log_err(cd, _("Volume key buffer too small.\n"));
2296                 return -ENOMEM;
2297         }
2298
2299         if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
2300                 r = process_key(cd, cd->u.plain.hdr.hash, key_len,
2301                                 passphrase, passphrase_size, &vk);
2302                 if (r < 0)
2303                         log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2304         } else if (isLUKS(cd->type)) {
2305                 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2306                                         passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2307         } else if (isTCRYPT(cd->type)) {
2308                 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
2309         } else
2310                 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2311
2312         if (r >= 0) {
2313                 memcpy(volume_key, vk->key, vk->keylength);
2314                 *volume_key_size = vk->keylength;
2315         }
2316
2317         crypt_free_volume_key(vk);
2318         return r;
2319 }
2320
2321 int crypt_volume_key_verify(struct crypt_device *cd,
2322         const char *volume_key,
2323         size_t volume_key_size)
2324 {
2325         struct volume_key *vk;
2326         int r;
2327
2328         r = onlyLUKS(cd);
2329         if (r < 0)
2330                 return r;
2331
2332         vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2333         if (!vk)
2334                 return -ENOMEM;
2335
2336         r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2337
2338         if (r == -EPERM)
2339                 log_err(cd, _("Volume key does not match the volume.\n"));
2340
2341         crypt_free_volume_key(vk);
2342
2343         return r;
2344 }
2345
2346 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2347 {
2348         log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2349         cd->timeout = timeout_sec;
2350 }
2351
2352 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2353 {
2354         log_dbg("Password retry count set to %d.", tries);
2355         cd->tries = tries;
2356 }
2357
2358 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2359 {
2360         log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2361         cd->iteration_time = iteration_time_ms;
2362 }
2363 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2364 {
2365         crypt_set_iteration_time(cd, iteration_time_ms);
2366 }
2367
2368 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2369 {
2370         log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2371         cd->password_verify = password_verify ? 1 : 0;
2372 }
2373
2374 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2375 {
2376         switch (rng_type) {
2377         case CRYPT_RNG_URANDOM:
2378         case CRYPT_RNG_RANDOM:
2379                 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2380                 cd->rng_type = rng_type;
2381         }
2382 }
2383
2384 int crypt_get_rng_type(struct crypt_device *cd)
2385 {
2386         if (!cd)
2387                 return -EINVAL;
2388
2389         return cd->rng_type;
2390 }
2391
2392 int crypt_memory_lock(struct crypt_device *cd, int lock)
2393 {
2394         return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2395 }
2396
2397 // reporting
2398 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2399 {
2400         int r;
2401
2402         if (!cd)
2403                 dm_backend_init();
2404
2405         r = dm_status_device(cd, name);
2406
2407         if (!cd)
2408                 dm_backend_exit();
2409
2410         if (r < 0 && r != -ENODEV)
2411                 return CRYPT_INVALID;
2412
2413         if (r == 0)
2414                 return CRYPT_ACTIVE;
2415
2416         if (r > 0)
2417                 return CRYPT_BUSY;
2418
2419         return CRYPT_INACTIVE;
2420 }
2421
2422 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2423 {
2424         int i;
2425         for(i = 0; i < n; i++)
2426                 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2427 }
2428
2429 static int _luks_dump(struct crypt_device *cd)
2430 {
2431         int i;
2432
2433         log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2434         log_std(cd, "Version:       \t%" PRIu16 "\n", cd->u.luks1.hdr.version);
2435         log_std(cd, "Cipher name:   \t%s\n", cd->u.luks1.hdr.cipherName);
2436         log_std(cd, "Cipher mode:   \t%s\n", cd->u.luks1.hdr.cipherMode);
2437         log_std(cd, "Hash spec:     \t%s\n", cd->u.luks1.hdr.hashSpec);
2438         log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset);
2439         log_std(cd, "MK bits:       \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8);
2440         log_std(cd, "MK digest:     \t");
2441         hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2442         log_std(cd, "\n");
2443         log_std(cd, "MK salt:       \t");
2444         hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2445         log_std(cd, "\n               \t");
2446         hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2447         log_std(cd, "\n");
2448         log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations);
2449         log_std(cd, "UUID:          \t%s\n\n", cd->u.luks1.hdr.uuid);
2450         for(i = 0; i < LUKS_NUMKEYS; i++) {
2451                 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2452                         log_std(cd, "Key Slot %d: ENABLED\n",i);
2453                         log_std(cd, "\tIterations:         \t%" PRIu32 "\n",
2454                                 cd->u.luks1.hdr.keyblock[i].passwordIterations);
2455                         log_std(cd, "\tSalt:               \t");
2456                         hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
2457                                  LUKS_SALTSIZE/2, " ");
2458                         log_std(cd, "\n\t                      \t");
2459                         hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
2460                                  LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2461                         log_std(cd, "\n");
2462
2463                         log_std(cd, "\tKey material offset:\t%" PRIu32 "\n",
2464                                 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
2465                         log_std(cd, "\tAF stripes:            \t%" PRIu32 "\n",
2466                                 cd->u.luks1.hdr.keyblock[i].stripes);
2467                 }
2468                 else 
2469                         log_std(cd, "Key Slot %d: DISABLED\n", i);
2470         }
2471         return 0;
2472 }
2473
2474 static int _verity_dump(struct crypt_device *cd)
2475 {
2476         log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2477         log_std(cd, "UUID:            \t%s\n", cd->u.verity.uuid ?: "");
2478         log_std(cd, "Hash type:       \t%u\n", cd->u.verity.hdr.hash_type);
2479         log_std(cd, "Data blocks:     \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
2480         log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
2481         log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
2482         log_std(cd, "Hash algorithm:  \t%s\n", cd->u.verity.hdr.hash_name);
2483         log_std(cd, "Salt:            \t");
2484         if (cd->u.verity.hdr.salt_size)
2485                 hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
2486         else
2487                 log_std(cd, "-");
2488         log_std(cd, "\n");
2489         if (cd->u.verity.root_hash) {
2490                 log_std(cd, "Root hash:      \t");
2491                 hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
2492                 log_std(cd, "\n");
2493         }
2494         return 0;
2495 }
2496
2497 int crypt_dump(struct crypt_device *cd)
2498 {
2499         if (isLUKS(cd->type))
2500                 return _luks_dump(cd);
2501         else if (isVERITY(cd->type))
2502                 return _verity_dump(cd);
2503         else if (isTCRYPT(cd->type))
2504                 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2505
2506         log_err(cd, _("Dump operation is not supported for this device type.\n"));
2507         return -EINVAL;
2508 }
2509
2510
2511 static int _init_by_name_crypt_none(struct crypt_device *cd)
2512 {
2513         struct crypt_dm_active_device dmd = {};
2514         int r;
2515
2516         if (cd->type || !cd->u.none.active_name)
2517                 return -EINVAL;
2518
2519         r = dm_query_device(cd, cd->u.none.active_name,
2520                         DM_ACTIVE_CRYPT_CIPHER |
2521                         DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
2522         if (r >= 0)
2523                 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher,
2524                                               cd->u.none.cipher, NULL,
2525                                               cd->u.none.cipher_mode);
2526
2527         if (!r)
2528                 cd->u.none.key_size = dmd.u.crypt.vk->keylength;
2529
2530         crypt_free_volume_key(dmd.u.crypt.vk);
2531         free(CONST_CAST(void*)dmd.u.crypt.cipher);
2532         return r;
2533 }
2534
2535 const char *crypt_get_cipher(struct crypt_device *cd)
2536 {
2537         if (isPLAIN(cd->type))
2538                 return cd->u.plain.cipher;
2539
2540         if (isLUKS(cd->type))
2541                 return cd->u.luks1.hdr.cipherName;
2542
2543         if (isLOOPAES(cd->type))
2544                 return cd->u.loopaes.cipher;
2545
2546         if (isTCRYPT(cd->type))
2547                 return cd->u.tcrypt.params.cipher;
2548
2549         if (!cd->type && !_init_by_name_crypt_none(cd))
2550                 return cd->u.none.cipher;
2551
2552         return NULL;
2553 }
2554
2555 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2556 {
2557         if (isPLAIN(cd->type))
2558                 return cd->u.plain.cipher_mode;
2559
2560         if (isLUKS(cd->type))
2561                 return cd->u.luks1.hdr.cipherMode;
2562
2563         if (isLOOPAES(cd->type))
2564                 return cd->u.loopaes.cipher_mode;
2565
2566         if (isTCRYPT(cd->type))
2567                 return cd->u.tcrypt.params.mode;
2568
2569         if (!cd->type && !_init_by_name_crypt_none(cd))
2570                 return cd->u.none.cipher_mode;
2571
2572         return NULL;
2573 }
2574
2575 const char *crypt_get_uuid(struct crypt_device *cd)
2576 {
2577         if (isLUKS(cd->type))
2578                 return cd->u.luks1.hdr.uuid;
2579
2580         if (isVERITY(cd->type))
2581                 return cd->u.verity.uuid;
2582
2583         return NULL;
2584 }
2585
2586 const char *crypt_get_device_name(struct crypt_device *cd)
2587 {
2588         const char *path = device_block_path(cd->device);
2589
2590         if (!path)
2591                 path = device_path(cd->device);
2592
2593         return path;
2594 }
2595
2596 int crypt_get_volume_key_size(struct crypt_device *cd)
2597 {
2598         if (isPLAIN(cd->type))
2599                 return cd->u.plain.key_size;
2600
2601         if (isLUKS(cd->type))
2602                 return cd->u.luks1.hdr.keyBytes;
2603
2604         if (isLOOPAES(cd->type))
2605                 return cd->u.loopaes.key_size;
2606
2607         if (isVERITY(cd->type))
2608                 return cd->u.verity.root_hash_size;
2609
2610         if (isTCRYPT(cd->type))
2611                 return cd->u.tcrypt.params.key_size;
2612
2613         if (!cd->type && !_init_by_name_crypt_none(cd))
2614                 return cd->u.none.key_size;
2615
2616         return 0;
2617 }
2618
2619 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2620 {
2621         if (isPLAIN(cd->type))
2622                 return cd->u.plain.hdr.offset;
2623
2624         if (isLUKS(cd->type))
2625                 return cd->u.luks1.hdr.payloadOffset;
2626
2627         if (isLOOPAES(cd->type))
2628                 return cd->u.loopaes.hdr.offset;
2629
2630         if (isTCRYPT(cd->type))
2631                 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2632
2633         return 0;
2634 }
2635
2636 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2637 {
2638         if (isPLAIN(cd->type))
2639                 return cd->u.plain.hdr.skip;
2640
2641         if (isLUKS(cd->type))
2642                 return 0;
2643
2644         if (isLOOPAES(cd->type))
2645                 return cd->u.loopaes.hdr.skip;
2646
2647         if (isTCRYPT(cd->type))
2648                 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2649
2650         return 0;
2651 }
2652
2653 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2654 {
2655         if (onlyLUKS(cd) < 0)
2656                 return CRYPT_SLOT_INVALID;
2657
2658         return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
2659 }
2660
2661 int crypt_keyslot_max(const char *type)
2662 {
2663         if (type && isLUKS(type))
2664                 return LUKS_NUMKEYS;
2665
2666         return -EINVAL;
2667 }
2668
2669 int crypt_keyslot_area(struct crypt_device *cd,
2670         int keyslot,
2671         uint64_t *offset,
2672         uint64_t *length)
2673 {
2674         if (!isLUKS(cd->type))
2675                 return -EINVAL;
2676
2677         return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
2678 }
2679
2680 const char *crypt_get_type(struct crypt_device *cd)
2681 {
2682         return cd->type;
2683 }
2684
2685 int crypt_get_verity_info(struct crypt_device *cd,
2686         struct crypt_params_verity *vp)
2687 {
2688         if (!isVERITY(cd->type) || !vp)
2689                 return -EINVAL;
2690
2691         vp->data_device = device_path(cd->device);
2692         vp->hash_device = mdata_device_path(cd);
2693         vp->hash_name = cd->u.verity.hdr.hash_name;
2694         vp->salt = cd->u.verity.hdr.salt;
2695         vp->salt_size = cd->u.verity.hdr.salt_size;
2696         vp->data_block_size = cd->u.verity.hdr.data_block_size;
2697         vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
2698         vp->data_size = cd->u.verity.hdr.data_size;
2699         vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
2700         vp->hash_type = cd->u.verity.hdr.hash_type;
2701         vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
2702         return 0;
2703 }
2704
2705 int crypt_get_active_device(struct crypt_device *cd, const char *name,
2706                             struct crypt_active_device *cad)
2707 {
2708         struct crypt_dm_active_device dmd;
2709         int r;
2710
2711         r = dm_query_device(cd, name, 0, &dmd);
2712         if (r < 0)
2713                 return r;
2714
2715         if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2716                 return -ENOTSUP;
2717
2718         if (cd && isTCRYPT(cd->type)) {
2719                 cad->offset     = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2720                 cad->iv_offset  = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2721         } else {
2722                 cad->offset     = dmd.u.crypt.offset;
2723                 cad->iv_offset  = dmd.u.crypt.iv_offset;
2724         }
2725         cad->size       = dmd.size;
2726         cad->flags      = dmd.flags;
2727
2728         return 0;
2729 }