Better detects dm-ioctl secure flag using version before use.
[platform/upstream/cryptsetup.git] / lib / internal.h
1 #ifndef INTERNAL_H
2 #define INTERNAL_H
3
4 #ifdef HAVE_CONFIG_H
5 #       include "config.h"
6 #endif
7
8 #include <stdint.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include "nls.h"
14 #include "utils_crypt.h"
15
16 #define SECTOR_SHIFT            9
17 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
18 #define DEFAULT_DISK_ALIGNMENT  1048576 /* 1MiB */
19 #define DEFAULT_MEM_ALIGNMENT   4096
20
21 /* private struct crypt_options flags */
22
23 #define CRYPT_FLAG_FREE_DEVICE  (1 << 24)
24 #define CRYPT_FLAG_FREE_CIPHER  (1 << 25)
25
26 #define CRYPT_FLAG_PRIVATE_MASK ((unsigned int)-1 << 24)
27
28 #define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
29
30 struct crypt_device;
31
32 struct hash_type {
33         char            *name;
34         void            *private;
35         int             (*fn)(void *data, int size, char *key,
36                               int sizep, const char *passphrase);
37 };
38
39 struct hash_backend {
40         const char              *name;
41         struct hash_type *      (*get_hashes)(void);
42         void                    (*free_hashes)(struct hash_type *hashes);
43 };
44
45 struct volume_key {
46         size_t keylength;
47         char key[];
48 };
49
50 struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key);
51 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, unsigned keylength);
52 void crypt_free_volume_key(struct volume_key *vk);
53
54 int crypt_confirm(struct crypt_device *cd, const char *msg);
55
56 void set_error_va(const char *fmt, va_list va);
57 void set_error(const char *fmt, ...);
58 const char *get_error(void);
59
60 /* Device mapper backend - kernel support flags */
61 #define DM_KEY_WIPE_SUPPORTED (1 << 0)  /* key wipe message */
62 #define DM_LMK_SUPPORTED      (1 << 1)  /* lmk mode */
63 #define DM_SECURE_SUPPORTED   (1 << 2)  /* wipe (secure) buffer flag */
64 uint32_t dm_flags(void);
65
66 const char *dm_get_dir(void);
67 int dm_init(struct crypt_device *context, int check_kernel);
68 void dm_exit(void);
69 int dm_remove_device(const char *name, int force, uint64_t size);
70 int dm_status_device(const char *name);
71 int dm_query_device(const char *name,
72                     char **device,
73                     uint64_t *size,
74                     uint64_t *skip,
75                     uint64_t *offset,
76                     char **cipher,
77                     int *key_size,
78                     char **key,
79                     int *read_only,
80                     int *suspended,
81                     char **uuid);
82 int dm_create_device(const char *name, const char *device, const char *cipher,
83                      const char *type, const char *uuid,
84                      uint64_t size, uint64_t skip, uint64_t offset,
85                      size_t key_size, const char *key,
86                      int read_only, int reload);
87 int dm_suspend_and_wipe_key(const char *name);
88 int dm_resume_and_reinstate_key(const char *name,
89                                 size_t key_size,
90                                 const char *key);
91
92 int sector_size_for_device(const char *device);
93 ssize_t write_blockwise(int fd, const void *buf, size_t count);
94 ssize_t read_blockwise(int fd, void *_buf, size_t count);
95 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset);
96 int device_ready(struct crypt_device *cd, const char *device, int mode);
97 int get_device_infos(const char *device,
98                      int open_exclusive,
99                      int *readonly,
100                      uint64_t *size);
101 int device_check_and_adjust(struct crypt_device *cd,
102                             const char *device,
103                             int open_exclusive,
104                             uint64_t *size,
105                             uint64_t *offset,
106                             int *read_only);
107 int wipe_device_header(const char *device, int sectors);
108
109 void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...);
110 #define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
111 #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
112 #define log_verbose(c, x...) logger(c, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
113 #define log_err(c, x...) do { \
114         logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x); \
115         set_error(x); } while(0)
116
117 int crypt_get_debug_level(void);
118 void debug_processes_using_device(const char *name);
119
120 int crypt_memlock_inc(struct crypt_device *ctx);
121 int crypt_memlock_dec(struct crypt_device *ctx);
122
123 void get_topology_alignment(const char *device,
124                             unsigned long *required_alignment, /* bytes */
125                             unsigned long *alignment_offset,   /* bytes */
126                             unsigned long default_alignment);
127
128 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1 };
129 int crypt_random_init(struct crypt_device *ctx);
130 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality);
131 void crypt_random_exit(void);
132 int crypt_random_default_key_rng(void);
133
134 int crypt_plain_hash(struct crypt_device *ctx, const char *hash_name,
135                      char *result, size_t size,
136                      const char *passphrase, size_t sizep);
137
138 #endif /* INTERNAL_H */