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