a418a465a85eba877f8177be360e9eb4338b7dba
[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-2020 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2020 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
35 #include "nls.h"
36 #include "bitops.h"
37 #include "utils_blkid.h"
38 #include "utils_crypt.h"
39 #include "utils_loop.h"
40 #include "utils_dm.h"
41 #include "utils_fips.h"
42 #include "utils_keyring.h"
43 #include "utils_io.h"
44 #include "crypto_backend.h"
45 #include "utils_storage_wrappers.h"
46
47 #include "libcryptsetup.h"
48
49 /* to silent gcc -Wcast-qual for const cast */
50 #define CONST_CAST(x) (x)(uintptr_t)
51
52 #define SHIFT_4K                12
53 #define SECTOR_SHIFT            9
54 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
55 #define MAX_SECTOR_SIZE         4096 /* min page size among all platforms */
56 #define DEFAULT_DISK_ALIGNMENT  1048576 /* 1MiB */
57 #define DEFAULT_MEM_ALIGNMENT   4096
58 #define LOG_MAX_LEN             4096
59 #define MAX_DM_DEPS             32
60
61 #define CRYPT_SUBDEV           "SUBDEV" /* prefix for sublayered devices underneath public crypt types */
62
63 #define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
64
65 #define MISALIGNED(a, b)        ((a) & ((b) - 1))
66 #define MISALIGNED_4K(a)        MISALIGNED((a), 1 << SHIFT_4K)
67 #define MISALIGNED_512(a)       MISALIGNED((a), 1 << SECTOR_SHIFT)
68 #define NOTPOW2(a)              MISALIGNED((a), (a))
69
70 #ifndef ARRAY_SIZE
71 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
72 #endif
73
74 #define MOVE_REF(x, y) \
75         do { \
76                 typeof (x) *_px = &(x), *_py = &(y); \
77                 *_px = *_py; \
78                 *_py = NULL; \
79         } while (0)
80
81 #ifndef O_CLOEXEC
82 #define O_CLOEXEC 0
83 #endif
84
85 struct crypt_device;
86 struct luks2_reenc_context;
87
88 struct volume_key {
89         int id;
90         size_t keylength;
91         const char *key_description;
92         struct volume_key *next;
93         char key[];
94 };
95
96 struct volume_key *crypt_alloc_volume_key(size_t keylength, const char *key);
97 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, size_t keylength);
98 void crypt_free_volume_key(struct volume_key *vk);
99 int crypt_volume_key_set_description(struct volume_key *key, const char *key_description);
100 void crypt_volume_key_set_id(struct volume_key *vk, int id);
101 int crypt_volume_key_get_id(const struct volume_key *vk);
102 void crypt_volume_key_add_next(struct volume_key **vks, struct volume_key *vk);
103 struct volume_key *crypt_volume_key_next(struct volume_key *vk);
104 struct volume_key *crypt_volume_key_by_id(struct volume_key *vk, int id);
105
106 struct crypt_pbkdf_type *crypt_get_pbkdf(struct crypt_device *cd);
107 int init_pbkdf_type(struct crypt_device *cd,
108                     const struct crypt_pbkdf_type *pbkdf,
109                     const char *dev_type);
110 int verify_pbkdf_params(struct crypt_device *cd,
111                         const struct crypt_pbkdf_type *pbkdf);
112 int crypt_benchmark_pbkdf_internal(struct crypt_device *cd,
113                                    struct crypt_pbkdf_type *pbkdf,
114                                    size_t volume_key_size);
115 const char *crypt_get_cipher_spec(struct crypt_device *cd);
116
117 /* Device backend */
118 struct device;
119 int device_alloc(struct crypt_device *cd, struct device **device, const char *path);
120 int device_alloc_no_check(struct device **device, const char *path);
121 void device_close(struct crypt_device *cd, struct device *device);
122 void device_free(struct crypt_device *cd, struct device *device);
123 const char *device_path(const struct device *device);
124 const char *device_dm_name(const struct device *device);
125 const char *device_block_path(const struct device *device);
126 void device_topology_alignment(struct crypt_device *cd,
127                                struct device *device,
128                                unsigned long *required_alignment, /* bytes */
129                                unsigned long *alignment_offset,   /* bytes */
130                                unsigned long default_alignment);
131 size_t device_block_size(struct crypt_device *cd, struct device *device);
132 int device_read_ahead(struct device *device, uint32_t *read_ahead);
133 int device_size(struct device *device, uint64_t *size);
134 int device_open(struct crypt_device *cd, struct device *device, int flags);
135 int device_open_excl(struct crypt_device *cd, struct device *device, int flags);
136 void device_release_excl(struct crypt_device *cd, struct device *device);
137 void device_disable_direct_io(struct device *device);
138 int device_is_identical(struct device *device1, struct device *device2);
139 int device_is_rotational(struct device *device);
140 size_t device_alignment(struct device *device);
141 int device_direct_io(const struct device *device);
142 int device_fallocate(struct device *device, uint64_t size);
143 void device_sync(struct crypt_device *cd, struct device *device);
144 int device_check_size(struct crypt_device *cd,
145                       struct device *device,
146                       uint64_t req_offset, int falloc);
147
148 int device_open_locked(struct crypt_device *cd, struct device *device, int flags);
149 int device_read_lock(struct crypt_device *cd, struct device *device);
150 int device_write_lock(struct crypt_device *cd, struct device *device);
151 void device_read_unlock(struct crypt_device *cd, struct device *device);
152 void device_write_unlock(struct crypt_device *cd, struct device *device);
153 bool device_is_locked(struct device *device);
154
155 enum devcheck { DEV_OK = 0, DEV_EXCL = 1 };
156 int device_check_access(struct crypt_device *cd,
157                         struct device *device,
158                         enum devcheck device_check);
159 int device_block_adjust(struct crypt_device *cd,
160                         struct device *device,
161                         enum devcheck device_check,
162                         uint64_t device_offset,
163                         uint64_t *size,
164                         uint32_t *flags);
165 size_t size_round_up(size_t size, size_t block);
166
167 int create_or_reload_device(struct crypt_device *cd, const char *name,
168                      const char *type, struct crypt_dm_active_device *dmd);
169
170 int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name,
171                      const char *type, struct crypt_dm_active_device *dmd,
172                      struct crypt_dm_active_device *dmdi);
173
174 /* Receive backend devices from context helpers */
175 struct device *crypt_metadata_device(struct crypt_device *cd);
176 struct device *crypt_data_device(struct crypt_device *cd);
177
178 int crypt_confirm(struct crypt_device *cd, const char *msg);
179
180 char *crypt_lookup_dev(const char *dev_id);
181 int crypt_dev_is_rotational(int major, int minor);
182 int crypt_dev_is_partition(const char *dev_path);
183 char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size);
184 char *crypt_get_base_device(const char *dev_path);
185 uint64_t crypt_dev_partition_offset(const char *dev_path);
186 int lookup_by_disk_id(const char *dm_uuid);
187 int lookup_by_sysfs_uuid_field(const char *dm_uuid, size_t max_len);
188 int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid);
189
190 size_t crypt_getpagesize(void);
191 unsigned crypt_cpusonline(void);
192 uint64_t crypt_getphysmemory_kb(void);
193
194 int init_crypto(struct crypt_device *ctx);
195
196 void logger(struct crypt_device *cd, int level, const char *file, int line, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
197 #define log_dbg(c, x...) logger(c, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
198 #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
199 #define log_verbose(c, x...) logger(c, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
200 #define log_err(c, x...) logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x)
201
202 int crypt_get_debug_level(void);
203
204 int crypt_memlock_inc(struct crypt_device *ctx);
205 int crypt_memlock_dec(struct crypt_device *ctx);
206
207 int crypt_metadata_locking_enabled(void);
208
209 int crypt_random_init(struct crypt_device *ctx);
210 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality);
211 void crypt_random_exit(void);
212 int crypt_random_default_key_rng(void);
213
214 int crypt_plain_hash(struct crypt_device *cd,
215                      const char *hash_name,
216                      char *key, size_t key_size,
217                      const char *passphrase, size_t passphrase_size);
218 int PLAIN_activate(struct crypt_device *cd,
219                      const char *name,
220                      struct volume_key *vk,
221                      uint64_t size,
222                      uint32_t flags);
223
224 void *crypt_get_hdr(struct crypt_device *cd, const char *type);
225 void crypt_set_reenc_context(struct crypt_device *cd, struct luks2_reenc_context *rh);
226 struct luks2_reenc_context *crypt_get_reenc_context(struct crypt_device *cd);
227
228 int onlyLUKS2(struct crypt_device *cd);
229 int onlyLUKS2mask(struct crypt_device *cd, uint32_t mask);
230
231 int crypt_wipe_device(struct crypt_device *cd,
232         struct device *device,
233         crypt_wipe_pattern pattern,
234         uint64_t offset,
235         uint64_t length,
236         size_t wipe_block_size,
237         int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
238         void *usrptr);
239
240 /* Internal integrity helpers */
241 const char *crypt_get_integrity(struct crypt_device *cd);
242 int crypt_get_integrity_key_size(struct crypt_device *cd);
243 int crypt_get_integrity_tag_size(struct crypt_device *cd);
244
245 int crypt_key_in_keyring(struct crypt_device *cd);
246 void crypt_set_key_in_keyring(struct crypt_device *cd, unsigned key_in_keyring);
247 int crypt_volume_key_load_in_keyring(struct crypt_device *cd, struct volume_key *vk);
248 int crypt_use_keyring_for_vk(struct crypt_device *cd);
249 void crypt_drop_keyring_key_by_description(struct crypt_device *cd, const char *key_description, key_type_t ktype);
250 void crypt_drop_keyring_key(struct crypt_device *cd, struct volume_key *vks);
251
252 static inline uint64_t version(uint16_t major, uint16_t minor, uint16_t patch, uint16_t release)
253 {
254         return (uint64_t)release | ((uint64_t)patch << 16) | ((uint64_t)minor << 32) | ((uint64_t)major << 48);
255 }
256
257 int kernel_version(uint64_t *kversion);
258
259 int crypt_serialize_lock(struct crypt_device *cd);
260 void crypt_serialize_unlock(struct crypt_device *cd);
261
262 bool crypt_string_in(const char *str, char **list, size_t list_size);
263 int crypt_strcmp(const char *a, const char *b);
264 int crypt_compare_dm_devices(struct crypt_device *cd,
265                                const struct crypt_dm_active_device *src,
266                                const struct crypt_dm_active_device *tgt);
267 static inline void *crypt_zalloc(size_t size) { return calloc(1, size); }
268
269 #endif /* INTERNAL_H */