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 */
188 struct crypt_params_loopaes {
189 const char *hash; /* key hash function */
190 uint64_t offset; /* offset in sectors */
191 uint64_t skip; /* IV offset / initialisation sector */
194 * Create (format) new crypt device (and possible header on-disk) but not activates it.
196 * Returns 0 on success or negative errno value otherwise.
198 * @cd - crypt device handle
199 * @type - type of device (optional params struct must be of this type)
200 * @cipher - (e.g. "aes")
201 * @cipher_mode - including IV specification (e.g. "xts-plain")
202 * @uuid - requested UUID or NULL if it should be generated
203 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
204 * @volume_key_size - size of volume key in bytes.
205 * @params - crypt type specific parameters
207 * Note that crypt_format does not enable any keyslot, but it stores volume key internally
208 * and subsequent crypt_keyslot_add_* calls can be used.
210 int crypt_format(struct crypt_device *cd,
213 const char *cipher_mode,
215 const char *volume_key,
216 size_t volume_key_size,
220 * Set new UUID for already existing device (if format supports it)
222 * Returns 0 on success or negative errno value otherwise.
224 * @cd - crypt device handle
225 * @uuid - requested UUID or NULL if it should be generated
227 int crypt_set_uuid(struct crypt_device *cd,
231 * Load crypt device parameters from on-disk header
233 * Returns 0 on success or negative errno value otherwise.
235 * @cd - crypt device handle
236 * @requested_type - use NULL for all known
237 * @params - crypt type specific parameters
239 int crypt_load(struct crypt_device *cd,
240 const char *requested_type,
244 * Resize crypt device
246 * Returns 0 on success or negative errno value otherwise.
248 * @cd - crypt device handle
249 * @name - name of device to resize
250 * @new_size - new device size in sectors or 0 to use underlying device size
252 int crypt_resize(struct crypt_device *cd,
257 * Suspends crypt device.
259 * Returns 0 on success or negative errno value otherwise.
261 * @cd - crypt device handle, can be NULL
262 * @name - name of device to suspend
264 int crypt_suspend(struct crypt_device *cd,
268 * Resumes crypt device using passphrase.
270 * Returns unlocked key slot number or negative errno otherwise.
272 * @cd - crypt device handle
273 * @name - name of device to resume
274 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
275 * @passphrase - passphrase used to unlock volume key, NULL for query
276 * @passphrase_size - size of @passphrase (binary data)
278 int crypt_resume_by_passphrase(struct crypt_device *cd,
281 const char *passphrase,
282 size_t passphrase_size);
285 * Resumes crypt device using key file.
287 * Returns unlocked key slot number or negative errno otherwise.
289 * @cd - crypt device handle
290 * @name - name of device to resume
291 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
292 * @keyfile - key file used to unlock volume key, NULL for passphrase query
293 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
295 int crypt_resume_by_keyfile(struct crypt_device *cd,
299 size_t keyfile_size);
302 * Releases crypt device context and used memory.
304 * @cd - crypt device handle
306 void crypt_free(struct crypt_device *cd);
309 * Add key slot using provided passphrase
311 * Returns allocated key slot number or negative errno otherwise.
313 * @cd - crypt device handle
314 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
315 * @passphrase - passphrase used to unlock volume key, NULL for query
316 * @passphrase_size - size of @passphrase (binary data)
317 * @new_passphrase - passphrase for new keyslot, NULL for query
318 * @new_passphrase_size - size of @new_passphrase (binary data)
320 #define CRYPT_ANY_SLOT -1
321 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
323 const char *passphrase,
324 size_t passphrase_size,
325 const char *new_passphrase,
326 size_t new_passphrase_size);
329 * Get number of keyslots supported for device type.
331 * Returns slot count or negative errno otherwise if device
332 * doesn't not support keyslots.
334 * @type - crypt device type
336 int crypt_keyslot_max(const char *type);
339 * Add key slot using provided key file path
341 * Returns allocated key slot number or negative errno otherwise.
343 * @cd - crypt device handle
344 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
345 * @keyfile - key file used to unlock volume key, NULL for passphrase query
346 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
347 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
348 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
350 * Note that @keyfile can be "-" for STDIN
352 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
356 const char *new_keyfile,
357 size_t new_keyfile_size);
360 * Add key slot using provided volume key
362 * Returns allocated key slot number or negative errno otherwise.
364 * @cd - crypt device handle
365 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
366 * @volume_key - provided volume key or NULL if used after crypt_format
367 * @volume_key_size - size of @volume_key
368 * @passphrase - passphrase for new keyslot, NULL for query
369 * @passphrase_size - size of @passphrase
371 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
373 const char *volume_key,
374 size_t volume_key_size,
375 const char *passphrase,
376 size_t passphrase_size);
379 * Destroy (and disable) key slot
381 * Returns 0 on success or negative errno value otherwise.
383 * @cd - crypt device handle
384 * @keyslot - requested key slot to destroy
386 * Note that there is no passphrase verification used.
388 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
393 #define CRYPT_ACTIVATE_READONLY (1 << 0)
394 #define CRYPT_ACTIVATE_NO_UUID (1 << 1) /* ignored */
395 #define CRYPT_ACTIVATE_SHARED (1 << 2)
396 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3) /* enable discards aka TRIM */
399 * Active device runtime attributes
401 struct crypt_active_device {
402 uint64_t offset; /* offset in sectors */
403 uint64_t iv_offset; /* IV initilisation sector */
404 uint64_t size; /* active device size */
405 uint32_t flags; /* activation flags */
409 * Receives runtime attributes of active crypt device
411 * Returns 0 on success or negative errno value otherwise.
413 * @cd - crypt device handle (can be NULL)
414 * @name - name of active device
415 * @cad - preallocated active device attributes to fill
417 int crypt_get_active_device(struct crypt_device *cd,
419 struct crypt_active_device *cad);
422 * Activate device or check passphrase
424 * Returns unlocked key slot number or negative errno otherwise.
426 * @cd - crypt device handle
427 * @name - name of device to create, if NULL only check passphrase
428 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
429 * @passphrase - passphrase used to unlock volume key, NULL for query
430 * @passphrase_size - size of @passphrase
431 * @flags - activation flags
433 int crypt_activate_by_passphrase(struct crypt_device *cd,
436 const char *passphrase,
437 size_t passphrase_size,
441 * Activate device or check using key file
443 * Returns unlocked key slot number or negative errno otherwise.
445 * @cd - crypt device handle
446 * @name - name of device to create, if NULL only check keyfile
447 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
448 * @keyfile - key file used to unlock volume key
449 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
450 * @flags - activation flags
452 int crypt_activate_by_keyfile(struct crypt_device *cd,
460 * Activate device using provided volume key
462 * Returns 0 on success or negative errno value otherwise.
464 * @cd - crypt device handle
465 * @name - name of device to create, if NULL only check volume key
466 * @volume_key - provided volume key (or NULL to use internal)
467 * @volume_key_size - size of @volume_key
468 * @flags - activation flags
470 * If NULL is used for volume_key, device has to be initialized
471 * by previous operation (like crypt_format() or crypt_init_by_name())
473 int crypt_activate_by_volume_key(struct crypt_device *cd,
475 const char *volume_key,
476 size_t volume_key_size,
480 * Deactivate crypt device
482 * @cd - crypt device handle, can be NULL
483 * @name - name of device to deactivate
485 int crypt_deactivate(struct crypt_device *cd, const char *name);
488 * Get volume key from of crypt device
490 * Returns unlocked key slot number or negative errno otherwise.
492 * @cd - crypt device handle
493 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
494 * @volume_key - buffer for volume key
495 * @volume_key_size - on input, size of buffer @volume_key,
496 * on output size of @volume_key
497 * @passphrase - passphrase used to unlock volume key, NULL for query
498 * @passphrase_size - size of @passphrase
500 int crypt_volume_key_get(struct crypt_device *cd,
503 size_t *volume_key_size,
504 const char *passphrase,
505 size_t passphrase_size);
508 * Verify that provided volume key is valid for crypt device
510 * Returns 0 on success or negative errno value otherwise.
512 * @cd - crypt device handle
513 * @volume_key - provided volume key
514 * @volume_key_size - size of @volume_key
516 int crypt_volume_key_verify(struct crypt_device *cd,
517 const char *volume_key,
518 size_t volume_key_size);
521 * Get status info about device name
523 * Returns value defined by crypt_status_info.
525 * @cd - crypt device handle, can be NULL
526 * @name -crypt device name
528 * CRYPT_INACTIVE - no such mapped device
529 * CRYPT_ACTIVE - device is active
530 * CRYPT_BUSY - device is active and has open count > 0
538 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
541 * Dump text-formatted information about crypt device to log output
543 * Returns 0 on success or negative errno value otherwise.
545 * @cd - crypt device handle
547 int crypt_dump(struct crypt_device *cd);
550 * Various crypt device info functions
552 * @cd - crypt device handle
554 * cipher - used cipher, e.g. "aes" or NULL otherwise
555 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
556 * uuid - device UUID or NULL if not set
557 * device_name - underlying device name or NULL if not yet set
558 * data_offset - device offset in sectors where real data starts on underlying device)
559 * iv_offset - IV offset in sectors (skip)
560 * volume_key_size - size (in bytes) of volume key for crypt device
562 const char *crypt_get_cipher(struct crypt_device *cd);
563 const char *crypt_get_cipher_mode(struct crypt_device *cd);
564 const char *crypt_get_uuid(struct crypt_device *cd);
565 const char *crypt_get_device_name(struct crypt_device *cd);
566 uint64_t crypt_get_data_offset(struct crypt_device *cd);
567 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
568 int crypt_get_volume_key_size(struct crypt_device *cd);
571 * Get information about particular key slot
573 * Returns value defined by crypt_keyslot_info.
575 * @cd - crypt device handle
576 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
582 CRYPT_SLOT_ACTIVE_LAST
583 } crypt_keyslot_info;
584 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
587 * Backup header and keyslots to file
589 * Returns 0 on success or negative errno value otherwise.
591 * @cd - crypt device handle
592 * @requested_type - type of header to backup
593 * @backup_file - file to backup header to
595 int crypt_header_backup(struct crypt_device *cd,
596 const char *requested_type,
597 const char *backup_file);
600 * Restore header and keyslots from backup file
602 * Returns 0 on success or negative errno value otherwise.
604 * @cd - crypt device handle
605 * @requested_type - type of header to restore
606 * @backup_file - file to restore header from
608 int crypt_header_restore(struct crypt_device *cd,
609 const char *requested_type,
610 const char *backup_file);
613 * Receives last reported error
615 * @buf - buffef for message
616 * @size - size of buffer
618 * Note that this is old API function using global context.
619 * All error messages are reported also through log callback.
621 void crypt_get_error(char *buf, size_t size);
624 * Get directory where mapped crypt devices are created
626 const char *crypt_get_dir(void);
629 * Set library debug level
631 #define CRYPT_DEBUG_ALL -1
632 #define CRYPT_DEBUG_NONE 0
633 void crypt_set_debug_level(int level);
638 #endif /* _LIBCRYPTSETUP_H */