]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/block/cryptoloop.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / drivers / block / cryptoloop.c
1 /*
2 Linux loop encryption enabling module
3
4 Copyright (C) 2002 Herbert Valerio Riedel <hvr@gnu.org>
5 Copyright (C) 2003 Fruhwirth Clemens <clemens@endorphin.org>
6
7 This module is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This module is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this module; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <linux/module.h>
23
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/crypto.h>
27 #include <linux/blkdev.h>
28 #include <linux/loop.h>
29 #include <asm/semaphore.h>
30 #include <asm/uaccess.h>
31
32 MODULE_LICENSE("GPL");
33 MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
34 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
35
36 #define LOOP_IV_SECTOR_BITS 9
37 #define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
38
39 static int
40 cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
41 {
42 int err = -EINVAL;
43 char cms[LO_NAME_SIZE]; /* cipher-mode string */
44 char *cipher;
45 char *mode;
46 char *cmsp = cms; /* c-m string pointer */
47 struct crypto_tfm *tfm = NULL;
48
49 /* encryption breaks for non sector aligned offsets */
50
51 if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
52 goto out;
53
54 strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
55 cms[LO_NAME_SIZE - 1] = 0;
56 cipher = strsep(&cmsp, "-");
57 mode = strsep(&cmsp, "-");
58
59 if (mode == NULL || strcmp(mode, "cbc") == 0)
60 tfm = crypto_alloc_tfm(cipher, CRYPTO_TFM_MODE_CBC);
61 else if (strcmp(mode, "ecb") == 0)
62 tfm = crypto_alloc_tfm(cipher, CRYPTO_TFM_MODE_ECB);
63 if (tfm == NULL)
64 return -EINVAL;
65
66 err = tfm->crt_u.cipher.cit_setkey(tfm, info->lo_encrypt_key,
67 info->lo_encrypt_key_size);
68
69 if (err != 0)
70 goto out_free_tfm;
71
72 lo->key_data = tfm;
73 return 0;
74
75 out_free_tfm:
76 crypto_free_tfm(tfm);
77
78 out:
79 return err;
80 }
81
82
83 typedef int (*encdec_ecb_t)(struct crypto_tfm *tfm,
84 struct scatterlist *sg_out,
85 struct scatterlist *sg_in,
86 unsigned int nsg);
87
88
89 static int
90 cryptoloop_transfer_ecb(struct loop_device *lo, int cmd,
91 struct page *raw_page, unsigned raw_off,
92 struct page *loop_page, unsigned loop_off,
93 int size, sector_t IV)
94 {
95 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
96 struct scatterlist sg_out = { NULL, };
97 struct scatterlist sg_in = { NULL, };
98
99 encdec_ecb_t encdecfunc;
100 struct page *in_page, *out_page;
101 unsigned in_offs, out_offs;
102
103 if (cmd == READ) {
104 in_page = raw_page;
105 in_offs = raw_off;
106 out_page = loop_page;
107 out_offs = loop_off;
108 encdecfunc = tfm->crt_u.cipher.cit_decrypt;
109 } else {
110 in_page = loop_page;
111 in_offs = loop_off;
112 out_page = raw_page;
113 out_offs = raw_off;
114 encdecfunc = tfm->crt_u.cipher.cit_encrypt;
115 }
116
117 while (size > 0) {
118 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
119
120 sg_in.page = in_page;
121 sg_in.offset = in_offs;
122 sg_in.length = sz;
123
124 sg_out.page = out_page;
125 sg_out.offset = out_offs;
126 sg_out.length = sz;
127
128 encdecfunc(tfm, &sg_out, &sg_in, sz);
129
130 size -= sz;
131 in_offs += sz;
132 out_offs += sz;
133 }
134
135 return 0;
136 }
137
138 typedef int (*encdec_cbc_t)(struct crypto_tfm *tfm,
139 struct scatterlist *sg_out,
140 struct scatterlist *sg_in,
141 unsigned int nsg, u8 *iv);
142
143 static int
144 cryptoloop_transfer_cbc(struct loop_device *lo, int cmd,
145 struct page *raw_page, unsigned raw_off,
146 struct page *loop_page, unsigned loop_off,
147 int size, sector_t IV)
148 {
149 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
150 struct scatterlist sg_out = { NULL, };
151 struct scatterlist sg_in = { NULL, };
152
153 encdec_cbc_t encdecfunc;
154 struct page *in_page, *out_page;
155 unsigned in_offs, out_offs;
156
157 if (cmd == READ) {
158 in_page = raw_page;
159 in_offs = raw_off;
160 out_page = loop_page;
161 out_offs = loop_off;
162 encdecfunc = tfm->crt_u.cipher.cit_decrypt_iv;
163 } else {
164 in_page = loop_page;
165 in_offs = loop_off;
166 out_page = raw_page;
167 out_offs = raw_off;
168 encdecfunc = tfm->crt_u.cipher.cit_encrypt_iv;
169 }
170
171 while (size > 0) {
172 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
173 u32 iv[4] = { 0, };
174 iv[0] = cpu_to_le32(IV & 0xffffffff);
175
176 sg_in.page = in_page;
177 sg_in.offset = in_offs;
178 sg_in.length = sz;
179
180 sg_out.page = out_page;
181 sg_out.offset = out_offs;
182 sg_out.length = sz;
183
184 encdecfunc(tfm, &sg_out, &sg_in, sz, (u8 *)iv);
185
186 IV++;
187 size -= sz;
188 in_offs += sz;
189 out_offs += sz;
190 }
191
192 return 0;
193 }
194
195 static int
196 cryptoloop_transfer(struct loop_device *lo, int cmd,
197 struct page *raw_page, unsigned raw_off,
198 struct page *loop_page, unsigned loop_off,
199 int size, sector_t IV)
200 {
201 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
202 if(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB)
203 {
204 lo->transfer = cryptoloop_transfer_ecb;
205 return cryptoloop_transfer_ecb(lo, cmd, raw_page, raw_off,
206 loop_page, loop_off, size, IV);
207 }
208 if(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_CBC)
209 {
210 lo->transfer = cryptoloop_transfer_cbc;
211 return cryptoloop_transfer_cbc(lo, cmd, raw_page, raw_off,
212 loop_page, loop_off, size, IV);
213 }
214
215 /* This is not supposed to happen */
216
217 printk( KERN_ERR "cryptoloop: unsupported cipher mode in cryptoloop_transfer!\n");
218 return -EINVAL;
219 }
220
221 static int
222 cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
223 {
224 return -EINVAL;
225 }
226
227 static int
228 cryptoloop_release(struct loop_device *lo)
229 {
230 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
231 if (tfm != NULL) {
232 crypto_free_tfm(tfm);
233 lo->key_data = NULL;
234 return 0;
235 }
236 printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
237 return -EINVAL;
238 }
239
240 static struct loop_func_table cryptoloop_funcs = {
241 .number = LO_CRYPT_CRYPTOAPI,
242 .init = cryptoloop_init,
243 .ioctl = cryptoloop_ioctl,
244 .transfer = cryptoloop_transfer,
245 .release = cryptoloop_release,
246 .owner = THIS_MODULE
247 };
248
249 static int __init
250 init_cryptoloop(void)
251 {
252 int rc = loop_register_transfer(&cryptoloop_funcs);
253
254 if (rc)
255 printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
256 return rc;
257 }
258
259 static void __exit
260 cleanup_cryptoloop(void)
261 {
262 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
263 printk(KERN_ERR
264 "cryptoloop: loop_unregister_transfer failed\n");
265 }
266
267 module_init(init_cryptoloop);
268 module_exit(cleanup_cryptoloop);