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 <crypto/skcipher.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/blkdev.h>
28 #include <linux/scatterlist.h>
29 #include <linux/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
)
45 char cms
[LO_NAME_SIZE
]; /* cipher-mode string */
47 char *cmsp
= cms
; /* c-m string pointer */
48 struct crypto_sync_skcipher
*tfm
;
50 /* encryption breaks for non sector aligned offsets */
52 if (info
->lo_offset
% LOOP_IV_SECTOR_SIZE
)
55 strncpy(cms
, info
->lo_crypt_name
, LO_NAME_SIZE
);
56 cms
[LO_NAME_SIZE
- 1] = 0;
58 cipher_len
= strcspn(cmsp
, "-");
60 mode
= cmsp
+ cipher_len
;
64 mode_len
= strcspn(mode
, "-");
72 if (cipher_len
+ mode_len
+ 3 > LO_NAME_SIZE
)
75 memmove(cms
, mode
, mode_len
);
76 cmsp
= cms
+ mode_len
;
78 memcpy(cmsp
, info
->lo_crypt_name
, cipher_len
);
83 tfm
= crypto_alloc_sync_skcipher(cms
, 0, 0);
87 err
= crypto_sync_skcipher_setkey(tfm
, info
->lo_encrypt_key
,
88 info
->lo_encrypt_key_size
);
97 crypto_free_sync_skcipher(tfm
);
104 typedef int (*encdec_cbc_t
)(struct skcipher_request
*req
);
107 cryptoloop_transfer(struct loop_device
*lo
, int cmd
,
108 struct page
*raw_page
, unsigned raw_off
,
109 struct page
*loop_page
, unsigned loop_off
,
110 int size
, sector_t IV
)
112 struct crypto_sync_skcipher
*tfm
= lo
->key_data
;
113 SYNC_SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
114 struct scatterlist sg_out
;
115 struct scatterlist sg_in
;
117 encdec_cbc_t encdecfunc
;
118 struct page
*in_page
, *out_page
;
119 unsigned in_offs
, out_offs
;
122 skcipher_request_set_sync_tfm(req
, tfm
);
123 skcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
,
126 sg_init_table(&sg_out
, 1);
127 sg_init_table(&sg_in
, 1);
132 out_page
= loop_page
;
134 encdecfunc
= crypto_skcipher_decrypt
;
140 encdecfunc
= crypto_skcipher_encrypt
;
144 const int sz
= min(size
, LOOP_IV_SECTOR_SIZE
);
146 iv
[0] = cpu_to_le32(IV
& 0xffffffff);
148 sg_set_page(&sg_in
, in_page
, sz
, in_offs
);
149 sg_set_page(&sg_out
, out_page
, sz
, out_offs
);
151 skcipher_request_set_crypt(req
, &sg_in
, &sg_out
, sz
, iv
);
152 err
= encdecfunc(req
);
165 skcipher_request_zero(req
);
170 cryptoloop_ioctl(struct loop_device
*lo
, int cmd
, unsigned long arg
)
176 cryptoloop_release(struct loop_device
*lo
)
178 struct crypto_sync_skcipher
*tfm
= lo
->key_data
;
180 crypto_free_sync_skcipher(tfm
);
184 printk(KERN_ERR
"cryptoloop_release(): tfm == NULL?\n");
188 static struct loop_func_table cryptoloop_funcs
= {
189 .number
= LO_CRYPT_CRYPTOAPI
,
190 .init
= cryptoloop_init
,
191 .ioctl
= cryptoloop_ioctl
,
192 .transfer
= cryptoloop_transfer
,
193 .release
= cryptoloop_release
,
198 init_cryptoloop(void)
200 int rc
= loop_register_transfer(&cryptoloop_funcs
);
203 printk(KERN_ERR
"cryptoloop: loop_register_transfer failed\n");
208 cleanup_cryptoloop(void)
210 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI
))
212 "cryptoloop: loop_unregister_transfer failed\n");
215 module_init(init_cryptoloop
);
216 module_exit(cleanup_cryptoloop
);