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