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