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