1 #ifndef _LIBCRYPTSETUP_H
2 #define _LIBCRYPTSETUP_H
9 struct crypt_device; /* crypt device handle */
12 * Initialise crypt device handle and check if provided device exists.
14 * Returns 0 on success or negative errno value otherwise.
16 * @cd - returns pointer to crypt device handle
17 * @device - path to device
19 * Note that logging is not initialized here, possible messages uses
20 * default log function.
22 int crypt_init(struct crypt_device **cd, const char *device);
25 * Initialise crypt device handle from provided active device name,
26 * and, optionally, from separate metadata (header) device
27 * and check if provided device exists.
29 * Returns 0 on success or negative errno value otherwise.
31 * @cd - crypt device handle
32 * @name - name of active crypt device
33 * @header_device - optional device containing on-disk header
34 * (NULL if it the same as underlying device on there is no on-disk header)
36 * crypt_init_by_name is quivalent to calling
37 * crypt_init_by_name_and_header(cd, name, NULL);
39 int crypt_init_by_name_and_header(struct crypt_device **cd,
41 const char *header_device);
42 int crypt_init_by_name(struct crypt_device **cd, const char *name);
47 * @cd - crypt device handle (can be NULL to set default log function)
48 * @usrptr - provided identification in callback
49 * @level - log level below (debug messages can uses other levels)
52 #define CRYPT_LOG_NORMAL 0
53 #define CRYPT_LOG_ERROR 1
54 #define CRYPT_LOG_VERBOSE 2
55 #define CRYPT_LOG_DEBUG -1 /* always on stdout */
56 void crypt_set_log_callback(struct crypt_device *cd,
57 void (*log)(int level, const char *msg, void *usrptr),
61 * Log message through log function.
63 * @cd - crypt device handle
67 void crypt_log(struct crypt_device *cd, int level, const char *msg);
70 * Set confirmation callback (yes/no)
72 * If code need confirmation (like deleting last key slot) this function
73 * is called. If not defined, everything is confirmed.
75 * Calback should return 0 if operation is declined, other values mean accepted.
77 * @cd - crypt device handle
78 * @usrptr - provided identification in callback
79 * @msg - Message for user to confirm
81 void crypt_set_confirm_callback(struct crypt_device *cd,
82 int (*confirm)(const char *msg, void *usrptr),
86 * Set password query callback.
88 * If code need _interactive_ query for password, this callback is called.
89 * If not defined, compiled-in default is called (uses terminal input).
91 * @cd - crypt device handle
92 * @usrptr - provided identification in callback
93 * @msg - Message for user
94 * @buf - buffer for password
95 * @length - size of buffer
97 * Calback should return length of password in buffer
98 * or negative errno value in case of error.
100 * - Note that if this function is defined, verify option is ignored
101 * (caller which provided callback is responsible for password verification)
102 * - Only zero terminated passwords can be entered this way, for complex
103 * use API functions directly.
104 * - Maximal length of password is limited to @length-1 (minimal 511 chars)
106 void crypt_set_password_callback(struct crypt_device *cd,
107 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
111 * Various crypt device parameters
113 * @cd - crypt device handle
114 * @timeout - timeout in seconds for password entry if compiled-in function used
115 * @password_retry - number of tries for password if not verified
116 * @iteration_time - iteration time for LUKS header in miliseconds
117 * @password_verify - for compiled-in password query always verify passwords twice
119 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
120 void crypt_set_password_retry(struct crypt_device *cd, int tries);
121 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
122 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
125 * Set data device (ciphertext device) if LUKS header is separated
126 * @cd - crypt device handle
127 * @device - path to device
129 int crypt_set_data_device(struct crypt_device *cd, const char *device);
132 * Set which RNG (random number generator) is used for generating long term key
133 * @cd - crypt device handle
134 * @rng_type - kernel random number generator to use
136 * CRYPT_RNG_URANDOM - use /dev/urandom
137 * CRYPT_RNG_RANDOM - use /dev/random (waits if no entropy in system)
139 #define CRYPT_RNG_URANDOM 0
140 #define CRYPT_RNG_RANDOM 1
141 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
144 * Get which RNG (random number generator) is used for generating long term key
146 * Returns RNG type on success or negative errno value otherwise.
148 * @cd - crypt device handle
150 int crypt_get_rng_type(struct crypt_device *cd);
153 * Helper to lock/unlock memory to avoid swap sensitive data to disk
155 * @cd - crypt device handle, can be NULL
156 * @lock - 0 to unloct otherwise lock memory
158 * Return value indicates that memory is locked (function can be called multiple times).
159 * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
161 int crypt_memory_lock(struct crypt_device *cd, int lock);
163 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
164 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
165 #define CRYPT_LOOPAES "LOOPAES" /* loop-AES compatibility mode */
170 * @cd - crypt device handle
172 * Returns string according to device type or NULL if not known.
174 const char *crypt_get_type(struct crypt_device *cd);
176 struct crypt_params_plain {
177 const char *hash; /* password hash function */
178 uint64_t offset; /* offset in sectors */
179 uint64_t skip; /* IV offset / initialisation sector */
180 uint64_t size; /* size of mapped device or 0 for autodetection */
183 struct crypt_params_luks1 {
184 const char *hash; /* hash used in LUKS header */
185 size_t data_alignment; /* in sectors, data offset is multiple of this */
186 const char *data_device; /* detached ciphertext device or NULL */
189 struct crypt_params_loopaes {
190 const char *hash; /* key hash function */
191 uint64_t offset; /* offset in sectors */
192 uint64_t skip; /* IV offset / initialisation sector */
195 * Create (format) new crypt device (and possible header on-disk) but not activates it.
197 * Returns 0 on success or negative errno value otherwise.
199 * @cd - crypt device handle
200 * @type - type of device (optional params struct must be of this type)
201 * @cipher - (e.g. "aes")
202 * @cipher_mode - including IV specification (e.g. "xts-plain")
203 * @uuid - requested UUID or NULL if it should be generated
204 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
205 * @volume_key_size - size of volume key in bytes.
206 * @params - crypt type specific parameters
208 * Note that crypt_format does not enable any keyslot, but it stores volume key internally
209 * and subsequent crypt_keyslot_add_* calls can be used.
211 int crypt_format(struct crypt_device *cd,
214 const char *cipher_mode,
216 const char *volume_key,
217 size_t volume_key_size,
221 * Set new UUID for already existing device (if format supports it)
223 * Returns 0 on success or negative errno value otherwise.
225 * @cd - crypt device handle
226 * @uuid - requested UUID or NULL if it should be generated
228 int crypt_set_uuid(struct crypt_device *cd,
232 * Load crypt device parameters from on-disk header
234 * Returns 0 on success or negative errno value otherwise.
236 * @cd - crypt device handle
237 * @requested_type - use NULL for all known
238 * @params - crypt type specific parameters
240 int crypt_load(struct crypt_device *cd,
241 const char *requested_type,
245 * Resize crypt device
247 * Returns 0 on success or negative errno value otherwise.
249 * @cd - crypt device handle
250 * @name - name of device to resize
251 * @new_size - new device size in sectors or 0 to use underlying device size
253 int crypt_resize(struct crypt_device *cd,
258 * Suspends crypt device.
260 * Returns 0 on success or negative errno value otherwise.
262 * @cd - crypt device handle, can be NULL
263 * @name - name of device to suspend
265 int crypt_suspend(struct crypt_device *cd,
269 * Resumes crypt device using passphrase.
271 * Returns unlocked key slot number or negative errno otherwise.
273 * @cd - crypt device handle
274 * @name - name of device to resume
275 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
276 * @passphrase - passphrase used to unlock volume key, NULL for query
277 * @passphrase_size - size of @passphrase (binary data)
279 int crypt_resume_by_passphrase(struct crypt_device *cd,
282 const char *passphrase,
283 size_t passphrase_size);
286 * Resumes crypt device using key file.
288 * Returns unlocked key slot number or negative errno otherwise.
290 * @cd - crypt device handle
291 * @name - name of device to resume
292 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
293 * @keyfile - key file used to unlock volume key, NULL for passphrase query
294 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
296 int crypt_resume_by_keyfile(struct crypt_device *cd,
300 size_t keyfile_size);
303 * Releases crypt device context and used memory.
305 * @cd - crypt device handle
307 void crypt_free(struct crypt_device *cd);
310 * Add key slot using provided passphrase
312 * Returns allocated key slot number or negative errno otherwise.
314 * @cd - crypt device handle
315 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
316 * @passphrase - passphrase used to unlock volume key, NULL for query
317 * @passphrase_size - size of @passphrase (binary data)
318 * @new_passphrase - passphrase for new keyslot, NULL for query
319 * @new_passphrase_size - size of @new_passphrase (binary data)
321 #define CRYPT_ANY_SLOT -1
322 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
324 const char *passphrase,
325 size_t passphrase_size,
326 const char *new_passphrase,
327 size_t new_passphrase_size);
330 * Get number of keyslots supported for device type.
332 * Returns slot count or negative errno otherwise if device
333 * doesn't not support keyslots.
335 * @type - crypt device type
337 int crypt_keyslot_max(const char *type);
340 * Add key slot using provided key file path
342 * Returns allocated key slot number or negative errno otherwise.
344 * @cd - crypt device handle
345 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
346 * @keyfile - key file used to unlock volume key, NULL for passphrase query
347 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
348 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
349 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
351 * Note that @keyfile can be "-" for STDIN
353 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
357 const char *new_keyfile,
358 size_t new_keyfile_size);
361 * Add key slot using provided volume key
363 * Returns allocated key slot number or negative errno otherwise.
365 * @cd - crypt device handle
366 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
367 * @volume_key - provided volume key or NULL if used after crypt_format
368 * @volume_key_size - size of @volume_key
369 * @passphrase - passphrase for new keyslot, NULL for query
370 * @passphrase_size - size of @passphrase
372 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
374 const char *volume_key,
375 size_t volume_key_size,
376 const char *passphrase,
377 size_t passphrase_size);
380 * Destroy (and disable) key slot
382 * Returns 0 on success or negative errno value otherwise.
384 * @cd - crypt device handle
385 * @keyslot - requested key slot to destroy
387 * Note that there is no passphrase verification used.
389 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
394 #define CRYPT_ACTIVATE_READONLY (1 << 0)
395 #define CRYPT_ACTIVATE_NO_UUID (1 << 1) /* ignored */
396 #define CRYPT_ACTIVATE_SHARED (1 << 2)
397 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3) /* enable discards aka TRIM */
400 * Active device runtime attributes
402 struct crypt_active_device {
403 uint64_t offset; /* offset in sectors */
404 uint64_t iv_offset; /* IV initilisation sector */
405 uint64_t size; /* active device size */
406 uint32_t flags; /* activation flags */
410 * Receives runtime attributes of active crypt device
412 * Returns 0 on success or negative errno value otherwise.
414 * @cd - crypt device handle (can be NULL)
415 * @name - name of active device
416 * @cad - preallocated active device attributes to fill
418 int crypt_get_active_device(struct crypt_device *cd,
420 struct crypt_active_device *cad);
423 * Activate device or check passphrase
425 * Returns unlocked key slot number or negative errno otherwise.
427 * @cd - crypt device handle
428 * @name - name of device to create, if NULL only check passphrase
429 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
430 * @passphrase - passphrase used to unlock volume key, NULL for query
431 * @passphrase_size - size of @passphrase
432 * @flags - activation flags
434 int crypt_activate_by_passphrase(struct crypt_device *cd,
437 const char *passphrase,
438 size_t passphrase_size,
442 * Activate device or check using key file
444 * Returns unlocked key slot number or negative errno otherwise.
446 * @cd - crypt device handle
447 * @name - name of device to create, if NULL only check keyfile
448 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
449 * @keyfile - key file used to unlock volume key
450 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
451 * @flags - activation flags
453 int crypt_activate_by_keyfile(struct crypt_device *cd,
461 * Activate device using provided volume key
463 * Returns 0 on success or negative errno value otherwise.
465 * @cd - crypt device handle
466 * @name - name of device to create, if NULL only check volume key
467 * @volume_key - provided volume key (or NULL to use internal)
468 * @volume_key_size - size of @volume_key
469 * @flags - activation flags
471 * If NULL is used for volume_key, device has to be initialized
472 * by previous operation (like crypt_format() or crypt_init_by_name())
474 int crypt_activate_by_volume_key(struct crypt_device *cd,
476 const char *volume_key,
477 size_t volume_key_size,
481 * Deactivate crypt device
483 * @cd - crypt device handle, can be NULL
484 * @name - name of device to deactivate
486 int crypt_deactivate(struct crypt_device *cd, const char *name);
489 * Get volume key from of crypt device
491 * Returns unlocked key slot number or negative errno otherwise.
493 * @cd - crypt device handle
494 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
495 * @volume_key - buffer for volume key
496 * @volume_key_size - on input, size of buffer @volume_key,
497 * on output size of @volume_key
498 * @passphrase - passphrase used to unlock volume key, NULL for query
499 * @passphrase_size - size of @passphrase
501 int crypt_volume_key_get(struct crypt_device *cd,
504 size_t *volume_key_size,
505 const char *passphrase,
506 size_t passphrase_size);
509 * Verify that provided volume key is valid for crypt device
511 * Returns 0 on success or negative errno value otherwise.
513 * @cd - crypt device handle
514 * @volume_key - provided volume key
515 * @volume_key_size - size of @volume_key
517 int crypt_volume_key_verify(struct crypt_device *cd,
518 const char *volume_key,
519 size_t volume_key_size);
522 * Get status info about device name
524 * Returns value defined by crypt_status_info.
526 * @cd - crypt device handle, can be NULL
527 * @name -crypt device name
529 * CRYPT_INACTIVE - no such mapped device
530 * CRYPT_ACTIVE - device is active
531 * CRYPT_BUSY - device is active and has open count > 0
539 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
542 * Dump text-formatted information about crypt device to log output
544 * Returns 0 on success or negative errno value otherwise.
546 * @cd - crypt device handle
548 int crypt_dump(struct crypt_device *cd);
551 * Various crypt device info functions
553 * @cd - crypt device handle
555 * cipher - used cipher, e.g. "aes" or NULL otherwise
556 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
557 * uuid - device UUID or NULL if not set
558 * device_name - underlying device name or NULL if not yet set
559 * data_offset - device offset in sectors where real data starts on underlying device)
560 * iv_offset - IV offset in sectors (skip)
561 * volume_key_size - size (in bytes) of volume key for crypt device
563 const char *crypt_get_cipher(struct crypt_device *cd);
564 const char *crypt_get_cipher_mode(struct crypt_device *cd);
565 const char *crypt_get_uuid(struct crypt_device *cd);
566 const char *crypt_get_device_name(struct crypt_device *cd);
567 uint64_t crypt_get_data_offset(struct crypt_device *cd);
568 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
569 int crypt_get_volume_key_size(struct crypt_device *cd);
572 * Get information about particular key slot
574 * Returns value defined by crypt_keyslot_info.
576 * @cd - crypt device handle
577 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
583 CRYPT_SLOT_ACTIVE_LAST
584 } crypt_keyslot_info;
585 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
588 * Backup header and keyslots to file
590 * Returns 0 on success or negative errno value otherwise.
592 * @cd - crypt device handle
593 * @requested_type - type of header to backup
594 * @backup_file - file to backup header to
596 int crypt_header_backup(struct crypt_device *cd,
597 const char *requested_type,
598 const char *backup_file);
601 * Restore header and keyslots from backup file
603 * Returns 0 on success or negative errno value otherwise.
605 * @cd - crypt device handle
606 * @requested_type - type of header to restore
607 * @backup_file - file to restore header from
609 int crypt_header_restore(struct crypt_device *cd,
610 const char *requested_type,
611 const char *backup_file);
614 * Receives last reported error
616 * @buf - buffef for message
617 * @size - size of buffer
619 * Note that this is old API function using global context.
620 * All error messages are reported also through log callback.
622 void crypt_get_error(char *buf, size_t size);
625 * Get directory where mapped crypt devices are created
627 const char *crypt_get_dir(void);
630 * Set library debug level
632 #define CRYPT_DEBUG_ALL -1
633 #define CRYPT_DEBUG_NONE 0
634 void crypt_set_debug_level(int level);
639 #endif /* _LIBCRYPTSETUP_H */