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