Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / luks2 / luks2_json_format.c
1 /*
2  * LUKS - Linux Unified Key Setup v2, LUKS2 header format code
3  *
4  * Copyright (C) 2015-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2015-2020 Milan Broz
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 #include "luks2_internal.h"
23 #include <uuid/uuid.h>
24 #include <assert.h>
25
26 struct area {
27         uint64_t offset;
28         uint64_t length;
29 };
30
31 static size_t get_area_size(size_t keylength)
32 {
33         //FIXME: calculate this properly, for now it is AF_split_sectors
34         return size_round_up(keylength * 4000, 4096);
35 }
36
37 static size_t get_min_offset(struct luks2_hdr *hdr)
38 {
39         return 2 * hdr->hdr_size;
40 }
41
42 static size_t get_max_offset(struct luks2_hdr *hdr)
43 {
44         return LUKS2_hdr_and_areas_size(hdr->jobj);
45 }
46
47 int LUKS2_find_area_max_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
48                         uint64_t *area_offset, uint64_t *area_length)
49 {
50         struct area areas[LUKS2_KEYSLOTS_MAX], sorted_areas[LUKS2_KEYSLOTS_MAX+1] = {};
51         int i, j, k, area_i;
52         size_t valid_offset, offset, length;
53
54         /* fill area offset + length table */
55         for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
56                 if (!LUKS2_keyslot_area(hdr, i, &areas[i].offset, &areas[i].length))
57                         continue;
58                 areas[i].length = 0;
59                 areas[i].offset = 0;
60         }
61
62         /* sort table */
63         k = 0; /* index in sorted table */
64         for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
65                 offset = get_max_offset(hdr) ?: UINT64_MAX;
66                 area_i = -1;
67                 /* search for the smallest offset in table */
68                 for (j = 0; j < LUKS2_KEYSLOTS_MAX; j++)
69                         if (areas[j].offset && areas[j].offset <= offset) {
70                                 area_i = j;
71                                 offset = areas[j].offset;
72                         }
73
74                 if (area_i >= 0) {
75                         sorted_areas[k].length = areas[area_i].length;
76                         sorted_areas[k].offset = areas[area_i].offset;
77                         areas[area_i].length = 0;
78                         areas[area_i].offset = 0;
79                         k++;
80                 }
81         }
82
83         sorted_areas[LUKS2_KEYSLOTS_MAX].offset = get_max_offset(hdr);
84         sorted_areas[LUKS2_KEYSLOTS_MAX].length = 1;
85
86         /* search for the gap we can use */
87         length = valid_offset = 0;
88         offset = get_min_offset(hdr);
89         for (i = 0; i < LUKS2_KEYSLOTS_MAX+1; i++) {
90                 /* skip empty */
91                 if (sorted_areas[i].offset == 0 || sorted_areas[i].length == 0)
92                         continue;
93
94                 /* found bigger gap than the last one */
95                 if ((offset < sorted_areas[i].offset) && (sorted_areas[i].offset - offset) > length) {
96                         length = sorted_areas[i].offset - offset;
97                         valid_offset = offset;
98                 }
99
100                 /* move beyond allocated area */
101                 offset = sorted_areas[i].offset + sorted_areas[i].length;
102         }
103
104         /* this search 'algorithm' does not work with unaligned areas */
105         assert(length == size_round_up(length, 4096));
106         assert(valid_offset == size_round_up(valid_offset, 4096));
107
108         if (!length) {
109                 log_dbg(cd, "Not enough space in header keyslot area.");
110                 return -EINVAL;
111         }
112
113         log_dbg(cd, "Found largest free area %zu -> %zu", valid_offset, length + valid_offset);
114
115         *area_offset = valid_offset;
116         *area_length = length;
117
118         return 0;
119 }
120
121 int LUKS2_find_area_gap(struct crypt_device *cd, struct luks2_hdr *hdr,
122                         size_t keylength, uint64_t *area_offset, uint64_t *area_length)
123 {
124         struct area areas[LUKS2_KEYSLOTS_MAX], sorted_areas[LUKS2_KEYSLOTS_MAX] = {};
125         int i, j, k, area_i;
126         size_t offset, length;
127
128         /* fill area offset + length table */
129         for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
130                 if (!LUKS2_keyslot_area(hdr, i, &areas[i].offset, &areas[i].length))
131                         continue;
132                 areas[i].length = 0;
133                 areas[i].offset = 0;
134         }
135
136         /* sort table */
137         k = 0; /* index in sorted table */
138         for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
139                 offset = get_max_offset(hdr) ?: UINT64_MAX;
140                 area_i = -1;
141                 /* search for the smallest offset in table */
142                 for (j = 0; j < LUKS2_KEYSLOTS_MAX; j++)
143                         if (areas[j].offset && areas[j].offset <= offset) {
144                                 area_i = j;
145                                 offset = areas[j].offset;
146                         }
147
148                 if (area_i >= 0) {
149                         sorted_areas[k].length = areas[area_i].length;
150                         sorted_areas[k].offset = areas[area_i].offset;
151                         areas[area_i].length = 0;
152                         areas[area_i].offset = 0;
153                         k++;
154                 }
155         }
156
157         /* search for the gap we can use */
158         offset = get_min_offset(hdr);
159         length = get_area_size(keylength);
160         for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
161                 /* skip empty */
162                 if (sorted_areas[i].offset == 0 || sorted_areas[i].length == 0)
163                         continue;
164
165                 /* enough space before the used area */
166                 if ((offset < sorted_areas[i].offset) && ((offset + length) <= sorted_areas[i].offset))
167                         break;
168
169                 /* both offset and length are already aligned to 4096 bytes */
170                 offset = sorted_areas[i].offset + sorted_areas[i].length;
171         }
172
173         if ((offset + length) > get_max_offset(hdr)) {
174                 log_dbg(cd, "Not enough space in header keyslot area.");
175                 return -EINVAL;
176         }
177
178         log_dbg(cd, "Found area %zu -> %zu", offset, length + offset);
179
180         *area_offset = offset;
181         *area_length = length;
182         return 0;
183 }
184
185 int LUKS2_check_metadata_area_size(uint64_t metadata_size)
186 {
187         /* see LUKS2_HDR2_OFFSETS */
188         return (metadata_size != 0x004000 &&
189                 metadata_size != 0x008000 && metadata_size != 0x010000 &&
190                 metadata_size != 0x020000 && metadata_size != 0x040000 &&
191                 metadata_size != 0x080000 && metadata_size != 0x100000 &&
192                 metadata_size != 0x200000 && metadata_size != 0x400000);
193 }
194
195 int LUKS2_check_keyslots_area_size(uint64_t keyslots_size)
196 {
197         return (MISALIGNED_4K(keyslots_size) ||
198                 keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE);
199 }
200
201 int LUKS2_generate_hdr(
202         struct crypt_device *cd,
203         struct luks2_hdr *hdr,
204         const struct volume_key *vk,
205         const char *cipherName,
206         const char *cipherMode,
207         const char *integrity,
208         const char *uuid,
209         unsigned int sector_size,  /* in bytes */
210         uint64_t data_offset,      /* in bytes */
211         uint64_t align_offset,     /* in bytes */
212         uint64_t required_alignment,
213         uint64_t metadata_size,
214         uint64_t keyslots_size)
215 {
216         struct json_object *jobj_segment, *jobj_integrity, *jobj_keyslots, *jobj_segments, *jobj_config;
217         char cipher[128];
218         uuid_t partitionUuid;
219         int digest;
220         uint64_t mdev_size;
221
222         if (!metadata_size)
223                 metadata_size = LUKS2_HDR_16K_LEN;
224         hdr->hdr_size = metadata_size;
225
226         if (data_offset && data_offset < get_min_offset(hdr)) {
227                 log_err(cd, _("Requested data offset is too small."));
228                 return -EINVAL;
229         }
230
231         /* Increase keyslot size according to data offset */
232         if (!keyslots_size && data_offset)
233                 keyslots_size = data_offset - get_min_offset(hdr);
234
235         /* keyslots size has to be 4 KiB aligned */
236         keyslots_size -= (keyslots_size % 4096);
237
238         if (keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE)
239                 keyslots_size = LUKS2_MAX_KEYSLOTS_SIZE;
240
241         if (!keyslots_size) {
242                 assert(LUKS2_DEFAULT_HDR_SIZE > 2 * LUKS2_HDR_OFFSET_MAX);
243                 keyslots_size = LUKS2_DEFAULT_HDR_SIZE - get_min_offset(hdr);
244                 /* Decrease keyslots_size due to metadata device being too small */
245                 if (!device_size(crypt_metadata_device(cd), &mdev_size) &&
246                     ((keyslots_size + get_min_offset(hdr)) > mdev_size) &&
247                     device_fallocate(crypt_metadata_device(cd), keyslots_size + get_min_offset(hdr)))
248                         keyslots_size = mdev_size - get_min_offset(hdr);
249         }
250
251         /* Decrease keyslots_size if we have smaller data_offset */
252         if (data_offset && (keyslots_size + get_min_offset(hdr)) > data_offset) {
253                 keyslots_size = data_offset - get_min_offset(hdr);
254                 log_dbg(cd, "Decreasing keyslot area size to %" PRIu64
255                         " bytes due to the requested data offset %"
256                         PRIu64 " bytes.", keyslots_size, data_offset);
257         }
258
259         /* Data offset has priority */
260         if (!data_offset && required_alignment) {
261                 data_offset = size_round_up(get_min_offset(hdr) + keyslots_size,
262                                             (size_t)required_alignment);
263                 data_offset += align_offset;
264         }
265
266         log_dbg(cd, "Formatting LUKS2 with JSON metadata area %" PRIu64
267                 " bytes and keyslots area %" PRIu64 " bytes.",
268                 metadata_size - LUKS2_HDR_BIN_LEN, keyslots_size);
269
270         if (keyslots_size < (LUKS2_HDR_OFFSET_MAX - 2*LUKS2_HDR_16K_LEN))
271                 log_std(cd, _("WARNING: keyslots area (%" PRIu64 " bytes) is very small,"
272                         " available LUKS2 keyslot count is very limited.\n"),
273                         keyslots_size);
274
275         hdr->seqid = 1;
276         hdr->version = 2;
277         memset(hdr->label, 0, LUKS2_LABEL_L);
278         strcpy(hdr->checksum_alg, "sha256");
279         crypt_random_get(cd, (char*)hdr->salt1, LUKS2_SALT_L, CRYPT_RND_SALT);
280         crypt_random_get(cd, (char*)hdr->salt2, LUKS2_SALT_L, CRYPT_RND_SALT);
281
282         if (uuid && uuid_parse(uuid, partitionUuid) == -1) {
283                 log_err(cd, _("Wrong LUKS UUID format provided."));
284                 return -EINVAL;
285         }
286         if (!uuid)
287                 uuid_generate(partitionUuid);
288
289         uuid_unparse(partitionUuid, hdr->uuid);
290
291         if (*cipherMode != '\0')
292                 snprintf(cipher, sizeof(cipher), "%s-%s", cipherName, cipherMode);
293         else
294                 snprintf(cipher, sizeof(cipher), "%s", cipherName);
295
296         hdr->jobj = json_object_new_object();
297
298         jobj_keyslots = json_object_new_object();
299         json_object_object_add(hdr->jobj, "keyslots", jobj_keyslots);
300         json_object_object_add(hdr->jobj, "tokens", json_object_new_object());
301         jobj_segments = json_object_new_object();
302         json_object_object_add(hdr->jobj, "segments", jobj_segments);
303         json_object_object_add(hdr->jobj, "digests", json_object_new_object());
304         jobj_config = json_object_new_object();
305         json_object_object_add(hdr->jobj, "config", jobj_config);
306
307         digest = LUKS2_digest_create(cd, "pbkdf2", hdr, vk);
308         if (digest < 0)
309                 goto err;
310
311         if (LUKS2_digest_segment_assign(cd, hdr, 0, digest, 1, 0) < 0)
312                 goto err;
313
314         jobj_segment = json_segment_create_crypt(data_offset, 0, NULL, cipher, sector_size, 0);
315         if (!jobj_segment)
316                 goto err;
317
318         if (integrity) {
319                 jobj_integrity = json_object_new_object();
320                 json_object_object_add(jobj_integrity, "type", json_object_new_string(integrity));
321                 json_object_object_add(jobj_integrity, "journal_encryption", json_object_new_string("none"));
322                 json_object_object_add(jobj_integrity, "journal_integrity", json_object_new_string("none"));
323                 json_object_object_add(jobj_segment, "integrity", jobj_integrity);
324         }
325
326         json_object_object_add_by_uint(jobj_segments, 0, jobj_segment);
327
328         json_object_object_add(jobj_config, "json_size", crypt_jobj_new_uint64(metadata_size - LUKS2_HDR_BIN_LEN));
329         json_object_object_add(jobj_config, "keyslots_size", crypt_jobj_new_uint64(keyslots_size));
330
331         JSON_DBG(cd, hdr->jobj, "Header JSON:");
332         return 0;
333 err:
334         json_object_put(hdr->jobj);
335         hdr->jobj = NULL;
336         return -EINVAL;
337 }
338
339 int LUKS2_wipe_header_areas(struct crypt_device *cd,
340         struct luks2_hdr *hdr)
341 {
342         int r;
343         uint64_t offset, length;
344         size_t wipe_block;
345
346         /* Wipe complete header, keyslots and padding areas with zeroes. */
347         offset = 0;
348         length = LUKS2_get_data_offset(hdr) * SECTOR_SIZE;
349         wipe_block = 1024 * 1024;
350
351         if (LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN))
352                 return -EINVAL;
353
354         /* On detached header wipe at least the first 4k */
355         if (length == 0) {
356                 length = 4096;
357                 wipe_block = 4096;
358         }
359
360         log_dbg(cd, "Wiping LUKS areas (0x%06" PRIx64 " - 0x%06" PRIx64") with zeroes.",
361                 offset, length + offset);
362
363         r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO,
364                               offset, length, wipe_block, NULL, NULL);
365         if (r < 0)
366                 return r;
367
368         /* Wipe keyslot area */
369         wipe_block = 1024 * 1024;
370         offset = get_min_offset(hdr);
371         length = LUKS2_keyslots_size(hdr->jobj);
372
373         log_dbg(cd, "Wiping keyslots area (0x%06" PRIx64 " - 0x%06" PRIx64") with random data.",
374                 offset, length + offset);
375
376         return crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_RANDOM,
377                                  offset, length, wipe_block, NULL, NULL);
378 }
379
380 /* FIXME: what if user wanted to keep original keyslots size? */
381 int LUKS2_set_keyslots_size(struct crypt_device *cd,
382                 struct luks2_hdr *hdr,
383                 uint64_t data_offset)
384 {
385         json_object *jobj_config;
386         uint64_t keyslots_size;
387
388         if (data_offset < get_min_offset(hdr))
389                 return 1;
390
391         keyslots_size = data_offset - get_min_offset(hdr);
392
393         /* keep keyslots_size reasonable for custom data alignments */
394         if (keyslots_size > LUKS2_MAX_KEYSLOTS_SIZE)
395                 keyslots_size = LUKS2_MAX_KEYSLOTS_SIZE;
396
397         /* keyslots size has to be 4 KiB aligned */
398         keyslots_size -= (keyslots_size % 4096);
399
400         if (!json_object_object_get_ex(hdr->jobj, "config", &jobj_config))
401                 return 1;
402
403         json_object_object_add(jobj_config, "keyslots_size", crypt_jobj_new_uint64(keyslots_size));
404         return 0;
405 }