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