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.
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.
28 static void _error_hint(struct crypt_device *ctx, const char *device,
29 const char *cipher_spec, const char *mode, size_t keyLength)
31 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
32 "Check that kernel supports %s cipher (check syslog for more info).\n"),
35 if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
36 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
39 static int LUKS_endec_template(char *src, size_t srcLength,
40 const char *cipher, const char *cipher_mode,
41 struct volume_key *vk,
43 ssize_t (*func)(int, int, void *, size_t),
45 struct crypt_device *ctx)
47 char name[PATH_MAX], path[PATH_MAX];
48 char cipher_spec[MAX_CIPHER_LEN * 3];
49 struct crypt_dm_active_device dmd = {
52 .flags = CRYPT_ACTIVATE_PRIVATE,
53 .data_device = crypt_metadata_device(ctx),
55 .cipher = cipher_spec,
61 int r, bsize, devfd = -1;
63 bsize = device_block_size(dmd.data_device);
67 dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
70 dmd.flags |= CRYPT_ACTIVATE_READONLY;
72 if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
74 if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
76 if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
79 r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
80 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
82 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
83 device_path(dmd.data_device));
87 if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
88 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
89 device_path(dmd.data_device));
93 r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
95 if (r != -EACCES && r != -ENOTSUP)
96 _error_hint(ctx, device_path(dmd.data_device),
97 cipher_spec, cipher_mode, vk->keylength * 8);
101 devfd = open(path, mode | O_DIRECT | O_SYNC);
103 log_err(ctx, _("Failed to open temporary keystore device.\n"));
108 r = func(devfd, bsize, src, srcLength);
110 log_err(ctx, _("Failed to access temporary keystore device.\n"));
117 dm_remove_device(ctx, name, 1, dmd.size);
121 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
123 const char *cipher_mode,
124 struct volume_key *vk,
126 struct crypt_device *ctx)
128 return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
129 vk, sector, write_blockwise, O_RDWR, ctx);
132 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
134 const char *cipher_mode,
135 struct volume_key *vk,
137 struct crypt_device *ctx)
139 return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
140 vk, sector, read_blockwise, O_RDONLY, ctx);