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 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 static void _error_hint(struct crypt_device *ctx, const char *device,
27 const char *cipher_spec, const char *mode, size_t keyLength)
29 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
30 "Check that kernel supports %s cipher (check syslog for more info).\n"),
33 if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
34 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
37 static int LUKS_endec_template(char *src, size_t srcLength,
38 const char *cipher, const char *cipher_mode,
39 struct volume_key *vk,
41 ssize_t (*func)(int, int, void *, size_t),
43 struct crypt_device *ctx)
45 char name[PATH_MAX], path[PATH_MAX];
46 char cipher_spec[MAX_CIPHER_LEN * 3];
47 struct crypt_dm_active_device dmd = {
50 .flags = CRYPT_ACTIVATE_PRIVATE,
51 .data_device = crypt_metadata_device(ctx),
53 .cipher = cipher_spec,
59 int r, bsize, devfd = -1;
61 bsize = device_block_size(dmd.data_device);
65 dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
68 dmd.flags |= CRYPT_ACTIVATE_READONLY;
70 if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
72 if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
74 if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
77 r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
78 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
80 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
81 device_path(dmd.data_device));
85 if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
86 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
87 device_path(dmd.data_device));
91 r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
93 if (r != -EACCES && r != -ENOTSUP)
94 _error_hint(ctx, device_path(dmd.data_device),
95 cipher_spec, cipher_mode, vk->keylength * 8);
99 devfd = open(path, mode | O_DIRECT | O_SYNC);
101 log_err(ctx, _("Failed to open temporary keystore device.\n"));
106 r = func(devfd, bsize, src, srcLength);
108 log_err(ctx, _("Failed to access temporary keystore device.\n"));
115 dm_remove_device(ctx, name, 1, dmd.size);
119 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
121 const char *cipher_mode,
122 struct volume_key *vk,
124 struct crypt_device *ctx)
126 return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
127 vk, sector, write_blockwise, O_RDWR, ctx);
130 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
132 const char *cipher_mode,
133 struct volume_key *vk,
135 struct crypt_device *ctx)
137 return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
138 vk, sector, read_blockwise, O_RDONLY, ctx);