Set context for DM log for all DM backend entries.
[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
306 /**
307  * Get device type
308  *
309  * @param cd crypt device handle
310  * @return string according to device type or @e NULL if not known.
311  */
312 const char *crypt_get_type(struct crypt_device *cd);
313
314 /**
315  *
316  * Structure used as parameter for PLAIN device type
317  *
318  * @see crypt_format
319  */
320 struct crypt_params_plain {
321         const char *hash; /**< password hash function */
322         uint64_t offset; /**< offset in sectors */
323         uint64_t skip; /**< IV offset / initialization sector */
324         uint64_t size; /**< size of mapped device or @e 0 for autodetection */
325 };
326
327 /**
328  * Structure used as parameter for LUKS device type
329  *
330  * @see crypt_format, crypt_load
331  *
332  * @note during crypt_format @e data_device attribute determines
333  *       if the LUKS header is separated from encrypted payload device
334  *
335  */
336 struct crypt_params_luks1 {
337         const char *hash; /**< hash used in LUKS header */
338         size_t data_alignment; /**< data alignment in sectors, data offset is multiple of this */
339         const char *data_device; /**< detached encrypted data device or @e NULL */
340 };
341
342 /**
343  *
344  * Structure used as parameter for loop-AES device type
345  *
346  * @see crypt_format
347  *
348  */
349 struct crypt_params_loopaes {
350         const char *hash; /**< key hash function */
351         uint64_t offset;  /**< offset in sectors */
352         uint64_t skip;    /**< IV offset / initialization sector */
353 };
354
355 /**
356  *
357  * Structure used as parameter for dm-verity device type
358  *
359  * @see crypt_format, crypt_load
360  *
361  */
362 /** No on-disk header (only hashes) */
363 #define CRYPT_VERITY_NO_HEADER   (1 << 0)
364 /** Verity hash in userspace before activation */
365 #define CRYPT_VERITY_CHECK_HASH  (1 << 1)
366 /** Create hash - format hash device */
367 #define CRYPT_VERITY_CREATE_HASH (1 << 2)
368
369 struct crypt_params_verity {
370         const char *hash_name;     /**< hash function */
371         const char *data_device;   /**< data_device (CRYPT_VERITY_CREATE_HASH) */
372         const char *hash_device;   /**< hash_device (output only) */
373         const char *salt;          /**< salt */
374         uint32_t salt_size;        /**< salt size (in bytes) */
375         uint32_t hash_type;        /**< in-kernel hashing type */
376         uint32_t data_block_size;  /**< data block size (in bytes) */
377         uint32_t hash_block_size;  /**< hash block size (in bytes) */
378         uint64_t data_size;        /**< data area size (in data blocks) */
379         uint64_t hash_area_offset; /**< hash/header offset (in bytes) */
380         uint32_t flags;            /**< CRYPT_VERITY* flags */
381 };
382
383 /** @} */
384
385 /**
386  * Create (format) new crypt device (and possible header on-disk) but not activates it.
387  *
388  * @pre @e cd contains initialized and not formatted device context (device type must @b not be set)
389  *
390  * @param cd crypt device handle
391  * @param type type of device (optional params struct must be of this type)
392  * @param cipher (e.g. "aes")
393  * @param cipher_mode including IV specification (e.g. "xts-plain")
394  * @param uuid requested UUID or @e NULL if it should be generated
395  * @param volume_key pre-generated volume key or @e NULL if it should be generated (only for LUKS)
396  * @param volume_key_size size of volume key in bytes.
397  * @param params crypt type specific parameters (see @link crypt_type @endlink)
398  *
399  * @returns @e 0 on success or negative errno value otherwise.
400  *
401  * @note Note that crypt_format does not enable any keyslot (in case of work with LUKS device),
402  *      but it stores volume key internally and subsequent crypt_keyslot_add_* calls can be used.
403  * @note For VERITY @link crypt_type @endlink, only uuid parameter is used, others paramaters
404  *      are ignored and verity specific attributes are set through mandatory params option.
405  */
406 int crypt_format(struct crypt_device *cd,
407         const char *type,
408         const char *cipher,
409         const char *cipher_mode,
410         const char *uuid,
411         const char *volume_key,
412         size_t volume_key_size,
413         void *params);
414
415 /**
416  * Set new UUID for already existing device
417  *
418  * @param cd crypt device handle
419  * @param uuid requested UUID or @e NULL if it should be generated
420  *
421  * @returns 0 on success or negative errno value otherwise.
422  *
423  * @note Currently, only LUKS device type are supported
424  */
425 int crypt_set_uuid(struct crypt_device *cd,
426                    const char *uuid);
427
428 /**
429  * Load crypt device parameters from on-disk header
430  *
431  * @param cd crypt device handle
432  * @param requested_type @link crypt_type @endlink or @e NULL for all known
433  * @param params crypt type specific parameters (see @link crypt_type @endlink)
434  *
435  * @returns 0 on success or negative errno value otherwise.
436  *
437  * @post In case LUKS header is read successfully but payload device is too small
438  * error is returned and device type in context is set to @e NULL
439  *
440  * @note Note that in current version load works only for LUKS and VERITY device type.
441  *
442  */
443 int crypt_load(struct crypt_device *cd,
444                const char *requested_type,
445                void *params);
446
447 /**
448  * Try to repair crypt device on-disk header if invalid
449  *
450  * @param cd crypt device handle
451  * @param requested_type @link crypt_type @endlink or @e NULL for all known
452  * @param params crypt type specific parameters (see @link crypt_type @endlink)
453  *
454  * @returns 0 on success or negative errno value otherwise.
455  *
456  */
457 int crypt_repair(struct crypt_device *cd,
458                  const char *requested_type,
459                  void *params);
460
461 /**
462  * Resize crypt device
463  *
464  * @param cd - crypt device handle
465  * @param name - name of device to resize
466  * @param new_size - new device size in sectors or @e 0 to use all of the underlying device size
467  *
468  * @return @e 0 on success or negative errno value otherwise.
469  */
470 int crypt_resize(struct crypt_device *cd,
471                  const char *name,
472                  uint64_t new_size);
473
474 /**
475  * Suspends crypt device.
476  *
477  * @param cd crypt device handle, can be @e NULL
478  * @param name name of device to suspend
479  *
480  * @return 0 on success or negative errno value otherwise.
481  *
482  * @note Only LUKS device type is supported
483  *
484  */
485 int crypt_suspend(struct crypt_device *cd,
486                   const char *name);
487
488 /**
489  * Resumes crypt device using passphrase.
490  *
491  *
492  * @param cd crypt device handle
493  * @param name name of device to resume
494  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
495  * @param passphrase passphrase used to unlock volume key, @e NULL for query
496  * @param passphrase_size size of @e passphrase (binary data)
497  *
498  * @return unlocked key slot number or negative errno otherwise.
499  *
500  * @note Only LUKS device type is supported
501  */
502 int crypt_resume_by_passphrase(struct crypt_device *cd,
503         const char *name,
504         int keyslot,
505         const char *passphrase,
506         size_t passphrase_size);
507
508 /**
509  * Resumes crypt device using key file.
510  *
511  * @param cd crypt device handle
512  * @param name name of device to resume
513  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
514  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
515  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
516  * @param keyfile_offset number of bytes to skip at start of keyfile
517  *
518  * @return unlocked key slot number or negative errno otherwise.
519  */
520 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
521         const char *name,
522         int keyslot,
523         const char *keyfile,
524         size_t keyfile_size,
525         size_t keyfile_offset);
526 /**
527  * Backward compatible crypt_resume_by_keyfile_offset() (without offset).
528  */
529 int crypt_resume_by_keyfile(struct crypt_device *cd,
530         const char *name,
531         int keyslot,
532         const char *keyfile,
533         size_t keyfile_size);
534
535 /**
536  * Releases crypt device context and used memory.
537  *
538  * @param cd crypt device handle
539  */
540 void crypt_free(struct crypt_device *cd);
541
542 /**
543  * @defgroup keyslot "Cryptsetup LUKS keyslots"
544  * @addtogroup keyslot
545  * @{
546  *
547  */
548
549 /** iterate through all keyslots and find first one that fits */
550 #define CRYPT_ANY_SLOT -1
551
552 /**
553  * Add key slot using provided passphrase
554  *
555  * @pre @e cd contains initialized and formatted LUKS device context
556  *
557  * @param cd crypt device handle
558  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
559  * @param passphrase passphrase used to unlock volume key, @e NULL for query
560  * @param passphrase_size size of passphrase (binary data)
561  * @param new_passphrase passphrase for new keyslot, @e NULL for query
562  * @param new_passphrase_size size of @e new_passphrase (binary data)
563  *
564  * @return allocated key slot number or negative errno otherwise.
565  */
566 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
567         int keyslot,
568         const char *passphrase,
569         size_t passphrase_size,
570         const char *new_passphrase,
571         size_t new_passphrase_size);
572
573 /**
574  * Get number of keyslots supported for device type.
575  *
576  * @param type crypt device type
577  *
578  * @return slot count or negative errno otherwise if device
579  * doesn't not support keyslots.
580  */
581 int crypt_keyslot_max(const char *type);
582
583 /**
584 * Add key slot using provided key file path
585  *
586  * @pre @e cd contains initialized and formatted LUKS device context
587  *
588  * @param cd crypt device handle
589  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
590  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
591  * @param keyfile_size number of bytes to read from keyfile, @e 0 is unlimited
592  * @param keyfile_offset number of bytes to skip at start of keyfile
593  * @param new_keyfile keyfile for new keyslot, @e NULL for passphrase query
594  * @param new_keyfile_size number of bytes to read from @e new_keyfile, @e 0 is unlimited
595  * @param new_keyfile_offset number of bytes to skip at start of new_keyfile
596  *
597  * @return allocated key slot number or negative errno otherwise.
598  *
599  * @note Note that @e keyfile can be "-" for STDIN
600  *
601  */
602 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
603         int keyslot,
604         const char *keyfile,
605         size_t keyfile_size,
606         size_t keyfile_offset,
607         const char *new_keyfile,
608         size_t new_keyfile_size,
609         size_t new_keyfile_offset);
610 /**
611  * Backward compatible crypt_keyslot_add_by_keyfile_offset() (without offset).
612  */
613 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
614         int keyslot,
615         const char *keyfile,
616         size_t keyfile_size,
617         const char *new_keyfile,
618         size_t new_keyfile_size);
619
620 /**
621  * Add key slot using provided volume key
622  *
623  * @pre @e cd contains initialized and formatted LUKS device context
624  *
625  * @param cd crypt device handle
626  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
627  * @param volume_key provided volume key or @e NULL if used after crypt_format
628  * @param volume_key_size size of volume_key
629  * @param passphrase passphrase for new keyslot, @e NULL for query
630  * @param passphrase_size size of passphrase
631  *
632  * @return allocated key slot number or negative errno otherwise.
633  *
634  */
635 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
636         int keyslot,
637         const char *volume_key,
638         size_t volume_key_size,
639         const char *passphrase,
640         size_t passphrase_size);
641
642 /**
643  * Destroy (and disable) key slot
644  *
645  * @pre @e cd contains initialized and formatted LUKS device context
646  *
647  * @param cd crypt device handle
648  * @param keyslot requested key slot to destroy
649  *
650  * @return @e 0 on success or negative errno value otherwise.
651  *
652  * @note Note that there is no passphrase verification used.
653  */
654 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
655
656 /** @} */
657
658 /**
659  * @defgroup aflags "Device runtime attributes"
660  *
661  * Activation flags
662  *
663  * @addtogroup aflags
664  * @{
665  *
666  */
667 /** device is read only */
668 #define CRYPT_ACTIVATE_READONLY (1 << 0)
669 /** only reported for device without uuid */
670 #define CRYPT_ACTIVATE_NO_UUID  (1 << 1)
671 /** activate even if cannot grant exclusive access (DANGEROUS) */
672 #define CRYPT_ACTIVATE_SHARED   (1 << 2)
673 /** enable discards aka TRIM */
674 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3)
675 /** skip global udev rules in activation ("private device"), input only */
676 #define CRYPT_ACTIVATE_PRIVATE (1 << 4)
677 /** corruption detected (verity), output only */
678 #define CRYPT_ACTIVATE_CORRUPTED (1 << 5)
679
680 /**
681  * Active device runtime attributes
682  */
683 struct crypt_active_device {
684         uint64_t offset; /**< offset in sectors */
685         uint64_t iv_offset; /**< IV initialization sector */
686         uint64_t size; /**< active device size */
687         uint32_t flags; /**< activation flags */
688 };
689
690 /**
691  * Receives runtime attributes of active crypt device
692  *
693  * @param cd crypt device handle (can be @e NULL)
694  * @param name name of active device
695  * @param cad preallocated active device attributes to fill
696  *
697  * @return @e 0 on success or negative errno value otherwise
698  *
699  */
700 int crypt_get_active_device(struct crypt_device *cd,
701                             const char *name,
702                             struct crypt_active_device *cad);
703
704 /** @} */
705
706 /**
707  * Activate device or check passphrase
708  *
709  * @param cd crypt device handle
710  * @param name name of device to create, if @e NULL only check passphrase
711  * @param keyslot requested keyslot to check or @e CRYPT_ANY_SLOT
712  * @param passphrase passphrase used to unlock volume key, @e NULL for query
713  * @param passphrase_size size of @e passphrase
714  * @param flags activation flags
715  *
716  * @return unlocked key slot number or negative errno otherwise.
717  */
718 int crypt_activate_by_passphrase(struct crypt_device *cd,
719         const char *name,
720         int keyslot,
721         const char *passphrase,
722         size_t passphrase_size,
723         uint32_t flags);
724
725 /**
726  * Activate device or check using key file
727  *
728  * @param cd crypt device handle
729  * @param name name of device to create, if @e NULL only check keyfile
730  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
731  * @param keyfile key file used to unlock volume key
732  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
733  * @param keyfile_offset number of bytes to skip at start of keyfile
734  * @param flags activation flags
735  *
736  * @return unlocked key slot number or negative errno otherwise.
737  */
738 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
739         const char *name,
740         int keyslot,
741         const char *keyfile,
742         size_t keyfile_size,
743         size_t keyfile_offset,
744         uint32_t flags);
745 /**
746  * Backward compatible crypt_activate_by_keyfile_offset() (without offset).
747  */
748 int crypt_activate_by_keyfile(struct crypt_device *cd,
749         const char *name,
750         int keyslot,
751         const char *keyfile,
752         size_t keyfile_size,
753         uint32_t flags);
754
755 /**
756  * Activate device using provided volume key
757  *
758  *
759  * @param cd crypt device handle
760  * @param name name of device to create, if @e NULL only check volume key
761  * @param volume_key provided volume key (or @e NULL to use internal)
762  * @param volume_key_size size of volume_key
763  * @param flags activation flags
764  *
765  * @return @e 0 on success or negative errno value otherwise.
766  *
767  * @note If @e NULL is used for volume_key, device has to be initialized
768  *       by previous operation (like @ref crypt_format
769  *       or @ref crypt_init_by_name)
770  * @note For VERITY the volume key means root hash required for activation.
771  *       Because kernel dm-verity is always read only, you have to provide
772  *       CRYPT_ACTIVATE_READONLY flag always.
773  */
774 int crypt_activate_by_volume_key(struct crypt_device *cd,
775         const char *name,
776         const char *volume_key,
777         size_t volume_key_size,
778         uint32_t flags);
779
780 /**
781  * Deactivate crypt device. This function tries to remove active device-mapper
782  * mapping from kernel. Also, sensitive data like the volume key are removed from
783  * memory
784  *
785  * @param cd crypt device handle, can be @e NULL
786  * @param name name of device to deactivate
787  *
788  * @return @e 0 on success or negative errno value otherwise.
789  *
790  */
791 int crypt_deactivate(struct crypt_device *cd, const char *name);
792
793 /**
794  * Get volume key from of crypt device
795  *
796  * @param cd crypt device handle
797  * @param keyslot use this keyslot or @e CRYPT_ANY_SLOT
798  * @param volume_key buffer for volume key
799  * @param volume_key_size on input, size of buffer @e volume_key,
800  *        on output size of @e volume_key
801  * @param passphrase passphrase used to unlock volume key
802  * @param passphrase_size size of @e passphrase
803  *
804  * @return unlocked key slot number or negative errno otherwise.
805  */
806 int crypt_volume_key_get(struct crypt_device *cd,
807         int keyslot,
808         char *volume_key,
809         size_t *volume_key_size,
810         const char *passphrase,
811         size_t passphrase_size);
812
813 /**
814  * Verify that provided volume key is valid for crypt device
815  *
816  * @param cd crypt device handle
817  * @param volume_key provided volume key
818  * @param volume_key_size size of @e volume_key
819  *
820  * @return @e 0 on success or negative errno value otherwise.
821  */
822 int crypt_volume_key_verify(struct crypt_device *cd,
823         const char *volume_key,
824         size_t volume_key_size);
825
826 /**
827  * @defgroup devstat "Crypt and Verity device status"
828  * @addtogroup devstat
829  * @{
830  */
831
832 /**
833  * Device status
834  */
835 typedef enum {
836         CRYPT_INVALID, /**< device mapping is invalid in this context */
837         CRYPT_INACTIVE, /**< no such mapped device */
838         CRYPT_ACTIVE, /**< device is active */
839         CRYPT_BUSY /**< device is active and has open count > 0 */
840 } crypt_status_info;
841
842 /**
843  * Get status info about device name
844  *
845  * @param cd crypt device handle, can be @e NULL
846  * @param name crypt device name
847  *
848  * @return value defined by crypt_status_info.
849  *
850  */
851 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
852
853 /**
854  * Dump text-formatted information about crypt or verity device to log output
855  *
856  * @param cd crypt device handle
857  *
858  * @return @e 0 on success or negative errno value otherwise.
859  */
860 int crypt_dump(struct crypt_device *cd);
861
862 /**
863  * Get cipher used in device
864  *
865  * @param cd crypt device handle
866  *
867  * @return used cipher, e.g. "aes" or @e NULL otherwise
868  *
869  */
870 const char *crypt_get_cipher(struct crypt_device *cd);
871
872 /**
873  * Get cipher mode used in device
874  *
875  * @param cd crypt device handle
876  *
877  * @return used cipher mode e.g. "xts-plain" or @e otherwise
878  *
879  */
880 const char *crypt_get_cipher_mode(struct crypt_device *cd);
881
882 /**
883  * Get device UUID
884  *
885  * @param cd crypt device handle
886  *
887  * @return device UUID or @e NULL if not set
888  *
889  */
890 const char *crypt_get_uuid(struct crypt_device *cd);
891
892 /**
893  * Get path to underlaying device
894  *
895  * @param cd crypt device handle
896  *
897  * @return path to underlaying device name
898  *
899  */
900 const char *crypt_get_device_name(struct crypt_device *cd);
901
902 /**
903  * Get device offset in sectors where real data starts on underlying device)
904  *
905  * @param cd crypt device handle
906  *
907  * @return device offset in sectors
908  *
909  */
910 uint64_t crypt_get_data_offset(struct crypt_device *cd);
911
912 /**
913  * Get IV offset in sectors (skip)
914  *
915  * @param cd crypt device handle
916  *
917  * @return IV offset
918  *
919  */
920 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
921
922 /**
923  * Get size (in bytes) of volume key for crypt device
924  *
925  * @param cd crypt device handle
926  *
927  * @return volume key size
928  *
929  */
930 int crypt_get_volume_key_size(struct crypt_device *cd);
931
932 /**
933  * Get device parameters for VERITY device
934  *
935  * @param cd crypt device handle
936  * @param vp verity device info
937  *
938  * @e 0 on success or negative errno value otherwise.
939  *
940  */
941 int crypt_get_verity_info(struct crypt_device *cd,
942         struct crypt_params_verity *vp);
943 /** @} */
944
945 /**
946  * @addtogroup keyslot
947  * @{
948  *
949  */
950
951 /**
952  * Crypt keyslot info
953  */
954 typedef enum {
955         CRYPT_SLOT_INVALID, /**< invalid keyslot */
956         CRYPT_SLOT_INACTIVE, /**< keyslot is inactive (free) */
957         CRYPT_SLOT_ACTIVE, /**< keyslot is active (used) */
958         CRYPT_SLOT_ACTIVE_LAST /**< keylost is active (used)
959                                 *   and last used at the same time */
960 } crypt_keyslot_info;
961
962 /**
963  * Get information about particular key slot
964  *
965  *
966  * @param cd crypt device handle
967  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
968  *
969  * @return value defined by crypt_keyslot_info
970  *
971  */
972 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
973 /** @} */
974
975 /**
976  * Backup header and keyslots to file
977  *
978  * @param cd crypt device handle
979  * @param requested_type @link crypt_type @endlink or @e NULL for all known
980  * @param backup_file file to backup header to
981  *
982  * @return @e 0 on success or negative errno value otherwise.
983  *
984  */
985 int crypt_header_backup(struct crypt_device *cd,
986         const char *requested_type,
987         const char *backup_file);
988
989 /**
990  * Restore header and keyslots from backup file
991  *
992  *
993  * @param cd crypt device handle
994  * @param requested_type @link crypt_type @endlink or @e NULL for all known
995  * @param backup_file file to restore header from
996  *
997  * @return @e 0 on success or negative errno value otherwise.
998  *
999  */
1000 int crypt_header_restore(struct crypt_device *cd,
1001         const char *requested_type,
1002         const char *backup_file);
1003
1004 /**
1005  * Receives last reported error
1006  *
1007  * @param cd crypt device handle
1008  * @param buf buffef for message
1009  * @param size size of buffer
1010  *
1011  * @note Note that this is old API function using global context.
1012  * All error messages are reported also through log callback.
1013  */
1014 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size);
1015
1016 /**
1017  * Receives last reported error, DEPRECATED
1018  *
1019  * @param buf buffef for message
1020  * @param size size of buffer
1021  *
1022  * @note Note that this is old API function using global context.
1023  * All error messages are reported also through log callback.
1024  */
1025 void crypt_get_error(char *buf, size_t size);
1026
1027 /**
1028  * Get directory where mapped crypt devices are created
1029  *
1030  * @return the directory path
1031  */
1032 const char *crypt_get_dir(void);
1033
1034 /**
1035  * @defgroup dbg "Library debug level"
1036  *
1037  * Set library debug level
1038  *
1039  * @addtogroup dbg
1040  * @{
1041  */
1042
1043 /** Debug all */
1044 #define CRYPT_DEBUG_ALL  -1
1045 /** Debug none */
1046 #define CRYPT_DEBUG_NONE  0
1047
1048 /**
1049  * Set the debug level for library
1050  *
1051  * @param level debug level
1052  *
1053  */
1054 void crypt_set_debug_level(int level);
1055
1056 /** @} */
1057
1058 #ifdef __cplusplus
1059 }
1060 #endif
1061 #endif /* _LIBCRYPTSETUP_H */