Avoid class C++ keyword in library header.
[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_DEBUG -1 /* always on stdout */
46 void crypt_set_log_callback(struct crypt_device *cd,
47         void (*log)(int level, const char *msg, void *usrptr),
48         void *usrptr);
49
50 /**
51  * Log message through log function.
52  *
53  * @cd - crypt device handle
54  * @level  - log level
55  * @msg    - log message
56  */
57 void crypt_log(struct crypt_device *cd, int level, const char *msg);
58
59 /**
60  * Set confirmation callback (yes/no)
61  *
62  * If code need confirmation (like deleting last key slot) this function
63  * is called. If not defined, everything is confirmed.
64  *
65  * Calback should return 0 if operation is declined, other values mean accepted.
66  *
67  * @cd - crypt device handle
68  * @usrptr - provided identification in callback
69  * @msg - Message for user to confirm
70  */
71 void crypt_set_confirm_callback(struct crypt_device *cd,
72         int (*confirm)(const char *msg, void *usrptr),
73         void *usrptr);
74
75 /**
76  * Set password query callback.
77  *
78  * If code need _interactive_ query for password, this callback is called.
79  * If not defined, compiled-in default is called (uses terminal input).
80  *
81  * @cd - crypt device handle
82  * @usrptr - provided identification in callback
83  * @msg - Message for user
84  * @buf - buffer for password
85  * @length - size of buffer
86  *
87  * - Note that if this function is defined, verify option is ignored
88  *   (caller whch provided callback is responsible fo password verification)
89  * - Only zero terminated passwords can be enteted this way, for complex
90  *   API functions directly.
91  * - Maximal length of password is limited to @length-1 (minimal 511 chars)
92  */
93 void crypt_set_password_callback(struct crypt_device *cd,
94         int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
95         void *usrptr);
96
97 /**
98  * Various crypt device parameters
99  *
100  * @cd - crypt device handle
101  * @timeout - timeout in secons for password entry if compiled-in function used
102  * @password_retry - number of tries for password if not verified
103  * @iteration_time - iteration time for LUKS header in miliseconds
104  * @password_verify - for compiled-in password query always verify passwords twice
105  */
106 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
107 void crypt_set_password_retry(struct crypt_device *cd, int tries);
108 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
109 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
110
111 /**
112  * Helper to lock/unlock memory to avoid swap sensitive data to disk
113  *
114  * @cd - crypt device handle, can be NULL
115  * @lock - 0 to unloct otherwise lock memory
116  *
117  * Return value indicates that memory is locked (function can be called multiple times).
118  * Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
119  */
120 int crypt_memory_lock(struct crypt_device *cd, int lock);
121
122 #define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
123 #define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
124
125 struct crypt_params_plain {
126         const char *hash; /* password hash function */
127         uint64_t offset;  /* offset in sectors */
128         uint64_t skip;    /* IV initilisation sector */
129 };
130
131 struct crypt_params_luks1 {
132         const char *hash;      /* hash used in LUKS header */
133         size_t data_alignment; /* in sectors, data offset is multiple of this */
134 };
135
136 /**
137  * Create (format) new crypt device (and possible header on-disk) but not activates it.
138  *
139  * Returns 0 on success or negative errno value otherwise.
140  *
141  * @cd - crypt device handle
142  * @type - type of device (optional params struct must be of this type)
143  * @cipher - (e.g. "aes")
144  * @cipher_mode - including IV specification (e.g. "xts-plain")
145  * @uuid - requested UUID or NULL if it should be generated
146  * @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
147  * @volume_key_size - size og volume key in bytes.
148  * @params - crypt type specific parameters
149  *
150  * Note that crypt_format do not enable any keyslot, but it stores volume key internally
151  * and subsequent crypt_keyslot_add_* calls can be used.
152  * (It is the only situation when crypt_keyslot_add_* do not require active key slots.)
153  */
154 int crypt_format(struct crypt_device *cd,
155         const char *type,
156         const char *cipher,
157         const char *cipher_mode,
158         const char *uuid,
159         const char *volume_key,
160         size_t volume_key_size,
161         void *params);
162
163 /**
164  * Load crypt device parameters from on-disk header
165  *
166  * Returns 0 on success or negative errno value otherwise.
167  *
168  * @cd - crypt device handle
169  * @requested_type - use NULL for all known
170  * @params - crypt type specific parameters
171  */
172 int crypt_load(struct crypt_device *cd,
173                const char *requested_type,
174                void *params);
175
176 /**
177  * Suspends crypt device.
178  *
179  * Returns 0 on success or negative errno value otherwise.
180  *
181  * @cd - crypt device handle, can be NULL
182  * @name - name of device to suspend
183  */
184 int crypt_suspend(struct crypt_device *cd,
185                   const char *name);
186
187 /**
188  * Resumes crypt device using passphrase.
189  *
190  * Returns unlocked key slot number or negative errno otherwise.
191  *
192  * @cd - crypt device handle
193  * @name - name of device to resume
194  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
195  * @passphrase - passphrase used to unlock volume key, NULL for query
196  * @passphrase_size - size of @passphrase (binary data)
197  */
198 int crypt_resume_by_passphrase(struct crypt_device *cd,
199                                const char *name,
200                                int keyslot,
201                                const char *passphrase,
202                                size_t passphrase_size);
203
204 /**
205  * Resumes crypt device using key file.
206  *
207  * Returns unlocked key slot number or negative errno otherwise.
208  *
209  * @cd - crypt device handle
210  * @name - name of device to resume
211  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
212  * @keyfile - key file used to unlock volume key, NULL for passphrase query
213  * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
214  */
215 int crypt_resume_by_keyfile(struct crypt_device *cd,
216                             const char *name,
217                             int keyslot,
218                             const char *keyfile,
219                             size_t keyfile_size);
220
221 /**
222  * Releases crypt device context and used memory.
223  *
224  * @cd - crypt device handle
225  */
226 void crypt_free(struct crypt_device *cd);
227
228 /**
229  * Add key slot using provided passphrase
230  *
231  * Returns allocated key slot number or negative errno otherwise.
232  *
233  * @cd - crypt device handle
234  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
235  * @passphrase - passphrase used to unlock volume key, NULL for query
236  * @passphrase_size - size of @passphrase (binary data)
237  * @new_passphrase - passphrase for new keyslot, NULL for query
238  * @new_passphrase_size - size of @new_passphrase (binary data)
239  */
240 #define CRYPT_ANY_SLOT -1
241 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
242         int keyslot,
243         const char *passphrase,
244         size_t passphrase_size,
245         const char *new_passphrase,
246         size_t new_passphrase_size);
247
248 /**
249 * Add key slot using provided key file path
250  *
251  * Returns allocated key slot number or negative errno otherwise.
252  *
253  * @cd - crypt device handle
254  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
255  * @keyfile - key file used to unlock volume key, NULL for passphrase query
256  * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
257  * @new_keyfile - keyfile for new keyslot, NULL for passphrase query
258  * @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
259  *
260  * Note that @keyfile can be "-" for STDIN
261  */
262 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
263         int keyslot,
264         const char *keyfile,
265         size_t keyfile_size,
266         const char *new_keyfile,
267         size_t new_keyfile_size);
268
269 /**
270  * Add key slot using provided volume key
271  *
272  * Returns allocated key slot number or negative errno otherwise.
273  *
274  * @cd - crypt device handle
275  * @keyslot - requested keyslot or CRYPT_ANY_SLOT
276  * @volume_key - provided volume key or NULL if used after crypt_format
277  * @volume_key_size - size of @volume_key
278  * @passphrase - passphrase for new keyslot, NULL for query
279  * @passphrase_size - size of @passphrase
280  */
281 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
282         int keyslot,
283         const char *volume_key,
284         size_t volume_key_size,
285         const char *passphrase,
286         size_t passphrase_size);
287
288 /**
289  * Destroy (and disable) key slot
290  *
291  * Returns 0 on success or negative errno value otherwise.
292  *
293  * @cd - crypt device handle
294  * @keyslot - requested key slot to destroy
295  *
296  * Note that there is no passphrase verification used.
297  */
298 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
299
300 /**
301  * Activation flags
302  */
303 #define CRYPT_ACTIVATE_READONLY (1 << 0)
304 #define CRYPT_ACTIVATE_NO_UUID  (1 << 1)
305
306 /**
307  * Activate device or check passphrase
308  *
309  * Returns unlocked key slot number or negative errno otherwise.
310  *
311  * @cd - crypt device handle
312  * @name - name of device to create, if NULL only check passphrase
313  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
314  * @passphrase - passphrase used to unlock volume key, NULL for query
315  * @passphrase_size - size of @passphrase
316  * @flags - activation flags
317  */
318 int crypt_activate_by_passphrase(struct crypt_device *cd,
319         const char *name,
320         int keyslot,
321         const char *passphrase,
322         size_t passphrase_size,
323         uint32_t flags);
324
325 /**
326  * Activate device or check using key file
327  *
328  * Returns unlocked key slot number or negative errno otherwise.
329  *
330  * @cd - crypt device handle
331  * @name - name of device to create, if NULL only check keyfile
332  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
333  * @keyfile - key file used to unlock volume key
334  * @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
335  * @flags - activation flags
336  */
337 int crypt_activate_by_keyfile(struct crypt_device *cd,
338         const char *name,
339         int keyslot,
340         const char *keyfile,
341         size_t keyfile_size,
342         uint32_t flags);
343
344 /**
345  * Activate device using provided volume key
346  *
347  * Returns 0 on success or negative errno value otherwise.
348  *
349  * @cd - crypt device handle
350  * @name - name of device to create, if NULL only check volume key
351  * @volume_key - provided volume key
352  * @volume_key_size - size of @volume_key
353  * @flags - activation flags
354  */
355 int crypt_activate_by_volume_key(struct crypt_device *cd,
356         const char *name,
357         const char *volume_key,
358         size_t volume_key_size,
359         uint32_t flags);
360
361 /**
362  * Deactivate crypt device
363  *
364  * @cd - crypt device handle, can be NULL
365  * @name - name of device to deactivate
366   */
367 int crypt_deactivate(struct crypt_device *cd, const char *name);
368
369 /**
370  * Get volume key from of crypt device
371  *
372  * Returns unlocked key slot number or negative errno otherwise.
373  *
374  * @cd - crypt device handle
375  * @keyslot - use this keyslot or CRYPT_ANY_SLOT
376  * @volume_key - buffer for volume key
377  * @volume_key_size - on input, size of buffer @volume_key,
378  *                    on output size of @volume_key
379  * @passphrase - passphrase used to unlock volume key, NULL for query
380  * @passphrase_size - size of @passphrase
381  */
382 int crypt_volume_key_get(struct crypt_device *cd,
383         int keyslot,
384         char *volume_key,
385         size_t *volume_key_size,
386         const char *passphrase,
387         size_t passphrase_size);
388
389 /**
390  * Verify that provided volume key is valid for crypt device
391  *
392  * Returns 0 on success or negative errno value otherwise.
393  *
394  * @cd - crypt device handle
395  * @volume_key - provided volume key
396  * @volume_key_size - size of @volume_key
397  */
398 int crypt_volume_key_verify(struct crypt_device *cd,
399         const char *volume_key,
400         size_t volume_key_size);
401
402 /**
403  * Get status info about device name
404  *
405  * Returns value defined by crypt_status_info.
406  *
407  * @cd - crypt device handle, can be NULL
408  * @name -crypt device name
409  *
410  * CRYPT_INACTIVE - no such mapped device
411  * CRYPT_ACTIVE - device is active
412  * CRYPT_BUSY - device is active and has open count > 0
413  */
414 typedef enum {
415         CRYPT_INVALID,
416         CRYPT_INACTIVE,
417         CRYPT_ACTIVE,
418         CRYPT_BUSY
419 } crypt_status_info;
420 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
421
422 /**
423  * Dump text-formatted information about crypt device to log output
424  *
425  * Returns 0 on success or negative errno value otherwise.
426  *
427  * @cd - crypt device handle, can be NULL
428  */
429 int crypt_dump(struct crypt_device *cd);
430
431 /**
432  * Various crypt device info functions
433  *
434  * @cd - crypt device handle
435  *
436  * cipher - used cipher, e.g. "aes" or NULL otherwise
437  * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
438  * uuid - device UUID or NULL if not set
439  * data_offset - device offset in sectors where real data starts on underlying device)
440  * volume_key_size - size (in bytes) of volume key for crypt device
441  */
442 const char *crypt_get_cipher(struct crypt_device *cd);
443 const char *crypt_get_cipher_mode(struct crypt_device *cd);
444 const char *crypt_get_uuid(struct crypt_device *cd);
445 uint64_t crypt_get_data_offset(struct crypt_device *cd);
446 int crypt_get_volume_key_size(struct crypt_device *cd);
447
448 /**
449  * Get information about particular key slot
450  *
451  * Returns value defined by crypt_keyslot_info.
452  *
453  * @cd - crypt device handle
454  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
455  */
456 typedef enum {
457         CRYPT_SLOT_INVALID,
458         CRYPT_SLOT_INACTIVE,
459         CRYPT_SLOT_ACTIVE,
460         CRYPT_SLOT_ACTIVE_LAST
461 } crypt_keyslot_info;
462 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
463
464 /**
465  * Backup header and keyslots to file
466  *
467  * Returns 0 on success or negative errno value otherwise.
468  *
469  * @cd - crypt device handle
470  * @requested_type - type of header to backup
471  * @backup_file - file to backup header to
472  */
473 int crypt_header_backup(struct crypt_device *cd,
474         const char *requested_type,
475         const char *backup_file);
476
477 /**
478  * Restore header and keyslots from backup file
479  *
480  * Returns 0 on success or negative errno value otherwise.
481  *
482  * @cd - crypt device handle
483  * @requested_type - type of header to restore
484  * @backup_file - file to restore header from
485  */
486 int crypt_header_restore(struct crypt_device *cd,
487         const char *requested_type,
488         const char *backup_file);
489
490 /**
491  * Receives last reported error
492  *
493  * @buf - buffef for message
494  * @size - size of buffer
495  *
496  * Note that this is old API function using global context.
497  * All error messages are reported also through log callback.
498  */
499 void crypt_get_error(char *buf, size_t size);
500
501 /**
502  * Get directory where mapped crypt devices are created
503  */
504 const char *crypt_get_dir(void);
505
506 /**
507  * Set library debug level
508  */
509 #define CRYPT_DEBUG_ALL  -1
510 #define CRYPT_DEBUG_NONE  0
511 void crypt_set_debug_level(int level);
512
513 /**
514  * OLD DEPRECATED API **********************************
515  *
516  * Provided only for backward compatibility.
517  */
518
519 struct interface_callbacks {
520     int (*yesDialog)(char *msg);
521     void (*log)(int level, char *msg);
522 };
523
524 #define CRYPT_FLAG_VERIFY               (1 << 0)
525 #define CRYPT_FLAG_READONLY             (1 << 1)
526 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE   (1 << 2)
527 #define CRYPT_FLAG_VERIFY_ON_DELKEY     (1 << 3)
528 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
529
530 struct crypt_options {
531         const char      *name;
532         const char      *device;
533
534         const char      *cipher;
535         const char      *hash;
536
537         const char      *passphrase;
538         int             passphrase_fd;
539         const char      *key_file;
540         const char      *new_key_file;
541         int             key_size;
542
543         unsigned int    flags;
544         int             key_slot;
545
546         uint64_t        size;
547         uint64_t        offset;
548         uint64_t        skip;
549         uint64_t        iteration_time;
550         uint64_t        timeout;
551
552         uint64_t        align_payload;
553         int             tries;
554
555         struct interface_callbacks *icb;
556 };
557
558 int crypt_create_device(struct crypt_options *options);
559 int crypt_update_device(struct crypt_options *options);
560 int crypt_resize_device(struct crypt_options *options);
561 int crypt_query_device(struct crypt_options *options);
562 int crypt_remove_device(struct crypt_options *options);
563 int crypt_luksFormat(struct crypt_options *options);
564 int crypt_luksOpen(struct crypt_options *options);
565 int crypt_luksKillSlot(struct crypt_options *options);
566 int crypt_luksRemoveKey(struct crypt_options *options);
567 int crypt_luksAddKey(struct crypt_options *options);
568 int crypt_luksUUID(struct crypt_options *options);
569 int crypt_isLuks(struct crypt_options *options);
570 int crypt_luksDump(struct crypt_options *options);
571
572 void crypt_put_options(struct crypt_options *options);
573
574 #ifdef __cplusplus
575 }
576 #endif
577 #endif /* _LIBCRYPTSETUP_H */