2 Linux loop encryption enabling module
4 Copyright (C) 2002 Herbert Valerio Riedel <hvr@gnu.org>
5 Copyright (C) 2003 Fruhwirth Clemens <clemens@endorphin.org>
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.
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.
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
22 #include <linux/module.h>
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>
32 MODULE_LICENSE("GPL");
33 MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
34 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
36 #define LOOP_IV_SECTOR_BITS 9
37 #define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
40 cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
43 char cms[LO_NAME_SIZE]; /* cipher-mode string */
46 char *cmsp = cms; /* c-m string pointer */
47 struct crypto_tfm *tfm = NULL;
49 /* encryption breaks for non sector aligned offsets */
51 if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
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, "-");
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);
66 err = tfm->crt_u.cipher.cit_setkey(tfm, info->lo_encrypt_key,
67 info->lo_encrypt_key_size);
83 typedef int (*encdec_ecb_t)(struct crypto_tfm *tfm,
84 struct scatterlist *sg_out,
85 struct scatterlist *sg_in,
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)
95 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
96 struct scatterlist sg_out = { NULL, };
97 struct scatterlist sg_in = { NULL, };
99 encdec_ecb_t encdecfunc;
100 struct page *in_page, *out_page;
101 unsigned in_offs, out_offs;
106 out_page = loop_page;
108 encdecfunc = tfm->crt_u.cipher.cit_decrypt;
114 encdecfunc = tfm->crt_u.cipher.cit_encrypt;
118 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
120 sg_in.page = in_page;
121 sg_in.offset = in_offs;
124 sg_out.page = out_page;
125 sg_out.offset = out_offs;
128 encdecfunc(tfm, &sg_out, &sg_in, sz);
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);
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)
149 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
150 struct scatterlist sg_out = { NULL, };
151 struct scatterlist sg_in = { NULL, };
153 encdec_cbc_t encdecfunc;
154 struct page *in_page, *out_page;
155 unsigned in_offs, out_offs;
160 out_page = loop_page;
162 encdecfunc = tfm->crt_u.cipher.cit_decrypt_iv;
168 encdecfunc = tfm->crt_u.cipher.cit_encrypt_iv;
172 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
174 iv[0] = cpu_to_le32(IV & 0xffffffff);
176 sg_in.page = in_page;
177 sg_in.offset = in_offs;
180 sg_out.page = out_page;
181 sg_out.offset = out_offs;
184 encdecfunc(tfm, &sg_out, &sg_in, sz, (u8 *)iv);
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)
201 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
202 if(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB)
204 lo->transfer = cryptoloop_transfer_ecb;
205 return cryptoloop_transfer_ecb(lo, cmd, raw_page, raw_off,
206 loop_page, loop_off, size, IV);
208 if(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_CBC)
210 lo->transfer = cryptoloop_transfer_cbc;
211 return cryptoloop_transfer_cbc(lo, cmd, raw_page, raw_off,
212 loop_page, loop_off, size, IV);
215 /* This is not supposed to happen */
217 printk( KERN_ERR "cryptoloop: unsupported cipher mode in cryptoloop_transfer!\n");
222 cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
228 cryptoloop_release(struct loop_device *lo)
230 struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
232 crypto_free_tfm(tfm);
236 printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
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,
250 init_cryptoloop(void)
252 int rc = loop_register_transfer(&cryptoloop_funcs);
255 printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
260 cleanup_cryptoloop(void)
262 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
264 "cryptoloop: loop_unregister_transfer failed\n");
267 module_init(init_cryptoloop);
268 module_exit(cleanup_cryptoloop);