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