04c53fb5445898742f1c4aadda4fb3d4ae1547b4
[platform/upstream/cryptsetup.git] / lib / libcryptsetup.h
1 /*
2  * libcryptsetup - cryptsetup library
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /**
23  * @file libcryptsetup.h
24  * @brief Public cryptsetup API
25  *
26  * For more verbose examples of LUKS related use cases,
27  * please read @ref index "examples".
28  */
29
30 #ifndef _LIBCRYPTSETUP_H
31 #define _LIBCRYPTSETUP_H
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #include <stddef.h>
37 #include <stdint.h>
38
39 struct crypt_device; /* crypt device handle */
40
41 /**
42  * Initialize crypt device handle and check if provided device exists.
43  *
44  * @param cd Returns pointer to crypt device handle
45  * @param device Path to the backing device.
46  *        If @e device is not a block device but a path to some file,
47  *        the function will try to create a loopdevice and attach
48  *        the file to the loopdevice with AUTOCLEAR flag set.
49  *        If @e device is @e NULL function it will initialize dm backend only.
50  *
51  * @return @e 0 on success or negative errno value otherwise.
52  *
53  * @note Note that logging is not initialized here, possible messages uses
54  *       default log function.
55  */
56 int crypt_init(struct crypt_device **cd, const char *device);
57
58 /**
59  * Initialize crypt device handle from provided active device name,
60  * and, optionally, from separate metadata (header) device
61  * and check if provided device exists.
62  *
63  * @return @e 0 on success or negative errno value otherwise.
64  *
65  * @param cd returns crypt device handle for active device
66  * @param name name of active crypt device
67  * @param header_device optional device containing on-disk header
68  *        (@e NULL if it the same as underlying device on there is no on-disk header)
69  *
70  * @post In case @e device points to active LUKS device but header load fails,
71  * context device type is set to @e NULL and @e 0 is returned as if it were successful.
72  * Context with @e NULL device type can only be deactivated by crypt_deactivate
73  *
74  * @note @link crypt_init_by_name @endlink is equivalent to calling
75  *       crypt_init_by_name_and_header(cd, name, NULL);
76  */
77 int crypt_init_by_name_and_header(struct crypt_device **cd,
78                                   const char *name,
79                                   const char *header_device);
80
81 /**
82  * This is equivalent to call
83  * @ref crypt_init_by_name_and_header "crypt_init_by_name_and_header(cd, name, NULL)"
84  *
85  * @sa crypt_init_by_name_and_header
86  */
87 int crypt_init_by_name(struct crypt_device **cd, const char *name);
88
89 /**
90  * @defgroup loglevel "Cryptsetup logging"
91  *
92  * Set of functions and defines used in cryptsetup for
93  * logging purposes
94  *
95  */
96
97 /**
98  * @addtogroup loglevel
99  * @{
100  */
101
102 /** normal log level */
103 #define CRYPT_LOG_NORMAL 0
104 /** error log level */
105 #define CRYPT_LOG_ERROR  1
106 /** verbose log level */
107 #define CRYPT_LOG_VERBOSE  2
108 /** debug log level - always on stdout */
109 #define CRYPT_LOG_DEBUG -1
110
111 /**
112  * Set log function.
113  *
114  * @param cd crypt device handle (can be @e NULL to set default log function)
115  * @param log user defined log function reference
116  * @param usrptr provided identification in callback
117  * @param level log level below (debug messages can uses other levels)
118  * @param msg log message
119  */
120 void crypt_set_log_callback(struct crypt_device *cd,
121         void (*log)(int level, const char *msg, void *usrptr),
122         void *usrptr);
123
124 /**
125  * Defines log function or use the default one otherwise.
126  *
127  * @see crypt_set_log_callback
128  *
129  * @param cd crypt device handle
130  * @param level log level
131  * @param msg log message
132  */
133 void crypt_log(struct crypt_device *cd, int level, const char *msg);
134 /** @} */
135
136 /**
137  * Set confirmation callback (yes/no)
138  *
139  * If code need confirmation (like resetting uuid or restoring LUKS header from file)
140  * this function is called. If not defined, everything is confirmed.
141  *
142  * Callback function @e confirm should return @e 0 if operation is declined,
143  * other values mean accepted.
144  *
145  * @param cd crypt device handle
146  * @param confirm user defined confirm callback reference
147  * @param usrptr provided identification in callback
148  * @param msg Message for user to confirm
149  *
150  * @note Current version of cryptsetup API requires confirmation only when UUID is being changed
151  */
152 void crypt_set_confirm_callback(struct crypt_device *cd,
153         int (*confirm)(const char *msg, void *usrptr),
154         void *usrptr);
155
156 /**
157  * Set password query callback.
158  *
159  * If code need @e _interactive_ query for password, this callback is called.
160  * If not defined, compiled-in default is called (uses terminal input).
161  *
162  * Callback should return length of password in buffer
163  * or negative errno value in case of error.
164  *
165  * @param cd crypt device handle
166  * @param password user defined password callback reference
167  * @param usrptr provided identification in callback
168  * @param msg Message for user
169  * @param buf buffer for password
170  * @param length size of buffer
171  *
172  * @note Note that if this function is defined, verify option is ignored
173  *   (caller which provided callback is responsible for password verification)
174  * @note Only zero terminated passwords can be entered this way, for complex
175  *   use API functions directly.
176  * @note Maximal length of password is limited to @e length @e - @e 1 (minimal 511 chars)
177  *
178  * @see Callback function is used in these call provided, that certain conditions are met:
179  * @li crypt_keyslot_add_by_passphrase
180  * @li crypt_activate_by_passphrase
181  * @li crypt_resume_by_passphrase
182  * @li crypt_resume_by_keyfile
183  * @li crypt_keyslot_add_by_keyfile
184  * @li crypt_keyslot_add_by_volume_key
185  *
186  */
187 void crypt_set_password_callback(struct crypt_device *cd,
188         int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
189         void *usrptr);
190
191 /**
192  * Set timeout for interactive password entry using default
193  * password callback
194  *
195  * @param cd crypt device handle
196  * @param timeout_sec timeout in seconds
197  */
198 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
199
200 /**
201  * Set number of retries in case password input has been incorrect
202  *
203  * @param cd crypt device handle
204  * @param tries the number
205  */
206 void crypt_set_password_retry(struct crypt_device *cd, int tries);
207
208 /**
209  * Set how long should cryptsetup iterate in PBKDF2 function.
210  * Default value heads towards the iterations which takes around 1 second
211  *
212  * @param cd crypt device handle
213  * @param iteration_time_ms the time in ms
214  */
215 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms);
216 /* Don't ask :-) */
217 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
218
219 /**
220  * Set whether passphrase will be verified on input
221  * (user has to input same passphrase twice)
222  *
223  * @param cd crypt device handle
224  * @param password_verify @e 0 = false, @e !0 true
225  */
226 void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
227
228 /**
229  * Set data device
230  * For LUKS it is encrypted data device when LUKS header is separated.
231  * For VERITY it is data device when hash device is separated.
232  *
233  * @param cd crypt device handle
234  * @param device path to device
235  *
236  */
237 int crypt_set_data_device(struct crypt_device *cd, const char *device);
238
239 /**
240  * @defgroup rng "Cryptsetup RNG"
241  *
242  * @addtogroup rng
243  * @{
244  *
245  */
246
247 /** CRYPT_RNG_URANDOM - use /dev/urandom */
248 #define CRYPT_RNG_URANDOM 0
249 /** CRYPT_RNG_RANDOM  - use /dev/random (waits if no entropy in system) */
250 #define CRYPT_RNG_RANDOM  1
251
252 /**
253  * Set which RNG (random number generator) is used for generating long term key
254  *
255  * @param cd crypt device handle
256  * @param rng_type kernel random number generator to use
257  *
258  */
259 void crypt_set_rng_type(struct crypt_device *cd, int rng_type);
260
261 /**
262  * Get which RNG (random number generator) is used for generating long term key
263  *
264  * @param cd crypt device handle
265  * @return RNG type on success or negative errno value otherwise.
266  *
267  */
268 int crypt_get_rng_type(struct crypt_device *cd);
269
270 /** @} */
271
272 /**
273  * Helper to lock/unlock memory to avoid swap sensitive data to disk
274  *
275  * @param cd crypt device handle, can be @e NULL
276  * @param lock 0 to unlock otherwise lock memory
277  *
278  * @returns Value indicating whether the memory is locked (function can be called multiple times).
279  *
280  * @note Only root can do this.
281  * @note It locks/unlocks all process memory, not only crypt context.
282  */
283 int crypt_memory_lock(struct crypt_device *cd, int lock);
284
285 /**
286  * @defgroup crypt_type "Cryptsetup on-disk format types"
287  *
288  * Set of functions, \#defines and structs related
289  * to on-disk format types
290  */
291
292 /**
293  * @addtogroup crypt_type
294  * @{
295  */
296
297 /** plain crypt device, no on-disk header */
298 #define CRYPT_PLAIN "PLAIN"
299 /** LUKS version 1 header on-disk */
300 #define CRYPT_LUKS1 "LUKS1"
301 /** loop-AES compatibility mode */
302 #define CRYPT_LOOPAES "LOOPAES"
303 /** dm-verity mode */
304 #define CRYPT_VERITY "VERITY"
305 /** TCRYPT (TrueCrypt-compatible) mode */
306 #define CRYPT_TCRYPT "TCRYPT"
307
308 /**
309  * Get device type
310  *
311  * @param cd crypt device handle
312  * @return string according to device type or @e NULL if not known.
313  */
314 const char *crypt_get_type(struct crypt_device *cd);
315
316 /**
317  *
318  * Structure used as parameter for PLAIN device type
319  *
320  * @see crypt_format
321  */
322 struct crypt_params_plain {
323         const char *hash; /**< password hash function */
324         uint64_t offset; /**< offset in sectors */
325         uint64_t skip; /**< IV offset / initialization sector */
326         uint64_t size; /**< size of mapped device or @e 0 for autodetection */
327 };
328
329 /**
330  * Structure used as parameter for LUKS device type
331  *
332  * @see crypt_format, crypt_load
333  *
334  * @note during crypt_format @e data_device attribute determines
335  *       if the LUKS header is separated from encrypted payload device
336  *
337  */
338 struct crypt_params_luks1 {
339         const char *hash; /**< hash used in LUKS header */
340         size_t data_alignment; /**< data alignment in sectors, data offset is multiple of this */
341         const char *data_device; /**< detached encrypted data device or @e NULL */
342 };
343
344 /**
345  *
346  * Structure used as parameter for loop-AES device type
347  *
348  * @see crypt_format
349  *
350  */
351 struct crypt_params_loopaes {
352         const char *hash; /**< key hash function */
353         uint64_t offset;  /**< offset in sectors */
354         uint64_t skip;    /**< IV offset / initialization sector */
355 };
356
357 /**
358  *
359  * Structure used as parameter for dm-verity device type
360  *
361  * @see crypt_format, crypt_load
362  *
363  */
364 /** No on-disk header (only hashes) */
365 #define CRYPT_VERITY_NO_HEADER   (1 << 0)
366 /** Verity hash in userspace before activation */
367 #define CRYPT_VERITY_CHECK_HASH  (1 << 1)
368 /** Create hash - format hash device */
369 #define CRYPT_VERITY_CREATE_HASH (1 << 2)
370
371 struct crypt_params_verity {
372         const char *hash_name;     /**< hash function */
373         const char *data_device;   /**< data_device (CRYPT_VERITY_CREATE_HASH) */
374         const char *hash_device;   /**< hash_device (output only) */
375         const char *salt;          /**< salt */
376         uint32_t salt_size;        /**< salt size (in bytes) */
377         uint32_t hash_type;        /**< in-kernel hashing type */
378         uint32_t data_block_size;  /**< data block size (in bytes) */
379         uint32_t hash_block_size;  /**< hash block size (in bytes) */
380         uint64_t data_size;        /**< data area size (in data blocks) */
381         uint64_t hash_area_offset; /**< hash/header offset (in bytes) */
382         uint32_t flags;            /**< CRYPT_VERITY* flags */
383 };
384
385 /**
386  *
387  * Structure used as parameter for TCRYPT device type
388  *
389  * @see crypt_format
390  *
391  */
392 /** Include legacy modes ehn scannig for header*/
393 #define CRYPT_TCRYPT_LEGACY_MODES    (1 << 0)
394 /** Try to load hidden header (describing hidden device) */
395 #define CRYPT_TCRYPT_HIDDEN_HEADER   (1 << 1)
396 /** Try to load backup header */
397 #define CRYPT_TCRYPT_BACKUP_HEADER   (1 << 2)
398
399 struct crypt_params_tcrypt {
400         const char *passphrase;    /**< passphrase to unlock header (input only) */
401         size_t passphrase_size;    /**< passphrase size (input only) */
402         const char **keyfiles;     /**< keyfile paths to unlock header (input only) */
403         unsigned int keyfiles_count;/**< keyfiles count (input only) */
404         const char *hash_name;     /**< hash function for PBKDF */
405         const char *cipher;        /**< cipher chain c1[-c2[-c3]] */
406         const char *mode;          /**< cipher block mode */
407         size_t key_size;           /**< key size in bytes (the whole chain) */
408         uint32_t flags;            /**< CRYPT_TCRYPT* flags */
409 };
410
411 /** @} */
412
413 /**
414  * Create (format) new crypt device (and possible header on-disk) but not activates it.
415  *
416  * @pre @e cd contains initialized and not formatted device context (device type must @b not be set)
417  *
418  * @param cd crypt device handle
419  * @param type type of device (optional params struct must be of this type)
420  * @param cipher (e.g. "aes")
421  * @param cipher_mode including IV specification (e.g. "xts-plain")
422  * @param uuid requested UUID or @e NULL if it should be generated
423  * @param volume_key pre-generated volume key or @e NULL if it should be generated (only for LUKS)
424  * @param volume_key_size size of volume key in bytes.
425  * @param params crypt type specific parameters (see @link crypt_type @endlink)
426  *
427  * @returns @e 0 on success or negative errno value otherwise.
428  *
429  * @note Note that crypt_format does not enable any keyslot (in case of work with LUKS device),
430  *      but it stores volume key internally and subsequent crypt_keyslot_add_* calls can be used.
431  * @note For VERITY @link crypt_type @endlink, only uuid parameter is used, others paramaters
432  *      are ignored and verity specific attributes are set through mandatory params option.
433  */
434 int crypt_format(struct crypt_device *cd,
435         const char *type,
436         const char *cipher,
437         const char *cipher_mode,
438         const char *uuid,
439         const char *volume_key,
440         size_t volume_key_size,
441         void *params);
442
443 /**
444  * Set new UUID for already existing device
445  *
446  * @param cd crypt device handle
447  * @param uuid requested UUID or @e NULL if it should be generated
448  *
449  * @returns 0 on success or negative errno value otherwise.
450  *
451  * @note Currently, only LUKS device type are supported
452  */
453 int crypt_set_uuid(struct crypt_device *cd,
454                    const char *uuid);
455
456 /**
457  * Load crypt device parameters from on-disk header
458  *
459  * @param cd crypt device handle
460  * @param requested_type @link crypt_type @endlink or @e NULL for all known
461  * @param params crypt type specific parameters (see @link crypt_type @endlink)
462  *
463  * @returns 0 on success or negative errno value otherwise.
464  *
465  * @post In case LUKS header is read successfully but payload device is too small
466  * error is returned and device type in context is set to @e NULL
467  *
468  * @note Note that in current version load works only for LUKS and VERITY device type.
469  *
470  */
471 int crypt_load(struct crypt_device *cd,
472                const char *requested_type,
473                void *params);
474
475 /**
476  * Try to repair crypt device on-disk header if invalid
477  *
478  * @param cd crypt device handle
479  * @param requested_type @link crypt_type @endlink or @e NULL for all known
480  * @param params crypt type specific parameters (see @link crypt_type @endlink)
481  *
482  * @returns 0 on success or negative errno value otherwise.
483  *
484  */
485 int crypt_repair(struct crypt_device *cd,
486                  const char *requested_type,
487                  void *params);
488
489 /**
490  * Resize crypt device
491  *
492  * @param cd - crypt device handle
493  * @param name - name of device to resize
494  * @param new_size - new device size in sectors or @e 0 to use all of the underlying device size
495  *
496  * @return @e 0 on success or negative errno value otherwise.
497  */
498 int crypt_resize(struct crypt_device *cd,
499                  const char *name,
500                  uint64_t new_size);
501
502 /**
503  * Suspends crypt device.
504  *
505  * @param cd crypt device handle, can be @e NULL
506  * @param name name of device to suspend
507  *
508  * @return 0 on success or negative errno value otherwise.
509  *
510  * @note Only LUKS device type is supported
511  *
512  */
513 int crypt_suspend(struct crypt_device *cd,
514                   const char *name);
515
516 /**
517  * Resumes crypt device using passphrase.
518  *
519  *
520  * @param cd crypt device handle
521  * @param name name of device to resume
522  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
523  * @param passphrase passphrase used to unlock volume key, @e NULL for query
524  * @param passphrase_size size of @e passphrase (binary data)
525  *
526  * @return unlocked key slot number or negative errno otherwise.
527  *
528  * @note Only LUKS device type is supported
529  */
530 int crypt_resume_by_passphrase(struct crypt_device *cd,
531         const char *name,
532         int keyslot,
533         const char *passphrase,
534         size_t passphrase_size);
535
536 /**
537  * Resumes crypt device using key file.
538  *
539  * @param cd crypt device handle
540  * @param name name of device to resume
541  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
542  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
543  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
544  * @param keyfile_offset number of bytes to skip at start of keyfile
545  *
546  * @return unlocked key slot number or negative errno otherwise.
547  */
548 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
549         const char *name,
550         int keyslot,
551         const char *keyfile,
552         size_t keyfile_size,
553         size_t keyfile_offset);
554 /**
555  * Backward compatible crypt_resume_by_keyfile_offset() (without offset).
556  */
557 int crypt_resume_by_keyfile(struct crypt_device *cd,
558         const char *name,
559         int keyslot,
560         const char *keyfile,
561         size_t keyfile_size);
562
563 /**
564  * Releases crypt device context and used memory.
565  *
566  * @param cd crypt device handle
567  */
568 void crypt_free(struct crypt_device *cd);
569
570 /**
571  * @defgroup keyslot "Cryptsetup LUKS keyslots"
572  * @addtogroup keyslot
573  * @{
574  *
575  */
576
577 /** iterate through all keyslots and find first one that fits */
578 #define CRYPT_ANY_SLOT -1
579
580 /**
581  * Add key slot using provided passphrase
582  *
583  * @pre @e cd contains initialized and formatted LUKS device context
584  *
585  * @param cd crypt device handle
586  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
587  * @param passphrase passphrase used to unlock volume key, @e NULL for query
588  * @param passphrase_size size of passphrase (binary data)
589  * @param new_passphrase passphrase for new keyslot, @e NULL for query
590  * @param new_passphrase_size size of @e new_passphrase (binary data)
591  *
592  * @return allocated key slot number or negative errno otherwise.
593  */
594 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
595         int keyslot,
596         const char *passphrase,
597         size_t passphrase_size,
598         const char *new_passphrase,
599         size_t new_passphrase_size);
600
601 /**
602 * Add key slot using provided key file path
603  *
604  * @pre @e cd contains initialized and formatted LUKS device context
605  *
606  * @param cd crypt device handle
607  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
608  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
609  * @param keyfile_size number of bytes to read from keyfile, @e 0 is unlimited
610  * @param keyfile_offset number of bytes to skip at start of keyfile
611  * @param new_keyfile keyfile for new keyslot, @e NULL for passphrase query
612  * @param new_keyfile_size number of bytes to read from @e new_keyfile, @e 0 is unlimited
613  * @param new_keyfile_offset number of bytes to skip at start of new_keyfile
614  *
615  * @return allocated key slot number or negative errno otherwise.
616  *
617  * @note Note that @e keyfile can be "-" for STDIN
618  *
619  */
620 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
621         int keyslot,
622         const char *keyfile,
623         size_t keyfile_size,
624         size_t keyfile_offset,
625         const char *new_keyfile,
626         size_t new_keyfile_size,
627         size_t new_keyfile_offset);
628 /**
629  * Backward compatible crypt_keyslot_add_by_keyfile_offset() (without offset).
630  */
631 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
632         int keyslot,
633         const char *keyfile,
634         size_t keyfile_size,
635         const char *new_keyfile,
636         size_t new_keyfile_size);
637
638 /**
639  * Add key slot using provided volume key
640  *
641  * @pre @e cd contains initialized and formatted LUKS device context
642  *
643  * @param cd crypt device handle
644  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
645  * @param volume_key provided volume key or @e NULL if used after crypt_format
646  * @param volume_key_size size of volume_key
647  * @param passphrase passphrase for new keyslot, @e NULL for query
648  * @param passphrase_size size of passphrase
649  *
650  * @return allocated key slot number or negative errno otherwise.
651  *
652  */
653 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
654         int keyslot,
655         const char *volume_key,
656         size_t volume_key_size,
657         const char *passphrase,
658         size_t passphrase_size);
659
660 /**
661  * Destroy (and disable) key slot
662  *
663  * @pre @e cd contains initialized and formatted LUKS device context
664  *
665  * @param cd crypt device handle
666  * @param keyslot requested key slot to destroy
667  *
668  * @return @e 0 on success or negative errno value otherwise.
669  *
670  * @note Note that there is no passphrase verification used.
671  */
672 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
673
674 /** @} */
675
676 /**
677  * @defgroup aflags "Device runtime attributes"
678  *
679  * Activation flags
680  *
681  * @addtogroup aflags
682  * @{
683  *
684  */
685 /** device is read only */
686 #define CRYPT_ACTIVATE_READONLY (1 << 0)
687 /** only reported for device without uuid */
688 #define CRYPT_ACTIVATE_NO_UUID  (1 << 1)
689 /** activate even if cannot grant exclusive access (DANGEROUS) */
690 #define CRYPT_ACTIVATE_SHARED   (1 << 2)
691 /** enable discards aka TRIM */
692 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3)
693 /** skip global udev rules in activation ("private device"), input only */
694 #define CRYPT_ACTIVATE_PRIVATE (1 << 4)
695 /** corruption detected (verity), output only */
696 #define CRYPT_ACTIVATE_CORRUPTED (1 << 5)
697
698 /**
699  * Active device runtime attributes
700  */
701 struct crypt_active_device {
702         uint64_t offset; /**< offset in sectors */
703         uint64_t iv_offset; /**< IV initialization sector */
704         uint64_t size; /**< active device size */
705         uint32_t flags; /**< activation flags */
706 };
707
708 /**
709  * Receives runtime attributes of active crypt device
710  *
711  * @param cd crypt device handle (can be @e NULL)
712  * @param name name of active device
713  * @param cad preallocated active device attributes to fill
714  *
715  * @return @e 0 on success or negative errno value otherwise
716  *
717  */
718 int crypt_get_active_device(struct crypt_device *cd,
719                             const char *name,
720                             struct crypt_active_device *cad);
721
722 /** @} */
723
724 /**
725  * Activate device or check passphrase
726  *
727  * @param cd crypt device handle
728  * @param name name of device to create, if @e NULL only check passphrase
729  * @param keyslot requested keyslot to check or @e CRYPT_ANY_SLOT
730  * @param passphrase passphrase used to unlock volume key, @e NULL for query
731  * @param passphrase_size size of @e passphrase
732  * @param flags activation flags
733  *
734  * @return unlocked key slot number or negative errno otherwise.
735  */
736 int crypt_activate_by_passphrase(struct crypt_device *cd,
737         const char *name,
738         int keyslot,
739         const char *passphrase,
740         size_t passphrase_size,
741         uint32_t flags);
742
743 /**
744  * Activate device or check using key file
745  *
746  * @param cd crypt device handle
747  * @param name name of device to create, if @e NULL only check keyfile
748  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
749  * @param keyfile key file used to unlock volume key
750  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
751  * @param keyfile_offset number of bytes to skip at start of keyfile
752  * @param flags activation flags
753  *
754  * @return unlocked key slot number or negative errno otherwise.
755  */
756 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
757         const char *name,
758         int keyslot,
759         const char *keyfile,
760         size_t keyfile_size,
761         size_t keyfile_offset,
762         uint32_t flags);
763 /**
764  * Backward compatible crypt_activate_by_keyfile_offset() (without offset).
765  */
766 int crypt_activate_by_keyfile(struct crypt_device *cd,
767         const char *name,
768         int keyslot,
769         const char *keyfile,
770         size_t keyfile_size,
771         uint32_t flags);
772
773 /**
774  * Activate device using provided volume key
775  *
776  *
777  * @param cd crypt device handle
778  * @param name name of device to create, if @e NULL only check volume key
779  * @param volume_key provided volume key (or @e NULL to use internal)
780  * @param volume_key_size size of volume_key
781  * @param flags activation flags
782  *
783  * @return @e 0 on success or negative errno value otherwise.
784  *
785  * @note If @e NULL is used for volume_key, device has to be initialized
786  *       by previous operation (like @ref crypt_format
787  *       or @ref crypt_init_by_name)
788  * @note For VERITY the volume key means root hash required for activation.
789  *       Because kernel dm-verity is always read only, you have to provide
790  *       CRYPT_ACTIVATE_READONLY flag always.
791  * @note For TCRYPT the volume key should be always NULL and because master
792  *       key from decrypted header is used instead.
793  */
794 int crypt_activate_by_volume_key(struct crypt_device *cd,
795         const char *name,
796         const char *volume_key,
797         size_t volume_key_size,
798         uint32_t flags);
799
800 /**
801  * Deactivate crypt device. This function tries to remove active device-mapper
802  * mapping from kernel. Also, sensitive data like the volume key are removed from
803  * memory
804  *
805  * @param cd crypt device handle, can be @e NULL
806  * @param name name of device to deactivate
807  *
808  * @return @e 0 on success or negative errno value otherwise.
809  *
810  */
811 int crypt_deactivate(struct crypt_device *cd, const char *name);
812
813 /**
814  * Get volume key from of crypt device
815  *
816  * @param cd crypt device handle
817  * @param keyslot use this keyslot or @e CRYPT_ANY_SLOT
818  * @param volume_key buffer for volume key
819  * @param volume_key_size on input, size of buffer @e volume_key,
820  *        on output size of @e volume_key
821  * @param passphrase passphrase used to unlock volume key
822  * @param passphrase_size size of @e passphrase
823  *
824  * @return unlocked key slot number or negative errno otherwise.
825  *
826  * @note For TCRYPT cipher chain is  the volume key concatenated
827  *       for all ciphers in chain.
828  */
829 int crypt_volume_key_get(struct crypt_device *cd,
830         int keyslot,
831         char *volume_key,
832         size_t *volume_key_size,
833         const char *passphrase,
834         size_t passphrase_size);
835
836 /**
837  * Verify that provided volume key is valid for crypt device
838  *
839  * @param cd crypt device handle
840  * @param volume_key provided volume key
841  * @param volume_key_size size of @e volume_key
842  *
843  * @return @e 0 on success or negative errno value otherwise.
844  */
845 int crypt_volume_key_verify(struct crypt_device *cd,
846         const char *volume_key,
847         size_t volume_key_size);
848
849 /**
850  * @defgroup devstat "Crypt and Verity device status"
851  * @addtogroup devstat
852  * @{
853  */
854
855 /**
856  * Device status
857  */
858 typedef enum {
859         CRYPT_INVALID, /**< device mapping is invalid in this context */
860         CRYPT_INACTIVE, /**< no such mapped device */
861         CRYPT_ACTIVE, /**< device is active */
862         CRYPT_BUSY /**< device is active and has open count > 0 */
863 } crypt_status_info;
864
865 /**
866  * Get status info about device name
867  *
868  * @param cd crypt device handle, can be @e NULL
869  * @param name crypt device name
870  *
871  * @return value defined by crypt_status_info.
872  *
873  */
874 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
875
876 /**
877  * Dump text-formatted information about crypt or verity device to log output
878  *
879  * @param cd crypt device handle
880  *
881  * @return @e 0 on success or negative errno value otherwise.
882  */
883 int crypt_dump(struct crypt_device *cd);
884
885 /**
886  * Informational benchmark for ciphers
887  *
888  * @param cd crypt device handle
889  * @param cipher (e.g. "aes")
890  * @param cipher_mode (e.g. "xts"), IV generator is ignored
891  * @param volume_key_size size of volume key in bytes
892  * @param iv_size size of IV in bytes
893  * @param buffer_size size of encryption buffer in bytes used in test
894  * @param encryption_mbs measured encryption speed in MiB/s
895  * @param decryption_mbs measured decryption speed in MiB/s
896  *
897  * @return @e 0 on success or negative errno value otherwise.
898  */
899 int crypt_benchmark(struct crypt_device *cd,
900         const char *cipher,
901         const char *cipher_mode,
902         size_t volume_key_size,
903         size_t iv_size,
904         size_t buffer_size,
905         double *encryption_mbs,
906         double *decryption_mbs);
907
908 int crypt_benchmark_kdf(struct crypt_device *cd,
909         const char *kdf,
910         const char *hash,
911         const char *password,
912         size_t password_size,
913         const char *salt,
914         size_t salt_size,
915         uint64_t *iterations_sec);
916
917 /**
918  * Get cipher used in device
919  *
920  * @param cd crypt device handle
921  *
922  * @return used cipher, e.g. "aes" or @e NULL otherwise
923  *
924  */
925 const char *crypt_get_cipher(struct crypt_device *cd);
926
927 /**
928  * Get cipher mode used in device
929  *
930  * @param cd crypt device handle
931  *
932  * @return used cipher mode e.g. "xts-plain" or @e otherwise
933  *
934  */
935 const char *crypt_get_cipher_mode(struct crypt_device *cd);
936
937 /**
938  * Get device UUID
939  *
940  * @param cd crypt device handle
941  *
942  * @return device UUID or @e NULL if not set
943  *
944  */
945 const char *crypt_get_uuid(struct crypt_device *cd);
946
947 /**
948  * Get path to underlaying device
949  *
950  * @param cd crypt device handle
951  *
952  * @return path to underlaying device name
953  *
954  */
955 const char *crypt_get_device_name(struct crypt_device *cd);
956
957 /**
958  * Get device offset in sectors where real data starts on underlying device)
959  *
960  * @param cd crypt device handle
961  *
962  * @return device offset in sectors
963  *
964  */
965 uint64_t crypt_get_data_offset(struct crypt_device *cd);
966
967 /**
968  * Get IV offset in sectors (skip)
969  *
970  * @param cd crypt device handle
971  *
972  * @return IV offset
973  *
974  */
975 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
976
977 /**
978  * Get size (in bytes) of volume key for crypt device
979  *
980  * @param cd crypt device handle
981  *
982  * @return volume key size
983  *
984  */
985 int crypt_get_volume_key_size(struct crypt_device *cd);
986
987 /**
988  * Get device parameters for VERITY device
989  *
990  * @param cd crypt device handle
991  * @param vp verity device info
992  *
993  * @e 0 on success or negative errno value otherwise.
994  *
995  */
996 int crypt_get_verity_info(struct crypt_device *cd,
997         struct crypt_params_verity *vp);
998 /** @} */
999
1000 /**
1001  * @addtogroup keyslot
1002  * @{
1003  *
1004  */
1005
1006 /**
1007  * Crypt keyslot info
1008  */
1009 typedef enum {
1010         CRYPT_SLOT_INVALID, /**< invalid keyslot */
1011         CRYPT_SLOT_INACTIVE, /**< keyslot is inactive (free) */
1012         CRYPT_SLOT_ACTIVE, /**< keyslot is active (used) */
1013         CRYPT_SLOT_ACTIVE_LAST /**< keylost is active (used)
1014                                 *   and last used at the same time */
1015 } crypt_keyslot_info;
1016
1017 /**
1018  * Get information about particular key slot
1019  *
1020  *
1021  * @param cd crypt device handle
1022  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
1023  *
1024  * @return value defined by crypt_keyslot_info
1025  *
1026  */
1027 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
1028 /** @} */
1029
1030 /**
1031  * Get number of keyslots supported for device type.
1032  *
1033  * @param type crypt device type
1034  *
1035  * @return slot count or negative errno otherwise if device
1036  * doesn't not support keyslots.
1037  */
1038 int crypt_keyslot_max(const char *type);
1039
1040 /**
1041  * Get keyslot area pointers (relative to metadata device)
1042  *
1043  * @param cd crypt device handle
1044  * @param keyslot keyslot number
1045  * @param offset offset on metadata device (in bytes)
1046  * @param length length of keyslot area (in bytes)
1047  *
1048  * @return @e 0 on success or negative errno value otherwise.
1049  *
1050  */
1051 int crypt_keyslot_area(struct crypt_device *cd,
1052         int keyslot,
1053         uint64_t *offset,
1054         uint64_t *length);
1055
1056 /**
1057  * Backup header and keyslots to file
1058  *
1059  * @param cd crypt device handle
1060  * @param requested_type @link crypt_type @endlink or @e NULL for all known
1061  * @param backup_file file to backup header to
1062  *
1063  * @return @e 0 on success or negative errno value otherwise.
1064  *
1065  */
1066 int crypt_header_backup(struct crypt_device *cd,
1067         const char *requested_type,
1068         const char *backup_file);
1069
1070 /**
1071  * Restore header and keyslots from backup file
1072  *
1073  *
1074  * @param cd crypt device handle
1075  * @param requested_type @link crypt_type @endlink or @e NULL for all known
1076  * @param backup_file file to restore header from
1077  *
1078  * @return @e 0 on success or negative errno value otherwise.
1079  *
1080  */
1081 int crypt_header_restore(struct crypt_device *cd,
1082         const char *requested_type,
1083         const char *backup_file);
1084
1085 /**
1086  * Receives last reported error
1087  *
1088  * @param cd crypt device handle
1089  * @param buf buffef for message
1090  * @param size size of buffer
1091  *
1092  * @note Note that this is old API function using global context.
1093  * All error messages are reported also through log callback.
1094  */
1095 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size);
1096
1097 /**
1098  * Receives last reported error, DEPRECATED
1099  *
1100  * @param buf buffef for message
1101  * @param size size of buffer
1102  *
1103  * @note Note that this is old API function using global context.
1104  * All error messages are reported also through log callback.
1105  */
1106 void crypt_get_error(char *buf, size_t size);
1107
1108 /**
1109  * Get directory where mapped crypt devices are created
1110  *
1111  * @return the directory path
1112  */
1113 const char *crypt_get_dir(void);
1114
1115 /**
1116  * @defgroup dbg "Library debug level"
1117  *
1118  * Set library debug level
1119  *
1120  * @addtogroup dbg
1121  * @{
1122  */
1123
1124 /** Debug all */
1125 #define CRYPT_DEBUG_ALL  -1
1126 /** Debug none */
1127 #define CRYPT_DEBUG_NONE  0
1128
1129 /**
1130  * Set the debug level for library
1131  *
1132  * @param level debug level
1133  *
1134  */
1135 void crypt_set_debug_level(int level);
1136
1137 /** @} */
1138
1139 #ifdef __cplusplus
1140 }
1141 #endif
1142 #endif /* _LIBCRYPTSETUP_H */