1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "json.h"
5 #include "macro.h"
6
7 #if HAVE_LIBCRYPTSETUP
8 #include <libcryptsetup.h>
9
10 /* These next two are defined in libcryptsetup.h from cryptsetup version 2.3.4 forwards. */
11 #ifndef CRYPT_ACTIVATE_NO_READ_WORKQUEUE
12 #define CRYPT_ACTIVATE_NO_READ_WORKQUEUE (1 << 24)
13 #endif
14 #ifndef CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE
15 #define CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE (1 << 25)
16 #endif
17
18 extern int (*sym_crypt_activate_by_passphrase)(struct crypt_device *cd, const char *name, int keyslot, const char *passphrase, size_t passphrase_size, uint32_t flags);
19 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
20 extern int (*sym_crypt_activate_by_signed_key)(struct crypt_device *cd, const char *name, const char *volume_key, size_t volume_key_size, const char *signature, size_t signature_size, uint32_t flags);
21 #endif
22 extern int (*sym_crypt_activate_by_volume_key)(struct crypt_device *cd, const char *name, const char *volume_key, size_t volume_key_size, uint32_t flags);
23 extern int (*sym_crypt_deactivate_by_name)(struct crypt_device *cd, const char *name, uint32_t flags);
24 extern int (*sym_crypt_format)(struct crypt_device *cd, const char *type, const char *cipher, const char *cipher_mode, const char *uuid, const char *volume_key, size_t volume_key_size, void *params);
25 extern void (*sym_crypt_free)(struct crypt_device *cd);
26 extern const char *(*sym_crypt_get_cipher)(struct crypt_device *cd);
27 extern const char *(*sym_crypt_get_cipher_mode)(struct crypt_device *cd);
28 extern uint64_t (*sym_crypt_get_data_offset)(struct crypt_device *cd);
29 extern const char *(*sym_crypt_get_device_name)(struct crypt_device *cd);
30 extern const char *(*sym_crypt_get_dir)(void);
31 extern const char *(*sym_crypt_get_type)(struct crypt_device *cd);
32 extern const char *(*sym_crypt_get_uuid)(struct crypt_device *cd);
33 extern int (*sym_crypt_get_verity_info)(struct crypt_device *cd, struct crypt_params_verity *vp);
34 extern int (*sym_crypt_get_volume_key_size)(struct crypt_device *cd);
35 extern int (*sym_crypt_init)(struct crypt_device **cd, const char *device);
36 extern int (*sym_crypt_init_by_name)(struct crypt_device **cd, const char *name);
37 extern int (*sym_crypt_keyslot_add_by_volume_key)(struct crypt_device *cd, int keyslot, const char *volume_key, size_t volume_key_size, const char *passphrase, size_t passphrase_size);
38 extern int (*sym_crypt_keyslot_destroy)(struct crypt_device *cd, int keyslot);
39 extern int (*sym_crypt_keyslot_max)(const char *type);
40 extern int (*sym_crypt_load)(struct crypt_device *cd, const char *requested_type, void *params);
41 extern int (*sym_crypt_resize)(struct crypt_device *cd, const char *name, uint64_t new_size);
42 extern int (*sym_crypt_resume_by_passphrase)(struct crypt_device *cd, const char *name, int keyslot, const char *passphrase, size_t passphrase_size);
43 extern int (*sym_crypt_set_data_device)(struct crypt_device *cd, const char *device);
44 extern void (*sym_crypt_set_debug_level)(int level);
45 extern void (*sym_crypt_set_log_callback)(struct crypt_device *cd, void (*log)(int level, const char *msg, void *usrptr), void *usrptr);
46 #if HAVE_CRYPT_SET_METADATA_SIZE
47 extern int (*sym_crypt_set_metadata_size)(struct crypt_device *cd, uint64_t metadata_size, uint64_t keyslots_size);
48 #endif
49 extern int (*sym_crypt_set_pbkdf_type)(struct crypt_device *cd, const struct crypt_pbkdf_type *pbkdf);
50 extern int (*sym_crypt_suspend)(struct crypt_device *cd, const char *name);
51 extern int (*sym_crypt_token_json_get)(struct crypt_device *cd, int token, const char **json);
52 extern int (*sym_crypt_token_json_set)(struct crypt_device *cd, int token, const char *json);
53 #if HAVE_CRYPT_TOKEN_MAX
54 extern int (*sym_crypt_token_max)(const char *type);
55 #else
56 /* As a fallback, use the same hard-coded value libcryptsetup uses internally. */
crypt_token_max(_unused_ const char * type)57 static inline int crypt_token_max(_unused_ const char *type) {
58 assert(streq(type, CRYPT_LUKS2));
59
60 return 32;
61 }
62 #define sym_crypt_token_max(type) crypt_token_max(type)
63 #endif
64 extern crypt_token_info (*sym_crypt_token_status)(struct crypt_device *cd, int token, const char **type);
65 extern int (*sym_crypt_volume_key_get)(struct crypt_device *cd, int keyslot, char *volume_key, size_t *volume_key_size, const char *passphrase, size_t passphrase_size);
66
67 int dlopen_cryptsetup(void);
68
69 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct crypt_device *, crypt_free, NULL);
70 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct crypt_device *, sym_crypt_free, NULL);
71
72 void cryptsetup_enable_logging(struct crypt_device *cd);
73
74 int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd);
75
76 int cryptsetup_get_token_as_json(struct crypt_device *cd, int idx, const char *verify_type, JsonVariant **ret);
77 int cryptsetup_get_keyslot_from_token(JsonVariant *v);
78 int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v);
79
80 #else
81
82 /* If libcryptsetup is not available, let's at least define the basic type and NOP destructors for it, to
83 * make a little bit less #ifdeferry necessary in main programs. */
84 struct crypt_device;
sym_crypt_free(struct crypt_device * cd)85 static inline void sym_crypt_free(struct crypt_device* cd) {}
sym_crypt_freep(struct crypt_device ** cd)86 static inline void sym_crypt_freep(struct crypt_device** cd) {}
87
88 #endif
89
mangle_none(const char * s)90 static inline const char *mangle_none(const char *s) {
91 /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */
92 return isempty(s) || STR_IN_SET(s, "-", "none") ? NULL : s;
93 }
94