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 * @class - log type below (debug messages can uses other levels)
43 #define CRYPT_LOG_NORMAL 0
44 #define CRYPT_LOG_ERROR 1
45 #define CRYPT_LOG_DEBUG -1 /* always on stdout */
46 void crypt_set_log_callback(struct crypt_device *cd,
47 void (*log)(int class, const char *msg, void *usrptr),
51 * Log message through log function.
53 * @cd - crypt device handle
57 void crypt_log(struct crypt_device *cd, int class, const char *msg);
60 * Set confirmation callback (yes/no)
62 * If code need confirmation (like deleting last key slot) this function
63 * is called. If not defined, everything is confirmed.
65 * Calback should return 0 if operation is declined, other values mean accepted.
67 * @cd - crypt device handle
68 * @usrptr - provided identification in callback
69 * @msg - Message for user to confirm
71 void crypt_set_confirm_callback(struct crypt_device *cd,
72 int (*confirm)(const char *msg, void *usrptr),
76 * Set password query callback.
78 * If code need _interactive_ query for password, this callback is called.
79 * If not defined, compiled-in default is called (uses terminal input).
81 * @cd - crypt device handle
82 * @usrptr - provided identification in callback
83 * @msg - Message for user
84 * @buf - buffer for password
85 * @length - size of buffer
87 * - Note that if this function is defined, verify option is ignored
88 * (caller whch provided callback is responsible fo password verification)
89 * - Only zero terminated passwords can be enteted this way, for complex
90 * API functions directly.
91 * - Maximal length of password is limited to @length-1 (minimal 511 chars)
93 void crypt_set_password_callback(struct crypt_device *cd,
94 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
98 * Various crypt device parameters
100 * @cd - crypt device handle
101 * @timeout - timeout in secons for password entry if compiled-in function used
102 * @password_retry - number of tries for password if not verified
103 * @iteration_time - iteration time for LUKS header in miliseconds
104 * @password_verify - for compiled-in password query always verify passwords twice
106 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
107 void crypt_set_password_retry(struct crypt_device *cd, int tries);
108 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
109 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
112 * Helper to lock/unlock memory to avoid swap sensitive data to disk
114 * @cd - crypt device handle, can be NULL
115 * @lock - 0 to unloct otherwise lock memory
117 * Return value indicates that memory is locked (function can be called multiple times).
118 * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
120 int crypt_memory_lock(struct crypt_device *cd, int lock);
122 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
123 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
125 struct crypt_params_plain {
126 const char *hash; /* password hash function */
127 uint64_t offset; /* offset in sectors */
128 uint64_t skip; /* IV initilisation sector */
131 struct crypt_params_luks1 {
132 const char *hash; /* hash used in LUKS header */
133 size_t data_alignment; /* in sectors, data offset is multiple of this */
137 * Create (format) new crypt device (and possible header on-disk) but not activates it.
139 * Returns 0 on success or negative errno value otherwise.
141 * @cd - crypt device handle
142 * @type - type of device (optional params struct must be of this type)
143 * @cipher - (e.g. "aes")
144 * @cipher_mode - including IV specification (e.g. "xts-plain")
145 * @uuid - requested UUID or NULL if it should be generated
146 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
147 * @volume_key_size - size og volume key in bytes.
148 * @params - crypt type specific parameters
150 * Note that crypt_format do not enable any keyslot, but it stores volume key internally
151 * and subsequent crypt_keyslot_add_* calls can be used.
152 * (It is the only situation when crypt_keyslot_add_* do not require active key slots.)
154 int crypt_format(struct crypt_device *cd,
157 const char *cipher_mode,
159 const char *volume_key,
160 size_t volume_key_size,
164 * Load crypt device parameters from on-disk header
166 * Returns 0 on success or negative errno value otherwise.
168 * @cd - crypt device handle
169 * @requested_type - use NULL for all known
170 * @params - crypt type specific parameters
172 int crypt_load(struct crypt_device *cd,
173 const char *requested_type,
177 * Suspends crypt device.
179 * Returns 0 on success or negative errno value otherwise.
181 * @cd - crypt device handle, can be NULL
182 * @name - name of device to suspend
184 int crypt_suspend(struct crypt_device *cd,
188 * Resumes crypt device using passphrase.
190 * Returns unlocked key slot number or negative errno otherwise.
192 * @cd - crypt device handle
193 * @name - name of device to resume
194 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
195 * @passphrase - passphrase used to unlock volume key, NULL for query
196 * @passphrase_size - size of @passphrase (binary data)
198 int crypt_resume_by_passphrase(struct crypt_device *cd,
201 const char *passphrase,
202 size_t passphrase_size);
205 * Resumes crypt device using key file.
207 * Returns unlocked key slot number or negative errno otherwise.
209 * @cd - crypt device handle
210 * @name - name of device to resume
211 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
212 * @keyfile - key file used to unlock volume key, NULL for passphrase query
213 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
215 int crypt_resume_by_keyfile(struct crypt_device *cd,
219 size_t keyfile_size);
222 * Releases crypt device context and used memory.
224 * @cd - crypt device handle
226 void crypt_free(struct crypt_device *cd);
229 * Add key slot using provided passphrase
231 * Returns allocated key slot number or negative errno otherwise.
233 * @cd - crypt device handle
234 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
235 * @passphrase - passphrase used to unlock volume key, NULL for query
236 * @passphrase_size - size of @passphrase (binary data)
237 * @new_passphrase - passphrase for new keyslot, NULL for query
238 * @new_passphrase_size - size of @new_passphrase (binary data)
240 #define CRYPT_ANY_SLOT -1
241 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
243 const char *passphrase,
244 size_t passphrase_size,
245 const char *new_passphrase,
246 size_t new_passphrase_size);
249 * Add key slot using provided key file path
251 * Returns allocated key slot number or negative errno otherwise.
253 * @cd - crypt device handle
254 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
255 * @keyfile - key file used to unlock volume key, NULL for passphrase query
256 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
257 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
258 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
260 * Note that @keyfile can be "-" for STDIN
262 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
266 const char *new_keyfile,
267 size_t new_keyfile_size);
270 * Add key slot using provided volume key
272 * Returns allocated key slot number or negative errno otherwise.
274 * @cd - crypt device handle
275 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
276 * @volume_key - provided volume key or NULL if used after crypt_format
277 * @volume_key_size - size of @volume_key
278 * @passphrase - passphrase for new keyslot, NULL for query
279 * @passphrase_size - size of @passphrase
281 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
283 const char *volume_key,
284 size_t volume_key_size,
285 const char *passphrase,
286 size_t passphrase_size);
289 * Destroy (and disable) key slot
291 * Returns 0 on success or negative errno value otherwise.
293 * @cd - crypt device handle
294 * @keyslot - requested key slot to destroy
296 * Note that there is no passphrase verification used.
298 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
303 #define CRYPT_ACTIVATE_READONLY (1 << 0)
304 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
307 * Activate device or check passphrase
309 * Returns unlocked key slot number or negative errno otherwise.
311 * @cd - crypt device handle
312 * @name - name of device to create, if NULL only check passphrase
313 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
314 * @passphrase - passphrase used to unlock volume key, NULL for query
315 * @passphrase_size - size of @passphrase
316 * @flags - activation flags
318 int crypt_activate_by_passphrase(struct crypt_device *cd,
321 const char *passphrase,
322 size_t passphrase_size,
326 * Activate device or check using key file
328 * Returns unlocked key slot number or negative errno otherwise.
330 * @cd - crypt device handle
331 * @name - name of device to create, if NULL only check keyfile
332 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
333 * @keyfile - key file used to unlock volume key
334 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
335 * @flags - activation flags
337 int crypt_activate_by_keyfile(struct crypt_device *cd,
345 * Activate device using provided volume key
347 * Returns 0 on success or negative errno value otherwise.
349 * @cd - crypt device handle
350 * @name - name of device to create, if NULL only check volume key
351 * @volume_key - provided volume key
352 * @volume_key_size - size of @volume_key
353 * @flags - activation flags
355 int crypt_activate_by_volume_key(struct crypt_device *cd,
357 const char *volume_key,
358 size_t volume_key_size,
362 * Deactivate crypt device
364 * @cd - crypt device handle, can be NULL
365 * @name - name of device to deactivate
367 int crypt_deactivate(struct crypt_device *cd, const char *name);
370 * Get volume key from of crypt device
372 * Returns unlocked key slot number or negative errno otherwise.
374 * @cd - crypt device handle
375 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
376 * @volume_key - buffer for volume key
377 * @volume_key_size - on input, size of buffer @volume_key,
378 * on output size of @volume_key
379 * @passphrase - passphrase used to unlock volume key, NULL for query
380 * @passphrase_size - size of @passphrase
382 int crypt_volume_key_get(struct crypt_device *cd,
385 size_t *volume_key_size,
386 const char *passphrase,
387 size_t passphrase_size);
390 * Verify that provided volume key is valid for crypt device
392 * Returns 0 on success or negative errno value otherwise.
394 * @cd - crypt device handle
395 * @volume_key - provided volume key
396 * @volume_key_size - size of @volume_key
398 int crypt_volume_key_verify(struct crypt_device *cd,
399 const char *volume_key,
400 size_t volume_key_size);
403 * Get status info about device name
405 * Returns value defined by crypt_status_info.
407 * @cd - crypt device handle, can be NULL
408 * @name -crypt device name
410 * CRYPT_INACTIVE - no such mapped device
411 * CRYPT_ACTIVE - device is active
412 * CRYPT_BUSY - device is active and has open count > 0
420 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
423 * Dump text-formatted information about crypt device to log output
425 * Returns 0 on success or negative errno value otherwise.
427 * @cd - crypt device handle, can be NULL
429 int crypt_dump(struct crypt_device *cd);
432 * Various crypt device info functions
434 * @cd - crypt device handle
436 * cipher - used cipher, e.g. "aes" or NULL otherwise
437 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
438 * uuid - device UUID or NULL if not set
439 * data_offset - device offset in sectors where real data starts on underlying device)
440 * volume_key_size - size (in bytes) of volume key for crypt device
442 const char *crypt_get_cipher(struct crypt_device *cd);
443 const char *crypt_get_cipher_mode(struct crypt_device *cd);
444 const char *crypt_get_uuid(struct crypt_device *cd);
445 uint64_t crypt_get_data_offset(struct crypt_device *cd);
446 int crypt_get_volume_key_size(struct crypt_device *cd);
449 * Get information about particular key slot
451 * Returns value defined by crypt_keyslot_info.
453 * @cd - crypt device handle
454 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
460 CRYPT_SLOT_ACTIVE_LAST
461 } crypt_keyslot_info;
462 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
465 * Backup header and keyslots to file
467 * Returns 0 on success or negative errno value otherwise.
469 * @cd - crypt device handle
470 * @requested_type - type of header to backup
471 * @backup_file - file to backup header to
473 int crypt_header_backup(struct crypt_device *cd,
474 const char *requested_type,
475 const char *backup_file);
478 * Restore header and keyslots from backup file
480 * Returns 0 on success or negative errno value otherwise.
482 * @cd - crypt device handle
483 * @requested_type - type of header to restore
484 * @backup_file - file to restore header from
486 int crypt_header_restore(struct crypt_device *cd,
487 const char *requested_type,
488 const char *backup_file);
491 * Receives last reported error
493 * @buf - buffef for message
494 * @size - size of buffer
496 * Note that this is old API function using global context.
497 * All error messages are reported also through log callback.
499 void crypt_get_error(char *buf, size_t size);
502 * Get directory where mapped crypt devices are created
504 const char *crypt_get_dir(void);
507 * Set library debug level
509 #define CRYPT_DEBUG_ALL -1
510 #define CRYPT_DEBUG_NONE 0
511 void crypt_set_debug_level(int level);
514 * OLD DEPRECATED API **********************************
516 * Provided only for backward compatibility.
519 struct interface_callbacks {
520 int (*yesDialog)(char *msg);
521 void (*log)(int class, char *msg);
524 #define CRYPT_FLAG_VERIFY (1 << 0)
525 #define CRYPT_FLAG_READONLY (1 << 1)
526 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE (1 << 2)
527 #define CRYPT_FLAG_VERIFY_ON_DELKEY (1 << 3)
528 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
530 struct crypt_options {
537 const char *passphrase;
539 const char *key_file;
540 const char *new_key_file;
549 uint64_t iteration_time;
552 uint64_t align_payload;
555 struct interface_callbacks *icb;
558 int crypt_create_device(struct crypt_options *options);
559 int crypt_update_device(struct crypt_options *options);
560 int crypt_resize_device(struct crypt_options *options);
561 int crypt_query_device(struct crypt_options *options);
562 int crypt_remove_device(struct crypt_options *options);
563 int crypt_luksFormat(struct crypt_options *options);
564 int crypt_luksOpen(struct crypt_options *options);
565 int crypt_luksKillSlot(struct crypt_options *options);
566 int crypt_luksRemoveKey(struct crypt_options *options);
567 int crypt_luksAddKey(struct crypt_options *options);
568 int crypt_luksUUID(struct crypt_options *options);
569 int crypt_isLuks(struct crypt_options *options);
570 int crypt_luksDump(struct crypt_options *options);
572 void crypt_put_options(struct crypt_options *options);
577 #endif /* _LIBCRYPTSETUP_H */