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