Merge branch 'vendor/OPENSSL' into HEAD
[dragonfly.git] / contrib / cryptsetup / luks / 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 "../lib/libcryptsetup.h"
37 #include "../lib/internal.h"
38 //#include "../lib/blockdev.h"
39
40 #define div_round_up(a,b) ({          \
41         typeof(a) __a = (a);          \
42         typeof(b) __b = (b);          \
43         (__a - 1) / __b + 1;          \
44 })
45
46 static inline int round_up_modulo(int x, int m) {
47         return div_round_up(x, m) * m;
48 }
49
50 static const char *cleaner_name=NULL;
51 static uint64_t cleaner_size = 0;
52 static int devfd=-1;
53
54 static int setup_mapping(const char *cipher, const char *name,
55                          const char *device, unsigned int payloadOffset,
56                          const char *key, size_t keyLength,
57                          unsigned int sector, size_t srcLength,
58                          int mode, struct crypt_device *ctx)
59 {
60         int device_sector_size = sector_size_for_device(device);
61         uint64_t size;
62
63         /*
64          * we need to round this to nearest multiple of the underlying
65          * device's sector size, otherwise the mapping will be refused.
66          */
67         if(device_sector_size < 0) {
68                 log_err(ctx, _("Unable to obtain sector size for %s"), device);
69                 return -EINVAL;
70         }
71         size = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE;
72         cleaner_size = size;
73
74         return dm_create_device(name, device, cipher, "TEMP", NULL, size, 0, sector,
75                                 keyLength, key, (mode == O_RDONLY), 0);
76 }
77
78 static void sigint_handler(int sig)
79 {
80         if(devfd >= 0)
81                 close(devfd);
82         devfd = -1;
83         if(cleaner_name) 
84                 dm_remove_device(cleaner_name, 1, cleaner_size);
85
86         signal(SIGINT, SIG_DFL);
87         kill(getpid(), SIGINT);
88 }
89
90 static char *_error_hint(char *cipherName, char *cipherMode, size_t keyLength)
91 {
92         char *hint = "";
93 #ifdef __linux__
94         char c, tmp[4] = {0};
95         struct utsname uts;
96         int i = 0, kernel_minor;
97
98         /* Nothing to suggest here */
99         if (uname(&uts) || strncmp(uts.release, "2.6.", 4))
100                 return hint;
101
102         /* Get kernel minor without suffixes */
103         while (i < 3 && (c = uts.release[i + 4]))
104                 tmp[i++] = isdigit(c) ? c : '\0';
105         kernel_minor = atoi(tmp);
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         else if (!strncmp(cipherMode, "xts", 3) && kernel_minor < 24)
110                 hint = _("Block mode XTS is available since kernel 2.6.24.\n");
111         if (!strncmp(cipherMode, "lrw", 3) && (keyLength != 256 && keyLength != 512))
112                 hint = _("Key size in LRW mode must be 256 or 512 bits.\n");
113         else if (!strncmp(cipherMode, "lrw", 3) && kernel_minor < 20)
114                 hint = _("Block mode LRW is available since kernel 2.6.20.\n");
115 #endif
116         return hint;
117 }
118
119 /* This function is not reentrant safe, as it installs a signal
120    handler and global vars for cleaning */
121 static int LUKS_endec_template(char *src, size_t srcLength, 
122                                struct luks_phdr *hdr, 
123                                char *key, size_t keyLength, 
124                                const char *device, 
125                                unsigned int sector,
126                                ssize_t (*func)(int, void *, size_t),
127                                int mode,
128                                struct crypt_device *ctx)
129 {
130         char *name = NULL;
131         char *fullpath = NULL;
132         char *dmCipherSpec = NULL;
133         const char *dmDir = dm_get_dir(); 
134         int r = -1;
135
136         if(dmDir == NULL) {
137                 log_err(ctx, _("Failed to obtain device mapper directory."));
138                 return -1;
139         }
140         if(asprintf(&name,"temporary-cryptsetup-%d",getpid())               == -1 ||
141            asprintf(&fullpath,"%s/%s",dmDir,name)                           == -1 ||
142            asprintf(&dmCipherSpec,"%s-%s",hdr->cipherName, hdr->cipherMode) == -1) {
143                 r = -ENOMEM;
144                 goto out1;
145         }
146
147         signal(SIGINT, sigint_handler);
148         cleaner_name = name;
149
150         r = setup_mapping(dmCipherSpec, name, device, hdr->payloadOffset,
151                           key, keyLength, sector, srcLength, mode, ctx);
152         if(r < 0) {
153                 log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
154                         "Check that kernel supports %s cipher (check syslog for more info).\n%s"),
155                         device, dmCipherSpec,
156                         _error_hint(hdr->cipherName, hdr->cipherMode, keyLength * 8));
157                 r = -EIO;
158                 goto out1;
159         }
160
161         devfd = open(fullpath, mode | O_DIRECT | O_SYNC);  /* devfd is a global var */
162         if(devfd == -1) {
163                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
164                 r = -EIO;
165                 goto out2;
166         }
167
168         r = func(devfd,src,srcLength);
169         if(r < 0) {
170                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
171                 r = -EIO;
172                 goto out3;
173         }
174
175         r = 0;
176  out3:
177         close(devfd);
178         devfd = -1;
179  out2:
180         dm_remove_device(cleaner_name, 1, cleaner_size);
181  out1:
182         signal(SIGINT, SIG_DFL);
183         cleaner_name = NULL;
184         cleaner_size = 0;
185         free(dmCipherSpec);
186         free(fullpath);
187         free(name);
188         return r;
189 }
190
191 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
192                             struct luks_phdr *hdr,
193                             char *key, size_t keyLength,
194                             const char *device,
195                             unsigned int sector,
196                             struct crypt_device *ctx)
197 {
198         return LUKS_endec_template(src,srcLength,hdr,key,keyLength, device, sector,
199                                    (ssize_t (*)(int, void *, size_t)) write_blockwise,
200                                    O_RDWR, ctx);
201 }
202
203 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
204                               struct luks_phdr *hdr,
205                               char *key, size_t keyLength,
206                               const char *device,
207                               unsigned int sector,
208                               struct crypt_device *ctx)
209 {
210         return LUKS_endec_template(dst,dstLength,hdr,key,keyLength, device,
211                                    sector, read_blockwise, O_RDONLY, ctx);
212 }