mm: update get_user_pages_longterm to migrate pages allocated from CMA region
[linux-2.6-block.git] / drivers / block / cryptoloop.c
CommitLineData
1da177e4
LT
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
84a2c931 24#include <crypto/skcipher.h>
1da177e4
LT
25#include <linux/init.h>
26#include <linux/string.h>
1da177e4 27#include <linux/blkdev.h>
45711f1a 28#include <linux/scatterlist.h>
7c0f6ba6 29#include <linux/uaccess.h>
83a87611 30#include "loop.h"
1da177e4
LT
31
32MODULE_LICENSE("GPL");
33MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
34MODULE_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
39static int
40cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
41{
42 int err = -EINVAL;
69affe7f
HX
43 int cipher_len;
44 int mode_len;
1da177e4 45 char cms[LO_NAME_SIZE]; /* cipher-mode string */
1da177e4
LT
46 char *mode;
47 char *cmsp = cms; /* c-m string pointer */
dc568baf 48 struct crypto_sync_skcipher *tfm;
1da177e4
LT
49
50 /* encryption breaks for non sector aligned offsets */
51
52 if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
53 goto out;
54
55 strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
56 cms[LO_NAME_SIZE - 1] = 0;
69affe7f 57
69affe7f
HX
58 cipher_len = strcspn(cmsp, "-");
59
60 mode = cmsp + cipher_len;
61 mode_len = 0;
62 if (*mode) {
63 mode++;
64 mode_len = strcspn(mode, "-");
65 }
66
67 if (!mode_len) {
68 mode = "cbc";
69 mode_len = 3;
70 }
71
72 if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
1da177e4
LT
73 return -EINVAL;
74
69affe7f
HX
75 memmove(cms, mode, mode_len);
76 cmsp = cms + mode_len;
77 *cmsp++ = '(';
78 memcpy(cmsp, info->lo_crypt_name, cipher_len);
79 cmsp += cipher_len;
80 *cmsp++ = ')';
81 *cmsp = 0;
82
dc568baf 83 tfm = crypto_alloc_sync_skcipher(cms, 0, 0);
69affe7f
HX
84 if (IS_ERR(tfm))
85 return PTR_ERR(tfm);
86
dc568baf
KC
87 err = crypto_sync_skcipher_setkey(tfm, info->lo_encrypt_key,
88 info->lo_encrypt_key_size);
89
1da177e4
LT
90 if (err != 0)
91 goto out_free_tfm;
92
93 lo->key_data = tfm;
94 return 0;
95
96 out_free_tfm:
dc568baf 97 crypto_free_sync_skcipher(tfm);
1da177e4
LT
98
99 out:
100 return err;
101}
102
103
84a2c931 104typedef int (*encdec_cbc_t)(struct skcipher_request *req);
1da177e4 105
1da177e4 106static int
69affe7f
HX
107cryptoloop_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)
1da177e4 111{
dc568baf
KC
112 struct crypto_sync_skcipher *tfm = lo->key_data;
113 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
45711f1a
JA
114 struct scatterlist sg_out;
115 struct scatterlist sg_in;
1da177e4
LT
116
117 encdec_cbc_t encdecfunc;
118 struct page *in_page, *out_page;
119 unsigned in_offs, out_offs;
69affe7f 120 int err;
1da177e4 121
dc568baf 122 skcipher_request_set_sync_tfm(req, tfm);
84a2c931
HX
123 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
124 NULL, NULL);
125
45711f1a
JA
126 sg_init_table(&sg_out, 1);
127 sg_init_table(&sg_in, 1);
128
1da177e4
LT
129 if (cmd == READ) {
130 in_page = raw_page;
131 in_offs = raw_off;
132 out_page = loop_page;
133 out_offs = loop_off;
84a2c931 134 encdecfunc = crypto_skcipher_decrypt;
1da177e4
LT
135 } else {
136 in_page = loop_page;
137 in_offs = loop_off;
138 out_page = raw_page;
139 out_offs = raw_off;
84a2c931 140 encdecfunc = crypto_skcipher_encrypt;
1da177e4
LT
141 }
142
143 while (size > 0) {
144 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
145 u32 iv[4] = { 0, };
146 iv[0] = cpu_to_le32(IV & 0xffffffff);
147
642f1490
JA
148 sg_set_page(&sg_in, in_page, sz, in_offs);
149 sg_set_page(&sg_out, out_page, sz, out_offs);
1da177e4 150
84a2c931
HX
151 skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
152 err = encdecfunc(req);
69affe7f 153 if (err)
84a2c931 154 goto out;
1da177e4
LT
155
156 IV++;
157 size -= sz;
158 in_offs += sz;
159 out_offs += sz;
160 }
161
84a2c931
HX
162 err = 0;
163
164out:
165 skcipher_request_zero(req);
166 return err;
1da177e4
LT
167}
168
1da177e4
LT
169static int
170cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
171{
172 return -EINVAL;
173}
174
175static int
176cryptoloop_release(struct loop_device *lo)
177{
dc568baf 178 struct crypto_sync_skcipher *tfm = lo->key_data;
1da177e4 179 if (tfm != NULL) {
dc568baf 180 crypto_free_sync_skcipher(tfm);
1da177e4
LT
181 lo->key_data = NULL;
182 return 0;
183 }
184 printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
185 return -EINVAL;
186}
187
188static 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,
194 .owner = THIS_MODULE
195};
196
197static int __init
198init_cryptoloop(void)
199{
200 int rc = loop_register_transfer(&cryptoloop_funcs);
201
202 if (rc)
203 printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
204 return rc;
205}
206
207static void __exit
208cleanup_cryptoloop(void)
209{
210 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
211 printk(KERN_ERR
212 "cryptoloop: loop_unregister_transfer failed\n");
213}
214
215module_init(init_cryptoloop);
216module_exit(cleanup_cryptoloop);