Commit 64db5e74 authored by Eric Biggers's avatar Eric Biggers Committed by Herbert Xu

crypto: sparc/aes - convert to skcipher API

Convert the glue code for the SPARC64 AES opcodes implementations of
AES-ECB, AES-CBC, and AES-CTR from the deprecated "blkcipher" API to the
"skcipher" API.  This is needed in order for the blkcipher API to be
removed.
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent fcf801cc
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <crypto/algapi.h> #include <crypto/algapi.h>
#include <crypto/aes.h> #include <crypto/aes.h>
#include <crypto/internal/skcipher.h>
#include <asm/fpumacro.h> #include <asm/fpumacro.h>
#include <asm/pstate.h> #include <asm/pstate.h>
...@@ -197,6 +198,12 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, ...@@ -197,6 +198,12 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
return 0; return 0;
} }
static int aes_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key,
unsigned int key_len)
{
return aes_set_key(crypto_skcipher_tfm(tfm), in_key, key_len);
}
static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
{ {
struct crypto_sparc64_aes_ctx *ctx = crypto_tfm_ctx(tfm); struct crypto_sparc64_aes_ctx *ctx = crypto_tfm_ctx(tfm);
...@@ -211,131 +218,108 @@ static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) ...@@ -211,131 +218,108 @@ static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ctx->ops->decrypt(&ctx->key[0], (const u32 *) src, (u32 *) dst); ctx->ops->decrypt(&ctx->key[0], (const u32 *) src, (u32 *) dst);
} }
#define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1)) static int ecb_encrypt(struct skcipher_request *req)
static int ecb_encrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; const struct crypto_sparc64_aes_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, true);
err = blkcipher_walk_virt(desc, &walk); if (err)
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; return err;
ctx->ops->load_encrypt_keys(&ctx->key[0]); ctx->ops->load_encrypt_keys(&ctx->key[0]);
while ((nbytes = walk.nbytes)) { while ((nbytes = walk.nbytes) != 0) {
unsigned int block_len = nbytes & AES_BLOCK_MASK; ctx->ops->ecb_encrypt(&ctx->key[0], walk.src.virt.addr,
walk.dst.virt.addr,
if (likely(block_len)) { round_down(nbytes, AES_BLOCK_SIZE));
ctx->ops->ecb_encrypt(&ctx->key[0], err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
(const u64 *)walk.src.virt.addr,
(u64 *) walk.dst.virt.addr,
block_len);
}
nbytes &= AES_BLOCK_SIZE - 1;
err = blkcipher_walk_done(desc, &walk, nbytes);
} }
fprs_write(0); fprs_write(0);
return err; return err;
} }
static int ecb_decrypt(struct blkcipher_desc *desc, static int ecb_decrypt(struct skcipher_request *req)
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; const struct crypto_sparc64_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
u64 *key_end; const u64 *key_end;
struct skcipher_walk walk;
unsigned int nbytes;
int err; int err;
blkcipher_walk_init(&walk, dst, src, nbytes); err = skcipher_walk_virt(&walk, req, true);
err = blkcipher_walk_virt(desc, &walk); if (err)
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; return err;
ctx->ops->load_decrypt_keys(&ctx->key[0]); ctx->ops->load_decrypt_keys(&ctx->key[0]);
key_end = &ctx->key[ctx->expanded_key_length / sizeof(u64)]; key_end = &ctx->key[ctx->expanded_key_length / sizeof(u64)];
while ((nbytes = walk.nbytes)) { while ((nbytes = walk.nbytes) != 0) {
unsigned int block_len = nbytes & AES_BLOCK_MASK; ctx->ops->ecb_decrypt(key_end, walk.src.virt.addr,
walk.dst.virt.addr,
if (likely(block_len)) { round_down(nbytes, AES_BLOCK_SIZE));
ctx->ops->ecb_decrypt(key_end, err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
(const u64 *) walk.src.virt.addr,
(u64 *) walk.dst.virt.addr, block_len);
}
nbytes &= AES_BLOCK_SIZE - 1;
err = blkcipher_walk_done(desc, &walk, nbytes);
} }
fprs_write(0); fprs_write(0);
return err; return err;
} }
static int cbc_encrypt(struct blkcipher_desc *desc, static int cbc_encrypt(struct skcipher_request *req)
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; const struct crypto_sparc64_aes_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, true);
err = blkcipher_walk_virt(desc, &walk); if (err)
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; return err;
ctx->ops->load_encrypt_keys(&ctx->key[0]); ctx->ops->load_encrypt_keys(&ctx->key[0]);
while ((nbytes = walk.nbytes)) { while ((nbytes = walk.nbytes) != 0) {
unsigned int block_len = nbytes & AES_BLOCK_MASK; ctx->ops->cbc_encrypt(&ctx->key[0], walk.src.virt.addr,
walk.dst.virt.addr,
if (likely(block_len)) { round_down(nbytes, AES_BLOCK_SIZE),
ctx->ops->cbc_encrypt(&ctx->key[0], walk.iv);
(const u64 *)walk.src.virt.addr, err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
(u64 *) walk.dst.virt.addr,
block_len, (u64 *) walk.iv);
}
nbytes &= AES_BLOCK_SIZE - 1;
err = blkcipher_walk_done(desc, &walk, nbytes);
} }
fprs_write(0); fprs_write(0);
return err; return err;
} }
static int cbc_decrypt(struct blkcipher_desc *desc, static int cbc_decrypt(struct skcipher_request *req)
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; const struct crypto_sparc64_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
u64 *key_end; const u64 *key_end;
struct skcipher_walk walk;
unsigned int nbytes;
int err; int err;
blkcipher_walk_init(&walk, dst, src, nbytes); err = skcipher_walk_virt(&walk, req, true);
err = blkcipher_walk_virt(desc, &walk); if (err)
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; return err;
ctx->ops->load_decrypt_keys(&ctx->key[0]); ctx->ops->load_decrypt_keys(&ctx->key[0]);
key_end = &ctx->key[ctx->expanded_key_length / sizeof(u64)]; key_end = &ctx->key[ctx->expanded_key_length / sizeof(u64)];
while ((nbytes = walk.nbytes)) { while ((nbytes = walk.nbytes) != 0) {
unsigned int block_len = nbytes & AES_BLOCK_MASK; ctx->ops->cbc_decrypt(key_end, walk.src.virt.addr,
walk.dst.virt.addr,
if (likely(block_len)) { round_down(nbytes, AES_BLOCK_SIZE),
ctx->ops->cbc_decrypt(key_end, walk.iv);
(const u64 *) walk.src.virt.addr, err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
(u64 *) walk.dst.virt.addr,
block_len, (u64 *) walk.iv);
}
nbytes &= AES_BLOCK_SIZE - 1;
err = blkcipher_walk_done(desc, &walk, nbytes);
} }
fprs_write(0); fprs_write(0);
return err; return err;
} }
static void ctr_crypt_final(struct crypto_sparc64_aes_ctx *ctx, static void ctr_crypt_final(const struct crypto_sparc64_aes_ctx *ctx,
struct blkcipher_walk *walk) struct skcipher_walk *walk)
{ {
u8 *ctrblk = walk->iv; u8 *ctrblk = walk->iv;
u64 keystream[AES_BLOCK_SIZE / sizeof(u64)]; u64 keystream[AES_BLOCK_SIZE / sizeof(u64)];
...@@ -349,40 +333,35 @@ static void ctr_crypt_final(struct crypto_sparc64_aes_ctx *ctx, ...@@ -349,40 +333,35 @@ static void ctr_crypt_final(struct crypto_sparc64_aes_ctx *ctx,
crypto_inc(ctrblk, AES_BLOCK_SIZE); crypto_inc(ctrblk, AES_BLOCK_SIZE);
} }
static int ctr_crypt(struct blkcipher_desc *desc, static int ctr_crypt(struct skcipher_request *req)
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct blkcipher_walk walk; const struct crypto_sparc64_aes_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, true);
err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE); if (err)
desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; return err;
ctx->ops->load_encrypt_keys(&ctx->key[0]); ctx->ops->load_encrypt_keys(&ctx->key[0]);
while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) { while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
unsigned int block_len = nbytes & AES_BLOCK_MASK; ctx->ops->ctr_crypt(&ctx->key[0], walk.src.virt.addr,
walk.dst.virt.addr,
if (likely(block_len)) { round_down(nbytes, AES_BLOCK_SIZE),
ctx->ops->ctr_crypt(&ctx->key[0], walk.iv);
(const u64 *)walk.src.virt.addr, err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
(u64 *) walk.dst.virt.addr,
block_len, (u64 *) walk.iv);
}
nbytes &= AES_BLOCK_SIZE - 1;
err = blkcipher_walk_done(desc, &walk, nbytes);
} }
if (walk.nbytes) { if (walk.nbytes) {
ctr_crypt_final(ctx, &walk); ctr_crypt_final(ctx, &walk);
err = blkcipher_walk_done(desc, &walk, 0); err = skcipher_walk_done(&walk, 0);
} }
fprs_write(0); fprs_write(0);
return err; return err;
} }
static struct crypto_alg algs[] = { { static struct crypto_alg cipher_alg = {
.cra_name = "aes", .cra_name = "aes",
.cra_driver_name = "aes-sparc64", .cra_driver_name = "aes-sparc64",
.cra_priority = SPARC_CR_OPCODE_PRIORITY, .cra_priority = SPARC_CR_OPCODE_PRIORITY,
...@@ -400,66 +379,53 @@ static struct crypto_alg algs[] = { { ...@@ -400,66 +379,53 @@ static struct crypto_alg algs[] = { {
.cia_decrypt = crypto_aes_decrypt .cia_decrypt = crypto_aes_decrypt
} }
} }
}, { };
.cra_name = "ecb(aes)",
.cra_driver_name = "ecb-aes-sparc64", static struct skcipher_alg skcipher_algs[] = {
.cra_priority = SPARC_CR_OPCODE_PRIORITY, {
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .base.cra_name = "ecb(aes)",
.cra_blocksize = AES_BLOCK_SIZE, .base.cra_driver_name = "ecb-aes-sparc64",
.cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx), .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
.cra_alignmask = 7, .base.cra_blocksize = AES_BLOCK_SIZE,
.cra_type = &crypto_blkcipher_type, .base.cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
.cra_module = THIS_MODULE, .base.cra_alignmask = 7,
.cra_u = { .base.cra_module = THIS_MODULE,
.blkcipher = { .min_keysize = AES_MIN_KEY_SIZE,
.min_keysize = AES_MIN_KEY_SIZE, .max_keysize = AES_MAX_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE, .setkey = aes_set_key_skcipher,
.setkey = aes_set_key, .encrypt = ecb_encrypt,
.encrypt = ecb_encrypt, .decrypt = ecb_decrypt,
.decrypt = ecb_decrypt, }, {
}, .base.cra_name = "cbc(aes)",
}, .base.cra_driver_name = "cbc-aes-sparc64",
}, { .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
.cra_name = "cbc(aes)", .base.cra_blocksize = AES_BLOCK_SIZE,
.cra_driver_name = "cbc-aes-sparc64", .base.cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
.cra_priority = SPARC_CR_OPCODE_PRIORITY, .base.cra_alignmask = 7,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .base.cra_module = THIS_MODULE,
.cra_blocksize = AES_BLOCK_SIZE, .min_keysize = AES_MIN_KEY_SIZE,
.cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx), .max_keysize = AES_MAX_KEY_SIZE,
.cra_alignmask = 7, .ivsize = AES_BLOCK_SIZE,
.cra_type = &crypto_blkcipher_type, .setkey = aes_set_key_skcipher,
.cra_module = THIS_MODULE, .encrypt = cbc_encrypt,
.cra_u = { .decrypt = cbc_decrypt,
.blkcipher = { }, {
.min_keysize = AES_MIN_KEY_SIZE, .base.cra_name = "ctr(aes)",
.max_keysize = AES_MAX_KEY_SIZE, .base.cra_driver_name = "ctr-aes-sparc64",
.ivsize = AES_BLOCK_SIZE, .base.cra_priority = SPARC_CR_OPCODE_PRIORITY,
.setkey = aes_set_key, .base.cra_blocksize = 1,
.encrypt = cbc_encrypt, .base.cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
.decrypt = cbc_decrypt, .base.cra_alignmask = 7,
}, .base.cra_module = THIS_MODULE,
}, .min_keysize = AES_MIN_KEY_SIZE,
}, { .max_keysize = AES_MAX_KEY_SIZE,
.cra_name = "ctr(aes)", .ivsize = AES_BLOCK_SIZE,
.cra_driver_name = "ctr-aes-sparc64", .setkey = aes_set_key_skcipher,
.cra_priority = SPARC_CR_OPCODE_PRIORITY, .encrypt = ctr_crypt,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, .decrypt = ctr_crypt,
.cra_blocksize = 1, .chunksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx), }
.cra_alignmask = 7, };
.cra_type = &crypto_blkcipher_type,
.cra_module = THIS_MODULE,
.cra_u = {
.blkcipher = {
.min_keysize = AES_MIN_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE,
.ivsize = AES_BLOCK_SIZE,
.setkey = aes_set_key,
.encrypt = ctr_crypt,
.decrypt = ctr_crypt,
},
},
} };
static bool __init sparc64_has_aes_opcode(void) static bool __init sparc64_has_aes_opcode(void)
{ {
...@@ -477,17 +443,27 @@ static bool __init sparc64_has_aes_opcode(void) ...@@ -477,17 +443,27 @@ static bool __init sparc64_has_aes_opcode(void)
static int __init aes_sparc64_mod_init(void) static int __init aes_sparc64_mod_init(void)
{ {
if (sparc64_has_aes_opcode()) { int err;
pr_info("Using sparc64 aes opcodes optimized AES implementation\n");
return crypto_register_algs(algs, ARRAY_SIZE(algs)); if (!sparc64_has_aes_opcode()) {
pr_info("sparc64 aes opcodes not available.\n");
return -ENODEV;
} }
pr_info("sparc64 aes opcodes not available.\n"); pr_info("Using sparc64 aes opcodes optimized AES implementation\n");
return -ENODEV; err = crypto_register_alg(&cipher_alg);
if (err)
return err;
err = crypto_register_skciphers(skcipher_algs,
ARRAY_SIZE(skcipher_algs));
if (err)
crypto_unregister_alg(&cipher_alg);
return err;
} }
static void __exit aes_sparc64_mod_fini(void) static void __exit aes_sparc64_mod_fini(void)
{ {
crypto_unregister_algs(algs, ARRAY_SIZE(algs)); crypto_unregister_alg(&cipher_alg);
crypto_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
} }
module_init(aes_sparc64_mod_init); module_init(aes_sparc64_mod_init);
......
...@@ -1098,8 +1098,7 @@ config CRYPTO_AES_NI_INTEL ...@@ -1098,8 +1098,7 @@ config CRYPTO_AES_NI_INTEL
config CRYPTO_AES_SPARC64 config CRYPTO_AES_SPARC64
tristate "AES cipher algorithms (SPARC64)" tristate "AES cipher algorithms (SPARC64)"
depends on SPARC64 depends on SPARC64
select CRYPTO_CRYPTD select CRYPTO_BLKCIPHER
select CRYPTO_ALGAPI
help help
Use SPARC64 crypto opcodes for AES algorithm. Use SPARC64 crypto opcodes for AES algorithm.
......
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