Commit 69affe7f authored by Herbert Xu's avatar Herbert Xu

[BLOCK] cryptoloop: Use block ciphers where applicable

This patch converts cryptoloop to use the new block cipher type where
applicable.  As a result the ECB-specific and CBC-specific transfer
functions have been merged.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent cba83564
...@@ -40,11 +40,13 @@ static int ...@@ -40,11 +40,13 @@ static int
cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info) cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
{ {
int err = -EINVAL; int err = -EINVAL;
int cipher_len;
int mode_len;
char cms[LO_NAME_SIZE]; /* cipher-mode string */ char cms[LO_NAME_SIZE]; /* cipher-mode string */
char *cipher; char *cipher;
char *mode; char *mode;
char *cmsp = cms; /* c-m string pointer */ char *cmsp = cms; /* c-m string pointer */
struct crypto_tfm *tfm = NULL; struct crypto_blkcipher *tfm;
/* encryption breaks for non sector aligned offsets */ /* encryption breaks for non sector aligned offsets */
...@@ -53,19 +55,38 @@ cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info) ...@@ -53,19 +55,38 @@ cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE); strncpy(cms, info->lo_crypt_name, LO_NAME_SIZE);
cms[LO_NAME_SIZE - 1] = 0; cms[LO_NAME_SIZE - 1] = 0;
cipher = strsep(&cmsp, "-");
mode = strsep(&cmsp, "-"); cipher = cmsp;
cipher_len = strcspn(cmsp, "-");
if (mode == NULL || strcmp(mode, "cbc") == 0)
tfm = crypto_alloc_tfm(cipher, CRYPTO_TFM_MODE_CBC | mode = cmsp + cipher_len;
CRYPTO_TFM_REQ_MAY_SLEEP); mode_len = 0;
else if (strcmp(mode, "ecb") == 0) if (*mode) {
tfm = crypto_alloc_tfm(cipher, CRYPTO_TFM_MODE_ECB | mode++;
CRYPTO_TFM_REQ_MAY_SLEEP); mode_len = strcspn(mode, "-");
if (tfm == NULL) }
if (!mode_len) {
mode = "cbc";
mode_len = 3;
}
if (cipher_len + mode_len + 3 > LO_NAME_SIZE)
return -EINVAL; return -EINVAL;
err = tfm->crt_u.cipher.cit_setkey(tfm, info->lo_encrypt_key, memmove(cms, mode, mode_len);
cmsp = cms + mode_len;
*cmsp++ = '(';
memcpy(cmsp, info->lo_crypt_name, cipher_len);
cmsp += cipher_len;
*cmsp++ = ')';
*cmsp = 0;
tfm = crypto_alloc_blkcipher(cms, 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(tfm))
return PTR_ERR(tfm);
err = crypto_blkcipher_setkey(tfm, info->lo_encrypt_key,
info->lo_encrypt_key_size); info->lo_encrypt_key_size);
if (err != 0) if (err != 0)
...@@ -75,99 +96,49 @@ cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info) ...@@ -75,99 +96,49 @@ cryptoloop_init(struct loop_device *lo, const struct loop_info64 *info)
return 0; return 0;
out_free_tfm: out_free_tfm:
crypto_free_tfm(tfm); crypto_free_blkcipher(tfm);
out: out:
return err; return err;
} }
typedef int (*encdec_ecb_t)(struct crypto_tfm *tfm, typedef int (*encdec_cbc_t)(struct blkcipher_desc *desc,
struct scatterlist *sg_out, struct scatterlist *sg_out,
struct scatterlist *sg_in, struct scatterlist *sg_in,
unsigned int nsg); unsigned int nsg);
static int
cryptoloop_transfer_ecb(struct loop_device *lo, int cmd,
struct page *raw_page, unsigned raw_off,
struct page *loop_page, unsigned loop_off,
int size, sector_t IV)
{
struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
struct scatterlist sg_out = { NULL, };
struct scatterlist sg_in = { NULL, };
encdec_ecb_t encdecfunc;
struct page *in_page, *out_page;
unsigned in_offs, out_offs;
if (cmd == READ) {
in_page = raw_page;
in_offs = raw_off;
out_page = loop_page;
out_offs = loop_off;
encdecfunc = tfm->crt_u.cipher.cit_decrypt;
} else {
in_page = loop_page;
in_offs = loop_off;
out_page = raw_page;
out_offs = raw_off;
encdecfunc = tfm->crt_u.cipher.cit_encrypt;
}
while (size > 0) {
const int sz = min(size, LOOP_IV_SECTOR_SIZE);
sg_in.page = in_page;
sg_in.offset = in_offs;
sg_in.length = sz;
sg_out.page = out_page;
sg_out.offset = out_offs;
sg_out.length = sz;
encdecfunc(tfm, &sg_out, &sg_in, sz);
size -= sz;
in_offs += sz;
out_offs += sz;
}
return 0;
}
typedef int (*encdec_cbc_t)(struct crypto_tfm *tfm,
struct scatterlist *sg_out,
struct scatterlist *sg_in,
unsigned int nsg, u8 *iv);
static int static int
cryptoloop_transfer_cbc(struct loop_device *lo, int cmd, cryptoloop_transfer(struct loop_device *lo, int cmd,
struct page *raw_page, unsigned raw_off, struct page *raw_page, unsigned raw_off,
struct page *loop_page, unsigned loop_off, struct page *loop_page, unsigned loop_off,
int size, sector_t IV) int size, sector_t IV)
{ {
struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data; struct crypto_blkcipher *tfm = lo->key_data;
struct blkcipher_desc desc = {
.tfm = tfm,
.flags = CRYPTO_TFM_REQ_MAY_SLEEP,
};
struct scatterlist sg_out = { NULL, }; struct scatterlist sg_out = { NULL, };
struct scatterlist sg_in = { NULL, }; struct scatterlist sg_in = { NULL, };
encdec_cbc_t encdecfunc; encdec_cbc_t encdecfunc;
struct page *in_page, *out_page; struct page *in_page, *out_page;
unsigned in_offs, out_offs; unsigned in_offs, out_offs;
int err;
if (cmd == READ) { if (cmd == READ) {
in_page = raw_page; in_page = raw_page;
in_offs = raw_off; in_offs = raw_off;
out_page = loop_page; out_page = loop_page;
out_offs = loop_off; out_offs = loop_off;
encdecfunc = tfm->crt_u.cipher.cit_decrypt_iv; encdecfunc = crypto_blkcipher_crt(tfm)->decrypt;
} else { } else {
in_page = loop_page; in_page = loop_page;
in_offs = loop_off; in_offs = loop_off;
out_page = raw_page; out_page = raw_page;
out_offs = raw_off; out_offs = raw_off;
encdecfunc = tfm->crt_u.cipher.cit_encrypt_iv; encdecfunc = crypto_blkcipher_crt(tfm)->encrypt;
} }
while (size > 0) { while (size > 0) {
...@@ -183,7 +154,10 @@ cryptoloop_transfer_cbc(struct loop_device *lo, int cmd, ...@@ -183,7 +154,10 @@ cryptoloop_transfer_cbc(struct loop_device *lo, int cmd,
sg_out.offset = out_offs; sg_out.offset = out_offs;
sg_out.length = sz; sg_out.length = sz;
encdecfunc(tfm, &sg_out, &sg_in, sz, (u8 *)iv); desc.info = iv;
err = encdecfunc(&desc, &sg_out, &sg_in, sz);
if (err)
return err;
IV++; IV++;
size -= sz; size -= sz;
...@@ -194,32 +168,6 @@ cryptoloop_transfer_cbc(struct loop_device *lo, int cmd, ...@@ -194,32 +168,6 @@ cryptoloop_transfer_cbc(struct loop_device *lo, int cmd,
return 0; return 0;
} }
static int
cryptoloop_transfer(struct loop_device *lo, int cmd,
struct page *raw_page, unsigned raw_off,
struct page *loop_page, unsigned loop_off,
int size, sector_t IV)
{
struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data;
if(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB)
{
lo->transfer = cryptoloop_transfer_ecb;
return cryptoloop_transfer_ecb(lo, cmd, raw_page, raw_off,
loop_page, loop_off, size, IV);
}
if(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_CBC)
{
lo->transfer = cryptoloop_transfer_cbc;
return cryptoloop_transfer_cbc(lo, cmd, raw_page, raw_off,
loop_page, loop_off, size, IV);
}
/* This is not supposed to happen */
printk( KERN_ERR "cryptoloop: unsupported cipher mode in cryptoloop_transfer!\n");
return -EINVAL;
}
static int static int
cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg) cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
{ {
...@@ -229,9 +177,9 @@ cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg) ...@@ -229,9 +177,9 @@ cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
static int static int
cryptoloop_release(struct loop_device *lo) cryptoloop_release(struct loop_device *lo)
{ {
struct crypto_tfm *tfm = (struct crypto_tfm *) lo->key_data; struct crypto_blkcipher *tfm = lo->key_data;
if (tfm != NULL) { if (tfm != NULL) {
crypto_free_tfm(tfm); crypto_free_blkcipher(tfm);
lo->key_data = NULL; lo->key_data = NULL;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment