2 * LUKS - Linux Unified Key Setup v2, reencryption keyslot handler
4 * Copyright (C) 2016-2021, Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2016-2021, Ondrej Kozina
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.
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.
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.
22 #include "luks2_internal.h"
24 static int reenc_keyslot_open(struct crypt_device *cd,
29 size_t volume_key_len)
34 int reenc_keyslot_alloc(struct crypt_device *cd,
35 struct luks2_hdr *hdr,
37 const struct crypt_params_reencrypt *params)
40 json_object *jobj_keyslots, *jobj_keyslot, *jobj_area;
41 uint64_t area_offset, area_length;
43 log_dbg(cd, "Allocating reencrypt keyslot %d.", keyslot);
45 if (keyslot < 0 || keyslot >= LUKS2_KEYSLOTS_MAX)
48 if (!json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots))
51 /* encryption doesn't require area (we shift data and backup will be available) */
52 if (!params->data_shift) {
53 r = LUKS2_find_area_max_gap(cd, hdr, &area_offset, &area_length);
56 } else { /* we can't have keyslot w/o area...bug? */
57 r = LUKS2_find_area_gap(cd, hdr, 1, &area_offset, &area_length);
62 jobj_keyslot = json_object_new_object();
66 jobj_area = json_object_new_object();
68 if (params->data_shift) {
69 json_object_object_add(jobj_area, "type", json_object_new_string("datashift"));
70 json_object_object_add(jobj_area, "shift_size", crypt_jobj_new_uint64(params->data_shift << SECTOR_SHIFT));
72 /* except data shift protection, initial setting is irrelevant. Type can be changed during reencryption */
73 json_object_object_add(jobj_area, "type", json_object_new_string("none"));
75 json_object_object_add(jobj_area, "offset", crypt_jobj_new_uint64(area_offset));
76 json_object_object_add(jobj_area, "size", crypt_jobj_new_uint64(area_length));
78 json_object_object_add(jobj_keyslot, "type", json_object_new_string("reencrypt"));
79 json_object_object_add(jobj_keyslot, "key_size", json_object_new_int(1)); /* useless but mandatory */
80 json_object_object_add(jobj_keyslot, "mode", json_object_new_string(crypt_reencrypt_mode_to_str(params->mode)));
81 if (params->direction == CRYPT_REENCRYPT_FORWARD)
82 json_object_object_add(jobj_keyslot, "direction", json_object_new_string("forward"));
83 else if (params->direction == CRYPT_REENCRYPT_BACKWARD)
84 json_object_object_add(jobj_keyslot, "direction", json_object_new_string("backward"));
88 json_object_object_add(jobj_keyslot, "area", jobj_area);
90 json_object_object_add_by_uint(jobj_keyslots, keyslot, jobj_keyslot);
91 if (LUKS2_check_json_size(cd, hdr)) {
92 log_dbg(cd, "New keyslot too large to fit in free metadata space.");
93 json_object_object_del_by_uint(jobj_keyslots, keyslot);
97 JSON_DBG(cd, hdr->jobj, "JSON:");
102 static int reenc_keyslot_store_data(struct crypt_device *cd,
103 json_object *jobj_keyslot,
104 const void *buffer, size_t buffer_len)
107 json_object *jobj_area, *jobj_offset, *jobj_length;
108 uint64_t area_offset, area_length;
109 struct device *device = crypt_metadata_device(cd);
111 if (!json_object_object_get_ex(jobj_keyslot, "area", &jobj_area) ||
112 !json_object_object_get_ex(jobj_area, "offset", &jobj_offset) ||
113 !json_object_object_get_ex(jobj_area, "size", &jobj_length))
116 area_offset = crypt_jobj_get_uint64(jobj_offset);
117 area_length = crypt_jobj_get_uint64(jobj_length);
119 if (!area_offset || !area_length || ((uint64_t)buffer_len > area_length))
122 devfd = device_open_locked(cd, device, O_RDWR);
124 if (write_lseek_blockwise(devfd, device_block_size(cd, device),
125 device_alignment(device), CONST_CAST(void *)buffer,
126 buffer_len, area_offset) < 0)
134 log_err(cd, _("IO error while encrypting keyslot."));
139 static int reenc_keyslot_store(struct crypt_device *cd,
141 const char *password __attribute__((unused)),
142 size_t password_len __attribute__((unused)),
146 struct luks2_hdr *hdr;
147 json_object *jobj_keyslot;
150 if (!cd || !buffer || !buffer_len)
153 if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
156 log_dbg(cd, "Reencrypt keyslot %d store.", keyslot);
158 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
162 r = LUKS2_device_write_lock(cd, hdr, crypt_metadata_device(cd));
166 r = reenc_keyslot_store_data(cd, jobj_keyslot, buffer, buffer_len);
168 device_write_unlock(cd, crypt_metadata_device(cd));
172 r = LUKS2_hdr_write(cd, hdr);
174 device_write_unlock(cd, crypt_metadata_device(cd));
176 return r < 0 ? r : keyslot;
179 static int reenc_keyslot_wipe(struct crypt_device *cd,
182 struct luks2_hdr *hdr;
184 if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
187 /* remove reencryption verification data */
188 LUKS2_digest_assign(cd, hdr, keyslot, CRYPT_ANY_DIGEST, 0, 0);
193 static int reenc_keyslot_dump(struct crypt_device *cd, int keyslot)
195 json_object *jobj_keyslot, *jobj_area, *jobj_direction, *jobj_mode, *jobj_resilience,
198 jobj_keyslot = LUKS2_get_keyslot_jobj(crypt_get_hdr(cd, CRYPT_LUKS2), keyslot);
202 if (!json_object_object_get_ex(jobj_keyslot, "direction", &jobj_direction) ||
203 !json_object_object_get_ex(jobj_keyslot, "mode", &jobj_mode) ||
204 !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area) ||
205 !json_object_object_get_ex(jobj_area, "type", &jobj_resilience))
208 log_std(cd, "\t%-12s%s\n", "Mode:", json_object_get_string(jobj_mode));
209 log_std(cd, "\t%-12s%s\n", "Direction:", json_object_get_string(jobj_direction));
210 log_std(cd, "\t%-12s%s\n", "Resilience:", json_object_get_string(jobj_resilience));
212 if (!strcmp(json_object_get_string(jobj_resilience), "checksum")) {
213 json_object_object_get_ex(jobj_area, "hash", &jobj1);
214 log_std(cd, "\t%-12s%s\n", "Hash:", json_object_get_string(jobj1));
215 json_object_object_get_ex(jobj_area, "sector_size", &jobj1);
216 log_std(cd, "\t%-12s%d [bytes]\n", "Hash data:", json_object_get_int(jobj1));
217 } else if (!strcmp(json_object_get_string(jobj_resilience), "datashift")) {
218 json_object_object_get_ex(jobj_area, "shift_size", &jobj1);
219 log_std(cd, "\t%-12s%" PRIu64 "[bytes]\n", "Shift size:", crypt_jobj_get_uint64(jobj1));
222 json_object_object_get_ex(jobj_area, "offset", &jobj1);
223 log_std(cd, "\tArea offset:%" PRIu64 " [bytes]\n", crypt_jobj_get_uint64(jobj1));
225 json_object_object_get_ex(jobj_area, "size", &jobj1);
226 log_std(cd, "\tArea length:%" PRIu64 " [bytes]\n", crypt_jobj_get_uint64(jobj1));
231 static int reenc_keyslot_validate(struct crypt_device *cd, json_object *jobj_keyslot)
233 json_object *jobj_mode, *jobj_area, *jobj_type, *jobj_shift_size, *jobj_hash, *jobj_sector_size, *jobj_direction, *jobj_key_size;
234 const char *mode, *type, *direction;
235 uint32_t sector_size;
238 /* mode (string: encrypt,reencrypt,decrypt)
239 * direction (string:)
241 * type: (string: datashift, journal, checksum, none)
242 * hash: (string: checksum only)
243 * sector_size (uint32: checksum only)
244 * shift_size (uint64: datashift only)
248 /* area and area type are validated in general validation code */
249 if (!jobj_keyslot || !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area) ||
250 !json_object_object_get_ex(jobj_area, "type", &jobj_type))
253 jobj_key_size = json_contains(cd, jobj_keyslot, "", "reencrypt keyslot", "key_size", json_type_int);
254 jobj_mode = json_contains(cd, jobj_keyslot, "", "reencrypt keyslot", "mode", json_type_string);
255 jobj_direction = json_contains(cd, jobj_keyslot, "", "reencrypt keyslot", "direction", json_type_string);
257 if (!jobj_mode || !jobj_direction || !jobj_key_size)
260 if (!validate_json_uint32(jobj_key_size) || crypt_jobj_get_uint32(jobj_key_size) != 1) {
261 log_dbg(cd, "Illegal reencrypt key size.");
265 mode = json_object_get_string(jobj_mode);
266 type = json_object_get_string(jobj_type);
267 direction = json_object_get_string(jobj_direction);
269 if (strcmp(mode, "reencrypt") && strcmp(mode, "encrypt") &&
270 strcmp(mode, "decrypt")) {
271 log_dbg(cd, "Illegal reencrypt mode %s.", mode);
275 if (strcmp(direction, "forward") && strcmp(direction, "backward")) {
276 log_dbg(cd, "Illegal reencrypt direction %s.", direction);
280 if (!strcmp(type, "checksum")) {
281 jobj_hash = json_contains(cd, jobj_area, "type:checksum", "Keyslot area", "hash", json_type_string);
282 jobj_sector_size = json_contains(cd, jobj_area, "type:checksum", "Keyslot area", "sector_size", json_type_int);
283 if (!jobj_hash || !jobj_sector_size)
285 if (!validate_json_uint32(jobj_sector_size))
287 sector_size = crypt_jobj_get_uint32(jobj_sector_size);
288 if (sector_size < SECTOR_SIZE || NOTPOW2(sector_size)) {
289 log_dbg(cd, "Invalid sector_size (%" PRIu32 ") for checksum resilience mode.", sector_size);
292 } else if (!strcmp(type, "datashift")) {
293 if (!(jobj_shift_size = json_contains(cd, jobj_area, "type:datashift", "Keyslot area", "shift_size", json_type_string)))
296 shift_size = crypt_jobj_get_uint64(jobj_shift_size);
300 if (MISALIGNED_512(shift_size)) {
301 log_dbg(cd, "Shift size field has to be aligned to sector size: %" PRIu32, SECTOR_SIZE);
309 const keyslot_handler reenc_keyslot = {
311 .open = reenc_keyslot_open,
312 .store = reenc_keyslot_store, /* initialization only or also per every chunk write */
313 .wipe = reenc_keyslot_wipe,
314 .dump = reenc_keyslot_dump,
315 .validate = reenc_keyslot_validate