* Add --header option for detached metadata (on-disk LUKS header) device.
[platform/upstream/cryptsetup.git] / lib / libcryptsetup.h
1 #ifndef _LIBCRYPTSETUP_H
2 #define _LIBCRYPTSETUP_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #include <stdint.h>
8
9 struct crypt_device; /* crypt device handle */
10
11 /**
12  * Initialise crypt device handle and check if provided device exists.
13  *
14  * Returns 0 on success or negative errno value otherwise.
15  *
16  * @cd - returns pointer to crypt device handle
17  * @device - path to device
18  *
19  * Note that logging is not initialized here, possible messages uses
20  * default log function.
21  */
22 int crypt_init(struct crypt_device **cd, const char *device);
23
24 /**
25  * Initialise crypt device handle from provided active device name,
26  * and, optionally, from separate metadata (header) device
27  * and check if provided device exists.
28  *
29  * Returns 0 on success or negative errno value otherwise.
30  *
31  * @cd - crypt device handle
32  * @name - name of active crypt device
33  * @header_device - optional device containing on-disk header
34  *  (NULL if it the same as underlying device on there is no on-disk header)
35  *
36  * crypt_init_by_name is quivalent to calling
37  * crypt_init_by_name_and_header(cd, name, NULL);
38  */
39 int crypt_init_by_name_and_header(struct crypt_device **cd,
40                                   const char *name,
41                                   const char *header_device);
42 int crypt_init_by_name(struct crypt_device **cd, const char *name);
43
44 /**
45  * Set log function.
46  *
47  * @cd - crypt device handle (can be NULL to set default log function)
48  * @usrptr - provided identification in callback
49  * @level  - log level below (debug messages can uses other levels)
50  * @msg    - log message
51  */
52 #define CRYPT_LOG_NORMAL 0
53 #define CRYPT_LOG_ERROR  1
54 #define CRYPT_LOG_VERBOSE  2
55 #define CRYPT_LOG_DEBUG -1 /* always on stdout */
56 void crypt_set_log_callback(struct crypt_device *cd,
57         void (*log)(int level, const char *msg, void *usrptr),
58         void *usrptr);
59
60 /**
61  * Log message through log function.
62  *
63  * @cd - crypt device handle
64  * @level  - log level
65  * @msg    - log message
66  */
67 void crypt_log(struct crypt_device *cd, int level, const char *msg);
68
69 /**
70  * Set confirmation callback (yes/no)
71  *
72  * If code need confirmation (like deleting last key slot) this function
73  * is called. If not defined, everything is confirmed.
74  *
75  * Calback should return 0 if operation is declined, other values mean accepted.
76  *
77  * @cd - crypt device handle
78  * @usrptr - provided identification in callback
79  * @msg - Message for user to confirm
80  */
81 void crypt_set_confirm_callback(struct crypt_device *cd,
82         int (*confirm)(const char *msg, void *usrptr),
83         void *usrptr);
84
85 /**
86  * Set password query callback.
87  *
88  * If code need _interactive_ query for password, this callback is called.
89  * If not defined, compiled-in default is called (uses terminal input).
90  *
91  * @cd - crypt device handle
92  * @usrptr - provided identification in callback
93  * @msg - Message for user
94  * @buf - buffer for password
95  * @length - size of buffer
96  *
97  * Calback should return length of password in buffer
98  * or negative errno value in case of error.
99  *
100  * - Note that if this function is defined, verify option is ignored
101  *   (caller which provided callback is responsible for password verification)
102  * - Only zero terminated passwords can be entered this way, for complex
103  *   use API functions directly.
104  * - Maximal length of password is limited to @length-1 (minimal 511 chars)
105  */
106 void crypt_set_password_callback(struct crypt_device *cd,
107         int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
108         void *usrptr);
109
110 /**
111  * Various crypt device parameters
112  *
113  * @cd - crypt device handle
114  * @timeout - timeout in seconds for password entry if compiled-in function used
115  * @password_retry - number of tries for password if not verified
116  * @iteration_time - iteration time for LUKS header in miliseconds
117  * @password_verify - for compiled-in password query always verify passwords twice
118  */
119 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
120 void crypt_set_password_retry(struct crypt_device *cd, int tries);
121 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
122 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
123
124 /**
125  * Set data device (ciphertext device) if LUKS header is separated
126  * @cd - crypt device handle
127  * @device - path to device
128  */
129 int crypt_set_data_device(struct crypt_device *cd, const char *device);
130
131 /**
132  * Set which RNG (random number generator) is used for generating long term key
133  * @cd - crypt device handle
134  * @rng_type - kernel random number generator to use
135  *
136  * CRYPT_RNG_URANDOM - use /dev/urandom
137  * CRYPT_RNG_RANDOM  - use /dev/random (waits if no entropy in system)
138  */
139 #define CRYPT_RNG_URANDOM 0
140 #define CRYPT_RNG_RANDOM  1
141 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
142
143 /**
144  * Get which RNG (random number generator) is used for generating long term key
145  *
146  * Returns RNG type on success or negative errno value otherwise.
147  *
148  * @cd - crypt device handle
149  */
150 int crypt_get_rng_type(struct crypt_device *cd);
151
152 /**
153  * Helper to lock/unlock memory to avoid swap sensitive data to disk
154  *
155  * @cd - crypt device handle, can be NULL
156  * @lock - 0 to unloct otherwise lock memory
157  *
158  * Return value indicates that memory is locked (function can be called multiple times).
159  * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
160  */
161 int crypt_memory_lock(struct crypt_device *cd, int lock);
162
163 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
164 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
165 #define CRYPT_LOOPAES "LOOPAES" /* loop-AES compatibility mode */
166
167 /**
168  * Get device type
169  *
170  * @cd - crypt device handle
171  *
172  * Returns string according to device type or NULL if not known.
173  */
174 const char *crypt_get_type(struct crypt_device *cd);
175
176 struct crypt_params_plain {
177         const char *hash; /* password hash function */
178         uint64_t offset;  /* offset in sectors */
179         uint64_t skip;    /* IV offset / initialisation sector */
180         uint64_t size;    /* size of mapped device or 0 for autodetection */
181 };
182
183 struct crypt_params_luks1 {
184         const char *hash;      /* hash used in LUKS header */
185         size_t data_alignment; /* in sectors, data offset is multiple of this */
186 };
187
188 struct crypt_params_loopaes {
189         const char *hash; /* key hash function */
190         uint64_t offset;  /* offset in sectors */
191         uint64_t skip;    /* IV offset / initialisation sector */
192 };
193 /**
194  * Create (format) new crypt device (and possible header on-disk) but not activates it.
195  *
196  * Returns 0 on success or negative errno value otherwise.
197  *
198  * @cd - crypt device handle
199  * @type - type of device (optional params struct must be of this type)
200  * @cipher - (e.g. "aes")
201  * @cipher_mode - including IV specification (e.g. "xts-plain")
202  * @uuid - requested UUID or NULL if it should be generated
203  * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
204  * @volume_key_size - size of volume key in bytes.
205  * @params - crypt type specific parameters
206  *
207  * Note that crypt_format does not enable any keyslot, but it stores volume key internally
208  * and subsequent crypt_keyslot_add_* calls can be used.
209  */
210 int crypt_format(struct crypt_device *cd,
211         const char *type,
212         const char *cipher,
213         const char *cipher_mode,
214         const char *uuid,
215         const char *volume_key,
216         size_t volume_key_size,
217         void *params);
218
219 /**
220  * Set new UUID for already existing device (if format supports it)
221  *
222  * Returns 0 on success or negative errno value otherwise.
223  *
224  * @cd - crypt device handle
225  * @uuid - requested UUID or NULL if it should be generated
226  */
227 int crypt_set_uuid(struct crypt_device *cd,
228                    const char *uuid);
229
230 /**
231  * Load crypt device parameters from on-disk header
232  *
233  * Returns 0 on success or negative errno value otherwise.
234  *
235  * @cd - crypt device handle
236  * @requested_type - use NULL for all known
237  * @params - crypt type specific parameters
238  */
239 int crypt_load(struct crypt_device *cd,
240                const char *requested_type,
241                void *params);
242
243 /**
244  * Resize crypt device
245  *
246  * Returns 0 on success or negative errno value otherwise.
247  *
248  * @cd - crypt device handle
249  * @name - name of device to resize
250  * @new_size - new device size in sectors or 0 to use underlying device size
251  */
252 int crypt_resize(struct crypt_device *cd,
253                  const char *name,
254                  uint64_t new_size);
255
256 /**
257  * Suspends crypt device.
258  *
259  * Returns 0 on success or negative errno value otherwise.
260  *
261  * @cd - crypt device handle, can be NULL
262  * @name - name of device to suspend
263  */
264 int crypt_suspend(struct crypt_device *cd,
265                   const char *name);
266
267 /**
268  * Resumes crypt device using passphrase.
269  *
270  * Returns unlocked key slot number or negative errno otherwise.
271  *
272  * @cd - crypt device handle
273  * @name - name of device to resume
274  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
275  * @passphrase - passphrase used to unlock volume key, NULL for query
276  * @passphrase_size - size of @passphrase (binary data)
277  */
278 int crypt_resume_by_passphrase(struct crypt_device *cd,
279                                const char *name,
280                                int keyslot,
281                                const char *passphrase,
282                                size_t passphrase_size);
283
284 /**
285  * Resumes crypt device using key file.
286  *
287  * Returns unlocked key slot number or negative errno otherwise.
288  *
289  * @cd - crypt device handle
290  * @name - name of device to resume
291  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
292  * @keyfile - key file used to unlock volume key, NULL for passphrase query
293  * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
294  */
295 int crypt_resume_by_keyfile(struct crypt_device *cd,
296                             const char *name,
297                             int keyslot,
298                             const char *keyfile,
299                             size_t keyfile_size);
300
301 /**
302  * Releases crypt device context and used memory.
303  *
304  * @cd - crypt device handle
305  */
306 void crypt_free(struct crypt_device *cd);
307
308 /**
309  * Add key slot using provided passphrase
310  *
311  * Returns allocated key slot number or negative errno otherwise.
312  *
313  * @cd - crypt device handle
314  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
315  * @passphrase - passphrase used to unlock volume key, NULL for query
316  * @passphrase_size - size of @passphrase (binary data)
317  * @new_passphrase - passphrase for new keyslot, NULL for query
318  * @new_passphrase_size - size of @new_passphrase (binary data)
319  */
320 #define CRYPT_ANY_SLOT -1
321 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
322         int keyslot,
323         const char *passphrase,
324         size_t passphrase_size,
325         const char *new_passphrase,
326         size_t new_passphrase_size);
327
328 /**
329  * Get number of keyslots supported for device type.
330  *
331  * Returns slot count or negative errno otherwise if device
332  * doesn't not support keyslots.
333  *
334  * @type - crypt device type
335  */
336 int crypt_keyslot_max(const char *type);
337
338 /**
339 * Add key slot using provided key file path
340  *
341  * Returns allocated key slot number or negative errno otherwise.
342  *
343  * @cd - crypt device handle
344  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
345  * @keyfile - key file used to unlock volume key, NULL for passphrase query
346  * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
347  * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
348  * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
349  *
350  * Note that @keyfile can be "-" for STDIN
351  */
352 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
353         int keyslot,
354         const char *keyfile,
355         size_t keyfile_size,
356         const char *new_keyfile,
357         size_t new_keyfile_size);
358
359 /**
360  * Add key slot using provided volume key
361  *
362  * Returns allocated key slot number or negative errno otherwise.
363  *
364  * @cd - crypt device handle
365  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
366  * @volume_key - provided volume key or NULL if used after crypt_format
367  * @volume_key_size - size of @volume_key
368  * @passphrase - passphrase for new keyslot, NULL for query
369  * @passphrase_size - size of @passphrase
370  */
371 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
372         int keyslot,
373         const char *volume_key,
374         size_t volume_key_size,
375         const char *passphrase,
376         size_t passphrase_size);
377
378 /**
379  * Destroy (and disable) key slot
380  *
381  * Returns 0 on success or negative errno value otherwise.
382  *
383  * @cd - crypt device handle
384  * @keyslot - requested key slot to destroy
385  *
386  * Note that there is no passphrase verification used.
387  */
388 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
389
390 /**
391  * Activation flags
392  */
393 #define CRYPT_ACTIVATE_READONLY (1 << 0)
394 #define CRYPT_ACTIVATE_NO_UUID  (1 << 1) /* ignored */
395 #define CRYPT_ACTIVATE_SHARED   (1 << 2)
396 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3) /* enable discards aka TRIM */
397
398 /**
399  * Active device runtime attributes
400  */
401 struct crypt_active_device {
402         uint64_t offset;        /* offset in sectors */
403         uint64_t iv_offset;     /* IV initilisation sector */
404         uint64_t size;          /* active device size */
405         uint32_t flags;         /* activation flags */
406 };
407
408 /**
409  * Receives runtime attributes of active crypt device
410  *
411  * Returns 0 on success or negative errno value otherwise.
412  *
413  * @cd - crypt device handle (can be NULL)
414  * @name - name of active device
415  * @cad - preallocated active device attributes to fill
416  */
417 int crypt_get_active_device(struct crypt_device *cd,
418                             const char *name,
419                             struct crypt_active_device *cad);
420
421 /**
422  * Activate device or check passphrase
423  *
424  * Returns unlocked key slot number or negative errno otherwise.
425  *
426  * @cd - crypt device handle
427  * @name - name of device to create, if NULL only check passphrase
428  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
429  * @passphrase - passphrase used to unlock volume key, NULL for query
430  * @passphrase_size - size of @passphrase
431  * @flags - activation flags
432  */
433 int crypt_activate_by_passphrase(struct crypt_device *cd,
434         const char *name,
435         int keyslot,
436         const char *passphrase,
437         size_t passphrase_size,
438         uint32_t flags);
439
440 /**
441  * Activate device or check using key file
442  *
443  * Returns unlocked key slot number or negative errno otherwise.
444  *
445  * @cd - crypt device handle
446  * @name - name of device to create, if NULL only check keyfile
447  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
448  * @keyfile - key file used to unlock volume key
449  * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
450  * @flags - activation flags
451  */
452 int crypt_activate_by_keyfile(struct crypt_device *cd,
453         const char *name,
454         int keyslot,
455         const char *keyfile,
456         size_t keyfile_size,
457         uint32_t flags);
458
459 /**
460  * Activate device using provided volume key
461  *
462  * Returns 0 on success or negative errno value otherwise.
463  *
464  * @cd - crypt device handle
465  * @name - name of device to create, if NULL only check volume key
466  * @volume_key - provided volume key (or NULL to use internal)
467  * @volume_key_size - size of @volume_key
468  * @flags - activation flags
469  *
470  * If NULL is used for volume_key, device has to be initialized
471  * by previous operation (like crypt_format() or crypt_init_by_name())
472  */
473 int crypt_activate_by_volume_key(struct crypt_device *cd,
474         const char *name,
475         const char *volume_key,
476         size_t volume_key_size,
477         uint32_t flags);
478
479 /**
480  * Deactivate crypt device
481  *
482  * @cd - crypt device handle, can be NULL
483  * @name - name of device to deactivate
484   */
485 int crypt_deactivate(struct crypt_device *cd, const char *name);
486
487 /**
488  * Get volume key from of crypt device
489  *
490  * Returns unlocked key slot number or negative errno otherwise.
491  *
492  * @cd - crypt device handle
493  * @keyslot - use this keyslot or CRYPT_ANY_SLOT
494  * @volume_key - buffer for volume key
495  * @volume_key_size - on input, size of buffer @volume_key,
496  *                    on output size of @volume_key
497  * @passphrase - passphrase used to unlock volume key, NULL for query
498  * @passphrase_size - size of @passphrase
499  */
500 int crypt_volume_key_get(struct crypt_device *cd,
501         int keyslot,
502         char *volume_key,
503         size_t *volume_key_size,
504         const char *passphrase,
505         size_t passphrase_size);
506
507 /**
508  * Verify that provided volume key is valid for crypt device
509  *
510  * Returns 0 on success or negative errno value otherwise.
511  *
512  * @cd - crypt device handle
513  * @volume_key - provided volume key
514  * @volume_key_size - size of @volume_key
515  */
516 int crypt_volume_key_verify(struct crypt_device *cd,
517         const char *volume_key,
518         size_t volume_key_size);
519
520 /**
521  * Get status info about device name
522  *
523  * Returns value defined by crypt_status_info.
524  *
525  * @cd - crypt device handle, can be NULL
526  * @name -crypt device name
527  *
528  * CRYPT_INACTIVE - no such mapped device
529  * CRYPT_ACTIVE - device is active
530  * CRYPT_BUSY - device is active and has open count > 0
531  */
532 typedef enum {
533         CRYPT_INVALID,
534         CRYPT_INACTIVE,
535         CRYPT_ACTIVE,
536         CRYPT_BUSY
537 } crypt_status_info;
538 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
539
540 /**
541  * Dump text-formatted information about crypt device to log output
542  *
543  * Returns 0 on success or negative errno value otherwise.
544  *
545  * @cd - crypt device handle
546  */
547 int crypt_dump(struct crypt_device *cd);
548
549 /**
550  * Various crypt device info functions
551  *
552  * @cd - crypt device handle
553  *
554  * cipher - used cipher, e.g. "aes" or NULL otherwise
555  * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
556  * uuid - device UUID or NULL if not set
557  * device_name - underlying device name or NULL if not yet set
558  * data_offset - device offset in sectors where real data starts on underlying device)
559  * iv_offset - IV offset in sectors (skip)
560  * volume_key_size - size (in bytes) of volume key for crypt device
561  */
562 const char *crypt_get_cipher(struct crypt_device *cd);
563 const char *crypt_get_cipher_mode(struct crypt_device *cd);
564 const char *crypt_get_uuid(struct crypt_device *cd);
565 const char *crypt_get_device_name(struct crypt_device *cd);
566 uint64_t crypt_get_data_offset(struct crypt_device *cd);
567 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
568 int crypt_get_volume_key_size(struct crypt_device *cd);
569
570 /**
571  * Get information about particular key slot
572  *
573  * Returns value defined by crypt_keyslot_info.
574  *
575  * @cd - crypt device handle
576  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
577  */
578 typedef enum {
579         CRYPT_SLOT_INVALID,
580         CRYPT_SLOT_INACTIVE,
581         CRYPT_SLOT_ACTIVE,
582         CRYPT_SLOT_ACTIVE_LAST
583 } crypt_keyslot_info;
584 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
585
586 /**
587  * Backup header and keyslots to file
588  *
589  * Returns 0 on success or negative errno value otherwise.
590  *
591  * @cd - crypt device handle
592  * @requested_type - type of header to backup
593  * @backup_file - file to backup header to
594  */
595 int crypt_header_backup(struct crypt_device *cd,
596         const char *requested_type,
597         const char *backup_file);
598
599 /**
600  * Restore header and keyslots from backup file
601  *
602  * Returns 0 on success or negative errno value otherwise.
603  *
604  * @cd - crypt device handle
605  * @requested_type - type of header to restore
606  * @backup_file - file to restore header from
607  */
608 int crypt_header_restore(struct crypt_device *cd,
609         const char *requested_type,
610         const char *backup_file);
611
612 /**
613  * Receives last reported error
614  *
615  * @buf - buffef for message
616  * @size - size of buffer
617  *
618  * Note that this is old API function using global context.
619  * All error messages are reported also through log callback.
620  */
621 void crypt_get_error(char *buf, size_t size);
622
623 /**
624  * Get directory where mapped crypt devices are created
625  */
626 const char *crypt_get_dir(void);
627
628 /**
629  * Set library debug level
630  */
631 #define CRYPT_DEBUG_ALL  -1
632 #define CRYPT_DEBUG_NONE  0
633 void crypt_set_debug_level(int level);
634
635 #ifdef __cplusplus
636 }
637 #endif
638 #endif /* _LIBCRYPTSETUP_H */