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 * Calback should return length of password in buffer
89 * or negative errno value in case of error.
91 * - Note that if this function is defined, verify option is ignored
92 * (caller which provided callback is responsible for password verification)
93 * - Only zero terminated passwords can be entered this way, for complex
94 * use API functions directly.
95 * - Maximal length of password is limited to @length-1 (minimal 511 chars)
97 void crypt_set_password_callback(struct crypt_device *cd,
98 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
102 * Various crypt device parameters
104 * @cd - crypt device handle
105 * @timeout - timeout in seconds for password entry if compiled-in function used
106 * @password_retry - number of tries for password if not verified
107 * @iteration_time - iteration time for LUKS header in miliseconds
108 * @password_verify - for compiled-in password query always verify passwords twice
110 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
111 void crypt_set_password_retry(struct crypt_device *cd, int tries);
112 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
113 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
116 * Set which RNG (random number generator) is used for generating long term key
117 * @cd - crypt device handle
118 * @rng_type - kernel random number generator to use
120 * CRYPT_RNG_URANDOM - use /dev/urandom
121 * CRYPT_RNG_RANDOM - use /dev/random (waits if no entropy in system)
123 #define CRYPT_RNG_URANDOM 0
124 #define CRYPT_RNG_RANDOM 1
125 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
128 * Get which RNG (random number generator) is used for generating long term key
130 * Returns RNG type on success or negative errno value otherwise.
132 * @cd - crypt device handle
134 int crypt_get_rng_type(struct crypt_device *cd);
137 * Helper to lock/unlock memory to avoid swap sensitive data to disk
139 * @cd - crypt device handle, can be NULL
140 * @lock - 0 to unloct otherwise lock memory
142 * Return value indicates that memory is locked (function can be called multiple times).
143 * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
145 int crypt_memory_lock(struct crypt_device *cd, int lock);
147 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
148 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
149 #define CRYPT_LOOPAES "LOOPAES" /* loop-AES compatibility mode */
154 * @cd - crypt device handle
156 * Returns string according to device type or NULL if not known.
158 const char *crypt_get_type(struct crypt_device *cd);
160 struct crypt_params_plain {
161 const char *hash; /* password hash function */
162 uint64_t offset; /* offset in sectors */
163 uint64_t skip; /* IV initilisation sector */
166 struct crypt_params_luks1 {
167 const char *hash; /* hash used in LUKS header */
168 size_t data_alignment; /* in sectors, data offset is multiple of this */
171 struct crypt_params_loopaes {
172 const char *hash; /* key hash function */
173 uint64_t offset; /* offset in sectors */
174 uint64_t skip; /* IV initilisation sector */
177 * Create (format) new crypt device (and possible header on-disk) but not activates it.
179 * Returns 0 on success or negative errno value otherwise.
181 * @cd - crypt device handle
182 * @type - type of device (optional params struct must be of this type)
183 * @cipher - (e.g. "aes")
184 * @cipher_mode - including IV specification (e.g. "xts-plain")
185 * @uuid - requested UUID or NULL if it should be generated
186 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
187 * @volume_key_size - size of volume key in bytes.
188 * @params - crypt type specific parameters
190 * Note that crypt_format does not enable any keyslot, but it stores volume key internally
191 * and subsequent crypt_keyslot_add_* calls can be used.
193 int crypt_format(struct crypt_device *cd,
196 const char *cipher_mode,
198 const char *volume_key,
199 size_t volume_key_size,
203 * Set new UUID for already existing device (if format supports it)
205 * Returns 0 on success or negative errno value otherwise.
207 * @cd - crypt device handle
208 * @uuid - requested UUID or NULL if it should be generated
210 int crypt_set_uuid(struct crypt_device *cd,
214 * Load crypt device parameters from on-disk header
216 * Returns 0 on success or negative errno value otherwise.
218 * @cd - crypt device handle
219 * @requested_type - use NULL for all known
220 * @params - crypt type specific parameters
222 int crypt_load(struct crypt_device *cd,
223 const char *requested_type,
227 * Resize crypt device
229 * Returns 0 on success or negative errno value otherwise.
231 * @cd - crypt device handle
232 * @name - name of device to resize
233 * @new_size - new device size in sectors or 0 to use underlying device size
235 int crypt_resize(struct crypt_device *cd,
240 * Suspends crypt device.
242 * Returns 0 on success or negative errno value otherwise.
244 * @cd - crypt device handle, can be NULL
245 * @name - name of device to suspend
247 int crypt_suspend(struct crypt_device *cd,
251 * Resumes crypt device using passphrase.
253 * Returns unlocked key slot number or negative errno otherwise.
255 * @cd - crypt device handle
256 * @name - name of device to resume
257 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
258 * @passphrase - passphrase used to unlock volume key, NULL for query
259 * @passphrase_size - size of @passphrase (binary data)
261 int crypt_resume_by_passphrase(struct crypt_device *cd,
264 const char *passphrase,
265 size_t passphrase_size);
268 * Resumes crypt device using key file.
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 * @keyfile - key file used to unlock volume key, NULL for passphrase query
276 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
278 int crypt_resume_by_keyfile(struct crypt_device *cd,
282 size_t keyfile_size);
285 * Releases crypt device context and used memory.
287 * @cd - crypt device handle
289 void crypt_free(struct crypt_device *cd);
292 * Add key slot using provided passphrase
294 * Returns allocated key slot number or negative errno otherwise.
296 * @cd - crypt device handle
297 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
298 * @passphrase - passphrase used to unlock volume key, NULL for query
299 * @passphrase_size - size of @passphrase (binary data)
300 * @new_passphrase - passphrase for new keyslot, NULL for query
301 * @new_passphrase_size - size of @new_passphrase (binary data)
303 #define CRYPT_ANY_SLOT -1
304 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
306 const char *passphrase,
307 size_t passphrase_size,
308 const char *new_passphrase,
309 size_t new_passphrase_size);
312 * Get number of keyslots supported for device type.
314 * Returns slot count or negative errno otherwise if device
315 * doesn't not support keyslots.
317 * @type - crypt device type
319 int crypt_keyslot_max(const char *type);
322 * Add key slot using provided key file path
324 * Returns allocated key slot number or negative errno otherwise.
326 * @cd - crypt device handle
327 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
328 * @keyfile - key file used to unlock volume key, NULL for passphrase query
329 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
330 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
331 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
333 * Note that @keyfile can be "-" for STDIN
335 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
339 const char *new_keyfile,
340 size_t new_keyfile_size);
343 * Add key slot using provided volume key
345 * Returns allocated key slot number or negative errno otherwise.
347 * @cd - crypt device handle
348 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
349 * @volume_key - provided volume key or NULL if used after crypt_format
350 * @volume_key_size - size of @volume_key
351 * @passphrase - passphrase for new keyslot, NULL for query
352 * @passphrase_size - size of @passphrase
354 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
356 const char *volume_key,
357 size_t volume_key_size,
358 const char *passphrase,
359 size_t passphrase_size);
362 * Destroy (and disable) key slot
364 * Returns 0 on success or negative errno value otherwise.
366 * @cd - crypt device handle
367 * @keyslot - requested key slot to destroy
369 * Note that there is no passphrase verification used.
371 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
376 #define CRYPT_ACTIVATE_READONLY (1 << 0)
377 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
380 * Active device runtime attributes
382 struct crypt_active_device {
383 uint64_t offset; /* offset in sectors */
384 uint64_t iv_offset; /* IV initilisation sector */
385 uint64_t size; /* active device size */
386 uint32_t flags; /* activation flags */
390 * Receives runtime attributes of active crypt device
392 * Returns 0 on success or negative errno value otherwise.
394 * @cd - crypt device handle (can be NULL)
395 * @name - name of active device
396 * @cad - preallocated active device attributes to fill
398 int crypt_get_active_device(struct crypt_device *cd,
400 struct crypt_active_device *cad);
403 * Activate device or check passphrase
405 * Returns unlocked key slot number or negative errno otherwise.
407 * @cd - crypt device handle
408 * @name - name of device to create, if NULL only check passphrase
409 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
410 * @passphrase - passphrase used to unlock volume key, NULL for query
411 * @passphrase_size - size of @passphrase
412 * @flags - activation flags
414 int crypt_activate_by_passphrase(struct crypt_device *cd,
417 const char *passphrase,
418 size_t passphrase_size,
422 * Activate device or check using key file
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 keyfile
428 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
429 * @keyfile - key file used to unlock volume key
430 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
431 * @flags - activation flags
433 int crypt_activate_by_keyfile(struct crypt_device *cd,
441 * Activate device using provided volume key
443 * Returns 0 on success or negative errno value otherwise.
445 * @cd - crypt device handle
446 * @name - name of device to create, if NULL only check volume key
447 * @volume_key - provided volume key (or NULL to use internal)
448 * @volume_key_size - size of @volume_key
449 * @flags - activation flags
451 * If NULL is used for volume_key, device has to be initialized
452 * by previous operation (like crypt_format() or crypt_init_by_name())
454 int crypt_activate_by_volume_key(struct crypt_device *cd,
456 const char *volume_key,
457 size_t volume_key_size,
461 * Deactivate crypt device
463 * @cd - crypt device handle, can be NULL
464 * @name - name of device to deactivate
466 int crypt_deactivate(struct crypt_device *cd, const char *name);
469 * Get volume key from of crypt device
471 * Returns unlocked key slot number or negative errno otherwise.
473 * @cd - crypt device handle
474 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
475 * @volume_key - buffer for volume key
476 * @volume_key_size - on input, size of buffer @volume_key,
477 * on output size of @volume_key
478 * @passphrase - passphrase used to unlock volume key, NULL for query
479 * @passphrase_size - size of @passphrase
481 int crypt_volume_key_get(struct crypt_device *cd,
484 size_t *volume_key_size,
485 const char *passphrase,
486 size_t passphrase_size);
489 * Verify that provided volume key is valid for crypt device
491 * Returns 0 on success or negative errno value otherwise.
493 * @cd - crypt device handle
494 * @volume_key - provided volume key
495 * @volume_key_size - size of @volume_key
497 int crypt_volume_key_verify(struct crypt_device *cd,
498 const char *volume_key,
499 size_t volume_key_size);
502 * Get status info about device name
504 * Returns value defined by crypt_status_info.
506 * @cd - crypt device handle, can be NULL
507 * @name -crypt device name
509 * CRYPT_INACTIVE - no such mapped device
510 * CRYPT_ACTIVE - device is active
511 * CRYPT_BUSY - device is active and has open count > 0
519 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
522 * Dump text-formatted information about crypt device to log output
524 * Returns 0 on success or negative errno value otherwise.
526 * @cd - crypt device handle
528 int crypt_dump(struct crypt_device *cd);
531 * Various crypt device info functions
533 * @cd - crypt device handle
535 * cipher - used cipher, e.g. "aes" or NULL otherwise
536 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
537 * uuid - device UUID or NULL if not set
538 * device_name - underlying device name or NULL if not yet set
539 * data_offset - device offset in sectors where real data starts on underlying device)
540 * volume_key_size - size (in bytes) of volume key for crypt device
542 const char *crypt_get_cipher(struct crypt_device *cd);
543 const char *crypt_get_cipher_mode(struct crypt_device *cd);
544 const char *crypt_get_uuid(struct crypt_device *cd);
545 const char *crypt_get_device_name(struct crypt_device *cd);
546 uint64_t crypt_get_data_offset(struct crypt_device *cd);
547 int crypt_get_volume_key_size(struct crypt_device *cd);
550 * Get information about particular key slot
552 * Returns value defined by crypt_keyslot_info.
554 * @cd - crypt device handle
555 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
561 CRYPT_SLOT_ACTIVE_LAST
562 } crypt_keyslot_info;
563 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
566 * Backup header and keyslots to file
568 * Returns 0 on success or negative errno value otherwise.
570 * @cd - crypt device handle
571 * @requested_type - type of header to backup
572 * @backup_file - file to backup header to
574 int crypt_header_backup(struct crypt_device *cd,
575 const char *requested_type,
576 const char *backup_file);
579 * Restore header and keyslots from backup file
581 * Returns 0 on success or negative errno value otherwise.
583 * @cd - crypt device handle
584 * @requested_type - type of header to restore
585 * @backup_file - file to restore header from
587 int crypt_header_restore(struct crypt_device *cd,
588 const char *requested_type,
589 const char *backup_file);
592 * Receives last reported error
594 * @buf - buffef for message
595 * @size - size of buffer
597 * Note that this is old API function using global context.
598 * All error messages are reported also through log callback.
600 void crypt_get_error(char *buf, size_t size);
603 * Get directory where mapped crypt devices are created
605 const char *crypt_get_dir(void);
608 * Set library debug level
610 #define CRYPT_DEBUG_ALL -1
611 #define CRYPT_DEBUG_NONE 0
612 void crypt_set_debug_level(int level);
615 * OLD DEPRECATED API **********************************
617 * Provided only for backward compatibility.
620 struct interface_callbacks {
621 int (*yesDialog)(char *msg);
622 void (*log)(int level, char *msg);
625 #define CRYPT_FLAG_VERIFY (1 << 0)
626 #define CRYPT_FLAG_READONLY (1 << 1)
627 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE (1 << 2)
628 #define CRYPT_FLAG_VERIFY_ON_DELKEY (1 << 3)
629 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
631 struct crypt_options {
638 const char *passphrase;
640 const char *key_file;
641 const char *new_key_file;
650 uint64_t iteration_time;
653 uint64_t align_payload;
656 struct interface_callbacks *icb;
659 int crypt_create_device(struct crypt_options *options);
660 int crypt_update_device(struct crypt_options *options);
661 int crypt_resize_device(struct crypt_options *options);
662 int crypt_query_device(struct crypt_options *options);
663 int crypt_remove_device(struct crypt_options *options);
664 int crypt_luksFormat(struct crypt_options *options);
665 int crypt_luksOpen(struct crypt_options *options);
666 int crypt_luksKillSlot(struct crypt_options *options);
667 int crypt_luksRemoveKey(struct crypt_options *options);
668 int crypt_luksAddKey(struct crypt_options *options);
669 int crypt_luksUUID(struct crypt_options *options);
670 int crypt_isLuks(struct crypt_options *options);
671 int crypt_luksDump(struct crypt_options *options);
673 void crypt_put_options(struct crypt_options *options);
678 #endif /* _LIBCRYPTSETUP_H */