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