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
18 * Note that logging is not initialized here, possible messages uses
19 * default log function.
21 int crypt_init(struct crypt_device **cd, const char *device);
26 * @cd - crypt device handle
27 * @usrptr - provided identification in callback
28 * @class - log type below (debug messages can uses other levels)
31 #define CRYPT_LOG_NORMAL 0
32 #define CRYPT_LOG_ERROR 1
33 #define CRYPT_LOG_DEBUG -1 /* always on stdout */
34 void crypt_set_log_callback(struct crypt_device *cd,
35 void (*log)(int class, const char *msg, void *usrptr),
39 * Set confirmation callback (yes/no)
41 * If code need confirmation (like deleting last key slot) this function
42 * is called. If not defined, everything is confirmed.
44 * Calback should return 0 if operation is declined, other values mean accepted.
46 * @cd - crypt device handle
47 * @usrptr - provided identification in callback
48 * @msg - Message for user to confirm
50 void crypt_set_confirm_callback(struct crypt_device *cd,
51 int (*confirm)(const char *msg, void *usrptr),
55 * Set password query callback.
57 * If code need _interactive_ query for password, this callback is called.
58 * If not defined, compiled-in default is called (uses terminal input).
60 * @cd - crypt device handle
61 * @usrptr - provided identification in callback
62 * @msg - Message for user
63 * @buf - buffer for password
64 * @length - size of buffer
66 * - Note that if this function is defined, verify option is ignored
67 * (caller whch provided callback is responsible fo password verification)
68 * - Only zero terminated passwords can be enteted this way, for complex
69 * API functions directly.
70 * - Maximal length of password is limited to @length-1 (minimal 511 chars)
72 void crypt_set_password_callback(struct crypt_device *cd,
73 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
77 * Various crypt device parameters
79 * @cd - crypt device handle
80 * @timeout - timeout in secons for password entry if compiled-in function used
81 * @password_retry - number of tries for password if not verified
82 * @iteration_time - iteration time for LUKS header in miliseconds
83 * @password_verify - for compiled-in password query always verify passwords twice
85 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
86 void crypt_set_password_retry(struct crypt_device *cd, int tries);
87 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
88 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
91 * Helper to lock/unlock memory to avoid swap sensitive data to disk
93 * @cd - crypt device handle, can be NULL
94 * @lock - 0 to unloct otherwise lock memory
96 * Return value indicates that memory is locked (function can be called multiple times).
97 * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
99 int crypt_memory_lock(struct crypt_device *cd, int lock);
101 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
102 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
104 struct crypt_params_plain {
105 const char *hash; /* password hash function */
106 uint64_t offset; /* offset in sectors */
107 uint64_t skip; /* IV initilisation sector */
110 struct crypt_params_luks1 {
111 const char *hash; /* hash used in LUKS header */
112 size_t data_alignment; /* in sectors, data offset is multiple of this */
116 * Create (format) new crypt device (and possible header on-disk) but not activates it.
118 * Returns 0 on success or negative errno value otherwise.
120 * @cd - crypt device handle
121 * @type - type of device (optional params struct must be of this type)
122 * @cipher - (e.g. "aes")
123 * @cipher_mode - including IV specification (e.g. "xts-plain")
124 * @uuid - requested UUID or NULL if it should be generated
125 * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
126 * @volume_key_size - size og volume key in bytes.
127 * @params - crypt type specific parameters
129 * Note that crypt_format do not enable any keyslot, but it stores volume key internally
130 * and subsequent crypt_keyslot_add_* calls can be used.
131 * (It is the only situation when crypt_keyslot_add_* do not require active key slots.)
133 int crypt_format(struct crypt_device *cd,
136 const char *cipher_mode,
138 const char *volume_key,
139 size_t volume_key_size,
143 * Load crypt device parameters from on-disk header
145 * Returns 0 on success or negative errno value otherwise.
147 * @cd - crypt device handle
148 * @requested_type - use NULL for all known
149 * @params - crypt type specific parameters
151 int crypt_load(struct crypt_device *cd,
152 const char *requested_type,
156 * Releases crypt device context and used memory.
158 * @cd - crypt device handle
160 void crypt_free(struct crypt_device *cd);
163 * Add key slot using provided passphrase
165 * Returns allocated key slot number or negative errno otherwise.
167 * @cd - crypt device handle
168 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
169 * @passphrase - passphrase used to unlock volume key, NULL for query
170 * @passphrase_size - size of @passphrase (binary data)
171 * @new_passphrase - passphrase for new keyslot, NULL for query
172 * @new_passphrase_size - size of @new_passphrase (binary data)
174 #define CRYPT_ANY_SLOT -1
175 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
177 const char *passphrase,
178 size_t passphrase_size,
179 const char *new_passphrase,
180 size_t new_passphrase_size);
183 * Add key slot using provided key file path
185 * Returns allocated key slot number or negative errno otherwise.
187 * @cd - crypt device handle
188 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
189 * @keyfile - key file used to unlock volume key, NULL for passphrase query
190 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
191 * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
192 * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
194 * Note that @keyfile can be "-" for STDIN
196 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
200 const char *new_keyfile,
201 size_t new_keyfile_size);
204 * Add key slot using provided volume key
206 * Returns allocated key slot number or negative errno otherwise.
208 * @cd - crypt device handle
209 * @keyslot - requested keyslot or CRYPT_ANY_SLOT
210 * @volume_key - provided volume key or NULL if used after crypt_format
211 * @volume_key_size - size of @volume_key
212 * @passphrase - passphrase for new keyslot, NULL for query
213 * @passphrase_size - size of @passphrase
215 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
217 const char *volume_key,
218 size_t volume_key_size,
219 const char *passphrase,
220 size_t passphrase_size);
223 * Destroy (and disable) key slot
225 * Returns 0 on success or negative errno value otherwise.
227 * @cd - crypt device handle
228 * @keyslot - requested key slot to destroy
230 * Note that there is no passphrase verification used.
232 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
237 #define CRYPT_ACTIVATE_READONLY (1 << 0)
238 #define CRYPT_ACTIVATE_NO_UUID (1 << 1)
241 * Activate device or check passphrase
243 * Returns unlocked key slot number or negative errno otherwise.
245 * @cd - crypt device handle
246 * @name - name of device to create, if NULL only check passphrase
247 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
248 * @passphrase - passphrase used to unlock volume key, NULL for query
249 * @passphrase_size - size of @passphrase
250 * @flags - activation flags
252 int crypt_activate_by_passphrase(struct crypt_device *cd,
255 const char *passphrase,
256 size_t passphrase_size,
260 * Activate device or check using key file
262 * Returns unlocked key slot number or negative errno otherwise.
264 * @cd - crypt device handle
265 * @name - name of device to create, if NULL only check keyfile
266 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
267 * @keyfile - key file used to unlock volume key
268 * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
269 * @flags - activation flags
271 int crypt_activate_by_keyfile(struct crypt_device *cd,
279 * Activate device using provided volume key
281 * Returns 0 on success or negative errno value otherwise.
283 * @cd - crypt device handle
284 * @name - name of device to create, if NULL only check volume key
285 * @volume_key - provided volume key
286 * @volume_key_size - size of @volume_key
287 * @flags - activation flags
289 int crypt_activate_by_volume_key(struct crypt_device *cd,
291 const char *volume_key,
292 size_t volume_key_size,
296 * Deactivate crypt device
298 * @cd - crypt device handle, can be NULL
299 * @name - name of device to deactivate
301 int crypt_deactivate(struct crypt_device *cd, const char *name);
304 * Get volume key from of crypt device
306 * Returns unlocked key slot number or negative errno otherwise.
308 * @cd - crypt device handle
309 * @keyslot - use this keyslot or CRYPT_ANY_SLOT
310 * @volume_key - buffer for volume key
311 * @volume_key_size - on input, size of buffer @volume_key,
312 * on output size of @volume_key
313 * @passphrase - passphrase used to unlock volume key, NULL for query
314 * @passphrase_size - size of @passphrase
316 int crypt_volume_key_get(struct crypt_device *cd,
319 size_t *volume_key_size,
320 const char *passphrase,
321 size_t passphrase_size);
324 * Verify that provided volume key is valid for crypt device
326 * Returns 0 on success or negative errno value otherwise.
328 * @cd - crypt device handle
329 * @volume_key - provided volume key
330 * @volume_key_size - size of @volume_key
332 int crypt_volume_key_verify(struct crypt_device *cd,
333 const char *volume_key,
334 size_t volume_key_size);
337 * Get status info about device name
339 * Returns value defined by crypt_status_info.
341 * @cd - crypt device handle, can be NULL
342 * @name -crypt device name
344 * INACTIVE - no such mapped device
345 * ACTIVE - device is active
346 * BUSY - device is active and has open count > 0
348 typedef enum { INVALID, INACTIVE, ACTIVE, BUSY } crypt_status_info;
349 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
352 * Dump text-formatted information about crypt device to log output
354 * Returns 0 on success or negative errno value otherwise.
356 * @cd - crypt device handle, can be NULL
358 int crypt_dump(struct crypt_device *cd);
361 * Various crypt device info functions
363 * @cd - crypt device handle
365 * cipher - used cipher, e.g. "aes" or NULL otherwise
366 * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
367 * uuid - device UUID or NULL if not set
368 * data_offset - device offset in sectors where real data starts on underlying device)
369 * volume_key_size - size (in bytes) of volume key for crypt device
371 const char *crypt_get_cipher(struct crypt_device *cd);
372 const char *crypt_get_cipher_mode(struct crypt_device *cd);
373 const char *crypt_get_uuid(struct crypt_device *cd);
374 uint64_t crypt_get_data_offset(struct crypt_device *cd);
375 int crypt_get_volume_key_size(struct crypt_device *cd);
378 * Get information about particular key slot
380 * Returns value defined by crypt_keyslot_info.
382 * @cd - crypt device handle
383 * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
385 typedef enum { SLOT_INVALID, SLOT_INACTIVE, SLOT_ACTIVE, SLOT_ACTIVE_LAST } crypt_keyslot_info;
386 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
389 * Receives last reported error
391 * @buf - buffef for message
392 * @size - size of buffer
394 * Note that this is old API function using global context.
395 * All error messages are reported also through log callback.
397 void crypt_get_error(char *buf, size_t size);
400 * Get directory where mapped crypt devices are created
402 const char *crypt_get_dir(void);
405 * Set library debug level
407 #define CRYPT_DEBUG_ALL -1
408 #define CRYPT_DEBUG_NONE 0
409 void crypt_set_debug_level(int level);
412 * OLD DEPRECATED API **********************************
414 * Provided only for backward compatibility.
417 struct interface_callbacks {
418 int (*yesDialog)(char *msg);
419 void (*log)(int class, char *msg);
422 #define CRYPT_FLAG_VERIFY (1 << 0)
423 #define CRYPT_FLAG_READONLY (1 << 1)
424 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE (1 << 2)
425 #define CRYPT_FLAG_VERIFY_ON_DELKEY (1 << 3)
426 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
428 struct crypt_options {
435 const char *passphrase;
437 const char *key_file;
438 const char *new_key_file;
447 uint64_t iteration_time;
450 uint64_t align_payload;
453 struct interface_callbacks *icb;
456 int crypt_create_device(struct crypt_options *options);
457 int crypt_update_device(struct crypt_options *options);
458 int crypt_resize_device(struct crypt_options *options);
459 int crypt_query_device(struct crypt_options *options);
460 int crypt_remove_device(struct crypt_options *options);
461 int crypt_luksFormat(struct crypt_options *options);
462 int crypt_luksOpen(struct crypt_options *options);
463 int crypt_luksKillSlot(struct crypt_options *options);
464 int crypt_luksRemoveKey(struct crypt_options *options);
465 int crypt_luksAddKey(struct crypt_options *options);
466 int crypt_luksUUID(struct crypt_options *options);
467 int crypt_isLuks(struct crypt_options *options);
468 int crypt_luksDump(struct crypt_options *options);
470 void crypt_put_options(struct crypt_options *options);
475 #endif /* _LIBCRYPTSETUP_H */