crypto: x86/blowfish: convert to skcipher interface

Convert the x86 asm implementation of Blowfish from the (deprecated)
blkcipher interface over to the skcipher interface.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Eric Biggers 2018-02-19 23:48:16 -08:00 committed by Herbert Xu
parent 4bd9692431
commit c1679171c6
2 changed files with 114 additions and 118 deletions

View File

@ -25,13 +25,13 @@
* *
*/ */
#include <asm/processor.h> #include <crypto/algapi.h>
#include <crypto/blowfish.h> #include <crypto/blowfish.h>
#include <crypto/internal/skcipher.h>
#include <linux/crypto.h> #include <linux/crypto.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/types.h> #include <linux/types.h>
#include <crypto/algapi.h>
/* regular block cipher functions */ /* regular block cipher functions */
asmlinkage void __blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src, asmlinkage void __blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src,
@ -77,20 +77,28 @@ static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src); blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
} }
static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk, static int blowfish_setkey_skcipher(struct crypto_skcipher *tfm,
const u8 *key, unsigned int keylen)
{
return blowfish_setkey(&tfm->base, key, keylen);
}
static int ecb_crypt(struct skcipher_request *req,
void (*fn)(struct bf_ctx *, u8 *, const u8 *), void (*fn)(struct bf_ctx *, u8 *, const u8 *),
void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *)) void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *))
{ {
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = BF_BLOCK_SIZE; unsigned int bsize = BF_BLOCK_SIZE;
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
struct skcipher_walk walk;
unsigned int nbytes; unsigned int nbytes;
int err; int err;
err = blkcipher_walk_virt(desc, walk); err = skcipher_walk_virt(&walk, req, false);
while ((nbytes = walk->nbytes)) { while ((nbytes = walk.nbytes)) {
u8 *wsrc = walk->src.virt.addr; u8 *wsrc = walk.src.virt.addr;
u8 *wdst = walk->dst.virt.addr; u8 *wdst = walk.dst.virt.addr;
/* Process four block batch */ /* Process four block batch */
if (nbytes >= bsize * 4) { if (nbytes >= bsize * 4) {
@ -116,34 +124,25 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
} while (nbytes >= bsize); } while (nbytes >= bsize);
done: done:
err = blkcipher_walk_done(desc, walk, nbytes); err = skcipher_walk_done(&walk, nbytes);
} }
return err; return err;
} }
static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int ecb_encrypt(struct skcipher_request *req)
struct scatterlist *src, unsigned int nbytes)
{ {
struct blkcipher_walk walk; return ecb_crypt(req, blowfish_enc_blk, blowfish_enc_blk_4way);
blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_crypt(desc, &walk, blowfish_enc_blk, blowfish_enc_blk_4way);
} }
static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int ecb_decrypt(struct skcipher_request *req)
struct scatterlist *src, unsigned int nbytes)
{ {
struct blkcipher_walk walk; return ecb_crypt(req, blowfish_dec_blk, blowfish_dec_blk_4way);
blkcipher_walk_init(&walk, dst, src, nbytes);
return ecb_crypt(desc, &walk, blowfish_dec_blk, blowfish_dec_blk_4way);
} }
static unsigned int __cbc_encrypt(struct blkcipher_desc *desc, static unsigned int __cbc_encrypt(struct bf_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = BF_BLOCK_SIZE; unsigned int bsize = BF_BLOCK_SIZE;
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
u64 *src = (u64 *)walk->src.virt.addr; u64 *src = (u64 *)walk->src.virt.addr;
@ -164,27 +163,27 @@ static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
return nbytes; return nbytes;
} }
static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int cbc_encrypt(struct skcipher_request *req)
struct scatterlist *src, unsigned int nbytes)
{ {
struct blkcipher_walk walk; struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
struct skcipher_walk walk;
unsigned int nbytes;
int err; int err;
blkcipher_walk_init(&walk, dst, src, nbytes); err = skcipher_walk_virt(&walk, req, false);
err = blkcipher_walk_virt(desc, &walk);
while ((nbytes = walk.nbytes)) { while ((nbytes = walk.nbytes)) {
nbytes = __cbc_encrypt(desc, &walk); nbytes = __cbc_encrypt(ctx, &walk);
err = blkcipher_walk_done(desc, &walk, nbytes); err = skcipher_walk_done(&walk, nbytes);
} }
return err; return err;
} }
static unsigned int __cbc_decrypt(struct blkcipher_desc *desc, static unsigned int __cbc_decrypt(struct bf_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = BF_BLOCK_SIZE; unsigned int bsize = BF_BLOCK_SIZE;
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
u64 *src = (u64 *)walk->src.virt.addr; u64 *src = (u64 *)walk->src.virt.addr;
@ -245,24 +244,25 @@ done:
return nbytes; return nbytes;
} }
static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int cbc_decrypt(struct skcipher_request *req)
struct scatterlist *src, unsigned int nbytes)
{ {
struct blkcipher_walk walk; struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
struct skcipher_walk walk;
unsigned int nbytes;
int err; int err;
blkcipher_walk_init(&walk, dst, src, nbytes); err = skcipher_walk_virt(&walk, req, false);
err = blkcipher_walk_virt(desc, &walk);
while ((nbytes = walk.nbytes)) { while ((nbytes = walk.nbytes)) {
nbytes = __cbc_decrypt(desc, &walk); nbytes = __cbc_decrypt(ctx, &walk);
err = blkcipher_walk_done(desc, &walk, nbytes); err = skcipher_walk_done(&walk, nbytes);
} }
return err; return err;
} }
static void ctr_crypt_final(struct bf_ctx *ctx, struct blkcipher_walk *walk) static void ctr_crypt_final(struct bf_ctx *ctx, struct skcipher_walk *walk)
{ {
u8 *ctrblk = walk->iv; u8 *ctrblk = walk->iv;
u8 keystream[BF_BLOCK_SIZE]; u8 keystream[BF_BLOCK_SIZE];
@ -276,10 +276,8 @@ static void ctr_crypt_final(struct bf_ctx *ctx, struct blkcipher_walk *walk)
crypto_inc(ctrblk, BF_BLOCK_SIZE); crypto_inc(ctrblk, BF_BLOCK_SIZE);
} }
static unsigned int __ctr_crypt(struct blkcipher_desc *desc, static unsigned int __ctr_crypt(struct bf_ctx *ctx, struct skcipher_walk *walk)
struct blkcipher_walk *walk)
{ {
struct bf_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = BF_BLOCK_SIZE; unsigned int bsize = BF_BLOCK_SIZE;
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
u64 *src = (u64 *)walk->src.virt.addr; u64 *src = (u64 *)walk->src.virt.addr;
@ -332,29 +330,30 @@ done:
return nbytes; return nbytes;
} }
static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, static int ctr_crypt(struct skcipher_request *req)
struct scatterlist *src, unsigned int nbytes)
{ {
struct blkcipher_walk walk; struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
struct skcipher_walk walk;
unsigned int nbytes;
int err; int err;
blkcipher_walk_init(&walk, dst, src, nbytes); err = skcipher_walk_virt(&walk, req, false);
err = blkcipher_walk_virt_block(desc, &walk, BF_BLOCK_SIZE);
while ((nbytes = walk.nbytes) >= BF_BLOCK_SIZE) { while ((nbytes = walk.nbytes) >= BF_BLOCK_SIZE) {
nbytes = __ctr_crypt(desc, &walk); nbytes = __ctr_crypt(ctx, &walk);
err = blkcipher_walk_done(desc, &walk, nbytes); err = skcipher_walk_done(&walk, nbytes);
} }
if (walk.nbytes) { if (nbytes) {
ctr_crypt_final(crypto_blkcipher_ctx(desc->tfm), &walk); ctr_crypt_final(ctx, &walk);
err = blkcipher_walk_done(desc, &walk, 0); err = skcipher_walk_done(&walk, 0);
} }
return err; return err;
} }
static struct crypto_alg bf_algs[4] = { { static struct crypto_alg bf_cipher_alg = {
.cra_name = "blowfish", .cra_name = "blowfish",
.cra_driver_name = "blowfish-asm", .cra_driver_name = "blowfish-asm",
.cra_priority = 200, .cra_priority = 200,
@ -372,66 +371,50 @@ static struct crypto_alg bf_algs[4] = { {
.cia_decrypt = blowfish_decrypt, .cia_decrypt = blowfish_decrypt,
} }
} }
}, { };
.cra_name = "ecb(blowfish)",
.cra_driver_name = "ecb-blowfish-asm", static struct skcipher_alg bf_skcipher_algs[] = {
.cra_priority = 300, {
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .base.cra_name = "ecb(blowfish)",
.cra_blocksize = BF_BLOCK_SIZE, .base.cra_driver_name = "ecb-blowfish-asm",
.cra_ctxsize = sizeof(struct bf_ctx), .base.cra_priority = 300,
.cra_alignmask = 0, .base.cra_blocksize = BF_BLOCK_SIZE,
.cra_type = &crypto_blkcipher_type, .base.cra_ctxsize = sizeof(struct bf_ctx),
.cra_module = THIS_MODULE, .base.cra_module = THIS_MODULE,
.cra_u = { .min_keysize = BF_MIN_KEY_SIZE,
.blkcipher = { .max_keysize = BF_MAX_KEY_SIZE,
.min_keysize = BF_MIN_KEY_SIZE, .setkey = blowfish_setkey_skcipher,
.max_keysize = BF_MAX_KEY_SIZE, .encrypt = ecb_encrypt,
.setkey = blowfish_setkey, .decrypt = ecb_decrypt,
.encrypt = ecb_encrypt, }, {
.decrypt = ecb_decrypt, .base.cra_name = "cbc(blowfish)",
}, .base.cra_driver_name = "cbc-blowfish-asm",
.base.cra_priority = 300,
.base.cra_blocksize = BF_BLOCK_SIZE,
.base.cra_ctxsize = sizeof(struct bf_ctx),
.base.cra_module = THIS_MODULE,
.min_keysize = BF_MIN_KEY_SIZE,
.max_keysize = BF_MAX_KEY_SIZE,
.ivsize = BF_BLOCK_SIZE,
.setkey = blowfish_setkey_skcipher,
.encrypt = cbc_encrypt,
.decrypt = cbc_decrypt,
}, {
.base.cra_name = "ctr(blowfish)",
.base.cra_driver_name = "ctr-blowfish-asm",
.base.cra_priority = 300,
.base.cra_blocksize = 1,
.base.cra_ctxsize = sizeof(struct bf_ctx),
.base.cra_module = THIS_MODULE,
.min_keysize = BF_MIN_KEY_SIZE,
.max_keysize = BF_MAX_KEY_SIZE,
.ivsize = BF_BLOCK_SIZE,
.chunksize = BF_BLOCK_SIZE,
.setkey = blowfish_setkey_skcipher,
.encrypt = ctr_crypt,
.decrypt = ctr_crypt,
}, },
}, { };
.cra_name = "cbc(blowfish)",
.cra_driver_name = "cbc-blowfish-asm",
.cra_priority = 300,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
.cra_blocksize = BF_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct bf_ctx),
.cra_alignmask = 0,
.cra_type = &crypto_blkcipher_type,
.cra_module = THIS_MODULE,
.cra_u = {
.blkcipher = {
.min_keysize = BF_MIN_KEY_SIZE,
.max_keysize = BF_MAX_KEY_SIZE,
.ivsize = BF_BLOCK_SIZE,
.setkey = blowfish_setkey,
.encrypt = cbc_encrypt,
.decrypt = cbc_decrypt,
},
},
}, {
.cra_name = "ctr(blowfish)",
.cra_driver_name = "ctr-blowfish-asm",
.cra_priority = 300,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
.cra_blocksize = 1,
.cra_ctxsize = sizeof(struct bf_ctx),
.cra_alignmask = 0,
.cra_type = &crypto_blkcipher_type,
.cra_module = THIS_MODULE,
.cra_u = {
.blkcipher = {
.min_keysize = BF_MIN_KEY_SIZE,
.max_keysize = BF_MAX_KEY_SIZE,
.ivsize = BF_BLOCK_SIZE,
.setkey = blowfish_setkey,
.encrypt = ctr_crypt,
.decrypt = ctr_crypt,
},
},
} };
static bool is_blacklisted_cpu(void) static bool is_blacklisted_cpu(void)
{ {
@ -456,6 +439,8 @@ MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
static int __init init(void) static int __init init(void)
{ {
int err;
if (!force && is_blacklisted_cpu()) { if (!force && is_blacklisted_cpu()) {
printk(KERN_INFO printk(KERN_INFO
"blowfish-x86_64: performance on this CPU " "blowfish-x86_64: performance on this CPU "
@ -464,12 +449,23 @@ static int __init init(void)
return -ENODEV; return -ENODEV;
} }
return crypto_register_algs(bf_algs, ARRAY_SIZE(bf_algs)); err = crypto_register_alg(&bf_cipher_alg);
if (err)
return err;
err = crypto_register_skciphers(bf_skcipher_algs,
ARRAY_SIZE(bf_skcipher_algs));
if (err)
crypto_unregister_alg(&bf_cipher_alg);
return err;
} }
static void __exit fini(void) static void __exit fini(void)
{ {
crypto_unregister_algs(bf_algs, ARRAY_SIZE(bf_algs)); crypto_unregister_alg(&bf_cipher_alg);
crypto_unregister_skciphers(bf_skcipher_algs,
ARRAY_SIZE(bf_skcipher_algs));
} }
module_init(init); module_init(init);

View File

@ -1114,7 +1114,7 @@ config CRYPTO_BLOWFISH_COMMON
config CRYPTO_BLOWFISH_X86_64 config CRYPTO_BLOWFISH_X86_64
tristate "Blowfish cipher algorithm (x86_64)" tristate "Blowfish cipher algorithm (x86_64)"
depends on X86 && 64BIT depends on X86 && 64BIT
select CRYPTO_ALGAPI select CRYPTO_BLKCIPHER
select CRYPTO_BLOWFISH_COMMON select CRYPTO_BLOWFISH_COMMON
help help
Blowfish cipher algorithm (x86_64), by Bruce Schneier. Blowfish cipher algorithm (x86_64), by Bruce Schneier.