Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / internal.h
1 /*
2  * libcryptsetup - cryptsetup library internal
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2023 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 INTERNAL_H
25 #define INTERNAL_H
26
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <stdbool.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <inttypes.h>
33 #include <fcntl.h>
34 #include <assert.h>
35
36 #include "nls.h"
37 #include "bitops.h"
38 #include "utils_blkid.h"
39 #include "utils_crypt.h"
40 #include "utils_loop.h"
41 #include "utils_dm.h"
42 #include "utils_keyring.h"
43 #include "utils_io.h"
44 #include "crypto_backend/crypto_backend.h"
45 #include "utils_storage_wrappers.h"
46
47 #include "libcryptsetup.h"
48
49 #include "libcryptsetup_macros.h"
50 #include "libcryptsetup_symver.h"
51
52 #define LOG_MAX_LEN             4096
53 #define MAX_DM_DEPS             32
54
55 #define CRYPT_SUBDEV           "SUBDEV" /* prefix for sublayered devices underneath public crypt types */
56
57 #ifndef O_CLOEXEC
58 #define O_CLOEXEC 0
59 #endif
60
61 struct crypt_device;
62 struct luks2_reencrypt;
63
64 struct volume_key {
65         int id;
66         size_t keylength;
67         const char *key_description;
68         struct volume_key *next;
69         char key[];
70 };
71
72 struct volume_key *crypt_alloc_volume_key(size_t keylength, const char *key);
73 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, size_t keylength);
74 void crypt_free_volume_key(struct volume_key *vk);
75 int crypt_volume_key_set_description(struct volume_key *key, const char *key_description);
76 void crypt_volume_key_set_id(struct volume_key *vk, int id);
77 int crypt_volume_key_get_id(const struct volume_key *vk);
78 void crypt_volume_key_add_next(struct volume_key **vks, struct volume_key *vk);
79 struct volume_key *crypt_volume_key_next(struct volume_key *vk);
80 struct volume_key *crypt_volume_key_by_id(struct volume_key *vk, int id);
81
82 struct crypt_pbkdf_type *crypt_get_pbkdf(struct crypt_device *cd);
83 int init_pbkdf_type(struct crypt_device *cd,
84                     const struct crypt_pbkdf_type *pbkdf,
85                     const char *dev_type);
86 int verify_pbkdf_params(struct crypt_device *cd,
87                         const struct crypt_pbkdf_type *pbkdf);
88 int crypt_benchmark_pbkdf_internal(struct crypt_device *cd,
89                                    struct crypt_pbkdf_type *pbkdf,
90                                    size_t volume_key_size);
91 const char *crypt_get_cipher_spec(struct crypt_device *cd);
92
93 /* Device backend */
94 struct device;
95 int device_alloc(struct crypt_device *cd, struct device **device, const char *path);
96 int device_alloc_no_check(struct device **device, const char *path);
97 void device_close(struct crypt_device *cd, struct device *device);
98 void device_free(struct crypt_device *cd, struct device *device);
99 const char *device_path(const struct device *device);
100 const char *device_dm_name(const struct device *device);
101 const char *device_block_path(const struct device *device);
102 void device_topology_alignment(struct crypt_device *cd,
103                                struct device *device,
104                                unsigned long *required_alignment, /* bytes */
105                                unsigned long *alignment_offset,   /* bytes */
106                                unsigned long default_alignment);
107 size_t device_block_size(struct crypt_device *cd, struct device *device);
108 int device_read_ahead(struct device *device, uint32_t *read_ahead);
109 int device_size(struct device *device, uint64_t *size);
110 int device_open(struct crypt_device *cd, struct device *device, int flags);
111 int device_open_excl(struct crypt_device *cd, struct device *device, int flags);
112 void device_release_excl(struct crypt_device *cd, struct device *device);
113 void device_disable_direct_io(struct device *device);
114 int device_is_identical(struct device *device1, struct device *device2);
115 int device_is_rotational(struct device *device);
116 size_t device_alignment(struct device *device);
117 int device_direct_io(const struct device *device);
118 int device_fallocate(struct device *device, uint64_t size);
119 void device_sync(struct crypt_device *cd, struct device *device);
120 int device_check_size(struct crypt_device *cd,
121                       struct device *device,
122                       uint64_t req_offset, int falloc);
123 void device_set_block_size(struct device *device, size_t size);
124 size_t device_optimal_encryption_sector_size(struct crypt_device *cd, struct device *device);
125
126 int device_open_locked(struct crypt_device *cd, struct device *device, int flags);
127 int device_read_lock(struct crypt_device *cd, struct device *device);
128 int device_write_lock(struct crypt_device *cd, struct device *device);
129 void device_read_unlock(struct crypt_device *cd, struct device *device);
130 void device_write_unlock(struct crypt_device *cd, struct device *device);
131 bool device_is_locked(struct device *device);
132
133 enum devcheck { DEV_OK = 0, DEV_EXCL = 1 };
134 int device_check_access(struct crypt_device *cd,
135                         struct device *device,
136                         enum devcheck device_check);
137 int device_block_adjust(struct crypt_device *cd,
138                         struct device *device,
139                         enum devcheck device_check,
140                         uint64_t device_offset,
141                         uint64_t *size,
142                         uint32_t *flags);
143 size_t size_round_up(size_t size, size_t block);
144
145 int create_or_reload_device(struct crypt_device *cd, const char *name,
146                      const char *type, struct crypt_dm_active_device *dmd);
147
148 int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name,
149                      const char *type, struct crypt_dm_active_device *dmd,
150                      struct crypt_dm_active_device *dmdi);
151
152 /* Receive backend devices from context helpers */
153 struct device *crypt_metadata_device(struct crypt_device *cd);
154 struct device *crypt_data_device(struct crypt_device *cd);
155
156 int crypt_confirm(struct crypt_device *cd, const char *msg);
157
158 char *crypt_lookup_dev(const char *dev_id);
159 int crypt_dev_is_rotational(int major, int minor);
160 int crypt_dev_is_partition(const char *dev_path);
161 char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size);
162 char *crypt_get_base_device(const char *dev_path);
163 uint64_t crypt_dev_partition_offset(const char *dev_path);
164 int lookup_by_disk_id(const char *dm_uuid);
165 int lookup_by_sysfs_uuid_field(const char *dm_uuid);
166 int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid);
167
168 size_t crypt_getpagesize(void);
169 unsigned crypt_cpusonline(void);
170 uint64_t crypt_getphysmemory_kb(void);
171
172 int init_crypto(struct crypt_device *ctx);
173
174 #define log_dbg(c, x...) crypt_logf(c, CRYPT_LOG_DEBUG, x)
175 #define log_std(c, x...) crypt_logf(c, CRYPT_LOG_NORMAL, x)
176 #define log_verbose(c, x...) crypt_logf(c, CRYPT_LOG_VERBOSE, x)
177 #define log_err(c, x...) crypt_logf(c, CRYPT_LOG_ERROR, x)
178
179 int crypt_get_debug_level(void);
180
181 void crypt_process_priority(struct crypt_device *cd, int *priority, bool raise);
182
183 int crypt_metadata_locking_enabled(void);
184
185 int crypt_random_init(struct crypt_device *ctx);
186 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality);
187 void crypt_random_exit(void);
188 int crypt_random_default_key_rng(void);
189
190 int crypt_plain_hash(struct crypt_device *cd,
191                      const char *hash_name,
192                      char *key, size_t key_size,
193                      const char *passphrase, size_t passphrase_size);
194 int PLAIN_activate(struct crypt_device *cd,
195                      const char *name,
196                      struct volume_key *vk,
197                      uint64_t size,
198                      uint32_t flags);
199
200 void *crypt_get_hdr(struct crypt_device *cd, const char *type);
201 void crypt_set_luks2_reencrypt(struct crypt_device *cd, struct luks2_reencrypt *rh);
202 struct luks2_reencrypt *crypt_get_luks2_reencrypt(struct crypt_device *cd);
203
204 int onlyLUKS2(struct crypt_device *cd);
205 int onlyLUKS2mask(struct crypt_device *cd, uint32_t mask);
206
207 int crypt_wipe_device(struct crypt_device *cd,
208         struct device *device,
209         crypt_wipe_pattern pattern,
210         uint64_t offset,
211         uint64_t length,
212         size_t wipe_block_size,
213         int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
214         void *usrptr);
215
216 /* Internal integrity helpers */
217 const char *crypt_get_integrity(struct crypt_device *cd);
218 int crypt_get_integrity_key_size(struct crypt_device *cd);
219 int crypt_get_integrity_tag_size(struct crypt_device *cd);
220
221 int crypt_key_in_keyring(struct crypt_device *cd);
222 void crypt_set_key_in_keyring(struct crypt_device *cd, unsigned key_in_keyring);
223 int crypt_volume_key_load_in_keyring(struct crypt_device *cd, struct volume_key *vk);
224 int crypt_use_keyring_for_vk(struct crypt_device *cd);
225 void crypt_drop_keyring_key_by_description(struct crypt_device *cd, const char *key_description, key_type_t ktype);
226 void crypt_drop_keyring_key(struct crypt_device *cd, struct volume_key *vks);
227
228 static inline uint64_t compact_version(uint16_t major, uint16_t minor, uint16_t patch, uint16_t release)
229 {
230         return (uint64_t)release | ((uint64_t)patch << 16) | ((uint64_t)minor << 32) | ((uint64_t)major << 48);
231 }
232
233 int kernel_version(uint64_t *kversion);
234
235 int crypt_serialize_lock(struct crypt_device *cd);
236 void crypt_serialize_unlock(struct crypt_device *cd);
237
238 bool crypt_string_in(const char *str, char **list, size_t list_size);
239 int crypt_strcmp(const char *a, const char *b);
240 int crypt_compare_dm_devices(struct crypt_device *cd,
241                                const struct crypt_dm_active_device *src,
242                                const struct crypt_dm_active_device *tgt);
243 static inline void *crypt_zalloc(size_t size) { return calloc(1, size); }
244
245 static inline bool uint64_mult_overflow(uint64_t *u, uint64_t b, size_t size)
246 {
247         *u = (uint64_t)b * size;
248         if ((uint64_t)(*u / size) != b)
249                 return true;
250         return false;
251 }
252
253 #endif /* INTERNAL_H */