^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Linux loop encryption enabling module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Copyright (C) 2002 Herbert Valerio Riedel <hvr@gnu.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) Copyright (C) 2003 Fruhwirth Clemens <clemens@endorphin.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "loop.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define LOOP_IV_SECTOR_BITS 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int cipher_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int mode_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) char cms[LO_NAME_SIZE]; /* cipher-mode string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) char *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) char *cmsp = cms; /* c-m string pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct crypto_sync_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* encryption breaks for non sector aligned offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) cms[LO_NAME_SIZE - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) cipher_len = strcspn(cmsp, "-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) mode = cmsp + cipher_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mode_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (*mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) mode++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) mode_len = strcspn(mode, "-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!mode_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) mode = "cbc";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) mode_len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) memmove(cms, mode, mode_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) cmsp = cms + mode_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *cmsp++ = '(';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) memcpy(cmsp, info->lo_crypt_name, cipher_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) cmsp += cipher_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *cmsp++ = ')';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *cmsp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) tfm = crypto_alloc_sync_skcipher(cms, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) err = crypto_sync_skcipher_setkey(tfm, info->lo_encrypt_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) info->lo_encrypt_key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out_free_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) lo->key_data = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) out_free_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) crypto_free_sync_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) typedef int (*encdec_cbc_t)(struct skcipher_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) cryptoloop_transfer(struct loop_device *lo, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct page *raw_page, unsigned raw_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct page *loop_page, unsigned loop_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int size, sector_t IV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct crypto_sync_skcipher *tfm = lo->key_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct scatterlist sg_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct scatterlist sg_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) encdec_cbc_t encdecfunc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct page *in_page, *out_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned in_offs, out_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) skcipher_request_set_sync_tfm(req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) sg_init_table(&sg_out, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) sg_init_table(&sg_in, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (cmd == READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) in_page = raw_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) in_offs = raw_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) out_page = loop_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) out_offs = loop_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) encdecfunc = crypto_skcipher_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) in_page = loop_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) in_offs = loop_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) out_page = raw_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) out_offs = raw_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) encdecfunc = crypto_skcipher_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) while (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) const int sz = min(size, LOOP_IV_SECTOR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u32 iv[4] = { 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) iv[0] = cpu_to_le32(IV & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) sg_set_page(&sg_in, in_page, sz, in_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) sg_set_page(&sg_out, out_page, sz, out_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) err = encdecfunc(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) IV++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) size -= sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) in_offs += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) out_offs += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) skcipher_request_zero(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) cryptoloop_release(struct loop_device *lo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct crypto_sync_skcipher *tfm = lo->key_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (tfm != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) crypto_free_sync_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) lo->key_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static struct loop_func_table cryptoloop_funcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .number = LO_CRYPT_CRYPTOAPI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .init = cryptoloop_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .ioctl = cryptoloop_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .transfer = cryptoloop_transfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .release = cryptoloop_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .owner = THIS_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) init_cryptoloop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int rc = loop_register_transfer(&cryptoloop_funcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) pr_warn("the cryptoloop driver has been deprecated and will be removed in in Linux 5.16\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void __exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) cleanup_cryptoloop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) "cryptoloop: loop_unregister_transfer failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) module_init(init_cryptoloop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) module_exit(cleanup_cryptoloop);