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