Commit 7c3d703f authored by Herbert Xu's avatar Herbert Xu

[CRYPTO] authenc: Merge common hashing code

This patch merges the common hashing code between encryption and decryption.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 12dc5e62
...@@ -87,17 +87,18 @@ badkey: ...@@ -87,17 +87,18 @@ badkey:
goto out; goto out;
} }
static int crypto_authenc_hash(struct aead_request *req) static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
struct scatterlist *cipher,
unsigned int cryptlen)
{ {
struct crypto_aead *authenc = crypto_aead_reqtfm(req); struct crypto_aead *authenc = crypto_aead_reqtfm(req);
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
struct crypto_hash *auth = ctx->auth; struct crypto_hash *auth = ctx->auth;
struct hash_desc desc = { struct hash_desc desc = {
.tfm = auth, .tfm = auth,
.flags = aead_request_flags(req) & flags,
}; };
u8 *hash = aead_request_ctx(req); u8 *hash = aead_request_ctx(req);
struct scatterlist *dst = req->dst;
unsigned int cryptlen = req->cryptlen;
int err; int err;
hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth), hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
...@@ -112,7 +113,7 @@ static int crypto_authenc_hash(struct aead_request *req) ...@@ -112,7 +113,7 @@ static int crypto_authenc_hash(struct aead_request *req)
if (err) if (err)
goto auth_unlock; goto auth_unlock;
err = crypto_hash_update(&desc, dst, cryptlen); err = crypto_hash_update(&desc, cipher, cryptlen);
if (err) if (err)
goto auth_unlock; goto auth_unlock;
...@@ -121,7 +122,21 @@ auth_unlock: ...@@ -121,7 +122,21 @@ auth_unlock:
spin_unlock_bh(&ctx->auth_lock); spin_unlock_bh(&ctx->auth_lock);
if (err) if (err)
return err; return ERR_PTR(err);
return hash;
}
static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
{
struct crypto_aead *authenc = crypto_aead_reqtfm(req);
struct scatterlist *dst = req->dst;
unsigned int cryptlen = req->cryptlen;
u8 *hash;
hash = crypto_authenc_hash(req, flags, dst, cryptlen);
if (IS_ERR(hash))
return PTR_ERR(hash);
scatterwalk_map_and_copy(hash, dst, cryptlen, scatterwalk_map_and_copy(hash, dst, cryptlen,
crypto_aead_authsize(authenc), 1); crypto_aead_authsize(authenc), 1);
...@@ -132,7 +147,7 @@ static void crypto_authenc_encrypt_done(struct crypto_async_request *req, ...@@ -132,7 +147,7 @@ static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
int err) int err)
{ {
if (!err) if (!err)
err = crypto_authenc_hash(req->data); err = crypto_authenc_genicv(req->data, 0);
aead_request_complete(req->data, err); aead_request_complete(req->data, err);
} }
...@@ -154,50 +169,25 @@ static int crypto_authenc_encrypt(struct aead_request *req) ...@@ -154,50 +169,25 @@ static int crypto_authenc_encrypt(struct aead_request *req)
if (err) if (err)
return err; return err;
return crypto_authenc_hash(req); return crypto_authenc_genicv(req, CRYPTO_TFM_REQ_MAY_SLEEP);
} }
static int crypto_authenc_verify(struct aead_request *req, static int crypto_authenc_verify(struct aead_request *req,
unsigned int cryptlen) unsigned int cryptlen)
{ {
struct crypto_aead *authenc = crypto_aead_reqtfm(req); struct crypto_aead *authenc = crypto_aead_reqtfm(req);
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); u8 *ohash;
struct crypto_hash *auth = ctx->auth;
struct hash_desc desc = {
.tfm = auth,
.flags = aead_request_flags(req),
};
u8 *ohash = aead_request_ctx(req);
u8 *ihash; u8 *ihash;
struct scatterlist *src = req->src; struct scatterlist *src = req->src;
unsigned int authsize; unsigned int authsize;
int err;
ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth), ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, src,
crypto_hash_alignmask(auth) + 1); cryptlen);
ihash = ohash + crypto_hash_digestsize(auth); if (IS_ERR(ohash))
return PTR_ERR(ohash);
spin_lock_bh(&ctx->auth_lock);
err = crypto_hash_init(&desc);
if (err)
goto auth_unlock;
err = crypto_hash_update(&desc, req->assoc, req->assoclen);
if (err)
goto auth_unlock;
err = crypto_hash_update(&desc, src, cryptlen);
if (err)
goto auth_unlock;
err = crypto_hash_final(&desc, ohash);
auth_unlock:
spin_unlock_bh(&ctx->auth_lock);
if (err)
return err;
authsize = crypto_aead_authsize(authenc); authsize = crypto_aead_authsize(authenc);
ihash = ohash + authsize;
scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0); scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0; return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
} }
......
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