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