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