3bc9c33a261bced124cd38bc4653558d2ca759dd
[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  * Copyright (C) 2012-2014, Milan Broz
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include "luks.h"
27 #include "internal.h"
28
29 static void _error_hint(struct crypt_device *ctx, const char *device,
30                         const char *cipher, const char *mode, size_t keyLength)
31 {
32         char cipher_spec[MAX_CIPHER_LEN * 3];
33
34         if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, mode) < 0)
35                 return;
36
37         log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
38                         "Check that kernel supports %s cipher (check syslog for more info).\n"),
39                         device, cipher_spec);
40
41         if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
42                 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
43 }
44
45 static int LUKS_endec_template(char *src, size_t srcLength,
46                                const char *cipher, const char *cipher_mode,
47                                struct volume_key *vk,
48                                unsigned int sector,
49                                ssize_t (*func)(int, int, void *, size_t),
50                                int mode,
51                                struct crypt_device *ctx)
52 {
53         char name[PATH_MAX], path[PATH_MAX];
54         char cipher_spec[MAX_CIPHER_LEN * 3];
55         struct crypt_dm_active_device dmd = {
56                 .target = DM_CRYPT,
57                 .uuid   = NULL,
58                 .flags  = CRYPT_ACTIVATE_PRIVATE,
59                 .data_device = crypt_metadata_device(ctx),
60                 .u.crypt = {
61                         .cipher = cipher_spec,
62                         .vk     = vk,
63                         .offset = sector,
64                         .iv_offset = 0,
65                 }
66         };
67         int r, bsize, devfd = -1;
68
69         log_dbg("Using dmcrypt to access keyslot area.");
70
71         bsize = device_block_size(dmd.data_device);
72         if (bsize <= 0)
73                 return -EINVAL;
74
75         dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
76
77         if (mode == O_RDONLY)
78                 dmd.flags |= CRYPT_ACTIVATE_READONLY;
79
80         if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
81                 return -ENOMEM;
82         if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
83                 return -ENOMEM;
84         if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
85                 return -ENOMEM;
86
87         r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
88                                 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
89         if (r < 0) {
90                 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
91                         device_path(dmd.data_device));
92                 return -EIO;
93         }
94
95         if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
96                 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
97                         device_path(dmd.data_device));
98                 return -EACCES;
99         }
100
101         r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
102         if (r < 0) {
103                 if (r != -EACCES && r != -ENOTSUP)
104                         _error_hint(ctx, device_path(dmd.data_device),
105                                     cipher, cipher_mode, vk->keylength * 8);
106                 return -EIO;
107         }
108
109         devfd = open(path, mode | O_DIRECT | O_SYNC);
110         if (devfd == -1) {
111                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
112                 r = -EIO;
113                 goto out;
114         }
115
116         r = func(devfd, bsize, src, srcLength);
117         if (r < 0) {
118                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
119                 r = -EIO;
120         } else
121                 r = 0;
122  out:
123         if(devfd != -1)
124                 close(devfd);
125         dm_remove_device(ctx, name, 1, dmd.size);
126         return r;
127 }
128
129 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
130                             const char *cipher,
131                             const char *cipher_mode,
132                             struct volume_key *vk,
133                             unsigned int sector,
134                             struct crypt_device *ctx)
135 {
136
137         struct device *device = crypt_metadata_device(ctx);
138         struct crypt_storage *s;
139         int devfd = -1, bsize, r = 0;
140
141         /* Only whole sector writes supported */
142         if (srcLength % SECTOR_SIZE)
143                 return -EINVAL;
144
145         /* Encrypt buffer */
146         r = crypt_storage_init(&s, 0, cipher, cipher_mode, vk->key, vk->keylength);
147
148         if (r)
149                 log_dbg("Userspace crypto wrapper cannot use %s-%s (%d).",
150                         cipher, cipher_mode, r);
151
152         /* Fallback to old temporary dmcrypt device */
153         if (r == -ENOTSUP || r == -ENOENT)
154                 return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
155                                            vk, sector, write_blockwise, O_RDWR, ctx);
156
157         if (r) {
158                 _error_hint(ctx, device_path(device), cipher, cipher_mode,
159                             vk->keylength * 8);
160                 return r;
161         }
162
163         log_dbg("Using userspace crypto wrapper to access keyslot area.");
164
165         r = crypt_storage_encrypt(s, 0, srcLength / SECTOR_SIZE, src);
166         crypt_storage_destroy(s);
167
168         if (r)
169                 return r;
170
171         r = -EIO;
172
173         /* Write buffer to device */
174         bsize = device_block_size(device);
175         if (bsize <= 0)
176                 goto out;
177
178         devfd = device_open(device, O_RDWR);
179         if (devfd == -1)
180                 goto out;
181
182         if (lseek(devfd, sector * SECTOR_SIZE, SEEK_SET) == -1 ||
183             write_blockwise(devfd, bsize, src, srcLength) == -1)
184                 goto out;
185
186         r = 0;
187 out:
188         if(devfd != -1)
189                 close(devfd);
190         if (r)
191                 log_err(ctx, _("IO error while encrypting keyslot.\n"));
192
193         return r;
194 }
195
196 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
197                               const char *cipher,
198                               const char *cipher_mode,
199                               struct volume_key *vk,
200                               unsigned int sector,
201                               struct crypt_device *ctx)
202 {
203         struct device *device = crypt_metadata_device(ctx);
204         struct crypt_storage *s;
205         int devfd = -1, bsize, r = 0;
206
207         /* Only whole sector reads supported */
208         if (dstLength % SECTOR_SIZE)
209                 return -EINVAL;
210
211         r = crypt_storage_init(&s, 0, cipher, cipher_mode, vk->key, vk->keylength);
212
213         if (r)
214                 log_dbg("Userspace crypto wrapper cannot use %s-%s (%d).",
215                         cipher, cipher_mode, r);
216
217         /* Fallback to old temporary dmcrypt device */
218         if (r == -ENOTSUP || r == -ENOENT)
219                 return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
220                                            vk, sector, read_blockwise, O_RDONLY, ctx);
221
222         if (r) {
223                 _error_hint(ctx, device_path(device), cipher, cipher_mode,
224                             vk->keylength * 8);
225                 return r;
226         }
227
228         log_dbg("Using userspace crypto wrapper to access keyslot area.");
229
230         r = -EIO;
231
232         /* Read buffer from device */
233         bsize = device_block_size(device);
234         if (bsize <= 0)
235                 goto bad;
236
237         devfd = device_open(device, O_RDONLY);
238         if (devfd == -1)
239                 goto bad;
240
241         if (lseek(devfd, sector * SECTOR_SIZE, SEEK_SET) == -1 ||
242             read_blockwise(devfd, bsize, dst, dstLength) == -1)
243                 goto bad;
244
245         close(devfd);
246
247         /* Decrypt buffer */
248         r = crypt_storage_decrypt(s, 0, dstLength / SECTOR_SIZE, dst);
249         crypt_storage_destroy(s);
250
251         return r;
252 bad:
253         if(devfd != -1)
254                 close(devfd);
255
256         log_err(ctx, _("IO error while decrypting keyslot.\n"));
257         crypt_storage_destroy(s);
258
259         return r;
260 }