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