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 CRYPTO_TFM_REQ_MAY_SLEEP
);
62 else if (strcmp(mode
, "ecb") == 0)
63 tfm
= crypto_alloc_tfm(cipher
, CRYPTO_TFM_MODE_ECB
|
64 CRYPTO_TFM_REQ_MAY_SLEEP
);
68 err
= tfm
->crt_u
.cipher
.cit_setkey(tfm
, info
->lo_encrypt_key
,
69 info
->lo_encrypt_key_size
);
85 typedef int (*encdec_ecb_t
)(struct crypto_tfm
*tfm
,
86 struct scatterlist
*sg_out
,
87 struct scatterlist
*sg_in
,
92 cryptoloop_transfer_ecb(struct loop_device
*lo
, int cmd
,
93 struct page
*raw_page
, unsigned raw_off
,
94 struct page
*loop_page
, unsigned loop_off
,
95 int size
, sector_t IV
)
97 struct crypto_tfm
*tfm
= (struct crypto_tfm
*) lo
->key_data
;
98 struct scatterlist sg_out
= { NULL
, };
99 struct scatterlist sg_in
= { NULL
, };
101 encdec_ecb_t encdecfunc
;
102 struct page
*in_page
, *out_page
;
103 unsigned in_offs
, out_offs
;
108 out_page
= loop_page
;
110 encdecfunc
= tfm
->crt_u
.cipher
.cit_decrypt
;
116 encdecfunc
= tfm
->crt_u
.cipher
.cit_encrypt
;
120 const int sz
= min(size
, LOOP_IV_SECTOR_SIZE
);
122 sg_in
.page
= in_page
;
123 sg_in
.offset
= in_offs
;
126 sg_out
.page
= out_page
;
127 sg_out
.offset
= out_offs
;
130 encdecfunc(tfm
, &sg_out
, &sg_in
, sz
);
140 typedef int (*encdec_cbc_t
)(struct crypto_tfm
*tfm
,
141 struct scatterlist
*sg_out
,
142 struct scatterlist
*sg_in
,
143 unsigned int nsg
, u8
*iv
);
146 cryptoloop_transfer_cbc(struct loop_device
*lo
, int cmd
,
147 struct page
*raw_page
, unsigned raw_off
,
148 struct page
*loop_page
, unsigned loop_off
,
149 int size
, sector_t IV
)
151 struct crypto_tfm
*tfm
= (struct crypto_tfm
*) lo
->key_data
;
152 struct scatterlist sg_out
= { NULL
, };
153 struct scatterlist sg_in
= { NULL
, };
155 encdec_cbc_t encdecfunc
;
156 struct page
*in_page
, *out_page
;
157 unsigned in_offs
, out_offs
;
162 out_page
= loop_page
;
164 encdecfunc
= tfm
->crt_u
.cipher
.cit_decrypt_iv
;
170 encdecfunc
= tfm
->crt_u
.cipher
.cit_encrypt_iv
;
174 const int sz
= min(size
, LOOP_IV_SECTOR_SIZE
);
176 iv
[0] = cpu_to_le32(IV
& 0xffffffff);
178 sg_in
.page
= in_page
;
179 sg_in
.offset
= in_offs
;
182 sg_out
.page
= out_page
;
183 sg_out
.offset
= out_offs
;
186 encdecfunc(tfm
, &sg_out
, &sg_in
, sz
, (u8
*)iv
);
198 cryptoloop_transfer(struct loop_device
*lo
, int cmd
,
199 struct page
*raw_page
, unsigned raw_off
,
200 struct page
*loop_page
, unsigned loop_off
,
201 int size
, sector_t IV
)
203 struct crypto_tfm
*tfm
= (struct crypto_tfm
*) lo
->key_data
;
204 if(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
)
206 lo
->transfer
= cryptoloop_transfer_ecb
;
207 return cryptoloop_transfer_ecb(lo
, cmd
, raw_page
, raw_off
,
208 loop_page
, loop_off
, size
, IV
);
210 if(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_CBC
)
212 lo
->transfer
= cryptoloop_transfer_cbc
;
213 return cryptoloop_transfer_cbc(lo
, cmd
, raw_page
, raw_off
,
214 loop_page
, loop_off
, size
, IV
);
217 /* This is not supposed to happen */
219 printk( KERN_ERR
"cryptoloop: unsupported cipher mode in cryptoloop_transfer!\n");
224 cryptoloop_ioctl(struct loop_device
*lo
, int cmd
, unsigned long arg
)
230 cryptoloop_release(struct loop_device
*lo
)
232 struct crypto_tfm
*tfm
= (struct crypto_tfm
*) lo
->key_data
;
234 crypto_free_tfm(tfm
);
238 printk(KERN_ERR
"cryptoloop_release(): tfm == NULL?\n");
242 static struct loop_func_table cryptoloop_funcs
= {
243 .number
= LO_CRYPT_CRYPTOAPI
,
244 .init
= cryptoloop_init
,
245 .ioctl
= cryptoloop_ioctl
,
246 .transfer
= cryptoloop_transfer
,
247 .release
= cryptoloop_release
,
252 init_cryptoloop(void)
254 int rc
= loop_register_transfer(&cryptoloop_funcs
);
257 printk(KERN_ERR
"cryptoloop: loop_register_transfer failed\n");
262 cleanup_cryptoloop(void)
264 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI
))
266 "cryptoloop: loop_unregister_transfer failed\n");
269 module_init(init_cryptoloop
);
270 module_exit(cleanup_cryptoloop
);