2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-07-12 01:37:37 +08:00
|
|
|
/*
|
2018-12-03 17:47:34 +08:00
|
|
|
* Glue Code for the AVX assembler implementation of the Cast5 Cipher
|
2012-07-12 01:37:37 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Johannes Goetzfried
|
|
|
|
* <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
|
|
|
|
*/
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
#include <asm/crypto/glue_helper.h>
|
2012-07-12 01:37:37 +08:00
|
|
|
#include <crypto/algapi.h>
|
|
|
|
#include <crypto/cast5.h>
|
2018-02-20 15:48:13 +08:00
|
|
|
#include <crypto/internal/simd.h>
|
|
|
|
#include <linux/crypto.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
#define CAST5_PARALLEL_BLOCKS 16
|
|
|
|
|
2012-10-20 20:06:56 +08:00
|
|
|
asmlinkage void cast5_ecb_enc_16way(struct cast5_ctx *ctx, u8 *dst,
|
2012-07-12 01:37:37 +08:00
|
|
|
const u8 *src);
|
2012-10-20 20:06:56 +08:00
|
|
|
asmlinkage void cast5_ecb_dec_16way(struct cast5_ctx *ctx, u8 *dst,
|
|
|
|
const u8 *src);
|
|
|
|
asmlinkage void cast5_cbc_dec_16way(struct cast5_ctx *ctx, u8 *dst,
|
|
|
|
const u8 *src);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static int cast5_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
|
|
|
|
unsigned int keylen)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
2018-02-20 15:48:13 +08:00
|
|
|
return cast5_setkey(&tfm->base, key, keylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool cast5_fpu_begin(bool fpu_enabled, struct skcipher_walk *walk,
|
|
|
|
unsigned int nbytes)
|
|
|
|
{
|
2018-02-20 15:48:27 +08:00
|
|
|
return glue_fpu_begin(CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS,
|
|
|
|
walk, fpu_enabled, nbytes);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cast5_fpu_end(bool fpu_enabled)
|
|
|
|
{
|
|
|
|
return glue_fpu_end(fpu_enabled);
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static int ecb_crypt(struct skcipher_request *req, bool enc)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
|
|
|
bool fpu_enabled = false;
|
2018-02-20 15:48:13 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct cast5_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct skcipher_walk walk;
|
2012-07-12 01:37:37 +08:00
|
|
|
const unsigned int bsize = CAST5_BLOCK_SIZE;
|
|
|
|
unsigned int nbytes;
|
2012-10-20 20:06:56 +08:00
|
|
|
void (*fn)(struct cast5_ctx *ctx, u8 *dst, const u8 *src);
|
2012-07-12 01:37:37 +08:00
|
|
|
int err;
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
while ((nbytes = walk.nbytes)) {
|
|
|
|
u8 *wsrc = walk.src.virt.addr;
|
|
|
|
u8 *wdst = walk.dst.virt.addr;
|
2012-07-12 01:37:37 +08:00
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
fpu_enabled = cast5_fpu_begin(fpu_enabled, &walk, nbytes);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
/* Process multi-block batch */
|
|
|
|
if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
|
2018-02-20 15:48:12 +08:00
|
|
|
fn = (enc) ? cast5_ecb_enc_16way : cast5_ecb_dec_16way;
|
2012-07-12 01:37:37 +08:00
|
|
|
do {
|
2012-10-20 20:06:56 +08:00
|
|
|
fn(ctx, wdst, wsrc);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
wsrc += bsize * CAST5_PARALLEL_BLOCKS;
|
|
|
|
wdst += bsize * CAST5_PARALLEL_BLOCKS;
|
|
|
|
nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
|
|
|
|
} while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
|
|
|
|
|
|
|
|
if (nbytes < bsize)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-10-20 20:06:56 +08:00
|
|
|
fn = (enc) ? __cast5_encrypt : __cast5_decrypt;
|
|
|
|
|
2012-07-12 01:37:37 +08:00
|
|
|
/* Handle leftovers */
|
|
|
|
do {
|
2012-10-20 20:06:56 +08:00
|
|
|
fn(ctx, wdst, wsrc);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
wsrc += bsize;
|
|
|
|
wdst += bsize;
|
|
|
|
nbytes -= bsize;
|
|
|
|
} while (nbytes >= bsize);
|
|
|
|
|
|
|
|
done:
|
2018-02-20 15:48:13 +08:00
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cast5_fpu_end(fpu_enabled);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static int ecb_encrypt(struct skcipher_request *req)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
2018-02-20 15:48:13 +08:00
|
|
|
return ecb_crypt(req, true);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static int ecb_decrypt(struct skcipher_request *req)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
2018-02-20 15:48:13 +08:00
|
|
|
return ecb_crypt(req, false);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static int cbc_encrypt(struct skcipher_request *req)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
|
|
|
const unsigned int bsize = CAST5_BLOCK_SIZE;
|
2018-02-20 15:48:13 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct cast5_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
unsigned int nbytes;
|
2012-07-12 01:37:37 +08:00
|
|
|
int err;
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
while ((nbytes = walk.nbytes)) {
|
2018-02-20 15:48:13 +08:00
|
|
|
u64 *src = (u64 *)walk.src.virt.addr;
|
|
|
|
u64 *dst = (u64 *)walk.dst.virt.addr;
|
|
|
|
u64 *iv = (u64 *)walk.iv;
|
|
|
|
|
|
|
|
do {
|
|
|
|
*dst = *src ^ *iv;
|
|
|
|
__cast5_encrypt(ctx, (u8 *)dst, (u8 *)dst);
|
|
|
|
iv = dst;
|
|
|
|
src++;
|
|
|
|
dst++;
|
|
|
|
nbytes -= bsize;
|
|
|
|
} while (nbytes >= bsize);
|
|
|
|
|
|
|
|
*(u64 *)walk.iv = *iv;
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static unsigned int __cbc_decrypt(struct cast5_ctx *ctx,
|
|
|
|
struct skcipher_walk *walk)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
|
|
|
const unsigned int bsize = CAST5_BLOCK_SIZE;
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
u64 *src = (u64 *)walk->src.virt.addr;
|
|
|
|
u64 *dst = (u64 *)walk->dst.virt.addr;
|
|
|
|
u64 last_iv;
|
|
|
|
|
|
|
|
/* Start of the last block. */
|
|
|
|
src += nbytes / bsize - 1;
|
|
|
|
dst += nbytes / bsize - 1;
|
|
|
|
|
|
|
|
last_iv = *src;
|
|
|
|
|
|
|
|
/* Process multi-block batch */
|
|
|
|
if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
|
|
|
|
do {
|
|
|
|
nbytes -= bsize * (CAST5_PARALLEL_BLOCKS - 1);
|
|
|
|
src -= CAST5_PARALLEL_BLOCKS - 1;
|
|
|
|
dst -= CAST5_PARALLEL_BLOCKS - 1;
|
|
|
|
|
2012-10-20 20:06:56 +08:00
|
|
|
cast5_cbc_dec_16way(ctx, (u8 *)dst, (u8 *)src);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
nbytes -= bsize;
|
|
|
|
if (nbytes < bsize)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
*dst ^= *(src - 1);
|
|
|
|
src -= 1;
|
|
|
|
dst -= 1;
|
|
|
|
} while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle leftovers */
|
|
|
|
for (;;) {
|
|
|
|
__cast5_decrypt(ctx, (u8 *)dst, (u8 *)src);
|
|
|
|
|
|
|
|
nbytes -= bsize;
|
|
|
|
if (nbytes < bsize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*dst ^= *(src - 1);
|
|
|
|
src -= 1;
|
|
|
|
dst -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
*dst ^= *(u64 *)walk->iv;
|
|
|
|
*(u64 *)walk->iv = last_iv;
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static int cbc_decrypt(struct skcipher_request *req)
|
2012-07-12 01:37:37 +08:00
|
|
|
{
|
2018-02-20 15:48:13 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct cast5_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2012-07-12 01:37:37 +08:00
|
|
|
bool fpu_enabled = false;
|
2018-02-20 15:48:13 +08:00
|
|
|
struct skcipher_walk walk;
|
|
|
|
unsigned int nbytes;
|
2012-07-12 01:37:37 +08:00
|
|
|
int err;
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
while ((nbytes = walk.nbytes)) {
|
2018-02-20 15:48:13 +08:00
|
|
|
fpu_enabled = cast5_fpu_begin(fpu_enabled, &walk, nbytes);
|
|
|
|
nbytes = __cbc_decrypt(ctx, &walk);
|
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cast5_fpu_end(fpu_enabled);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static struct skcipher_alg cast5_algs[] = {
|
|
|
|
{
|
|
|
|
.base.cra_name = "__ecb(cast5)",
|
|
|
|
.base.cra_driver_name = "__ecb-cast5-avx",
|
|
|
|
.base.cra_priority = 200,
|
|
|
|
.base.cra_flags = CRYPTO_ALG_INTERNAL,
|
|
|
|
.base.cra_blocksize = CAST5_BLOCK_SIZE,
|
|
|
|
.base.cra_ctxsize = sizeof(struct cast5_ctx),
|
|
|
|
.base.cra_module = THIS_MODULE,
|
|
|
|
.min_keysize = CAST5_MIN_KEY_SIZE,
|
|
|
|
.max_keysize = CAST5_MAX_KEY_SIZE,
|
|
|
|
.setkey = cast5_setkey_skcipher,
|
|
|
|
.encrypt = ecb_encrypt,
|
|
|
|
.decrypt = ecb_decrypt,
|
|
|
|
}, {
|
|
|
|
.base.cra_name = "__cbc(cast5)",
|
|
|
|
.base.cra_driver_name = "__cbc-cast5-avx",
|
|
|
|
.base.cra_priority = 200,
|
|
|
|
.base.cra_flags = CRYPTO_ALG_INTERNAL,
|
|
|
|
.base.cra_blocksize = CAST5_BLOCK_SIZE,
|
|
|
|
.base.cra_ctxsize = sizeof(struct cast5_ctx),
|
|
|
|
.base.cra_module = THIS_MODULE,
|
|
|
|
.min_keysize = CAST5_MIN_KEY_SIZE,
|
|
|
|
.max_keysize = CAST5_MAX_KEY_SIZE,
|
|
|
|
.ivsize = CAST5_BLOCK_SIZE,
|
|
|
|
.setkey = cast5_setkey_skcipher,
|
|
|
|
.encrypt = cbc_encrypt,
|
|
|
|
.decrypt = cbc_decrypt,
|
|
|
|
}
|
|
|
|
};
|
2012-07-12 01:37:37 +08:00
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
static struct simd_skcipher_alg *cast5_simd_algs[ARRAY_SIZE(cast5_algs)];
|
2012-07-12 01:37:37 +08:00
|
|
|
|
|
|
|
static int __init cast5_init(void)
|
|
|
|
{
|
2015-04-28 16:11:24 +08:00
|
|
|
const char *feature_name;
|
2012-07-12 01:37:37 +08:00
|
|
|
|
2015-09-03 07:31:26 +08:00
|
|
|
if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
|
|
|
|
&feature_name)) {
|
2015-04-28 16:11:24 +08:00
|
|
|
pr_info("CPU feature '%s' is not supported.\n", feature_name);
|
2012-07-12 01:37:37 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:48:13 +08:00
|
|
|
return simd_register_skciphers_compat(cast5_algs,
|
|
|
|
ARRAY_SIZE(cast5_algs),
|
|
|
|
cast5_simd_algs);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit cast5_exit(void)
|
|
|
|
{
|
2018-02-20 15:48:13 +08:00
|
|
|
simd_unregister_skciphers(cast5_algs, ARRAY_SIZE(cast5_algs),
|
|
|
|
cast5_simd_algs);
|
2012-07-12 01:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(cast5_init);
|
|
|
|
module_exit(cast5_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
|
|
|
|
MODULE_LICENSE("GPL");
|
2014-11-21 09:05:53 +08:00
|
|
|
MODULE_ALIAS_CRYPTO("cast5");
|