Allow "private" activation (skip some udev global rules) flag.
[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  = 0
69         };
70
71         dmd.flags = CRYPT_ACTIVATE_PRIVATE;
72         if (mode == O_RDONLY)
73                 dmd.flags |= CRYPT_ACTIVATE_READONLY;
74         /*
75          * we need to round this to nearest multiple of the underlying
76          * device's sector size, otherwise the mapping will be refused.
77          */
78         if(device_sector_size < 0) {
79                 log_err(ctx, _("Unable to obtain sector size for %s"), device);
80                 return -EINVAL;
81         }
82
83         dmd.size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
84         cleaner_size = dmd.size;
85
86         return dm_create_device(name, "TEMP", &dmd, 0);
87 }
88
89 static void sigint_handler(int sig __attribute__((unused)))
90 {
91         if(devfd >= 0)
92                 close(devfd);
93         devfd = -1;
94         if(cleaner_name)
95                 dm_remove_device(cleaner_name, 1, cleaner_size);
96
97         signal(SIGINT, SIG_DFL);
98         kill(getpid(), SIGINT);
99 }
100
101 static const char *_error_hint(char *cipherMode, size_t keyLength)
102 {
103         const char *hint= "";
104
105         if (!strncmp(cipherMode, "xts", 3) && (keyLength != 256 && keyLength != 512))
106                 hint = _("Key size in XTS mode must be 256 or 512 bits.\n");
107
108         return hint;
109 }
110
111 /* This function is not reentrant safe, as it installs a signal
112    handler and global vars for cleaning */
113 static int LUKS_endec_template(char *src, size_t srcLength,
114                                struct luks_phdr *hdr,
115                                struct volume_key *vk,
116                                const char *device,
117                                unsigned int sector,
118                                ssize_t (*func)(int, void *, size_t),
119                                int mode,
120                                struct crypt_device *ctx)
121 {
122         char *name = NULL;
123         char *fullpath = NULL;
124         char *dmCipherSpec = NULL;
125         const char *dmDir = dm_get_dir();
126         int r = -1;
127
128         if(dmDir == NULL) {
129                 log_err(ctx, _("Failed to obtain device mapper directory."));
130                 return -1;
131         }
132         if(asprintf(&name,"temporary-cryptsetup-%d",getpid())               == -1 ||
133            asprintf(&fullpath,"%s/%s",dmDir,name)                           == -1 ||
134            asprintf(&dmCipherSpec,"%s-%s",hdr->cipherName, hdr->cipherMode) == -1) {
135                 r = -ENOMEM;
136                 goto out1;
137         }
138
139         signal(SIGINT, sigint_handler);
140         cleaner_name = name;
141
142         r = setup_mapping(dmCipherSpec, name, device,
143                           vk, sector, srcLength, mode, ctx);
144         if(r < 0) {
145                 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
146                         "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
147                         device, dmCipherSpec,
148                         _error_hint(hdr->cipherMode, vk->keylength * 8));
149                 r = -EIO;
150                 goto out1;
151         }
152
153         devfd = open(fullpath, mode | O_DIRECT | O_SYNC);  /* devfd is a global var */
154         if(devfd == -1) {
155                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
156                 r = -EIO;
157                 goto out2;
158         }
159
160         r = func(devfd,src,srcLength);
161         if(r < 0) {
162                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
163                 r = -EIO;
164                 goto out3;
165         }
166
167         r = 0;
168  out3:
169         close(devfd);
170         devfd = -1;
171  out2:
172         dm_remove_device(cleaner_name, 1, cleaner_size);
173  out1:
174         signal(SIGINT, SIG_DFL);
175         cleaner_name = NULL;
176         cleaner_size = 0;
177         free(dmCipherSpec);
178         free(fullpath);
179         free(name);
180         return r;
181 }
182
183 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
184                             struct luks_phdr *hdr,
185                             struct volume_key *vk,
186                             const char *device,
187                             unsigned int sector,
188                             struct crypt_device *ctx)
189 {
190         return LUKS_endec_template(src,srcLength,hdr,vk, device,
191                                    sector, write_blockwise, O_RDWR, ctx);
192 }
193
194 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
195                               struct luks_phdr *hdr,
196                               struct volume_key *vk,
197                               const char *device,
198                               unsigned int sector,
199                               struct crypt_device *ctx)
200 {
201         return LUKS_endec_template(dst,dstLength,hdr,vk, device,
202                                    sector, read_blockwise, O_RDONLY, ctx);
203 }