1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "alloc-util.h"
5 #include "dlfcn-util.h"
10 #include <libcryptsetup.h>
12 /* These next two are defined in libcryptsetup.h from cryptsetup version 2.3.4 forwards. */
13 #ifndef CRYPT_ACTIVATE_NO_READ_WORKQUEUE
14 #define CRYPT_ACTIVATE_NO_READ_WORKQUEUE (1 << 24)
16 #ifndef CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE
17 #define CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE (1 << 25)
20 DLSYM_PROTOTYPE(crypt_activate_by_passphrase
);
21 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
22 DLSYM_PROTOTYPE(crypt_activate_by_signed_key
);
24 DLSYM_PROTOTYPE(crypt_activate_by_volume_key
);
25 DLSYM_PROTOTYPE(crypt_deactivate_by_name
);
26 DLSYM_PROTOTYPE(crypt_format
);
27 DLSYM_PROTOTYPE(crypt_free
);
28 DLSYM_PROTOTYPE(crypt_get_cipher
);
29 DLSYM_PROTOTYPE(crypt_get_cipher_mode
);
30 DLSYM_PROTOTYPE(crypt_get_data_offset
);
31 DLSYM_PROTOTYPE(crypt_get_device_name
);
32 DLSYM_PROTOTYPE(crypt_get_dir
);
33 DLSYM_PROTOTYPE(crypt_get_type
);
34 DLSYM_PROTOTYPE(crypt_get_uuid
);
35 DLSYM_PROTOTYPE(crypt_get_verity_info
);
36 DLSYM_PROTOTYPE(crypt_get_volume_key_size
);
37 DLSYM_PROTOTYPE(crypt_init
);
38 DLSYM_PROTOTYPE(crypt_init_by_name
);
39 DLSYM_PROTOTYPE(crypt_keyslot_add_by_volume_key
);
40 DLSYM_PROTOTYPE(crypt_keyslot_destroy
);
41 DLSYM_PROTOTYPE(crypt_keyslot_max
);
42 DLSYM_PROTOTYPE(crypt_load
);
43 DLSYM_PROTOTYPE(crypt_resize
);
44 #if HAVE_CRYPT_RESUME_BY_VOLUME_KEY
45 DLSYM_PROTOTYPE(crypt_resume_by_volume_key
);
47 DLSYM_PROTOTYPE(crypt_set_data_device
);
48 DLSYM_PROTOTYPE(crypt_set_debug_level
);
49 DLSYM_PROTOTYPE(crypt_set_log_callback
);
50 #if HAVE_CRYPT_SET_METADATA_SIZE
51 DLSYM_PROTOTYPE(crypt_set_metadata_size
);
53 DLSYM_PROTOTYPE(crypt_set_pbkdf_type
);
54 DLSYM_PROTOTYPE(crypt_suspend
);
55 DLSYM_PROTOTYPE(crypt_token_json_get
);
56 DLSYM_PROTOTYPE(crypt_token_json_set
);
57 #if HAVE_CRYPT_TOKEN_MAX
58 DLSYM_PROTOTYPE(crypt_token_max
);
60 /* As a fallback, use the same hard-coded value libcryptsetup uses internally. */
61 static inline int crypt_token_max(_unused_
const char *type
) {
62 assert(streq(type
, CRYPT_LUKS2
));
66 #define sym_crypt_token_max(type) crypt_token_max(type)
68 DLSYM_PROTOTYPE(crypt_token_status
);
69 DLSYM_PROTOTYPE(crypt_volume_key_get
);
70 #if HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE
71 DLSYM_PROTOTYPE(crypt_reencrypt_init_by_passphrase
);
73 #if HAVE_CRYPT_REENCRYPT
74 DISABLE_WARNING_DEPRECATED_DECLARATIONS
;
75 DLSYM_PROTOTYPE(crypt_reencrypt
);
78 DLSYM_PROTOTYPE(crypt_metadata_locking
);
79 #if HAVE_CRYPT_SET_DATA_OFFSET
80 DLSYM_PROTOTYPE(crypt_set_data_offset
);
82 DLSYM_PROTOTYPE(crypt_header_restore
);
83 DLSYM_PROTOTYPE(crypt_volume_key_keyring
);
85 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct crypt_device
*, crypt_free
, NULL
);
86 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct crypt_device
*, sym_crypt_free
, NULL
);
88 /* Be careful, this works with dlopen_cryptsetup(), that is, it calls sym_crypt_free() instead of crypt_free(). */
89 #define crypt_free_and_replace(a, b) \
90 free_and_replace_full(a, b, sym_crypt_free)
92 void cryptsetup_enable_logging(struct crypt_device
*cd
);
94 int cryptsetup_set_minimal_pbkdf(struct crypt_device
*cd
);
96 int cryptsetup_get_token_as_json(struct crypt_device
*cd
, int idx
, const char *verify_type
, JsonVariant
**ret
);
97 int cryptsetup_add_token_json(struct crypt_device
*cd
, JsonVariant
*v
);
101 /* If libcryptsetup is not available, let's at least define the basic type and NOP destructors for it, to
102 * make a little bit less #ifdeferry necessary in main programs. */
104 static inline void sym_crypt_free(struct crypt_device
* cd
) {}
105 static inline void sym_crypt_freep(struct crypt_device
** cd
) {}
109 int dlopen_cryptsetup(void);
111 int cryptsetup_get_keyslot_from_token(JsonVariant
*v
);
113 static inline const char *mangle_none(const char *s
) {
114 /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */
115 return isempty(s
) || STR_IN_SET(s
, "-", "none") ? NULL
: s
;