Fixed the build error for riscv64 arch using gcc 13
[platform/upstream/cryptsetup.git] / lib / utils_dm.h
1 /*
2  * libdevmapper - device-mapper backend for cryptsetup
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2021 Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #ifndef _UTILS_DM_H
25 #define _UTILS_DM_H
26
27 /* device-mapper library helpers */
28 #include <stddef.h>
29 #include <stdint.h>
30
31 struct crypt_device;
32 struct volume_key;
33 struct crypt_params_verity;
34 struct device;
35 struct crypt_params_integrity;
36
37 /* Device mapper internal flags */
38 #define DM_RESUME_PRIVATE      (1 << 4) /* CRYPT_ACTIVATE_PRIVATE */
39 #define DM_SUSPEND_SKIP_LOCKFS (1 << 5)
40 #define DM_SUSPEND_WIPE_KEY    (1 << 6)
41 #define DM_SUSPEND_NOFLUSH     (1 << 7)
42
43 static inline uint32_t act2dmflags(uint32_t act_flags)
44 {
45         return (act_flags & DM_RESUME_PRIVATE);
46 }
47
48 /* Device mapper backend - kernel support flags */
49 #define DM_KEY_WIPE_SUPPORTED (1 << 0)  /* key wipe message */
50 #define DM_LMK_SUPPORTED      (1 << 1)  /* lmk mode */
51 #define DM_SECURE_SUPPORTED   (1 << 2)  /* wipe (secure) buffer flag */
52 #define DM_PLAIN64_SUPPORTED  (1 << 3)  /* plain64 IV */
53 #define DM_DISCARDS_SUPPORTED (1 << 4)  /* discards/TRIM option is supported */
54 #define DM_VERITY_SUPPORTED   (1 << 5)  /* dm-verity target supported */
55 #define DM_TCW_SUPPORTED      (1 << 6)  /* tcw (TCRYPT CBC with whitening) */
56 #define DM_SAME_CPU_CRYPT_SUPPORTED (1 << 7) /* same_cpu_crypt */
57 #define DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED (1 << 8) /* submit_from_crypt_cpus */
58 #define DM_VERITY_ON_CORRUPTION_SUPPORTED (1 << 9) /* ignore/restart_on_corruption, ignore_zero_block */
59 #define DM_VERITY_FEC_SUPPORTED (1 << 10) /* Forward Error Correction (FEC) */
60 #define DM_KERNEL_KEYRING_SUPPORTED (1 << 11) /* dm-crypt allows loading kernel keyring keys */
61 #define DM_INTEGRITY_SUPPORTED (1 << 12) /* dm-integrity target supported */
62 #define DM_SECTOR_SIZE_SUPPORTED (1 << 13) /* support for sector size setting in dm-crypt/dm-integrity */
63 #define DM_CAPI_STRING_SUPPORTED (1 << 14) /* support for cryptoapi format cipher definition */
64 #define DM_DEFERRED_SUPPORTED (1 << 15) /* deferred removal of device */
65 #define DM_INTEGRITY_RECALC_SUPPORTED (1 << 16) /* dm-integrity automatic recalculation supported */
66 #define DM_INTEGRITY_BITMAP_SUPPORTED (1 << 17) /* dm-integrity bitmap mode supported */
67 #define DM_GET_TARGET_VERSION_SUPPORTED (1 << 18) /* dm DM_GET_TARGET version ioctl supported */
68 #define DM_INTEGRITY_FIX_PADDING_SUPPORTED (1 << 19) /* supports the parameter fix_padding that fixes a bug that caused excessive padding */
69 #define DM_BITLK_EBOIV_SUPPORTED (1 << 20) /* EBOIV for BITLK supported */
70 #define DM_BITLK_ELEPHANT_SUPPORTED (1 << 21) /* Elephant diffuser for BITLK supported */
71 #define DM_VERITY_SIGNATURE_SUPPORTED (1 << 22) /* Verity option root_hash_sig_key_desc supported */
72 #define DM_INTEGRITY_DISCARDS_SUPPORTED (1 << 23) /* dm-integrity discards/TRIM option is supported */
73 #define DM_VERITY_PANIC_CORRUPTION_SUPPORTED (1 << 24) /* dm-verity panic on corruption  */
74 #define DM_CRYPT_NO_WORKQUEUE_SUPPORTED (1 << 25) /* dm-crypt suppot for bypassing workqueues  */
75 #define DM_INTEGRITY_FIX_HMAC_SUPPORTED (1 << 26) /* hmac covers also superblock */
76
77 typedef enum { DM_CRYPT = 0, DM_VERITY, DM_INTEGRITY, DM_LINEAR, DM_ERROR, DM_ZERO, DM_UNKNOWN } dm_target_type;
78 enum tdirection { TARGET_SET = 1, TARGET_QUERY };
79
80 int dm_flags(struct crypt_device *cd, dm_target_type target, uint32_t *flags);
81
82 #define DM_ACTIVE_DEVICE        (1 << 0)
83 #define DM_ACTIVE_UUID          (1 << 1)
84 #define DM_ACTIVE_HOLDERS       (1 << 2)
85
86 #define DM_ACTIVE_CRYPT_CIPHER  (1 << 3)
87 #define DM_ACTIVE_CRYPT_KEYSIZE (1 << 4)
88 #define DM_ACTIVE_CRYPT_KEY     (1 << 5)
89
90 #define DM_ACTIVE_VERITY_ROOT_HASH      (1 << 6)
91 #define DM_ACTIVE_VERITY_HASH_DEVICE    (1 << 7)
92 #define DM_ACTIVE_VERITY_PARAMS         (1 << 8)
93
94 #define DM_ACTIVE_INTEGRITY_PARAMS      (1 << 9)
95
96 struct dm_target {
97         dm_target_type type;
98         enum tdirection direction;
99         uint64_t offset;
100         uint64_t size;
101         struct device *data_device;
102         union {
103         struct {
104                 const char *cipher;
105                 const char *integrity;
106
107                 /* Active key for device */
108                 struct volume_key *vk;
109
110                 /* struct crypt_active_device */
111                 uint64_t offset;        /* offset in sectors */
112                 uint64_t iv_offset;     /* IV initialisation sector */
113                 uint32_t tag_size;      /* additional on-disk tag size */
114                 uint32_t sector_size;   /* encryption sector size */
115         } crypt;
116         struct {
117                 struct device *hash_device;
118                 struct device *fec_device;
119
120                 const char *root_hash;
121                 uint32_t root_hash_size;
122                 const char *root_hash_sig_key_desc;
123
124                 uint64_t hash_offset;   /* hash offset in blocks (not header) */
125                 uint64_t fec_offset;    /* FEC offset in blocks (not header) */
126                 uint64_t fec_blocks;    /* FEC blocks covering data + hash + padding (foreign metadata)*/
127                 struct crypt_params_verity *vp;
128         } verity;
129         struct {
130                 uint64_t journal_size;
131                 uint32_t journal_watermark;
132                 uint32_t journal_commit_time;
133                 uint32_t interleave_sectors;
134                 uint32_t tag_size;
135                 uint64_t offset;        /* offset in sectors */
136                 uint32_t sector_size;   /* integrity sector size */
137                 uint32_t buffer_sectors;
138
139                 const char *integrity;
140                 /* Active key for device */
141                 struct volume_key *vk;
142
143                 const char *journal_integrity;
144                 struct volume_key *journal_integrity_key;
145
146                 const char *journal_crypt;
147                 struct volume_key *journal_crypt_key;
148
149                 struct device *meta_device;
150
151                 bool fix_padding;
152                 bool fix_hmac;
153                 bool legacy_recalc;
154         } integrity;
155         struct {
156                 uint64_t offset;
157         } linear;
158         struct {
159         } zero;
160         } u;
161
162         char *params;
163         struct dm_target *next;
164 };
165
166 struct crypt_dm_active_device {
167         uint64_t size;          /* active device size */
168         uint32_t flags;         /* activation flags */
169         const char *uuid;
170
171         unsigned holders:1;     /* device holders detected (on query only) */
172
173         struct dm_target segment;
174 };
175
176 static inline bool single_segment(const struct crypt_dm_active_device *dmd)
177 {
178         return dmd && !dmd->segment.next;
179 }
180
181 void dm_backend_init(struct crypt_device *cd);
182 void dm_backend_exit(struct crypt_device *cd);
183
184 int dm_targets_allocate(struct dm_target *first, unsigned count);
185 void dm_targets_free(struct crypt_device *cd, struct crypt_dm_active_device *dmd);
186
187 int dm_crypt_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
188         struct device *data_device, struct volume_key *vk, const char *cipher,
189         uint64_t iv_offset, uint64_t data_offset, const char *integrity,
190         uint32_t tag_size, uint32_t sector_size);
191 int dm_verity_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
192         struct device *data_device, struct device *hash_device, struct device *fec_device,
193         const char *root_hash, uint32_t root_hash_size, const char* root_hash_sig_key_desc,
194         uint64_t hash_offset_block, uint64_t fec_blocks, struct crypt_params_verity *vp);
195 int dm_integrity_target_set(struct crypt_device *cd,
196         struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
197         struct device *meta_device,
198         struct device *data_device, uint64_t tag_size, uint64_t offset, uint32_t sector_size,
199         struct volume_key *vk,
200         struct volume_key *journal_crypt_key, struct volume_key *journal_mac_key,
201         const struct crypt_params_integrity *ip);
202 int dm_linear_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
203         struct device *data_device, uint64_t data_offset);
204 int dm_zero_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size);
205
206 int dm_remove_device(struct crypt_device *cd, const char *name, uint32_t flags);
207 int dm_status_device(struct crypt_device *cd, const char *name);
208 int dm_status_suspended(struct crypt_device *cd, const char *name);
209 int dm_status_verity_ok(struct crypt_device *cd, const char *name);
210 int dm_status_integrity_failures(struct crypt_device *cd, const char *name, uint64_t *count);
211 int dm_query_device(struct crypt_device *cd, const char *name,
212                     uint32_t get_flags, struct crypt_dm_active_device *dmd);
213 int dm_device_deps(struct crypt_device *cd, const char *name, const char *prefix,
214                    char **names, size_t names_length);
215 int dm_create_device(struct crypt_device *cd, const char *name,
216                      const char *type, struct crypt_dm_active_device *dmd);
217 int dm_reload_device(struct crypt_device *cd, const char *name,
218                      struct crypt_dm_active_device *dmd, uint32_t dmflags, unsigned resume);
219 int dm_suspend_device(struct crypt_device *cd, const char *name, uint32_t dmflags);
220 int dm_resume_device(struct crypt_device *cd, const char *name, uint32_t dmflags);
221 int dm_resume_and_reinstate_key(struct crypt_device *cd, const char *name,
222                                 const struct volume_key *vk);
223 int dm_error_device(struct crypt_device *cd, const char *name);
224 int dm_clear_device(struct crypt_device *cd, const char *name);
225
226 const char *dm_get_dir(void);
227
228 int lookup_dm_dev_by_uuid(struct crypt_device *cd, const char *uuid, const char *type);
229
230 /* These are DM helpers used only by utils_devpath file */
231 int dm_is_dm_device(int major);
232 int dm_is_dm_kernel_name(const char *name);
233 char *dm_device_path(const char *prefix, int major, int minor);
234 char *dm_device_name(const char *path);
235
236 #endif /* _UTILS_DM_H */