Rewrite dm query/create function backend.
[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  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <inttypes.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/utsname.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <signal.h>
34
35 #include "luks.h"
36 #include "internal.h"
37
38 #define div_round_up(a,b) ({          \
39         typeof(a) __a = (a);          \
40         typeof(b) __b = (b);          \
41         (__a - 1) / __b + 1;          \
42 })
43
44 static inline int round_up_modulo(int x, int m) {
45         return div_round_up(x, m) * m;
46 }
47
48 static const char *cleaner_name=NULL;
49 static uint64_t cleaner_size = 0;
50 static int devfd=-1;
51
52 static int setup_mapping(const char *cipher, const char *name,
53                          const char *device,
54                          const char *key, size_t keyLength,
55                          unsigned int sector, size_t srcLength,
56                          int mode, struct crypt_device *ctx)
57 {
58         int device_sector_size = sector_size_for_device(device);
59         struct crypt_dm_active_device dmd = {
60                 .device = device,
61                 .cipher = cipher,
62                 .uuid   = NULL,
63                 .key    = (char*)key,
64                 .key_size = keyLength,
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 #ifdef __linux__
102         char c, tmp[4] = {0};
103         struct utsname uts;
104         int i = 0, kernel_minor;
105
106         /* Nothing to suggest here */
107         if (uname(&uts) || strncmp(uts.release, "2.6.", 4))
108                 return hint;
109
110         /* Get kernel minor without suffixes */
111         while (i < 3 && (c = uts.release[i + 4]))
112                 tmp[i++] = isdigit(c) ? c : '\0';
113         kernel_minor = atoi(tmp);
114
115         if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
116                 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
117         else if (!strncmp(cipherMode, "xts", 3) && kernel_minor < 24)
118                 hint = _("Block mode XTS is available since kernel 2.6.24.\n");
119         if (!strncmp(cipherMode, "lrw", 3) && (keyLength != 256 && keyLength != 512))
120                 hint = _("Key size in LRW mode must be 256 or 512 bits.\n");
121         else if (!strncmp(cipherMode, "lrw", 3) && kernel_minor < 20)
122                 hint = _("Block mode LRW is available since kernel 2.6.20.\n");
123 #endif
124         return hint;
125 }
126
127 /* This function is not reentrant safe, as it installs a signal
128    handler and global vars for cleaning */
129 static int LUKS_endec_template(char *src, size_t srcLength,
130                                struct luks_phdr *hdr,
131                                char *key, size_t keyLength,
132                                const char *device,
133                                unsigned int sector,
134                                ssize_t (*func)(int, void *, size_t),
135                                int mode,
136                                struct crypt_device *ctx)
137 {
138         char *name = NULL;
139         char *fullpath = NULL;
140         char *dmCipherSpec = NULL;
141         const char *dmDir = dm_get_dir();
142         int r = -1;
143
144         if(dmDir == NULL) {
145                 log_err(ctx, _("Failed to obtain device mapper directory."));
146                 return -1;
147         }
148         if(asprintf(&name,"temporary-cryptsetup-%d",getpid())               == -1 ||
149            asprintf(&fullpath,"%s/%s",dmDir,name)                           == -1 ||
150            asprintf(&dmCipherSpec,"%s-%s",hdr->cipherName, hdr->cipherMode) == -1) {
151                 r = -ENOMEM;
152                 goto out1;
153         }
154
155         signal(SIGINT, sigint_handler);
156         cleaner_name = name;
157
158         r = setup_mapping(dmCipherSpec, name, device,
159                           key, keyLength, sector, srcLength, mode, ctx);
160         if(r < 0) {
161                 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
162                         "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
163                         device, dmCipherSpec,
164                         _error_hint(hdr->cipherMode, keyLength * 8));
165                 r = -EIO;
166                 goto out1;
167         }
168
169         devfd = open(fullpath, mode | O_DIRECT | O_SYNC);  /* devfd is a global var */
170         if(devfd == -1) {
171                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
172                 r = -EIO;
173                 goto out2;
174         }
175
176         r = func(devfd,src,srcLength);
177         if(r < 0) {
178                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
179                 r = -EIO;
180                 goto out3;
181         }
182
183         r = 0;
184  out3:
185         close(devfd);
186         devfd = -1;
187  out2:
188         dm_remove_device(cleaner_name, 1, cleaner_size);
189  out1:
190         signal(SIGINT, SIG_DFL);
191         cleaner_name = NULL;
192         cleaner_size = 0;
193         free(dmCipherSpec);
194         free(fullpath);
195         free(name);
196         return r;
197 }
198
199 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
200                             struct luks_phdr *hdr,
201                             char *key, size_t keyLength,
202                             const char *device,
203                             unsigned int sector,
204                             struct crypt_device *ctx)
205 {
206         return LUKS_endec_template(src,srcLength,hdr,key,keyLength, device,
207                                    sector, write_blockwise, O_RDWR, ctx);
208 }
209
210 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
211                               struct luks_phdr *hdr,
212                               char *key, size_t keyLength,
213                               const char *device,
214                               unsigned int sector,
215                               struct crypt_device *ctx)
216 {
217         return LUKS_endec_template(dst,dstLength,hdr,key,keyLength, device,
218                                    sector, read_blockwise, O_RDONLY, ctx);
219 }