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 #define div_round_up(a,b) ({ \
40 typeof(a) __a = (a); \
41 typeof(b) __b = (b); \
42 (__a - 1) / __b + 1; \
45 static inline int round_up_modulo(int x, int m) {
46 return div_round_up(x, m) * m;
49 static const char *cleaner_name=NULL;
50 static uint64_t cleaner_size = 0;
53 static int setup_mapping(const char *cipher, const char *name,
54 int bsize, struct volume_key *vk,
55 unsigned int sector, size_t srcLength,
56 int mode, struct crypt_device *ctx)
58 struct device *device = crypt_metadata_device(ctx);
59 struct crypt_dm_active_device dmd = {
62 .size = round_up_modulo(srcLength, bsize) / SECTOR_SIZE,
63 .flags = CRYPT_ACTIVATE_PRIVATE,
64 .data_device = device,
75 dmd.flags |= CRYPT_ACTIVATE_READONLY;
77 r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
78 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
82 cleaner_size = dmd.size;
83 return dm_create_device(name, "TEMP", &dmd, 0);
86 static void sigint_handler(int sig __attribute__((unused)))
92 dm_remove_device(cleaner_name, 1, cleaner_size);
94 signal(SIGINT, SIG_DFL);
95 kill(getpid(), SIGINT);
98 static const char *_error_hint(char *cipherMode, size_t keyLength)
100 const char *hint= "";
102 if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
103 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
108 /* This function is not reentrant safe, as it installs a signal
109 handler and global vars for cleaning */
110 static int LUKS_endec_template(char *src, size_t srcLength,
111 struct luks_phdr *hdr,
112 struct volume_key *vk,
114 ssize_t (*func)(int, int, void *, size_t),
116 struct crypt_device *ctx)
119 char *fullpath = NULL;
120 char *dmCipherSpec = NULL;
121 const char *dmDir = dm_get_dir();
123 int bsize = device_block_size(crypt_metadata_device(ctx));
126 log_err(ctx, _("Failed to obtain device mapper directory."));
129 if(asprintf(&name,"temporary-cryptsetup-%d",getpid()) == -1 ||
130 asprintf(&fullpath,"%s/%s",dmDir,name) == -1 ||
131 asprintf(&dmCipherSpec,"%s-%s",hdr->cipherName, hdr->cipherMode) == -1) {
136 signal(SIGINT, sigint_handler);
139 r = setup_mapping(dmCipherSpec, name, bsize, vk, sector, srcLength, mode, ctx);
141 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
142 "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
143 device_path(crypt_metadata_device(ctx)), dmCipherSpec,
144 _error_hint(hdr->cipherMode, vk->keylength * 8));
149 devfd = open(fullpath, mode | O_DIRECT | O_SYNC); /* devfd is a global var */
151 log_err(ctx, _("Failed to open temporary keystore device.\n"));
156 r = func(devfd, bsize, src, srcLength);
158 log_err(ctx, _("Failed to access temporary keystore device.\n"));
168 dm_remove_device(cleaner_name, 1, cleaner_size);
170 signal(SIGINT, SIG_DFL);
179 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
180 struct luks_phdr *hdr,
181 struct volume_key *vk,
183 struct crypt_device *ctx)
185 return LUKS_endec_template(src, srcLength, hdr, vk, sector,
186 write_blockwise, O_RDWR, ctx);
189 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
190 struct luks_phdr *hdr,
191 struct volume_key *vk,
193 struct crypt_device *ctx)
195 return LUKS_endec_template(dst, dstLength, hdr, vk, sector,
196 read_blockwise, O_RDONLY, ctx);