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