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