Generalise volume key struct.
[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
15 #define SECTOR_SHIFT            9
16 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
17 #define DEFAULT_DISK_ALIGNMENT  1048576 /* 1MiB */
18 #define DEFAULT_MEM_ALIGNMENT   4096
19
20 #define MAX_TTY_PASSWORD_LEN    512
21
22 /* private struct crypt_options flags */
23
24 #define CRYPT_FLAG_FREE_DEVICE  (1 << 24)
25 #define CRYPT_FLAG_FREE_CIPHER  (1 << 25)
26
27 #define CRYPT_FLAG_PRIVATE_MASK ((unsigned int)-1 << 24)
28
29 #define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
30
31 struct hash_type {
32         char            *name;
33         void            *private;
34         int             (*fn)(void *data, int size, char *key,
35                               int sizep, const char *passphrase);
36 };
37
38 struct hash_backend {
39         const char              *name;
40         struct hash_type *      (*get_hashes)(void);
41         void                    (*free_hashes)(struct hash_type *hashes);
42 };
43
44 struct device_infos {
45         uint64_t        size;
46         int             readonly;
47 };
48
49 struct volume_key {
50         size_t keylength;
51         char key[];
52 };
53 struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key);
54 struct volume_key *crypt_generate_volume_key(unsigned keylength);
55 void crypt_free_volume_key(struct volume_key *mk);
56
57 struct crypt_device;
58 int crypt_confirm(struct crypt_device *cd, const char *msg);
59
60 void set_error_va(const char *fmt, va_list va);
61 void set_error(const char *fmt, ...);
62 const char *get_error(void);
63 void *safe_alloc(size_t size);
64 void safe_free(void *data);
65 void *safe_realloc(void *data, size_t size);
66 char *safe_strdup(const char *s);
67 void set_debug_level(int level);
68
69 int init_crypto(void);
70 struct hash_backend *get_hash_backend(const char *name);
71 void put_hash_backend(struct hash_backend *backend);
72 int hash(const char *backend_name, const char *hash_name,
73          char *result, size_t size,
74          const char *passphrase, size_t sizep);
75
76 void hexprint(char *d, int n);
77
78 /* Device mapper backend */
79 const char *dm_get_dir(void);
80 int dm_init(struct crypt_device *context, int check_kernel);
81 void dm_exit(void);
82 int dm_remove_device(const char *name, int force, uint64_t size);
83 int dm_status_device(const char *name);
84 int dm_query_device(const char *name,
85                     char **device,
86                     uint64_t *size,
87                     uint64_t *skip,
88                     uint64_t *offset,
89                     char **cipher,
90                     int *key_size,
91                     char **key,
92                     int *read_only,
93                     int *suspended,
94                     char **uuid);
95 int dm_create_device(const char *name, const char *device, const char *cipher,
96                      const char *type, const char *uuid,
97                      uint64_t size, uint64_t skip, uint64_t offset,
98                      size_t key_size, const char *key,
99                      int read_only, int reload);
100 int dm_suspend_and_wipe_key(const char *name);
101 int dm_resume_and_reinstate_key(const char *name,
102                                 size_t key_size,
103                                 const char *key);
104
105 int sector_size_for_device(const char *device);
106 ssize_t write_blockwise(int fd, const void *buf, size_t count);
107 ssize_t read_blockwise(int fd, void *_buf, size_t count);
108 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset);
109 int device_ready(struct crypt_device *cd, const char *device, int mode);
110 int get_device_infos(const char *device, struct device_infos *infos, struct crypt_device *cd);
111 int wipe_device_header(const char *device, int sectors);
112
113 void get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
114              const char *key_file, int timeout, int how2verify,
115              struct crypt_device *cd);
116
117 int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode);
118
119 void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...);
120 #define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
121 #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
122 #define log_verbose(c, x...) logger(c, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
123 #define log_err(c, x...) do { \
124         logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x); \
125         set_error(x); } while(0)
126
127 int crypt_get_debug_level(void);
128 void debug_processes_using_device(const char *name);
129
130 int crypt_memlock_inc(struct crypt_device *ctx);
131 int crypt_memlock_dec(struct crypt_device *ctx);
132
133 void get_topology_alignment(const char *device,
134                             unsigned long *required_alignment, /* bytes */
135                             unsigned long *alignment_offset,   /* bytes */
136                             unsigned long default_alignment);
137
138 #endif /* INTERNAL_H */