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 #include <sys/types.h>
28 #include <sys/ioctl.h>
30 #include <sys/utsname.h>
39 static const char *cleaner_name=NULL;
40 static uint64_t cleaner_size = 0;
43 static int setup_mapping(const char *cipher, const char *name,
44 unsigned int bsize, struct volume_key *vk,
45 unsigned int sector, size_t srcLength,
46 int mode, struct crypt_device *ctx)
48 struct device *device = crypt_metadata_device(ctx);
49 struct crypt_dm_active_device dmd = {
52 .size = size_round_up(srcLength, bsize) / SECTOR_SIZE,
53 .flags = CRYPT_ACTIVATE_PRIVATE,
54 .data_device = device,
65 dmd.flags |= CRYPT_ACTIVATE_READONLY;
67 r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
68 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
72 if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
73 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
77 cleaner_size = dmd.size;
78 return dm_create_device(ctx, name, "TEMP", &dmd, 0);
81 static void sigint_handler(int sig __attribute__((unused)))
87 dm_remove_device(NULL, cleaner_name, 1, cleaner_size);
89 signal(SIGINT, SIG_DFL);
90 kill(getpid(), SIGINT);
93 static const char *_error_hint(char *cipherMode, size_t keyLength)
97 if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
98 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
103 /* This function is not reentrant safe, as it installs a signal
104 handler and global vars for cleaning */
105 static int LUKS_endec_template(char *src, size_t srcLength,
106 struct luks_phdr *hdr,
107 struct volume_key *vk,
109 ssize_t (*func)(int, int, void *, size_t),
111 struct crypt_device *ctx)
114 char *fullpath = NULL;
115 char *dmCipherSpec = NULL;
116 const char *dmDir = dm_get_dir();
118 int bsize = device_block_size(crypt_metadata_device(ctx));
121 log_err(ctx, _("Failed to obtain device mapper directory."));
124 if(asprintf(&name,"temporary-cryptsetup-%d",getpid()) == -1 ||
125 asprintf(&fullpath,"%s/%s",dmDir,name) == -1 ||
126 asprintf(&dmCipherSpec,"%s-%s",hdr->cipherName, hdr->cipherMode) == -1) {
131 signal(SIGINT, sigint_handler);
134 r = setup_mapping(dmCipherSpec, name, bsize, vk, sector, srcLength, mode, ctx);
136 if (r != -EACCES && r != -ENOTSUP)
137 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
138 "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
139 device_path(crypt_metadata_device(ctx)), dmCipherSpec,
140 _error_hint(hdr->cipherMode, vk->keylength * 8));
145 devfd = open(fullpath, mode | O_DIRECT | O_SYNC); /* devfd is a global var */
147 log_err(ctx, _("Failed to open temporary keystore device.\n"));
152 r = func(devfd, bsize, src, srcLength);
154 log_err(ctx, _("Failed to access temporary keystore device.\n"));
164 dm_remove_device(ctx, cleaner_name, 1, cleaner_size);
166 signal(SIGINT, SIG_DFL);
175 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
176 struct luks_phdr *hdr,
177 struct volume_key *vk,
179 struct crypt_device *ctx)
181 return LUKS_endec_template(src, srcLength, hdr, vk, sector,
182 write_blockwise, O_RDWR, ctx);
185 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
186 struct luks_phdr *hdr,
187 struct volume_key *vk,
189 struct crypt_device *ctx)
191 return LUKS_endec_template(dst, dstLength, hdr, vk, sector,
192 read_blockwise, O_RDONLY, ctx);