Add --keyfile-offset and --new-keyfile-offset to cryptsetup.
[platform/upstream/cryptsetup.git] / lib / luks1 / luks.h
1 #ifndef INCLUDED_CRYPTSETUP_LUKS_LUKS_H
2 #define INCLUDED_CRYPTSETUP_LUKS_LUKS_H
3
4 /*
5  * LUKS partition header
6  */
7
8 #include "libcryptsetup.h"
9
10 #define LUKS_CIPHERNAME_L 32
11 #define LUKS_CIPHERMODE_L 32
12 #define LUKS_HASHSPEC_L 32
13 #define LUKS_DIGESTSIZE 20 // since SHA1
14 #define LUKS_HMACSIZE 32
15 #define LUKS_SALTSIZE 32
16 #define LUKS_NUMKEYS 8
17
18 // Minimal number of iterations
19 #define LUKS_MKD_ITERATIONS_MIN  1000
20 #define LUKS_SLOT_ITERATIONS_MIN 1000
21
22 #define LUKS_KEY_DISABLED_OLD 0
23 #define LUKS_KEY_ENABLED_OLD 0xCAFE
24
25 #define LUKS_KEY_DISABLED 0x0000DEAD
26 #define LUKS_KEY_ENABLED  0x00AC71F3
27
28 #define LUKS_STRIPES 4000
29
30 // partition header starts with magic
31 #define LUKS_MAGIC {'L','U','K','S', 0xba, 0xbe};
32 #define LUKS_MAGIC_L 6
33
34 #define LUKS_PHDR_SIZE (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
35
36 /* Actually we need only 37, but we don't want struct autoaligning to kick in */
37 #define UUID_STRING_L 40
38
39 /* Offset to keyslot area [in bytes] */
40 #define LUKS_ALIGN_KEYSLOTS 4096
41
42 /* Any integer values are stored in network byte order on disk and must be
43 converted */
44
45 struct volume_key;
46
47 struct luks_phdr {
48         char            magic[LUKS_MAGIC_L];
49         uint16_t        version;
50         char            cipherName[LUKS_CIPHERNAME_L];
51         char            cipherMode[LUKS_CIPHERMODE_L];
52         char            hashSpec[LUKS_HASHSPEC_L];
53         uint32_t        payloadOffset;
54         uint32_t        keyBytes;
55         char            mkDigest[LUKS_DIGESTSIZE];
56         char            mkDigestSalt[LUKS_SALTSIZE];
57         uint32_t        mkDigestIterations;
58         char            uuid[UUID_STRING_L];
59
60         struct {
61                 uint32_t active;
62
63                 /* parameters used for password processing */
64                 uint32_t passwordIterations;
65                 char     passwordSalt[LUKS_SALTSIZE];
66
67                 /* parameters used for AF store/load */
68                 uint32_t keyMaterialOffset;
69                 uint32_t stripes;
70         } keyblock[LUKS_NUMKEYS];
71
72         /* Align it to 512 sector size */
73         char            _padding[432];
74 };
75
76 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
77                            const struct volume_key *vk);
78
79 int LUKS_generate_phdr(
80         struct luks_phdr *header,
81         const struct volume_key *vk,
82         const char *cipherName,
83         const char *cipherMode,
84         const char *hashSpec,
85         const char *uuid,
86         unsigned int stripes,
87         unsigned int alignPayload,
88         unsigned int alignOffset,
89         uint32_t iteration_time_ms,
90         uint64_t *PBKDF2_per_sec,
91         const char *metadata_device,
92         struct crypt_device *ctx);
93
94 int LUKS_read_phdr(
95         const char *device,
96         struct luks_phdr *hdr,
97         int require_luks_device,
98         struct crypt_device *ctx);
99
100 int LUKS_read_phdr_backup(
101         const char *backup_file,
102         const char *device,
103         struct luks_phdr *hdr,
104         int require_luks_device,
105         struct crypt_device *ctx);
106
107 int LUKS_hdr_uuid_set(
108         const char *device,
109         struct luks_phdr *hdr,
110         const char *uuid,
111         struct crypt_device *ctx);
112
113 int LUKS_hdr_backup(
114         const char *backup_file,
115         const char *device,
116         struct luks_phdr *hdr,
117         struct crypt_device *ctx);
118
119 int LUKS_hdr_restore(
120         const char *backup_file,
121         const char *device,
122         struct luks_phdr *hdr,
123         struct crypt_device *ctx);
124
125 int LUKS_write_phdr(
126         const char *device,
127         struct luks_phdr *hdr,
128         struct crypt_device *ctx);
129
130 int LUKS_set_key(
131         const char *device,
132         unsigned int keyIndex,
133         const char *password,
134         size_t passwordLen,
135         struct luks_phdr *hdr,
136         struct volume_key *vk,
137         uint32_t iteration_time_ms,
138         uint64_t *PBKDF2_per_sec,
139         struct crypt_device *ctx);
140
141 int LUKS_open_key_with_hdr(
142         const char *device,
143         int keyIndex,
144         const char *password,
145         size_t passwordLen,
146         struct luks_phdr *hdr,
147         struct volume_key **vk,
148         struct crypt_device *ctx);
149
150 int LUKS_del_key(
151         const char *device,
152         unsigned int keyIndex,
153         struct luks_phdr *hdr,
154         struct crypt_device *ctx);
155
156 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot);
157 int LUKS_keyslot_find_empty(struct luks_phdr *hdr);
158 int LUKS_keyslot_active_count(struct luks_phdr *hdr);
159 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable);
160
161 int LUKS_encrypt_to_storage(
162         char *src, size_t srcLength,
163         struct luks_phdr *hdr,
164         struct volume_key *vk,
165         const char *device,
166         unsigned int sector,
167         struct crypt_device *ctx);
168
169 int LUKS_decrypt_from_storage(
170         char *dst, size_t dstLength,
171         struct luks_phdr *hdr,
172         struct volume_key *vk,
173         const char *device,
174         unsigned int sector,
175         struct crypt_device *ctx);
176
177 int LUKS1_activate(struct crypt_device *cd,
178                    const char *name,
179                    struct volume_key *vk,
180                    uint32_t flags);
181
182 #endif