]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/cryptsetup-util.h
Merge pull request #31807 from yuwata/sd-ndisc-send
[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 #if HAVE_CRYPT_RESUME_BY_VOLUME_KEY
45 DLSYM_PROTOTYPE(crypt_resume_by_volume_key);
46 #endif
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);
52 #endif
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);
59 #else
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));
63
64 return 32;
65 }
66 #define sym_crypt_token_max(type) crypt_token_max(type)
67 #endif
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);
72 #endif
73 #if HAVE_CRYPT_REENCRYPT
74 DISABLE_WARNING_DEPRECATED_DECLARATIONS;
75 DLSYM_PROTOTYPE(crypt_reencrypt);
76 REENABLE_WARNING;
77 #endif
78 DLSYM_PROTOTYPE(crypt_metadata_locking);
79 #if HAVE_CRYPT_SET_DATA_OFFSET
80 DLSYM_PROTOTYPE(crypt_set_data_offset);
81 #endif
82 DLSYM_PROTOTYPE(crypt_header_restore);
83 DLSYM_PROTOTYPE(crypt_volume_key_keyring);
84
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);
87
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)
91
92 void cryptsetup_enable_logging(struct crypt_device *cd);
93
94 int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd);
95
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);
98
99 #else
100
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. */
103 struct crypt_device;
104 static inline void sym_crypt_free(struct crypt_device* cd) {}
105 static inline void sym_crypt_freep(struct crypt_device** cd) {}
106
107 #endif
108
109 int dlopen_cryptsetup(void);
110
111 int cryptsetup_get_keyslot_from_token(JsonVariant *v);
112
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;
116 }