Commit a7378d4e authored by Jussi Kivilinna's avatar Jussi Kivilinna Committed by Herbert Xu

crypto: twofish-avx - remove duplicated glue code and use shared glue code from glue_helper

Now that shared glue code is available, convert twofish-avx to use it.

Cc: Johannes Goetzfried <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
Signed-off-by: default avatarJussi Kivilinna <jussi.kivilinna@mbnet.fi>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 414cb5e7
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
* Copyright (C) 2012 Johannes Goetzfried * Copyright (C) 2012 Johannes Goetzfried
* <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
* *
* Glue code based on serpent_sse2_glue.c by:
* Copyright (C) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
*
* 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
...@@ -39,38 +36,21 @@ ...@@ -39,38 +36,21 @@
#include <asm/i387.h> #include <asm/i387.h>
#include <asm/xcr.h> #include <asm/xcr.h>
#include <asm/xsave.h> #include <asm/xsave.h>
#include <asm/crypto/twofish.h>
#include <asm/crypto/ablk_helper.h> #include <asm/crypto/ablk_helper.h>
#include <asm/crypto/glue_helper.h>
#include <crypto/scatterwalk.h> #include <crypto/scatterwalk.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#define TWOFISH_PARALLEL_BLOCKS 8 #define TWOFISH_PARALLEL_BLOCKS 8
/* regular block cipher functions from twofish_x86_64 module */
asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
asmlinkage void twofish_dec_blk(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
/* 3-way parallel cipher functions from twofish_x86_64-3way module */
asmlinkage void __twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src, bool xor);
asmlinkage void twofish_dec_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst, static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src) const u8 *src)
{ {
__twofish_enc_blk_3way(ctx, dst, src, false); __twofish_enc_blk_3way(ctx, dst, src, false);
} }
static inline void twofish_enc_blk_3way_xor(struct twofish_ctx *ctx, u8 *dst,
const u8 *src)
{
__twofish_enc_blk_3way(ctx, dst, src, true);
}
/* 8-way parallel cipher functions */ /* 8-way parallel cipher functions */
asmlinkage void __twofish_enc_blk_8way(struct twofish_ctx *ctx, u8 *dst, asmlinkage void __twofish_enc_blk_8way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src, bool xor); const u8 *src, bool xor);
...@@ -95,423 +75,142 @@ static inline void twofish_dec_blk_xway(struct twofish_ctx *ctx, u8 *dst, ...@@ -95,423 +75,142 @@ static inline void twofish_dec_blk_xway(struct twofish_ctx *ctx, u8 *dst,
twofish_dec_blk_8way(ctx, dst, src); twofish_dec_blk_8way(ctx, dst, src);
} }
static void twofish_dec_blk_cbc_xway(void *ctx, u128 *dst, const u128 *src)
static inline bool twofish_fpu_begin(bool fpu_enabled, unsigned int nbytes)
{ {
if (fpu_enabled) u128 ivs[TWOFISH_PARALLEL_BLOCKS - 1];
return true; unsigned int j;
/* AVX is only used when chunk to be processed is large enough, so for (j = 0; j < TWOFISH_PARALLEL_BLOCKS - 1; j++)
* do not enable FPU until it is necessary. ivs[j] = src[j];
*/
if (nbytes < TF_BLOCK_SIZE * TWOFISH_PARALLEL_BLOCKS)
return false;
kernel_fpu_begin(); twofish_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src);
return true;
}
static inline void twofish_fpu_end(bool fpu_enabled) for (j = 0; j < TWOFISH_PARALLEL_BLOCKS - 1; j++)
{ u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
if (fpu_enabled)
kernel_fpu_end();
} }
static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk, static void twofish_enc_blk_ctr_xway(void *ctx, u128 *dst, const u128 *src,
bool enc) u128 *iv)
{ {
bool fpu_enabled = false; be128 ctrblks[TWOFISH_PARALLEL_BLOCKS];
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); unsigned int i;
const unsigned int bsize = TF_BLOCK_SIZE;
unsigned int nbytes;
int err;
err = blkcipher_walk_virt(desc, walk); for (i = 0; i < TWOFISH_PARALLEL_BLOCKS; i++) {
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; if (dst != src)
dst[i] = src[i];
while ((nbytes = walk->nbytes)) { u128_to_be128(&ctrblks[i], iv);
u8 *wsrc = walk->src.virt.addr; u128_inc(iv);
u8 *wdst = walk->dst.virt.addr;
fpu_enabled = twofish_fpu_begin(fpu_enabled, nbytes);
/* Process multi-block batch */
if (nbytes >= bsize * TWOFISH_PARALLEL_BLOCKS) {
do {
if (enc)
twofish_enc_blk_xway(ctx, wdst, wsrc);
else
twofish_dec_blk_xway(ctx, wdst, wsrc);
wsrc += bsize * TWOFISH_PARALLEL_BLOCKS;
wdst += bsize * TWOFISH_PARALLEL_BLOCKS;
nbytes -= bsize * TWOFISH_PARALLEL_BLOCKS;
} while (nbytes >= bsize * TWOFISH_PARALLEL_BLOCKS);
if (nbytes < bsize)
goto done;
}
/* Process three block batch */
if (nbytes >= bsize * 3) {
do {
if (enc)
twofish_enc_blk_3way(ctx, wdst, wsrc);
else
twofish_dec_blk_3way(ctx, wdst, wsrc);
wsrc += bsize * 3;
wdst += bsize * 3;
nbytes -= bsize * 3;
} while (nbytes >= bsize * 3);
if (nbytes < bsize)
goto done;
}
/* Handle leftovers */
do {
if (enc)
twofish_enc_blk(ctx, wdst, wsrc);
else
twofish_dec_blk(ctx, wdst, wsrc);
wsrc += bsize;
wdst += bsize;
nbytes -= bsize;
} while (nbytes >= bsize);
done:
err = blkcipher_walk_done(desc, walk, nbytes);
} }
twofish_fpu_end(fpu_enabled); twofish_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
return err;
} }
static const struct common_glue_ctx twofish_enc = {
.num_funcs = 3,
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
.funcs = { {
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_xway) }
}, {
.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 = 3,
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
.funcs = { {
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
.fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_xway) }
}, {
.num_blocks = 3,
.fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way) }
}, {
.num_blocks = 1,
.fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr) }
} }
};
static const struct common_glue_ctx twofish_dec = {
.num_funcs = 3,
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
.funcs = { {
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
.fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_xway) }
}, {
.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 = 3,
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
.funcs = { {
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
.fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_xway) }
}, {
.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, true);
} }
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, false);
}
static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
struct blkcipher_walk *walk)
{
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
const 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);
const 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[TWOFISH_PARALLEL_BLOCKS - 1];
u128 last_iv;
int i;
/* Start of the last block. */
src += nbytes / bsize - 1;
dst += nbytes / bsize - 1;
last_iv = *src;
/* Process multi-block batch */
if (nbytes >= bsize * TWOFISH_PARALLEL_BLOCKS) {
do {
nbytes -= bsize * (TWOFISH_PARALLEL_BLOCKS - 1);
src -= TWOFISH_PARALLEL_BLOCKS - 1;
dst -= TWOFISH_PARALLEL_BLOCKS - 1;
for (i = 0; i < TWOFISH_PARALLEL_BLOCKS - 1; i++)
ivs[i] = src[i];
twofish_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src);
for (i = 0; i < TWOFISH_PARALLEL_BLOCKS - 1; i++)
u128_xor(dst + (i + 1), dst + (i + 1), ivs + i);
nbytes -= bsize;
if (nbytes < bsize)
goto done;
u128_xor(dst, dst, src - 1);
src -= 1;
dst -= 1;
} while (nbytes >= bsize * TWOFISH_PARALLEL_BLOCKS);
if (nbytes < bsize)
goto done;
}
/* 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)
{ {
bool fpu_enabled = false; return glue_cbc_decrypt_128bit(&twofish_dec_cbc, desc, dst, src,
struct blkcipher_walk walk; nbytes);
int err;
blkcipher_walk_init(&walk, dst, src, nbytes);
err = blkcipher_walk_virt(desc, &walk);
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
while ((nbytes = walk.nbytes)) {
fpu_enabled = twofish_fpu_begin(fpu_enabled, nbytes);
nbytes = __cbc_decrypt(desc, &walk);
err = blkcipher_walk_done(desc, &walk, nbytes);
}
twofish_fpu_end(fpu_enabled);
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) static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
{ struct scatterlist *src, unsigned int nbytes)
dst->a = be64_to_cpu(src->a);
dst->b = be64_to_cpu(src->b);
}
static inline void u128_inc(u128 *i)
{ {
i->b++; return glue_ctr_crypt_128bit(&twofish_ctr, desc, dst, src, nbytes);
if (!i->b)
i->a++;
} }
static void ctr_crypt_final(struct blkcipher_desc *desc, static inline bool twofish_fpu_begin(bool fpu_enabled, unsigned int nbytes)
struct blkcipher_walk *walk)
{ {
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); return glue_fpu_begin(TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS, NULL,
u8 *ctrblk = walk->iv; fpu_enabled, nbytes);
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, static inline void twofish_fpu_end(bool fpu_enabled)
struct blkcipher_walk *walk)
{
struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
const 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[TWOFISH_PARALLEL_BLOCKS];
int i;
be128_to_u128(&ctrblk, (be128 *)walk->iv);
/* Process multi-block batch */
if (nbytes >= bsize * TWOFISH_PARALLEL_BLOCKS) {
do {
/* create ctrblks for parallel encrypt */
for (i = 0; i < TWOFISH_PARALLEL_BLOCKS; i++) {
if (dst != src)
dst[i] = src[i];
u128_to_be128(&ctrblocks[i], &ctrblk);
u128_inc(&ctrblk);
}
twofish_enc_blk_xway_xor(ctx, (u8 *)dst,
(u8 *)ctrblocks);
src += TWOFISH_PARALLEL_BLOCKS;
dst += TWOFISH_PARALLEL_BLOCKS;
nbytes -= bsize * TWOFISH_PARALLEL_BLOCKS;
} while (nbytes >= bsize * TWOFISH_PARALLEL_BLOCKS);
if (nbytes < bsize)
goto done;
}
/* 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_3way_xor(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,
struct scatterlist *src, unsigned int nbytes)
{ {
bool fpu_enabled = false; glue_fpu_end(fpu_enabled);
struct blkcipher_walk walk;
int err;
blkcipher_walk_init(&walk, dst, src, nbytes);
err = blkcipher_walk_virt_block(desc, &walk, TF_BLOCK_SIZE);
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
while ((nbytes = walk.nbytes) >= TF_BLOCK_SIZE) {
fpu_enabled = twofish_fpu_begin(fpu_enabled, nbytes);
nbytes = __ctr_crypt(desc, &walk);
err = blkcipher_walk_done(desc, &walk, nbytes);
}
twofish_fpu_end(fpu_enabled);
if (walk.nbytes) {
ctr_crypt_final(desc, &walk);
err = blkcipher_walk_done(desc, &walk, 0);
}
return err;
} }
struct crypt_priv { struct crypt_priv {
...@@ -563,26 +262,6 @@ static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes) ...@@ -563,26 +262,6 @@ static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
twofish_dec_blk(ctx->ctx, srcdst, srcdst); twofish_dec_blk(ctx->ctx, srcdst, srcdst);
} }
struct twofish_lrw_ctx {
struct lrw_table_ctx lrw_table;
struct twofish_ctx twofish_ctx;
};
static int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen)
{
struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
int err;
err = __twofish_setkey(&ctx->twofish_ctx, key,
keylen - TF_BLOCK_SIZE, &tfm->crt_flags);
if (err)
return err;
return lrw_init_table(&ctx->lrw_table, key + keylen -
TF_BLOCK_SIZE);
}
static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
struct scatterlist *src, unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
{ {
...@@ -635,43 +314,6 @@ static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, ...@@ -635,43 +314,6 @@ static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
return ret; return ret;
} }
static void lrw_exit_tfm(struct crypto_tfm *tfm)
{
struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
lrw_free_table(&ctx->lrw_table);
}
struct twofish_xts_ctx {
struct twofish_ctx tweak_ctx;
struct twofish_ctx crypt_ctx;
};
static int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen)
{
struct twofish_xts_ctx *ctx = crypto_tfm_ctx(tfm);
u32 *flags = &tfm->crt_flags;
int err;
/* key consists of keys of equal size concatenated, therefore
* the length must be even
*/
if (keylen % 2) {
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
return -EINVAL;
}
/* first half of xts-key is for crypt */
err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
if (err)
return err;
/* second half of xts-key is for tweak */
return __twofish_setkey(&ctx->tweak_ctx,
key + keylen / 2, keylen / 2, flags);
}
static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
struct scatterlist *src, unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
{ {
...@@ -798,7 +440,7 @@ static struct crypto_alg twofish_algs[10] = { { ...@@ -798,7 +440,7 @@ static struct crypto_alg twofish_algs[10] = { {
.cra_type = &crypto_blkcipher_type, .cra_type = &crypto_blkcipher_type,
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
.cra_list = LIST_HEAD_INIT(twofish_algs[3].cra_list), .cra_list = LIST_HEAD_INIT(twofish_algs[3].cra_list),
.cra_exit = lrw_exit_tfm, .cra_exit = lrw_twofish_exit_tfm,
.cra_u = { .cra_u = {
.blkcipher = { .blkcipher = {
.min_keysize = TF_MIN_KEY_SIZE + .min_keysize = TF_MIN_KEY_SIZE +
......
...@@ -28,22 +28,12 @@ ...@@ -28,22 +28,12 @@
#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/twofish.h>
#include <asm/crypto/glue_helper.h> #include <asm/crypto/glue_helper.h>
#include <crypto/lrw.h> #include <crypto/lrw.h>
#include <crypto/xts.h> #include <crypto/xts.h>
/* regular block cipher functions from twofish_x86_64 module */
asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
asmlinkage void twofish_dec_blk(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
/* 3-way parallel cipher functions */
asmlinkage void __twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src, bool xor);
EXPORT_SYMBOL_GPL(__twofish_enc_blk_3way); EXPORT_SYMBOL_GPL(__twofish_enc_blk_3way);
asmlinkage void twofish_dec_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
EXPORT_SYMBOL_GPL(twofish_dec_blk_3way); EXPORT_SYMBOL_GPL(twofish_dec_blk_3way);
static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst, static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
...@@ -58,7 +48,7 @@ static inline void twofish_enc_blk_xor_3way(struct twofish_ctx *ctx, u8 *dst, ...@@ -58,7 +48,7 @@ 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 void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src) void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src)
{ {
u128 ivs[2]; u128 ivs[2];
...@@ -70,8 +60,9 @@ static void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src) ...@@ -70,8 +60,9 @@ static void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src)
u128_xor(&dst[1], &dst[1], &ivs[0]); u128_xor(&dst[1], &dst[1], &ivs[0]);
u128_xor(&dst[2], &dst[2], &ivs[1]); u128_xor(&dst[2], &dst[2], &ivs[1]);
} }
EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way);
static void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv)
{ {
be128 ctrblk; be128 ctrblk;
...@@ -84,8 +75,9 @@ static void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) ...@@ -84,8 +75,9 @@ static void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv)
twofish_enc_blk(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); twofish_enc_blk(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
u128_xor(dst, dst, (u128 *)&ctrblk); u128_xor(dst, dst, (u128 *)&ctrblk);
} }
EXPORT_SYMBOL_GPL(twofish_enc_blk_ctr);
static void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src, void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
u128 *iv) u128 *iv)
{ {
be128 ctrblks[3]; be128 ctrblks[3];
...@@ -105,6 +97,7 @@ static void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src, ...@@ -105,6 +97,7 @@ static void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
twofish_enc_blk_xor_3way(ctx, (u8 *)dst, (u8 *)ctrblks); twofish_enc_blk_xor_3way(ctx, (u8 *)dst, (u8 *)ctrblks);
} }
EXPORT_SYMBOL_GPL(twofish_enc_blk_ctr_3way);
static const struct common_glue_ctx twofish_enc = { static const struct common_glue_ctx twofish_enc = {
.num_funcs = 2, .num_funcs = 2,
...@@ -220,13 +213,8 @@ static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes) ...@@ -220,13 +213,8 @@ static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
twofish_dec_blk(ctx, srcdst, srcdst); twofish_dec_blk(ctx, srcdst, srcdst);
} }
struct twofish_lrw_ctx { int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
struct lrw_table_ctx lrw_table; unsigned int keylen)
struct twofish_ctx twofish_ctx;
};
static int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen)
{ {
struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm); struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
int err; int err;
...@@ -238,6 +226,7 @@ static int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key, ...@@ -238,6 +226,7 @@ static int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
return lrw_init_table(&ctx->lrw_table, key + keylen - TF_BLOCK_SIZE); return lrw_init_table(&ctx->lrw_table, key + keylen - TF_BLOCK_SIZE);
} }
EXPORT_SYMBOL_GPL(lrw_twofish_setkey);
static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
struct scatterlist *src, unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
...@@ -273,20 +262,16 @@ static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, ...@@ -273,20 +262,16 @@ static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
return lrw_crypt(desc, dst, src, nbytes, &req); return lrw_crypt(desc, dst, src, nbytes, &req);
} }
static void lrw_exit_tfm(struct crypto_tfm *tfm) void lrw_twofish_exit_tfm(struct crypto_tfm *tfm)
{ {
struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm); struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
lrw_free_table(&ctx->lrw_table); lrw_free_table(&ctx->lrw_table);
} }
EXPORT_SYMBOL_GPL(lrw_twofish_exit_tfm);
struct twofish_xts_ctx { int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
struct twofish_ctx tweak_ctx; unsigned int keylen)
struct twofish_ctx crypt_ctx;
};
static int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen)
{ {
struct twofish_xts_ctx *ctx = crypto_tfm_ctx(tfm); struct twofish_xts_ctx *ctx = crypto_tfm_ctx(tfm);
u32 *flags = &tfm->crt_flags; u32 *flags = &tfm->crt_flags;
...@@ -309,6 +294,7 @@ static int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key, ...@@ -309,6 +294,7 @@ static int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2, return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
flags); flags);
} }
EXPORT_SYMBOL_GPL(xts_twofish_setkey);
static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
struct scatterlist *src, unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
...@@ -419,7 +405,7 @@ static struct crypto_alg tf_algs[5] = { { ...@@ -419,7 +405,7 @@ static struct crypto_alg tf_algs[5] = { {
.cra_type = &crypto_blkcipher_type, .cra_type = &crypto_blkcipher_type,
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
.cra_list = LIST_HEAD_INIT(tf_algs[3].cra_list), .cra_list = LIST_HEAD_INIT(tf_algs[3].cra_list),
.cra_exit = lrw_exit_tfm, .cra_exit = lrw_twofish_exit_tfm,
.cra_u = { .cra_u = {
.blkcipher = { .blkcipher = {
.min_keysize = TF_MIN_KEY_SIZE + TF_BLOCK_SIZE, .min_keysize = TF_MIN_KEY_SIZE + TF_BLOCK_SIZE,
......
#ifndef ASM_X86_TWOFISH_H
#define ASM_X86_TWOFISH_H
#include <linux/crypto.h>
#include <crypto/twofish.h>
#include <crypto/lrw.h>
#include <crypto/b128ops.h>
struct twofish_lrw_ctx {
struct lrw_table_ctx lrw_table;
struct twofish_ctx twofish_ctx;
};
struct twofish_xts_ctx {
struct twofish_ctx tweak_ctx;
struct twofish_ctx crypt_ctx;
};
/* regular block cipher functions from twofish_x86_64 module */
asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
asmlinkage void twofish_dec_blk(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
/* 3-way parallel cipher functions */
asmlinkage void __twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src, bool xor);
asmlinkage void twofish_dec_blk_3way(struct twofish_ctx *ctx, u8 *dst,
const u8 *src);
/* helpers from twofish_x86_64-3way module */
extern void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src);
extern void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src,
u128 *iv);
extern void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
u128 *iv);
extern int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen);
extern void lrw_twofish_exit_tfm(struct crypto_tfm *tfm);
extern int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen);
#endif /* ASM_X86_TWOFISH_H */
...@@ -958,6 +958,7 @@ config CRYPTO_TWOFISH_AVX_X86_64 ...@@ -958,6 +958,7 @@ config CRYPTO_TWOFISH_AVX_X86_64
select CRYPTO_ALGAPI select CRYPTO_ALGAPI
select CRYPTO_CRYPTD select CRYPTO_CRYPTD
select CRYPTO_ABLK_HELPER_X86 select CRYPTO_ABLK_HELPER_X86
select CRYPTO_GLUE_HELPER_X86
select CRYPTO_TWOFISH_COMMON select CRYPTO_TWOFISH_COMMON
select CRYPTO_TWOFISH_X86_64 select CRYPTO_TWOFISH_X86_64
select CRYPTO_TWOFISH_X86_64_3WAY select CRYPTO_TWOFISH_X86_64_3WAY
......
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