2 * libcryptsetup - cryptsetup library
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.
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.
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.
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.
23 * @file libcryptsetup.h
24 * @brief Public cryptsetup API
26 * For more verbose examples of LUKS related use cases,
27 * please read @ref index "examples".
30 #ifndef _LIBCRYPTSETUP_H
31 #define _LIBCRYPTSETUP_H
39 struct crypt_device; /* crypt device handle */
42 * Initialize crypt device handle and check if provided device exists.
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.
51 * @return @e 0 on success or negative errno value otherwise.
53 * @note Note that logging is not initialized here, possible messages uses
54 * default log function.
56 int crypt_init(struct crypt_device **cd, const char *device);
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.
63 * @return @e 0 on success or negative errno value otherwise.
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)
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
74 * @note @link crypt_init_by_name @endlink is equivalent to calling
75 * crypt_init_by_name_and_header(cd, name, NULL);
77 int crypt_init_by_name_and_header(struct crypt_device **cd,
79 const char *header_device);
82 * This is equivalent to call
83 * @ref crypt_init_by_name_and_header "crypt_init_by_name_and_header(cd, name, NULL)"
85 * @sa crypt_init_by_name_and_header
87 int crypt_init_by_name(struct crypt_device **cd, const char *name);
90 * @defgroup loglevel "Cryptsetup logging"
92 * Set of functions and defines used in cryptsetup for
98 * @addtogroup loglevel
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
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
120 void crypt_set_log_callback(struct crypt_device *cd,
121 void (*log)(int level, const char *msg, void *usrptr),
125 * Defines log function or use the default one otherwise.
127 * @see crypt_set_log_callback
129 * @param cd crypt device handle
130 * @param level log level
131 * @param msg log message
133 void crypt_log(struct crypt_device *cd, int level, const char *msg);
137 * Set confirmation callback (yes/no)
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.
142 * Callback function @e confirm should return @e 0 if operation is declined,
143 * other values mean accepted.
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
150 * @note Current version of cryptsetup API requires confirmation only when UUID is being changed
152 void crypt_set_confirm_callback(struct crypt_device *cd,
153 int (*confirm)(const char *msg, void *usrptr),
157 * Set password query callback.
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).
162 * Callback should return length of password in buffer
163 * or negative errno value in case of error.
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
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)
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
187 void crypt_set_password_callback(struct crypt_device *cd,
188 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
192 * Set timeout for interactive password entry using default
195 * @param cd crypt device handle
196 * @param timeout_sec timeout in seconds
198 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
201 * Set number of retries in case password input has been incorrect
203 * @param cd crypt device handle
204 * @param tries the number
206 void crypt_set_password_retry(struct crypt_device *cd, int tries);
209 * Set how long should cryptsetup iterate in PBKDF2 function.
210 * Default value heads towards the iterations which takes around 1 second
212 * @param cd crypt device handle
213 * @param iteration_time_ms the time in ms
215 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms);
217 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
220 * Set whether passphrase will be verified on input
221 * (user has to input same passphrase twice)
223 * @param cd crypt device handle
224 * @param password_verify @e 0 = false, @e !0 true
226 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
230 * For LUKS it is encrypted data device when LUKS header is separated.
231 * For VERITY it is data device when hash device is separated.
233 * @param cd crypt device handle
234 * @param device path to device
237 int crypt_set_data_device(struct crypt_device *cd, const char *device);
240 * @defgroup rng "Cryptsetup RNG"
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
253 * Set which RNG (random number generator) is used for generating long term key
255 * @param cd crypt device handle
256 * @param rng_type kernel random number generator to use
259 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
262 * Get which RNG (random number generator) is used for generating long term key
264 * @param cd crypt device handle
265 * @return RNG type on success or negative errno value otherwise.
268 int crypt_get_rng_type(struct crypt_device *cd);
273 * Helper to lock/unlock memory to avoid swap sensitive data to disk
275 * @param cd crypt device handle, can be @e NULL
276 * @param lock 0 to unlock otherwise lock memory
278 * @returns Value indicating whether the memory is locked (function can be called multiple times).
280 * @note Only root can do this.
281 * @note It locks/unlocks all process memory, not only crypt context.
283 int crypt_memory_lock(struct crypt_device *cd, int lock);
286 * @defgroup crypt_type "Cryptsetup on-disk format types"
288 * Set of functions, \#defines and structs related
289 * to on-disk format types
293 * @addtogroup crypt_type
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 /** dm-verity mode */
304 #define CRYPT_VERITY "VERITY"
305 /** TCRYPT (TrueCrypt-compatible) mode */
306 #define CRYPT_TCRYPT "TCRYPT"
311 * @param cd crypt device handle
312 * @return string according to device type or @e NULL if not known.
314 const char *crypt_get_type(struct crypt_device *cd);
318 * Structure used as parameter for PLAIN device type
322 struct crypt_params_plain {
323 const char *hash; /**< password hash function */
324 uint64_t offset; /**< offset in sectors */
325 uint64_t skip; /**< IV offset / initialization sector */
326 uint64_t size; /**< size of mapped device or @e 0 for autodetection */
330 * Structure used as parameter for LUKS device type
332 * @see crypt_format, crypt_load
334 * @note during crypt_format @e data_device attribute determines
335 * if the LUKS header is separated from encrypted payload device
338 struct crypt_params_luks1 {
339 const char *hash; /**< hash used in LUKS header */
340 size_t data_alignment; /**< data alignment in sectors, data offset is multiple of this */
341 const char *data_device; /**< detached encrypted data device or @e NULL */
346 * Structure used as parameter for loop-AES device type
351 struct crypt_params_loopaes {
352 const char *hash; /**< key hash function */
353 uint64_t offset; /**< offset in sectors */
354 uint64_t skip; /**< IV offset / initialization sector */
359 * Structure used as parameter for dm-verity device type
361 * @see crypt_format, crypt_load
364 /** No on-disk header (only hashes) */
365 #define CRYPT_VERITY_NO_HEADER (1 << 0)
366 /** Verity hash in userspace before activation */
367 #define CRYPT_VERITY_CHECK_HASH (1 << 1)
368 /** Create hash - format hash device */
369 #define CRYPT_VERITY_CREATE_HASH (1 << 2)
371 struct crypt_params_verity {
372 const char *hash_name; /**< hash function */
373 const char *data_device; /**< data_device (CRYPT_VERITY_CREATE_HASH) */
374 const char *hash_device; /**< hash_device (output only) */
375 const char *salt; /**< salt */
376 uint32_t salt_size; /**< salt size (in bytes) */
377 uint32_t hash_type; /**< in-kernel hashing type */
378 uint32_t data_block_size; /**< data block size (in bytes) */
379 uint32_t hash_block_size; /**< hash block size (in bytes) */
380 uint64_t data_size; /**< data area size (in data blocks) */
381 uint64_t hash_area_offset; /**< hash/header offset (in bytes) */
382 uint32_t flags; /**< CRYPT_VERITY* flags */
387 * Structure used as parameter for TCRYPT device type
392 /** Include legacy modes ehn scannig for header*/
393 #define CRYPT_TCRYPT_LEGACY_MODES (1 << 0)
394 /** Try to load hidden header (describing hidden device) */
395 #define CRYPT_TCRYPT_HIDDEN_HEADER (1 << 1)
396 /** Try to load backup header */
397 #define CRYPT_TCRYPT_BACKUP_HEADER (1 << 2)
399 struct crypt_params_tcrypt {
400 const char *passphrase; /**< passphrase to unlock header (input only) */
401 size_t passphrase_size; /**< passphrase size (input only) */
402 const char **keyfiles; /**< keyfile paths to unlock header (input only) */
403 unsigned int keyfiles_count;/**< keyfiles count (input only) */
404 const char *hash_name; /**< hash function for PBKDF */
405 const char *cipher; /**< cipher chain c1[-c2[-c3]] */
406 const char *mode; /**< cipher block mode */
407 size_t key_size; /**< key size in bytes (the whole chain) */
408 uint32_t flags; /**< CRYPT_TCRYPT* flags */
414 * Create (format) new crypt device (and possible header on-disk) but not activates it.
416 * @pre @e cd contains initialized and not formatted device context (device type must @b not be set)
418 * @param cd crypt device handle
419 * @param type type of device (optional params struct must be of this type)
420 * @param cipher (e.g. "aes")
421 * @param cipher_mode including IV specification (e.g. "xts-plain")
422 * @param uuid requested UUID or @e NULL if it should be generated
423 * @param volume_key pre-generated volume key or @e NULL if it should be generated (only for LUKS)
424 * @param volume_key_size size of volume key in bytes.
425 * @param params crypt type specific parameters (see @link crypt_type @endlink)
427 * @returns @e 0 on success or negative errno value otherwise.
429 * @note Note that crypt_format does not enable any keyslot (in case of work with LUKS device),
430 * but it stores volume key internally and subsequent crypt_keyslot_add_* calls can be used.
431 * @note For VERITY @link crypt_type @endlink, only uuid parameter is used, others paramaters
432 * are ignored and verity specific attributes are set through mandatory params option.
434 int crypt_format(struct crypt_device *cd,
437 const char *cipher_mode,
439 const char *volume_key,
440 size_t volume_key_size,
444 * Set new UUID for already existing device
446 * @param cd crypt device handle
447 * @param uuid requested UUID or @e NULL if it should be generated
449 * @returns 0 on success or negative errno value otherwise.
451 * @note Currently, only LUKS device type are supported
453 int crypt_set_uuid(struct crypt_device *cd,
457 * Load crypt device parameters from on-disk header
459 * @param cd crypt device handle
460 * @param requested_type @link crypt_type @endlink or @e NULL for all known
461 * @param params crypt type specific parameters (see @link crypt_type @endlink)
463 * @returns 0 on success or negative errno value otherwise.
465 * @post In case LUKS header is read successfully but payload device is too small
466 * error is returned and device type in context is set to @e NULL
468 * @note Note that in current version load works only for LUKS and VERITY device type.
471 int crypt_load(struct crypt_device *cd,
472 const char *requested_type,
476 * Try to repair crypt device on-disk header if invalid
478 * @param cd crypt device handle
479 * @param requested_type @link crypt_type @endlink or @e NULL for all known
480 * @param params crypt type specific parameters (see @link crypt_type @endlink)
482 * @returns 0 on success or negative errno value otherwise.
485 int crypt_repair(struct crypt_device *cd,
486 const char *requested_type,
490 * Resize crypt device
492 * @param cd - crypt device handle
493 * @param name - name of device to resize
494 * @param new_size - new device size in sectors or @e 0 to use all of the underlying device size
496 * @return @e 0 on success or negative errno value otherwise.
498 int crypt_resize(struct crypt_device *cd,
503 * Suspends crypt device.
505 * @param cd crypt device handle, can be @e NULL
506 * @param name name of device to suspend
508 * @return 0 on success or negative errno value otherwise.
510 * @note Only LUKS device type is supported
513 int crypt_suspend(struct crypt_device *cd,
517 * Resumes crypt device using passphrase.
520 * @param cd crypt device handle
521 * @param name name of device to resume
522 * @param keyslot requested keyslot or CRYPT_ANY_SLOT
523 * @param passphrase passphrase used to unlock volume key, @e NULL for query
524 * @param passphrase_size size of @e passphrase (binary data)
526 * @return unlocked key slot number or negative errno otherwise.
528 * @note Only LUKS device type is supported
530 int crypt_resume_by_passphrase(struct crypt_device *cd,
533 const char *passphrase,
534 size_t passphrase_size);
537 * Resumes crypt device using key file.
539 * @param cd crypt device handle
540 * @param name name of device to resume
541 * @param keyslot requested keyslot or CRYPT_ANY_SLOT
542 * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
543 * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
544 * @param keyfile_offset number of bytes to skip at start of keyfile
546 * @return unlocked key slot number or negative errno otherwise.
548 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
553 size_t keyfile_offset);
555 * Backward compatible crypt_resume_by_keyfile_offset() (without offset).
557 int crypt_resume_by_keyfile(struct crypt_device *cd,
561 size_t keyfile_size);
564 * Releases crypt device context and used memory.
566 * @param cd crypt device handle
568 void crypt_free(struct crypt_device *cd);
571 * @defgroup keyslot "Cryptsetup LUKS keyslots"
572 * @addtogroup keyslot
577 /** iterate through all keyslots and find first one that fits */
578 #define CRYPT_ANY_SLOT -1
581 * Add key slot using provided passphrase
583 * @pre @e cd contains initialized and formatted LUKS device context
585 * @param cd crypt device handle
586 * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
587 * @param passphrase passphrase used to unlock volume key, @e NULL for query
588 * @param passphrase_size size of passphrase (binary data)
589 * @param new_passphrase passphrase for new keyslot, @e NULL for query
590 * @param new_passphrase_size size of @e new_passphrase (binary data)
592 * @return allocated key slot number or negative errno otherwise.
594 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
596 const char *passphrase,
597 size_t passphrase_size,
598 const char *new_passphrase,
599 size_t new_passphrase_size);
602 * Change defined key slot using provided passphrase
604 * @pre @e cd contains initialized and formatted LUKS device context
606 * @param cd crypt device handle
607 * @param keyslot_old old keyslot or @e CRYPT_ANY_SLOT
608 * @param keyslot_new new keyslot (can be the same as old)
609 * @param passphrase passphrase used to unlock volume key, @e NULL for query
610 * @param passphrase_size size of passphrase (binary data)
611 * @param new_passphrase passphrase for new keyslot, @e NULL for query
612 * @param new_passphrase_size size of @e new_passphrase (binary data)
614 * @return allocated key slot number or negative errno otherwise.
616 * @note This function is just internal implementation of luksChange
617 * command to avoid reading of volume key outside libcryptsetup boundary
620 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
623 const char *passphrase,
624 size_t passphrase_size,
625 const char *new_passphrase,
626 size_t new_passphrase_size);
629 * Add key slot using provided key file path
631 * @pre @e cd contains initialized and formatted LUKS device context
633 * @param cd crypt device handle
634 * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
635 * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
636 * @param keyfile_size number of bytes to read from keyfile, @e 0 is unlimited
637 * @param keyfile_offset number of bytes to skip at start of keyfile
638 * @param new_keyfile keyfile for new keyslot, @e NULL for passphrase query
639 * @param new_keyfile_size number of bytes to read from @e new_keyfile, @e 0 is unlimited
640 * @param new_keyfile_offset number of bytes to skip at start of new_keyfile
642 * @return allocated key slot number or negative errno otherwise.
644 * @note Note that @e keyfile can be "-" for STDIN
647 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
651 size_t keyfile_offset,
652 const char *new_keyfile,
653 size_t new_keyfile_size,
654 size_t new_keyfile_offset);
656 * Backward compatible crypt_keyslot_add_by_keyfile_offset() (without offset).
658 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
662 const char *new_keyfile,
663 size_t new_keyfile_size);
666 * Add key slot using provided volume key
668 * @pre @e cd contains initialized and formatted LUKS device context
670 * @param cd crypt device handle
671 * @param keyslot requested keyslot or CRYPT_ANY_SLOT
672 * @param volume_key provided volume key or @e NULL if used after crypt_format
673 * @param volume_key_size size of volume_key
674 * @param passphrase passphrase for new keyslot, @e NULL for query
675 * @param passphrase_size size of passphrase
677 * @return allocated key slot number or negative errno otherwise.
680 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
682 const char *volume_key,
683 size_t volume_key_size,
684 const char *passphrase,
685 size_t passphrase_size);
688 * Destroy (and disable) key slot
690 * @pre @e cd contains initialized and formatted LUKS device context
692 * @param cd crypt device handle
693 * @param keyslot requested key slot to destroy
695 * @return @e 0 on success or negative errno value otherwise.
697 * @note Note that there is no passphrase verification used.
699 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
704 * @defgroup aflags "Device runtime attributes"
712 /** device is read only */
713 #define CRYPT_ACTIVATE_READONLY (1 << 0)
714 /** only reported for device without uuid */
715 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
716 /** activate even if cannot grant exclusive access (DANGEROUS) */
717 #define CRYPT_ACTIVATE_SHARED (1 << 2)
718 /** enable discards aka TRIM */
719 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3)
720 /** skip global udev rules in activation ("private device"), input only */
721 #define CRYPT_ACTIVATE_PRIVATE (1 << 4)
722 /** corruption detected (verity), output only */
723 #define CRYPT_ACTIVATE_CORRUPTED (1 << 5)
726 * Active device runtime attributes
728 struct crypt_active_device {
729 uint64_t offset; /**< offset in sectors */
730 uint64_t iv_offset; /**< IV initialization sector */
731 uint64_t size; /**< active device size */
732 uint32_t flags; /**< activation flags */
736 * Receives runtime attributes of active crypt device
738 * @param cd crypt device handle (can be @e NULL)
739 * @param name name of active device
740 * @param cad preallocated active device attributes to fill
742 * @return @e 0 on success or negative errno value otherwise
745 int crypt_get_active_device(struct crypt_device *cd,
747 struct crypt_active_device *cad);
752 * Activate device or check passphrase
754 * @param cd crypt device handle
755 * @param name name of device to create, if @e NULL only check passphrase
756 * @param keyslot requested keyslot to check or @e CRYPT_ANY_SLOT
757 * @param passphrase passphrase used to unlock volume key, @e NULL for query
758 * @param passphrase_size size of @e passphrase
759 * @param flags activation flags
761 * @return unlocked key slot number or negative errno otherwise.
763 int crypt_activate_by_passphrase(struct crypt_device *cd,
766 const char *passphrase,
767 size_t passphrase_size,
771 * Activate device or check using key file
773 * @param cd crypt device handle
774 * @param name name of device to create, if @e NULL only check keyfile
775 * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
776 * @param keyfile key file used to unlock volume key
777 * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
778 * @param keyfile_offset number of bytes to skip at start of keyfile
779 * @param flags activation flags
781 * @return unlocked key slot number or negative errno otherwise.
783 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
788 size_t keyfile_offset,
791 * Backward compatible crypt_activate_by_keyfile_offset() (without offset).
793 int crypt_activate_by_keyfile(struct crypt_device *cd,
801 * Activate device using provided volume key
804 * @param cd crypt device handle
805 * @param name name of device to create, if @e NULL only check volume key
806 * @param volume_key provided volume key (or @e NULL to use internal)
807 * @param volume_key_size size of volume_key
808 * @param flags activation flags
810 * @return @e 0 on success or negative errno value otherwise.
812 * @note If @e NULL is used for volume_key, device has to be initialized
813 * by previous operation (like @ref crypt_format
814 * or @ref crypt_init_by_name)
815 * @note For VERITY the volume key means root hash required for activation.
816 * Because kernel dm-verity is always read only, you have to provide
817 * CRYPT_ACTIVATE_READONLY flag always.
818 * @note For TCRYPT the volume key should be always NULL and because master
819 * key from decrypted header is used instead.
821 int crypt_activate_by_volume_key(struct crypt_device *cd,
823 const char *volume_key,
824 size_t volume_key_size,
828 * Deactivate crypt device. This function tries to remove active device-mapper
829 * mapping from kernel. Also, sensitive data like the volume key are removed from
832 * @param cd crypt device handle, can be @e NULL
833 * @param name name of device to deactivate
835 * @return @e 0 on success or negative errno value otherwise.
838 int crypt_deactivate(struct crypt_device *cd, const char *name);
841 * Get volume key from of crypt device
843 * @param cd crypt device handle
844 * @param keyslot use this keyslot or @e CRYPT_ANY_SLOT
845 * @param volume_key buffer for volume key
846 * @param volume_key_size on input, size of buffer @e volume_key,
847 * on output size of @e volume_key
848 * @param passphrase passphrase used to unlock volume key
849 * @param passphrase_size size of @e passphrase
851 * @return unlocked key slot number or negative errno otherwise.
853 * @note For TCRYPT cipher chain is the volume key concatenated
854 * for all ciphers in chain.
856 int crypt_volume_key_get(struct crypt_device *cd,
859 size_t *volume_key_size,
860 const char *passphrase,
861 size_t passphrase_size);
864 * Verify that provided volume key is valid for crypt device
866 * @param cd crypt device handle
867 * @param volume_key provided volume key
868 * @param volume_key_size size of @e volume_key
870 * @return @e 0 on success or negative errno value otherwise.
872 int crypt_volume_key_verify(struct crypt_device *cd,
873 const char *volume_key,
874 size_t volume_key_size);
877 * @defgroup devstat "Crypt and Verity device status"
878 * @addtogroup devstat
886 CRYPT_INVALID, /**< device mapping is invalid in this context */
887 CRYPT_INACTIVE, /**< no such mapped device */
888 CRYPT_ACTIVE, /**< device is active */
889 CRYPT_BUSY /**< device is active and has open count > 0 */
893 * Get status info about device name
895 * @param cd crypt device handle, can be @e NULL
896 * @param name crypt device name
898 * @return value defined by crypt_status_info.
901 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
904 * Dump text-formatted information about crypt or verity device to log output
906 * @param cd crypt device handle
908 * @return @e 0 on success or negative errno value otherwise.
910 int crypt_dump(struct crypt_device *cd);
913 * Informational benchmark for ciphers
915 * @param cd crypt device handle
916 * @param cipher (e.g. "aes")
917 * @param cipher_mode (e.g. "xts"), IV generator is ignored
918 * @param volume_key_size size of volume key in bytes
919 * @param iv_size size of IV in bytes
920 * @param buffer_size size of encryption buffer in bytes used in test
921 * @param encryption_mbs measured encryption speed in MiB/s
922 * @param decryption_mbs measured decryption speed in MiB/s
924 * @return @e 0 on success or negative errno value otherwise.
926 int crypt_benchmark(struct crypt_device *cd,
928 const char *cipher_mode,
929 size_t volume_key_size,
932 double *encryption_mbs,
933 double *decryption_mbs);
935 int crypt_benchmark_kdf(struct crypt_device *cd,
938 const char *password,
939 size_t password_size,
942 uint64_t *iterations_sec);
945 * Get cipher used in device
947 * @param cd crypt device handle
949 * @return used cipher, e.g. "aes" or @e NULL otherwise
952 const char *crypt_get_cipher(struct crypt_device *cd);
955 * Get cipher mode used in device
957 * @param cd crypt device handle
959 * @return used cipher mode e.g. "xts-plain" or @e otherwise
962 const char *crypt_get_cipher_mode(struct crypt_device *cd);
967 * @param cd crypt device handle
969 * @return device UUID or @e NULL if not set
972 const char *crypt_get_uuid(struct crypt_device *cd);
975 * Get path to underlaying device
977 * @param cd crypt device handle
979 * @return path to underlaying device name
982 const char *crypt_get_device_name(struct crypt_device *cd);
985 * Get device offset in sectors where real data starts on underlying device)
987 * @param cd crypt device handle
989 * @return device offset in sectors
992 uint64_t crypt_get_data_offset(struct crypt_device *cd);
995 * Get IV offset in sectors (skip)
997 * @param cd crypt device handle
1002 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
1005 * Get size (in bytes) of volume key for crypt device
1007 * @param cd crypt device handle
1009 * @return volume key size
1012 int crypt_get_volume_key_size(struct crypt_device *cd);
1015 * Get device parameters for VERITY device
1017 * @param cd crypt device handle
1018 * @param vp verity device info
1020 * @e 0 on success or negative errno value otherwise.
1023 int crypt_get_verity_info(struct crypt_device *cd,
1024 struct crypt_params_verity *vp);
1028 * @addtogroup keyslot
1034 * Crypt keyslot info
1037 CRYPT_SLOT_INVALID, /**< invalid keyslot */
1038 CRYPT_SLOT_INACTIVE, /**< keyslot is inactive (free) */
1039 CRYPT_SLOT_ACTIVE, /**< keyslot is active (used) */
1040 CRYPT_SLOT_ACTIVE_LAST /**< keylost is active (used)
1041 * and last used at the same time */
1042 } crypt_keyslot_info;
1045 * Get information about particular key slot
1048 * @param cd crypt device handle
1049 * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
1051 * @return value defined by crypt_keyslot_info
1054 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
1058 * Get number of keyslots supported for device type.
1060 * @param type crypt device type
1062 * @return slot count or negative errno otherwise if device
1063 * doesn't not support keyslots.
1065 int crypt_keyslot_max(const char *type);
1068 * Get keyslot area pointers (relative to metadata device)
1070 * @param cd crypt device handle
1071 * @param keyslot keyslot number
1072 * @param offset offset on metadata device (in bytes)
1073 * @param length length of keyslot area (in bytes)
1075 * @return @e 0 on success or negative errno value otherwise.
1078 int crypt_keyslot_area(struct crypt_device *cd,
1084 * Backup header and keyslots to file
1086 * @param cd crypt device handle
1087 * @param requested_type @link crypt_type @endlink or @e NULL for all known
1088 * @param backup_file file to backup header to
1090 * @return @e 0 on success or negative errno value otherwise.
1093 int crypt_header_backup(struct crypt_device *cd,
1094 const char *requested_type,
1095 const char *backup_file);
1098 * Restore header and keyslots from backup file
1101 * @param cd crypt device handle
1102 * @param requested_type @link crypt_type @endlink or @e NULL for all known
1103 * @param backup_file file to restore header from
1105 * @return @e 0 on success or negative errno value otherwise.
1108 int crypt_header_restore(struct crypt_device *cd,
1109 const char *requested_type,
1110 const char *backup_file);
1113 * Receives last reported error
1115 * @param cd crypt device handle
1116 * @param buf buffef for message
1117 * @param size size of buffer
1119 * @note Note that this is old API function using global context.
1120 * All error messages are reported also through log callback.
1122 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size);
1125 * Receives last reported error, DEPRECATED
1127 * @param buf buffef for message
1128 * @param size size of buffer
1130 * @note Note that this is old API function using global context.
1131 * All error messages are reported also through log callback.
1133 void crypt_get_error(char *buf, size_t size);
1136 * Get directory where mapped crypt devices are created
1138 * @return the directory path
1140 const char *crypt_get_dir(void);
1143 * @defgroup dbg "Library debug level"
1145 * Set library debug level
1152 #define CRYPT_DEBUG_ALL -1
1154 #define CRYPT_DEBUG_NONE 0
1157 * Set the debug level for library
1159 * @param level debug level
1162 void crypt_set_debug_level(int level);
1169 #endif /* _LIBCRYPTSETUP_H */