Add crypt_last_error() API call (using crypt context).
[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 #include "utils_loop.h"
16 #include "utils_dm.h"
17
18 /* to silent gcc -Wcast-qual for const cast */
19 #define CONST_CAST(x) (x)(uintptr_t)
20
21 #define SECTOR_SHIFT            9
22 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
23 #define DEFAULT_DISK_ALIGNMENT  1048576 /* 1MiB */
24 #define DEFAULT_MEM_ALIGNMENT   4096
25 #define MAX_ERROR_LENGTH        512
26
27 #define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
28
29 struct crypt_device;
30
31 struct volume_key {
32         size_t keylength;
33         char key[];
34 };
35
36 struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key);
37 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, unsigned keylength);
38 void crypt_free_volume_key(struct volume_key *vk);
39
40 int crypt_confirm(struct crypt_device *cd, const char *msg);
41
42 char *crypt_lookup_dev(const char *dev_id);
43 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size);
44 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
45
46 int sector_size_for_device(const char *device);
47 int device_read_ahead(const char *dev, uint32_t *read_ahead);
48 ssize_t write_blockwise(int fd, void *buf, size_t count);
49 ssize_t read_blockwise(int fd, void *_buf, size_t count);
50 ssize_t write_lseek_blockwise(int fd, char *buf, size_t count, off_t offset);
51 int device_ready(struct crypt_device *cd, const char *device, int mode);
52 int device_size(const char *device, uint64_t *size);
53
54 enum devcheck { DEV_OK = 0, DEV_EXCL = 1, DEV_SHARED = 2 };
55 int device_check_and_adjust(struct crypt_device *cd,
56                             const char *device,
57                             enum devcheck device_check,
58                             uint64_t *size,
59                             uint64_t *offset,
60                             uint32_t *flags);
61
62 void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...);
63 #define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
64 #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
65 #define log_verbose(c, x...) logger(c, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
66 #define log_err(c, x...) logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x)
67
68 int crypt_get_debug_level(void);
69 void debug_processes_using_device(const char *name);
70
71 int crypt_memlock_inc(struct crypt_device *ctx);
72 int crypt_memlock_dec(struct crypt_device *ctx);
73
74 void get_topology_alignment(const char *device,
75                             unsigned long *required_alignment, /* bytes */
76                             unsigned long *alignment_offset,   /* bytes */
77                             unsigned long default_alignment);
78
79 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1 };
80 int crypt_random_init(struct crypt_device *ctx);
81 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality);
82 void crypt_random_exit(void);
83 int crypt_random_default_key_rng(void);
84
85 int crypt_plain_hash(struct crypt_device *ctx,
86                      const char *hash_name,
87                      char *key, size_t key_size,
88                      const char *passphrase, size_t passphrase_size);
89 int PLAIN_activate(struct crypt_device *cd,
90                      const char *name,
91                      struct volume_key *vk,
92                      uint64_t size,
93                      uint32_t flags);
94
95 /**
96  * Different methods used to erase sensitive data concerning
97  * either encrypted payload area or master key inside keyslot
98  * area
99  */
100 typedef enum {
101         CRYPT_WIPE_ZERO, /**< overwrite area using zero blocks */
102         CRYPT_WIPE_DISK, /**< erase disk (using Gutmann method if it is rotational disk)*/
103         CRYPT_WIPE_SSD, /**< erase solid state disk (random write) */
104         CRYPT_WIPE_RANDOM /**< overwrite area using some up to now unspecified
105                             * random algorithm */
106 } crypt_wipe_type;
107
108 int crypt_wipe(const char *device,
109                uint64_t offset,
110                uint64_t sectors,
111                crypt_wipe_type type,
112                int flags);
113
114 #endif /* INTERNAL_H */