]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/cryptsetup-util.h
Merge pull request #30480 from keszybz/kernel-install-more-paths
[thirdparty/systemd.git] / src / shared / cryptsetup-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "alloc-util.h"
5 #include "dlfcn-util.h"
6 #include "json.h"
7 #include "macro.h"
8
9 #if HAVE_LIBCRYPTSETUP
10 #include <libcryptsetup.h>
11
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)
15 #endif
16 #ifndef CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE
17 #define CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE (1 << 25)
18 #endif
19
20 DLSYM_PROTOTYPE(crypt_activate_by_passphrase);
21 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
22 DLSYM_PROTOTYPE(crypt_activate_by_signed_key);
23 #endif
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 DLSYM_PROTOTYPE(crypt_resume_by_passphrase);
45 DLSYM_PROTOTYPE(crypt_set_data_device);
46 DLSYM_PROTOTYPE(crypt_set_debug_level);
47 DLSYM_PROTOTYPE(crypt_set_log_callback);
48 #if HAVE_CRYPT_SET_METADATA_SIZE
49 DLSYM_PROTOTYPE(crypt_set_metadata_size);
50 #endif
51 DLSYM_PROTOTYPE(crypt_set_pbkdf_type);
52 DLSYM_PROTOTYPE(crypt_suspend);
53 DLSYM_PROTOTYPE(crypt_token_json_get);
54 DLSYM_PROTOTYPE(crypt_token_json_set);
55 #if HAVE_CRYPT_TOKEN_MAX
56 DLSYM_PROTOTYPE(crypt_token_max);
57 #else
58 /* As a fallback, use the same hard-coded value libcryptsetup uses internally. */
59 static inline int crypt_token_max(_unused_ const char *type) {
60 assert(streq(type, CRYPT_LUKS2));
61
62 return 32;
63 }
64 #define sym_crypt_token_max(type) crypt_token_max(type)
65 #endif
66 DLSYM_PROTOTYPE(crypt_token_status);
67 DLSYM_PROTOTYPE(crypt_volume_key_get);
68 #if HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE
69 DLSYM_PROTOTYPE(crypt_reencrypt_init_by_passphrase);
70 #endif
71 #if HAVE_CRYPT_REENCRYPT
72 DISABLE_WARNING_DEPRECATED_DECLARATIONS;
73 DLSYM_PROTOTYPE(crypt_reencrypt);
74 REENABLE_WARNING;
75 #endif
76 DLSYM_PROTOTYPE(crypt_metadata_locking);
77 #if HAVE_CRYPT_SET_DATA_OFFSET
78 DLSYM_PROTOTYPE(crypt_set_data_offset);
79 #endif
80 DLSYM_PROTOTYPE(crypt_header_restore);
81 DLSYM_PROTOTYPE(crypt_volume_key_keyring);
82
83 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct crypt_device *, crypt_free, NULL);
84 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct crypt_device *, sym_crypt_free, NULL);
85
86 /* Be careful, this works with dlopen_cryptsetup(), that is, it calls sym_crypt_free() instead of crypt_free(). */
87 #define crypt_free_and_replace(a, b) \
88 free_and_replace_full(a, b, sym_crypt_free)
89
90 void cryptsetup_enable_logging(struct crypt_device *cd);
91
92 int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd);
93
94 int cryptsetup_get_token_as_json(struct crypt_device *cd, int idx, const char *verify_type, JsonVariant **ret);
95 int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v);
96
97 #else
98
99 /* If libcryptsetup is not available, let's at least define the basic type and NOP destructors for it, to
100 * make a little bit less #ifdeferry necessary in main programs. */
101 struct crypt_device;
102 static inline void sym_crypt_free(struct crypt_device* cd) {}
103 static inline void sym_crypt_freep(struct crypt_device** cd) {}
104
105 #endif
106
107 int dlopen_cryptsetup(void);
108
109 int cryptsetup_get_keyslot_from_token(JsonVariant *v);
110
111 static inline const char *mangle_none(const char *s) {
112 /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */
113 return isempty(s) || STR_IN_SET(s, "-", "none") ? NULL : s;
114 }