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