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