2 * LUKS - Linux Unified Key Setup
4 * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
29 #include <sys/utsname.h>
38 #define div_round_up(a,b) ({ \
39 typeof(a) __a = (a); \
40 typeof(b) __b = (b); \
41 (__a - 1) / __b + 1; \
44 static inline int round_up_modulo(int x, int m) {
45 return div_round_up(x, m) * m;
48 static const char *cleaner_name=NULL;
49 static uint64_t cleaner_size = 0;
52 static int setup_mapping(const char *cipher, const char *name,
54 struct volume_key *vk,
55 unsigned int sector, size_t srcLength,
56 int mode, struct crypt_device *ctx)
58 int device_sector_size = sector_size_for_device(device);
59 struct crypt_dm_active_device dmd = {
67 .flags = (mode == O_RDONLY) ? CRYPT_ACTIVATE_READONLY : 0
71 * we need to round this to nearest multiple of the underlying
72 * device's sector size, otherwise the mapping will be refused.
74 if(device_sector_size < 0) {
75 log_err(ctx, _("Unable to obtain sector size for %s"), device);
79 dmd.size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
80 cleaner_size = dmd.size;
82 return dm_create_device(name, "TEMP", &dmd, 0);
85 static void sigint_handler(int sig __attribute__((unused)))
91 dm_remove_device(cleaner_name, 1, cleaner_size);
93 signal(SIGINT, SIG_DFL);
94 kill(getpid(), SIGINT);
97 static const char *_error_hint(char *cipherMode, size_t keyLength)
101 if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
102 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
107 /* This function is not reentrant safe, as it installs a signal
108 handler and global vars for cleaning */
109 static int LUKS_endec_template(char *src, size_t srcLength,
110 struct luks_phdr *hdr,
111 struct volume_key *vk,
114 ssize_t (*func)(int, void *, size_t),
116 struct crypt_device *ctx)
119 char *fullpath = NULL;
120 char *dmCipherSpec = NULL;
121 const char *dmDir = dm_get_dir();
125 log_err(ctx, _("Failed to obtain device mapper directory."));
128 if(asprintf(&name,"temporary-cryptsetup-%d",getpid()) == -1 ||
129 asprintf(&fullpath,"%s/%s",dmDir,name) == -1 ||
130 asprintf(&dmCipherSpec,"%s-%s",hdr->cipherName, hdr->cipherMode) == -1) {
135 signal(SIGINT, sigint_handler);
138 r = setup_mapping(dmCipherSpec, name, device,
139 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, 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,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,
184 struct crypt_device *ctx)
186 return LUKS_endec_template(src,srcLength,hdr,vk, device,
187 sector, write_blockwise, O_RDWR, ctx);
190 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
191 struct luks_phdr *hdr,
192 struct volume_key *vk,
195 struct crypt_device *ctx)
197 return LUKS_endec_template(dst,dstLength,hdr,vk, device,
198 sector, read_blockwise, O_RDONLY, ctx);