1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
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)
28 cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
33 char cms[LO_NAME_SIZE]; /* cipher-mode string */
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)
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;
52 mode_len = strcspn(mode, "-");
60 if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
63 memmove(cms, mode, mode_len);
64 cmsp = cms + mode_len;
66 memcpy(cmsp, info->lo_crypt_name, cipher_len);
71 tfm = crypto_alloc_sync_skcipher(cms, 0, 0);
75 err = crypto_sync_skcipher_setkey(tfm, info->lo_encrypt_key,
76 info->lo_encrypt_key_size);
85 crypto_free_sync_skcipher(tfm);
92 typedef int (*encdec_cbc_t)(struct skcipher_request *req);
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;
110 skcipher_request_set_sync_tfm(req, tfm);
111 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
114 sg_init_table(&sg_out, 1);
115 sg_init_table(&sg_in, 1);
120 out_page = loop_page;
122 encdecfunc = crypto_skcipher_decrypt;
128 encdecfunc = crypto_skcipher_encrypt;
132 const int sz = min(size, LOOP_IV_SECTOR_SIZE);
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);
153 skcipher_request_zero(req);
158 cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
164 cryptoloop_release(struct loop_device *lo)
166 struct crypto_sync_skcipher *tfm = lo->key_data;
168 crypto_free_sync_skcipher(tfm);
172 printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
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,
186 init_cryptoloop(void)
188 int rc = loop_register_transfer(&cryptoloop_funcs);
191 printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
193 pr_warn("the cryptoloop driver has been deprecated and will be removed in in Linux 5.16\n");
198 cleanup_cryptoloop(void)
200 if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
202 "cryptoloop: loop_unregister_transfer failed\n");
205 module_init(init_cryptoloop);
206 module_exit(cleanup_cryptoloop);