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.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * @file libcryptsetup.h
26 * @brief Public cryptsetup API
28 * For more verbose examples of LUKS related use cases,
29 * please read @ref index "examples".
32 #ifndef _LIBCRYPTSETUP_H
33 #define _LIBCRYPTSETUP_H
41 struct crypt_device; /* crypt device handle */
44 * Initialize crypt device handle and check if provided device exists.
46 * @param cd Returns pointer to crypt device handle
47 * @param device Path to the backing device.
48 * If @e device is not a block device but a path to some file,
49 * the function will try to create a loopdevice and attach
50 * the file to the loopdevice with AUTOCLEAR flag set.
51 * If @e device is @e NULL function it will initialize dm backend only.
53 * @return @e 0 on success or negative errno value otherwise.
55 * @note Note that logging is not initialized here, possible messages uses
56 * default log function.
58 int crypt_init(struct crypt_device **cd, const char *device);
61 * Initialize crypt device handle from provided active device name,
62 * and, optionally, from separate metadata (header) device
63 * and check if provided device exists.
65 * @return @e 0 on success or negative errno value otherwise.
67 * @param cd returns crypt device handle for active device
68 * @param name name of active crypt device
69 * @param header_device optional device containing on-disk header
70 * (@e NULL if it the same as underlying device on there is no on-disk header)
72 * @post In case @e device points to active LUKS device but header load fails,
73 * context device type is set to @e NULL and @e 0 is returned as if it were successful.
74 * Context with @e NULL device type can only be deactivated by crypt_deactivate
76 * @note @link crypt_init_by_name @endlink is equivalent to calling
77 * crypt_init_by_name_and_header(cd, name, NULL);
79 int crypt_init_by_name_and_header(struct crypt_device **cd,
81 const char *header_device);
84 * This is equivalent to call
85 * @ref crypt_init_by_name_and_header "crypt_init_by_name_and_header(cd, name, NULL)"
87 * @sa crypt_init_by_name_and_header
89 int crypt_init_by_name(struct crypt_device **cd, const char *name);
92 * @defgroup loglevel Cryptsetup logging
94 * Set of functions and defines used in cryptsetup for
100 * @addtogroup loglevel
104 /** normal log level */
105 #define CRYPT_LOG_NORMAL 0
106 /** error log level */
107 #define CRYPT_LOG_ERROR 1
108 /** verbose log level */
109 #define CRYPT_LOG_VERBOSE 2
110 /** debug log level - always on stdout */
111 #define CRYPT_LOG_DEBUG -1
116 * @param cd crypt device handle (can be @e NULL to set default log function)
117 * @param log user defined log function reference
118 * @param usrptr provided identification in callback
119 * @param level log level below (debug messages can uses other levels)
120 * @param msg log message
122 void crypt_set_log_callback(struct crypt_device *cd,
123 void (*log)(int level, const char *msg, void *usrptr),
127 * Defines log function or use the default one otherwise.
129 * @see crypt_set_log_callback
131 * @param cd crypt device handle
132 * @param level log level
133 * @param msg log message
135 void crypt_log(struct crypt_device *cd, int level, const char *msg);
139 * Set confirmation callback (yes/no)
141 * If code need confirmation (like resetting uuid or restoring LUKS header from file)
142 * this function is called. If not defined, everything is confirmed.
144 * Callback function @e confirm should return @e 0 if operation is declined,
145 * other values mean accepted.
147 * @param cd crypt device handle
148 * @param confirm user defined confirm callback reference
149 * @param usrptr provided identification in callback
150 * @param msg Message for user to confirm
152 * @note Current version of cryptsetup API requires confirmation only when UUID is being changed
154 void crypt_set_confirm_callback(struct crypt_device *cd,
155 int (*confirm)(const char *msg, void *usrptr),
159 * Set password query callback.
161 * If code need @e _interactive_ query for password, this callback is called.
162 * If not defined, compiled-in default is called (uses terminal input).
164 * Callback should return length of password in buffer
165 * or negative errno value in case of error.
167 * @param cd crypt device handle
168 * @param password user defined password callback reference
169 * @param usrptr provided identification in callback
170 * @param msg Message for user
171 * @param buf buffer for password
172 * @param length size of buffer
174 * @note Note that if this function is defined, verify option is ignored
175 * (caller which provided callback is responsible for password verification)
176 * @note Only zero terminated passwords can be entered this way, for complex
177 * use API functions directly.
178 * @note Maximal length of password is limited to @e length @e - @e 1 (minimal 511 chars)
180 * @see Callback function is used in these call provided, that certain conditions are met:
181 * @li crypt_keyslot_add_by_passphrase
182 * @li crypt_activate_by_passphrase
183 * @li crypt_resume_by_passphrase
184 * @li crypt_resume_by_keyfile
185 * @li crypt_keyslot_add_by_keyfile
186 * @li crypt_keyslot_add_by_volume_key
189 void crypt_set_password_callback(struct crypt_device *cd,
190 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
194 * Set timeout for interactive password entry using default
197 * @param cd crypt device handle
198 * @param timeout_sec timeout in seconds
200 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
203 * Set number of retries in case password input has been incorrect
205 * @param cd crypt device handle
206 * @param tries the number
208 void crypt_set_password_retry(struct crypt_device *cd, int tries);
211 * Set how long should cryptsetup iterate in PBKDF2 function.
212 * Default value heads towards the iterations which takes around 1 second
214 * @param cd crypt device handle
215 * @param iteration_time_ms the time in ms
217 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms);
219 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
222 * Set whether passphrase will be verified on input
223 * (user has to input same passphrase twice)
225 * @param cd crypt device handle
226 * @param password_verify @e 0 = false, @e !0 true
228 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
232 * For LUKS it is encrypted data device when LUKS header is separated.
233 * For VERITY it is data device when hash device is separated.
235 * @param cd crypt device handle
236 * @param device path to device
239 int crypt_set_data_device(struct crypt_device *cd, const char *device);
242 * @defgroup rng Cryptsetup RNG
249 /** CRYPT_RNG_URANDOM - use /dev/urandom */
250 #define CRYPT_RNG_URANDOM 0
251 /** CRYPT_RNG_RANDOM - use /dev/random (waits if no entropy in system) */
252 #define CRYPT_RNG_RANDOM 1
255 * Set which RNG (random number generator) is used for generating long term key
257 * @param cd crypt device handle
258 * @param rng_type kernel random number generator to use
261 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
264 * Get which RNG (random number generator) is used for generating long term key
266 * @param cd crypt device handle
267 * @return RNG type on success or negative errno value otherwise.
270 int crypt_get_rng_type(struct crypt_device *cd);
275 * Helper to lock/unlock memory to avoid swap sensitive data to disk
277 * @param cd crypt device handle, can be @e NULL
278 * @param lock 0 to unlock otherwise lock memory
280 * @returns Value indicating whether the memory is locked (function can be called multiple times).
282 * @note Only root can do this.
283 * @note It locks/unlocks all process memory, not only crypt context.
285 int crypt_memory_lock(struct crypt_device *cd, int lock);
288 * @defgroup crypt_type Cryptsetup on-disk format types
290 * Set of functions, \#defines and structs related
291 * to on-disk format types
295 * @addtogroup crypt_type
299 /** plain crypt device, no on-disk header */
300 #define CRYPT_PLAIN "PLAIN"
301 /** LUKS version 1 header on-disk */
302 #define CRYPT_LUKS1 "LUKS1"
303 /** loop-AES compatibility mode */
304 #define CRYPT_LOOPAES "LOOPAES"
305 /** dm-verity mode */
306 #define CRYPT_VERITY "VERITY"
307 /** TCRYPT (TrueCrypt-compatible) mode */
308 #define CRYPT_TCRYPT "TCRYPT"
313 * @param cd crypt device handle
314 * @return string according to device type or @e NULL if not known.
316 const char *crypt_get_type(struct crypt_device *cd);
320 * Structure used as parameter for PLAIN device type
324 struct crypt_params_plain {
325 const char *hash; /**< password hash function */
326 uint64_t offset; /**< offset in sectors */
327 uint64_t skip; /**< IV offset / initialization sector */
328 uint64_t size; /**< size of mapped device or @e 0 for autodetection */
332 * Structure used as parameter for LUKS device type
334 * @see crypt_format, crypt_load
336 * @note during crypt_format @e data_device attribute determines
337 * if the LUKS header is separated from encrypted payload device
340 struct crypt_params_luks1 {
341 const char *hash; /**< hash used in LUKS header */
342 size_t data_alignment; /**< data alignment in sectors, data offset is multiple of this */
343 const char *data_device; /**< detached encrypted data device or @e NULL */
348 * Structure used as parameter for loop-AES device type
353 struct crypt_params_loopaes {
354 const char *hash; /**< key hash function */
355 uint64_t offset; /**< offset in sectors */
356 uint64_t skip; /**< IV offset / initialization sector */
361 * Structure used as parameter for dm-verity device type
363 * @see crypt_format, crypt_load
366 struct crypt_params_verity {
367 const char *hash_name; /**< hash function */
368 const char *data_device; /**< data_device (CRYPT_VERITY_CREATE_HASH) */
369 const char *hash_device; /**< hash_device (output only) */
370 const char *salt; /**< salt */
371 uint32_t salt_size; /**< salt size (in bytes) */
372 uint32_t hash_type; /**< in-kernel hashing type */
373 uint32_t data_block_size; /**< data block size (in bytes) */
374 uint32_t hash_block_size; /**< hash block size (in bytes) */
375 uint64_t data_size; /**< data area size (in data blocks) */
376 uint64_t hash_area_offset; /**< hash/header offset (in bytes) */
377 uint32_t flags; /**< CRYPT_VERITY* flags */
380 /** No on-disk header (only hashes) */
381 #define CRYPT_VERITY_NO_HEADER (1 << 0)
382 /** Verity hash in userspace before activation */
383 #define CRYPT_VERITY_CHECK_HASH (1 << 1)
384 /** Create hash - format hash device */
385 #define CRYPT_VERITY_CREATE_HASH (1 << 2)
389 * Structure used as parameter for TCRYPT device type
394 struct crypt_params_tcrypt {
395 const char *passphrase; /**< passphrase to unlock header (input only) */
396 size_t passphrase_size; /**< passphrase size (input only, max length is 64) */
397 const char **keyfiles; /**< keyfile paths to unlock header (input only) */
398 unsigned int keyfiles_count;/**< keyfiles count (input only) */
399 const char *hash_name; /**< hash function for PBKDF */
400 const char *cipher; /**< cipher chain c1[-c2[-c3]] */
401 const char *mode; /**< cipher block mode */
402 size_t key_size; /**< key size in bytes (the whole chain) */
403 uint32_t flags; /**< CRYPT_TCRYPT* flags */
406 /** Include legacy modes ehn scannig for header*/
407 #define CRYPT_TCRYPT_LEGACY_MODES (1 << 0)
408 /** Try to load hidden header (describing hidden device) */
409 #define CRYPT_TCRYPT_HIDDEN_HEADER (1 << 1)
410 /** Try to load backup header */
411 #define CRYPT_TCRYPT_BACKUP_HEADER (1 << 2)
412 /** Device contains encrypted system (with boot loader) */
413 #define CRYPT_TCRYPT_SYSTEM_HEADER (1 << 3)
418 * Create (format) new crypt device (and possible header on-disk) but not activates it.
420 * @pre @e cd contains initialized and not formatted device context (device type must @b not be set)
422 * @param cd crypt device handle
423 * @param type type of device (optional params struct must be of this type)
424 * @param cipher (e.g. "aes")
425 * @param cipher_mode including IV specification (e.g. "xts-plain")
426 * @param uuid requested UUID or @e NULL if it should be generated
427 * @param volume_key pre-generated volume key or @e NULL if it should be generated (only for LUKS)
428 * @param volume_key_size size of volume key in bytes.
429 * @param params crypt type specific parameters (see @link crypt_type @endlink)
431 * @returns @e 0 on success or negative errno value otherwise.
433 * @note Note that crypt_format does not enable any keyslot (in case of work with LUKS device),
434 * but it stores volume key internally and subsequent crypt_keyslot_add_* calls can be used.
435 * @note For VERITY @link crypt_type @endlink, only uuid parameter is used, others paramaters
436 * are ignored and verity specific attributes are set through mandatory params option.
438 int crypt_format(struct crypt_device *cd,
441 const char *cipher_mode,
443 const char *volume_key,
444 size_t volume_key_size,
448 * Set new UUID for already existing device
450 * @param cd crypt device handle
451 * @param uuid requested UUID or @e NULL if it should be generated
453 * @returns 0 on success or negative errno value otherwise.
455 * @note Currently, only LUKS device type are supported
457 int crypt_set_uuid(struct crypt_device *cd,
461 * Load crypt device parameters from on-disk header
463 * @param cd crypt device handle
464 * @param requested_type @link crypt_type @endlink or @e NULL for all known
465 * @param params crypt type specific parameters (see @link crypt_type @endlink)
467 * @returns 0 on success or negative errno value otherwise.
469 * @post In case LUKS header is read successfully but payload device is too small
470 * error is returned and device type in context is set to @e NULL
472 * @note Note that in current version load works only for LUKS and VERITY device type.
475 int crypt_load(struct crypt_device *cd,
476 const char *requested_type,
480 * Try to repair crypt device on-disk header if invalid
482 * @param cd crypt device handle
483 * @param requested_type @link crypt_type @endlink or @e NULL for all known
484 * @param params crypt type specific parameters (see @link crypt_type @endlink)
486 * @returns 0 on success or negative errno value otherwise.
489 int crypt_repair(struct crypt_device *cd,
490 const char *requested_type,
494 * Resize crypt device
496 * @param cd - crypt device handle
497 * @param name - name of device to resize
498 * @param new_size - new device size in sectors or @e 0 to use all of the underlying device size
500 * @return @e 0 on success or negative errno value otherwise.
502 int crypt_resize(struct crypt_device *cd,
507 * Suspends crypt device.
509 * @param cd crypt device handle, can be @e NULL
510 * @param name name of device to suspend
512 * @return 0 on success or negative errno value otherwise.
514 * @note Only LUKS device type is supported
517 int crypt_suspend(struct crypt_device *cd,
521 * Resumes crypt device using passphrase.
524 * @param cd crypt device handle
525 * @param name name of device to resume
526 * @param keyslot requested keyslot or CRYPT_ANY_SLOT
527 * @param passphrase passphrase used to unlock volume key, @e NULL for query
528 * @param passphrase_size size of @e passphrase (binary data)
530 * @return unlocked key slot number or negative errno otherwise.
532 * @note Only LUKS device type is supported
534 int crypt_resume_by_passphrase(struct crypt_device *cd,
537 const char *passphrase,
538 size_t passphrase_size);
541 * Resumes crypt device using key file.
543 * @param cd crypt device handle
544 * @param name name of device to resume
545 * @param keyslot requested keyslot or CRYPT_ANY_SLOT
546 * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
547 * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
548 * @param keyfile_offset number of bytes to skip at start of keyfile
550 * @return unlocked key slot number or negative errno otherwise.
552 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
557 size_t keyfile_offset);
559 * Backward compatible crypt_resume_by_keyfile_offset() (without offset).
561 int crypt_resume_by_keyfile(struct crypt_device *cd,
565 size_t keyfile_size);
568 * Releases crypt device context and used memory.
570 * @param cd crypt device handle
572 void crypt_free(struct crypt_device *cd);
575 * @defgroup keyslot Cryptsetup LUKS keyslots
576 * @addtogroup keyslot
581 /** iterate through all keyslots and find first one that fits */
582 #define CRYPT_ANY_SLOT -1
585 * Add key slot using provided passphrase
587 * @pre @e cd contains initialized and formatted LUKS device context
589 * @param cd crypt device handle
590 * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
591 * @param passphrase passphrase used to unlock volume key, @e NULL for query
592 * @param passphrase_size size of passphrase (binary data)
593 * @param new_passphrase passphrase for new keyslot, @e NULL for query
594 * @param new_passphrase_size size of @e new_passphrase (binary data)
596 * @return allocated key slot number or negative errno otherwise.
598 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
600 const char *passphrase,
601 size_t passphrase_size,
602 const char *new_passphrase,
603 size_t new_passphrase_size);
606 * Change defined key slot using provided passphrase
608 * @pre @e cd contains initialized and formatted LUKS device context
610 * @param cd crypt device handle
611 * @param keyslot_old old keyslot or @e CRYPT_ANY_SLOT
612 * @param keyslot_new new keyslot (can be the same as old)
613 * @param passphrase passphrase used to unlock volume key, @e NULL for query
614 * @param passphrase_size size of passphrase (binary data)
615 * @param new_passphrase passphrase for new keyslot, @e NULL for query
616 * @param new_passphrase_size size of @e new_passphrase (binary data)
618 * @return allocated key slot number or negative errno otherwise.
620 * @note This function is just internal implementation of luksChange
621 * command to avoid reading of volume key outside libcryptsetup boundary
624 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
627 const char *passphrase,
628 size_t passphrase_size,
629 const char *new_passphrase,
630 size_t new_passphrase_size);
633 * Add key slot using provided key file path
635 * @pre @e cd contains initialized and formatted LUKS device context
637 * @param cd crypt device handle
638 * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
639 * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
640 * @param keyfile_size number of bytes to read from keyfile, @e 0 is unlimited
641 * @param keyfile_offset number of bytes to skip at start of keyfile
642 * @param new_keyfile keyfile for new keyslot, @e NULL for passphrase query
643 * @param new_keyfile_size number of bytes to read from @e new_keyfile, @e 0 is unlimited
644 * @param new_keyfile_offset number of bytes to skip at start of new_keyfile
646 * @return allocated key slot number or negative errno otherwise.
648 * @note Note that @e keyfile can be "-" for STDIN
651 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
655 size_t keyfile_offset,
656 const char *new_keyfile,
657 size_t new_keyfile_size,
658 size_t new_keyfile_offset);
660 * Backward compatible crypt_keyslot_add_by_keyfile_offset() (without offset).
662 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
666 const char *new_keyfile,
667 size_t new_keyfile_size);
670 * Add key slot using provided volume key
672 * @pre @e cd contains initialized and formatted LUKS device context
674 * @param cd crypt device handle
675 * @param keyslot requested keyslot or CRYPT_ANY_SLOT
676 * @param volume_key provided volume key or @e NULL if used after crypt_format
677 * @param volume_key_size size of volume_key
678 * @param passphrase passphrase for new keyslot, @e NULL for query
679 * @param passphrase_size size of passphrase
681 * @return allocated key slot number or negative errno otherwise.
684 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
686 const char *volume_key,
687 size_t volume_key_size,
688 const char *passphrase,
689 size_t passphrase_size);
692 * Destroy (and disable) key slot
694 * @pre @e cd contains initialized and formatted LUKS device context
696 * @param cd crypt device handle
697 * @param keyslot requested key slot to destroy
699 * @return @e 0 on success or negative errno value otherwise.
701 * @note Note that there is no passphrase verification used.
703 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
708 * @defgroup aflags Device runtime attributes
716 /** device is read only */
717 #define CRYPT_ACTIVATE_READONLY (1 << 0)
718 /** only reported for device without uuid */
719 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
720 /** activate even if cannot grant exclusive access (DANGEROUS) */
721 #define CRYPT_ACTIVATE_SHARED (1 << 2)
722 /** enable discards aka TRIM */
723 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3)
724 /** skip global udev rules in activation ("private device"), input only */
725 #define CRYPT_ACTIVATE_PRIVATE (1 << 4)
726 /** corruption detected (verity), output only */
727 #define CRYPT_ACTIVATE_CORRUPTED (1 << 5)
730 * Active device runtime attributes
732 struct crypt_active_device {
733 uint64_t offset; /**< offset in sectors */
734 uint64_t iv_offset; /**< IV initialization sector */
735 uint64_t size; /**< active device size */
736 uint32_t flags; /**< activation flags */
740 * Receives runtime attributes of active crypt device
742 * @param cd crypt device handle (can be @e NULL)
743 * @param name name of active device
744 * @param cad preallocated active device attributes to fill
746 * @return @e 0 on success or negative errno value otherwise
749 int crypt_get_active_device(struct crypt_device *cd,
751 struct crypt_active_device *cad);
756 * Activate device or check passphrase
758 * @param cd crypt device handle
759 * @param name name of device to create, if @e NULL only check passphrase
760 * @param keyslot requested keyslot to check or @e CRYPT_ANY_SLOT
761 * @param passphrase passphrase used to unlock volume key, @e NULL for query
762 * @param passphrase_size size of @e passphrase
763 * @param flags activation flags
765 * @return unlocked key slot number or negative errno otherwise.
767 int crypt_activate_by_passphrase(struct crypt_device *cd,
770 const char *passphrase,
771 size_t passphrase_size,
775 * Activate device or check using key file
777 * @param cd crypt device handle
778 * @param name name of device to create, if @e NULL only check keyfile
779 * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
780 * @param keyfile key file used to unlock volume key
781 * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
782 * @param keyfile_offset number of bytes to skip at start of keyfile
783 * @param flags activation flags
785 * @return unlocked key slot number or negative errno otherwise.
787 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
792 size_t keyfile_offset,
795 * Backward compatible crypt_activate_by_keyfile_offset() (without offset).
797 int crypt_activate_by_keyfile(struct crypt_device *cd,
805 * Activate device using provided volume key
808 * @param cd crypt device handle
809 * @param name name of device to create, if @e NULL only check volume key
810 * @param volume_key provided volume key (or @e NULL to use internal)
811 * @param volume_key_size size of volume_key
812 * @param flags activation flags
814 * @return @e 0 on success or negative errno value otherwise.
816 * @note If @e NULL is used for volume_key, device has to be initialized
817 * by previous operation (like @ref crypt_format
818 * or @ref crypt_init_by_name)
819 * @note For VERITY the volume key means root hash required for activation.
820 * Because kernel dm-verity is always read only, you have to provide
821 * CRYPT_ACTIVATE_READONLY flag always.
822 * @note For TCRYPT the volume key should be always NULL and because master
823 * key from decrypted header is used instead.
825 int crypt_activate_by_volume_key(struct crypt_device *cd,
827 const char *volume_key,
828 size_t volume_key_size,
832 * Deactivate crypt device. This function tries to remove active device-mapper
833 * mapping from kernel. Also, sensitive data like the volume key are removed from
836 * @param cd crypt device handle, can be @e NULL
837 * @param name name of device to deactivate
839 * @return @e 0 on success or negative errno value otherwise.
842 int crypt_deactivate(struct crypt_device *cd, const char *name);
845 * Get volume key from of crypt device
847 * @param cd crypt device handle
848 * @param keyslot use this keyslot or @e CRYPT_ANY_SLOT
849 * @param volume_key buffer for volume key
850 * @param volume_key_size on input, size of buffer @e volume_key,
851 * on output size of @e volume_key
852 * @param passphrase passphrase used to unlock volume key
853 * @param passphrase_size size of @e passphrase
855 * @return unlocked key slot number or negative errno otherwise.
857 * @note For TCRYPT cipher chain is the volume key concatenated
858 * for all ciphers in chain.
860 int crypt_volume_key_get(struct crypt_device *cd,
863 size_t *volume_key_size,
864 const char *passphrase,
865 size_t passphrase_size);
868 * Verify that provided volume key is valid for crypt device
870 * @param cd crypt device handle
871 * @param volume_key provided volume key
872 * @param volume_key_size size of @e volume_key
874 * @return @e 0 on success or negative errno value otherwise.
876 int crypt_volume_key_verify(struct crypt_device *cd,
877 const char *volume_key,
878 size_t volume_key_size);
881 * @defgroup devstat Crypt and Verity device status
882 * @addtogroup devstat
890 CRYPT_INVALID, /**< device mapping is invalid in this context */
891 CRYPT_INACTIVE, /**< no such mapped device */
892 CRYPT_ACTIVE, /**< device is active */
893 CRYPT_BUSY /**< device is active and has open count > 0 */
897 * Get status info about device name
899 * @param cd crypt device handle, can be @e NULL
900 * @param name crypt device name
902 * @return value defined by crypt_status_info.
905 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
908 * Dump text-formatted information about crypt or verity device to log output
910 * @param cd crypt device handle
912 * @return @e 0 on success or negative errno value otherwise.
914 int crypt_dump(struct crypt_device *cd);
917 * Get cipher used in device
919 * @param cd crypt device handle
921 * @return used cipher, e.g. "aes" or @e NULL otherwise
924 const char *crypt_get_cipher(struct crypt_device *cd);
927 * Get cipher mode used in device
929 * @param cd crypt device handle
931 * @return used cipher mode e.g. "xts-plain" or @e otherwise
934 const char *crypt_get_cipher_mode(struct crypt_device *cd);
939 * @param cd crypt device handle
941 * @return device UUID or @e NULL if not set
944 const char *crypt_get_uuid(struct crypt_device *cd);
947 * Get path to underlaying device
949 * @param cd crypt device handle
951 * @return path to underlaying device name
954 const char *crypt_get_device_name(struct crypt_device *cd);
957 * Get device offset in sectors where real data starts on underlying device)
959 * @param cd crypt device handle
961 * @return device offset in sectors
964 uint64_t crypt_get_data_offset(struct crypt_device *cd);
967 * Get IV offset in sectors (skip)
969 * @param cd crypt device handle
974 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
977 * Get size (in bytes) of volume key for crypt device
979 * @param cd crypt device handle
981 * @return volume key size
984 int crypt_get_volume_key_size(struct crypt_device *cd);
987 * Get device parameters for VERITY device
989 * @param cd crypt device handle
990 * @param vp verity device info
992 * @e 0 on success or negative errno value otherwise.
995 int crypt_get_verity_info(struct crypt_device *cd,
996 struct crypt_params_verity *vp);
1000 * @defgroup benchmark Benchmarking
1002 * Benchmarking of algorithms
1004 * @addtogroup benchmark
1010 * Informational benchmark for ciphers
1012 * @param cd crypt device handle
1013 * @param cipher (e.g. "aes")
1014 * @param cipher_mode (e.g. "xts"), IV generator is ignored
1015 * @param volume_key_size size of volume key in bytes
1016 * @param iv_size size of IV in bytes
1017 * @param buffer_size size of encryption buffer in bytes used in test
1018 * @param encryption_mbs measured encryption speed in MiB/s
1019 * @param decryption_mbs measured decryption speed in MiB/s
1021 * @return @e 0 on success or negative errno value otherwise.
1023 int crypt_benchmark(struct crypt_device *cd,
1025 const char *cipher_mode,
1026 size_t volume_key_size,
1029 double *encryption_mbs,
1030 double *decryption_mbs);
1033 * Informational benchmark for KDF
1035 * @param cd crypt device handle
1036 * @param kdf Key derivation function (e.g. "pbkdf2")
1037 * @param hash Hash algorithm used in KDF (e.g. "sha256")
1038 * @param password password for benchmark
1039 * @param password_size size of password
1040 * @param salt salt for benchmark
1041 * @param salt_size size of salt
1042 * @param iterations_sec returns measured KDF iterations per second
1044 * @return @e 0 on success or negative errno value otherwise.
1046 int crypt_benchmark_kdf(struct crypt_device *cd,
1049 const char *password,
1050 size_t password_size,
1053 uint64_t *iterations_sec);
1057 * @addtogroup keyslot
1063 * Crypt keyslot info
1066 CRYPT_SLOT_INVALID, /**< invalid keyslot */
1067 CRYPT_SLOT_INACTIVE, /**< keyslot is inactive (free) */
1068 CRYPT_SLOT_ACTIVE, /**< keyslot is active (used) */
1069 CRYPT_SLOT_ACTIVE_LAST /**< keylost is active (used)
1070 * and last used at the same time */
1071 } crypt_keyslot_info;
1074 * Get information about particular key slot
1077 * @param cd crypt device handle
1078 * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
1080 * @return value defined by crypt_keyslot_info
1083 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
1087 * Get number of keyslots supported for device type.
1089 * @param type crypt device type
1091 * @return slot count or negative errno otherwise if device
1092 * doesn't not support keyslots.
1094 int crypt_keyslot_max(const char *type);
1097 * Get keyslot area pointers (relative to metadata device)
1099 * @param cd crypt device handle
1100 * @param keyslot keyslot number
1101 * @param offset offset on metadata device (in bytes)
1102 * @param length length of keyslot area (in bytes)
1104 * @return @e 0 on success or negative errno value otherwise.
1107 int crypt_keyslot_area(struct crypt_device *cd,
1113 * Backup header and keyslots to file
1115 * @param cd crypt device handle
1116 * @param requested_type @link crypt_type @endlink or @e NULL for all known
1117 * @param backup_file file to backup header to
1119 * @return @e 0 on success or negative errno value otherwise.
1122 int crypt_header_backup(struct crypt_device *cd,
1123 const char *requested_type,
1124 const char *backup_file);
1127 * Restore header and keyslots from backup file
1130 * @param cd crypt device handle
1131 * @param requested_type @link crypt_type @endlink or @e NULL for all known
1132 * @param backup_file file to restore header from
1134 * @return @e 0 on success or negative errno value otherwise.
1137 int crypt_header_restore(struct crypt_device *cd,
1138 const char *requested_type,
1139 const char *backup_file);
1142 * Receives last reported error
1144 * @param cd crypt device handle
1145 * @param buf buffef for message
1146 * @param size size of buffer
1148 * @note Note that this is old API function using global context.
1149 * All error messages are reported also through log callback.
1151 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size);
1154 * Receives last reported error, DEPRECATED
1156 * @param buf buffef for message
1157 * @param size size of buffer
1159 * @note Note that this is old API function using global context.
1160 * All error messages are reported also through log callback.
1162 void crypt_get_error(char *buf, size_t size);
1165 * Get directory where mapped crypt devices are created
1167 * @return the directory path
1169 const char *crypt_get_dir(void);
1172 * @defgroup dbg Library debug level
1174 * Set library debug level
1181 #define CRYPT_DEBUG_ALL -1
1183 #define CRYPT_DEBUG_NONE 0
1186 * Set the debug level for library
1188 * @param level debug level
1191 void crypt_set_debug_level(int level);
1198 #endif /* _LIBCRYPTSETUP_H */