Fix clang compile warnings (error path).
[platform/upstream/cryptsetup.git] / lib / setup.c
1 /*
2  * libcryptsetup - cryptsetup library
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <fcntl.h>
27 #include <errno.h>
28
29 #include "libcryptsetup.h"
30 #include "luks.h"
31 #include "loopaes.h"
32 #include "internal.h"
33 #include "crypto_backend.h"
34
35 struct crypt_device {
36         char *type;
37
38         char *device;
39         char *metadata_device;
40
41         char *backing_file;
42         int loop_fd;
43         struct volume_key *volume_key;
44         uint64_t timeout;
45         uint64_t iteration_time;
46         int tries;
47         int password_verify;
48         int rng_type;
49
50         /* used in CRYPT_LUKS1 */
51         struct luks_phdr hdr;
52         uint64_t PBKDF2_per_sec;
53
54         /* used in CRYPT_PLAIN */
55         struct crypt_params_plain plain_hdr;
56         char *plain_cipher;
57         char *plain_cipher_mode;
58         char *plain_uuid;
59         unsigned int plain_key_size;
60
61         /* used in CRYPT_LOOPAES */
62         struct crypt_params_loopaes loopaes_hdr;
63         char *loopaes_cipher;
64         char *loopaes_cipher_mode;
65         char *loopaes_uuid;
66         unsigned int loopaes_key_size;
67
68         /* callbacks definitions */
69         void (*log)(int level, const char *msg, void *usrptr);
70         void *log_usrptr;
71         int (*confirm)(const char *msg, void *usrptr);
72         void *confirm_usrptr;
73         int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
74         void *password_usrptr;
75
76         /* last error message */
77         char error[MAX_ERROR_LENGTH];
78 };
79
80 /* Global error */
81 /* FIXME: not thread safe, remove this later */
82 static char global_error[MAX_ERROR_LENGTH] = {0};
83
84 /* Log helper */
85 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
86 static int _debug_level = 0;
87
88 void crypt_set_debug_level(int level)
89 {
90         _debug_level = level;
91 }
92
93 int crypt_get_debug_level(void)
94 {
95         return _debug_level;
96 }
97
98 static void crypt_set_error(struct crypt_device *cd, const char *error)
99 {
100         size_t size = strlen(error);
101
102         /* Set global error, ugly hack... */
103         strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
104         if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
105                 global_error[size - 1] = '\0';
106
107         /* Set error string per context */
108         if (cd) {
109                 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
110                 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
111                         cd->error[size - 1] = '\0';
112         }
113 }
114
115 void crypt_log(struct crypt_device *cd, int level, const char *msg)
116 {
117         if (cd && cd->log)
118                 cd->log(level, msg, cd->log_usrptr);
119         else if (_default_log)
120                 _default_log(level, msg, NULL);
121
122         if (level == CRYPT_LOG_ERROR)
123                 crypt_set_error(cd, msg);
124 }
125
126 __attribute__((format(printf, 5, 6)))
127 void logger(struct crypt_device *cd, int level, const char *file,
128             int line, const char *format, ...)
129 {
130         va_list argp;
131         char *target = NULL;
132
133         va_start(argp, format);
134
135         if (vasprintf(&target, format, argp) > 0 ) {
136                 if (level >= 0) {
137                         crypt_log(cd, level, target);
138 #ifdef CRYPT_DEBUG
139                 } else if (_debug_level)
140                         printf("# %s:%d %s\n", file ?: "?", line, target);
141 #else
142                 } else if (_debug_level)
143                         printf("# %s\n", target);
144 #endif
145         }
146
147         va_end(argp);
148         free(target);
149 }
150
151 static const char *mdata_device(struct crypt_device *cd)
152 {
153         return cd->metadata_device ?: cd->device;
154 }
155
156 static int init_crypto(struct crypt_device *ctx)
157 {
158         int r;
159
160         r = crypt_random_init(ctx);
161         if (r < 0) {
162                 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
163                 return r;
164         }
165
166         r = crypt_backend_init(ctx);
167         if (r < 0)
168                 log_err(ctx, _("Cannot initialize crypto backend.\n"));
169
170         return r;
171 }
172
173 static int process_key(struct crypt_device *cd, const char *hash_name,
174                        size_t key_size, const char *pass, size_t passLen,
175                        struct volume_key **vk)
176 {
177         int r;
178
179         if (!key_size)
180                 return -EINVAL;
181
182         *vk = crypt_alloc_volume_key(key_size, NULL);
183         if (!*vk)
184                 return -ENOMEM;
185
186         if (hash_name) {
187                 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
188                 if (r < 0) {
189                         if (r == -ENOENT)
190                                 log_err(cd, _("Hash algorithm %s not supported.\n"),
191                                         hash_name);
192                         else
193                                 log_err(cd, _("Key processing error (using hash %s).\n"),
194                                         hash_name);
195                         crypt_free_volume_key(*vk);
196                         *vk = NULL;
197                         return -EINVAL;
198                 }
199         } else if (passLen > key_size) {
200                 memcpy((*vk)->key, pass, key_size);
201         } else {
202                 memcpy((*vk)->key, pass, passLen);
203         }
204
205         return 0;
206 }
207
208 static int isPLAIN(const char *type)
209 {
210         return (type && !strcmp(CRYPT_PLAIN, type));
211 }
212
213 static int isLUKS(const char *type)
214 {
215         return (type && !strcmp(CRYPT_LUKS1, type));
216 }
217
218 static int isLOOPAES(const char *type)
219 {
220         return (type && !strcmp(CRYPT_LOOPAES, type));
221 }
222
223 /* keyslot helpers */
224 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
225 {
226         if (*keyslot == CRYPT_ANY_SLOT) {
227                 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
228                 if (*keyslot < 0) {
229                         log_err(cd, _("All key slots full.\n"));
230                         return -EINVAL;
231                 }
232         }
233
234         switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
235                 case CRYPT_SLOT_INVALID:
236                         log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
237                                 *keyslot, LUKS_NUMKEYS - 1);
238                         return -EINVAL;
239                 case CRYPT_SLOT_INACTIVE:
240                         break;
241                 default:
242                         log_err(cd, _("Key slot %d is full, please select another one.\n"),
243                                 *keyslot);
244                         return -EINVAL;
245         }
246
247         return 0;
248 }
249
250 /*
251  * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
252  */
253 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
254 {
255         int i, j;
256         char *str;
257
258         if (!dm_uuid || !hdr_uuid)
259                 return -EINVAL;
260
261         str = strchr(dm_uuid, '-');
262         if (!str)
263                 return -EINVAL;
264
265         for (i = 0, j = 1; hdr_uuid[i]; i++) {
266                 if (hdr_uuid[i] == '-')
267                         continue;
268
269                 if (!str[j] || str[j] == '-')
270                         return -EINVAL;
271
272                 if (str[j] != hdr_uuid[i])
273                         return -EINVAL;
274                 j++;
275         }
276
277         return 0;
278 }
279
280 int PLAIN_activate(struct crypt_device *cd,
281                      const char *name,
282                      struct volume_key *vk,
283                      uint64_t size,
284                      uint32_t flags)
285 {
286         int r;
287         char *dm_cipher = NULL;
288         enum devcheck device_check;
289         struct crypt_dm_active_device dmd = {
290                 .device = crypt_get_device_name(cd),
291                 .cipher = NULL,
292                 .uuid   = crypt_get_uuid(cd),
293                 .vk    = vk,
294                 .offset = crypt_get_data_offset(cd),
295                 .iv_offset = crypt_get_iv_offset(cd),
296                 .size   = size,
297                 .flags  = flags
298         };
299
300         if (dmd.flags & CRYPT_ACTIVATE_SHARED)
301                 device_check = DEV_SHARED;
302         else
303                 device_check = DEV_EXCL;
304
305         r = device_check_and_adjust(cd, dmd.device, device_check,
306                                     &dmd.size, &dmd.offset, &flags);
307         if (r)
308                 return r;
309
310         if (crypt_get_cipher_mode(cd))
311                 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
312         else
313                 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
314         if (r < 0)
315                 return -ENOMEM;
316
317         dmd.cipher = dm_cipher;
318         log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
319
320         r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
321
322         // FIXME
323         if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
324                 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
325
326         free(dm_cipher);
327         return r;
328 }
329
330 int crypt_confirm(struct crypt_device *cd, const char *msg)
331 {
332         if (!cd || !cd->confirm)
333                 return 1;
334         else
335                 return cd->confirm(msg, cd->confirm_usrptr);
336 }
337
338 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
339                               size_t *key_len, int force_verify)
340 {
341         char *prompt = NULL;
342         int r;
343
344         *key = NULL;
345         if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
346                             cd->backing_file ?: crypt_get_device_name(cd)) < 0)
347                 return -ENOMEM;
348
349         if (!msg)
350                 msg = prompt;
351
352         if (cd->password) {
353                 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
354                 if (!*key) {
355                         r = -ENOMEM;
356                         goto out;
357                 }
358                 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
359                                  cd->password_usrptr);
360                 if (r < 0) {
361                         crypt_safe_free(*key);
362                         *key = NULL;
363                 } else
364                         *key_len = r;
365         } else
366                 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
367                                   (force_verify || cd->password_verify), cd);
368 out:
369         free(prompt);
370         return (r < 0) ? r: 0;
371 }
372
373 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
374                                              struct volume_key **vk)
375 {
376         char *passphrase_read = NULL;
377         size_t passphrase_size_read;
378         int r = -EINVAL, eperm = 0, tries = cd->tries;
379
380         *vk = NULL;
381         do {
382                 crypt_free_volume_key(*vk);
383                 *vk = NULL;
384
385                 r = key_from_terminal(cd, NULL, &passphrase_read,
386                                       &passphrase_size_read, 0);
387                 /* Continue if it is just passphrase verify mismatch */
388                 if (r == -EPERM)
389                         continue;
390                 if(r < 0)
391                         goto out;
392
393                 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
394                                            passphrase_size_read, &cd->hdr, vk, cd);
395                 if (r == -EPERM)
396                         eperm = 1;
397                 crypt_safe_free(passphrase_read);
398                 passphrase_read = NULL;
399         } while (r == -EPERM && (--tries > 0));
400 out:
401         if (r < 0) {
402                 crypt_free_volume_key(*vk);
403                 *vk = NULL;
404
405                 /* Report wrong passphrase if at least one try failed */
406                 if (eperm && r == -EPIPE)
407                         r = -EPERM;
408         }
409
410         crypt_safe_free(passphrase_read);
411         return r;
412 }
413
414 static int key_from_file(struct crypt_device *cd, char *msg,
415                           char **key, size_t *key_len,
416                           const char *key_file, size_t key_offset,
417                           size_t key_size)
418 {
419         return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
420                              cd->timeout, 0, cd);
421 }
422
423 void crypt_set_log_callback(struct crypt_device *cd,
424         void (*log)(int level, const char *msg, void *usrptr),
425         void *usrptr)
426 {
427         if (!cd)
428                 _default_log = log;
429         else {
430                 cd->log = log;
431                 cd->log_usrptr = usrptr;
432         }
433 }
434
435 void crypt_set_confirm_callback(struct crypt_device *cd,
436         int (*confirm)(const char *msg, void *usrptr),
437         void *usrptr)
438 {
439         cd->confirm = confirm;
440         cd->confirm_usrptr = usrptr;
441 }
442
443 void crypt_set_password_callback(struct crypt_device *cd,
444         int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
445         void *usrptr)
446 {
447         cd->password = password;
448         cd->password_usrptr = usrptr;
449 }
450
451 static void _get_error(char *error, char *buf, size_t size)
452 {
453         if (!buf || size < 1)
454                 error[0] = '\0';
455         else if (*error) {
456                 strncpy(buf, error, size - 1);
457                 buf[size - 1] = '\0';
458                 error[0] = '\0';
459         } else
460                 buf[0] = '\0';
461 }
462
463 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
464 {
465         if (cd)
466                 return _get_error(cd->error, buf, size);
467 }
468
469 /* Deprecated global error interface */
470 void crypt_get_error(char *buf, size_t size)
471 {
472         return _get_error(global_error, buf, size);
473 }
474
475 const char *crypt_get_dir(void)
476 {
477         return dm_get_dir();
478 }
479
480 int crypt_init(struct crypt_device **cd, const char *device)
481 {
482         struct crypt_device *h = NULL;
483         int r, readonly = 0;
484
485         if (!cd)
486                 return -EINVAL;
487
488         log_dbg("Allocating crypt device %s context.", device);
489
490         if (!(h = malloc(sizeof(struct crypt_device))))
491                 return -ENOMEM;
492
493         memset(h, 0, sizeof(*h));
494         h->loop_fd = -1;
495
496         if (device) {
497                 r = device_ready(NULL, device, O_RDONLY);
498                 if (r == -ENOTBLK) {
499                         h->device = crypt_loop_get_device();
500                         log_dbg("Not a block device, %s%s.",
501                                 h->device ? "using free loop device " :
502                                          "no free loop device found",
503                                 h->device ?: "");
504                         if (!h->device) {
505                                 log_err(NULL, _("Cannot find a free loopback device.\n"));
506                                 r = -ENOSYS;
507                                 goto bad;
508                         }
509
510                         /* Keep the loop open, dettached on last close. */
511                         h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
512                         if (h->loop_fd == -1) {
513                                 log_err(NULL, _("Attaching loopback device failed "
514                                         "(loop device with autoclear flag is required).\n"));
515                                 r = -EINVAL;
516                                 goto bad;
517                         }
518
519                         h->backing_file = crypt_loop_backing_file(h->device);
520                         r = device_ready(NULL, h->device, O_RDONLY);
521                 }
522                 if (r < 0) {
523                         r = -ENOTBLK;
524                         goto bad;
525                 }
526         }
527
528         if (!h->device && device && !(h->device = strdup(device))) {
529                 r = -ENOMEM;
530                 goto bad;
531         }
532
533         if (dm_init(h, 1) < 0) {
534                 r = -ENOSYS;
535                 goto bad;
536         }
537
538         h->iteration_time = 1000;
539         h->password_verify = 0;
540         h->tries = 3;
541         h->rng_type = crypt_random_default_key_rng();
542         *cd = h;
543         return 0;
544 bad:
545
546         if (h) {
547                 if (h->loop_fd != -1)
548                         close(h->loop_fd);
549                 free(h->device);
550                 free(h->backing_file);
551         }
552         free(h);
553         return r;
554 }
555
556 static int crypt_check_data_device_size(struct crypt_device *cd)
557 {
558         int r;
559         uint64_t size, size_min;
560
561         /* Check data device size, require at least one sector */
562         size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
563
564         r = device_size(crypt_get_device_name(cd), &size);
565         if (r < 0)
566                 return r;
567
568         if (size < size_min) {
569                 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
570                         crypt_get_device_name(cd));
571                 return -EINVAL;
572         }
573
574         return r;
575 }
576
577 int crypt_set_data_device(struct crypt_device *cd, const char *device)
578 {
579         char *data_device;
580         int r;
581
582         log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
583
584         if (!isLUKS(cd->type)) {
585                 log_err(cd, _("This operation is not supported for this device type.\n"));
586                 return  -EINVAL;
587         }
588
589         /* metadata device must be set */
590         if (!cd->device || !device)
591                 return -EINVAL;
592
593         r = device_ready(NULL, device, O_RDONLY);
594         if (r < 0)
595                 return r;
596
597         if (!(data_device = strdup(device)))
598                 return -ENOMEM;
599
600         if (!cd->metadata_device)
601                 cd->metadata_device = cd->device;
602         else
603                 free(cd->device);
604
605         cd->device = data_device;
606
607         return crypt_check_data_device_size(cd);
608 }
609
610 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
611 {
612         struct luks_phdr hdr;
613         int r;
614
615         r = init_crypto(cd);
616         if (r < 0)
617                 return r;
618
619         r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
620         if (r < 0)
621                 return r;
622
623         if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
624                 return -ENOMEM;
625
626         memcpy(&cd->hdr, &hdr, sizeof(hdr));
627
628         return r;
629 }
630
631 int crypt_init_by_name_and_header(struct crypt_device **cd,
632                                   const char *name,
633                                   const char *header_device)
634 {
635         crypt_status_info ci;
636         struct crypt_dm_active_device dmd;
637         char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
638         int key_nums, r;
639
640
641         log_dbg("Allocating crypt device context by device %s.", name);
642
643         ci = crypt_status(NULL, name);
644         if (ci == CRYPT_INVALID)
645                 return -ENODEV;
646
647         if (ci < CRYPT_ACTIVE) {
648                 log_err(NULL, _("Device %s is not active.\n"), name);
649                 return -ENODEV;
650         }
651
652         r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
653                                   DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
654         if (r < 0)
655                 goto out;
656
657         *cd = NULL;
658
659         if (header_device) {
660                 r = crypt_init(cd, header_device);
661         } else {
662                 r = crypt_init(cd, dmd.device);
663
664                 /* Underlying device disappeared but mapping still active */
665                 if (!dmd.device || r == -ENOTBLK)
666                         log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
667                                     name);
668
669                 /* Underlying device is not readable but crypt mapping exists */
670                 if (r == -ENOTBLK) {
671                         free(CONST_CAST(void*)dmd.device);
672                         dmd.device = NULL;
673                         r = crypt_init(cd, NULL);
674                 }
675         }
676
677         if (r < 0)
678                 goto out;
679
680         if (dmd.uuid) {
681                 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
682                         (*cd)->type = strdup(CRYPT_PLAIN);
683                 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
684                         (*cd)->type = strdup(CRYPT_LOOPAES);
685                 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
686                         (*cd)->type = strdup(CRYPT_LUKS1);
687                 else
688                         log_dbg("Unknown UUID set, some parameters are not set.");
689         } else
690                 log_dbg("Active device has no UUID set, some parameters are not set.");
691
692         if (header_device) {
693                 r = crypt_set_data_device(*cd, dmd.device);
694                 if (r < 0)
695                         goto out;
696         }
697
698         /* Try to initialise basic parameters from active device */
699
700         if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
701             !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
702                 r = -ENOMEM;
703                 goto out;
704         }
705
706         if (isPLAIN((*cd)->type)) {
707                 (*cd)->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
708                 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
709                 (*cd)->plain_hdr.offset = dmd.offset;
710                 (*cd)->plain_hdr.skip = dmd.iv_offset;
711                 (*cd)->plain_key_size = dmd.vk->keylength;
712
713                 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
714                 if (!r) {
715                         (*cd)->plain_cipher = strdup(cipher);
716                         (*cd)->plain_cipher_mode = strdup(cipher_mode);
717                 }
718         } else if (isLOOPAES((*cd)->type)) {
719                 (*cd)->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
720                 (*cd)->loopaes_hdr.offset = dmd.offset;
721
722                 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
723                                               &key_nums, cipher_mode);
724                 if (!r) {
725                         (*cd)->loopaes_cipher = strdup(cipher);
726                         (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
727                         /* version 3 uses last key for IV */
728                         if (dmd.vk->keylength % key_nums)
729                                 key_nums++;
730                         (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
731                 }
732         } else if (isLUKS((*cd)->type)) {
733                 if (mdata_device(*cd)) {
734                         r = _crypt_load_luks1(*cd, 0, 0);
735                         if (r < 0) {
736                                 log_dbg("LUKS device header does not match active device.");
737                                 free((*cd)->type);
738                                 (*cd)->type = NULL;
739                                 r = 0;
740                                 goto out;
741                         }
742                         /* checks whether UUIDs match each other */
743                         r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
744                         if (r < 0) {
745                                 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
746                                         (*cd)->hdr.uuid, dmd.uuid);
747                                 free((*cd)->type);
748                                 (*cd)->type = NULL;
749                                 r = 0;
750                                 goto out;
751                         }
752                 }
753         }
754
755 out:
756         if (r < 0) {
757                 crypt_free(*cd);
758                 *cd = NULL;
759         }
760         crypt_free_volume_key(dmd.vk);
761         free(CONST_CAST(void*)dmd.device);
762         free(CONST_CAST(void*)dmd.cipher);
763         free(CONST_CAST(void*)dmd.uuid);
764         return r;
765 }
766
767 int crypt_init_by_name(struct crypt_device **cd, const char *name)
768 {
769         return crypt_init_by_name_and_header(cd, name, NULL);
770 }
771
772 static int _crypt_format_plain(struct crypt_device *cd,
773                                const char *cipher,
774                                const char *cipher_mode,
775                                const char *uuid,
776                                size_t volume_key_size,
777                                struct crypt_params_plain *params)
778 {
779         if (!cipher || !cipher_mode) {
780                 log_err(cd, _("Invalid plain crypt parameters.\n"));
781                 return -EINVAL;
782         }
783
784         if (volume_key_size > 1024) {
785                 log_err(cd, _("Invalid key size.\n"));
786                 return -EINVAL;
787         }
788
789         cd->plain_key_size = volume_key_size;
790         cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
791         if (!cd->volume_key)
792                 return -ENOMEM;
793
794         cd->plain_cipher = strdup(cipher);
795         cd->plain_cipher_mode = strdup(cipher_mode);
796
797         if (uuid)
798                 cd->plain_uuid = strdup(uuid);
799
800         if (params && params->hash)
801                 cd->plain_hdr.hash = strdup(params->hash);
802
803         cd->plain_hdr.offset = params ? params->offset : 0;
804         cd->plain_hdr.skip = params ? params->skip : 0;
805         cd->plain_hdr.size = params ? params->size : 0;
806
807         if (!cd->plain_cipher || !cd->plain_cipher_mode)
808                 return -ENOMEM;
809
810         return 0;
811 }
812
813 static int _crypt_format_luks1(struct crypt_device *cd,
814                                const char *cipher,
815                                const char *cipher_mode,
816                                const char *uuid,
817                                const char *volume_key,
818                                size_t volume_key_size,
819                                struct crypt_params_luks1 *params)
820 {
821         int r;
822         unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
823         unsigned long alignment_offset = 0;
824
825         if (!mdata_device(cd)) {
826                 log_err(cd, _("Can't format LUKS without device.\n"));
827                 return -EINVAL;
828         }
829
830         if (volume_key)
831                 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
832                                                       volume_key);
833         else
834                 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
835
836         if(!cd->volume_key)
837                 return -ENOMEM;
838
839         if (params && params->data_device) {
840                 cd->metadata_device = cd->device;
841                 if (!(cd->device = strdup(params->data_device)))
842                         return -ENOMEM;
843                 required_alignment = params->data_alignment * SECTOR_SIZE;
844         } else if (params && params->data_alignment) {
845                 required_alignment = params->data_alignment * SECTOR_SIZE;
846         } else
847                 get_topology_alignment(cd->device, &required_alignment,
848                                        &alignment_offset, DEFAULT_DISK_ALIGNMENT);
849
850         r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
851                                (params && params->hash) ? params->hash : "sha1",
852                                uuid, LUKS_STRIPES,
853                                required_alignment / SECTOR_SIZE,
854                                alignment_offset / SECTOR_SIZE,
855                                cd->iteration_time, &cd->PBKDF2_per_sec,
856                                cd->metadata_device, cd);
857         if(r < 0)
858                 return r;
859
860         /* Wipe first 8 sectors - fs magic numbers etc. */
861         r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
862         if(r < 0) {
863                 if (r == -EBUSY)
864                         log_err(cd, _("Cannot format device %s which is still in use.\n"),
865                                 mdata_device(cd));
866                 else
867                         log_err(cd, _("Cannot wipe header on device %s.\n"),
868                                 mdata_device(cd));
869
870                 return r;
871         }
872
873         r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
874
875         return r;
876 }
877
878 static int _crypt_format_loopaes(struct crypt_device *cd,
879                                  const char *cipher,
880                                  const char *uuid,
881                                  size_t volume_key_size,
882                                  struct crypt_params_loopaes *params)
883 {
884         if (!mdata_device(cd)) {
885                 log_err(cd, _("Can't format LOOPAES without device.\n"));
886                 return -EINVAL;
887         }
888
889         if (volume_key_size > 1024) {
890                 log_err(cd, _("Invalid key size.\n"));
891                 return -EINVAL;
892         }
893
894         cd->loopaes_key_size = volume_key_size;
895
896         cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
897
898         if (uuid)
899                 cd->loopaes_uuid = strdup(uuid);
900
901         if (params && params->hash)
902                 cd->loopaes_hdr.hash = strdup(params->hash);
903
904         cd->loopaes_hdr.offset = params ? params->offset : 0;
905         cd->loopaes_hdr.skip = params ? params->skip : 0;
906
907         return 0;
908 }
909
910 int crypt_format(struct crypt_device *cd,
911         const char *type,
912         const char *cipher,
913         const char *cipher_mode,
914         const char *uuid,
915         const char *volume_key,
916         size_t volume_key_size,
917         void *params)
918 {
919         int r;
920
921         if (!type)
922                 return -EINVAL;
923
924         if (cd->type) {
925                 log_dbg("Context already formatted as %s.", cd->type);
926                 return -EINVAL;
927         }
928
929         log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
930
931         r = init_crypto(cd);
932         if (r < 0)
933                 return r;
934
935         if (isPLAIN(type))
936                 r = _crypt_format_plain(cd, cipher, cipher_mode,
937                                         uuid, volume_key_size, params);
938         else if (isLUKS(type))
939                 r = _crypt_format_luks1(cd, cipher, cipher_mode,
940                                         uuid, volume_key, volume_key_size, params);
941         else if (isLOOPAES(type))
942                 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
943         else {
944                 /* FIXME: allow plugins here? */
945                 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
946                 r = -EINVAL;
947         }
948
949         if (!r && !(cd->type = strdup(type)))
950                 r = -ENOMEM;
951
952         if (r < 0) {
953                 crypt_free_volume_key(cd->volume_key);
954                 cd->volume_key = NULL;
955         }
956
957         return r;
958 }
959
960 int crypt_load(struct crypt_device *cd,
961                const char *requested_type,
962                void *params __attribute__((unused)))
963 {
964         int r;
965
966         log_dbg("Trying to load %s crypt type from device %s.",
967                 requested_type ?: "any", mdata_device(cd) ?: "(none)");
968
969         if (!mdata_device(cd))
970                 return -EINVAL;
971
972         if (requested_type && !isLUKS(requested_type))
973                 return -EINVAL;
974
975         if (cd->type && !isLUKS(cd->type)) {
976                 log_dbg("Context is already initialised to type %s", cd->type);
977                 return -EINVAL;
978         }
979
980         r = _crypt_load_luks1(cd, 1, 0);
981         if (r < 0)
982                 return r;
983
984         /* cd->type and header must be set in context */
985         r = crypt_check_data_device_size(cd);
986         if (r < 0) {
987                 free(cd->type);
988                 cd->type = NULL;
989         }
990
991         return r;
992 }
993
994 int crypt_repair(struct crypt_device *cd,
995                  const char *requested_type,
996                  void *params __attribute__((unused)))
997 {
998         int r;
999
1000         log_dbg("Trying to repair %s crypt type from device %s.",
1001                 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1002
1003         if (!mdata_device(cd))
1004                 return -EINVAL;
1005
1006         if (requested_type && !isLUKS(requested_type))
1007                 return -EINVAL;
1008
1009
1010         /* Load with repair */
1011         r = _crypt_load_luks1(cd, 1, 1);
1012         if (r < 0)
1013                 return r;
1014
1015         /* cd->type and header must be set in context */
1016         r = crypt_check_data_device_size(cd);
1017         if (r < 0) {
1018                 free(cd->type);
1019                 cd->type = NULL;
1020         }
1021
1022         return r;
1023 }
1024
1025 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1026 {
1027         struct crypt_dm_active_device dmd;
1028         int r;
1029
1030         /* Device context type must be initialised */
1031         if (!cd->type || !crypt_get_uuid(cd))
1032                 return -EINVAL;
1033
1034         log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1035
1036         r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
1037                                   DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
1038                                   DM_ACTIVE_KEY, &dmd);
1039         if (r < 0) {
1040                 log_err(NULL, _("Device %s is not active.\n"), name);
1041                 goto out;
1042         }
1043
1044         if (!dmd.uuid) {
1045                 r = -EINVAL;
1046                 goto out;
1047         }
1048
1049         r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
1050         if (r)
1051                 goto out;
1052
1053         if (new_size == dmd.size) {
1054                 log_dbg("Device has already requested size %" PRIu64
1055                         " sectors.", dmd.size);
1056                 r = 0;
1057         } else {
1058                 dmd.size = new_size;
1059                 r = dm_create_device(name, cd->type, &dmd, 1);
1060         }
1061 out:
1062         crypt_free_volume_key(dmd.vk);
1063         free(CONST_CAST(void*)dmd.cipher);
1064         free(CONST_CAST(void*)dmd.device);
1065         free(CONST_CAST(void*)dmd.uuid);
1066
1067         return r;
1068 }
1069
1070 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1071 {
1072         if (!isLUKS(cd->type)) {
1073                 log_err(cd, _("This operation is not supported for this device type.\n"));
1074                 return  -EINVAL;
1075         }
1076
1077         if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1078                 log_dbg("UUID is the same as requested (%s) for device %s.",
1079                         uuid, mdata_device(cd));
1080                 return 0;
1081         }
1082
1083         if (uuid)
1084                 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1085         else
1086                 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1087
1088         if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1089                 return -EPERM;
1090
1091         return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1092 }
1093
1094 int crypt_header_backup(struct crypt_device *cd,
1095                         const char *requested_type,
1096                         const char *backup_file)
1097 {
1098         int r;
1099
1100         if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1101                 return -EINVAL;
1102
1103         r = init_crypto(cd);
1104         if (r < 0)
1105                 return r;
1106
1107         log_dbg("Requested header backup of device %s (%s) to "
1108                 "file %s.", mdata_device(cd), requested_type, backup_file);
1109
1110         return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1111 }
1112
1113 int crypt_header_restore(struct crypt_device *cd,
1114                          const char *requested_type,
1115                          const char *backup_file)
1116 {
1117         int r;
1118
1119         if (requested_type && !isLUKS(requested_type))
1120                 return -EINVAL;
1121
1122         /* Some hash functions need initialized gcrypt library */
1123         r = init_crypto(cd);
1124         if (r < 0)
1125                 return r;
1126
1127         log_dbg("Requested header restore to device %s (%s) from "
1128                 "file %s.", mdata_device(cd), requested_type, backup_file);
1129
1130         return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1131 }
1132
1133 void crypt_free(struct crypt_device *cd)
1134 {
1135         if (cd) {
1136                 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1137
1138                 if (cd->loop_fd != -1)
1139                         close(cd->loop_fd);
1140
1141                 dm_exit();
1142                 crypt_free_volume_key(cd->volume_key);
1143
1144                 free(cd->device);
1145                 free(cd->metadata_device);
1146                 free(cd->backing_file);
1147                 free(cd->type);
1148
1149                 /* used in plain device only */
1150                 free(CONST_CAST(void*)cd->plain_hdr.hash);
1151                 free(cd->plain_cipher);
1152                 free(cd->plain_cipher_mode);
1153                 free(cd->plain_uuid);
1154
1155                 /* used in loop-AES device only */
1156                 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1157                 free(cd->loopaes_cipher);
1158                 free(cd->loopaes_uuid);
1159
1160                 free(cd);
1161         }
1162 }
1163
1164 int crypt_suspend(struct crypt_device *cd,
1165                   const char *name)
1166 {
1167         crypt_status_info ci;
1168         int r;
1169
1170         log_dbg("Suspending volume %s.", name);
1171
1172         if (!isLUKS(cd->type)) {
1173                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1174                 r = -EINVAL;
1175                 goto out;
1176         }
1177
1178         ci = crypt_status(NULL, name);
1179         if (ci < CRYPT_ACTIVE) {
1180                 log_err(cd, _("Volume %s is not active.\n"), name);
1181                 return -EINVAL;
1182         }
1183
1184         if (!cd && dm_init(NULL, 1) < 0)
1185                 return -ENOSYS;
1186
1187         r = dm_status_suspended(name);
1188         if (r < 0)
1189                 goto out;
1190
1191         if (r) {
1192                 log_err(cd, _("Volume %s is already suspended.\n"), name);
1193                 r = -EINVAL;
1194                 goto out;
1195         }
1196
1197         r = dm_suspend_and_wipe_key(name);
1198         if (r == -ENOTSUP)
1199                 log_err(cd, "Suspend is not supported for device %s.\n", name);
1200         else if (r)
1201                 log_err(cd, "Error during suspending device %s.\n", name);
1202 out:
1203         if (!cd)
1204                 dm_exit();
1205         return r;
1206 }
1207
1208 int crypt_resume_by_passphrase(struct crypt_device *cd,
1209                                const char *name,
1210                                int keyslot,
1211                                const char *passphrase,
1212                                size_t passphrase_size)
1213 {
1214         struct volume_key *vk = NULL;
1215         int r;
1216
1217         log_dbg("Resuming volume %s.", name);
1218
1219         if (!isLUKS(cd->type)) {
1220                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1221                 r = -EINVAL;
1222                 goto out;
1223         }
1224
1225         r = dm_status_suspended(name);
1226         if (r < 0)
1227                 return r;
1228
1229         if (!r) {
1230                 log_err(cd, _("Volume %s is not suspended.\n"), name);
1231                 return -EINVAL;
1232         }
1233
1234         if (passphrase) {
1235                 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1236                                            passphrase_size, &cd->hdr, &vk, cd);
1237         } else
1238                 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1239
1240         if (r >= 0) {
1241                 keyslot = r;
1242                 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1243                 if (r == -ENOTSUP)
1244                         log_err(cd, "Resume is not supported for device %s.\n", name);
1245                 else if (r)
1246                         log_err(cd, "Error during resuming device %s.\n", name);
1247         } else
1248                 r = keyslot;
1249 out:
1250         crypt_free_volume_key(vk);
1251         return r < 0 ? r : keyslot;
1252 }
1253
1254 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1255                                    const char *name,
1256                                    int keyslot,
1257                                    const char *keyfile,
1258                                    size_t keyfile_size,
1259                                    size_t keyfile_offset)
1260 {
1261         struct volume_key *vk = NULL;
1262         char *passphrase_read = NULL;
1263         size_t passphrase_size_read;
1264         int r;
1265
1266         log_dbg("Resuming volume %s.", name);
1267
1268         if (!isLUKS(cd->type)) {
1269                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1270                 r = -EINVAL;
1271                 goto out;
1272         }
1273
1274         r = dm_status_suspended(name);
1275         if (r < 0)
1276                 return r;
1277
1278         if (!r) {
1279                 log_err(cd, _("Volume %s is not suspended.\n"), name);
1280                 return -EINVAL;
1281         }
1282
1283         if (!keyfile)
1284                 return -EINVAL;
1285
1286         r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1287                           &passphrase_size_read, keyfile, keyfile_offset,
1288                           keyfile_size);
1289         if (r < 0)
1290                 goto out;
1291
1292         r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1293                                    passphrase_size_read, &cd->hdr, &vk, cd);
1294         if (r < 0)
1295                 goto out;
1296
1297         keyslot = r;
1298         r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1299         if (r)
1300                 log_err(cd, "Error during resuming device %s.\n", name);
1301 out:
1302         crypt_safe_free(passphrase_read);
1303         crypt_free_volume_key(vk);
1304         return r < 0 ? r : keyslot;
1305 }
1306
1307 int crypt_resume_by_keyfile(struct crypt_device *cd,
1308                             const char *name,
1309                             int keyslot,
1310                             const char *keyfile,
1311                             size_t keyfile_size)
1312 {
1313         return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1314                                               keyfile, keyfile_size, 0);
1315 }
1316
1317 // slot manipulation
1318 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1319         int keyslot, // -1 any
1320         const char *passphrase, // NULL -> terminal
1321         size_t passphrase_size,
1322         const char *new_passphrase, // NULL -> terminal
1323         size_t new_passphrase_size)
1324 {
1325         struct volume_key *vk = NULL;
1326         char *password = NULL, *new_password = NULL;
1327         size_t passwordLen, new_passwordLen;
1328         int r;
1329
1330         log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1331                 "new passphrase %sprovided.",
1332                 passphrase ? "" : "not ", new_passphrase  ? "" : "not ");
1333
1334         if (!isLUKS(cd->type)) {
1335                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1336                 return -EINVAL;
1337         }
1338
1339         r = keyslot_verify_or_find_empty(cd, &keyslot);
1340         if (r)
1341                 return r;
1342
1343         if (!LUKS_keyslot_active_count(&cd->hdr)) {
1344                 /* No slots used, try to use pre-generated key in header */
1345                 if (cd->volume_key) {
1346                         vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1347                         r = vk ? 0 : -ENOMEM;
1348                 } else {
1349                         log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1350                         return -EINVAL;
1351                 }
1352         } else if (passphrase) {
1353                 /* Passphrase provided, use it to unlock existing keyslot */
1354                 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1355                                            passphrase_size, &cd->hdr, &vk, cd);
1356         } else {
1357                 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1358                 r = key_from_terminal(cd, _("Enter any passphrase: "),
1359                                       &password, &passwordLen, 0);
1360                 if (r < 0)
1361                         goto out;
1362
1363                 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1364                                            passwordLen, &cd->hdr, &vk, cd);
1365                 crypt_safe_free(password);
1366         }
1367
1368         if(r < 0)
1369                 goto out;
1370
1371         if (new_passphrase) {
1372                 new_password = CONST_CAST(char*)new_passphrase;
1373                 new_passwordLen = new_passphrase_size;
1374         } else {
1375                 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1376                                       &new_password, &new_passwordLen, 1);
1377                 if(r < 0)
1378                         goto out;
1379         }
1380
1381         r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1382                          &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1383         if(r < 0) goto out;
1384
1385         r = 0;
1386 out:
1387         if (!new_passphrase)
1388                 crypt_safe_free(new_password);
1389         crypt_free_volume_key(vk);
1390         return r ?: keyslot;
1391 }
1392
1393 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1394         int keyslot,
1395         const char *keyfile,
1396         size_t keyfile_size,
1397         size_t keyfile_offset,
1398         const char *new_keyfile,
1399         size_t new_keyfile_size,
1400         size_t new_keyfile_offset)
1401 {
1402         struct volume_key *vk = NULL;
1403         char *password = NULL; size_t passwordLen;
1404         char *new_password = NULL; size_t new_passwordLen;
1405         int r;
1406
1407         log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1408                 keyfile ?: "[none]", new_keyfile ?: "[none]");
1409
1410         if (!isLUKS(cd->type)) {
1411                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1412                 return -EINVAL;
1413         }
1414
1415         r = keyslot_verify_or_find_empty(cd, &keyslot);
1416         if (r)
1417                 return r;
1418
1419         if (!LUKS_keyslot_active_count(&cd->hdr)) {
1420                 /* No slots used, try to use pre-generated key in header */
1421                 if (cd->volume_key) {
1422                         vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1423                         r = vk ? 0 : -ENOMEM;
1424                 } else {
1425                         log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1426                         return -EINVAL;
1427                 }
1428         } else {
1429                 /* Read password from file of (if NULL) from terminal */
1430                 if (keyfile)
1431                         r = key_from_file(cd, _("Enter any passphrase: "),
1432                                           &password, &passwordLen,
1433                                           keyfile, keyfile_offset, keyfile_size);
1434                 else
1435                         r = key_from_terminal(cd, _("Enter any passphrase: "),
1436                                               &password, &passwordLen, 0);
1437                 if (r < 0)
1438                         goto out;
1439
1440                 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1441                                            &cd->hdr, &vk, cd);
1442         }
1443
1444         if(r < 0)
1445                 goto out;
1446
1447         if (new_keyfile)
1448                 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1449                                   &new_password, &new_passwordLen, new_keyfile,
1450                                   new_keyfile_offset, new_keyfile_size);
1451         else
1452                 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1453                                       &new_password, &new_passwordLen, 1);
1454         if (r < 0)
1455                 goto out;
1456
1457         r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1458                          &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1459 out:
1460         crypt_safe_free(password);
1461         crypt_safe_free(new_password);
1462         crypt_free_volume_key(vk);
1463         return r < 0 ? r : keyslot;
1464 }
1465
1466 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1467         int keyslot,
1468         const char *keyfile,
1469         size_t keyfile_size,
1470         const char *new_keyfile,
1471         size_t new_keyfile_size)
1472 {
1473         return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1474                                 keyfile, keyfile_size, 0,
1475                                 new_keyfile, new_keyfile_size, 0);
1476 }
1477
1478 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1479         int keyslot,
1480         const char *volume_key,
1481         size_t volume_key_size,
1482         const char *passphrase,
1483         size_t passphrase_size)
1484 {
1485         struct volume_key *vk = NULL;
1486         int r = -EINVAL;
1487         char *new_password = NULL; size_t new_passwordLen;
1488
1489         log_dbg("Adding new keyslot %d using volume key.", keyslot);
1490
1491         if (!isLUKS(cd->type)) {
1492                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1493                 return -EINVAL;
1494         }
1495
1496         if (volume_key)
1497                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1498         else if (cd->volume_key)
1499                 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1500
1501         if (!vk)
1502                 return -ENOMEM;
1503
1504         r = LUKS_verify_volume_key(&cd->hdr, vk);
1505         if (r < 0) {
1506                 log_err(cd, _("Volume key does not match the volume.\n"));
1507                 goto out;
1508         }
1509
1510         r = keyslot_verify_or_find_empty(cd, &keyslot);
1511         if (r)
1512                 goto out;
1513
1514         if (!passphrase) {
1515                 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1516                                       &new_password, &new_passwordLen, 1);
1517                 if (r < 0)
1518                         goto out;
1519                 passphrase = new_password;
1520                 passphrase_size = new_passwordLen;
1521         }
1522
1523         r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1524                          &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1525 out:
1526         crypt_safe_free(new_password);
1527         crypt_free_volume_key(vk);
1528         return (r < 0) ? r : keyslot;
1529 }
1530
1531 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1532 {
1533         crypt_keyslot_info ki;
1534
1535         log_dbg("Destroying keyslot %d.", keyslot);
1536
1537         if (!isLUKS(cd->type)) {
1538                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1539                 return -EINVAL;
1540         }
1541
1542         ki = crypt_keyslot_status(cd, keyslot);
1543         if (ki == CRYPT_SLOT_INVALID) {
1544                 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1545                 return -EINVAL;
1546         }
1547
1548         if (ki == CRYPT_SLOT_INACTIVE) {
1549                 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1550                 return -EINVAL;
1551         }
1552
1553         return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1554 }
1555
1556 // activation/deactivation of device mapping
1557 int crypt_activate_by_passphrase(struct crypt_device *cd,
1558         const char *name,
1559         int keyslot,
1560         const char *passphrase,
1561         size_t passphrase_size,
1562         uint32_t flags)
1563 {
1564         crypt_status_info ci;
1565         struct volume_key *vk = NULL;
1566         char *read_passphrase = NULL;
1567         size_t passphraseLen = 0;
1568         int r;
1569
1570         log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1571                 name ? "Activating" : "Checking", name ?: "",
1572                 keyslot, passphrase ? "" : "[none] ");
1573
1574         if (name) {
1575                 ci = crypt_status(NULL, name);
1576                 if (ci == CRYPT_INVALID)
1577                         return -EINVAL;
1578                 else if (ci >= CRYPT_ACTIVE) {
1579                         log_err(cd, _("Device %s already exists.\n"), name);
1580                         return -EEXIST;
1581                 }
1582         }
1583
1584         /* plain, use hashed passphrase */
1585         if (isPLAIN(cd->type)) {
1586                 if (!name)
1587                         return -EINVAL;
1588
1589                 if (!passphrase) {
1590                         r = key_from_terminal(cd, NULL, &read_passphrase,
1591                                               &passphraseLen, 0);
1592                         if (r < 0)
1593                                 goto out;
1594                         passphrase = read_passphrase;
1595                         passphrase_size = passphraseLen;
1596                 }
1597
1598                 r = process_key(cd, cd->plain_hdr.hash,
1599                                 cd->plain_key_size,
1600                                 passphrase, passphrase_size, &vk);
1601                 if (r < 0)
1602                         goto out;
1603
1604                 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1605                 keyslot = 0;
1606         } else if (isLUKS(cd->type)) {
1607                 /* provided passphrase, do not retry */
1608                 if (passphrase) {
1609                         r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1610                                                    passphrase_size, &cd->hdr, &vk, cd);
1611                 } else
1612                         r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1613
1614                 if (r >= 0) {
1615                         keyslot = r;
1616                         if (name)
1617                                 r = LUKS1_activate(cd, name, vk, flags);
1618                 }
1619         } else
1620                 r = -EINVAL;
1621 out:
1622         crypt_safe_free(read_passphrase);
1623         crypt_free_volume_key(vk);
1624
1625         return r < 0  ? r : keyslot;
1626 }
1627
1628 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1629         const char *name,
1630         int keyslot,
1631         const char *keyfile,
1632         size_t keyfile_size,
1633         size_t keyfile_offset,
1634         uint32_t flags)
1635 {
1636         crypt_status_info ci;
1637         struct volume_key *vk = NULL;
1638         char *passphrase_read = NULL;
1639         size_t passphrase_size_read;
1640         unsigned int key_count = 0;
1641         int r;
1642
1643         log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1644                 name ?: "", keyslot, keyfile ?: "[none]");
1645
1646         if (name) {
1647                 ci = crypt_status(NULL, name);
1648                 if (ci == CRYPT_INVALID)
1649                         return -EINVAL;
1650                 else if (ci >= CRYPT_ACTIVE) {
1651                         log_err(cd, _("Device %s already exists.\n"), name);
1652                         return -EEXIST;
1653                 }
1654         }
1655
1656         if (!keyfile)
1657                 return -EINVAL;
1658
1659         if (isPLAIN(cd->type)) {
1660                 if (!name)
1661                         return -EINVAL;
1662
1663                 r = key_from_file(cd, _("Enter passphrase: "),
1664                                   &passphrase_read, &passphrase_size_read,
1665                                   keyfile, keyfile_offset, keyfile_size);
1666                 if (r < 0)
1667                         goto out;
1668
1669                 r = process_key(cd, cd->plain_hdr.hash,
1670                                 cd->plain_key_size,
1671                                 passphrase_read, passphrase_size_read, &vk);
1672                 if (r < 0)
1673                         goto out;
1674
1675                 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1676         } else if (isLUKS(cd->type)) {
1677                 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1678                           &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1679                 if (r < 0)
1680                         goto out;
1681                 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1682                                            passphrase_size_read, &cd->hdr, &vk, cd);
1683                 if (r < 0)
1684                         goto out;
1685                 keyslot = r;
1686
1687                 if (name) {
1688                         r = LUKS1_activate(cd, name, vk, flags);
1689                         if (r < 0)
1690                                 goto out;
1691                 }
1692                 r = keyslot;
1693         } else if (isLOOPAES(cd->type)) {
1694                 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1695                                   keyfile, keyfile_offset, keyfile_size);
1696                 if (r < 0)
1697                         goto out;
1698                 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1699                                           passphrase_read, passphrase_size_read);
1700                 if (r < 0)
1701                         goto out;
1702                 if (name)
1703                         r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1704                                              key_count, vk, flags);
1705         } else
1706                 r = -EINVAL;
1707
1708 out:
1709         crypt_safe_free(passphrase_read);
1710         crypt_free_volume_key(vk);
1711
1712         return r;
1713 }
1714
1715 int crypt_activate_by_keyfile(struct crypt_device *cd,
1716         const char *name,
1717         int keyslot,
1718         const char *keyfile,
1719         size_t keyfile_size,
1720         uint32_t flags)
1721 {
1722         return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1723                                                 keyfile_size, 0, flags);
1724 }
1725
1726 int crypt_activate_by_volume_key(struct crypt_device *cd,
1727         const char *name,
1728         const char *volume_key,
1729         size_t volume_key_size,
1730         uint32_t flags)
1731 {
1732         crypt_status_info ci;
1733         struct volume_key *vk = NULL;
1734         int r = -EINVAL;
1735
1736         log_dbg("Activating volume %s by volume key.", name);
1737
1738         if (name) {
1739                 ci = crypt_status(NULL, name);
1740                 if (ci == CRYPT_INVALID)
1741                         return -EINVAL;
1742                 else if (ci >= CRYPT_ACTIVE) {
1743                         log_err(cd, _("Device %s already exists.\n"), name);
1744                         return -EEXIST;
1745                 }
1746         }
1747
1748         /* use key directly, no hash */
1749         if (isPLAIN(cd->type)) {
1750                 if (!name)
1751                         return -EINVAL;
1752
1753                 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1754                         log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1755                         return -EINVAL;
1756                 }
1757
1758                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1759                 if (!vk)
1760                         return -ENOMEM;
1761
1762                 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1763         } else if (isLUKS(cd->type)) {
1764                 /* If key is not provided, try to use internal key */
1765                 if (!volume_key) {
1766                         if (!cd->volume_key) {
1767                                 log_err(cd, _("Volume key does not match the volume.\n"));
1768                                 return -EINVAL;
1769                         }
1770                         volume_key_size = cd->volume_key->keylength;
1771                         volume_key = cd->volume_key->key;
1772                 }
1773
1774                 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1775                 if (!vk)
1776                         return -ENOMEM;
1777                 r = LUKS_verify_volume_key(&cd->hdr, vk);
1778
1779                 if (r == -EPERM)
1780                         log_err(cd, _("Volume key does not match the volume.\n"));
1781
1782                 if (!r && name)
1783                         r = LUKS1_activate(cd, name, vk, flags);
1784         } else
1785                 log_err(cd, _("Device type is not properly initialised.\n"));
1786
1787         crypt_free_volume_key(vk);
1788
1789         return r;
1790 }
1791
1792 int crypt_deactivate(struct crypt_device *cd, const char *name)
1793 {
1794         int r;
1795
1796         if (!name)
1797                 return -EINVAL;
1798
1799         log_dbg("Deactivating volume %s.", name);
1800
1801         if (!cd && dm_init(NULL, 1) < 0)
1802                 return -ENOSYS;
1803
1804         switch (crypt_status(cd, name)) {
1805                 case CRYPT_ACTIVE:
1806                 case CRYPT_BUSY:
1807                         r = dm_remove_device(name, 0, 0);
1808                         break;
1809                 case CRYPT_INACTIVE:
1810                         log_err(cd, _("Device %s is not active.\n"), name);
1811                         r = -ENODEV;
1812                         break;
1813                 default:
1814                         log_err(cd, _("Invalid device %s.\n"), name);
1815                         r = -EINVAL;
1816         }
1817
1818         if (!cd)
1819                 dm_exit();
1820
1821         return r;
1822 }
1823
1824 int crypt_volume_key_get(struct crypt_device *cd,
1825         int keyslot,
1826         char *volume_key,
1827         size_t *volume_key_size,
1828         const char *passphrase,
1829         size_t passphrase_size)
1830 {
1831         struct volume_key *vk = NULL;
1832         unsigned key_len;
1833         int r = -EINVAL;
1834
1835         if (crypt_fips_mode()) {
1836                 log_err(cd, "Function not available in FIPS mode.\n");
1837                 return -EACCES;
1838         }
1839
1840         key_len = crypt_get_volume_key_size(cd);
1841         if (key_len > *volume_key_size) {
1842                 log_err(cd, _("Volume key buffer too small.\n"));
1843                 return -ENOMEM;
1844         }
1845
1846         if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1847                 r = process_key(cd, cd->plain_hdr.hash, key_len,
1848                                 passphrase, passphrase_size, &vk);
1849                 if (r < 0)
1850                         log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1851         } else if (isLUKS(cd->type)) {
1852                 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1853                                         passphrase_size, &cd->hdr, &vk, cd);
1854
1855         } else
1856                 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1857
1858         if (r >= 0) {
1859                 memcpy(volume_key, vk->key, vk->keylength);
1860                 *volume_key_size = vk->keylength;
1861         }
1862
1863         crypt_free_volume_key(vk);
1864         return r;
1865 }
1866
1867 int crypt_volume_key_verify(struct crypt_device *cd,
1868         const char *volume_key,
1869         size_t volume_key_size)
1870 {
1871         struct volume_key *vk;
1872         int r;
1873
1874         if (!isLUKS(cd->type)) {
1875                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1876                 return -EINVAL;
1877         }
1878
1879         vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1880         if (!vk)
1881                 return -ENOMEM;
1882
1883         r = LUKS_verify_volume_key(&cd->hdr, vk);
1884
1885         if (r == -EPERM)
1886                 log_err(cd, _("Volume key does not match the volume.\n"));
1887
1888         crypt_free_volume_key(vk);
1889
1890         return r;
1891 }
1892
1893 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1894 {
1895         log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1896         cd->timeout = timeout_sec;
1897 }
1898
1899 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1900 {
1901         log_dbg("Password retry count set to %d.", tries);
1902         cd->tries = tries;
1903 }
1904
1905 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1906 {
1907         log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1908         cd->iteration_time = iteration_time_ms;
1909 }
1910 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1911 {
1912         crypt_set_iteration_time(cd, iteration_time_ms);
1913 }
1914
1915 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1916 {
1917         log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1918         cd->password_verify = password_verify ? 1 : 0;
1919 }
1920
1921 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1922 {
1923         switch (rng_type) {
1924         case CRYPT_RNG_URANDOM:
1925         case CRYPT_RNG_RANDOM:
1926                 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1927                 cd->rng_type = rng_type;
1928         }
1929 }
1930
1931 int crypt_get_rng_type(struct crypt_device *cd)
1932 {
1933         if (!cd)
1934                 return -EINVAL;
1935
1936         return cd->rng_type;
1937 }
1938
1939 int crypt_memory_lock(struct crypt_device *cd, int lock)
1940 {
1941         return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1942 }
1943
1944 // reporting
1945 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1946 {
1947         int r;
1948
1949         if (!cd && dm_init(NULL, 1) < 0)
1950                 return CRYPT_INVALID;
1951
1952         r = dm_status_device(name);
1953
1954         if (!cd)
1955                 dm_exit();
1956
1957         if (r < 0 && r != -ENODEV)
1958                 return CRYPT_INVALID;
1959
1960         if (r == 0)
1961                 return CRYPT_ACTIVE;
1962
1963         if (r > 0)
1964                 return CRYPT_BUSY;
1965
1966         return CRYPT_INACTIVE;
1967 }
1968
1969 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1970 {
1971         int i;
1972         for(i = 0; i < n; i++)
1973                 log_std(cd, "%02hhx ", (char)d[i]);
1974 }
1975
1976 int crypt_dump(struct crypt_device *cd)
1977 {
1978         int i;
1979         if (!isLUKS(cd->type)) { //FIXME
1980                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1981                 return -EINVAL;
1982         }
1983
1984         log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1985         log_std(cd, "Version:       \t%d\n", cd->hdr.version);
1986         log_std(cd, "Cipher name:   \t%s\n", cd->hdr.cipherName);
1987         log_std(cd, "Cipher mode:   \t%s\n", cd->hdr.cipherMode);
1988         log_std(cd, "Hash spec:     \t%s\n", cd->hdr.hashSpec);
1989         log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1990         log_std(cd, "MK bits:       \t%d\n", cd->hdr.keyBytes * 8);
1991         log_std(cd, "MK digest:     \t");
1992         hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1993         log_std(cd, "\n");
1994         log_std(cd, "MK salt:       \t");
1995         hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1996         log_std(cd, "\n               \t");
1997         hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1998         log_std(cd, "\n");
1999         log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2000         log_std(cd, "UUID:          \t%s\n\n", cd->hdr.uuid);
2001         for(i = 0; i < LUKS_NUMKEYS; i++) {
2002                 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2003                         log_std(cd, "Key Slot %d: ENABLED\n",i);
2004                         log_std(cd, "\tIterations:         \t%d\n",
2005                                 cd->hdr.keyblock[i].passwordIterations);
2006                         log_std(cd, "\tSalt:               \t");
2007                         hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2008                                     LUKS_SALTSIZE/2);
2009                         log_std(cd, "\n\t                      \t");
2010                         hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2011                                     LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2012                         log_std(cd, "\n");
2013
2014                         log_std(cd, "\tKey material offset:\t%d\n",
2015                                 cd->hdr.keyblock[i].keyMaterialOffset);
2016                         log_std(cd, "\tAF stripes:            \t%d\n",
2017                                 cd->hdr.keyblock[i].stripes);
2018                 }
2019                 else 
2020                         log_std(cd, "Key Slot %d: DISABLED\n", i);
2021         }
2022
2023         return 0;
2024 }
2025
2026 const char *crypt_get_cipher(struct crypt_device *cd)
2027 {
2028         if (isPLAIN(cd->type))
2029                 return cd->plain_cipher;
2030
2031         if (isLUKS(cd->type))
2032                 return cd->hdr.cipherName;
2033
2034         if (isLOOPAES(cd->type))
2035                 return cd->loopaes_cipher;
2036
2037         return NULL;
2038 }
2039
2040 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2041 {
2042         if (isPLAIN(cd->type))
2043                 return cd->plain_cipher_mode;
2044
2045         if (isLUKS(cd->type))
2046                 return cd->hdr.cipherMode;
2047
2048         if (isLOOPAES(cd->type))
2049                 return cd->loopaes_cipher_mode;
2050
2051         return NULL;
2052 }
2053
2054 const char *crypt_get_uuid(struct crypt_device *cd)
2055 {
2056         if (isLUKS(cd->type))
2057                 return cd->hdr.uuid;
2058
2059         if (isPLAIN(cd->type))
2060                 return cd->plain_uuid;
2061
2062         if (isLOOPAES(cd->type))
2063                 return cd->loopaes_uuid;
2064
2065         return NULL;
2066 }
2067
2068 const char *crypt_get_device_name(struct crypt_device *cd)
2069 {
2070         return cd->device;
2071 }
2072
2073
2074 int crypt_get_volume_key_size(struct crypt_device *cd)
2075 {
2076         if (isPLAIN(cd->type))
2077                 return cd->plain_key_size;
2078
2079         if (isLUKS(cd->type))
2080                 return cd->hdr.keyBytes;
2081
2082         if (isLOOPAES(cd->type))
2083                 return cd->loopaes_key_size;
2084
2085         return 0;
2086 }
2087
2088 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2089 {
2090         if (isPLAIN(cd->type))
2091                 return cd->plain_hdr.offset;
2092
2093         if (isLUKS(cd->type))
2094                 return cd->hdr.payloadOffset;
2095
2096         if (isLOOPAES(cd->type))
2097                 return cd->loopaes_hdr.offset;
2098
2099         return 0;
2100 }
2101
2102 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2103 {
2104         if (isPLAIN(cd->type))
2105                 return cd->plain_hdr.skip;
2106
2107         if (isLUKS(cd->type))
2108                 return 0;
2109
2110         if (isLOOPAES(cd->type))
2111                 return cd->loopaes_hdr.skip;
2112
2113         return 0;
2114 }
2115
2116 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2117 {
2118         if (!isLUKS(cd->type)) {
2119                 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2120                 return CRYPT_SLOT_INVALID;
2121         }
2122
2123         return LUKS_keyslot_info(&cd->hdr, keyslot);
2124 }
2125
2126 int crypt_keyslot_max(const char *type)
2127 {
2128         if (type && isLUKS(type))
2129                 return LUKS_NUMKEYS;
2130
2131         return -EINVAL;
2132 }
2133
2134 const char *crypt_get_type(struct crypt_device *cd)
2135 {
2136         return cd->type;
2137 }
2138
2139 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2140                             const char *name,
2141                             struct crypt_active_device *cad)
2142 {
2143         struct crypt_dm_active_device dmd;
2144         int r;
2145
2146         r = dm_query_device(name, 0, &dmd);
2147         if (r < 0)
2148                 return r;
2149
2150         cad->offset     = dmd.offset;
2151         cad->iv_offset  = dmd.iv_offset;
2152         cad->size       = dmd.size;
2153         cad->flags      = dmd.flags;
2154
2155         return 0;
2156 }