Version 1.4.2.
[platform/upstream/cryptsetup.git] / lib / luks1 / keyencryption.c
1 /*
2  * LUKS - Linux Unified Key Setup
3  *
4  * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
6  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30 #include <sys/utsname.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <signal.h>
35
36 #include "luks.h"
37 #include "internal.h"
38
39 #define div_round_up(a,b) ({          \
40         typeof(a) __a = (a);          \
41         typeof(b) __b = (b);          \
42         (__a - 1) / __b + 1;          \
43 })
44
45 static inline int round_up_modulo(int x, int m) {
46         return div_round_up(x, m) * m;
47 }
48
49 static const char *cleaner_name=NULL;
50 static uint64_t cleaner_size = 0;
51 static int devfd=-1;
52
53 static int setup_mapping(const char *cipher, const char *name,
54                          const char *device,
55                          struct volume_key *vk,
56                          unsigned int sector, size_t srcLength,
57                          int mode, struct crypt_device *ctx)
58 {
59         int device_sector_size = sector_size_for_device(device);
60         struct crypt_dm_active_device dmd = {
61                 .device = device,
62                 .cipher = cipher,
63                 .uuid   = NULL,
64                 .vk     = vk,
65                 .offset = sector,
66                 .iv_offset = 0,
67                 .size   = 0,
68                 .flags  = (mode == O_RDONLY) ? CRYPT_ACTIVATE_READONLY : 0
69         };
70
71         /*
72          * we need to round this to nearest multiple of the underlying
73          * device's sector size, otherwise the mapping will be refused.
74          */
75         if(device_sector_size < 0) {
76                 log_err(ctx, _("Unable to obtain sector size for %s"), device);
77                 return -EINVAL;
78         }
79
80         dmd.size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
81         cleaner_size = dmd.size;
82
83         return dm_create_device(name, "TEMP", &dmd, 0);
84 }
85
86 static void sigint_handler(int sig __attribute__((unused)))
87 {
88         if(devfd >= 0)
89                 close(devfd);
90         devfd = -1;
91         if(cleaner_name)
92                 dm_remove_device(cleaner_name, 1, cleaner_size);
93
94         signal(SIGINT, SIG_DFL);
95         kill(getpid(), SIGINT);
96 }
97
98 static const char *_error_hint(char *cipherMode, size_t keyLength)
99 {
100         const char *hint= "";
101
102         if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
103                 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
104
105         return hint;
106 }
107
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,
113                                const char *device,
114                                unsigned int sector,
115                                ssize_t (*func)(int, void *, size_t),
116                                int mode,
117                                struct crypt_device *ctx)
118 {
119         char *name = NULL;
120         char *fullpath = NULL;
121         char *dmCipherSpec = NULL;
122         const char *dmDir = dm_get_dir();
123         int r = -1;
124
125         if(dmDir == NULL) {
126                 log_err(ctx, _("Failed to obtain device mapper directory."));
127                 return -1;
128         }
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) {
132                 r = -ENOMEM;
133                 goto out1;
134         }
135
136         signal(SIGINT, sigint_handler);
137         cleaner_name = name;
138
139         r = setup_mapping(dmCipherSpec, name, device,
140                           vk, sector, srcLength, mode, ctx);
141         if(r < 0) {
142                 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
143                         "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
144                         device, dmCipherSpec,
145                         _error_hint(hdr->cipherMode, vk->keylength * 8));
146                 r = -EIO;
147                 goto out1;
148         }
149
150         devfd = open(fullpath, mode | O_DIRECT | O_SYNC);  /* devfd is a global var */
151         if(devfd == -1) {
152                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
153                 r = -EIO;
154                 goto out2;
155         }
156
157         r = func(devfd,src,srcLength);
158         if(r < 0) {
159                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
160                 r = -EIO;
161                 goto out3;
162         }
163
164         r = 0;
165  out3:
166         close(devfd);
167         devfd = -1;
168  out2:
169         dm_remove_device(cleaner_name, 1, cleaner_size);
170  out1:
171         signal(SIGINT, SIG_DFL);
172         cleaner_name = NULL;
173         cleaner_size = 0;
174         free(dmCipherSpec);
175         free(fullpath);
176         free(name);
177         return r;
178 }
179
180 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
181                             struct luks_phdr *hdr,
182                             struct volume_key *vk,
183                             const char *device,
184                             unsigned int sector,
185                             struct crypt_device *ctx)
186 {
187         return LUKS_endec_template(src,srcLength,hdr,vk, device,
188                                    sector, write_blockwise, O_RDWR, ctx);
189 }
190
191 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
192                               struct luks_phdr *hdr,
193                               struct volume_key *vk,
194                               const char *device,
195                               unsigned int sector,
196                               struct crypt_device *ctx)
197 {
198         return LUKS_endec_template(dst,dstLength,hdr,vk, device,
199                                    sector, read_blockwise, O_RDONLY, ctx);
200 }