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 check if provided device exists.
28 * Returns 0 on success or negative errno value otherwise.
30 * @cd - crypt device handle
31 * @name - name of active crypt device
33 int crypt_init_by_name(struct crypt_device **cd, const char *name);
38 * @cd - crypt device handle (can be NULL to set default log function)
39 * @usrptr - provided identification in callback
40 * @level - log level below (debug messages can uses other levels)
43 #define CRYPT_LOG_NORMAL 0
44 #define CRYPT_LOG_ERROR 1
45 #define CRYPT_LOG_VERBOSE 2
46 #define CRYPT_LOG_DEBUG -1 /* always on stdout */
47 void crypt_set_log_callback(struct crypt_device *cd,
48 void (*log)(int level, const char *msg, void *usrptr),
52 * Log message through log function.
54 * @cd - crypt device handle
58 void crypt_log(struct crypt_device *cd, int level, const char *msg);
61 * Set confirmation callback (yes/no)
63 * If code need confirmation (like deleting last key slot) this function
64 * is called. If not defined, everything is confirmed.
66 * Calback should return 0 if operation is declined, other values mean accepted.
68 * @cd - crypt device handle
69 * @usrptr - provided identification in callback
70 * @msg - Message for user to confirm
72 void crypt_set_confirm_callback(struct crypt_device *cd,
73 int (*confirm)(const char *msg, void *usrptr),
77 * Set password query callback.
79 * If code need _interactive_ query for password, this callback is called.
80 * If not defined, compiled-in default is called (uses terminal input).
82 * @cd - crypt device handle
83 * @usrptr - provided identification in callback
84 * @msg - Message for user
85 * @buf - buffer for password
86 * @length - size of buffer
88 * - Note that if this function is defined, verify option is ignored
89 * (caller which provided callback is responsible for password verification)
90 * - Only zero terminated passwords can be entered this way, for complex
91 * use API functions directly.
92 * - Maximal length of password is limited to @length-1 (minimal 511 chars)
94 void crypt_set_password_callback(struct crypt_device *cd,
95 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
99 * Various crypt device parameters
101 * @cd - crypt device handle
102 * @timeout - timeout in seconds for password entry if compiled-in function used
103 * @password_retry - number of tries for password if not verified
104 * @iteration_time - iteration time for LUKS header in miliseconds
105 * @password_verify - for compiled-in password query always verify passwords twice
107 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
108 void crypt_set_password_retry(struct crypt_device *cd, int tries);
109 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
110 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
113 * Set which RNG (random number generator) is used for generating long term key
114 * @cd - crypt device handle
115 * @rng_type - kernel random number generator to use
117 * CRYPT_RNG_URANDOM - use /dev/urandom
118 * CRYPT_RNG_RANDOM - use /dev/random (waits if no entropy in system)
120 #define CRYPT_RNG_URANDOM 0
121 #define CRYPT_RNG_RANDOM 1
122 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
125 * Get which RNG (random number generator) is used for generating long term key
127 * Returns RNG type on success or negative errno value otherwise.
129 * @cd - crypt device handle
131 int crypt_get_rng_type(struct crypt_device *cd);
134 * Helper to lock/unlock memory to avoid swap sensitive data to disk
136 * @cd - crypt device handle, can be NULL
137 * @lock - 0 to unloct otherwise lock memory
139 * Return value indicates that memory is locked (function can be called multiple times).
140 * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
142 int crypt_memory_lock(struct crypt_device *cd, int lock);
144 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
145 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
150 * @cd - crypt device handle
152 * Returns string according to device type or NULL if not known.
154 const char *crypt_get_type(struct crypt_device *cd);
156 struct crypt_params_plain {
157 const char *hash; /* password hash function */
158 uint64_t offset; /* offset in sectors */
159 uint64_t skip; /* IV initilisation sector */
162 struct crypt_params_luks1 {
163 const char *hash; /* hash used in LUKS header */
164 size_t data_alignment; /* in sectors, data offset is multiple of this */
168 * Create (format) new crypt device (and possible header on-disk) but not activates it.
170 * Returns 0 on success or negative errno value otherwise.
172 * @cd - crypt device handle
173 * @type - type of device (optional params struct must be of this type)
174 * @cipher - (e.g. "aes")
175 * @cipher_mode - including IV specification (e.g. "xts-plain")
176 * @uuid - requested UUID or NULL if it should be generated
177 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
178 * @volume_key_size - size of volume key in bytes.
179 * @params - crypt type specific parameters
181 * Note that crypt_format does not enable any keyslot, but it stores volume key internally
182 * and subsequent crypt_keyslot_add_* calls can be used.
184 int crypt_format(struct crypt_device *cd,
187 const char *cipher_mode,
189 const char *volume_key,
190 size_t volume_key_size,
194 * Set new UUID for already existing device (if format supports it)
196 * Returns 0 on success or negative errno value otherwise.
198 * @cd - crypt device handle
199 * @uuid - requested UUID or NULL if it should be generated
201 int crypt_set_uuid(struct crypt_device *cd,
205 * Load crypt device parameters from on-disk header
207 * Returns 0 on success or negative errno value otherwise.
209 * @cd - crypt device handle
210 * @requested_type - use NULL for all known
211 * @params - crypt type specific parameters
213 int crypt_load(struct crypt_device *cd,
214 const char *requested_type,
218 * Resize crypt device
220 * Returns 0 on success or negative errno value otherwise.
222 * @cd - crypt device handle
223 * @name - name of device to resize
224 * @new_size - new device size in sectors or 0 to use underlying device size
226 int crypt_resize(struct crypt_device *cd,
231 * Suspends crypt device.
233 * Returns 0 on success or negative errno value otherwise.
235 * @cd - crypt device handle, can be NULL
236 * @name - name of device to suspend
238 int crypt_suspend(struct crypt_device *cd,
242 * Resumes crypt device using passphrase.
244 * Returns unlocked key slot number or negative errno otherwise.
246 * @cd - crypt device handle
247 * @name - name of device to resume
248 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
249 * @passphrase - passphrase used to unlock volume key, NULL for query
250 * @passphrase_size - size of @passphrase (binary data)
252 int crypt_resume_by_passphrase(struct crypt_device *cd,
255 const char *passphrase,
256 size_t passphrase_size);
259 * Resumes crypt device using key file.
261 * Returns unlocked key slot number or negative errno otherwise.
263 * @cd - crypt device handle
264 * @name - name of device to resume
265 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
266 * @keyfile - key file used to unlock volume key, NULL for passphrase query
267 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
269 int crypt_resume_by_keyfile(struct crypt_device *cd,
273 size_t keyfile_size);
276 * Releases crypt device context and used memory.
278 * @cd - crypt device handle
280 void crypt_free(struct crypt_device *cd);
283 * Add key slot using provided passphrase
285 * Returns allocated key slot number or negative errno otherwise.
287 * @cd - crypt device handle
288 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
289 * @passphrase - passphrase used to unlock volume key, NULL for query
290 * @passphrase_size - size of @passphrase (binary data)
291 * @new_passphrase - passphrase for new keyslot, NULL for query
292 * @new_passphrase_size - size of @new_passphrase (binary data)
294 #define CRYPT_ANY_SLOT -1
295 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
297 const char *passphrase,
298 size_t passphrase_size,
299 const char *new_passphrase,
300 size_t new_passphrase_size);
303 * Get number of keyslots supported for device type.
305 * Returns slot count or negative errno otherwise if device
306 * doesn't not support keyslots.
308 * @type - crypt device type
310 int crypt_keyslot_max(const char *type);
313 * Add key slot using provided key file path
315 * Returns allocated key slot number or negative errno otherwise.
317 * @cd - crypt device handle
318 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
319 * @keyfile - key file used to unlock volume key, NULL for passphrase query
320 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
321 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
322 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
324 * Note that @keyfile can be "-" for STDIN
326 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
330 const char *new_keyfile,
331 size_t new_keyfile_size);
334 * Add key slot using provided volume key
336 * Returns allocated key slot number or negative errno otherwise.
338 * @cd - crypt device handle
339 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
340 * @volume_key - provided volume key or NULL if used after crypt_format
341 * @volume_key_size - size of @volume_key
342 * @passphrase - passphrase for new keyslot, NULL for query
343 * @passphrase_size - size of @passphrase
345 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
347 const char *volume_key,
348 size_t volume_key_size,
349 const char *passphrase,
350 size_t passphrase_size);
353 * Destroy (and disable) key slot
355 * Returns 0 on success or negative errno value otherwise.
357 * @cd - crypt device handle
358 * @keyslot - requested key slot to destroy
360 * Note that there is no passphrase verification used.
362 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
367 #define CRYPT_ACTIVATE_READONLY (1 << 0)
368 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
371 * Active device runtime attributes
373 struct crypt_active_device {
374 uint64_t offset; /* offset in sectors */
375 uint64_t iv_offset; /* IV initilisation sector */
376 uint64_t size; /* active device size */
377 uint32_t flags; /* activation flags */
381 * Receives runtime attributes of active crypt device
383 * Returns 0 on success or negative errno value otherwise.
385 * @cd - crypt device handle (can be NULL)
386 * @name - name of active device
387 * @cad - preallocated active device attributes to fill
389 int crypt_get_active_device(struct crypt_device *cd,
391 struct crypt_active_device *cad);
394 * Activate device or check passphrase
396 * Returns unlocked key slot number or negative errno otherwise.
398 * @cd - crypt device handle
399 * @name - name of device to create, if NULL only check passphrase
400 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
401 * @passphrase - passphrase used to unlock volume key, NULL for query
402 * @passphrase_size - size of @passphrase
403 * @flags - activation flags
405 int crypt_activate_by_passphrase(struct crypt_device *cd,
408 const char *passphrase,
409 size_t passphrase_size,
413 * Activate device or check using key file
415 * Returns unlocked key slot number or negative errno otherwise.
417 * @cd - crypt device handle
418 * @name - name of device to create, if NULL only check keyfile
419 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
420 * @keyfile - key file used to unlock volume key
421 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
422 * @flags - activation flags
424 int crypt_activate_by_keyfile(struct crypt_device *cd,
432 * Activate device using provided volume key
434 * Returns 0 on success or negative errno value otherwise.
436 * @cd - crypt device handle
437 * @name - name of device to create, if NULL only check volume key
438 * @volume_key - provided volume key (or NULL to use internal)
439 * @volume_key_size - size of @volume_key
440 * @flags - activation flags
442 * If NULL is used for volume_key, device has to be initialized
443 * by previous operation (like crypt_format() or crypt_init_by_name())
445 int crypt_activate_by_volume_key(struct crypt_device *cd,
447 const char *volume_key,
448 size_t volume_key_size,
452 * Deactivate crypt device
454 * @cd - crypt device handle, can be NULL
455 * @name - name of device to deactivate
457 int crypt_deactivate(struct crypt_device *cd, const char *name);
460 * Get volume key from of crypt device
462 * Returns unlocked key slot number or negative errno otherwise.
464 * @cd - crypt device handle
465 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
466 * @volume_key - buffer for volume key
467 * @volume_key_size - on input, size of buffer @volume_key,
468 * on output size of @volume_key
469 * @passphrase - passphrase used to unlock volume key, NULL for query
470 * @passphrase_size - size of @passphrase
472 int crypt_volume_key_get(struct crypt_device *cd,
475 size_t *volume_key_size,
476 const char *passphrase,
477 size_t passphrase_size);
480 * Verify that provided volume key is valid for crypt device
482 * Returns 0 on success or negative errno value otherwise.
484 * @cd - crypt device handle
485 * @volume_key - provided volume key
486 * @volume_key_size - size of @volume_key
488 int crypt_volume_key_verify(struct crypt_device *cd,
489 const char *volume_key,
490 size_t volume_key_size);
493 * Get status info about device name
495 * Returns value defined by crypt_status_info.
497 * @cd - crypt device handle, can be NULL
498 * @name -crypt device name
500 * CRYPT_INACTIVE - no such mapped device
501 * CRYPT_ACTIVE - device is active
502 * CRYPT_BUSY - device is active and has open count > 0
510 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
513 * Dump text-formatted information about crypt device to log output
515 * Returns 0 on success or negative errno value otherwise.
517 * @cd - crypt device handle
519 int crypt_dump(struct crypt_device *cd);
522 * Various crypt device info functions
524 * @cd - crypt device handle
526 * cipher - used cipher, e.g. "aes" or NULL otherwise
527 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
528 * uuid - device UUID or NULL if not set
529 * device_name - underlying device name or NULL if not yet set
530 * data_offset - device offset in sectors where real data starts on underlying device)
531 * volume_key_size - size (in bytes) of volume key for crypt device
533 const char *crypt_get_cipher(struct crypt_device *cd);
534 const char *crypt_get_cipher_mode(struct crypt_device *cd);
535 const char *crypt_get_uuid(struct crypt_device *cd);
536 const char *crypt_get_device_name(struct crypt_device *cd);
537 uint64_t crypt_get_data_offset(struct crypt_device *cd);
538 int crypt_get_volume_key_size(struct crypt_device *cd);
541 * Get information about particular key slot
543 * Returns value defined by crypt_keyslot_info.
545 * @cd - crypt device handle
546 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
552 CRYPT_SLOT_ACTIVE_LAST
553 } crypt_keyslot_info;
554 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
557 * Backup header and keyslots to file
559 * Returns 0 on success or negative errno value otherwise.
561 * @cd - crypt device handle
562 * @requested_type - type of header to backup
563 * @backup_file - file to backup header to
565 int crypt_header_backup(struct crypt_device *cd,
566 const char *requested_type,
567 const char *backup_file);
570 * Restore header and keyslots from backup file
572 * Returns 0 on success or negative errno value otherwise.
574 * @cd - crypt device handle
575 * @requested_type - type of header to restore
576 * @backup_file - file to restore header from
578 int crypt_header_restore(struct crypt_device *cd,
579 const char *requested_type,
580 const char *backup_file);
583 * Receives last reported error
585 * @buf - buffef for message
586 * @size - size of buffer
588 * Note that this is old API function using global context.
589 * All error messages are reported also through log callback.
591 void crypt_get_error(char *buf, size_t size);
594 * Get directory where mapped crypt devices are created
596 const char *crypt_get_dir(void);
599 * Set library debug level
601 #define CRYPT_DEBUG_ALL -1
602 #define CRYPT_DEBUG_NONE 0
603 void crypt_set_debug_level(int level);
606 * OLD DEPRECATED API **********************************
608 * Provided only for backward compatibility.
611 struct interface_callbacks {
612 int (*yesDialog)(char *msg);
613 void (*log)(int level, char *msg);
616 #define CRYPT_FLAG_VERIFY (1 << 0)
617 #define CRYPT_FLAG_READONLY (1 << 1)
618 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE (1 << 2)
619 #define CRYPT_FLAG_VERIFY_ON_DELKEY (1 << 3)
620 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
622 struct crypt_options {
629 const char *passphrase;
631 const char *key_file;
632 const char *new_key_file;
641 uint64_t iteration_time;
644 uint64_t align_payload;
647 struct interface_callbacks *icb;
650 int crypt_create_device(struct crypt_options *options);
651 int crypt_update_device(struct crypt_options *options);
652 int crypt_resize_device(struct crypt_options *options);
653 int crypt_query_device(struct crypt_options *options);
654 int crypt_remove_device(struct crypt_options *options);
655 int crypt_luksFormat(struct crypt_options *options);
656 int crypt_luksOpen(struct crypt_options *options);
657 int crypt_luksKillSlot(struct crypt_options *options);
658 int crypt_luksRemoveKey(struct crypt_options *options);
659 int crypt_luksAddKey(struct crypt_options *options);
660 int crypt_luksUUID(struct crypt_options *options);
661 int crypt_isLuks(struct crypt_options *options);
662 int crypt_luksDump(struct crypt_options *options);
664 void crypt_put_options(struct crypt_options *options);
669 #endif /* _LIBCRYPTSETUP_H */