2 * loop-AES compatible volume handling
4 * Copyright (C) 2011, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "crypto_backend.h"
28 static const char *get_hash(unsigned int key_size)
33 case 16: hash = "sha256"; break;
34 case 24: hash = "sha384"; break;
35 case 32: hash = "sha512"; break;
42 static unsigned char get_tweak(unsigned int keys_count)
52 static int hash_key(const char *src, size_t src_len,
53 char *dst, size_t dst_len,
54 const char *hash_name)
56 struct crypt_hash *hd = NULL;
59 if (crypt_hash_init(&hd, hash_name))
62 r = crypt_hash_write(hd, src, src_len);
64 r = crypt_hash_final(hd, dst, dst_len);
66 crypt_hash_destroy(hd);
70 static int hash_keys(struct volume_key **vk,
71 const char **input_keys,
72 unsigned int keys_count,
73 unsigned int key_len_output)
75 const char *hash_name;
77 int r, i, key_len_input;
79 hash_name = get_hash(key_len_output);
80 tweak = get_tweak(keys_count);
81 key_len_input = strlen(input_keys[0]);
83 if (!keys_count || !key_len_output || !hash_name || !key_len_input)
86 *vk = crypt_alloc_volume_key(key_len_output * keys_count, NULL);
90 for (i = 0; i < keys_count; i++) {
91 key_ptr = &(*vk)->key[i * key_len_output];
92 r = hash_key(input_keys[i], key_len_input, key_ptr,
93 key_len_output, hash_name);
101 crypt_free_volume_key(*vk);
107 static int keyfile_is_gpg(char *buffer, size_t buffer_len)
110 int index = buffer_len < 100 ? buffer_len - 1 : 100;
111 char eos = buffer[index];
113 buffer[index] = '\0';
114 if (strstr(buffer, "BEGIN PGP MESSAGE"))
120 int LOOPAES_parse_keyfile(struct crypt_device *cd,
121 struct volume_key **vk,
122 unsigned int *keys_count,
126 const char *keys[LOOPAES_KEYS_MAX];
127 int i, key_index, key_len, offset;
129 log_dbg("Parsing loop-AES keyfile of size %d.", buffer_len);
134 if (keyfile_is_gpg(buffer, buffer_len)) {
135 log_err(cd, _("Detected not yet supported GPG encrypted keyfile.\n"));
136 log_std(cd, _("Please use gpg --decrypt <KEYFILE> | cryptsetup --keyfile=- ...\n"));
140 /* Remove EOL in buffer */
141 for (i = 0; i < buffer_len; i++)
142 if (buffer[i] == '\n' || buffer[i] == '\r')
147 while (offset < buffer_len && key_index < LOOPAES_KEYS_MAX) {
148 keys[key_index++] = &buffer[offset];
149 while (offset < buffer_len && buffer[offset])
151 while (offset < buffer_len && !buffer[offset])
155 /* All keys must be the same length */
156 key_len = key_index ? strlen(keys[0]) : 0;
157 for (i = 0; i < key_index; i++)
158 if (key_len != strlen(keys[i])) {
159 log_dbg("Unexpected length %d of key #%d (should be %d).",
160 strlen(keys[i]), i, key_len);
165 log_dbg("Keyfile: %d keys of length %d.", key_index, key_len);
166 if (offset != buffer_len || key_len == 0 ||
167 (key_index != 1 && key_index !=64 && key_index != 65)) {
168 log_err(cd, _("Incompatible loop-AES keyfile detected.\n"));
172 *keys_count = key_index;
173 return hash_keys(vk, keys, key_index, crypt_get_volume_key_size(cd));
176 int LOOPAES_activate(struct crypt_device *cd,
178 const char *base_cipher,
179 unsigned int keys_count,
180 struct volume_key *vk,
193 /* Initial IV (skip) is always the same as offset */
194 device = crypt_get_device_name(cd);
195 read_only = flags & CRYPT_ACTIVATE_READONLY;
197 r = device_check_and_adjust(cd, device, 1, &size, &offset, &read_only);
201 if (keys_count == 1) {
202 req_flags = DM_PLAIN64_SUPPORTED;
203 r = asprintf(&cipher, "%s-%s", base_cipher, "cbc-plain64");
205 req_flags = DM_LMK_SUPPORTED;
206 r = asprintf(&cipher, "%s:%d-%s", base_cipher, 64, "cbc-lmk");
211 log_dbg("Trying to activate loop-AES device %s using cipher %s.", name, cipher);
212 r = dm_create_device(name, device,
213 cipher, CRYPT_LOOPAES,
215 size, skip, offset, vk->keylength, vk->key,
218 if (!r && !(dm_flags() & req_flags)) {
219 log_err(cd, _("Kernel doesn't support loop-AES compatible mapping.\n"));