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