blob: 3cabc335ae7448fc671642df86caa4fe2c3d3bf9 [file] [log] [blame]
Thomas Gleixner1b9391e2019-05-20 19:07:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 Linux loop encryption enabling module
4
5 Copyright (C) 2002 Herbert Valerio Riedel <hvr@gnu.org>
6 Copyright (C) 2003 Fruhwirth Clemens <clemens@endorphin.org>
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/module.h>
11
Herbert Xu84a2c932016-01-24 21:16:06 +080012#include <crypto/skcipher.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/blkdev.h>
Jens Axboe45711f12007-10-22 21:19:53 +020016#include <linux/scatterlist.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
Al Viro83a87612013-05-12 10:14:07 -040018#include "loop.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20MODULE_LICENSE("GPL");
21MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
22MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
23
24#define LOOP_IV_SECTOR_BITS 9
25#define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
26
27static int
28cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
29{
30 int err = -EINVAL;
Herbert Xu69affe72006-09-21 11:45:53 +100031 int cipher_len;
32 int mode_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 char cms[LO_NAME_SIZE]; /* cipher-mode string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 char *mode;
35 char *cmsp = cms; /* c-m string pointer */
Kees Cookdc568ba2018-09-18 19:10:44 -070036 struct crypto_sync_skcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 /* encryption breaks for non sector aligned offsets */
39
40 if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
41 goto out;
42
43 strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
44 cms[LO_NAME_SIZE - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Herbert Xu69affe72006-09-21 11:45:53 +100046 cipher_len = strcspn(cmsp, "-");
47
48 mode = cmsp + cipher_len;
49 mode_len = 0;
50 if (*mode) {
51 mode++;
52 mode_len = strcspn(mode, "-");
53 }
54
55 if (!mode_len) {
56 mode = "cbc";
57 mode_len = 3;
58 }
59
60 if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return -EINVAL;
62
Herbert Xu69affe72006-09-21 11:45:53 +100063 memmove(cms, mode, mode_len);
64 cmsp = cms + mode_len;
65 *cmsp++ = '(';
66 memcpy(cmsp, info->lo_crypt_name, cipher_len);
67 cmsp += cipher_len;
68 *cmsp++ = ')';
69 *cmsp = 0;
70
Kees Cookdc568ba2018-09-18 19:10:44 -070071 tfm = crypto_alloc_sync_skcipher(cms, 0, 0);
Herbert Xu69affe72006-09-21 11:45:53 +100072 if (IS_ERR(tfm))
73 return PTR_ERR(tfm);
74
Kees Cookdc568ba2018-09-18 19:10:44 -070075 err = crypto_sync_skcipher_setkey(tfm, info->lo_encrypt_key,
76 info->lo_encrypt_key_size);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (err != 0)
79 goto out_free_tfm;
80
81 lo->key_data = tfm;
82 return 0;
83
84 out_free_tfm:
Kees Cookdc568ba2018-09-18 19:10:44 -070085 crypto_free_sync_skcipher(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 out:
88 return err;
89}
90
91
Herbert Xu84a2c932016-01-24 21:16:06 +080092typedef int (*encdec_cbc_t)(struct skcipher_request *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int
Herbert Xu69affe72006-09-21 11:45:53 +100095cryptoloop_transfer(struct loop_device *lo, int cmd,
96 struct page *raw_page, unsigned raw_off,
97 struct page *loop_page, unsigned loop_off,
98 int size, sector_t IV)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Kees Cookdc568ba2018-09-18 19:10:44 -0700100 struct crypto_sync_skcipher *tfm = lo->key_data;
101 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
Jens Axboe45711f12007-10-22 21:19:53 +0200102 struct scatterlist sg_out;
103 struct scatterlist sg_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 encdec_cbc_t encdecfunc;
106 struct page *in_page, *out_page;
107 unsigned in_offs, out_offs;
Herbert Xu69affe72006-09-21 11:45:53 +1000108 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Kees Cookdc568ba2018-09-18 19:10:44 -0700110 skcipher_request_set_sync_tfm(req, tfm);
Herbert Xu84a2c932016-01-24 21:16:06 +0800111 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
112 NULL, NULL);
113
Jens Axboe45711f12007-10-22 21:19:53 +0200114 sg_init_table(&sg_out, 1);
115 sg_init_table(&sg_in, 1);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (cmd == READ) {
118 in_page = raw_page;
119 in_offs = raw_off;
120 out_page = loop_page;
121 out_offs = loop_off;
Herbert Xu84a2c932016-01-24 21:16:06 +0800122 encdecfunc = crypto_skcipher_decrypt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else {
124 in_page = loop_page;
125 in_offs = loop_off;
126 out_page = raw_page;
127 out_offs = raw_off;
Herbert Xu84a2c932016-01-24 21:16:06 +0800128 encdecfunc = crypto_skcipher_encrypt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
131 while (size > 0) {
132 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
133 u32 iv[4] = { 0, };
134 iv[0] = cpu_to_le32(IV & 0xffffffff);
135
Jens Axboe642f149032007-10-24 11:20:47 +0200136 sg_set_page(&sg_in, in_page, sz, in_offs);
137 sg_set_page(&sg_out, out_page, sz, out_offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Herbert Xu84a2c932016-01-24 21:16:06 +0800139 skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
140 err = encdecfunc(req);
Herbert Xu69affe72006-09-21 11:45:53 +1000141 if (err)
Herbert Xu84a2c932016-01-24 21:16:06 +0800142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 IV++;
145 size -= sz;
146 in_offs += sz;
147 out_offs += sz;
148 }
149
Herbert Xu84a2c932016-01-24 21:16:06 +0800150 err = 0;
151
152out:
153 skcipher_request_zero(req);
154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
159{
160 return -EINVAL;
161}
162
163static int
164cryptoloop_release(struct loop_device *lo)
165{
Kees Cookdc568ba2018-09-18 19:10:44 -0700166 struct crypto_sync_skcipher *tfm = lo->key_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (tfm != NULL) {
Kees Cookdc568ba2018-09-18 19:10:44 -0700168 crypto_free_sync_skcipher(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 lo->key_data = NULL;
170 return 0;
171 }
172 printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
173 return -EINVAL;
174}
175
176static struct loop_func_table cryptoloop_funcs = {
177 .number = LO_CRYPT_CRYPTOAPI,
178 .init = cryptoloop_init,
179 .ioctl = cryptoloop_ioctl,
180 .transfer = cryptoloop_transfer,
181 .release = cryptoloop_release,
182 .owner = THIS_MODULE
183};
184
185static int __init
186init_cryptoloop(void)
187{
188 int rc = loop_register_transfer(&cryptoloop_funcs);
189
190 if (rc)
191 printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
192 return rc;
193}
194
195static void __exit
196cleanup_cryptoloop(void)
197{
198 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
199 printk(KERN_ERR
200 "cryptoloop: loop_unregister_transfer failed\n");
201}
202
203module_init(init_cryptoloop);
204module_exit(cleanup_cryptoloop);