Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 29 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/semaphore.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI"); |
| 35 | MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>"); |
| 36 | |
| 37 | #define LOOP_IV_SECTOR_BITS 9 |
| 38 | #define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS) |
| 39 | |
| 40 | static int |
| 41 | cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info) |
| 42 | { |
| 43 | int err = -EINVAL; |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 44 | int cipher_len; |
| 45 | int mode_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | char cms[LO_NAME_SIZE]; /* cipher-mode string */ |
| 47 | char *cipher; |
| 48 | char *mode; |
| 49 | char *cmsp = cms; /* c-m string pointer */ |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 50 | struct crypto_blkcipher *tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* encryption breaks for non sector aligned offsets */ |
| 53 | |
| 54 | if (info->lo_offset % LOOP_IV_SECTOR_SIZE) |
| 55 | goto out; |
| 56 | |
| 57 | strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE); |
| 58 | cms[LO_NAME_SIZE - 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 60 | cipher = cmsp; |
| 61 | cipher_len = strcspn(cmsp, "-"); |
| 62 | |
| 63 | mode = cmsp + cipher_len; |
| 64 | mode_len = 0; |
| 65 | if (*mode) { |
| 66 | mode++; |
| 67 | mode_len = strcspn(mode, "-"); |
| 68 | } |
| 69 | |
| 70 | if (!mode_len) { |
| 71 | mode = "cbc"; |
| 72 | mode_len = 3; |
| 73 | } |
| 74 | |
| 75 | if (cipher_len + mode_len + 3 > LO_NAME_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return -EINVAL; |
| 77 | |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 78 | memmove(cms, mode, mode_len); |
| 79 | cmsp = cms + mode_len; |
| 80 | *cmsp++ = '('; |
| 81 | memcpy(cmsp, info->lo_crypt_name, cipher_len); |
| 82 | cmsp += cipher_len; |
| 83 | *cmsp++ = ')'; |
| 84 | *cmsp = 0; |
| 85 | |
| 86 | tfm = crypto_alloc_blkcipher(cms, 0, CRYPTO_ALG_ASYNC); |
| 87 | if (IS_ERR(tfm)) |
| 88 | return PTR_ERR(tfm); |
| 89 | |
| 90 | err = crypto_blkcipher_setkey(tfm, info->lo_encrypt_key, |
| 91 | info->lo_encrypt_key_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | if (err != 0) |
| 94 | goto out_free_tfm; |
| 95 | |
| 96 | lo->key_data = tfm; |
| 97 | return 0; |
| 98 | |
| 99 | out_free_tfm: |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 100 | crypto_free_blkcipher(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | out: |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 107 | typedef int (*encdec_cbc_t)(struct blkcipher_desc *desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | struct scatterlist *sg_out, |
| 109 | struct scatterlist *sg_in, |
| 110 | unsigned int nsg); |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static int |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 113 | cryptoloop_transfer(struct loop_device *lo, int cmd, |
| 114 | struct page *raw_page, unsigned raw_off, |
| 115 | struct page *loop_page, unsigned loop_off, |
| 116 | int size, sector_t IV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 118 | struct crypto_blkcipher *tfm = lo->key_data; |
| 119 | struct blkcipher_desc desc = { |
| 120 | .tfm = tfm, |
| 121 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP, |
| 122 | }; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 123 | struct scatterlist sg_out; |
| 124 | struct scatterlist sg_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | encdec_cbc_t encdecfunc; |
| 127 | struct page *in_page, *out_page; |
| 128 | unsigned in_offs, out_offs; |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 129 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 131 | sg_init_table(&sg_out, 1); |
| 132 | sg_init_table(&sg_in, 1); |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (cmd == READ) { |
| 135 | in_page = raw_page; |
| 136 | in_offs = raw_off; |
| 137 | out_page = loop_page; |
| 138 | out_offs = loop_off; |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 139 | encdecfunc = crypto_blkcipher_crt(tfm)->decrypt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } else { |
| 141 | in_page = loop_page; |
| 142 | in_offs = loop_off; |
| 143 | out_page = raw_page; |
| 144 | out_offs = raw_off; |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 145 | encdecfunc = crypto_blkcipher_crt(tfm)->encrypt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | while (size > 0) { |
| 149 | const int sz = min(size, LOOP_IV_SECTOR_SIZE); |
| 150 | u32 iv[4] = { 0, }; |
| 151 | iv[0] = cpu_to_le32(IV & 0xffffffff); |
| 152 | |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 153 | sg_set_page(&sg_in, in_page, sz, in_offs); |
| 154 | sg_set_page(&sg_out, out_page, sz, out_offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 156 | desc.info = iv; |
| 157 | err = encdecfunc(&desc, &sg_out, &sg_in, sz); |
| 158 | if (err) |
| 159 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | IV++; |
| 162 | size -= sz; |
| 163 | in_offs += sz; |
| 164 | out_offs += sz; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg) |
| 172 | { |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | static int |
| 177 | cryptoloop_release(struct loop_device *lo) |
| 178 | { |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 179 | struct crypto_blkcipher *tfm = lo->key_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (tfm != NULL) { |
Herbert Xu | 69affe7 | 2006-09-21 11:45:53 +1000 | [diff] [blame] | 181 | crypto_free_blkcipher(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | lo->key_data = NULL; |
| 183 | return 0; |
| 184 | } |
| 185 | printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n"); |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | static struct loop_func_table cryptoloop_funcs = { |
| 190 | .number = LO_CRYPT_CRYPTOAPI, |
| 191 | .init = cryptoloop_init, |
| 192 | .ioctl = cryptoloop_ioctl, |
| 193 | .transfer = cryptoloop_transfer, |
| 194 | .release = cryptoloop_release, |
| 195 | .owner = THIS_MODULE |
| 196 | }; |
| 197 | |
| 198 | static int __init |
| 199 | init_cryptoloop(void) |
| 200 | { |
| 201 | int rc = loop_register_transfer(&cryptoloop_funcs); |
| 202 | |
| 203 | if (rc) |
| 204 | printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n"); |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | static void __exit |
| 209 | cleanup_cryptoloop(void) |
| 210 | { |
| 211 | if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI)) |
| 212 | printk(KERN_ERR |
| 213 | "cryptoloop: loop_unregister_transfer failed\n"); |
| 214 | } |
| 215 | |
| 216 | module_init(init_cryptoloop); |
| 217 | module_exit(cleanup_cryptoloop); |