Skip to contentSkip to conversation

crypto: atmel-aes - Fix saving of IV for CTR mode

bug report In review Dan Carpenter 3 participants 3 messages Dec 13, 2019 – Dec 20, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello Tudor Ambarus,

The patch 371731ec2179: "crypto: atmel-aes - Fix saving of IV for CTR mode" from Dec 5, 2019, leads to the following static checker warning:

drivers/crypto/atmel-aes.c:1058 atmel_aes_ctr_transfer()
warn: right shifting more than type allows 16 vs 16

drivers/crypto/atmel-aes.c

  1044          /* Check for transfer completion. */
  1045          ctx->offset += dd->total;
  1046          if (ctx->offset >= req->cryptlen)
  1047                  return atmel_aes_transfer_complete(dd);
  1048  
  1049          /* Compute data length. */
  1050          datalen = req->cryptlen - ctx->offset;
  1051          ctx->blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
  1052          ctr = be32_to_cpu(ctx->iv[3]);
  1053  
  1054          /* Check 16bit counter overflow. */
  1055          start = ctr & 0xffff;
  1056          end = start + ctx->blocks - 1;
  1057  
  1058          if (ctx->blocks >> 16 || end < start) {
                    ^^^^^^^^^^^^^^^^^

Impossible condition.

  1059                  ctr |= 0xffff;
  1060                  datalen = AES_BLOCK_SIZE * (0x10000 - start);
  1061                  fragmented = true;

1062 }

regards,
dan carpenter

#1 lore raw

PATCH 1/1 crypto: atmel-aes - Fix CTR counter overflow when multiple fragments

From: Tudor Ambarus <[email protected]>

The CTR transfer works in fragments of data of maximum 1 MByte because of the 16 bit CTR counter embedded in the IP. Fix the CTR counter overflow handling for messages larger than 1 MByte.

Fixes 781a08d9740a ("crypto: atmel-aes - Fix counter overflow in CTR mode")

---
Thanks, Dan.

 drivers/crypto/atmel-aes.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/crypto/atmel-aes.c +7 -1
index 245d45f93b61..b001fdcd9d95 100644
@@ -120,7 +120,7 @@ struct atmel_aes_ctr_ctx {
 	size_t			offset;
 	struct scatterlist	src[2];
 	struct scatterlist	dst[2];
-	u16			blocks;
+	u32			blocks;
 };
 
 struct atmel_aes_gcm_ctx {
@@ -527,6 +527,12 @@ static void atmel_aes_ctr_update_req_iv(struct atmel_aes_dev *dd)
 	unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
 	int i;
 
+	/*
+	 * The CTR transfer works in fragments of data of maximum 1 MByte
+	 * because of the 16 bit CTR counter embedded in the IP. When reaching
+	 * here, ctx->blocks contains the number of blocks of the last fragment
+	 * processed, there is no need to explicit cast it to u16.
+	 */
 	for (i = 0; i < ctx->blocks; i++)
 		crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
 
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH] crypto: atmel-aes - Fix CTR counter overflow when multiple fragments

Quoted 6 lines · On Fri, Dec 13, 2019 at 02:45:44PM +0000, [email protected] wrote:

From: Tudor Ambarus <[email protected]>

The CTR transfer works in fragments of data of maximum 1 MByte because of the 16 bit CTR counter embedded in the IP. Fix the CTR counter overflow handling for messages larger than 1 MByte.

Quoted 8 lines
Fixes 781a08d9740a ("crypto: atmel-aes - Fix counter overflow in CTR mode")

---
Thanks, Dan.

 drivers/crypto/atmel-aes.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt