Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / block / cryptoloop.c
blob3cabc335ae7448fc671642df86caa4fe2c3d3bf9
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Linux loop encryption enabling module
5 Copyright (C) 2002 Herbert Valerio Riedel <hvr@gnu.org>
6 Copyright (C) 2003 Fruhwirth Clemens <clemens@endorphin.org>
8 */
10 #include <linux/module.h>
12 #include <crypto/skcipher.h>
13 #include <linux/init.h>
14 #include <linux/string.h>
15 #include <linux/blkdev.h>
16 #include <linux/scatterlist.h>
17 #include <linux/uaccess.h>
18 #include "loop.h"
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
22 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
24 #define LOOP_IV_SECTOR_BITS 9
25 #define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
27 static int
28 cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
30 int err = -EINVAL;
31 int cipher_len;
32 int mode_len;
33 char cms[LO_NAME_SIZE]; /* cipher-mode string */
34 char *mode;
35 char *cmsp = cms; /* c-m string pointer */
36 struct crypto_sync_skcipher *tfm;
38 /* encryption breaks for non sector aligned offsets */
40 if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
41 goto out;
43 strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
44 cms[LO_NAME_SIZE - 1] = 0;
46 cipher_len = strcspn(cmsp, "-");
48 mode = cmsp + cipher_len;
49 mode_len = 0;
50 if (*mode) {
51 mode++;
52 mode_len = strcspn(mode, "-");
55 if (!mode_len) {
56 mode = "cbc";
57 mode_len = 3;
60 if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
61 return -EINVAL;
63 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;
71 tfm = crypto_alloc_sync_skcipher(cms, 0, 0);
72 if (IS_ERR(tfm))
73 return PTR_ERR(tfm);
75 err = crypto_sync_skcipher_setkey(tfm, info->lo_encrypt_key,
76 info->lo_encrypt_key_size);
78 if (err != 0)
79 goto out_free_tfm;
81 lo->key_data = tfm;
82 return 0;
84 out_free_tfm:
85 crypto_free_sync_skcipher(tfm);
87 out:
88 return err;
92 typedef int (*encdec_cbc_t)(struct skcipher_request *req);
94 static int
95 cryptoloop_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)
100 struct crypto_sync_skcipher *tfm = lo->key_data;
101 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
102 struct scatterlist sg_out;
103 struct scatterlist sg_in;
105 encdec_cbc_t encdecfunc;
106 struct page *in_page, *out_page;
107 unsigned in_offs, out_offs;
108 int err;
110 skcipher_request_set_sync_tfm(req, tfm);
111 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
112 NULL, NULL);
114 sg_init_table(&sg_out, 1);
115 sg_init_table(&sg_in, 1);
117 if (cmd == READ) {
118 in_page = raw_page;
119 in_offs = raw_off;
120 out_page = loop_page;
121 out_offs = loop_off;
122 encdecfunc = crypto_skcipher_decrypt;
123 } else {
124 in_page = loop_page;
125 in_offs = loop_off;
126 out_page = raw_page;
127 out_offs = raw_off;
128 encdecfunc = crypto_skcipher_encrypt;
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);
136 sg_set_page(&sg_in, in_page, sz, in_offs);
137 sg_set_page(&sg_out, out_page, sz, out_offs);
139 skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
140 err = encdecfunc(req);
141 if (err)
142 goto out;
144 IV++;
145 size -= sz;
146 in_offs += sz;
147 out_offs += sz;
150 err = 0;
152 out:
153 skcipher_request_zero(req);
154 return err;
157 static int
158 cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
160 return -EINVAL;
163 static int
164 cryptoloop_release(struct loop_device *lo)
166 struct crypto_sync_skcipher *tfm = lo->key_data;
167 if (tfm != NULL) {
168 crypto_free_sync_skcipher(tfm);
169 lo->key_data = NULL;
170 return 0;
172 printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
173 return -EINVAL;
176 static 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
185 static int __init
186 init_cryptoloop(void)
188 int rc = loop_register_transfer(&cryptoloop_funcs);
190 if (rc)
191 printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
192 return rc;
195 static void __exit
196 cleanup_cryptoloop(void)
198 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
199 printk(KERN_ERR
200 "cryptoloop: loop_unregister_transfer failed\n");
203 module_init(init_cryptoloop);
204 module_exit(cleanup_cryptoloop);