Skip to content

Commit

Permalink
[CRYPTO] authenc: Merge common hashing code
Browse files Browse the repository at this point in the history
This patch merges the common hashing code between encryption and decryption.

Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
herbertx committed Jan 10, 2008
1 parent 12dc5e6 commit 7c3d703
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions crypto/authenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,18 @@ static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
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_authenc_ctx *ctx = crypto_aead_ctx(authenc);
struct crypto_hash *auth = ctx->auth;
struct hash_desc desc = {
.tfm = auth,
.flags = aead_request_flags(req) & flags,
};
u8 *hash = aead_request_ctx(req);
struct scatterlist *dst = req->dst;
unsigned int cryptlen = req->cryptlen;
int err;

hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
Expand All @@ -112,7 +113,7 @@ static int crypto_authenc_hash(struct aead_request *req)
if (err)
goto auth_unlock;

err = crypto_hash_update(&desc, dst, cryptlen);
err = crypto_hash_update(&desc, cipher, cryptlen);
if (err)
goto auth_unlock;

Expand All @@ -121,7 +122,21 @@ static int crypto_authenc_hash(struct aead_request *req)
spin_unlock_bh(&ctx->auth_lock);

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,
crypto_aead_authsize(authenc), 1);
Expand All @@ -132,7 +147,7 @@ static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
int err)
{
if (!err)
err = crypto_authenc_hash(req->data);
err = crypto_authenc_genicv(req->data, 0);

aead_request_complete(req->data, err);
}
Expand All @@ -154,50 +169,25 @@ static int crypto_authenc_encrypt(struct aead_request *req)
if (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,
unsigned int cryptlen)
{
struct crypto_aead *authenc = crypto_aead_reqtfm(req);
struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
struct crypto_hash *auth = ctx->auth;
struct hash_desc desc = {
.tfm = auth,
.flags = aead_request_flags(req),
};
u8 *ohash = aead_request_ctx(req);
u8 *ohash;
u8 *ihash;
struct scatterlist *src = req->src;
unsigned int authsize;
int err;

ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth),
crypto_hash_alignmask(auth) + 1);
ihash = ohash + crypto_hash_digestsize(auth);

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;
ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, src,
cryptlen);
if (IS_ERR(ohash))
return PTR_ERR(ohash);

authsize = crypto_aead_authsize(authenc);
ihash = ohash + authsize;
scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
}
Expand Down

0 comments on commit 7c3d703

Please sign in to comment.