Skip to contentSkip to conversation

crypto/fsl: fsl_hash: Fix dcache issue in caam_hash_finish

PATCH In review Gaurav Jain 4 participants 4 messages May 11, 2022 – May 20, 2022 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto/fsl: fsl_hash: Fix dcache issue in caam_hash_finish

HW accelerated hash operations are giving incorrect hash output. so add flush and invalidate for input/output hash buffers.

Fixes 94e3c8c4fd (crypto/fsl - Add progressive hashing support using hardware acceleration.)
---
 drivers/crypto/fsl/fsl_hash.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/crypto/fsl/fsl_hash.c +8 -0
index a52c4ac957..9e6829b7ad 100644
@@ -149,12 +149,20 @@ static int caam_hash_finish(void *hash_ctx, void *dest_buf,
 				  driver_hash[caam_algo].digestsize,
 				  1);
 
+	flush_dcache_range((ulong)ctx->sg_tbl, (ulong)(ctx->sg_tbl) + len);
+	flush_dcache_range((ulong)ctx->sha_desc,
+			   (ulong)(ctx->sha_desc) + (sizeof(uint32_t) * MAX_CAAM_DESCSIZE));
+	flush_dcache_range((ulong)ctx->hash,
+			   (ulong)(ctx->hash) + driver_hash[caam_algo].digestsize);
+
 	ret = run_descriptor_jr(ctx->sha_desc);
 
 	if (ret) {
 		debug("Error %x\n", ret);
 		return ret;
 	} else {
+		invalidate_dcache_range((ulong)ctx->hash,
+					(ulong)(ctx->hash) + driver_hash[caam_algo].digestsize);
 		memcpy(dest_buf, ctx->hash, sizeof(ctx->hash));
 	}
 	free(ctx);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Gaurav Jain

HW accelerated hash operations are giving incorrect hash output. so add flush and invalidate for input/output hash buffers.

Fixes 94e3c8c4fd (crypto/fsl - Add progressive hashing support using hardware acceleration.)

AFAICT, it takes somewhat more to fix that commit; the progressive hashing is entirely broken.

It doesn't actually do anything progressive, it just stashes the address/length pairs it is given, but doesn't feed the contents of those buffers to the hardware, folding it into the hash state. So the caller must not touch the buffers it passes until the finalization. I.e. I think this won't work:

  char buf[SOMETHING];

  update_buffer(buf);
  hash_update(buf, len);
  update_buffer_again(buf);
  hash_update(buf, len);

And this pattern can be found in e.g. drivers/dfu/dfu.c which seems to repeatedly pass the same address (dfu->i_buf_start) to ->hash_update.

Am I reading the code wrong?

Rasmus

#2 lore raw
Quoted 4 lines · Rasmus Villemoes
Quoted 3 lines · Gaurav Jain

HW accelerated hash operations are giving incorrect hash output. so add flush and invalidate for input/output hash buffers.

Quoted 8 lines
Quoted 1 line
Fixes 94e3c8c4fd (crypto/fsl - Add progressive hashing support using hardware acceleration.)

AFAICT, it takes somewhat more to fix that commit; the progressive hashing is entirely broken.

It doesn't actually do anything progressive, it just stashes the address/length pairs it is given, but doesn't feed the contents of those buffers to the hardware, folding it into the hash state. So the caller

Correct.

Quoted 10 lines

must not touch the buffers it passes until the finalization. I.e. I think this won't work:

  char buf[SOMETHING];

  update_buffer(buf);
  hash_update(buf, len);
  update_buffer_again(buf);
  hash_update(buf, len);

Indeed, this won't work.

Quoted 3 lines

And this pattern can be found in e.g. drivers/dfu/dfu.c which seems to repeatedly pass the same address (dfu->i_buf_start) to ->hash_update.

At first glance, dfu asks for crc32 algorithm, so it won't use this backend. But the concept is correct: user is not forced to keep a buffer unmodified (or even allocated) after .hash_update returns.

Thanks,
Horia

#3 lore raw

PATCH 1/1 crypto/fsl: fsl_hash: Fix dcache issue in caam_hash_finish

In reply to Gaurav Jain
Quoted 4 lines

HW accelerated hash operations are giving incorrect hash output. so add flush and invalidate for input/output hash buffers.

Fixes 94e3c8c4fd (crypto/fsl - Add progressive hashing support using hardware acceleration.)

Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================