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