The fallback tfm is being called on tegra_cmac_do_final() with the assumption that the message with nbytes == 0 is empty, however tegra_cmac_do_update() always keeps the last block of the message, full or partial, in the residue buffer for tegra_cmac_do_final(), while req->nbytes is always set to 0 by crypto_ahash_final(), meaning we have a request with nbytes == 0 but residue.size > 0. Checked on a NVIDIA Orin IGX system during its self-test: alg: ahash: tegra-se-cmac test failed (wrong result) on test vector 1, cfg="init+update+final aligned buffer" alg: self-tests for cmac(aes) using tegra-se-cmac failed (rc=-22) And no self-tests failures after the patch applied. From /proc/crypto: driver : tegra-se-cmac module : tegra_se ... selftest : passed This commit changes the check to use the residue size instead of req->nbytes and also free the residue buffer on the fallback path, which was being skipped, leaking it on every CMAC of an empty message. Fixes: 0880bb3b00c8 ("crypto: tegra - Add Tegra Security Engine driver") Cc: stable@vger.kernel.org Suggested-by: Thomas Huth Signed-off-by: Bruno Meneguele --- drivers/crypto/tegra/tegra-se-aes.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/crypto/tegra/tegra-se-aes.c b/drivers/crypto/tegra/tegra-se-aes.c index 0fd1d7035899..122c3345b7e7 100644 --- a/drivers/crypto/tegra/tegra-se-aes.c +++ b/drivers/crypto/tegra/tegra-se-aes.c @@ -1647,9 +1647,11 @@ static int tegra_cmac_do_final(struct ahash_request *req) u32 *result = (u32 *)req->result; int ret = 0, i, cmdlen; - if (!req->nbytes && !rctx->total_len && ctx->fallback_tfm) { - return crypto_shash_tfm_digest(ctx->fallback_tfm, - NULL, 0, req->result); + /* After update, leftover data is kept in the residue buffer. */ + if (!rctx->residue.size && !rctx->total_len && ctx->fallback_tfm) { + ret = crypto_shash_tfm_digest(ctx->fallback_tfm, NULL, 0, + req->result); + goto out_free; } if (rctx->residue.size) { -- 2.52.0