1
0
Fork 0

crypto: x86/des3_ede - convert to skcipher interface

Convert the x86 asm implementation of Triple DES 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>
hifive-unleashed-5.1
Eric Biggers 2018-02-19 23:48:17 -08:00 committed by Herbert Xu
parent c1679171c6
commit 09c0f03bf8
2 changed files with 120 additions and 120 deletions

View File

@ -20,13 +20,13 @@
* *
*/ */
#include <asm/processor.h> #include <crypto/algapi.h>
#include <crypto/des.h> #include <crypto/des.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>
struct des3_ede_x86_ctx { struct des3_ede_x86_ctx {
u32 enc_expkey[DES3_EDE_EXPKEY_WORDS]; u32 enc_expkey[DES3_EDE_EXPKEY_WORDS];
@ -83,18 +83,18 @@ static void des3_ede_x86_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src); des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src);
} }
static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk, static int ecb_crypt(struct skcipher_request *req, const u32 *expkey)
const u32 *expkey)
{ {
unsigned int bsize = DES3_EDE_BLOCK_SIZE; const unsigned int bsize = DES3_EDE_BLOCK_SIZE;
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 * 3) { if (nbytes >= bsize * 3) {
@ -121,36 +121,31 @@ 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 des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
blkcipher_walk_init(&walk, dst, src, nbytes); return ecb_crypt(req, ctx->enc_expkey);
return ecb_crypt(desc, &walk, ctx->enc_expkey);
} }
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 des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
blkcipher_walk_init(&walk, dst, src, nbytes); return ecb_crypt(req, ctx->dec_expkey);
return ecb_crypt(desc, &walk, ctx->dec_expkey);
} }
static unsigned int __cbc_encrypt(struct blkcipher_desc *desc, static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = DES3_EDE_BLOCK_SIZE; unsigned int bsize = DES3_EDE_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;
@ -171,27 +166,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 des3_ede_x86_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 des3_ede_x86_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = DES3_EDE_BLOCK_SIZE; unsigned int bsize = DES3_EDE_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;
@ -250,25 +245,26 @@ 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 des3_ede_x86_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 des3_ede_x86_ctx *ctx, static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
u8 *ctrblk = walk->iv; u8 *ctrblk = walk->iv;
u8 keystream[DES3_EDE_BLOCK_SIZE]; u8 keystream[DES3_EDE_BLOCK_SIZE];
@ -282,10 +278,9 @@ static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE); crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE);
} }
static unsigned int __ctr_crypt(struct blkcipher_desc *desc, static unsigned int __ctr_crypt(struct des3_ede_x86_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
struct des3_ede_x86_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
unsigned int bsize = DES3_EDE_BLOCK_SIZE; unsigned int bsize = DES3_EDE_BLOCK_SIZE;
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
__be64 *src = (__be64 *)walk->src.virt.addr; __be64 *src = (__be64 *)walk->src.virt.addr;
@ -333,23 +328,24 @@ 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 des3_ede_x86_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, DES3_EDE_BLOCK_SIZE);
while ((nbytes = walk.nbytes) >= DES3_EDE_BLOCK_SIZE) { while ((nbytes = walk.nbytes) >= DES3_EDE_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;
@ -381,7 +377,14 @@ static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
return 0; return 0;
} }
static struct crypto_alg des3_ede_algs[4] = { { static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
const u8 *key,
unsigned int keylen)
{
return des3_ede_x86_setkey(&tfm->base, key, keylen);
}
static struct crypto_alg des3_ede_cipher = {
.cra_name = "des3_ede", .cra_name = "des3_ede",
.cra_driver_name = "des3_ede-asm", .cra_driver_name = "des3_ede-asm",
.cra_priority = 200, .cra_priority = 200,
@ -399,66 +402,50 @@ static struct crypto_alg des3_ede_algs[4] = { {
.cia_decrypt = des3_ede_x86_decrypt, .cia_decrypt = des3_ede_x86_decrypt,
} }
} }
}, { };
.cra_name = "ecb(des3_ede)",
.cra_driver_name = "ecb-des3_ede-asm", struct skcipher_alg des3_ede_skciphers[] = {
.cra_priority = 300, {
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .base.cra_name = "ecb(des3_ede)",
.cra_blocksize = DES3_EDE_BLOCK_SIZE, .base.cra_driver_name = "ecb-des3_ede-asm",
.cra_ctxsize = sizeof(struct des3_ede_x86_ctx), .base.cra_priority = 300,
.cra_alignmask = 0, .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
.cra_type = &crypto_blkcipher_type, .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
.cra_module = THIS_MODULE, .base.cra_module = THIS_MODULE,
.cra_u = { .min_keysize = DES3_EDE_KEY_SIZE,
.blkcipher = { .max_keysize = DES3_EDE_KEY_SIZE,
.min_keysize = DES3_EDE_KEY_SIZE, .setkey = des3_ede_x86_setkey_skcipher,
.max_keysize = DES3_EDE_KEY_SIZE, .encrypt = ecb_encrypt,
.setkey = des3_ede_x86_setkey, .decrypt = ecb_decrypt,
.encrypt = ecb_encrypt, }, {
.decrypt = ecb_decrypt, .base.cra_name = "cbc(des3_ede)",
}, .base.cra_driver_name = "cbc-des3_ede-asm",
}, .base.cra_priority = 300,
}, { .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
.cra_name = "cbc(des3_ede)", .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
.cra_driver_name = "cbc-des3_ede-asm", .base.cra_module = THIS_MODULE,
.cra_priority = 300, .min_keysize = DES3_EDE_KEY_SIZE,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .max_keysize = DES3_EDE_KEY_SIZE,
.cra_blocksize = DES3_EDE_BLOCK_SIZE, .ivsize = DES3_EDE_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct des3_ede_x86_ctx), .setkey = des3_ede_x86_setkey_skcipher,
.cra_alignmask = 0, .encrypt = cbc_encrypt,
.cra_type = &crypto_blkcipher_type, .decrypt = cbc_decrypt,
.cra_module = THIS_MODULE, }, {
.cra_u = { .base.cra_name = "ctr(des3_ede)",
.blkcipher = { .base.cra_driver_name = "ctr-des3_ede-asm",
.min_keysize = DES3_EDE_KEY_SIZE, .base.cra_priority = 300,
.max_keysize = DES3_EDE_KEY_SIZE, .base.cra_blocksize = 1,
.ivsize = DES3_EDE_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
.setkey = des3_ede_x86_setkey, .base.cra_module = THIS_MODULE,
.encrypt = cbc_encrypt, .min_keysize = DES3_EDE_KEY_SIZE,
.decrypt = cbc_decrypt, .max_keysize = DES3_EDE_KEY_SIZE,
}, .ivsize = DES3_EDE_BLOCK_SIZE,
}, .chunksize = DES3_EDE_BLOCK_SIZE,
}, { .setkey = des3_ede_x86_setkey_skcipher,
.cra_name = "ctr(des3_ede)", .encrypt = ctr_crypt,
.cra_driver_name = "ctr-des3_ede-asm", .decrypt = ctr_crypt,
.cra_priority = 300, }
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, };
.cra_blocksize = 1,
.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
.cra_alignmask = 0,
.cra_type = &crypto_blkcipher_type,
.cra_module = THIS_MODULE,
.cra_u = {
.blkcipher = {
.min_keysize = DES3_EDE_KEY_SIZE,
.max_keysize = DES3_EDE_KEY_SIZE,
.ivsize = DES3_EDE_BLOCK_SIZE,
.setkey = des3_ede_x86_setkey,
.encrypt = ctr_crypt,
.decrypt = ctr_crypt,
},
},
} };
static bool is_blacklisted_cpu(void) static bool is_blacklisted_cpu(void)
{ {
@ -483,17 +470,30 @@ MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
static int __init des3_ede_x86_init(void) static int __init des3_ede_x86_init(void)
{ {
int err;
if (!force && is_blacklisted_cpu()) { if (!force && is_blacklisted_cpu()) {
pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n"); pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
return -ENODEV; return -ENODEV;
} }
return crypto_register_algs(des3_ede_algs, ARRAY_SIZE(des3_ede_algs)); err = crypto_register_alg(&des3_ede_cipher);
if (err)
return err;
err = crypto_register_skciphers(des3_ede_skciphers,
ARRAY_SIZE(des3_ede_skciphers));
if (err)
crypto_unregister_alg(&des3_ede_cipher);
return err;
} }
static void __exit des3_ede_x86_fini(void) static void __exit des3_ede_x86_fini(void)
{ {
crypto_unregister_algs(des3_ede_algs, ARRAY_SIZE(des3_ede_algs)); crypto_unregister_alg(&des3_ede_cipher);
crypto_unregister_skciphers(des3_ede_skciphers,
ARRAY_SIZE(des3_ede_skciphers));
} }
module_init(des3_ede_x86_init); module_init(des3_ede_x86_init);

View File

@ -1291,7 +1291,7 @@ config CRYPTO_DES_SPARC64
config CRYPTO_DES3_EDE_X86_64 config CRYPTO_DES3_EDE_X86_64
tristate "Triple DES EDE cipher algorithm (x86-64)" tristate "Triple DES EDE cipher algorithm (x86-64)"
depends on X86 && 64BIT depends on X86 && 64BIT
select CRYPTO_ALGAPI select CRYPTO_BLKCIPHER
select CRYPTO_DES select CRYPTO_DES
help help
Triple DES EDE (FIPS 46-3) algorithm. Triple DES EDE (FIPS 46-3) algorithm.