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