Fix FSF address in license text according to
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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                          struct volume_key *vk,
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                 .vk     = vk,
64                 .offset = sector,
65                 .iv_offset = 0,
66                 .size   = 0,
67                 .flags  = (mode == O_RDONLY) ? CRYPT_ACTIVATE_READONLY : 0
68         };
69
70         /*
71          * we need to round this to nearest multiple of the underlying
72          * device's sector size, otherwise the mapping will be refused.
73          */
74         if(device_sector_size < 0) {
75                 log_err(ctx, _("Unable to obtain sector size for %s"), device);
76                 return -EINVAL;
77         }
78
79         dmd.size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
80         cleaner_size = dmd.size;
81
82         return dm_create_device(name, "TEMP", &dmd, 0);
83 }
84
85 static void sigint_handler(int sig __attribute__((unused)))
86 {
87         if(devfd >= 0)
88                 close(devfd);
89         devfd = -1;
90         if(cleaner_name)
91                 dm_remove_device(cleaner_name, 1, cleaner_size);
92
93         signal(SIGINT, SIG_DFL);
94         kill(getpid(), SIGINT);
95 }
96
97 static const char *_error_hint(char *cipherMode, size_t keyLength)
98 {
99         const char *hint= "";
100
101         if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
102                 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
103
104         return hint;
105 }
106
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,
112                                const char *device,
113                                unsigned int sector,
114                                ssize_t (*func)(int, void *, size_t),
115                                int mode,
116                                struct crypt_device *ctx)
117 {
118         char *name = NULL;
119         char *fullpath = NULL;
120         char *dmCipherSpec = NULL;
121         const char *dmDir = dm_get_dir();
122         int r = -1;
123
124         if(dmDir == NULL) {
125                 log_err(ctx, _("Failed to obtain device mapper directory."));
126                 return -1;
127         }
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) {
131                 r = -ENOMEM;
132                 goto out1;
133         }
134
135         signal(SIGINT, sigint_handler);
136         cleaner_name = name;
137
138         r = setup_mapping(dmCipherSpec, name, device,
139                           vk, sector, srcLength, mode, ctx);
140         if(r < 0) {
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));
145                 r = -EIO;
146                 goto out1;
147         }
148
149         devfd = open(fullpath, mode | O_DIRECT | O_SYNC);  /* devfd is a global var */
150         if(devfd == -1) {
151                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
152                 r = -EIO;
153                 goto out2;
154         }
155
156         r = func(devfd,src,srcLength);
157         if(r < 0) {
158                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
159                 r = -EIO;
160                 goto out3;
161         }
162
163         r = 0;
164  out3:
165         close(devfd);
166         devfd = -1;
167  out2:
168         dm_remove_device(cleaner_name, 1, cleaner_size);
169  out1:
170         signal(SIGINT, SIG_DFL);
171         cleaner_name = NULL;
172         cleaner_size = 0;
173         free(dmCipherSpec);
174         free(fullpath);
175         free(name);
176         return r;
177 }
178
179 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
180                             struct luks_phdr *hdr,
181                             struct volume_key *vk,
182                             const char *device,
183                             unsigned int sector,
184                             struct crypt_device *ctx)
185 {
186         return LUKS_endec_template(src,srcLength,hdr,vk, device,
187                                    sector, write_blockwise, O_RDWR, ctx);
188 }
189
190 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
191                               struct luks_phdr *hdr,
192                               struct volume_key *vk,
193                               const char *device,
194                               unsigned int sector,
195                               struct crypt_device *ctx)
196 {
197         return LUKS_endec_template(dst,dstLength,hdr,vk, device,
198                                    sector, read_blockwise, O_RDONLY, ctx);
199 }