Merge branch 'master' of https://code.google.com/p/cryptsetup
[platform/upstream/cryptsetup.git] / lib / libcryptsetup.h
1 /*
2  * libcryptsetup - cryptsetup library
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /**
23  * @file libcryptsetup.h
24  * @brief Public cryptsetup API
25  *
26  * For more verbose examples of LUKS related use cases,
27  * please read @ref index "examples".
28  */
29
30 #ifndef _LIBCRYPTSETUP_H
31 #define _LIBCRYPTSETUP_H
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #include <stddef.h>
37 #include <stdint.h>
38
39 struct crypt_device; /* crypt device handle */
40
41 /**
42  * Initialize crypt device handle and check if provided device exists.
43  *
44  * @param cd Returns pointer to crypt device handle
45  * @param device Path to the backing device.
46  *        If @e device is not a block device but a path to some file,
47  *        the function will try to create a loopdevice and attach
48  *        the file to the loopdevice with AUTOCLEAR flag set.
49  *        If @e device is @e NULL function it will initialize dm backend only.
50  *
51  * @return @e 0 on success or negative errno value otherwise.
52  *
53  * @note Note that logging is not initialized here, possible messages uses
54  *       default log function.
55  */
56 int crypt_init(struct crypt_device **cd, const char *device);
57
58 /**
59  * Initialize crypt device handle from provided active device name,
60  * and, optionally, from separate metadata (header) device
61  * and check if provided device exists.
62  *
63  * @return @e 0 on success or negative errno value otherwise.
64  *
65  * @param cd returns crypt device handle for active device
66  * @param name name of active crypt device
67  * @param header_device optional device containing on-disk header
68  *        (@e NULL if it the same as underlying device on there is no on-disk header)
69  *
70  * @post In case @e device points to active LUKS device but header load fails,
71  * context device type is set to @e NULL and @e 0 is returned as if it were successful.
72  * Context with @e NULL device type can only be deactivated by crypt_deactivate
73  *
74  * @note @link crypt_init_by_name @endlink is equivalent to calling
75  *       crypt_init_by_name_and_header(cd, name, NULL);
76  */
77 int crypt_init_by_name_and_header(struct crypt_device **cd,
78                                   const char *name,
79                                   const char *header_device);
80
81 /**
82  * This is equivalent to call
83  * @ref crypt_init_by_name_and_header "crypt_init_by_name_and_header(cd, name, NULL)"
84  *
85  * @sa crypt_init_by_name_and_header
86  */
87 int crypt_init_by_name(struct crypt_device **cd, const char *name);
88
89 /**
90  * @defgroup loglevel "Cryptsetup logging"
91  *
92  * Set of functions and defines used in cryptsetup for
93  * logging purposes
94  *
95  */
96
97 /**
98  * @addtogroup loglevel
99  * @{
100  */
101
102 /** normal log level */
103 #define CRYPT_LOG_NORMAL 0
104 /** error log level */
105 #define CRYPT_LOG_ERROR  1
106 /** verbose log level */
107 #define CRYPT_LOG_VERBOSE  2
108 /** debug log level - always on stdout */
109 #define CRYPT_LOG_DEBUG -1
110
111 /**
112  * Set log function.
113  *
114  * @param cd crypt device handle (can be @e NULL to set default log function)
115  * @param log user defined log function reference
116  * @param usrptr provided identification in callback
117  * @param level log level below (debug messages can uses other levels)
118  * @param msg log message
119  */
120 void crypt_set_log_callback(struct crypt_device *cd,
121         void (*log)(int level, const char *msg, void *usrptr),
122         void *usrptr);
123
124 /**
125  * Defines log function or use the default one otherwise.
126  *
127  * @see crypt_set_log_callback
128  *
129  * @param cd crypt device handle
130  * @param level log level
131  * @param msg log message
132  */
133 void crypt_log(struct crypt_device *cd, int level, const char *msg);
134 /** @} */
135
136 /**
137  * Set confirmation callback (yes/no)
138  *
139  * If code need confirmation (like resetting uuid or restoring LUKS header from file)
140  * this function is called. If not defined, everything is confirmed.
141  *
142  * Callback function @e confirm should return @e 0 if operation is declined,
143  * other values mean accepted.
144  *
145  * @param cd crypt device handle
146  * @param confirm user defined confirm callback reference
147  * @param usrptr provided identification in callback
148  * @param msg Message for user to confirm
149  *
150  * @note Current version of cryptsetup API requires confirmation only when UUID is being changed
151  */
152 void crypt_set_confirm_callback(struct crypt_device *cd,
153         int (*confirm)(const char *msg, void *usrptr),
154         void *usrptr);
155
156 /**
157  * Set password query callback.
158  *
159  * If code need @e _interactive_ query for password, this callback is called.
160  * If not defined, compiled-in default is called (uses terminal input).
161  *
162  * Callback should return length of password in buffer
163  * or negative errno value in case of error.
164  *
165  * @param cd crypt device handle
166  * @param password user defined password callback reference
167  * @param usrptr provided identification in callback
168  * @param msg Message for user
169  * @param buf buffer for password
170  * @param length size of buffer
171  *
172  * @note Note that if this function is defined, verify option is ignored
173  *   (caller which provided callback is responsible for password verification)
174  * @note Only zero terminated passwords can be entered this way, for complex
175  *   use API functions directly.
176  * @note Maximal length of password is limited to @e length @e - @e 1 (minimal 511 chars)
177  *
178  * @see Callback function is used in these call provided, that certain conditions are met:
179  * @li crypt_keyslot_add_by_passphrase
180  * @li crypt_activate_by_passphrase
181  * @li crypt_resume_by_passphrase
182  * @li crypt_resume_by_keyfile
183  * @li crypt_keyslot_add_by_keyfile
184  * @li crypt_keyslot_add_by_volume_key
185  *
186  */
187 void crypt_set_password_callback(struct crypt_device *cd,
188         int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
189         void *usrptr);
190
191 /**
192  * Set timeout for interactive password entry using default
193  * password callback
194  *
195  * @param cd crypt device handle
196  * @param timeout_sec timeout in seconds
197  */
198 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
199
200 /**
201  * Set number of retries in case password input has been incorrect
202  *
203  * @param cd crypt device handle
204  * @param tries the number
205  */
206 void crypt_set_password_retry(struct crypt_device *cd, int tries);
207
208 /**
209  * Set how long should cryptsetup iterate in PBKDF2 function.
210  * Default value heads towards the iterations which takes around 1 second
211  *
212  * @param cd crypt device handle
213  * @param iteration_time_ms the time in ms
214  */
215 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms);
216 /* Don't ask :-) */
217 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
218
219 /**
220  * Set whether passphrase will be verified on input
221  * (user has to input same passphrase twice)
222  *
223  * @param cd crypt device handle
224  * @param password_verify @e 0 = false, @e !0 true
225  */
226 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
227
228 /**
229  * Set data device (encrypted payload area device) if LUKS header is separated
230  *
231  * @param cd crypt device handle
232  * @param device path to device
233  *
234  * @pre context is of LUKS type
235  * @pre unlike @ref crypt_init, in this function param @e device
236  *      has to be block device (at least 512B large)
237  */
238 int crypt_set_data_device(struct crypt_device *cd, const char *device);
239
240 /**
241  * @defgroup rng "Cryptsetup RNG"
242  *
243  * @addtogroup rng
244  * @{
245  *
246  */
247
248 /** CRYPT_RNG_URANDOM - use /dev/urandom */
249 #define CRYPT_RNG_URANDOM 0
250 /** CRYPT_RNG_RANDOM  - use /dev/random (waits if no entropy in system) */
251 #define CRYPT_RNG_RANDOM  1
252
253 /**
254  * Set which RNG (random number generator) is used for generating long term key
255  *
256  * @param cd crypt device handle
257  * @param rng_type kernel random number generator to use
258  *
259  */
260 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
261
262 /**
263  * Get which RNG (random number generator) is used for generating long term key
264  *
265  * @param cd crypt device handle
266  * @return RNG type on success or negative errno value otherwise.
267  *
268  */
269 int crypt_get_rng_type(struct crypt_device *cd);
270
271 /** @} */
272
273 /**
274  * Helper to lock/unlock memory to avoid swap sensitive data to disk
275  *
276  * @param cd crypt device handle, can be @e NULL
277  * @param lock 0 to unlock otherwise lock memory
278  *
279  * @returns Value indicating whether the memory is locked (function can be called multiple times).
280  *
281  * @note Only root can do this.
282  * @note It locks/unlocks all process memory, not only crypt context.
283  */
284 int crypt_memory_lock(struct crypt_device *cd, int lock);
285
286 /**
287  * @defgroup crypt_type "Cryptsetup on-disk format types"
288  *
289  * Set of functions, \#defines and structs related
290  * to on-disk format types
291  */
292
293 /**
294  * @addtogroup crypt_type
295  * @{
296  */
297
298 /** plain crypt device, no on-disk header */
299 #define CRYPT_PLAIN "PLAIN"
300 /** LUKS version 1 header on-disk */
301 #define CRYPT_LUKS1 "LUKS1"
302 /** loop-AES compatibility mode */
303 #define CRYPT_LOOPAES "LOOPAES"
304
305 /**
306  * Get device type
307  *
308  * @param cd crypt device handle
309  * @return string according to device type or @e NULL if not known.
310  */
311 const char *crypt_get_type(struct crypt_device *cd);
312
313 /**
314  *
315  * Structure used as parameter for PLAIN device type
316  *
317  * @see crypt_format
318  */
319 struct crypt_params_plain {
320         const char *hash; /**< password hash function */
321         uint64_t offset; /**< offset in sectors */
322         uint64_t skip; /**< IV offset / initialization sector */
323         uint64_t size; /**< size of mapped device or @e 0 for autodetection */
324 };
325
326 /**
327  * Structure used as parameter for LUKS device type
328  *
329  * @see crypt_format, crypt_load
330  *
331  * @note during crypt_format @e data_device attribute determines
332  *       if the LUKS header is separated from encrypted payload device
333  *
334  */
335 struct crypt_params_luks1 {
336         const char *hash; /**< hash used in LUKS header */
337         size_t data_alignment; /**< data alignment in sectors, data offset is multiple of this */
338         const char *data_device; /**< detached encrypted data device or @e NULL */
339 };
340
341 /**
342  *
343  * Structure used as parameter for loop-AES device type
344  *
345  * @see crypt_format
346  *
347  */
348 struct crypt_params_loopaes {
349         const char *hash; /**< key hash function */
350         uint64_t offset;  /**< offset in sectors */
351         uint64_t skip;    /**< IV offset / initialization sector */
352 };
353
354 /** @} */
355
356 /**
357  * Create (format) new crypt device (and possible header on-disk) but not activates it.
358  *
359  * @pre @e cd contains initialized and not formatted device context (device type must @b not be set)
360  *
361  * @param cd crypt device handle
362  * @param type type of device (optional params struct must be of this type)
363  * @param cipher (e.g. "aes")
364  * @param cipher_mode including IV specification (e.g. "xts-plain")
365  * @param uuid requested UUID or @e NULL if it should be generated
366  * @param volume_key pre-generated volume key or @e NULL if it should be generated (only for LUKS)
367  * @param volume_key_size size of volume key in bytes.
368  * @param params crypt type specific parameters (see @link crypt_type @endlink)
369  *
370  * @returns @e 0 on success or negative errno value otherwise.
371  *
372  * @note Note that crypt_format does not enable any keyslot (in case of work with LUKS device), but it stores volume key internally
373  *       and subsequent crypt_keyslot_add_* calls can be used.
374  */
375 int crypt_format(struct crypt_device *cd,
376         const char *type,
377         const char *cipher,
378         const char *cipher_mode,
379         const char *uuid,
380         const char *volume_key,
381         size_t volume_key_size,
382         void *params);
383
384 /**
385  * Set new UUID for already existing device
386  *
387  * @param cd crypt device handle
388  * @param uuid requested UUID or @e NULL if it should be generated
389  *
390  * @returns 0 on success or negative errno value otherwise.
391  *
392  * @note Currently, only LUKS device type are supported
393  */
394 int crypt_set_uuid(struct crypt_device *cd,
395                    const char *uuid);
396
397 /**
398  * Load crypt device parameters from on-disk header
399  *
400  * @param cd crypt device handle
401  * @param requested_type @link crypt_type @endlink or @e NULL for all known
402  * @param params crypt type specific parameters (see @link crypt_type @endlink)
403  *
404  * @returns 0 on success or negative errno value otherwise.
405  *
406  * @post In case LUKS header is read successfully but payload device is too small
407  * error is returned and device type in context is set to @e NULL
408  *
409  * @note Note that in current version load works only for LUKS device type
410  *
411  */
412 int crypt_load(struct crypt_device *cd,
413                const char *requested_type,
414                void *params);
415
416 /**
417  * Try to repair crypt device on-disk header if invalid
418  *
419  * @param cd crypt device handle
420  * @param requested_type @link crypt_type @endlink or @e NULL for all known
421  * @param params crypt type specific parameters (see @link crypt_type @endlink)
422  *
423  * @returns 0 on success or negative errno value otherwise.
424  *
425  */
426 int crypt_repair(struct crypt_device *cd,
427                  const char *requested_type,
428                  void *params);
429
430 /**
431  * Resize crypt device
432  *
433  * @param cd - crypt device handle
434  * @param name - name of device to resize
435  * @param new_size - new device size in sectors or @e 0 to use all of the underlying device size
436  *
437  * @return @e 0 on success or negative errno value otherwise.
438  */
439 int crypt_resize(struct crypt_device *cd,
440                  const char *name,
441                  uint64_t new_size);
442
443 /**
444  * Suspends crypt device.
445  *
446  * @param cd crypt device handle, can be @e NULL
447  * @param name name of device to suspend
448  *
449  * @return 0 on success or negative errno value otherwise.
450  *
451  * @note Only LUKS device type is supported
452  *
453  */
454 int crypt_suspend(struct crypt_device *cd,
455                   const char *name);
456
457 /**
458  * Resumes crypt device using passphrase.
459  *
460  *
461  * @param cd crypt device handle
462  * @param name name of device to resume
463  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
464  * @param passphrase passphrase used to unlock volume key, @e NULL for query
465  * @param passphrase_size size of @e passphrase (binary data)
466  *
467  * @return unlocked key slot number or negative errno otherwise.
468  *
469  * @note Only LUKS device type is supported
470  */
471 int crypt_resume_by_passphrase(struct crypt_device *cd,
472         const char *name,
473         int keyslot,
474         const char *passphrase,
475         size_t passphrase_size);
476
477 /**
478  * Resumes crypt device using key file.
479  *
480  * @param cd crypt device handle
481  * @param name name of device to resume
482  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
483  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
484  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
485  * @param keyfile_offset number of bytes to skip at start of keyfile
486  *
487  * @return unlocked key slot number or negative errno otherwise.
488  */
489 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
490         const char *name,
491         int keyslot,
492         const char *keyfile,
493         size_t keyfile_size,
494         size_t keyfile_offset);
495 /**
496  * Backward compatible crypt_resume_by_keyfile_offset() (without offset).
497  */
498 int crypt_resume_by_keyfile(struct crypt_device *cd,
499         const char *name,
500         int keyslot,
501         const char *keyfile,
502         size_t keyfile_size);
503
504 /**
505  * Releases crypt device context and used memory.
506  *
507  * @param cd crypt device handle
508  */
509 void crypt_free(struct crypt_device *cd);
510
511 /**
512  * @defgroup keyslot "Cryptsetup LUKS keyslots"
513  * @addtogroup keyslot
514  * @{
515  *
516  */
517
518 /** iterate through all keyslots and find first one that fits */
519 #define CRYPT_ANY_SLOT -1
520
521 /**
522  * Add key slot using provided passphrase
523  *
524  * @pre @e cd contains initialized and formatted LUKS device context
525  *
526  * @param cd crypt device handle
527  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
528  * @param passphrase passphrase used to unlock volume key, @e NULL for query
529  * @param passphrase_size size of passphrase (binary data)
530  * @param new_passphrase passphrase for new keyslot, @e NULL for query
531  * @param new_passphrase_size size of @e new_passphrase (binary data)
532  *
533  * @return allocated key slot number or negative errno otherwise.
534  */
535 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
536         int keyslot,
537         const char *passphrase,
538         size_t passphrase_size,
539         const char *new_passphrase,
540         size_t new_passphrase_size);
541
542 /**
543  * Get number of keyslots supported for device type.
544  *
545  * @param type crypt device type
546  *
547  * @return slot count or negative errno otherwise if device
548  * doesn't not support keyslots.
549  */
550 int crypt_keyslot_max(const char *type);
551
552 /**
553 * Add key slot using provided key file path
554  *
555  * @pre @e cd contains initialized and formatted LUKS device context
556  *
557  * @param cd crypt device handle
558  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
559  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
560  * @param keyfile_size number of bytes to read from keyfile, @e 0 is unlimited
561  * @param keyfile_offset number of bytes to skip at start of keyfile
562  * @param new_keyfile keyfile for new keyslot, @e NULL for passphrase query
563  * @param new_keyfile_size number of bytes to read from @e new_keyfile, @e 0 is unlimited
564  * @param new_keyfile_offset number of bytes to skip at start of new_keyfile
565  *
566  * @return allocated key slot number or negative errno otherwise.
567  *
568  * @note Note that @e keyfile can be "-" for STDIN
569  *
570  */
571 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
572         int keyslot,
573         const char *keyfile,
574         size_t keyfile_size,
575         size_t keyfile_offset,
576         const char *new_keyfile,
577         size_t new_keyfile_size,
578         size_t new_keyfile_offset);
579 /**
580  * Backward compatible crypt_keyslot_add_by_keyfile_offset() (without offset).
581  */
582 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
583         int keyslot,
584         const char *keyfile,
585         size_t keyfile_size,
586         const char *new_keyfile,
587         size_t new_keyfile_size);
588
589 /**
590  * Add key slot using provided volume key
591  *
592  * @pre @e cd contains initialized and formatted LUKS device context
593  *
594  * @param cd crypt device handle
595  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
596  * @param volume_key provided volume key or @e NULL if used after crypt_format
597  * @param volume_key_size size of volume_key
598  * @param passphrase passphrase for new keyslot, @e NULL for query
599  * @param passphrase_size size of passphrase
600  *
601  * @return allocated key slot number or negative errno otherwise.
602  *
603  */
604 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
605         int keyslot,
606         const char *volume_key,
607         size_t volume_key_size,
608         const char *passphrase,
609         size_t passphrase_size);
610
611 /**
612  * Destroy (and disable) key slot
613  *
614  * @pre @e cd contains initialized and formatted LUKS device context
615  *
616  * @param cd crypt device handle
617  * @param keyslot requested key slot to destroy
618  *
619  * @return @e 0 on success or negative errno value otherwise.
620  *
621  * @note Note that there is no passphrase verification used.
622  */
623 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
624
625 /** @} */
626
627 /**
628  * @defgroup aflags "Device runtime attributes"
629  *
630  * Activation flags
631  *
632  * @addtogroup aflags
633  * @{
634  *
635  */
636 /** device is read only */
637 #define CRYPT_ACTIVATE_READONLY (1 << 0)
638 /** only reported for device without uuid */
639 #define CRYPT_ACTIVATE_NO_UUID  (1 << 1)
640 /** activate even if cannot grant exclusive access (DANGEROUS) */
641 #define CRYPT_ACTIVATE_SHARED   (1 << 2)
642 /** enable discards aka TRIM */
643 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3)
644 /** skip global udev rules in activation ("private device"), input only */
645 #define CRYPT_ACTIVATE_PRIVATE (1 << 4)
646
647 /**
648  * Active device runtime attributes
649  */
650 struct crypt_active_device {
651         uint64_t offset; /**< offset in sectors */
652         uint64_t iv_offset; /**< IV initialization sector */
653         uint64_t size; /**< active device size */
654         uint32_t flags; /**< activation flags */
655 };
656
657 /**
658  * Receives runtime attributes of active crypt device
659  *
660  * @param cd crypt device handle (can be @e NULL)
661  * @param name name of active device
662  * @param cad preallocated active device attributes to fill
663  *
664  * @return @e 0 on success or negative errno value otherwise
665  *
666  */
667 int crypt_get_active_device(struct crypt_device *cd,
668                             const char *name,
669                             struct crypt_active_device *cad);
670
671 /** @} */
672
673 /**
674  * Activate device or check passphrase
675  *
676  * @param cd crypt device handle
677  * @param name name of device to create, if @e NULL only check passphrase
678  * @param keyslot requested keyslot to check or @e CRYPT_ANY_SLOT
679  * @param passphrase passphrase used to unlock volume key, @e NULL for query
680  * @param passphrase_size size of @e passphrase
681  * @param flags activation flags
682  *
683  * @return unlocked key slot number or negative errno otherwise.
684  */
685 int crypt_activate_by_passphrase(struct crypt_device *cd,
686         const char *name,
687         int keyslot,
688         const char *passphrase,
689         size_t passphrase_size,
690         uint32_t flags);
691
692 /**
693  * Activate device or check using key file
694  *
695  * @param cd crypt device handle
696  * @param name name of device to create, if @e NULL only check keyfile
697  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
698  * @param keyfile key file used to unlock volume key
699  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
700  * @param keyfile_offset number of bytes to skip at start of keyfile
701  * @param flags activation flags
702  *
703  * @return unlocked key slot number or negative errno otherwise.
704  */
705 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
706         const char *name,
707         int keyslot,
708         const char *keyfile,
709         size_t keyfile_size,
710         size_t keyfile_offset,
711         uint32_t flags);
712 /**
713  * Backward compatible crypt_activate_by_keyfile_offset() (without offset).
714  */
715 int crypt_activate_by_keyfile(struct crypt_device *cd,
716         const char *name,
717         int keyslot,
718         const char *keyfile,
719         size_t keyfile_size,
720         uint32_t flags);
721
722 /**
723  * Activate device using provided volume key
724  *
725  *
726  * @param cd crypt device handle
727  * @param name name of device to create, if @e NULL only check volume key
728  * @param volume_key provided volume key (or @e NULL to use internal)
729  * @param volume_key_size size of volume_key
730  * @param flags activation flags
731  *
732  * @return @e 0 on success or negative errno value otherwise.
733  *
734  * @note If @e NULL is used for volume_key, device has to be initialized
735  *       by previous operation (like @ref crypt_format
736  *       or @ref crypt_init_by_name)
737  */
738 int crypt_activate_by_volume_key(struct crypt_device *cd,
739         const char *name,
740         const char *volume_key,
741         size_t volume_key_size,
742         uint32_t flags);
743
744 /**
745  * Deactivate crypt device. This function tries to remove active device-mapper
746  * mapping from kernel. Also, sensitive data like the volume key are removed from
747  * memory
748  *
749  * @param cd crypt device handle, can be @e NULL
750  * @param name name of device to deactivate
751  *
752  * @return @e 0 on success or negative errno value otherwise.
753  *
754  */
755 int crypt_deactivate(struct crypt_device *cd, const char *name);
756
757 /**
758  * Get volume key from of crypt device
759  *
760  * @param cd crypt device handle
761  * @param keyslot use this keyslot or @e CRYPT_ANY_SLOT
762  * @param volume_key buffer for volume key
763  * @param volume_key_size on input, size of buffer @e volume_key,
764  *        on output size of @e volume_key
765  * @param passphrase passphrase used to unlock volume key
766  * @param passphrase_size size of @e passphrase
767  *
768  * @return unlocked key slot number or negative errno otherwise.
769  */
770 int crypt_volume_key_get(struct crypt_device *cd,
771         int keyslot,
772         char *volume_key,
773         size_t *volume_key_size,
774         const char *passphrase,
775         size_t passphrase_size);
776
777 /**
778  * Verify that provided volume key is valid for crypt device
779  *
780  * @param cd crypt device handle
781  * @param volume_key provided volume key
782  * @param volume_key_size size of @e volume_key
783  *
784  * @return @e 0 on success or negative errno value otherwise.
785  */
786 int crypt_volume_key_verify(struct crypt_device *cd,
787         const char *volume_key,
788         size_t volume_key_size);
789
790
791 /*
792  * @defgroup devstat "dmcrypt device status"
793  * @addtogroup devstat
794  * @{
795  */
796
797 /**
798  * Device status
799  */
800 typedef enum {
801         CRYPT_INVALID, /**< device mapping is invalid in this context */
802         CRYPT_INACTIVE, /**< no such mapped device */
803         CRYPT_ACTIVE, /**< device is active */
804         CRYPT_BUSY /**< device is active and has open count > 0 */
805 } crypt_status_info;
806
807 /**
808  * Get status info about device name
809  *
810  * @param cd crypt device handle, can be @e NULL
811  * @param name crypt device name
812  *
813  * @return value defined by crypt_status_info.
814  *
815  */
816 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
817
818 /**
819  * Dump text-formatted information about crypt device to log output
820  *
821  * @param cd crypt device handle
822  *
823  * @return @e 0 on success or negative errno value otherwise.
824  */
825 int crypt_dump(struct crypt_device *cd);
826
827 /**
828  * Get cipher used in device
829  *
830  * @param cd crypt device handle
831  *
832  * @return used cipher, e.g. "aes" or @e NULL otherwise
833  *
834  */
835 const char *crypt_get_cipher(struct crypt_device *cd);
836
837 /**
838  * Get cipher mode used in device
839  *
840  * @param cd crypt device handle
841  *
842  * @return used cipher mode e.g. "xts-plain" or @e otherwise
843  *
844  */
845 const char *crypt_get_cipher_mode(struct crypt_device *cd);
846
847 /**
848  * Get device UUID
849  *
850  * @param cd crypt device handle
851  *
852  * @return device UUID or @e NULL if not set
853  *
854  */
855 const char *crypt_get_uuid(struct crypt_device *cd);
856
857 /**
858  * Get path to underlaying device
859  *
860  * @param cd crypt device handle
861  *
862  * @return path to underlaying device name
863  *
864  */
865 const char *crypt_get_device_name(struct crypt_device *cd);
866
867 /**
868  * Get device offset in sectors where real data starts on underlying device)
869  *
870  * @param cd crypt device handle
871  *
872  * @return device offset in sectors
873  *
874  */
875 uint64_t crypt_get_data_offset(struct crypt_device *cd);
876
877 /**
878  * Get IV offset in sectors (skip)
879  *
880  * @param cd crypt device handle
881  *
882  * @return IV offset
883  *
884  */
885 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
886
887 /**
888  * Get size (in bytes) of volume key for crypt device
889  *
890  * @param cd crypt device handle
891  *
892  * @return volume key size
893  *
894  */
895 int crypt_get_volume_key_size(struct crypt_device *cd);
896
897 /**
898  * @addtogroup keyslot
899  * @{
900  *
901  */
902
903 /**
904  * Crypt keyslot info
905  */
906 typedef enum {
907         CRYPT_SLOT_INVALID, /**< invalid keyslot */
908         CRYPT_SLOT_INACTIVE, /**< keyslot is inactive (free) */
909         CRYPT_SLOT_ACTIVE, /**< keyslot is active (used) */
910         CRYPT_SLOT_ACTIVE_LAST /**< keylost is active (used)
911                                 *   and last used at the same time */
912 } crypt_keyslot_info;
913
914 /**
915  * Get information about particular key slot
916  *
917  *
918  * @param cd crypt device handle
919  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
920  *
921  * @return value defined by crypt_keyslot_info
922  *
923  */
924 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
925 /** @} */
926
927 /**
928  * Backup header and keyslots to file
929  *
930  * @param cd crypt device handle
931  * @param requested_type @link crypt_type @endlink or @e NULL for all known
932  * @param backup_file file to backup header to
933  *
934  * @return @e 0 on success or negative errno value otherwise.
935  *
936  */
937 int crypt_header_backup(struct crypt_device *cd,
938         const char *requested_type,
939         const char *backup_file);
940
941 /**
942  * Restore header and keyslots from backup file
943  *
944  *
945  * @param cd crypt device handle
946  * @param requested_type @link crypt_type @endlink or @e NULL for all known
947  * @param backup_file file to restore header from
948  *
949  * @return @e 0 on success or negative errno value otherwise.
950  *
951  */
952 int crypt_header_restore(struct crypt_device *cd,
953         const char *requested_type,
954         const char *backup_file);
955
956 /**
957  * Receives last reported error
958  *
959  * @param cd crypt device handle
960  * @param buf buffef for message
961  * @param size size of buffer
962  *
963  * @note Note that this is old API function using global context.
964  * All error messages are reported also through log callback.
965  */
966 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size);
967
968 /**
969  * Receives last reported error, DEPRECATED
970  *
971  * @param buf buffef for message
972  * @param size size of buffer
973  *
974  * @note Note that this is old API function using global context.
975  * All error messages are reported also through log callback.
976  */
977 void crypt_get_error(char *buf, size_t size);
978
979 /**
980  * Get directory where mapped crypt devices are created
981  *
982  * @return the directory path
983  */
984 const char *crypt_get_dir(void);
985
986 /**
987  * @defgroup dbg "Library debug level"
988  *
989  * Set library debug level
990  *
991  * @addtogroup dbg
992  * @{
993  */
994
995 /** Debug all */
996 #define CRYPT_DEBUG_ALL  -1
997 /** Debug none */
998 #define CRYPT_DEBUG_NONE  0
999
1000 /**
1001  * Set the debug level for library
1002  *
1003  * @param level debug level
1004  *
1005  */
1006 void crypt_set_debug_level(int level);
1007
1008 /** @} */
1009
1010 #ifdef __cplusplus
1011 }
1012 #endif
1013 #endif /* _LIBCRYPTSETUP_H */