* Move memory locking and dm initialization to command layer.
[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
12 #include <libintl.h>
13 #include <locale.h>
14 #define _(String) gettext((String))
15
16 #define SECTOR_SHIFT            9
17 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
18 #define DEFAULT_ALIGNMENT       4096
19
20 /* private struct crypt_options flags */
21
22 #define CRYPT_FLAG_FREE_DEVICE  (1 << 24)
23 #define CRYPT_FLAG_FREE_CIPHER  (1 << 25)
24
25 #define CRYPT_FLAG_PRIVATE_MASK ((unsigned int)-1 << 24)
26
27 struct hash_type {
28         char            *name;
29         void            *private;
30         int             (*fn)(void *data, int size, char *key,
31                               int sizep, const char *passphrase);
32 };
33
34 struct hash_backend {
35         const char              *name;
36         struct hash_type *      (*get_hashes)(void);
37         void                    (*free_hashes)(struct hash_type *hashes);
38 };
39
40 struct crypt_device;
41
42 void set_error_va(const char *fmt, va_list va);
43 void set_error(const char *fmt, ...);
44 const char *get_error(void);
45 void *safe_alloc(size_t size);
46 void safe_free(void *data);
47 void *safe_realloc(void *data, size_t size);
48 char *safe_strdup(const char *s);
49
50 struct hash_backend *get_hash_backend(const char *name);
51 void put_hash_backend(struct hash_backend *backend);
52 int hash(const char *backend_name, const char *hash_name,
53          char *result, size_t size,
54          const char *passphrase, size_t sizep);
55
56 void hexprint(char *d, int n);
57
58 /* Device mapper backend */
59 const char *dm_get_dir(void);
60 int dm_init(struct crypt_device *context, int check_kernel);
61 void dm_exit(void);
62 int dm_remove_device(const char *name, int force, uint64_t size);
63 int dm_status_device(const char *name);
64 int dm_query_device(const char *name,
65                     char **device,
66                     uint64_t *size,
67                     uint64_t *skip,
68                     uint64_t *offset,
69                     char **cipher,
70                     int *key_size,
71                     char **key,
72                     int *read_only);
73 int dm_create_device(const char *name, const char *device, const char *cipher, const char *uuid,
74                      uint64_t size, uint64_t skip, uint64_t offset,
75                      size_t key_size, const char *key,
76                      int read_only, int reload);
77
78 int sector_size_for_device(const char *device);
79 ssize_t write_blockwise(int fd, const void *buf, size_t count);
80 ssize_t read_blockwise(int fd, void *_buf, size_t count);
81 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset);
82
83
84 int get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
85             const char *key_file, int passphrase_fd, int timeout, int how2verify);
86
87 int memlock_inc(struct crypt_device *ctx);
88 int memlock_dec(struct crypt_device *ctx);
89
90 #endif /* INTERNAL_H */