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