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 whch provided callback is responsible fo password verification)
90 * - Only zero terminated passwords can be enteted this way, for complex
91 * 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 secons 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 */
147 struct crypt_params_plain {
148 const char *hash; /* password hash function */
149 uint64_t offset; /* offset in sectors */
150 uint64_t skip; /* IV initilisation sector */
153 struct crypt_params_luks1 {
154 const char *hash; /* hash used in LUKS header */
155 size_t data_alignment; /* in sectors, data offset is multiple of this */
159 * Create (format) new crypt device (and possible header on-disk) but not activates it.
161 * Returns 0 on success or negative errno value otherwise.
163 * @cd - crypt device handle
164 * @type - type of device (optional params struct must be of this type)
165 * @cipher - (e.g. "aes")
166 * @cipher_mode - including IV specification (e.g. "xts-plain")
167 * @uuid - requested UUID or NULL if it should be generated
168 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
169 * @volume_key_size - size og volume key in bytes.
170 * @params - crypt type specific parameters
172 * Note that crypt_format do not enable any keyslot, but it stores volume key internally
173 * and subsequent crypt_keyslot_add_* calls can be used.
174 * (It is the only situation when crypt_keyslot_add_* do not require active key slots.)
176 int crypt_format(struct crypt_device *cd,
179 const char *cipher_mode,
181 const char *volume_key,
182 size_t volume_key_size,
186 * Load crypt device parameters from on-disk header
188 * Returns 0 on success or negative errno value otherwise.
190 * @cd - crypt device handle
191 * @requested_type - use NULL for all known
192 * @params - crypt type specific parameters
194 int crypt_load(struct crypt_device *cd,
195 const char *requested_type,
199 * Suspends crypt device.
201 * Returns 0 on success or negative errno value otherwise.
203 * @cd - crypt device handle, can be NULL
204 * @name - name of device to suspend
206 int crypt_suspend(struct crypt_device *cd,
210 * Resumes crypt device using passphrase.
212 * Returns unlocked key slot number or negative errno otherwise.
214 * @cd - crypt device handle
215 * @name - name of device to resume
216 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
217 * @passphrase - passphrase used to unlock volume key, NULL for query
218 * @passphrase_size - size of @passphrase (binary data)
220 int crypt_resume_by_passphrase(struct crypt_device *cd,
223 const char *passphrase,
224 size_t passphrase_size);
227 * Resumes crypt device using key file.
229 * Returns unlocked key slot number or negative errno otherwise.
231 * @cd - crypt device handle
232 * @name - name of device to resume
233 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
234 * @keyfile - key file used to unlock volume key, NULL for passphrase query
235 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
237 int crypt_resume_by_keyfile(struct crypt_device *cd,
241 size_t keyfile_size);
244 * Releases crypt device context and used memory.
246 * @cd - crypt device handle
248 void crypt_free(struct crypt_device *cd);
251 * Add key slot using provided passphrase
253 * Returns allocated key slot number or negative errno otherwise.
255 * @cd - crypt device handle
256 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
257 * @passphrase - passphrase used to unlock volume key, NULL for query
258 * @passphrase_size - size of @passphrase (binary data)
259 * @new_passphrase - passphrase for new keyslot, NULL for query
260 * @new_passphrase_size - size of @new_passphrase (binary data)
262 #define CRYPT_ANY_SLOT -1
263 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
265 const char *passphrase,
266 size_t passphrase_size,
267 const char *new_passphrase,
268 size_t new_passphrase_size);
271 * Add key slot using provided key file path
273 * Returns allocated key slot number or negative errno otherwise.
275 * @cd - crypt device handle
276 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
277 * @keyfile - key file used to unlock volume key, NULL for passphrase query
278 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
279 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
280 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
282 * Note that @keyfile can be "-" for STDIN
284 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
288 const char *new_keyfile,
289 size_t new_keyfile_size);
292 * Add key slot using provided volume key
294 * Returns allocated key slot number or negative errno otherwise.
296 * @cd - crypt device handle
297 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
298 * @volume_key - provided volume key or NULL if used after crypt_format
299 * @volume_key_size - size of @volume_key
300 * @passphrase - passphrase for new keyslot, NULL for query
301 * @passphrase_size - size of @passphrase
303 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
305 const char *volume_key,
306 size_t volume_key_size,
307 const char *passphrase,
308 size_t passphrase_size);
311 * Destroy (and disable) key slot
313 * Returns 0 on success or negative errno value otherwise.
315 * @cd - crypt device handle
316 * @keyslot - requested key slot to destroy
318 * Note that there is no passphrase verification used.
320 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
325 #define CRYPT_ACTIVATE_READONLY (1 << 0)
326 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
329 * Activate device or check passphrase
331 * Returns unlocked key slot number or negative errno otherwise.
333 * @cd - crypt device handle
334 * @name - name of device to create, if NULL only check passphrase
335 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
336 * @passphrase - passphrase used to unlock volume key, NULL for query
337 * @passphrase_size - size of @passphrase
338 * @flags - activation flags
340 int crypt_activate_by_passphrase(struct crypt_device *cd,
343 const char *passphrase,
344 size_t passphrase_size,
348 * Activate device or check using key file
350 * Returns unlocked key slot number or negative errno otherwise.
352 * @cd - crypt device handle
353 * @name - name of device to create, if NULL only check keyfile
354 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
355 * @keyfile - key file used to unlock volume key
356 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
357 * @flags - activation flags
359 int crypt_activate_by_keyfile(struct crypt_device *cd,
367 * Activate device using provided volume key
369 * Returns 0 on success or negative errno value otherwise.
371 * @cd - crypt device handle
372 * @name - name of device to create, if NULL only check volume key
373 * @volume_key - provided volume key
374 * @volume_key_size - size of @volume_key
375 * @flags - activation flags
377 int crypt_activate_by_volume_key(struct crypt_device *cd,
379 const char *volume_key,
380 size_t volume_key_size,
384 * Deactivate crypt device
386 * @cd - crypt device handle, can be NULL
387 * @name - name of device to deactivate
389 int crypt_deactivate(struct crypt_device *cd, const char *name);
392 * Get volume key from of crypt device
394 * Returns unlocked key slot number or negative errno otherwise.
396 * @cd - crypt device handle
397 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
398 * @volume_key - buffer for volume key
399 * @volume_key_size - on input, size of buffer @volume_key,
400 * on output size of @volume_key
401 * @passphrase - passphrase used to unlock volume key, NULL for query
402 * @passphrase_size - size of @passphrase
404 int crypt_volume_key_get(struct crypt_device *cd,
407 size_t *volume_key_size,
408 const char *passphrase,
409 size_t passphrase_size);
412 * Verify that provided volume key is valid for crypt device
414 * Returns 0 on success or negative errno value otherwise.
416 * @cd - crypt device handle
417 * @volume_key - provided volume key
418 * @volume_key_size - size of @volume_key
420 int crypt_volume_key_verify(struct crypt_device *cd,
421 const char *volume_key,
422 size_t volume_key_size);
425 * Get status info about device name
427 * Returns value defined by crypt_status_info.
429 * @cd - crypt device handle, can be NULL
430 * @name -crypt device name
432 * CRYPT_INACTIVE - no such mapped device
433 * CRYPT_ACTIVE - device is active
434 * CRYPT_BUSY - device is active and has open count > 0
442 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
445 * Dump text-formatted information about crypt device to log output
447 * Returns 0 on success or negative errno value otherwise.
449 * @cd - crypt device handle
451 int crypt_dump(struct crypt_device *cd);
454 * Various crypt device info functions
456 * @cd - crypt device handle
458 * cipher - used cipher, e.g. "aes" or NULL otherwise
459 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
460 * uuid - device UUID or NULL if not set
461 * device_name - underlying device name or NULL if not yet set
462 * data_offset - device offset in sectors where real data starts on underlying device)
463 * volume_key_size - size (in bytes) of volume key for crypt device
465 const char *crypt_get_cipher(struct crypt_device *cd);
466 const char *crypt_get_cipher_mode(struct crypt_device *cd);
467 const char *crypt_get_uuid(struct crypt_device *cd);
468 const char *crypt_get_device_name(struct crypt_device *cd);
469 uint64_t crypt_get_data_offset(struct crypt_device *cd);
470 int crypt_get_volume_key_size(struct crypt_device *cd);
473 * Get information about particular key slot
475 * Returns value defined by crypt_keyslot_info.
477 * @cd - crypt device handle
478 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
484 CRYPT_SLOT_ACTIVE_LAST
485 } crypt_keyslot_info;
486 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
489 * Backup header and keyslots to file
491 * Returns 0 on success or negative errno value otherwise.
493 * @cd - crypt device handle
494 * @requested_type - type of header to backup
495 * @backup_file - file to backup header to
497 int crypt_header_backup(struct crypt_device *cd,
498 const char *requested_type,
499 const char *backup_file);
502 * Restore header and keyslots from backup file
504 * Returns 0 on success or negative errno value otherwise.
506 * @cd - crypt device handle
507 * @requested_type - type of header to restore
508 * @backup_file - file to restore header from
510 int crypt_header_restore(struct crypt_device *cd,
511 const char *requested_type,
512 const char *backup_file);
515 * Receives last reported error
517 * @buf - buffef for message
518 * @size - size of buffer
520 * Note that this is old API function using global context.
521 * All error messages are reported also through log callback.
523 void crypt_get_error(char *buf, size_t size);
526 * Get directory where mapped crypt devices are created
528 const char *crypt_get_dir(void);
531 * Set library debug level
533 #define CRYPT_DEBUG_ALL -1
534 #define CRYPT_DEBUG_NONE 0
535 void crypt_set_debug_level(int level);
538 * OLD DEPRECATED API **********************************
540 * Provided only for backward compatibility.
543 struct interface_callbacks {
544 int (*yesDialog)(char *msg);
545 void (*log)(int level, char *msg);
548 #define CRYPT_FLAG_VERIFY (1 << 0)
549 #define CRYPT_FLAG_READONLY (1 << 1)
550 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE (1 << 2)
551 #define CRYPT_FLAG_VERIFY_ON_DELKEY (1 << 3)
552 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
554 struct crypt_options {
561 const char *passphrase;
563 const char *key_file;
564 const char *new_key_file;
573 uint64_t iteration_time;
576 uint64_t align_payload;
579 struct interface_callbacks *icb;
582 int crypt_create_device(struct crypt_options *options);
583 int crypt_update_device(struct crypt_options *options);
584 int crypt_resize_device(struct crypt_options *options);
585 int crypt_query_device(struct crypt_options *options);
586 int crypt_remove_device(struct crypt_options *options);
587 int crypt_luksFormat(struct crypt_options *options);
588 int crypt_luksOpen(struct crypt_options *options);
589 int crypt_luksKillSlot(struct crypt_options *options);
590 int crypt_luksRemoveKey(struct crypt_options *options);
591 int crypt_luksAddKey(struct crypt_options *options);
592 int crypt_luksUUID(struct crypt_options *options);
593 int crypt_isLuks(struct crypt_options *options);
594 int crypt_luksDump(struct crypt_options *options);
596 void crypt_put_options(struct crypt_options *options);
601 #endif /* _LIBCRYPTSETUP_H */