New device access backend.
[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 #define LUKS_PHDR_SIZE (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
55
56 /* Actually we need only 37, but we don't want struct autoaligning to kick in */
57 #define UUID_STRING_L 40
58
59 /* Offset to keyslot area [in bytes] */
60 #define LUKS_ALIGN_KEYSLOTS 4096
61
62 /* Any integer values are stored in network byte order on disk and must be
63 converted */
64
65 struct volume_key;
66 struct device_backend;
67
68 struct luks_phdr {
69         char            magic[LUKS_MAGIC_L];
70         uint16_t        version;
71         char            cipherName[LUKS_CIPHERNAME_L];
72         char            cipherMode[LUKS_CIPHERMODE_L];
73         char            hashSpec[LUKS_HASHSPEC_L];
74         uint32_t        payloadOffset;
75         uint32_t        keyBytes;
76         char            mkDigest[LUKS_DIGESTSIZE];
77         char            mkDigestSalt[LUKS_SALTSIZE];
78         uint32_t        mkDigestIterations;
79         char            uuid[UUID_STRING_L];
80
81         struct {
82                 uint32_t active;
83
84                 /* parameters used for password processing */
85                 uint32_t passwordIterations;
86                 char     passwordSalt[LUKS_SALTSIZE];
87
88                 /* parameters used for AF store/load */
89                 uint32_t keyMaterialOffset;
90                 uint32_t stripes;
91         } keyblock[LUKS_NUMKEYS];
92
93         /* Align it to 512 sector size */
94         char            _padding[432];
95 };
96
97 int LUKS_verify_volume_key(const struct luks_phdr *hdr,
98                            const struct volume_key *vk);
99
100 int LUKS_generate_phdr(
101         struct luks_phdr *header,
102         const struct volume_key *vk,
103         const char *cipherName,
104         const char *cipherMode,
105         const char *hashSpec,
106         const char *uuid,
107         unsigned int stripes,
108         unsigned int alignPayload,
109         unsigned int alignOffset,
110         uint32_t iteration_time_ms,
111         uint64_t *PBKDF2_per_sec,
112         int detached_metadata_device,
113         struct crypt_device *ctx);
114
115 int LUKS_read_phdr(
116         struct luks_phdr *hdr,
117         int require_luks_device,
118         int repair,
119         struct crypt_device *ctx);
120
121 int LUKS_read_phdr_backup(
122         const char *backup_file,
123         struct luks_phdr *hdr,
124         int require_luks_device,
125         struct crypt_device *ctx);
126
127 int LUKS_hdr_uuid_set(
128         struct luks_phdr *hdr,
129         const char *uuid,
130         struct crypt_device *ctx);
131
132 int LUKS_hdr_backup(
133         const char *backup_file,
134         struct luks_phdr *hdr,
135         struct crypt_device *ctx);
136
137 int LUKS_hdr_restore(
138         const char *backup_file,
139         struct luks_phdr *hdr,
140         struct crypt_device *ctx);
141
142 int LUKS_write_phdr(
143         struct luks_phdr *hdr,
144         struct crypt_device *ctx);
145
146 int LUKS_set_key(
147         unsigned int keyIndex,
148         const char *password,
149         size_t passwordLen,
150         struct luks_phdr *hdr,
151         struct volume_key *vk,
152         uint32_t iteration_time_ms,
153         uint64_t *PBKDF2_per_sec,
154         struct crypt_device *ctx);
155
156 int LUKS_open_key_with_hdr(
157         int keyIndex,
158         const char *password,
159         size_t passwordLen,
160         struct luks_phdr *hdr,
161         struct volume_key **vk,
162         struct crypt_device *ctx);
163
164 int LUKS_del_key(
165         unsigned int keyIndex,
166         struct luks_phdr *hdr,
167         struct crypt_device *ctx);
168
169 crypt_keyslot_info LUKS_keyslot_info(struct luks_phdr *hdr, int keyslot);
170 int LUKS_keyslot_find_empty(struct luks_phdr *hdr);
171 int LUKS_keyslot_active_count(struct luks_phdr *hdr);
172 int LUKS_keyslot_set(struct luks_phdr *hdr, int keyslot, int enable);
173
174 int LUKS_encrypt_to_storage(
175         char *src, size_t srcLength,
176         struct luks_phdr *hdr,
177         struct volume_key *vk,
178         unsigned int sector,
179         struct crypt_device *ctx);
180
181 int LUKS_decrypt_from_storage(
182         char *dst, size_t dstLength,
183         struct luks_phdr *hdr,
184         struct volume_key *vk,
185         unsigned int sector,
186         struct crypt_device *ctx);
187
188 int LUKS1_activate(struct crypt_device *cd,
189                    const char *name,
190                    struct volume_key *vk,
191                    uint32_t flags);
192
193 #endif