Makefile: Add security compiling option (RELRO, SC, and FORTIFY)
[platform/upstream/cryptsetup.git] / tests / api_test.h
1 /*
2  * cryptsetup library API check functions
3  *
4  * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2009-2023 Milan Broz
6  * Copyright (C) 2016-2023 Ondrej Kozina
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef API_TEST_H
24 #define API_TEST_H
25
26 #include <stdio.h>
27 #include <stdint.h>
28
29 extern char *THE_LOOP_DEV;
30 extern int _debug;
31 extern int global_lines;
32 extern int _quit;
33 extern int _verbose;
34 extern uint64_t t_dev_offset;
35
36 int t_device_size(const char *device, uint64_t *size);
37 int t_dm_check_versions(void);
38 int t_dm_crypt_keyring_support(void);
39 int t_dm_crypt_cpu_switch_support(void);
40 int t_dm_crypt_discard_support(void);
41 int t_dm_integrity_resize_support(void);
42 int t_dm_integrity_recalculate_support(void);
43 int t_dm_capi_string_supported(void);
44 int t_set_readahead(const char *device, unsigned value);
45
46 int fips_mode(void);
47
48 int create_dmdevice_over_loop(const char *dm_name, const uint64_t size);
49
50 int get_key_dm(const char *name, char *buffer, unsigned int buffer_size);
51
52 int prepare_keyfile(const char *name, const char *passphrase, int size);
53
54 int crypt_decode_key(char *key, const char *hex, unsigned int size);
55
56 void global_log_callback(int level, const char *msg, void *usrptr);
57
58 void reset_log(void);
59
60 int _system(const char *command, int warn);
61
62 void register_cleanup(void (*cleanup)(void));
63
64 void check_ok(int status, int line, const char *func);
65 void check_ok_return(int status, int line, const char *func);
66 void check_ko(int status, int line, const char *func);
67 void check_equal(int line, const char *func, int64_t x, int64_t y);
68 void check_ge_equal(int line, const char *func, int64_t x, int64_t y);
69 void check_null(int line, const char *func, const void *x);
70 void check_notnull(int line, const char *func, const void *x);
71 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt);
72
73 /* crypt_device context must be "cd" to parse error properly here */
74 #define OK_(x)          do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
75                              check_ok((x), __LINE__, __FUNCTION__); \
76                         } while(0)
77 #define NOTFAIL_(x, y)  do { xlog("(notfail)", #x, __FUNCTION__, __LINE__, y); \
78                              check_ok_return((x), __LINE__, __FUNCTION__); \
79                         } while(0)
80 #define FAIL_(x, y)     do { xlog("(fail)   ", #x, __FUNCTION__, __LINE__, y); \
81                              check_ko((x), __LINE__, __FUNCTION__); \
82                         } while(0)
83 #define EQ_(x, y)       do { int64_t _x = (x), _y = (y); \
84                              xlog("(equal)  ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
85                              if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
86                         } while(0)
87 #define GE_(x, y)       do { int64_t _x = (x), _y = (y); \
88                              xlog("(g_equal)", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
89                              if (_x < _y) check_ge_equal(__LINE__, __FUNCTION__, _x, _y); \
90                         } while(0)
91 #define NULL_(x)        do { xlog("(null)   ", #x, __FUNCTION__, __LINE__, NULL); \
92                              check_null(__LINE__, __FUNCTION__, (x)); \
93                         } while(0)
94 #define NOTNULL_(x)     do { xlog("(notnull)", #x, __FUNCTION__, __LINE__, NULL); \
95                              check_notnull(__LINE__, __FUNCTION__, (x)); \
96                         } while(0)
97 #define RUN_(x, y)      do { reset_log(); \
98                              printf("%s: %s\n", #x, (y)); x(); \
99                         } while (0)
100
101 #define CRYPT_FREE(x) do { crypt_free(x); x = NULL; } while (0)
102
103 /* to silent clang -Wcast-align when working with byte arrays */
104 #define VOIDP_CAST(x) (x)(void*)
105
106 #define DMDIR "/dev/mapper/"
107
108 #define TST_SECTOR_SHIFT 9L
109 #define TST_SECTOR_SIZE 512
110 #define TST_LOOP_FILE_SIZE (((1 << 20) * 100) >> TST_SECTOR_SHIFT)
111 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
112 #define DIV_ROUND_UP_MODULO(n,d) (DIV_ROUND_UP(n,d)*(d))
113
114 /* Device mapper backend - kernel support flags */
115 #define T_DM_KEY_WIPE_SUPPORTED (1 << 0)        /* key wipe message */
116 #define T_DM_LMK_SUPPORTED      (1 << 1)        /* lmk mode */
117 #define T_DM_SECURE_SUPPORTED   (1 << 2)        /* wipe (secure) buffer flag */
118 #define T_DM_PLAIN64_SUPPORTED  (1 << 3)        /* plain64 IV */
119 #define T_DM_DISCARDS_SUPPORTED (1 << 4)        /* discards/TRIM option is supported */
120 #define T_DM_VERITY_SUPPORTED   (1 << 5)        /* dm-verity target supported */
121 #define T_DM_TCW_SUPPORTED      (1 << 6)        /* tcw (TCRYPT CBC with whitening) */
122 #define T_DM_SAME_CPU_CRYPT_SUPPORTED (1 << 7) /* same_cpu_crypt */
123 #define T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED (1 << 8) /* submit_from_crypt_cpus */
124 #define T_DM_VERITY_ON_CORRUPTION_SUPPORTED (1 << 9) /* ignore/restart_on_corruption, ignore_zero_block */
125 #define T_DM_VERITY_FEC_SUPPORTED (1 << 10) /* Forward Error Correction (FEC) */
126 #define T_DM_KERNEL_KEYRING_SUPPORTED (1 << 11) /* dm-crypt allows loading kernel keyring keys */
127 #define T_DM_INTEGRITY_SUPPORTED (1 << 12) /* dm-integrity target supported */
128 #define T_DM_SECTOR_SIZE_SUPPORTED (1 << 13) /* support for sector size setting in dm-crypt/dm-integrity */
129 #define T_DM_CAPI_STRING_SUPPORTED (1 << 14) /* support for cryptoapi format cipher definition */
130 #define T_DM_DEFERRED_SUPPORTED (1 << 15) /* deferred removal of device */
131 #define T_DM_INTEGRITY_RECALC_SUPPORTED (1 << 16) /* dm-integrity automatic recalculation supported */
132 #define T_DM_INTEGRITY_BITMAP_SUPPORTED (1 << 17) /* dm-integrity bitmap mode supported */
133 #define T_DM_GET_TARGET_VERSION_SUPPORTED (1 << 18) /* dm DM_GET_TARGET version ioctl supported */
134 #define T_DM_INTEGRITY_FIX_PADDING_SUPPORTED (1 << 19) /* supports the parameter fix_padding that fixes a bug that caused excessive padding */
135 #define T_DM_BITLK_EBOIV_SUPPORTED (1 << 20) /* EBOIV for BITLK supported */
136 #define T_DM_BITLK_ELEPHANT_SUPPORTED (1 << 21) /* Elephant diffuser for BITLK supported */
137 #define T_DM_VERITY_SIGNATURE_SUPPORTED (1 << 22) /* Verity option root_hash_sig_key_desc supported */
138 #define T_DM_INTEGRITY_DISCARDS_SUPPORTED (1 << 23) /* dm-integrity discards/TRIM option is supported */
139 #define T_DM_INTEGRITY_RESIZE_SUPPORTED (1 << 23) /* dm-integrity resize of the integrity device supported (introduced in the same version as discards)*/
140 #define T_DM_VERITY_PANIC_CORRUPTION_SUPPORTED (1 << 24) /* dm-verity panic on corruption  */
141 #define T_DM_CRYPT_NO_WORKQUEUE_SUPPORTED (1 << 25) /* dm-crypt suppot for bypassing workqueues  */
142 #define T_DM_INTEGRITY_FIX_HMAC_SUPPORTED (1 << 26) /* hmac covers also superblock */
143 #define T_DM_INTEGRITY_RESET_RECALC_SUPPORTED (1 << 27) /* dm-integrity automatic recalculation supported */
144 #define T_DM_VERITY_TASKLETS_SUPPORTED (1 << 28) /* dm-verity tasklets supported */
145
146 /* loop helpers */
147 int loop_device(const char *loop);
148 int loop_attach(char **loop, const char *file, int offset,
149                       int autoclear, int *readonly);
150 int loop_detach(const char *loop);
151
152 int t_device_size_by_devno(dev_t devno, uint64_t *retval);
153 int t_get_devno(const char *dev, dev_t *devno);
154
155 typedef enum { ERR_RD = 0, ERR_WR, ERR_RW, ERR_REMOVE } error_io_info;
156
157 int dmdevice_error_io(const char *dm_name,
158         const char *dm_device,
159         const char *error_device,
160         uint64_t data_offset,
161         uint64_t offset,
162         uint64_t length,
163         error_io_info ei);
164
165 #endif