051875c863a9cb106f770765f2eeb4381a007440
[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
400 struct crypt_params_tcrypt {
401         const char *passphrase;    /**< passphrase to unlock header (input only) */
402         size_t passphrase_size;    /**< passphrase size (input only) */
403         const char **keyfiles;     /**< keyfile paths to unlock header (input only) */
404         unsigned int keyfiles_count;/**< keyfiles count (input only) */
405         const char *hash_name;     /**< hash function for PBKDF */
406         const char *cipher;        /**< cipher chain c1[-c2[-c3]] */
407         const char *mode;          /**< cipher block mode */
408         size_t key_size;           /**< key size in bytes (the whole chain) */
409         uint32_t flags;            /**< CRYPT_TCRYPT* flags */
410 };
411
412 /** @} */
413
414 /**
415  * Create (format) new crypt device (and possible header on-disk) but not activates it.
416  *
417  * @pre @e cd contains initialized and not formatted device context (device type must @b not be set)
418  *
419  * @param cd crypt device handle
420  * @param type type of device (optional params struct must be of this type)
421  * @param cipher (e.g. "aes")
422  * @param cipher_mode including IV specification (e.g. "xts-plain")
423  * @param uuid requested UUID or @e NULL if it should be generated
424  * @param volume_key pre-generated volume key or @e NULL if it should be generated (only for LUKS)
425  * @param volume_key_size size of volume key in bytes.
426  * @param params crypt type specific parameters (see @link crypt_type @endlink)
427  *
428  * @returns @e 0 on success or negative errno value otherwise.
429  *
430  * @note Note that crypt_format does not enable any keyslot (in case of work with LUKS device),
431  *      but it stores volume key internally and subsequent crypt_keyslot_add_* calls can be used.
432  * @note For VERITY @link crypt_type @endlink, only uuid parameter is used, others paramaters
433  *      are ignored and verity specific attributes are set through mandatory params option.
434  */
435 int crypt_format(struct crypt_device *cd,
436         const char *type,
437         const char *cipher,
438         const char *cipher_mode,
439         const char *uuid,
440         const char *volume_key,
441         size_t volume_key_size,
442         void *params);
443
444 /**
445  * Set new UUID for already existing device
446  *
447  * @param cd crypt device handle
448  * @param uuid requested UUID or @e NULL if it should be generated
449  *
450  * @returns 0 on success or negative errno value otherwise.
451  *
452  * @note Currently, only LUKS device type are supported
453  */
454 int crypt_set_uuid(struct crypt_device *cd,
455                    const char *uuid);
456
457 /**
458  * Load crypt device parameters from on-disk header
459  *
460  * @param cd crypt device handle
461  * @param requested_type @link crypt_type @endlink or @e NULL for all known
462  * @param params crypt type specific parameters (see @link crypt_type @endlink)
463  *
464  * @returns 0 on success or negative errno value otherwise.
465  *
466  * @post In case LUKS header is read successfully but payload device is too small
467  * error is returned and device type in context is set to @e NULL
468  *
469  * @note Note that in current version load works only for LUKS and VERITY device type.
470  *
471  */
472 int crypt_load(struct crypt_device *cd,
473                const char *requested_type,
474                void *params);
475
476 /**
477  * Try to repair crypt device on-disk header if invalid
478  *
479  * @param cd crypt device handle
480  * @param requested_type @link crypt_type @endlink or @e NULL for all known
481  * @param params crypt type specific parameters (see @link crypt_type @endlink)
482  *
483  * @returns 0 on success or negative errno value otherwise.
484  *
485  */
486 int crypt_repair(struct crypt_device *cd,
487                  const char *requested_type,
488                  void *params);
489
490 /**
491  * Resize crypt device
492  *
493  * @param cd - crypt device handle
494  * @param name - name of device to resize
495  * @param new_size - new device size in sectors or @e 0 to use all of the underlying device size
496  *
497  * @return @e 0 on success or negative errno value otherwise.
498  */
499 int crypt_resize(struct crypt_device *cd,
500                  const char *name,
501                  uint64_t new_size);
502
503 /**
504  * Suspends crypt device.
505  *
506  * @param cd crypt device handle, can be @e NULL
507  * @param name name of device to suspend
508  *
509  * @return 0 on success or negative errno value otherwise.
510  *
511  * @note Only LUKS device type is supported
512  *
513  */
514 int crypt_suspend(struct crypt_device *cd,
515                   const char *name);
516
517 /**
518  * Resumes crypt device using passphrase.
519  *
520  *
521  * @param cd crypt device handle
522  * @param name name of device to resume
523  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
524  * @param passphrase passphrase used to unlock volume key, @e NULL for query
525  * @param passphrase_size size of @e passphrase (binary data)
526  *
527  * @return unlocked key slot number or negative errno otherwise.
528  *
529  * @note Only LUKS device type is supported
530  */
531 int crypt_resume_by_passphrase(struct crypt_device *cd,
532         const char *name,
533         int keyslot,
534         const char *passphrase,
535         size_t passphrase_size);
536
537 /**
538  * Resumes crypt device using key file.
539  *
540  * @param cd crypt device handle
541  * @param name name of device to resume
542  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
543  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
544  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
545  * @param keyfile_offset number of bytes to skip at start of keyfile
546  *
547  * @return unlocked key slot number or negative errno otherwise.
548  */
549 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
550         const char *name,
551         int keyslot,
552         const char *keyfile,
553         size_t keyfile_size,
554         size_t keyfile_offset);
555 /**
556  * Backward compatible crypt_resume_by_keyfile_offset() (without offset).
557  */
558 int crypt_resume_by_keyfile(struct crypt_device *cd,
559         const char *name,
560         int keyslot,
561         const char *keyfile,
562         size_t keyfile_size);
563
564 /**
565  * Releases crypt device context and used memory.
566  *
567  * @param cd crypt device handle
568  */
569 void crypt_free(struct crypt_device *cd);
570
571 /**
572  * @defgroup keyslot "Cryptsetup LUKS keyslots"
573  * @addtogroup keyslot
574  * @{
575  *
576  */
577
578 /** iterate through all keyslots and find first one that fits */
579 #define CRYPT_ANY_SLOT -1
580
581 /**
582  * Add key slot using provided passphrase
583  *
584  * @pre @e cd contains initialized and formatted LUKS device context
585  *
586  * @param cd crypt device handle
587  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
588  * @param passphrase passphrase used to unlock volume key, @e NULL for query
589  * @param passphrase_size size of passphrase (binary data)
590  * @param new_passphrase passphrase for new keyslot, @e NULL for query
591  * @param new_passphrase_size size of @e new_passphrase (binary data)
592  *
593  * @return allocated key slot number or negative errno otherwise.
594  */
595 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
596         int keyslot,
597         const char *passphrase,
598         size_t passphrase_size,
599         const char *new_passphrase,
600         size_t new_passphrase_size);
601
602 /**
603  * Change defined key slot using provided passphrase
604  *
605  * @pre @e cd contains initialized and formatted LUKS device context
606  *
607  * @param cd crypt device handle
608  * @param keyslot_old old keyslot or @e CRYPT_ANY_SLOT
609  * @param keyslot_new new keyslot (can be the same as old)
610  * @param passphrase passphrase used to unlock volume key, @e NULL for query
611  * @param passphrase_size size of passphrase (binary data)
612  * @param new_passphrase passphrase for new keyslot, @e NULL for query
613  * @param new_passphrase_size size of @e new_passphrase (binary data)
614  *
615  * @return allocated key slot number or negative errno otherwise.
616  *
617  * @note This function is just internal implementation of luksChange
618  * command to avoid reading of volume key outside libcryptsetup boundary
619  * in FIPS mode.
620  */
621 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
622         int keyslot_old,
623         int keyslot_new,
624         const char *passphrase,
625         size_t passphrase_size,
626         const char *new_passphrase,
627         size_t new_passphrase_size);
628
629 /**
630 * Add key slot using provided key file path
631  *
632  * @pre @e cd contains initialized and formatted LUKS device context
633  *
634  * @param cd crypt device handle
635  * @param keyslot requested keyslot or @e CRYPT_ANY_SLOT
636  * @param keyfile key file used to unlock volume key, @e NULL for passphrase query
637  * @param keyfile_size number of bytes to read from keyfile, @e 0 is unlimited
638  * @param keyfile_offset number of bytes to skip at start of keyfile
639  * @param new_keyfile keyfile for new keyslot, @e NULL for passphrase query
640  * @param new_keyfile_size number of bytes to read from @e new_keyfile, @e 0 is unlimited
641  * @param new_keyfile_offset number of bytes to skip at start of new_keyfile
642  *
643  * @return allocated key slot number or negative errno otherwise.
644  *
645  * @note Note that @e keyfile can be "-" for STDIN
646  *
647  */
648 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
649         int keyslot,
650         const char *keyfile,
651         size_t keyfile_size,
652         size_t keyfile_offset,
653         const char *new_keyfile,
654         size_t new_keyfile_size,
655         size_t new_keyfile_offset);
656 /**
657  * Backward compatible crypt_keyslot_add_by_keyfile_offset() (without offset).
658  */
659 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
660         int keyslot,
661         const char *keyfile,
662         size_t keyfile_size,
663         const char *new_keyfile,
664         size_t new_keyfile_size);
665
666 /**
667  * Add key slot using provided volume key
668  *
669  * @pre @e cd contains initialized and formatted LUKS device context
670  *
671  * @param cd crypt device handle
672  * @param keyslot requested keyslot or CRYPT_ANY_SLOT
673  * @param volume_key provided volume key or @e NULL if used after crypt_format
674  * @param volume_key_size size of volume_key
675  * @param passphrase passphrase for new keyslot, @e NULL for query
676  * @param passphrase_size size of passphrase
677  *
678  * @return allocated key slot number or negative errno otherwise.
679  *
680  */
681 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
682         int keyslot,
683         const char *volume_key,
684         size_t volume_key_size,
685         const char *passphrase,
686         size_t passphrase_size);
687
688 /**
689  * Destroy (and disable) key slot
690  *
691  * @pre @e cd contains initialized and formatted LUKS device context
692  *
693  * @param cd crypt device handle
694  * @param keyslot requested key slot to destroy
695  *
696  * @return @e 0 on success or negative errno value otherwise.
697  *
698  * @note Note that there is no passphrase verification used.
699  */
700 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
701
702 /** @} */
703
704 /**
705  * @defgroup aflags "Device runtime attributes"
706  *
707  * Activation flags
708  *
709  * @addtogroup aflags
710  * @{
711  *
712  */
713 /** device is read only */
714 #define CRYPT_ACTIVATE_READONLY (1 << 0)
715 /** only reported for device without uuid */
716 #define CRYPT_ACTIVATE_NO_UUID  (1 << 1)
717 /** activate even if cannot grant exclusive access (DANGEROUS) */
718 #define CRYPT_ACTIVATE_SHARED   (1 << 2)
719 /** enable discards aka TRIM */
720 #define CRYPT_ACTIVATE_ALLOW_DISCARDS (1 << 3)
721 /** skip global udev rules in activation ("private device"), input only */
722 #define CRYPT_ACTIVATE_PRIVATE (1 << 4)
723 /** corruption detected (verity), output only */
724 #define CRYPT_ACTIVATE_CORRUPTED (1 << 5)
725
726 /**
727  * Active device runtime attributes
728  */
729 struct crypt_active_device {
730         uint64_t offset; /**< offset in sectors */
731         uint64_t iv_offset; /**< IV initialization sector */
732         uint64_t size; /**< active device size */
733         uint32_t flags; /**< activation flags */
734 };
735
736 /**
737  * Receives runtime attributes of active crypt device
738  *
739  * @param cd crypt device handle (can be @e NULL)
740  * @param name name of active device
741  * @param cad preallocated active device attributes to fill
742  *
743  * @return @e 0 on success or negative errno value otherwise
744  *
745  */
746 int crypt_get_active_device(struct crypt_device *cd,
747                             const char *name,
748                             struct crypt_active_device *cad);
749
750 /** @} */
751
752 /**
753  * Activate device or check passphrase
754  *
755  * @param cd crypt device handle
756  * @param name name of device to create, if @e NULL only check passphrase
757  * @param keyslot requested keyslot to check or @e CRYPT_ANY_SLOT
758  * @param passphrase passphrase used to unlock volume key, @e NULL for query
759  * @param passphrase_size size of @e passphrase
760  * @param flags activation flags
761  *
762  * @return unlocked key slot number or negative errno otherwise.
763  */
764 int crypt_activate_by_passphrase(struct crypt_device *cd,
765         const char *name,
766         int keyslot,
767         const char *passphrase,
768         size_t passphrase_size,
769         uint32_t flags);
770
771 /**
772  * Activate device or check using key file
773  *
774  * @param cd crypt device handle
775  * @param name name of device to create, if @e NULL only check keyfile
776  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
777  * @param keyfile key file used to unlock volume key
778  * @param keyfile_size number of bytes to read from keyfile, 0 is unlimited
779  * @param keyfile_offset number of bytes to skip at start of keyfile
780  * @param flags activation flags
781  *
782  * @return unlocked key slot number or negative errno otherwise.
783  */
784 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
785         const char *name,
786         int keyslot,
787         const char *keyfile,
788         size_t keyfile_size,
789         size_t keyfile_offset,
790         uint32_t flags);
791 /**
792  * Backward compatible crypt_activate_by_keyfile_offset() (without offset).
793  */
794 int crypt_activate_by_keyfile(struct crypt_device *cd,
795         const char *name,
796         int keyslot,
797         const char *keyfile,
798         size_t keyfile_size,
799         uint32_t flags);
800
801 /**
802  * Activate device using provided volume key
803  *
804  *
805  * @param cd crypt device handle
806  * @param name name of device to create, if @e NULL only check volume key
807  * @param volume_key provided volume key (or @e NULL to use internal)
808  * @param volume_key_size size of volume_key
809  * @param flags activation flags
810  *
811  * @return @e 0 on success or negative errno value otherwise.
812  *
813  * @note If @e NULL is used for volume_key, device has to be initialized
814  *       by previous operation (like @ref crypt_format
815  *       or @ref crypt_init_by_name)
816  * @note For VERITY the volume key means root hash required for activation.
817  *       Because kernel dm-verity is always read only, you have to provide
818  *       CRYPT_ACTIVATE_READONLY flag always.
819  * @note For TCRYPT the volume key should be always NULL and because master
820  *       key from decrypted header is used instead.
821  */
822 int crypt_activate_by_volume_key(struct crypt_device *cd,
823         const char *name,
824         const char *volume_key,
825         size_t volume_key_size,
826         uint32_t flags);
827
828 /**
829  * Deactivate crypt device. This function tries to remove active device-mapper
830  * mapping from kernel. Also, sensitive data like the volume key are removed from
831  * memory
832  *
833  * @param cd crypt device handle, can be @e NULL
834  * @param name name of device to deactivate
835  *
836  * @return @e 0 on success or negative errno value otherwise.
837  *
838  */
839 int crypt_deactivate(struct crypt_device *cd, const char *name);
840
841 /**
842  * Get volume key from of crypt device
843  *
844  * @param cd crypt device handle
845  * @param keyslot use this keyslot or @e CRYPT_ANY_SLOT
846  * @param volume_key buffer for volume key
847  * @param volume_key_size on input, size of buffer @e volume_key,
848  *        on output size of @e volume_key
849  * @param passphrase passphrase used to unlock volume key
850  * @param passphrase_size size of @e passphrase
851  *
852  * @return unlocked key slot number or negative errno otherwise.
853  *
854  * @note For TCRYPT cipher chain is  the volume key concatenated
855  *       for all ciphers in chain.
856  */
857 int crypt_volume_key_get(struct crypt_device *cd,
858         int keyslot,
859         char *volume_key,
860         size_t *volume_key_size,
861         const char *passphrase,
862         size_t passphrase_size);
863
864 /**
865  * Verify that provided volume key is valid for crypt device
866  *
867  * @param cd crypt device handle
868  * @param volume_key provided volume key
869  * @param volume_key_size size of @e volume_key
870  *
871  * @return @e 0 on success or negative errno value otherwise.
872  */
873 int crypt_volume_key_verify(struct crypt_device *cd,
874         const char *volume_key,
875         size_t volume_key_size);
876
877 /**
878  * @defgroup devstat "Crypt and Verity device status"
879  * @addtogroup devstat
880  * @{
881  */
882
883 /**
884  * Device status
885  */
886 typedef enum {
887         CRYPT_INVALID, /**< device mapping is invalid in this context */
888         CRYPT_INACTIVE, /**< no such mapped device */
889         CRYPT_ACTIVE, /**< device is active */
890         CRYPT_BUSY /**< device is active and has open count > 0 */
891 } crypt_status_info;
892
893 /**
894  * Get status info about device name
895  *
896  * @param cd crypt device handle, can be @e NULL
897  * @param name crypt device name
898  *
899  * @return value defined by crypt_status_info.
900  *
901  */
902 crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
903
904 /**
905  * Dump text-formatted information about crypt or verity device to log output
906  *
907  * @param cd crypt device handle
908  *
909  * @return @e 0 on success or negative errno value otherwise.
910  */
911 int crypt_dump(struct crypt_device *cd);
912
913 /**
914  * Informational benchmark for ciphers
915  *
916  * @param cd crypt device handle
917  * @param cipher (e.g. "aes")
918  * @param cipher_mode (e.g. "xts"), IV generator is ignored
919  * @param volume_key_size size of volume key in bytes
920  * @param iv_size size of IV in bytes
921  * @param buffer_size size of encryption buffer in bytes used in test
922  * @param encryption_mbs measured encryption speed in MiB/s
923  * @param decryption_mbs measured decryption speed in MiB/s
924  *
925  * @return @e 0 on success or negative errno value otherwise.
926  */
927 int crypt_benchmark(struct crypt_device *cd,
928         const char *cipher,
929         const char *cipher_mode,
930         size_t volume_key_size,
931         size_t iv_size,
932         size_t buffer_size,
933         double *encryption_mbs,
934         double *decryption_mbs);
935
936 int crypt_benchmark_kdf(struct crypt_device *cd,
937         const char *kdf,
938         const char *hash,
939         const char *password,
940         size_t password_size,
941         const char *salt,
942         size_t salt_size,
943         uint64_t *iterations_sec);
944
945 /**
946  * Get cipher used in device
947  *
948  * @param cd crypt device handle
949  *
950  * @return used cipher, e.g. "aes" or @e NULL otherwise
951  *
952  */
953 const char *crypt_get_cipher(struct crypt_device *cd);
954
955 /**
956  * Get cipher mode used in device
957  *
958  * @param cd crypt device handle
959  *
960  * @return used cipher mode e.g. "xts-plain" or @e otherwise
961  *
962  */
963 const char *crypt_get_cipher_mode(struct crypt_device *cd);
964
965 /**
966  * Get device UUID
967  *
968  * @param cd crypt device handle
969  *
970  * @return device UUID or @e NULL if not set
971  *
972  */
973 const char *crypt_get_uuid(struct crypt_device *cd);
974
975 /**
976  * Get path to underlaying device
977  *
978  * @param cd crypt device handle
979  *
980  * @return path to underlaying device name
981  *
982  */
983 const char *crypt_get_device_name(struct crypt_device *cd);
984
985 /**
986  * Get device offset in sectors where real data starts on underlying device)
987  *
988  * @param cd crypt device handle
989  *
990  * @return device offset in sectors
991  *
992  */
993 uint64_t crypt_get_data_offset(struct crypt_device *cd);
994
995 /**
996  * Get IV offset in sectors (skip)
997  *
998  * @param cd crypt device handle
999  *
1000  * @return IV offset
1001  *
1002  */
1003 uint64_t crypt_get_iv_offset(struct crypt_device *cd);
1004
1005 /**
1006  * Get size (in bytes) of volume key for crypt device
1007  *
1008  * @param cd crypt device handle
1009  *
1010  * @return volume key size
1011  *
1012  */
1013 int crypt_get_volume_key_size(struct crypt_device *cd);
1014
1015 /**
1016  * Get device parameters for VERITY device
1017  *
1018  * @param cd crypt device handle
1019  * @param vp verity device info
1020  *
1021  * @e 0 on success or negative errno value otherwise.
1022  *
1023  */
1024 int crypt_get_verity_info(struct crypt_device *cd,
1025         struct crypt_params_verity *vp);
1026 /** @} */
1027
1028 /**
1029  * @addtogroup keyslot
1030  * @{
1031  *
1032  */
1033
1034 /**
1035  * Crypt keyslot info
1036  */
1037 typedef enum {
1038         CRYPT_SLOT_INVALID, /**< invalid keyslot */
1039         CRYPT_SLOT_INACTIVE, /**< keyslot is inactive (free) */
1040         CRYPT_SLOT_ACTIVE, /**< keyslot is active (used) */
1041         CRYPT_SLOT_ACTIVE_LAST /**< keylost is active (used)
1042                                 *   and last used at the same time */
1043 } crypt_keyslot_info;
1044
1045 /**
1046  * Get information about particular key slot
1047  *
1048  *
1049  * @param cd crypt device handle
1050  * @param keyslot requested keyslot to check or CRYPT_ANY_SLOT
1051  *
1052  * @return value defined by crypt_keyslot_info
1053  *
1054  */
1055 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
1056 /** @} */
1057
1058 /**
1059  * Get number of keyslots supported for device type.
1060  *
1061  * @param type crypt device type
1062  *
1063  * @return slot count or negative errno otherwise if device
1064  * doesn't not support keyslots.
1065  */
1066 int crypt_keyslot_max(const char *type);
1067
1068 /**
1069  * Get keyslot area pointers (relative to metadata device)
1070  *
1071  * @param cd crypt device handle
1072  * @param keyslot keyslot number
1073  * @param offset offset on metadata device (in bytes)
1074  * @param length length of keyslot area (in bytes)
1075  *
1076  * @return @e 0 on success or negative errno value otherwise.
1077  *
1078  */
1079 int crypt_keyslot_area(struct crypt_device *cd,
1080         int keyslot,
1081         uint64_t *offset,
1082         uint64_t *length);
1083
1084 /**
1085  * Backup header and keyslots to file
1086  *
1087  * @param cd crypt device handle
1088  * @param requested_type @link crypt_type @endlink or @e NULL for all known
1089  * @param backup_file file to backup header to
1090  *
1091  * @return @e 0 on success or negative errno value otherwise.
1092  *
1093  */
1094 int crypt_header_backup(struct crypt_device *cd,
1095         const char *requested_type,
1096         const char *backup_file);
1097
1098 /**
1099  * Restore header and keyslots from backup file
1100  *
1101  *
1102  * @param cd crypt device handle
1103  * @param requested_type @link crypt_type @endlink or @e NULL for all known
1104  * @param backup_file file to restore header from
1105  *
1106  * @return @e 0 on success or negative errno value otherwise.
1107  *
1108  */
1109 int crypt_header_restore(struct crypt_device *cd,
1110         const char *requested_type,
1111         const char *backup_file);
1112
1113 /**
1114  * Receives last reported error
1115  *
1116  * @param cd crypt device handle
1117  * @param buf buffef for message
1118  * @param size size of buffer
1119  *
1120  * @note Note that this is old API function using global context.
1121  * All error messages are reported also through log callback.
1122  */
1123 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size);
1124
1125 /**
1126  * Receives last reported error, DEPRECATED
1127  *
1128  * @param buf buffef for message
1129  * @param size size of buffer
1130  *
1131  * @note Note that this is old API function using global context.
1132  * All error messages are reported also through log callback.
1133  */
1134 void crypt_get_error(char *buf, size_t size);
1135
1136 /**
1137  * Get directory where mapped crypt devices are created
1138  *
1139  * @return the directory path
1140  */
1141 const char *crypt_get_dir(void);
1142
1143 /**
1144  * @defgroup dbg "Library debug level"
1145  *
1146  * Set library debug level
1147  *
1148  * @addtogroup dbg
1149  * @{
1150  */
1151
1152 /** Debug all */
1153 #define CRYPT_DEBUG_ALL  -1
1154 /** Debug none */
1155 #define CRYPT_DEBUG_NONE  0
1156
1157 /**
1158  * Set the debug level for library
1159  *
1160  * @param level debug level
1161  *
1162  */
1163 void crypt_set_debug_level(int level);
1164
1165 /** @} */
1166
1167 #ifdef __cplusplus
1168 }
1169 #endif
1170 #endif /* _LIBCRYPTSETUP_H */