2 * loop-AES compatible volume handling
4 * Copyright (C) 2011-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2011-2021 Milan Broz
7 * This file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This file 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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this file; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "libcryptsetup.h"
31 static const char *get_hash(unsigned int key_size)
36 case 16: hash = "sha256"; break;
37 case 24: hash = "sha384"; break;
38 case 32: hash = "sha512"; break;
45 static unsigned char get_tweak(unsigned int keys_count)
55 static int hash_key(const char *src, size_t src_len,
56 char *dst, size_t dst_len,
57 const char *hash_name)
59 struct crypt_hash *hd = NULL;
62 if (crypt_hash_init(&hd, hash_name))
65 r = crypt_hash_write(hd, src, src_len);
67 r = crypt_hash_final(hd, dst, dst_len);
69 crypt_hash_destroy(hd);
73 static int hash_keys(struct crypt_device *cd,
74 struct volume_key **vk,
75 const char *hash_override,
76 const char **input_keys,
77 unsigned int keys_count,
78 unsigned int key_len_output,
79 unsigned int key_len_input)
81 const char *hash_name;
86 hash_name = hash_override ?: get_hash(key_len_output);
87 tweak = get_tweak(keys_count);
89 if (!keys_count || !key_len_output || !hash_name || !key_len_input) {
90 log_err(cd, _("Key processing error (using hash %s)."),
91 hash_name ?: "[none]");
95 *vk = crypt_alloc_volume_key((size_t)key_len_output * keys_count, NULL);
99 for (i = 0; i < keys_count; i++) {
100 key_ptr = &(*vk)->key[i * key_len_output];
101 r = hash_key(input_keys[i], key_len_input, key_ptr,
102 key_len_output, hash_name);
110 crypt_free_volume_key(*vk);
116 static int keyfile_is_gpg(char *buffer, size_t buffer_len)
119 int index = buffer_len < 100 ? buffer_len - 1 : 100;
120 char eos = buffer[index];
122 buffer[index] = '\0';
123 if (strstr(buffer, "BEGIN PGP MESSAGE"))
129 int LOOPAES_parse_keyfile(struct crypt_device *cd,
130 struct volume_key **vk,
132 unsigned int *keys_count,
136 const char *keys[LOOPAES_KEYS_MAX];
137 unsigned int key_lengths[LOOPAES_KEYS_MAX];
138 unsigned int i, key_index, key_len, offset;
140 log_dbg(cd, "Parsing loop-AES keyfile of size %zu.", buffer_len);
145 if (keyfile_is_gpg(buffer, buffer_len)) {
146 log_err(cd, _("Detected not yet supported GPG encrypted keyfile."));
147 log_std(cd, _("Please use gpg --decrypt <KEYFILE> | cryptsetup --keyfile=- ...\n"));
151 /* Remove EOL in buffer */
152 for (i = 0; i < buffer_len; i++)
153 if (buffer[i] == '\n' || buffer[i] == '\r')
159 while (offset < buffer_len && key_index < LOOPAES_KEYS_MAX) {
160 keys[key_index] = &buffer[offset];
161 key_lengths[key_index] = 0;;
162 while (offset < buffer_len && buffer[offset]) {
164 key_lengths[key_index]++;
166 if (offset == buffer_len) {
167 log_dbg(cd, "Unterminated key #%d in keyfile.", key_index);
168 log_err(cd, _("Incompatible loop-AES keyfile detected."));
171 while (offset < buffer_len && !buffer[offset])
176 /* All keys must be the same length */
177 key_len = key_lengths[0];
178 for (i = 0; i < key_index; i++)
179 if (!key_lengths[i] || (key_lengths[i] != key_len)) {
180 log_dbg(cd, "Unexpected length %d of key #%d (should be %d).",
181 key_lengths[i], i, key_len);
186 if (offset != buffer_len || key_len == 0 ||
187 (key_index != 1 && key_index !=64 && key_index != 65)) {
188 log_err(cd, _("Incompatible loop-AES keyfile detected."));
192 log_dbg(cd, "Keyfile: %d keys of length %d.", key_index, key_len);
194 *keys_count = key_index;
195 return hash_keys(cd, vk, hash, keys, key_index,
196 crypt_get_volume_key_size(cd), key_len);
199 int LOOPAES_activate(struct crypt_device *cd,
201 const char *base_cipher,
202 unsigned int keys_count,
203 struct volume_key *vk,
207 uint32_t req_flags, dmc_flags;
209 struct crypt_dm_active_device dmd = {
213 r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
214 crypt_get_data_offset(cd), &dmd.size, &dmd.flags);
218 if (keys_count == 1) {
219 req_flags = DM_PLAIN64_SUPPORTED;
220 r = asprintf(&cipher, "%s-%s", base_cipher, "cbc-plain64");
222 req_flags = DM_LMK_SUPPORTED;
223 r = asprintf(&cipher, "%s:%d-%s", base_cipher, 64, "cbc-lmk");
228 r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
229 vk, cipher, crypt_get_iv_offset(cd),
230 crypt_get_data_offset(cd), crypt_get_integrity(cd),
231 crypt_get_integrity_tag_size(cd), crypt_get_sector_size(cd));
238 log_dbg(cd, "Trying to activate loop-AES device %s using cipher %s.",
241 r = dm_create_device(cd, name, CRYPT_LOOPAES, &dmd);
243 if (r < 0 && !dm_flags(cd, DM_CRYPT, &dmc_flags) &&
244 (dmc_flags & req_flags) != req_flags) {
245 log_err(cd, _("Kernel does not support loop-AES compatible mapping."));
249 dm_targets_free(cd, &dmd);