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