961df35354b1e35c9c390f36d119ee74a3a4e621
[platform/upstream/cryptsetup.git] / lib / luks1 / luks.h
1 /*
2  * LUKS - Linux Unified Key Setup
3  *
4  * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #ifndef INCLUDED_CRYPTSETUP_LUKS_LUKS_H
22 #define INCLUDED_CRYPTSETUP_LUKS_LUKS_H
23
24 /*
25  * LUKS partition header
26  */
27
28 #include "libcryptsetup.h"
29
30 #define LUKS_CIPHERNAME_L 32
31 #define LUKS_CIPHERMODE_L 32
32 #define LUKS_HASHSPEC_L 32
33 #define LUKS_DIGESTSIZE 20 // since SHA1
34 #define LUKS_HMACSIZE 32
35 #define LUKS_SALTSIZE 32
36 #define LUKS_NUMKEYS 8
37
38 // Minimal number of iterations
39 #define LUKS_MKD_ITERATIONS_MIN  1000
40 #define LUKS_SLOT_ITERATIONS_MIN 1000
41
42 #define LUKS_KEY_DISABLED_OLD 0
43 #define LUKS_KEY_ENABLED_OLD 0xCAFE
44
45 #define LUKS_KEY_DISABLED 0x0000DEAD
46 #define LUKS_KEY_ENABLED  0x00AC71F3
47
48 #define LUKS_STRIPES 4000
49
50 // partition header starts with magic
51 #define LUKS_MAGIC {'L','U','K','S', 0xba, 0xbe};
52 #define LUKS_MAGIC_L 6
53
54 /* Actually we need only 37, but we don't want struct autoaligning to kick in */
55 #define UUID_STRING_L 40
56
57 /* Offset to keyslot area [in bytes] */
58 #define LUKS_ALIGN_KEYSLOTS 4096
59
60 /* Any integer values are stored in network byte order on disk and must be
61 converted */
62
63 struct volume_key;
64 struct device_backend;
65
66 struct luks_phdr {
67         char            magic[LUKS_MAGIC_L];
68         uint16_t        version;
69         char            cipherName[LUKS_CIPHERNAME_L];
70         char            cipherMode[LUKS_CIPHERMODE_L];
71         char            hashSpec[LUKS_HASHSPEC_L];
72         uint32_t        payloadOffset;
73         uint32_t        keyBytes;
74         char            mkDigest[LUKS_DIGESTSIZE];
75         char            mkDigestSalt[LUKS_SALTSIZE];
76         uint32_t        mkDigestIterations;
77         char            uuid[UUID_STRING_L];
78
79         struct {
80                 uint32_t active;
81
82                 /* parameters used for password processing */
83                 uint32_t passwordIterations;
84                 char     passwordSalt[LUKS_SALTSIZE];
85
86                 /* parameters used for AF store/load */
87                 uint32_t keyMaterialOffset;
88                 uint32_t stripes;
89         } keyblock[LUKS_NUMKEYS];
90
91         /* Align it to 512 sector size */
92         char            _padding[432];
93 };
94
95 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
96                            const struct volume_key *vk);
97
98 int LUKS_generate_phdr(
99         struct luks_phdr *header,
100         const struct volume_key *vk,
101         const char *cipherName,
102         const char *cipherMode,
103         const char *hashSpec,
104         const char *uuid,
105         unsigned int stripes,
106         unsigned int alignPayload,
107         unsigned int alignOffset,
108         uint32_t iteration_time_ms,
109         uint64_t *PBKDF2_per_sec,
110         int detached_metadata_device,
111         struct crypt_device *ctx);
112
113 int LUKS_read_phdr(
114         struct luks_phdr *hdr,
115         int require_luks_device,
116         int repair,
117         struct crypt_device *ctx);
118
119 int LUKS_read_phdr_backup(
120         const char *backup_file,
121         struct luks_phdr *hdr,
122         int require_luks_device,
123         struct crypt_device *ctx);
124
125 int LUKS_hdr_uuid_set(
126         struct luks_phdr *hdr,
127         const char *uuid,
128         struct crypt_device *ctx);
129
130 int LUKS_hdr_backup(
131         const char *backup_file,
132         struct luks_phdr *hdr,
133         struct crypt_device *ctx);
134
135 int LUKS_hdr_restore(
136         const char *backup_file,
137         struct luks_phdr *hdr,
138         struct crypt_device *ctx);
139
140 int LUKS_write_phdr(
141         struct luks_phdr *hdr,
142         struct crypt_device *ctx);
143
144 int LUKS_set_key(
145         unsigned int keyIndex,
146         const char *password,
147         size_t passwordLen,
148         struct luks_phdr *hdr,
149         struct volume_key *vk,
150         uint32_t iteration_time_ms,
151         uint64_t *PBKDF2_per_sec,
152         struct crypt_device *ctx);
153
154 int LUKS_open_key_with_hdr(
155         int keyIndex,
156         const char *password,
157         size_t passwordLen,
158         struct luks_phdr *hdr,
159         struct volume_key **vk,
160         struct crypt_device *ctx);
161
162 int LUKS_del_key(
163         unsigned int keyIndex,
164         struct luks_phdr *hdr,
165         struct crypt_device *ctx);
166
167 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot);
168 int LUKS_keyslot_find_empty(struct luks_phdr *hdr);
169 int LUKS_keyslot_active_count(struct luks_phdr *hdr);
170 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable);
171 int LUKS_keyslot_area(struct luks_phdr *hdr,
172         int keyslot,
173         uint64_t *offset,
174         uint64_t *length);
175
176 int LUKS_encrypt_to_storage(
177         char *src, size_t srcLength,
178         const char *cipher,
179         const char *cipher_mode,
180         struct volume_key *vk,
181         unsigned int sector,
182         struct crypt_device *ctx);
183
184 int LUKS_decrypt_from_storage(
185         char *dst, size_t dstLength,
186         const char *cipher,
187         const char *cipher_mode,
188         struct volume_key *vk,
189         unsigned int sector,
190         struct crypt_device *ctx);
191
192 int LUKS1_activate(struct crypt_device *cd,
193                    const char *name,
194                    struct volume_key *vk,
195                    uint32_t flags);
196
197 #endif