diff -ruN linux-2.4/crypto/Config.in linux-2.4-cl/crypto/Config.in --- linux-2.4/crypto/Config.in 2003-08-25 19:40:22.309358234 +0200 +++ linux-2.4-cl/crypto/Config.in 2003-08-25 19:43:22.918631090 +0200 @@ -4,7 +4,9 @@ mainmenu_option next_comment comment 'Cryptographic options' -if [ "$CONFIG_INET_AH" = "y" -o \ +if [ "$CONFIG_BLK_DEV_CRYPTOLOOP" = "y" -o \ + "$CONFIG_BLK_DEV_CRYPTOLOOP" = "m" -o \ + "$CONFIG_INET_AH" = "y" -o \ "$CONFIG_INET_AH" = "m" -o \ "$CONFIG_INET_ESP" = "y" -o \ "$CONFIG_INET_ESP" = "m" -o \ diff -ruN linux-2.4/drivers/block/Config.in linux-2.4-cl/drivers/block/Config.in --- linux-2.4/drivers/block/Config.in 2003-08-25 19:38:08.428583128 +0200 +++ linux-2.4-cl/drivers/block/Config.in 2003-08-25 19:43:22.920630683 +0200 @@ -40,6 +40,7 @@ dep_tristate 'Micro Memory MM5415 Battery Backed RAM support (EXPERIMENTAL)' CONFIG_BLK_DEV_UMEM $CONFIG_PCI $CONFIG_EXPERIMENTAL tristate 'Loopback device support' CONFIG_BLK_DEV_LOOP +dep_tristate ' Cryptoloop support' CONFIG_BLK_DEV_CRYPTOLOOP $CONFIG_BLK_DEV_LOOP dep_tristate 'Network block device support' CONFIG_BLK_DEV_NBD $CONFIG_NET tristate 'RAM disk support' CONFIG_BLK_DEV_RAM diff -ruN linux-2.4/drivers/block/cryptoloop.c linux-2.4-cl/drivers/block/cryptoloop.c --- linux-2.4/drivers/block/cryptoloop.c 1970-01-01 01:00:00.000000000 +0100 +++ linux-2.4-cl/drivers/block/cryptoloop.c 2003-08-25 19:43:22.924629870 +0200 @@ -0,0 +1,179 @@ +/* + Linux loop encryption enabling module + + Copyright (C) 2002 Herbert Valerio Riedel + Copyright (C) 2003 Fruhwirth Clemens + + This module is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This module is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this module; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("loop blockdevice transferfunction adaptor / CryptoAPI"); +MODULE_AUTHOR("Herbert Valerio Riedel "); + +static int +cryptoloop_init(struct loop_device *lo, /* const */ struct loop_info *info) +{ + int err = -EINVAL; + char cms[LO_NAME_SIZE]; /* cipher-mode string */ + char *cipher; + char *mode; + char *cmsp = cms; /* c-m string pointer */ + struct crypto_tfm *tfm = NULL; + + /* encryption breaks for non sector aligned offsets */ + + if (info->lo_offset % LOOP_IV_SECTOR_SIZE) + goto out; + + strncpy(cms, info->lo_name, LO_NAME_SIZE); + cms[LO_NAME_SIZE - 1] = 0; + cipher = strsep(&cmsp, "-"); + mode = strsep(&cmsp, "-"); + + if (mode == NULL || strcmp(mode, "cbc") == 0) + tfm = crypto_alloc_tfm(cipher, CRYPTO_TFM_MODE_CBC); + else if (strcmp(mode, "ecb") == 0) + tfm = crypto_alloc_tfm(cipher, CRYPTO_TFM_MODE_ECB); + if (tfm == NULL) + return -EINVAL; + + err = tfm->crt_u.cipher.cit_setkey(tfm, info->lo_encrypt_key, + info->lo_encrypt_key_size); + + if (err != 0) + goto out_free_tfm; + + lo->key_data = tfm; + return 0; + + out_free_tfm: + crypto_free_tfm(tfm); + + out: + return err; +} + +typedef int (*encdec_t)(struct crypto_tfm *tfm, + struct scatterlist *sg_out, + struct scatterlist *sg_in, + unsigned int nsg, u8 *iv); + +static int +cryptoloop_transfer(struct loop_device *lo, int cmd, char *raw_buf, + char *loop_buf, int size, sector_t IV) +{ + struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data; + struct scatterlist sg_out = { 0, }; + struct scatterlist sg_in = { 0, }; + + encdec_t encdecfunc; + char const *in; + char *out; + + if (cmd == READ) { + in = raw_buf; + out = loop_buf; + encdecfunc = tfm->crt_u.cipher.cit_decrypt_iv; + } else { + in = loop_buf; + out = raw_buf; + encdecfunc = tfm->crt_u.cipher.cit_encrypt_iv; + } + + while (size > 0) { + const int sz = min(size, LOOP_IV_SECTOR_SIZE); + u32 iv[4] = { 0, }; + iv[0] = cpu_to_le32(IV & 0xffffffff); + + sg_in.page = virt_to_page(in); + sg_in.offset = (unsigned long)in & ~PAGE_MASK; + sg_in.length = sz; + + sg_out.page = virt_to_page(out); + sg_out.offset = (unsigned long)out & ~PAGE_MASK; + sg_out.length = sz; + + encdecfunc(tfm, &sg_out, &sg_in, sz, (u8 *)iv); + + IV++; + size -= sz; + in += sz; + out += sz; + } + + return 0; +} + + +static int +cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg) +{ + return -EINVAL; +} + +static int +cryptoloop_release(struct loop_device *lo) +{ + struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data; + if (tfm != NULL) { + crypto_free_tfm(tfm); + lo->key_data = NULL; + return 0; + } + printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n"); + return -EINVAL; +} + +static struct loop_func_table cryptoloop_funcs = { + .number = LO_CRYPT_CRYPTOAPI, + .init = cryptoloop_init, + .ioctl = cryptoloop_ioctl, + .transfer = cryptoloop_transfer, + .release = cryptoloop_release, + /* .owner = THIS_MODULE */ +}; + +static int __init +init_cryptoloop(void) +{ + int rc = loop_register_transfer(&cryptoloop_funcs); + + if (rc) + printk(KERN_ERR "cryptoloop: loop_register_transfer failed\n"); + return rc; +} + +static void __exit +cleanup_cryptoloop(void) +{ + if (loop_unregister_transfer(LO_CRYPT_CRYPTOAPI)) + printk(KERN_ERR + "cryptoloop: loop_unregister_transfer failed\n"); +} + +module_init(init_cryptoloop); +module_exit(cleanup_cryptoloop); diff -ruN linux-2.4/drivers/block/loop.c linux-2.4-cl/drivers/block/loop.c --- linux-2.4/drivers/block/loop.c 2003-08-25 19:40:28.297140608 +0200 +++ linux-2.4-cl/drivers/block/loop.c 2003-08-25 19:43:22.942626210 +0200 @@ -88,7 +88,7 @@ * Transfer functions */ static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf, - char *loop_buf, int size, int real_block) + char *loop_buf, int size, sector_t IV) { if (raw_buf != loop_buf) { if (cmd == READ) @@ -101,7 +101,7 @@ } static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf, - char *loop_buf, int size, int real_block) + char *loop_buf, int size, sector_t IV) { char *in, *out, *key; int i, keysize; @@ -189,7 +189,7 @@ len = bh->b_size; data = bh->b_data; while (len > 0) { - int IV = index * (PAGE_CACHE_SIZE/bsize) + offset/bsize; + const sector_t IV = (index << (PAGE_CACHE_SHIFT - LOOP_IV_SECTOR_BITS)) + (offset >> LOOP_IV_SECTOR_BITS); int transfer_result; size = PAGE_CACHE_SIZE - offset; @@ -249,7 +249,7 @@ unsigned long count = desc->count; struct lo_read_data *p = (struct lo_read_data*)desc->buf; struct loop_device *lo = p->lo; - int IV = page->index * (PAGE_CACHE_SIZE/p->bsize) + offset/p->bsize; + const sector_t IV = (page->index << (PAGE_CACHE_SHIFT - LOOP_IV_SECTOR_BITS)) + (offset >> LOOP_IV_SECTOR_BITS); if (size > count) size = count; @@ -301,20 +301,6 @@ return bs; } -static inline unsigned long loop_get_iv(struct loop_device *lo, - unsigned long sector) -{ - int bs = loop_get_bs(lo); - unsigned long offset, IV; - - IV = sector / (bs >> 9) + lo->lo_offset / bs; - offset = ((sector % (bs >> 9)) << 9) + lo->lo_offset % bs; - if (offset >= bs) - IV++; - - return IV; -} - static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw) { loff_t pos; @@ -462,7 +448,7 @@ { struct buffer_head *bh = NULL; struct loop_device *lo; - unsigned long IV; + sector_t IV; if (!buffer_locked(rbh)) BUG(); @@ -507,7 +493,7 @@ * piggy old buffer on original, and submit for I/O */ bh = loop_get_buffer(lo, rbh); - IV = loop_get_iv(lo, rbh->b_rsector); + IV = rbh->b_rsector + (lo->lo_offset >> LOOP_IV_SECTOR_BITS); if (rw == WRITE) { set_bit(BH_Dirty, &bh->b_state); if (lo_do_transfer(lo, WRITE, bh->b_data, rbh->b_data, @@ -544,7 +530,7 @@ bh->b_end_io(bh, !ret); } else { struct buffer_head *rbh = bh->b_private; - unsigned long IV = loop_get_iv(lo, rbh->b_rsector); + const sector_t IV = rbh->b_rsector + (lo->lo_offset >> LOOP_IV_SECTOR_BITS); ret = lo_do_transfer(lo, READ, bh->b_data, rbh->b_data, bh->b_size, IV); diff -ruN linux-2.4/drivers/block/Makefile linux-2.4-cl/drivers/block/Makefile --- linux-2.4/drivers/block/Makefile 2003-08-25 19:39:13.175416737 +0200 +++ linux-2.4-cl/drivers/block/Makefile 2003-08-25 19:43:22.944625804 +0200 @@ -31,6 +31,7 @@ obj-$(CONFIG_BLK_DEV_DAC960) += DAC960.o obj-$(CONFIG_BLK_DEV_UMEM) += umem.o obj-$(CONFIG_BLK_DEV_NBD) += nbd.o +obj-$(CONFIG_BLK_DEV_CRYPTOLOOP) += cryptoloop.o subdir-$(CONFIG_PARIDE) += paride diff -ruN linux-2.4/include/linux/loop.h linux-2.4-cl/include/linux/loop.h --- linux-2.4/include/linux/loop.h 2001-09-17 22:16:30.000000000 +0200 +++ linux-2.4-cl/include/linux/loop.h 2003-08-25 19:44:07.991465478 +0200 @@ -16,6 +16,14 @@ #define LO_KEY_SIZE 32 #ifdef __KERNEL__ +typedef int sector_t; /* for 2.6 this is defined in and + most likely an u64; but since cryptoloop uses + only the lower 32 bits of the block number + passed, let's just use an 32bit int for now */ + +/* definitions for IV metric */ +#define LOOP_IV_SECTOR_BITS 9 +#define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS) /* Possible states of device */ enum { @@ -24,6 +32,12 @@ Lo_rundown, }; +struct loop_device; + +typedef int (* transfer_proc_t)(struct loop_device *, int cmd, + char *raw_buf, char *loop_buf, int size, + sector_t real_block); + struct loop_device { int lo_number; int lo_refcnt; @@ -32,9 +46,7 @@ int lo_encrypt_type; int lo_encrypt_key_size; int lo_flags; - int (*transfer)(struct loop_device *, int cmd, - char *raw_buf, char *loop_buf, int size, - int real_block); + transfer_proc_t transfer; char lo_name[LO_NAME_SIZE]; char lo_encrypt_key[LO_KEY_SIZE]; __u32 lo_init[2]; @@ -58,17 +70,13 @@ atomic_t lo_pending; }; -typedef int (* transfer_proc_t)(struct loop_device *, int cmd, - char *raw_buf, char *loop_buf, int size, - int real_block); - static inline int lo_do_transfer(struct loop_device *lo, int cmd, char *rbuf, - char *lbuf, int size, int rblock) + char *lbuf, int size, sector_t real_block) { if (!lo->transfer) return 0; - return lo->transfer(lo, cmd, rbuf, lbuf, size, rblock); + return lo->transfer(lo, cmd, rbuf, lbuf, size, real_block); } #endif /* __KERNEL__ */ @@ -122,6 +130,7 @@ #define LO_CRYPT_IDEA 6 #define LO_CRYPT_DUMMY 9 #define LO_CRYPT_SKIPJACK 10 +#define LO_CRYPT_CRYPTOAPI 18 #define MAX_LO_CRYPT 20 #ifdef __KERNEL__ @@ -129,7 +138,7 @@ struct loop_func_table { int number; /* filter type */ int (*transfer)(struct loop_device *lo, int cmd, char *raw_buf, - char *loop_buf, int size, int real_block); + char *loop_buf, int size, sector_t real_block); int (*init)(struct loop_device *, struct loop_info *); /* release is called from loop_unregister_transfer or clr_fd */ int (*release)(struct loop_device *);