Linux Audio

Check our new training course

Loading...
v3.1
 
  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
 
 24#include <linux/init.h>
 25#include <linux/string.h>
 26#include <linux/crypto.h>
 27#include <linux/blkdev.h>
 28#include <linux/loop.h>
 29#include <linux/scatterlist.h>
 30#include <asm/uaccess.h>
 
 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;
 43	int cipher_len;
 44	int mode_len;
 45	char cms[LO_NAME_SIZE];			/* cipher-mode string */
 46	char *cipher;
 47	char *mode;
 48	char *cmsp = cms;			/* c-m string pointer */
 49	struct crypto_blkcipher *tfm;
 50
 51	/* encryption breaks for non sector aligned offsets */
 52
 53	if (info->lo_offset % LOOP_IV_SECTOR_SIZE)
 54		goto out;
 55
 56	strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
 57	cms[LO_NAME_SIZE - 1] = 0;
 58
 59	cipher = cmsp;
 60	cipher_len = strcspn(cmsp, "-");
 61
 62	mode = cmsp + cipher_len;
 63	mode_len = 0;
 64	if (*mode) {
 65		mode++;
 66		mode_len = strcspn(mode, "-");
 67	}
 68
 69	if (!mode_len) {
 70		mode = "cbc";
 71		mode_len = 3;
 72	}
 73
 74	if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
 75		return -EINVAL;
 76
 77	memmove(cms, mode, mode_len);
 78	cmsp = cms + mode_len;
 79	*cmsp++ = '(';
 80	memcpy(cmsp, info->lo_crypt_name, cipher_len);
 81	cmsp += cipher_len;
 82	*cmsp++ = ')';
 83	*cmsp = 0;
 84
 85	tfm = crypto_alloc_blkcipher(cms, 0, CRYPTO_ALG_ASYNC);
 86	if (IS_ERR(tfm))
 87		return PTR_ERR(tfm);
 88
 89	err = crypto_blkcipher_setkey(tfm, info->lo_encrypt_key,
 90				      info->lo_encrypt_key_size);
 91	
 92	if (err != 0)
 93		goto out_free_tfm;
 94
 95	lo->key_data = tfm;
 96	return 0;
 97
 98 out_free_tfm:
 99	crypto_free_blkcipher(tfm);
100
101 out:
102	return err;
103}
104
105
106typedef int (*encdec_cbc_t)(struct blkcipher_desc *desc,
107			struct scatterlist *sg_out,
108			struct scatterlist *sg_in,
109			unsigned int nsg);
110
111static int
112cryptoloop_transfer(struct loop_device *lo, int cmd,
113		    struct page *raw_page, unsigned raw_off,
114		    struct page *loop_page, unsigned loop_off,
115		    int size, sector_t IV)
116{
117	struct crypto_blkcipher *tfm = lo->key_data;
118	struct blkcipher_desc desc = {
119		.tfm = tfm,
120		.flags = CRYPTO_TFM_REQ_MAY_SLEEP,
121	};
122	struct scatterlist sg_out;
123	struct scatterlist sg_in;
124
125	encdec_cbc_t encdecfunc;
126	struct page *in_page, *out_page;
127	unsigned in_offs, out_offs;
128	int err;
129
 
 
 
 
130	sg_init_table(&sg_out, 1);
131	sg_init_table(&sg_in, 1);
132
133	if (cmd == READ) {
134		in_page = raw_page;
135		in_offs = raw_off;
136		out_page = loop_page;
137		out_offs = loop_off;
138		encdecfunc = crypto_blkcipher_crt(tfm)->decrypt;
139	} else {
140		in_page = loop_page;
141		in_offs = loop_off;
142		out_page = raw_page;
143		out_offs = raw_off;
144		encdecfunc = crypto_blkcipher_crt(tfm)->encrypt;
145	}
146
147	while (size > 0) {
148		const int sz = min(size, LOOP_IV_SECTOR_SIZE);
149		u32 iv[4] = { 0, };
150		iv[0] = cpu_to_le32(IV & 0xffffffff);
151
152		sg_set_page(&sg_in, in_page, sz, in_offs);
153		sg_set_page(&sg_out, out_page, sz, out_offs);
154
155		desc.info = iv;
156		err = encdecfunc(&desc, &sg_out, &sg_in, sz);
157		if (err)
158			return err;
159
160		IV++;
161		size -= sz;
162		in_offs += sz;
163		out_offs += sz;
164	}
165
166	return 0;
 
 
 
 
167}
168
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{
178	struct crypto_blkcipher *tfm = lo->key_data;
179	if (tfm != NULL) {
180		crypto_free_blkcipher(tfm);
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);
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  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
 
 
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/module.h>
 11
 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"
 19
 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;
 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;
 37
 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;
 45
 
 46	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)
 61		return -EINVAL;
 62
 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;
 70
 71	tfm = crypto_alloc_sync_skcipher(cms, 0, 0);
 72	if (IS_ERR(tfm))
 73		return PTR_ERR(tfm);
 74
 75	err = crypto_sync_skcipher_setkey(tfm, info->lo_encrypt_key,
 76					  info->lo_encrypt_key_size);
 77
 78	if (err != 0)
 79		goto out_free_tfm;
 80
 81	lo->key_data = tfm;
 82	return 0;
 83
 84 out_free_tfm:
 85	crypto_free_sync_skcipher(tfm);
 86
 87 out:
 88	return err;
 89}
 90
 91
 92typedef int (*encdec_cbc_t)(struct skcipher_request *req);
 
 
 
 93
 94static int
 95cryptoloop_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)
 99{
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;
104
105	encdec_cbc_t encdecfunc;
106	struct page *in_page, *out_page;
107	unsigned in_offs, out_offs;
108	int err;
109
110	skcipher_request_set_sync_tfm(req, tfm);
111	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
112				      NULL, NULL);
113
114	sg_init_table(&sg_out, 1);
115	sg_init_table(&sg_in, 1);
116
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;
129	}
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
136		sg_set_page(&sg_in, in_page, sz, in_offs);
137		sg_set_page(&sg_out, out_page, sz, out_offs);
138
139		skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
140		err = encdecfunc(req);
141		if (err)
142			goto out;
143
144		IV++;
145		size -= sz;
146		in_offs += sz;
147		out_offs += sz;
148	}
149
150	err = 0;
151
152out:
153	skcipher_request_zero(req);
154	return err;
155}
156
157static int
158cryptoloop_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{
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;
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);