Commit 5499b1a7 authored by Herbert Xu's avatar Herbert Xu

crypto: echainiv - Fix encryption convention

This patch fixes a bug where we were incorrectly including the
IV in the AD during encryption.  The IV must remain in the plain
text for it to be encrypted.

During decryption there is no need to copy the IV to dst because
it's now part of the AD.

This patch removes an unncessary check on authsize which would be
performed by the underlying decrypt call.

Finally this patch makes use of the type-safe init/exit functions.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent f6da3205
...@@ -145,8 +145,8 @@ static int echainiv_encrypt(struct aead_request *req) ...@@ -145,8 +145,8 @@ static int echainiv_encrypt(struct aead_request *req)
aead_request_set_callback(subreq, req->base.flags, compl, data); aead_request_set_callback(subreq, req->base.flags, compl, data);
aead_request_set_crypt(subreq, req->dst, req->dst, aead_request_set_crypt(subreq, req->dst, req->dst,
req->cryptlen - ivsize, info); req->cryptlen, info);
aead_request_set_ad(subreq, req->assoclen + ivsize); aead_request_set_ad(subreq, req->assoclen);
crypto_xor(info, ctx->salt, ivsize); crypto_xor(info, ctx->salt, ivsize);
scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
...@@ -166,7 +166,7 @@ static int echainiv_decrypt(struct aead_request *req) ...@@ -166,7 +166,7 @@ static int echainiv_decrypt(struct aead_request *req)
void *data; void *data;
unsigned int ivsize = crypto_aead_ivsize(geniv); unsigned int ivsize = crypto_aead_ivsize(geniv);
if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) if (req->cryptlen < ivsize)
return -EINVAL; return -EINVAL;
aead_request_set_tfm(subreq, ctx->geniv.child); aead_request_set_tfm(subreq, ctx->geniv.child);
...@@ -180,16 +180,12 @@ static int echainiv_decrypt(struct aead_request *req) ...@@ -180,16 +180,12 @@ static int echainiv_decrypt(struct aead_request *req)
aead_request_set_ad(subreq, req->assoclen + ivsize); aead_request_set_ad(subreq, req->assoclen + ivsize);
scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
if (req->src != req->dst)
scatterwalk_map_and_copy(req->iv, req->dst,
req->assoclen, ivsize, 1);
return crypto_aead_decrypt(subreq); return crypto_aead_decrypt(subreq);
} }
static int echainiv_init(struct crypto_tfm *tfm) static int echainiv_init(struct crypto_aead *geniv)
{ {
struct crypto_aead *geniv = __crypto_aead_cast(tfm);
struct echainiv_ctx *ctx = crypto_aead_ctx(geniv); struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
int err; int err;
...@@ -212,7 +208,7 @@ static int echainiv_init(struct crypto_tfm *tfm) ...@@ -212,7 +208,7 @@ static int echainiv_init(struct crypto_tfm *tfm)
if (IS_ERR(ctx->null)) if (IS_ERR(ctx->null))
goto out; goto out;
err = aead_geniv_init(tfm); err = aead_geniv_init(crypto_aead_tfm(geniv));
if (err) if (err)
goto drop_null; goto drop_null;
...@@ -227,9 +223,9 @@ static int echainiv_init(struct crypto_tfm *tfm) ...@@ -227,9 +223,9 @@ static int echainiv_init(struct crypto_tfm *tfm)
goto out; goto out;
} }
static void echainiv_exit(struct crypto_tfm *tfm) static void echainiv_exit(struct crypto_aead *tfm)
{ {
struct echainiv_ctx *ctx = crypto_tfm_ctx(tfm); struct echainiv_ctx *ctx = crypto_aead_ctx(tfm);
crypto_free_aead(ctx->geniv.child); crypto_free_aead(ctx->geniv.child);
crypto_put_default_null_skcipher(); crypto_put_default_null_skcipher();
...@@ -262,13 +258,15 @@ static int echainiv_aead_create(struct crypto_template *tmpl, ...@@ -262,13 +258,15 @@ static int echainiv_aead_create(struct crypto_template *tmpl,
inst->alg.encrypt = echainiv_encrypt; inst->alg.encrypt = echainiv_encrypt;
inst->alg.decrypt = echainiv_decrypt; inst->alg.decrypt = echainiv_decrypt;
inst->alg.base.cra_init = echainiv_init; inst->alg.init = echainiv_init;
inst->alg.base.cra_exit = echainiv_exit; inst->alg.exit = echainiv_exit;
inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
inst->alg.base.cra_ctxsize = sizeof(struct echainiv_ctx); inst->alg.base.cra_ctxsize = sizeof(struct echainiv_ctx);
inst->alg.base.cra_ctxsize += inst->alg.ivsize; inst->alg.base.cra_ctxsize += inst->alg.ivsize;
inst->free = aead_geniv_free;
done: done:
err = aead_register_instance(tmpl, inst); err = aead_register_instance(tmpl, inst);
if (err) if (err)
......
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