Commit 6b611d98 authored by Eric Biggers's avatar Eric Biggers Committed by Herbert Xu

crypto: keywrap - convert to skcipher API

Convert the keywrap template from the deprecated "blkcipher" API to the
"skcipher" API, taking advantage of skcipher_alloc_instance_simple() to
simplify it considerably.

Cc: Stephan Mueller <smueller@chronox.de>
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Reviewed-by: default avatarStephan Mueller <smueller@chronox.de>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 52e9368f
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
* u8 *iv = data; * u8 *iv = data;
* u8 *pt = data + crypto_skcipher_ivsize(tfm); * u8 *pt = data + crypto_skcipher_ivsize(tfm);
* <ensure that pt contains the plaintext of size ptlen> * <ensure that pt contains the plaintext of size ptlen>
* sg_init_one(&sg, ptdata, ptlen); * sg_init_one(&sg, pt, ptlen);
* skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv); * skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv);
* *
* ==> After encryption, data now contains full KW result as per SP800-38F. * ==> After encryption, data now contains full KW result as per SP800-38F.
...@@ -70,8 +70,8 @@ ...@@ -70,8 +70,8 @@
* u8 *iv = data; * u8 *iv = data;
* u8 *ct = data + crypto_skcipher_ivsize(tfm); * u8 *ct = data + crypto_skcipher_ivsize(tfm);
* unsigned int ctlen = datalen - crypto_skcipher_ivsize(tfm); * unsigned int ctlen = datalen - crypto_skcipher_ivsize(tfm);
* sg_init_one(&sg, ctdata, ctlen); * sg_init_one(&sg, ct, ctlen);
* skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv); * skcipher_request_set_crypt(req, &sg, &sg, ctlen, iv);
* *
* ==> After decryption (which hopefully does not return EBADMSG), the ct * ==> After decryption (which hopefully does not return EBADMSG), the ct
* pointer now points to the plaintext of size ctlen. * pointer now points to the plaintext of size ctlen.
...@@ -87,10 +87,6 @@ ...@@ -87,10 +87,6 @@
#include <crypto/scatterwalk.h> #include <crypto/scatterwalk.h>
#include <crypto/internal/skcipher.h> #include <crypto/internal/skcipher.h>
struct crypto_kw_ctx {
struct crypto_cipher *child;
};
struct crypto_kw_block { struct crypto_kw_block {
#define SEMIBSIZE 8 #define SEMIBSIZE 8
__be64 A; __be64 A;
...@@ -124,16 +120,13 @@ static void crypto_kw_scatterlist_ff(struct scatter_walk *walk, ...@@ -124,16 +120,13 @@ static void crypto_kw_scatterlist_ff(struct scatter_walk *walk,
} }
} }
static int crypto_kw_decrypt(struct blkcipher_desc *desc, static int crypto_kw_decrypt(struct skcipher_request *req)
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_blkcipher *tfm = desc->tfm; struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct crypto_kw_ctx *ctx = crypto_blkcipher_ctx(tfm); struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
struct crypto_cipher *child = ctx->child;
struct crypto_kw_block block; struct crypto_kw_block block;
struct scatterlist *lsrc, *ldst; struct scatterlist *src, *dst;
u64 t = 6 * ((nbytes) >> 3); u64 t = 6 * ((req->cryptlen) >> 3);
unsigned int i; unsigned int i;
int ret = 0; int ret = 0;
...@@ -141,27 +134,27 @@ static int crypto_kw_decrypt(struct blkcipher_desc *desc, ...@@ -141,27 +134,27 @@ static int crypto_kw_decrypt(struct blkcipher_desc *desc,
* Require at least 2 semiblocks (note, the 3rd semiblock that is * Require at least 2 semiblocks (note, the 3rd semiblock that is
* required by SP800-38F is the IV. * required by SP800-38F is the IV.
*/ */
if (nbytes < (2 * SEMIBSIZE) || nbytes % SEMIBSIZE) if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE)
return -EINVAL; return -EINVAL;
/* Place the IV into block A */ /* Place the IV into block A */
memcpy(&block.A, desc->info, SEMIBSIZE); memcpy(&block.A, req->iv, SEMIBSIZE);
/* /*
* src scatterlist is read-only. dst scatterlist is r/w. During the * src scatterlist is read-only. dst scatterlist is r/w. During the
* first loop, lsrc points to src and ldst to dst. For any * first loop, src points to req->src and dst to req->dst. For any
* subsequent round, the code operates on dst only. * subsequent round, the code operates on req->dst only.
*/ */
lsrc = src; src = req->src;
ldst = dst; dst = req->dst;
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
struct scatter_walk src_walk, dst_walk; struct scatter_walk src_walk, dst_walk;
unsigned int tmp_nbytes = nbytes; unsigned int nbytes = req->cryptlen;
while (tmp_nbytes) { while (nbytes) {
/* move pointer by tmp_nbytes in the SGL */ /* move pointer by nbytes in the SGL */
crypto_kw_scatterlist_ff(&src_walk, lsrc, tmp_nbytes); crypto_kw_scatterlist_ff(&src_walk, src, nbytes);
/* get the source block */ /* get the source block */
scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE, scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
false); false);
...@@ -170,21 +163,21 @@ static int crypto_kw_decrypt(struct blkcipher_desc *desc, ...@@ -170,21 +163,21 @@ static int crypto_kw_decrypt(struct blkcipher_desc *desc,
block.A ^= cpu_to_be64(t); block.A ^= cpu_to_be64(t);
t--; t--;
/* perform KW operation: decrypt block */ /* perform KW operation: decrypt block */
crypto_cipher_decrypt_one(child, (u8*)&block, crypto_cipher_decrypt_one(cipher, (u8 *)&block,
(u8*)&block); (u8 *)&block);
/* move pointer by tmp_nbytes in the SGL */ /* move pointer by nbytes in the SGL */
crypto_kw_scatterlist_ff(&dst_walk, ldst, tmp_nbytes); crypto_kw_scatterlist_ff(&dst_walk, dst, nbytes);
/* Copy block->R into place */ /* Copy block->R into place */
scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE, scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
true); true);
tmp_nbytes -= SEMIBSIZE; nbytes -= SEMIBSIZE;
} }
/* we now start to operate on the dst SGL only */ /* we now start to operate on the dst SGL only */
lsrc = dst; src = req->dst;
ldst = dst; dst = req->dst;
} }
/* Perform authentication check */ /* Perform authentication check */
...@@ -196,15 +189,12 @@ static int crypto_kw_decrypt(struct blkcipher_desc *desc, ...@@ -196,15 +189,12 @@ static int crypto_kw_decrypt(struct blkcipher_desc *desc,
return ret; return ret;
} }
static int crypto_kw_encrypt(struct blkcipher_desc *desc, static int crypto_kw_encrypt(struct skcipher_request *req)
struct scatterlist *dst, struct scatterlist *src,
unsigned int nbytes)
{ {
struct crypto_blkcipher *tfm = desc->tfm; struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct crypto_kw_ctx *ctx = crypto_blkcipher_ctx(tfm); struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
struct crypto_cipher *child = ctx->child;
struct crypto_kw_block block; struct crypto_kw_block block;
struct scatterlist *lsrc, *ldst; struct scatterlist *src, *dst;
u64 t = 1; u64 t = 1;
unsigned int i; unsigned int i;
...@@ -214,7 +204,7 @@ static int crypto_kw_encrypt(struct blkcipher_desc *desc, ...@@ -214,7 +204,7 @@ static int crypto_kw_encrypt(struct blkcipher_desc *desc,
* This means that the dst memory must be one semiblock larger than src. * This means that the dst memory must be one semiblock larger than src.
* Also ensure that the given data is aligned to semiblock. * Also ensure that the given data is aligned to semiblock.
*/ */
if (nbytes < (2 * SEMIBSIZE) || nbytes % SEMIBSIZE) if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE)
return -EINVAL; return -EINVAL;
/* /*
...@@ -225,26 +215,26 @@ static int crypto_kw_encrypt(struct blkcipher_desc *desc, ...@@ -225,26 +215,26 @@ static int crypto_kw_encrypt(struct blkcipher_desc *desc,
/* /*
* src scatterlist is read-only. dst scatterlist is r/w. During the * src scatterlist is read-only. dst scatterlist is r/w. During the
* first loop, lsrc points to src and ldst to dst. For any * first loop, src points to req->src and dst to req->dst. For any
* subsequent round, the code operates on dst only. * subsequent round, the code operates on req->dst only.
*/ */
lsrc = src; src = req->src;
ldst = dst; dst = req->dst;
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
struct scatter_walk src_walk, dst_walk; struct scatter_walk src_walk, dst_walk;
unsigned int tmp_nbytes = nbytes; unsigned int nbytes = req->cryptlen;
scatterwalk_start(&src_walk, lsrc); scatterwalk_start(&src_walk, src);
scatterwalk_start(&dst_walk, ldst); scatterwalk_start(&dst_walk, dst);
while (tmp_nbytes) { while (nbytes) {
/* get the source block */ /* get the source block */
scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE, scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
false); false);
/* perform KW operation: encrypt block */ /* perform KW operation: encrypt block */
crypto_cipher_encrypt_one(child, (u8 *)&block, crypto_cipher_encrypt_one(cipher, (u8 *)&block,
(u8 *)&block); (u8 *)&block);
/* perform KW operation: modify IV with counter */ /* perform KW operation: modify IV with counter */
block.A ^= cpu_to_be64(t); block.A ^= cpu_to_be64(t);
...@@ -254,117 +244,59 @@ static int crypto_kw_encrypt(struct blkcipher_desc *desc, ...@@ -254,117 +244,59 @@ static int crypto_kw_encrypt(struct blkcipher_desc *desc,
scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE, scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
true); true);
tmp_nbytes -= SEMIBSIZE; nbytes -= SEMIBSIZE;
} }
/* we now start to operate on the dst SGL only */ /* we now start to operate on the dst SGL only */
lsrc = dst; src = req->dst;
ldst = dst; dst = req->dst;
} }
/* establish the IV for the caller to pick up */ /* establish the IV for the caller to pick up */
memcpy(desc->info, &block.A, SEMIBSIZE); memcpy(req->iv, &block.A, SEMIBSIZE);
memzero_explicit(&block, sizeof(struct crypto_kw_block)); memzero_explicit(&block, sizeof(struct crypto_kw_block));
return 0; return 0;
} }
static int crypto_kw_setkey(struct crypto_tfm *parent, const u8 *key, static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb)
unsigned int keylen)
{ {
struct crypto_kw_ctx *ctx = crypto_tfm_ctx(parent); struct skcipher_instance *inst;
struct crypto_cipher *child = ctx->child; struct crypto_alg *alg;
int err; int err;
crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & if (IS_ERR(inst))
CRYPTO_TFM_REQ_MASK); return PTR_ERR(inst);
err = crypto_cipher_setkey(child, key, keylen);
crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
CRYPTO_TFM_RES_MASK);
return err;
}
static int crypto_kw_init_tfm(struct crypto_tfm *tfm)
{
struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
struct crypto_spawn *spawn = crypto_instance_ctx(inst);
struct crypto_kw_ctx *ctx = crypto_tfm_ctx(tfm);
struct crypto_cipher *cipher;
cipher = crypto_spawn_cipher(spawn);
if (IS_ERR(cipher))
return PTR_ERR(cipher);
ctx->child = cipher;
return 0;
}
static void crypto_kw_exit_tfm(struct crypto_tfm *tfm)
{
struct crypto_kw_ctx *ctx = crypto_tfm_ctx(tfm);
crypto_free_cipher(ctx->child);
}
static struct crypto_instance *crypto_kw_alloc(struct rtattr **tb)
{
struct crypto_instance *inst = NULL;
struct crypto_alg *alg = NULL;
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg))
return ERR_CAST(alg);
inst = ERR_PTR(-EINVAL); err = -EINVAL;
/* Section 5.1 requirement for KW */ /* Section 5.1 requirement for KW */
if (alg->cra_blocksize != sizeof(struct crypto_kw_block)) if (alg->cra_blocksize != sizeof(struct crypto_kw_block))
goto err; goto out_free_inst;
inst = crypto_alloc_instance("kw", alg); inst->alg.base.cra_blocksize = SEMIBSIZE;
if (IS_ERR(inst)) inst->alg.base.cra_alignmask = 0;
goto err; inst->alg.ivsize = SEMIBSIZE;
inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; inst->alg.encrypt = crypto_kw_encrypt;
inst->alg.cra_priority = alg->cra_priority; inst->alg.decrypt = crypto_kw_decrypt;
inst->alg.cra_blocksize = SEMIBSIZE;
inst->alg.cra_alignmask = 0;
inst->alg.cra_type = &crypto_blkcipher_type;
inst->alg.cra_blkcipher.ivsize = SEMIBSIZE;
inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
inst->alg.cra_ctxsize = sizeof(struct crypto_kw_ctx); err = skcipher_register_instance(tmpl, inst);
if (err)
inst->alg.cra_init = crypto_kw_init_tfm; goto out_free_inst;
inst->alg.cra_exit = crypto_kw_exit_tfm; goto out_put_alg;
inst->alg.cra_blkcipher.setkey = crypto_kw_setkey;
inst->alg.cra_blkcipher.encrypt = crypto_kw_encrypt;
inst->alg.cra_blkcipher.decrypt = crypto_kw_decrypt;
err: out_free_inst:
inst->free(inst);
out_put_alg:
crypto_mod_put(alg); crypto_mod_put(alg);
return inst; return err;
}
static void crypto_kw_free(struct crypto_instance *inst)
{
crypto_drop_spawn(crypto_instance_ctx(inst));
kfree(inst);
} }
static struct crypto_template crypto_kw_tmpl = { static struct crypto_template crypto_kw_tmpl = {
.name = "kw", .name = "kw",
.alloc = crypto_kw_alloc, .create = crypto_kw_create,
.free = crypto_kw_free,
.module = THIS_MODULE, .module = THIS_MODULE,
}; };
......
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