2 * LUKS - Linux Unified Key Setup
4 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2012-2014, Milan Broz
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 static void _error_hint(struct crypt_device *ctx, const char *device,
30 const char *cipher, const char *mode, size_t keyLength)
32 char cipher_spec[MAX_CIPHER_LEN * 3];
34 if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, mode) < 0)
37 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
38 "Check that kernel supports %s cipher (check syslog for more info).\n"),
41 if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
42 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
45 static int LUKS_endec_template(char *src, size_t srcLength,
46 const char *cipher, const char *cipher_mode,
47 struct volume_key *vk,
49 ssize_t (*func)(int, int, void *, size_t),
51 struct crypt_device *ctx)
53 char name[PATH_MAX], path[PATH_MAX];
54 char cipher_spec[MAX_CIPHER_LEN * 3];
55 struct crypt_dm_active_device dmd = {
58 .flags = CRYPT_ACTIVATE_PRIVATE,
59 .data_device = crypt_metadata_device(ctx),
61 .cipher = cipher_spec,
67 int r, bsize, devfd = -1;
69 log_dbg("Using dmcrypt to access keyslot area.");
71 bsize = device_block_size(dmd.data_device);
75 dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
78 dmd.flags |= CRYPT_ACTIVATE_READONLY;
80 if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
82 if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
84 if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
87 r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
88 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
90 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
91 device_path(dmd.data_device));
95 if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
96 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
97 device_path(dmd.data_device));
101 r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
103 if (r != -EACCES && r != -ENOTSUP)
104 _error_hint(ctx, device_path(dmd.data_device),
105 cipher, cipher_mode, vk->keylength * 8);
109 devfd = open(path, mode | O_DIRECT | O_SYNC);
111 log_err(ctx, _("Failed to open temporary keystore device.\n"));
116 r = func(devfd, bsize, src, srcLength);
118 log_err(ctx, _("Failed to access temporary keystore device.\n"));
125 dm_remove_device(ctx, name, 1, dmd.size);
129 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
131 const char *cipher_mode,
132 struct volume_key *vk,
134 struct crypt_device *ctx)
137 struct device *device = crypt_metadata_device(ctx);
138 struct crypt_storage *s;
139 int devfd = -1, bsize, r = 0;
141 /* Only whole sector writes supported */
142 if (srcLength % SECTOR_SIZE)
146 r = crypt_storage_init(&s, 0, cipher, cipher_mode, vk->key, vk->keylength);
149 log_dbg("Userspace crypto wrapper cannot use %s-%s (%d).",
150 cipher, cipher_mode, r);
152 /* Fallback to old temporary dmcrypt device */
153 if (r == -ENOTSUP || r == -ENOENT)
154 return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
155 vk, sector, write_blockwise, O_RDWR, ctx);
158 _error_hint(ctx, device_path(device), cipher, cipher_mode,
163 log_dbg("Using userspace crypto wrapper to access keyslot area.");
165 r = crypt_storage_encrypt(s, 0, srcLength / SECTOR_SIZE, src);
166 crypt_storage_destroy(s);
173 /* Write buffer to device */
174 bsize = device_block_size(device);
178 devfd = device_open(device, O_RDWR);
182 if (lseek(devfd, sector * SECTOR_SIZE, SEEK_SET) == -1 ||
183 write_blockwise(devfd, bsize, src, srcLength) == -1)
191 log_err(ctx, _("IO error while encrypting keyslot.\n"));
196 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
198 const char *cipher_mode,
199 struct volume_key *vk,
201 struct crypt_device *ctx)
203 struct device *device = crypt_metadata_device(ctx);
204 struct crypt_storage *s;
205 int devfd = -1, bsize, r = 0;
207 /* Only whole sector reads supported */
208 if (dstLength % SECTOR_SIZE)
211 r = crypt_storage_init(&s, 0, cipher, cipher_mode, vk->key, vk->keylength);
214 log_dbg("Userspace crypto wrapper cannot use %s-%s (%d).",
215 cipher, cipher_mode, r);
217 /* Fallback to old temporary dmcrypt device */
218 if (r == -ENOTSUP || r == -ENOENT)
219 return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
220 vk, sector, read_blockwise, O_RDONLY, ctx);
223 _error_hint(ctx, device_path(device), cipher, cipher_mode,
228 log_dbg("Using userspace crypto wrapper to access keyslot area.");
232 /* Read buffer from device */
233 bsize = device_block_size(device);
237 devfd = device_open(device, O_RDONLY);
241 if (lseek(devfd, sector * SECTOR_SIZE, SEEK_SET) == -1 ||
242 read_blockwise(devfd, bsize, dst, dstLength) == -1)
248 r = crypt_storage_decrypt(s, 0, dstLength / SECTOR_SIZE, dst);
249 crypt_storage_destroy(s);
256 log_err(ctx, _("IO error while decrypting keyslot.\n"));
257 crypt_storage_destroy(s);