Allow manipulate with device with only header on it (no data).
[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  * @class  - log type 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 class, const char *msg, void *usrptr),
48         void *usrptr);
49
50 /**
51  * Log message through log function.
52  *
53  * @cd - crypt device handle
54  * @class  - log type
55  * @msg    - log message
56  */
57 void crypt_log(struct crypt_device *cd, int class, 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  * INACTIVE - no such mapped device
411  * ACTIVE - device is active
412  * BUSY - device is active and has open count > 0
413  */
414 typedef enum { INVALID, INACTIVE, ACTIVE, BUSY } crypt_status_info;
415 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
416
417 /**
418  * Dump text-formatted information about crypt device to log output
419  *
420  * Returns 0 on success or negative errno value otherwise.
421  *
422  * @cd - crypt device handle, can be NULL
423  */
424 int crypt_dump(struct crypt_device *cd);
425
426 /**
427  * Various crypt device info functions
428  *
429  * @cd - crypt device handle
430  *
431  * cipher - used cipher, e.g. "aes" or NULL otherwise
432  * cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
433  * uuid - device UUID or NULL if not set
434  * data_offset - device offset in sectors where real data starts on underlying device)
435  * volume_key_size - size (in bytes) of volume key for crypt device
436  */
437 const char *crypt_get_cipher(struct crypt_device *cd);
438 const char *crypt_get_cipher_mode(struct crypt_device *cd);
439 const char *crypt_get_uuid(struct crypt_device *cd);
440 uint64_t crypt_get_data_offset(struct crypt_device *cd);
441 int crypt_get_volume_key_size(struct crypt_device *cd);
442
443 /**
444  * Get information about particular key slot
445  *
446  * Returns value defined by crypt_keyslot_info.
447  *
448  * @cd - crypt device handle
449  * @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
450  */
451 typedef enum { SLOT_INVALID, SLOT_INACTIVE, SLOT_ACTIVE, SLOT_ACTIVE_LAST } crypt_keyslot_info;
452 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
453
454 /**
455  * Backup header and keyslots to file
456  *
457  * Returns 0 on success or negative errno value otherwise.
458  *
459  * @cd - crypt device handle
460  * @requested_type - type of header to backup
461  * @backup_file - file to backup header to
462  */
463 int crypt_header_backup(struct crypt_device *cd,
464         const char *requested_type,
465         const char *backup_file);
466
467 /**
468  * Restore header and keyslots from backup file
469  *
470  * Returns 0 on success or negative errno value otherwise.
471  *
472  * @cd - crypt device handle
473  * @requested_type - type of header to restore
474  * @backup_file - file to restore header from
475  */
476 int crypt_header_restore(struct crypt_device *cd,
477         const char *requested_type,
478         const char *backup_file);
479
480 /**
481  * Receives last reported error
482  *
483  * @buf - buffef for message
484  * @size - size of buffer
485  *
486  * Note that this is old API function using global context.
487  * All error messages are reported also through log callback.
488  */
489 void crypt_get_error(char *buf, size_t size);
490
491 /**
492  * Get directory where mapped crypt devices are created
493  */
494 const char *crypt_get_dir(void);
495
496 /**
497  * Set library debug level
498  */
499 #define CRYPT_DEBUG_ALL  -1
500 #define CRYPT_DEBUG_NONE  0
501 void crypt_set_debug_level(int level);
502
503 /**
504  * OLD DEPRECATED API **********************************
505  *
506  * Provided only for backward compatibility.
507  */
508
509 struct interface_callbacks {
510     int (*yesDialog)(char *msg);
511     void (*log)(int class, char *msg);
512 };
513
514 #define CRYPT_FLAG_VERIFY               (1 << 0)
515 #define CRYPT_FLAG_READONLY             (1 << 1)
516 #define CRYPT_FLAG_VERIFY_IF_POSSIBLE   (1 << 2)
517 #define CRYPT_FLAG_VERIFY_ON_DELKEY     (1 << 3)
518 #define CRYPT_FLAG_NON_EXCLUSIVE_ACCESS (1 << 4)
519
520 struct crypt_options {
521         const char      *name;
522         const char      *device;
523
524         const char      *cipher;
525         const char      *hash;
526
527         const char      *passphrase;
528         int             passphrase_fd;
529         const char      *key_file;
530         const char      *new_key_file;
531         int             key_size;
532
533         unsigned int    flags;
534         int             key_slot;
535
536         uint64_t        size;
537         uint64_t        offset;
538         uint64_t        skip;
539         uint64_t        iteration_time;
540         uint64_t        timeout;
541
542         uint64_t        align_payload;
543         int             tries;
544
545         struct interface_callbacks *icb;
546 };
547
548 int crypt_create_device(struct crypt_options *options);
549 int crypt_update_device(struct crypt_options *options);
550 int crypt_resize_device(struct crypt_options *options);
551 int crypt_query_device(struct crypt_options *options);
552 int crypt_remove_device(struct crypt_options *options);
553 int crypt_luksFormat(struct crypt_options *options);
554 int crypt_luksOpen(struct crypt_options *options);
555 int crypt_luksKillSlot(struct crypt_options *options);
556 int crypt_luksRemoveKey(struct crypt_options *options);
557 int crypt_luksAddKey(struct crypt_options *options);
558 int crypt_luksUUID(struct crypt_options *options);
559 int crypt_isLuks(struct crypt_options *options);
560 int crypt_luksDump(struct crypt_options *options);
561
562 void crypt_put_options(struct crypt_options *options);
563
564 #ifdef __cplusplus
565 }
566 #endif
567 #endif /* _LIBCRYPTSETUP_H */