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.
27 static void _error_hint(struct crypt_device *ctx, const char *device,
28 const char *cipher_spec, const char *mode, size_t keyLength)
30 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
31 "Check that kernel supports %s cipher (check syslog for more info).\n"),
34 if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
35 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
38 static int LUKS_endec_template(char *src, size_t srcLength,
39 const char *cipher, const char *cipher_mode,
40 struct volume_key *vk,
42 ssize_t (*func)(int, int, void *, size_t),
44 struct crypt_device *ctx)
46 char name[PATH_MAX], path[PATH_MAX];
47 char cipher_spec[MAX_CIPHER_LEN * 3];
48 struct crypt_dm_active_device dmd = {
51 .flags = CRYPT_ACTIVATE_PRIVATE,
52 .data_device = crypt_metadata_device(ctx),
54 .cipher = cipher_spec,
60 int r, bsize, devfd = -1;
62 bsize = device_block_size(dmd.data_device);
66 dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
69 dmd.flags |= CRYPT_ACTIVATE_READONLY;
71 if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
73 if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
75 if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
78 r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
79 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
81 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
82 device_path(dmd.data_device));
86 if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
87 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
88 device_path(dmd.data_device));
92 r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
94 if (r != -EACCES && r != -ENOTSUP)
95 _error_hint(ctx, device_path(dmd.data_device),
96 cipher_spec, cipher_mode, vk->keylength * 8);
100 devfd = open(path, mode | O_DIRECT | O_SYNC);
102 log_err(ctx, _("Failed to open temporary keystore device.\n"));
107 r = func(devfd, bsize, src, srcLength);
109 log_err(ctx, _("Failed to access temporary keystore device.\n"));
116 dm_remove_device(ctx, name, 1, dmd.size);
120 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
122 const char *cipher_mode,
123 struct volume_key *vk,
125 struct crypt_device *ctx)
127 return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
128 vk, sector, write_blockwise, O_RDWR, ctx);
131 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
133 const char *cipher_mode,
134 struct volume_key *vk,
136 struct crypt_device *ctx)
138 return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
139 vk, sector, read_blockwise, O_RDONLY, ctx);