d1d9b00df1f711c74b8b056185d07f51db067186
[platform/upstream/cryptsetup.git] / tests / api_test.h
1 /*
2  * cryptsetup library API check functions
3  *
4  * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2009-2020 Milan Broz
6  * Copyright (C) 2016-2020 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
42 int fips_mode(void);
43
44 int create_dmdevice_over_loop(const char *dm_name, const uint64_t size);
45
46 int get_key_dm(const char *name, char *buffer, unsigned int buffer_size);
47
48 int prepare_keyfile(const char *name, const char *passphrase, int size);
49
50 int crypt_decode_key(char *key, const char *hex, unsigned int size);
51
52 void global_log_callback(int level, const char *msg, void *usrptr);
53
54 void reset_log(void);
55
56 int _system(const char *command, int warn);
57
58 void register_cleanup(void (*cleanup)(void));
59
60 void check_ok(int status, int line, const char *func);
61 void check_ok_return(int status, int line, const char *func);
62 void check_ko(int status, int line, const char *func);
63 void check_equal(int line, const char *func, int64_t x, int64_t y);
64 void check_null(int line, const char *func, const void *x);
65 void check_notnull(int line, const char *func, const void *x);
66 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt);
67
68 /* crypt_device context must be "cd" to parse error properly here */
69 #define OK_(x)          do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
70                              check_ok((x), __LINE__, __FUNCTION__); \
71                         } while(0)
72 #define NOTFAIL_(x, y)  do { xlog("(notfail)", #x, __FUNCTION__, __LINE__, y); \
73                              check_ok_return((x), __LINE__, __FUNCTION__); \
74                         } while(0)
75 #define FAIL_(x, y)     do { xlog("(fail)   ", #x, __FUNCTION__, __LINE__, y); \
76                              check_ko((x), __LINE__, __FUNCTION__); \
77                         } while(0)
78 #define EQ_(x, y)       do { int64_t _x = (x), _y = (y); \
79                              xlog("(equal)  ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
80                              if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
81                         } while(0)
82 #define NULL_(x)        do { xlog("(null)   ", #x, __FUNCTION__, __LINE__, NULL); \
83                              check_null(__LINE__, __FUNCTION__, (x)); \
84                         } while(0)
85 #define NOTNULL_(x)     do { xlog("(notnull)", #x, __FUNCTION__, __LINE__, NULL); \
86                              check_notnull(__LINE__, __FUNCTION__, (x)); \
87                         } while(0)
88 #define RUN_(x, y)      do { reset_log(); \
89                              printf("%s: %s\n", #x, (y)); x(); \
90                         } while (0)
91
92 #define CRYPT_FREE(x) do { crypt_free(x); x = NULL; } while (0)
93
94 #define SECTOR_SHIFT 9L
95 #define SECTOR_SIZE 512
96 #define TST_LOOP_FILE_SIZE (((1<<20)*100)>>SECTOR_SHIFT)
97 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
98 #define DIV_ROUND_UP_MODULO(n,d) (DIV_ROUND_UP(n,d)*(d))
99
100 /* Device mapper backend - kernel support flags */
101 #define T_DM_KEY_WIPE_SUPPORTED (1 << 0)        /* key wipe message */
102 #define T_DM_LMK_SUPPORTED      (1 << 1)        /* lmk mode */
103 #define T_DM_SECURE_SUPPORTED   (1 << 2)        /* wipe (secure) buffer flag */
104 #define T_DM_PLAIN64_SUPPORTED  (1 << 3)        /* plain64 IV */
105 #define T_DM_DISCARDS_SUPPORTED (1 << 4)        /* discards/TRIM option is supported */
106 #define T_DM_VERITY_SUPPORTED   (1 << 5)        /* dm-verity target supported */
107 #define T_DM_TCW_SUPPORTED      (1 << 6)        /* tcw (TCRYPT CBC with whitening) */
108 #define T_DM_SAME_CPU_CRYPT_SUPPORTED (1 << 7) /* same_cpu_crypt */
109 #define T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED (1 << 8) /* submit_from_crypt_cpus */
110 #define T_DM_VERITY_ON_CORRUPTION_SUPPORTED (1 << 9) /* ignore/restart_on_corruption, ignore_zero_block */
111 #define T_DM_VERITY_FEC_SUPPORTED (1 << 10) /* Forward Error Correction (FEC) */
112 #define T_DM_KERNEL_KEYRING_SUPPORTED (1 << 11) /* dm-crypt allows loading kernel keyring keys */
113 #define T_DM_INTEGRITY_SUPPORTED (1 << 12) /* dm-integrity target supported */
114 //FIXME add T_DM_SECTOR_SIZE once we have version
115
116 /* loop helpers */
117 int loop_device(const char *loop);
118 int loop_attach(char **loop, const char *file, int offset,
119                       int autoclear, int *readonly);
120 int loop_detach(const char *loop);
121
122 #endif