crypto: twofish-x86_64-3way - remove duplicated glue code and use shared glue code from glue_helper
Now that shared glue code is available, convert twofish-x86_64-3way to use it. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
parent
964263afdc
commit
414cb5e7cc
@ -3,11 +3,6 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
|
* Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
|
||||||
*
|
*
|
||||||
* CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
|
|
||||||
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
||||||
* CTR part based on code (crypto/ctr.c) by:
|
|
||||||
* (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
@ -33,6 +28,7 @@
|
|||||||
#include <crypto/algapi.h>
|
#include <crypto/algapi.h>
|
||||||
#include <crypto/twofish.h>
|
#include <crypto/twofish.h>
|
||||||
#include <crypto/b128ops.h>
|
#include <crypto/b128ops.h>
|
||||||
|
#include <asm/crypto/glue_helper.h>
|
||||||
#include <crypto/lrw.h>
|
#include <crypto/lrw.h>
|
||||||
#include <crypto/xts.h>
|
#include <crypto/xts.h>
|
||||||
|
|
||||||
@ -62,311 +58,136 @@ static inline void twofish_enc_blk_xor_3way(struct twofish_ctx *ctx, u8 *dst,
|
|||||||
__twofish_enc_blk_3way(ctx, dst, src, true);
|
__twofish_enc_blk_3way(ctx, dst, src, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
|
static void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src)
|
||||||
void (*fn)(struct twofish_ctx *, u8 *, const u8 *),
|
|
||||||
void (*fn_3way)(struct twofish_ctx *, u8 *, const u8 *))
|
|
||||||
{
|
{
|
||||||
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
|
u128 ivs[2];
|
||||||
unsigned int bsize = TF_BLOCK_SIZE;
|
|
||||||
unsigned int nbytes;
|
|
||||||
int err;
|
|
||||||
|
|
||||||
err = blkcipher_walk_virt(desc, walk);
|
ivs[0] = src[0];
|
||||||
|
ivs[1] = src[1];
|
||||||
|
|
||||||
while ((nbytes = walk->nbytes)) {
|
twofish_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
|
||||||
u8 *wsrc = walk->src.virt.addr;
|
|
||||||
u8 *wdst = walk->dst.virt.addr;
|
|
||||||
|
|
||||||
/* Process three block batch */
|
u128_xor(&dst[1], &dst[1], &ivs[0]);
|
||||||
if (nbytes >= bsize * 3) {
|
u128_xor(&dst[2], &dst[2], &ivs[1]);
|
||||||
do {
|
}
|
||||||
fn_3way(ctx, wdst, wsrc);
|
|
||||||
|
|
||||||
wsrc += bsize * 3;
|
static void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv)
|
||||||
wdst += bsize * 3;
|
{
|
||||||
nbytes -= bsize * 3;
|
be128 ctrblk;
|
||||||
} while (nbytes >= bsize * 3);
|
|
||||||
|
|
||||||
if (nbytes < bsize)
|
if (dst != src)
|
||||||
goto done;
|
*dst = *src;
|
||||||
}
|
|
||||||
|
|
||||||
/* Handle leftovers */
|
u128_to_be128(&ctrblk, iv);
|
||||||
do {
|
u128_inc(iv);
|
||||||
fn(ctx, wdst, wsrc);
|
|
||||||
|
|
||||||
wsrc += bsize;
|
twofish_enc_blk(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
|
||||||
wdst += bsize;
|
u128_xor(dst, dst, (u128 *)&ctrblk);
|
||||||
nbytes -= bsize;
|
}
|
||||||
} while (nbytes >= bsize);
|
|
||||||
|
|
||||||
done:
|
static void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
|
||||||
err = blkcipher_walk_done(desc, walk, nbytes);
|
u128 *iv)
|
||||||
|
{
|
||||||
|
be128 ctrblks[3];
|
||||||
|
|
||||||
|
if (dst != src) {
|
||||||
|
dst[0] = src[0];
|
||||||
|
dst[1] = src[1];
|
||||||
|
dst[2] = src[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
u128_to_be128(&ctrblks[0], iv);
|
||||||
|
u128_inc(iv);
|
||||||
|
u128_to_be128(&ctrblks[1], iv);
|
||||||
|
u128_inc(iv);
|
||||||
|
u128_to_be128(&ctrblks[2], iv);
|
||||||
|
u128_inc(iv);
|
||||||
|
|
||||||
|
twofish_enc_blk_xor_3way(ctx, (u8 *)dst, (u8 *)ctrblks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const struct common_glue_ctx twofish_enc = {
|
||||||
|
.num_funcs = 2,
|
||||||
|
.fpu_blocks_limit = -1,
|
||||||
|
|
||||||
|
.funcs = { {
|
||||||
|
.num_blocks = 3,
|
||||||
|
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) }
|
||||||
|
}, {
|
||||||
|
.num_blocks = 1,
|
||||||
|
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) }
|
||||||
|
} }
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct common_glue_ctx twofish_ctr = {
|
||||||
|
.num_funcs = 2,
|
||||||
|
.fpu_blocks_limit = -1,
|
||||||
|
|
||||||
|
.funcs = { {
|
||||||
|
.num_blocks = 3,
|
||||||
|
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_ctr_3way) }
|
||||||
|
}, {
|
||||||
|
.num_blocks = 1,
|
||||||
|
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_ctr) }
|
||||||
|
} }
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct common_glue_ctx twofish_dec = {
|
||||||
|
.num_funcs = 2,
|
||||||
|
.fpu_blocks_limit = -1,
|
||||||
|
|
||||||
|
.funcs = { {
|
||||||
|
.num_blocks = 3,
|
||||||
|
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_3way) }
|
||||||
|
}, {
|
||||||
|
.num_blocks = 1,
|
||||||
|
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk) }
|
||||||
|
} }
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct common_glue_ctx twofish_dec_cbc = {
|
||||||
|
.num_funcs = 2,
|
||||||
|
.fpu_blocks_limit = -1,
|
||||||
|
|
||||||
|
.funcs = { {
|
||||||
|
.num_blocks = 3,
|
||||||
|
.fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) }
|
||||||
|
}, {
|
||||||
|
.num_blocks = 1,
|
||||||
|
.fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) }
|
||||||
|
} }
|
||||||
|
};
|
||||||
|
|
||||||
static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
struct scatterlist *src, unsigned int nbytes)
|
struct scatterlist *src, unsigned int nbytes)
|
||||||
{
|
{
|
||||||
struct blkcipher_walk walk;
|
return glue_ecb_crypt_128bit(&twofish_enc, desc, dst, src, nbytes);
|
||||||
|
|
||||||
blkcipher_walk_init(&walk, dst, src, nbytes);
|
|
||||||
return ecb_crypt(desc, &walk, twofish_enc_blk, twofish_enc_blk_3way);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
struct scatterlist *src, unsigned int nbytes)
|
struct scatterlist *src, unsigned int nbytes)
|
||||||
{
|
{
|
||||||
struct blkcipher_walk walk;
|
return glue_ecb_crypt_128bit(&twofish_dec, desc, dst, src, nbytes);
|
||||||
|
|
||||||
blkcipher_walk_init(&walk, dst, src, nbytes);
|
|
||||||
return ecb_crypt(desc, &walk, twofish_dec_blk, twofish_dec_blk_3way);
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
|
|
||||||
struct blkcipher_walk *walk)
|
|
||||||
{
|
|
||||||
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
|
|
||||||
unsigned int bsize = TF_BLOCK_SIZE;
|
|
||||||
unsigned int nbytes = walk->nbytes;
|
|
||||||
u128 *src = (u128 *)walk->src.virt.addr;
|
|
||||||
u128 *dst = (u128 *)walk->dst.virt.addr;
|
|
||||||
u128 *iv = (u128 *)walk->iv;
|
|
||||||
|
|
||||||
do {
|
|
||||||
u128_xor(dst, src, iv);
|
|
||||||
twofish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
|
|
||||||
iv = dst;
|
|
||||||
|
|
||||||
src += 1;
|
|
||||||
dst += 1;
|
|
||||||
nbytes -= bsize;
|
|
||||||
} while (nbytes >= bsize);
|
|
||||||
|
|
||||||
u128_xor((u128 *)walk->iv, (u128 *)walk->iv, iv);
|
|
||||||
return nbytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
struct scatterlist *src, unsigned int nbytes)
|
struct scatterlist *src, unsigned int nbytes)
|
||||||
{
|
{
|
||||||
struct blkcipher_walk walk;
|
return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(twofish_enc_blk), desc,
|
||||||
int err;
|
dst, src, nbytes);
|
||||||
|
|
||||||
blkcipher_walk_init(&walk, dst, src, nbytes);
|
|
||||||
err = blkcipher_walk_virt(desc, &walk);
|
|
||||||
|
|
||||||
while ((nbytes = walk.nbytes)) {
|
|
||||||
nbytes = __cbc_encrypt(desc, &walk);
|
|
||||||
err = blkcipher_walk_done(desc, &walk, nbytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
|
|
||||||
struct blkcipher_walk *walk)
|
|
||||||
{
|
|
||||||
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
|
|
||||||
unsigned int bsize = TF_BLOCK_SIZE;
|
|
||||||
unsigned int nbytes = walk->nbytes;
|
|
||||||
u128 *src = (u128 *)walk->src.virt.addr;
|
|
||||||
u128 *dst = (u128 *)walk->dst.virt.addr;
|
|
||||||
u128 ivs[3 - 1];
|
|
||||||
u128 last_iv;
|
|
||||||
|
|
||||||
/* Start of the last block. */
|
|
||||||
src += nbytes / bsize - 1;
|
|
||||||
dst += nbytes / bsize - 1;
|
|
||||||
|
|
||||||
last_iv = *src;
|
|
||||||
|
|
||||||
/* Process three block batch */
|
|
||||||
if (nbytes >= bsize * 3) {
|
|
||||||
do {
|
|
||||||
nbytes -= bsize * (3 - 1);
|
|
||||||
src -= 3 - 1;
|
|
||||||
dst -= 3 - 1;
|
|
||||||
|
|
||||||
ivs[0] = src[0];
|
|
||||||
ivs[1] = src[1];
|
|
||||||
|
|
||||||
twofish_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
|
|
||||||
|
|
||||||
u128_xor(dst + 1, dst + 1, ivs + 0);
|
|
||||||
u128_xor(dst + 2, dst + 2, ivs + 1);
|
|
||||||
|
|
||||||
nbytes -= bsize;
|
|
||||||
if (nbytes < bsize)
|
|
||||||
goto done;
|
|
||||||
|
|
||||||
u128_xor(dst, dst, src - 1);
|
|
||||||
src -= 1;
|
|
||||||
dst -= 1;
|
|
||||||
} while (nbytes >= bsize * 3);
|
|
||||||
|
|
||||||
if (nbytes < bsize)
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Handle leftovers */
|
|
||||||
for (;;) {
|
|
||||||
twofish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
|
|
||||||
|
|
||||||
nbytes -= bsize;
|
|
||||||
if (nbytes < bsize)
|
|
||||||
break;
|
|
||||||
|
|
||||||
u128_xor(dst, dst, src - 1);
|
|
||||||
src -= 1;
|
|
||||||
dst -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
done:
|
|
||||||
u128_xor(dst, dst, (u128 *)walk->iv);
|
|
||||||
*(u128 *)walk->iv = last_iv;
|
|
||||||
|
|
||||||
return nbytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
struct scatterlist *src, unsigned int nbytes)
|
struct scatterlist *src, unsigned int nbytes)
|
||||||
{
|
{
|
||||||
struct blkcipher_walk walk;
|
return glue_cbc_decrypt_128bit(&twofish_dec_cbc, desc, dst, src,
|
||||||
int err;
|
nbytes);
|
||||||
|
|
||||||
blkcipher_walk_init(&walk, dst, src, nbytes);
|
|
||||||
err = blkcipher_walk_virt(desc, &walk);
|
|
||||||
|
|
||||||
while ((nbytes = walk.nbytes)) {
|
|
||||||
nbytes = __cbc_decrypt(desc, &walk);
|
|
||||||
err = blkcipher_walk_done(desc, &walk, nbytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void u128_to_be128(be128 *dst, const u128 *src)
|
|
||||||
{
|
|
||||||
dst->a = cpu_to_be64(src->a);
|
|
||||||
dst->b = cpu_to_be64(src->b);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void be128_to_u128(u128 *dst, const be128 *src)
|
|
||||||
{
|
|
||||||
dst->a = be64_to_cpu(src->a);
|
|
||||||
dst->b = be64_to_cpu(src->b);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void u128_inc(u128 *i)
|
|
||||||
{
|
|
||||||
i->b++;
|
|
||||||
if (!i->b)
|
|
||||||
i->a++;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ctr_crypt_final(struct blkcipher_desc *desc,
|
|
||||||
struct blkcipher_walk *walk)
|
|
||||||
{
|
|
||||||
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
|
|
||||||
u8 *ctrblk = walk->iv;
|
|
||||||
u8 keystream[TF_BLOCK_SIZE];
|
|
||||||
u8 *src = walk->src.virt.addr;
|
|
||||||
u8 *dst = walk->dst.virt.addr;
|
|
||||||
unsigned int nbytes = walk->nbytes;
|
|
||||||
|
|
||||||
twofish_enc_blk(ctx, keystream, ctrblk);
|
|
||||||
crypto_xor(keystream, src, nbytes);
|
|
||||||
memcpy(dst, keystream, nbytes);
|
|
||||||
|
|
||||||
crypto_inc(ctrblk, TF_BLOCK_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
|
|
||||||
struct blkcipher_walk *walk)
|
|
||||||
{
|
|
||||||
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
|
|
||||||
unsigned int bsize = TF_BLOCK_SIZE;
|
|
||||||
unsigned int nbytes = walk->nbytes;
|
|
||||||
u128 *src = (u128 *)walk->src.virt.addr;
|
|
||||||
u128 *dst = (u128 *)walk->dst.virt.addr;
|
|
||||||
u128 ctrblk;
|
|
||||||
be128 ctrblocks[3];
|
|
||||||
|
|
||||||
be128_to_u128(&ctrblk, (be128 *)walk->iv);
|
|
||||||
|
|
||||||
/* Process three block batch */
|
|
||||||
if (nbytes >= bsize * 3) {
|
|
||||||
do {
|
|
||||||
if (dst != src) {
|
|
||||||
dst[0] = src[0];
|
|
||||||
dst[1] = src[1];
|
|
||||||
dst[2] = src[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
/* create ctrblks for parallel encrypt */
|
|
||||||
u128_to_be128(&ctrblocks[0], &ctrblk);
|
|
||||||
u128_inc(&ctrblk);
|
|
||||||
u128_to_be128(&ctrblocks[1], &ctrblk);
|
|
||||||
u128_inc(&ctrblk);
|
|
||||||
u128_to_be128(&ctrblocks[2], &ctrblk);
|
|
||||||
u128_inc(&ctrblk);
|
|
||||||
|
|
||||||
twofish_enc_blk_xor_3way(ctx, (u8 *)dst,
|
|
||||||
(u8 *)ctrblocks);
|
|
||||||
|
|
||||||
src += 3;
|
|
||||||
dst += 3;
|
|
||||||
nbytes -= bsize * 3;
|
|
||||||
} while (nbytes >= bsize * 3);
|
|
||||||
|
|
||||||
if (nbytes < bsize)
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Handle leftovers */
|
|
||||||
do {
|
|
||||||
if (dst != src)
|
|
||||||
*dst = *src;
|
|
||||||
|
|
||||||
u128_to_be128(&ctrblocks[0], &ctrblk);
|
|
||||||
u128_inc(&ctrblk);
|
|
||||||
|
|
||||||
twofish_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
|
|
||||||
u128_xor(dst, dst, (u128 *)ctrblocks);
|
|
||||||
|
|
||||||
src += 1;
|
|
||||||
dst += 1;
|
|
||||||
nbytes -= bsize;
|
|
||||||
} while (nbytes >= bsize);
|
|
||||||
|
|
||||||
done:
|
|
||||||
u128_to_be128((be128 *)walk->iv, &ctrblk);
|
|
||||||
return nbytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
|
||||||
struct scatterlist *src, unsigned int nbytes)
|
struct scatterlist *src, unsigned int nbytes)
|
||||||
{
|
{
|
||||||
struct blkcipher_walk walk;
|
return glue_ctr_crypt_128bit(&twofish_ctr, desc, dst, src, nbytes);
|
||||||
int err;
|
|
||||||
|
|
||||||
blkcipher_walk_init(&walk, dst, src, nbytes);
|
|
||||||
err = blkcipher_walk_virt_block(desc, &walk, TF_BLOCK_SIZE);
|
|
||||||
|
|
||||||
while ((nbytes = walk.nbytes) >= TF_BLOCK_SIZE) {
|
|
||||||
nbytes = __ctr_crypt(desc, &walk);
|
|
||||||
err = blkcipher_walk_done(desc, &walk, nbytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (walk.nbytes) {
|
|
||||||
ctr_crypt_final(desc, &walk);
|
|
||||||
err = blkcipher_walk_done(desc, &walk, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
|
static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
|
||||||
|
@ -935,6 +935,7 @@ config CRYPTO_TWOFISH_X86_64_3WAY
|
|||||||
select CRYPTO_ALGAPI
|
select CRYPTO_ALGAPI
|
||||||
select CRYPTO_TWOFISH_COMMON
|
select CRYPTO_TWOFISH_COMMON
|
||||||
select CRYPTO_TWOFISH_X86_64
|
select CRYPTO_TWOFISH_X86_64
|
||||||
|
select CRYPTO_GLUE_HELPER_X86
|
||||||
select CRYPTO_LRW
|
select CRYPTO_LRW
|
||||||
select CRYPTO_XTS
|
select CRYPTO_XTS
|
||||||
help
|
help
|
||||||
|
Loading…
x
Reference in New Issue
Block a user