Skip to contentSkip to conversation

crypto: atmel-sha204a - multiple RNG fixes

PATCH Superseded Lothar Rubusch 3 participants 15 messages Apr 22 – Apr 28

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover crypto: atmel-sha204a - multiple RNG fixes

When testing the RNG functionality on the Atmel SHA204a hardware, I found the following issues: rngtest reported failures and hexdump reveiled only the first 8 bytes out of 32 provided actually entropy.

Having a closer look into it, I found a (small) memory leak, missing to free work_data, miss-reading of the count field into the entropy fields and parts of the 32 random bytes staying 0 due to reading the slow i2c device.

The series proposes fixes and how fixed functionality can be/was verified. Executing rngtest afterward showed a decent result, due to the i2c bus a bit slow.

All setups require selecting the Atmel-sha204a as active RNG.

$ cat /sys/class/misc/hw_random/rng_available
    3f104000.rng 1-0064 none
$ echo 1-0064 > /sys/class/misc/hw_random/rng_current
$ cat /sys/class/misc/hw_random/rng_current
    1-0064

Testing RNG properties currently shows problematic results:

$ rngtest < /dev/hwrng
    rngtest 2.6
    Copyright (c) 2004 by Henrique de Moraes Holschuh
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    rngtest: starting FIPS tests...
    rngtest: bits received from input: 1040032
    rngtest: FIPS 140-2 successes: 0
    rngtest: FIPS 140-2 failures: 52
    rngtest: FIPS 140-2(2001-10-10) Monobit: 52
    rngtest: FIPS 140-2(2001-10-10) Poker: 52
    rngtest: FIPS 140-2(2001-10-10) Runs: 52
    rngtest: FIPS 140-2(2001-10-10) Long run: 52
    rngtest: FIPS 140-2(2001-10-10) Continuous run: 52
    rngtest: input channel speed: (min=7.631; avg=7.804; max=7.827)Kibits/s
    rngtest: FIPS tests speed: (min=32.273; avg=32.701; max=33.056)Mibits/s
    rngtest: Program run time: 130177956 microseconds

---
v2 -> v3: Removal blank line, rebased
v1 -> v2: Removal of C++ style comment (I saw it too late, sry for that)
---
Lothar Rubusch (3):
crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data
crypto: atmel-sha204a - fix truncated 32-byte blocking read
crypto: atmel-sha204a - fix non-blocking read logic

 drivers/crypto/atmel-sha204a.c | 60 ++++++++++++++++++++++------------
 1 file changed, 39 insertions(+), 21 deletions(-)

base-commit: 3bfbf5f0a99c991769ec562721285df7ab69240b

Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/3 crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data

The driver allocated memory for work_data in the non-blocking read path but never free'd it again. After first read-out the memory pointer seemed to be recycled and never was allocated again, due to some errors in the logic, so that the leak was not growing.

Add kfree(work_data) in the completion callback on error. then add kfree(work_data) after the data is consumed in the subsequent read call. Finally ensure atomic_dec() is called only after the data has been consumed or an error occurred to prevent race conditions.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
---
 drivers/crypto/atmel-sha204a.c | 43 ++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 17 deletions(-)
drivers/crypto/atmel-sha204a.c +26 -17
index dbb39ed0cea1..19720bdd446d 100644
@@ -25,13 +25,17 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
 	struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
 	struct hwrng *rng = areq;
 
-	if (status)
+	if (status) {
 		dev_warn_ratelimited(&i2c_priv->client->dev,
 				     "i2c transaction failed (%d)\n",
 				     status);
+		kfree(work_data);
+		rng->priv = 0;
+		atomic_dec(&i2c_priv->tfm_count);
+		return;
+	}
 
 	rng->priv = (unsigned long)work_data;
-	atomic_dec(&i2c_priv->tfm_count);
 }
 
 static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
@@ -42,31 +46,36 @@ static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
 
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
-	/* keep maximum 1 asynchronous read in flight at any time */
-	if (!atomic_add_unless(&i2c_priv->tfm_count, 1, 1))
-		return 0;
-
+	/* Verify if data available from last run */
 	if (rng->priv) {
 		work_data = (struct atmel_i2c_work_data *)rng->priv;
 		max = min(sizeof(work_data->cmd.data), max);
 		memcpy(data, &work_data->cmd.data, max);
-		rng->priv = 0;
-	} else {
-		work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
-		if (!work_data) {
-			atomic_dec(&i2c_priv->tfm_count);
-			return -ENOMEM;
-		}
-		work_data->ctx = i2c_priv;
-		work_data->client = i2c_priv->client;
 
-		max = 0;
+		/* Now, free memory */
+		kfree(work_data);
+		rng->priv = 0;
+		atomic_dec(&i2c_priv->tfm_count);
+		return max;
 	}
 
+	/* When a request is still in-flight but not processed */
+	if (atomic_read(&i2c_priv->tfm_count) > 0)
+		return 0;
+
+	/* Start a new request */
+	work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
+	if (!work_data)
+		return -ENOMEM;
+
+	atomic_inc(&i2c_priv->tfm_count);
+	work_data->ctx = i2c_priv;
+	work_data->client = i2c_priv->client;
+
 	atmel_i2c_init_random_cmd(&work_data->cmd);
 	atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
 
-	return max;
+	return 0;
 }
 
 static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
Signature · 1 line
2.53.0
#5 lore raw

PATCH v3 2/3 crypto: atmel-sha204a - fix truncated 32-byte blocking read

In reply to Lothar Rubusch

The ATSHA204A returns a 35-byte packet consisting of a 1-byte count, 32 bytes of entropy, and a 2-byte CRC. The current blocking read implementation was incorrectly copying data starting from the count byte, leading to offset data and truncated entropy.

Additionally, the chip requires significant execution time to generate random numbers, going by the datasheet. Reading the I2C bus too early results in the chip NACK-ing or returning a partial buffer followed by zeros.

Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  02 28 85 b3 47 40 f2 ee  00 00 00 00 00 00 00 00  |.(..G@..........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020

After this patch applied, the result will be as follows:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  5a fc 3f 13 14 68 fe 06  68 0a bd 04 83 6e 09 69  |Z.?..h..h....n.i|
00000010  75 ff cf 87 10 84 3b c9  c1 df ae eb 45 53 4c c3  |u.....;.....ESL.|
00000020

Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire hardware response. Eventually, correct the memcpy() offset to index 1 of the data buffer to skip the count byte and retrieve exactly 32 bytes of entropy.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
---
 drivers/crypto/atmel-sha204a.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/crypto/atmel-sha204a.c +7 -2
index 19720bdd446d..f7dc00d0f4cd 100644
@@ -19,6 +19,9 @@
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
+#define ATMEL_RNG_BLOCK_SIZE 32
+#define ATMEL_RNG_EXEC_TIME 30
+
 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
 				   void *areq, int status)
 {
@@ -91,13 +94,15 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
 	atmel_i2c_init_random_cmd(&cmd);
+	cmd.msecs = ATMEL_RNG_EXEC_TIME;
+	cmd.rxsize = RANDOM_RSP_SIZE;
 
 	ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
 	if (ret)
 		return ret;
 
-	max = min(sizeof(cmd.data), max);
-	memcpy(data, cmd.data, max);
+	max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+	memcpy(data, &cmd.data[1], max);
 
 	return max;
 }
Signature · 1 line
2.53.0
#11 lore raw

PATCH v3 3/3 crypto: atmel-sha204a - fix non-blocking read logic

In reply to Lothar Rubusch

The non-blocking path was (also) failing to provide valid entropy due to improper buffer management and a lack of hardware execution time.

Ensure cmd.msecs (30ms) and cmd.rxsize (35ms) are initialized before enqueuing the background work. Fix the data offset to skip the 1-byte hardware count header when copying bits to the caller. Correctly return 0 (busy) to the hwrng core while hardware execution is in progress, preventing zero-filled buffers, which was the situation before.

With this fix applied, tests will look similar to this:

$ socat -u OPEN:/dev/hwrng,nonblock - | head -c 32 | hexdump -C
00000000  23 cc 42 3c 90 b1 38 fc  54 37 35 4b 09 c5 e1 0d  |#.B<..8.T75K....|
2026/03/23 14:30:18 socat[858] E read(5, 0x55be363000, 8192): Resource temporarily unavailable
00000010  73 3b af d9 02 70 76 bd  2d 59 4b 12 01 ac ae 2b  |s;...pv.-YK....+|
00000020
Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
---
 drivers/crypto/atmel-sha204a.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/crypto/atmel-sha204a.c +10 -6
index f7dc00d0f4cd..04cbf80c1411 100644
@@ -33,7 +33,6 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
 				     "i2c transaction failed (%d)\n",
 				     status);
 		kfree(work_data);
-		rng->priv = 0;
 		atomic_dec(&i2c_priv->tfm_count);
 		return;
 	}
@@ -49,20 +48,19 @@ static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
 
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
-	/* Verify if data available from last run */
 	if (rng->priv) {
 		work_data = (struct atmel_i2c_work_data *)rng->priv;
-		max = min(sizeof(work_data->cmd.data), max);
-		memcpy(data, &work_data->cmd.data, max);
+		max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+		memcpy(data, &work_data->cmd.data[1], max);
 
-		/* Now, free memory */
+		/* Free memory and clear the in-flight flag */
 		kfree(work_data);
 		rng->priv = 0;
 		atomic_dec(&i2c_priv->tfm_count);
 		return max;
 	}
 
-	/* When a request is still in-flight but not processed */
+	/* If a request is still in-flight, return 0 (busy) */
 	if (atomic_read(&i2c_priv->tfm_count) > 0)
 		return 0;
 
@@ -76,8 +74,14 @@ static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
 	work_data->client = i2c_priv->client;
 
 	atmel_i2c_init_random_cmd(&work_data->cmd);
+
+	/* Set the execution time for the RNG command (from datasheet) */
+	work_data->cmd.msecs = ATMEL_RNG_EXEC_TIME;
+	work_data->cmd.rxsize = RANDOM_RSP_SIZE;
+
 	atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
 
+	/* Return 0 to indicate 'busy', data will be ready on next call */
 	return 0;
 }
 
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v3 1/3] crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data

In reply to Lothar Rubusch

Hi Lothar,

Quoted 5 lines · Lothar Rubusch

The driver allocated memory for work_data in the non-blocking read path but never free'd it again. After first read-out the memory pointer seemed to be recycled and never was allocated again, due to some errors in the logic, so that the leak was not growing.

Why can't we just reuse the work_data, instead of alloc/freeing it every time?

Quoted 94 lines

Add kfree(work_data) in the completion callback on error. then add kfree(work_data) after the data is consumed in the subsequent read call. Finally ensure atomic_dec() is called only after the data has been consumed or an error occurred to prevent race conditions.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A

random number generator")

---
 drivers/crypto/atmel-sha204a.c | 43 ++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 17 deletions(-)
drivers/crypto/atmel-sha204a.c +6 -2
index dbb39ed0cea1..19720bdd446d 100644
@@ -25,13 +25,17 @@ static void atmel_sha204a_rng_done(struct 
 atmel_i2c_work_data *work_data,
 	struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
 	struct hwrng *rng = areq;
 
-	if (status)
+	if (status) {
 		dev_warn_ratelimited(&i2c_priv->client->dev,
 				     "i2c transaction failed (%d)\n",
 				     status);
+		kfree(work_data);
+		rng->priv = 0;
+		atomic_dec(&i2c_priv->tfm_count);
+		return;
+	}
 
 	rng->priv = (unsigned long)work_data;
-	atomic_dec(&i2c_priv->tfm_count);
 }
 

static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void
*data,

drivers/crypto/atmel-sha204a.c +20 -15
@@ -42,31 +46,36 @@ static int 
 atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
 
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
-	/* keep maximum 1 asynchronous read in flight at any time */
-	if (!atomic_add_unless(&i2c_priv->tfm_count, 1, 1))
-		return 0;
-
+	/* Verify if data available from last run */
 	if (rng->priv) {
 		work_data = (struct atmel_i2c_work_data *)rng->priv;
 		max = min(sizeof(work_data->cmd.data), max);
 		memcpy(data, &work_data->cmd.data, max);
-		rng->priv = 0;
-	} else {
-		work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
-		if (!work_data) {
-			atomic_dec(&i2c_priv->tfm_count);
-			return -ENOMEM;
-		}
-		work_data->ctx = i2c_priv;
-		work_data->client = i2c_priv->client;
 
-		max = 0;
+		/* Now, free memory */
+		kfree(work_data);
+		rng->priv = 0;
+		atomic_dec(&i2c_priv->tfm_count);
+		return max;
 	}
 
+	/* When a request is still in-flight but not processed */
+	if (atomic_read(&i2c_priv->tfm_count) > 0)
+		return 0;
+
+	/* Start a new request */
+	work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
+	if (!work_data)
+		return -ENOMEM;
+
+	atomic_inc(&i2c_priv->tfm_count);
+	work_data->ctx = i2c_priv;
+	work_data->client = i2c_priv->client;
+
 	atmel_i2c_init_random_cmd(&work_data->cmd);
 	atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
 
-	return max;
+	return 0;
 }
 

static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,

Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH v3 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read

In reply to Lothar Rubusch
Quoted 53 lines · Lothar Rubusch

The ATSHA204A returns a 35-byte packet consisting of a 1-byte count, 32 bytes of entropy, and a 2-byte CRC. The current blocking read implementation was incorrectly copying data starting from the count byte, leading to offset data and truncated entropy.

Additionally, the chip requires significant execution time to generate random numbers, going by the datasheet. Reading the I2C bus too early results in the chip NACK-ing or returning a partial buffer followed by zeros.

Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  02 28 85 b3 47 40 f2 ee  00 00 00 00 00 00 00 00  |.(..G@..........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020

After this patch applied, the result will be as follows:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  5a fc 3f 13 14 68 fe 06  68 0a bd 04 83 6e 09 69  |Z.?..h..h....n.i|
00000010  75 ff cf 87 10 84 3b c9  c1 df ae eb 45 53 4c c3  |u.....;.....ESL.|
00000020

Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire hardware response. Eventually, correct the memcpy() offset to index 1 of the data buffer to skip the count byte and retrieve exactly 32 bytes of entropy.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A

random number generator")

---
 drivers/crypto/atmel-sha204a.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/crypto/atmel-sha204a.c +3 -0
index 19720bdd446d..f7dc00d0f4cd 100644
@@ -19,6 +19,9 @@
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
+#define ATMEL_RNG_BLOCK_SIZE 32
+#define ATMEL_RNG_EXEC_TIME 30
+
 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data 
 *work_data,
 				   void *areq, int status)

{

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng · line 100

Quoted 7 lines
drivers/crypto/atmel-sha204a.c +2 -0
@@ -91,13 +94,15 @@ static int atmel_sha204a_rng_read(struct hwrng 
 *rng, void *data, size_t max,
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
 	atmel_i2c_init_random_cmd(&cmd);
+	cmd.msecs = ATMEL_RNG_EXEC_TIME;
+	cmd.rxsize = RANDOM_RSP_SIZE;
 

Please fix atmel_i2c_init_random_cmd() instead if it doesn't set the right values for these fields. But afaict, you are decreasing the execution time here, so I struggle to see how this could explain the improved behavior.

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 106

Quoted 9 lines
drivers/crypto/atmel-sha204a.c +2 -2
 	ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
 	if (ret)
 		return ret;
 
-	max = min(sizeof(cmd.data), max);
-	memcpy(data, cmd.data, max);
+	max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+	memcpy(data, &cmd.data[1], max);
 

This looks correct - better to put this in a separate patch.

Quoted 4 lines
drivers/crypto/atmel-sha204a.c +0 -0
 	return max;

}

Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCH v3 3/3] crypto: atmel-sha204a - fix non-blocking read logic

In reply to Lothar Rubusch
Quoted 45 lines · Lothar Rubusch

The non-blocking path was (also) failing to provide valid entropy due to improper buffer management and a lack of hardware execution time.

Ensure cmd.msecs (30ms) and cmd.rxsize (35ms) are initialized before enqueuing the background work. Fix the data offset to skip the 1-byte hardware count header when copying bits to the caller. Correctly return 0 (busy) to the hwrng core while hardware execution is in progress, preventing zero-filled buffers, which was the situation before.

With this fix applied, tests will look similar to this:

$ socat -u OPEN:/dev/hwrng,nonblock - | head -c 32 | hexdump -C
00000000  23 cc 42 3c 90 b1 38 fc  54 37 35 4b 09 c5 e1 0d  
|#.B<..8.T75K....|
2026/03/23 14:30:18 socat[858] E read(5, 0x55be363000, 8192): Resource 
temporarily unavailable
00000010  73 3b af d9 02 70 76 bd  2d 59 4b 12 01 ac ae 2b  
|s;...pv.-YK....+|
00000020
Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A

random number generator")

---
 drivers/crypto/atmel-sha204a.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/crypto/atmel-sha204a.c +0 -1
index f7dc00d0f4cd..04cbf80c1411 100644
@@ -33,7 +33,6 @@ static void atmel_sha204a_rng_done(struct 
 atmel_i2c_work_data *work_data,
 				     "i2c transaction failed (%d)\n",
 				     status);
 		kfree(work_data);
-		rng->priv = 0;
 		atomic_dec(&i2c_priv->tfm_count);
 		return;
drivers/crypto/atmel-sha204a.c +0 -0
 	}
@@ -49,20 +48,19 @@ static int 
 atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
 

drivers/crypto/atmel-sha204a.c static int · line 56

Quoted 10 lines
drivers/crypto/atmel-sha204a.c +2 -3
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
-	/* Verify if data available from last run */
 	if (rng->priv) {
 		work_data = (struct atmel_i2c_work_data *)rng->priv;
-		max = min(sizeof(work_data->cmd.data), max);
-		memcpy(data, &work_data->cmd.data, max);
+		max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+		memcpy(data, &work_data->cmd.data[1], max);
 

Please combine this with the buffer size fix in the previous patch.

Quoted 14 lines
drivers/crypto/atmel-sha204a.c +2 -2
-		/* Now, free memory */
+		/* Free memory and clear the in-flight flag */
 		kfree(work_data);
 		rng->priv = 0;
 		atomic_dec(&i2c_priv->tfm_count);
 		return max;
 	}
 
-	/* When a request is still in-flight but not processed */
+	/* If a request is still in-flight, return 0 (busy) */
 	if (atomic_read(&i2c_priv->tfm_count) > 0)
 		return 0;

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read_nonblocking(struct · line 82

Quoted 9 lines
drivers/crypto/atmel-sha204a.c +5 -0
@@ -76,8 +74,14 @@ static int atmel_sha204a_rng_read_nonblocking(struct 
 hwrng *rng, void *data,
 	work_data->client = i2c_priv->client;
 
 	atmel_i2c_init_random_cmd(&work_data->cmd);
+
+	/* Set the execution time for the RNG command (from datasheet) */
+	work_data->cmd.msecs = ATMEL_RNG_EXEC_TIME;
+	work_data->cmd.rxsize = RANDOM_RSP_SIZE;
+

Again, this is either redundant or wrong.

Quoted 8 lines
drivers/crypto/atmel-sha204a.c +1 -0
 	atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
 
+	/* Return 0 to indicate 'busy', data will be ready on next call */
 	return 0;
 }
Signature · 2 lines
-- 
2.53.0
#13 lore raw
In reply to Lothar Rubusch

Hi Lothar,

Quoted 52 lines · Lothar Rubusch

When testing the RNG functionality on the Atmel SHA204a hardware, I found the following issues: rngtest reported failures and hexdump reveiled only the first 8 bytes out of 32 provided actually entropy.

Having a closer look into it, I found a (small) memory leak, missing to free work_data, miss-reading of the count field into the entropy fields and parts of the 32 random bytes staying 0 due to reading the slow i2c device.

The series proposes fixes and how fixed functionality can be/was verified. Executing rngtest afterward showed a decent result, due to the i2c bus a bit slow.

All setups require selecting the Atmel-sha204a as active RNG.

$ cat /sys/class/misc/hw_random/rng_available
    3f104000.rng 1-0064 none
$ echo 1-0064 > /sys/class/misc/hw_random/rng_current
$ cat /sys/class/misc/hw_random/rng_current
    1-0064

Testing RNG properties currently shows problematic results:

$ rngtest < /dev/hwrng
    rngtest 2.6
    Copyright (c) 2004 by Henrique de Moraes Holschuh
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    rngtest: starting FIPS tests...
    rngtest: bits received from input: 1040032
    rngtest: FIPS 140-2 successes: 0
    rngtest: FIPS 140-2 failures: 52
    rngtest: FIPS 140-2(2001-10-10) Monobit: 52
    rngtest: FIPS 140-2(2001-10-10) Poker: 52
    rngtest: FIPS 140-2(2001-10-10) Runs: 52
    rngtest: FIPS 140-2(2001-10-10) Long run: 52
    rngtest: FIPS 140-2(2001-10-10) Continuous run: 52
    rngtest: input channel speed: (min=7.631; avg=7.804; max=7.827)Kibits/s
    rngtest: FIPS tests speed: (min=32.273; avg=32.701; max=33.056)Mibits/s
    rngtest: Program run time: 130177956 microseconds

---
v2 -> v3: Removal blank line, rebased
v1 -> v2: Removal of C++ style comment (I saw it too late, sry for that)
---
Lothar Rubusch (3):
crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data
crypto: atmel-sha204a - fix truncated 32-byte blocking read
crypto: atmel-sha204a - fix non-blocking read logic

Quoted 3 lines
 drivers/crypto/atmel-sha204a.c | 60 ++++++++++++++++++++++------------
 1 file changed, 39 insertions(+), 21 deletions(-)

Thanks for the report and the fixes. However, I'm not sure you are entirely on the right track here. I managed to fix the rngtest issues that you report by making the changes below. As I already replied, I think it would be better to propose this as a standalone patch, and backport it to stable.

The remaining changes are somewhat debatable IMO: the leak is not really a leak, so I'd like to understand better what you are fixing here. The command field changes seems completely misguided (unless I am missing something)

drivers/crypto/atmel-sha204a.c +4 -4
@@ -47,8 +47,8 @@
 
        if (rng->priv) {
                work_data = (struct atmel_i2c_work_data *)rng->priv;
-               max = min(sizeof(work_data->cmd.data), max);
-               memcpy(data, &work_data->cmd.data, max);
+               max = min(RANDOM_RSP_SIZE - CMD_OVERHEAD_SIZE, max);
+               memcpy(data, &work_data->cmd.data[1], max);
                rng->priv = 0;
        } else {
                work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
@@ -86,8 +86,8 @@
        if (ret)
                return ret;
 
-       max = min(sizeof(cmd.data), max);
-       memcpy(data, cmd.data, max);
+       max = min(RANDOM_RSP_SIZE - CMD_OVERHEAD_SIZE, max);
+       memcpy(data, &cmd.data[1], max);
 
        return max;
 }
#14 lore raw

Hi Ard and ML!

Quoted 56 lines · Ard Biesheuvel

Hi Lothar,

Quoted 52 lines · Lothar Rubusch

When testing the RNG functionality on the Atmel SHA204a hardware, I found the following issues: rngtest reported failures and hexdump reveiled only the first 8 bytes out of 32 provided actually entropy.

Having a closer look into it, I found a (small) memory leak, missing to free work_data, miss-reading of the count field into the entropy fields and parts of the 32 random bytes staying 0 due to reading the slow i2c device.

The series proposes fixes and how fixed functionality can be/was verified. Executing rngtest afterward showed a decent result, due to the i2c bus a bit slow.

All setups require selecting the Atmel-sha204a as active RNG.

$ cat /sys/class/misc/hw_random/rng_available
    3f104000.rng 1-0064 none
$ echo 1-0064 > /sys/class/misc/hw_random/rng_current
$ cat /sys/class/misc/hw_random/rng_current
    1-0064

Testing RNG properties currently shows problematic results:

$ rngtest < /dev/hwrng
    rngtest 2.6
    Copyright (c) 2004 by Henrique de Moraes Holschuh
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    rngtest: starting FIPS tests...
    rngtest: bits received from input: 1040032
    rngtest: FIPS 140-2 successes: 0
    rngtest: FIPS 140-2 failures: 52
    rngtest: FIPS 140-2(2001-10-10) Monobit: 52
    rngtest: FIPS 140-2(2001-10-10) Poker: 52
    rngtest: FIPS 140-2(2001-10-10) Runs: 52
    rngtest: FIPS 140-2(2001-10-10) Long run: 52
    rngtest: FIPS 140-2(2001-10-10) Continuous run: 52
    rngtest: input channel speed: (min=7.631; avg=7.804; max=7.827)Kibits/s
    rngtest: FIPS tests speed: (min=32.273; avg=32.701; max=33.056)Mibits/s
    rngtest: Program run time: 130177956 microseconds

---
v2 -> v3: Removal blank line, rebased
v1 -> v2: Removal of C++ style comment (I saw it too late, sry for that)
---
Lothar Rubusch (3):
crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data
crypto: atmel-sha204a - fix truncated 32-byte blocking read
crypto: atmel-sha204a - fix non-blocking read logic

Quoted 9 lines
Quoted 3 lines
 drivers/crypto/atmel-sha204a.c | 60 ++++++++++++++++++++++------------
 1 file changed, 39 insertions(+), 21 deletions(-)

Thanks for the report and the fixes. However, I'm not sure you are entirely on the right track here. I managed to fix the rngtest issues that you report by making the changes below. As I already replied, I think it would be better to propose this as a standalone patch, and backport it to stable.

Thank you so much for taking the time and answering, I really appreciate!

Like two months ago I started playing a bit more with these Atmel i2c devices. Actually I wanted to prepare something
different (and still pretend doing so). Figuring out things here, I noticed this RNG feature did not work as I expected. Around that time I prepared this patch set.
Probably, I was a bit too euphoric - I mean, it's a bit unlikely the driver was upstreamed and never really worked.
I have to admit, I did not go thoroughly through the git history first, since the driver does not do significantly more than RNG, there must have happened a degradation. I'll keep this in mind:
first to build up better understanding of the driver history. than to start digging into the code. Anyway, I wanted to get some feedback.

Quoted 4 lines

The remaining changes are somewhat debatable IMO: the leak is not really a leak, so I'd like to understand better what you are fixing here. The command field changes seems completely misguided (unless I am missing something)

So, firstly in the current RNG implementation I definitely see an issue. If that's ok, I'll to prepare a patch to re-init probably rather
to what you proposed, since it makes more sense to me now. Thank you for pointing this out.

Secondly, I noticed the device is picky about its i2c communication timings. Yes, definitely. Nowadays, after having worked with it a bit more, I'd rather say generally reducing response time is probably not the solution. I still think I see some timing issue, which
I wanted to better address in an upcoming patch set (but probably not related to this RNG thing). Currently the AtSHA204a and
the AtECC508a (and related families) are sharing the same Atmel i2c driver. This core driver uses hardcoded max timings for
its i2c command operations. Going over the datasheets of both chips, both have totally different max timings.
Currently just having RNG for AtSHA204a and ECDH and related for AtECC
- it probably doesn't matter too much. Anyway, also
read and write command timings differ and mix up then in the Atmel i2c core driver. I guess, this was kind of a starting point for me to dig deeper. So, I probably will drop that i2c timing change from this RNG fix set.

Thirdly, the "leak" I think is probably rather a theoretic issue. I'll sepearate it out, think over it, and in case prepare something, let me then know what you think.

Quoted 26 lines
drivers/crypto/atmel-sha204a.c +4 -4
@@ -47,8 +47,8 @@
 
        if (rng->priv) {
                work_data = (struct atmel_i2c_work_data *)rng->priv;
-               max = min(sizeof(work_data->cmd.data), max);
-               memcpy(data, &work_data->cmd.data, max);
+               max = min(RANDOM_RSP_SIZE - CMD_OVERHEAD_SIZE, max);
+               memcpy(data, &work_data->cmd.data[1], max);
                rng->priv = 0;
        } else {
                work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
@@ -86,8 +86,8 @@
        if (ret)
                return ret;
 
-       max = min(sizeof(cmd.data), max);
-       memcpy(data, cmd.data, max);
+       max = min(RANDOM_RSP_SIZE - CMD_OVERHEAD_SIZE, max);
+       memcpy(data, &cmd.data[1], max);
 
        return max;
 }
#7 lore raw

Re: [PATCH v3 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read

In reply to Lothar Rubusch

Hi Lothar,

Quoted 30 lines · Lothar Rubusch

The ATSHA204A returns a 35-byte packet consisting of a 1-byte count, 32 bytes of entropy, and a 2-byte CRC. The current blocking read implementation was incorrectly copying data starting from the count byte, leading to offset data and truncated entropy.

Additionally, the chip requires significant execution time to generate random numbers, going by the datasheet. Reading the I2C bus too early results in the chip NACK-ing or returning a partial buffer followed by zeros.

Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  02 28 85 b3 47 40 f2 ee  00 00 00 00 00 00 00 00  |.(..G@..........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020

After this patch applied, the result will be as follows:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  5a fc 3f 13 14 68 fe 06  68 0a bd 04 83 6e 09 69  |Z.?..h..h....n.i|
00000010  75 ff cf 87 10 84 3b c9  c1 df ae eb 45 53 4c c3  |u.....;.....ESL.|
00000020

Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire hardware response. Eventually, correct the memcpy() offset to index 1 of the data buffer to skip the count byte and retrieve exactly 32 bytes of entropy.

Quoted 2 lines
Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")

Thank you for your patches. I tested patch 2/3 on real hardware and it fixes rngtest for me. However, I have a few comments below.

Quoted 19 lines
---
 drivers/crypto/atmel-sha204a.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/crypto/atmel-sha204a.c +3 -0
index 19720bdd446d..f7dc00d0f4cd 100644
@@ -19,6 +19,9 @@
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
+#define ATMEL_RNG_BLOCK_SIZE 32
+#define ATMEL_RNG_EXEC_TIME 30
+
 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
 				   void *areq, int status)
 {

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 98

Quoted 5 lines
drivers/crypto/atmel-sha204a.c +2 -0
@@ -91,13 +94,15 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
 	i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
 	atmel_i2c_init_random_cmd(&cmd);
+	cmd.msecs = ATMEL_RNG_EXEC_TIME;
+	cmd.rxsize = RANDOM_RSP_SIZE;

atmel_i2c_init_random_cmd() already sets cmd.rxsize to RANDOM_RSP_SIZE.

Changing cmd.msecs does not appear to be strictly necessary for the fix. The only difference I observe is that rngtest runs faster with the new value.

Here are my test results without changing cmd.msecs:

$ sudo head -c 300000 /dev/hwrng | rngtest -c 100
[...]
rngtest: starting FIPS tests...
rngtest: bits received from input: 2000032
rngtest: FIPS 140-2 successes: 100
rngtest: FIPS 140-2 failures: 0
[...]
rngtest: input channel speed: (min=1.118; avg=3.746; max=6510416.667)Kibits/s
rngtest: FIPS tests speed: (min=26.272; avg=27.471; max=32.829)Mibits/s
rngtest: Program run time: 538942640 microseconds

and with 'cmd.msecs = ATMEL_RNG_EXEC_TIME':

$ sudo head -c 300000 /dev/hwrng | rngtest -c 100
[...]
rngtest: starting FIPS tests...
rngtest: bits received from input: 2000032
rngtest: FIPS 140-2 successes: 100
rngtest: FIPS 140-2 failures: 0
[...]
rngtest: input channel speed: (min=1.584; avg=5.295; max=6510416.667)Kibits/s
rngtest: FIPS tests speed: (min=26.602; avg=27.321; max=28.257)Mibits/s
rngtest: Program run time: 381309284 microseconds

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 105

Quoted 8 lines
drivers/crypto/atmel-sha204a.c +2 -2
 	ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
 	if (ret)
 		return ret;
 
-	max = min(sizeof(cmd.data), max);
-	memcpy(data, cmd.data, max);
+	max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+	memcpy(data, &cmd.data[1], max);

This works and fixes rngtest for me on real hardware.

min_t() is not strictly necessary here, since the types are compatible and min() is sufficient.

I agree with Ard that patch 2/3 should be a separate patch for easier stable backporting.

Thanks,
Thorsten

#8 lore raw

Re: [PATCH v3 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read

Quoted 35 lines · Thorsten Blum

Hi Lothar,

Quoted 32 lines · Lothar Rubusch

The ATSHA204A returns a 35-byte packet consisting of a 1-byte count, 32 bytes of entropy, and a 2-byte CRC. The current blocking read implementation was incorrectly copying data starting from the count byte, leading to offset data and truncated entropy.

Additionally, the chip requires significant execution time to generate random numbers, going by the datasheet. Reading the I2C bus too early results in the chip NACK-ing or returning a partial buffer followed by zeros.

Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  02 28 85 b3 47 40 f2 ee  00 00 00 00 00 00 00 00  |.(..G@..........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020

After this patch applied, the result will be as follows:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  5a fc 3f 13 14 68 fe 06  68 0a bd 04 83 6e 09 69  |Z.?..h..h....n.i|
00000010  75 ff cf 87 10 84 3b c9  c1 df ae eb 45 53 4c c3  |u.....;.....ESL.|
00000020

Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire hardware response. Eventually, correct the memcpy() offset to index 1 of the data buffer to skip the count byte and retrieve exactly 32 bytes of entropy.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")

[...]

Quoted 4 lines

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 105

Quoted 4 lines
drivers/crypto/atmel-sha204a.c +2 -2
-	max = min(sizeof(cmd.data), max);
-	memcpy(data, cmd.data, max);
+	max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+	memcpy(data, &cmd.data[1], max);

I just noticed that index 1 is already defined as RSP_DATA_IDX in drivers/crypto/atmel-i2c.h.

Quoted 7 lines

This works and fixes rngtest for me on real hardware.

min_t() is not strictly necessary here, since the types are compatible and min() is sufficient.

I agree with Ard that patch 2/3 should be a separate patch for easier stable backporting.

Thanks,
Thorsten

#9 lore raw

Re: [PATCH v3 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read

In reply to Thorsten Blum

Hi Thorsten!

Quoted 106 lines · Thorsten Blum

Hi Lothar,

Quoted 30 lines · Lothar Rubusch

The ATSHA204A returns a 35-byte packet consisting of a 1-byte count, 32 bytes of entropy, and a 2-byte CRC. The current blocking read implementation was incorrectly copying data starting from the count byte, leading to offset data and truncated entropy.

Additionally, the chip requires significant execution time to generate random numbers, going by the datasheet. Reading the I2C bus too early results in the chip NACK-ing or returning a partial buffer followed by zeros.

Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  02 28 85 b3 47 40 f2 ee  00 00 00 00 00 00 00 00  |.(..G@..........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020

After this patch applied, the result will be as follows:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  5a fc 3f 13 14 68 fe 06  68 0a bd 04 83 6e 09 69  |Z.?..h..h....n.i|
00000010  75 ff cf 87 10 84 3b c9  c1 df ae eb 45 53 4c c3  |u.....;.....ESL.|
00000020

Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire hardware response. Eventually, correct the memcpy() offset to index 1 of the data buffer to skip the count byte and retrieve exactly 32 bytes of entropy.

Quoted 2 lines
Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")

Thank you for your patches. I tested patch 2/3 on real hardware and it fixes rngtest for me. However, I have a few comments below.

Quoted 19 lines
---
 drivers/crypto/atmel-sha204a.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/crypto/atmel-sha204a.c +3 -0
index 19720bdd446d..f7dc00d0f4cd 100644
@@ -19,6 +19,9 @@
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
+#define ATMEL_RNG_BLOCK_SIZE 32
+#define ATMEL_RNG_EXEC_TIME 30
+
 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
                                 void *areq, int status)
 {

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 98

Quoted 5 lines
drivers/crypto/atmel-sha204a.c +2 -0
@@ -91,13 +94,15 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
      i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
 
      atmel_i2c_init_random_cmd(&cmd);
+     cmd.msecs = ATMEL_RNG_EXEC_TIME;
+     cmd.rxsize = RANDOM_RSP_SIZE;

atmel_i2c_init_random_cmd() already sets cmd.rxsize to RANDOM_RSP_SIZE.

Changing cmd.msecs does not appear to be strictly necessary for the fix. The only difference I observe is that rngtest runs faster with the new value.

Here are my test results without changing cmd.msecs:

$ sudo head -c 300000 /dev/hwrng | rngtest -c 100
[...]
rngtest: starting FIPS tests...
rngtest: bits received from input: 2000032
rngtest: FIPS 140-2 successes: 100
rngtest: FIPS 140-2 failures: 0
[...]
rngtest: input channel speed: (min=1.118; avg=3.746; max=6510416.667)Kibits/s
rngtest: FIPS tests speed: (min=26.272; avg=27.471; max=32.829)Mibits/s
rngtest: Program run time: 538942640 microseconds

and with 'cmd.msecs = ATMEL_RNG_EXEC_TIME':

$ sudo head -c 300000 /dev/hwrng | rngtest -c 100
[...]
rngtest: starting FIPS tests...
rngtest: bits received from input: 2000032
rngtest: FIPS 140-2 successes: 100
rngtest: FIPS 140-2 failures: 0
[...]
rngtest: input channel speed: (min=1.584; avg=5.295; max=6510416.667)Kibits/s
rngtest: FIPS tests speed: (min=26.602; avg=27.321; max=28.257)Mibits/s
rngtest: Program run time: 381309284 microseconds

drivers/crypto/atmel-sha204a.c static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max, · line 105

Quoted 8 lines
drivers/crypto/atmel-sha204a.c +2 -2
      ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
      if (ret)
              return ret;
 
-     max = min(sizeof(cmd.data), max);
-     memcpy(data, cmd.data, max);
+     max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+     memcpy(data, &cmd.data[1], max);
Quoted 8 lines

This works and fixes rngtest for me on real hardware.

min_t() is not strictly necessary here, since the types are compatible and min() is sufficient.

I agree with Ard that patch 2/3 should be a separate patch for easier stable backporting.

I appreciate your answers and will take the review into account. Looks like we had similar plans for
this weekend. So, since I answered to Ard quite in parallel to your review, I'll restructure as I said in my
last mail on this list. I'll go through your comments and think about it.

Best,
L

Quoted 2 lines

Thanks,
Thorsten

#3 lore raw

Re: [PATCH v3 1/3] crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data

In reply to Lothar Rubusch
Quoted 2 lines · Lothar Rubusch

The driver allocated memory for work_data in the non-blocking read path but never free'd it again.

Yes, 'work_data' is allocated once on the first nonblocking RNG request and reused for subsequent requests, but the memory is eventually freed on device removal in atmel_sha204a_remove().

The memory might be retained unnecessarily after use when the device is idle (probably negligible), but it's not a memory leak.

Best,
Thorsten

#4 lore raw

Re: [PATCH v3 1/3] crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data

Hi Thorsten and Ard & ML,

This is just two general questions on some details of this driver. Perhaps you could
give me some insights here.

Quoted 9 lines · Thorsten Blum
Quoted 2 lines · Lothar Rubusch

The driver allocated memory for work_data in the non-blocking read path but never free'd it again.

Yes, 'work_data' is allocated once on the first nonblocking RNG request and reused for subsequent requests, but the memory is eventually freed on device removal in atmel_sha204a_remove().

Quoted 5 lines

The memory might be retained unnecessarily after use when the device is idle (probably negligible), but it's not a memory leak.

Best,
Thorsten

1. Actually, having work_data allocated once/rarely and usually re-used. Stupid question: could it be allocated also with managed device memory? I usually use devm_ allocs rather in probe for related general instances, here I'm a bit in doubt. Could it be used here as well, or why not?

2. In atmel_sha204a_rng_read() there is the function variable struct atmel_i2c_cmd cmd;.
AFAIR allocation on the heap is preferable to stack variables, particularly for structs and any non-primitive types. So, wouldn't it be better to have *cmd as a pointer, dynamically allocate memory at the beginning of this function and free it at the end of this function again?

Best,
L

#10 lore raw

Re: [PATCH v3 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read

In reply to Lothar Rubusch
Quoted 41 lines · Lothar Rubusch

The ATSHA204A returns a 35-byte packet consisting of a 1-byte count, 32 bytes of entropy, and a 2-byte CRC. The current blocking read implementation was incorrectly copying data starting from the count byte, leading to offset data and truncated entropy.

Additionally, the chip requires significant execution time to generate random numbers, going by the datasheet. Reading the I2C bus too early results in the chip NACK-ing or returning a partial buffer followed by zeros.

Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  02 28 85 b3 47 40 f2 ee  00 00 00 00 00 00 00 00  |.(..G@..........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020

After this patch applied, the result will be as follows:

$ head -c 32 /dev/hwrng | hexdump -C
00000000  5a fc 3f 13 14 68 fe 06  68 0a bd 04 83 6e 09 69  |Z.?..h..h....n.i|
00000010  75 ff cf 87 10 84 3b c9  c1 df ae eb 45 53 4c c3  |u.....;.....ESL.|
00000020

Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire hardware response. Eventually, correct the memcpy() offset to index 1 of the data buffer to skip the count byte and retrieve exactly 32 bytes of entropy.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
---
 drivers/crypto/atmel-sha204a.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/crypto/atmel-sha204a.c +0 -0
index 19720bdd446d..f7dc00d0f4cd 100644

drivers/crypto/atmel-sha204a.c line 23

Quoted 5 lines
drivers/crypto/atmel-sha204a.c +2 -0
@@ -19,6 +19,9 @@
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
+#define ATMEL_RNG_BLOCK_SIZE 32
+#define ATMEL_RNG_EXEC_TIME 30

Maybe I missed it, but how did you arrive at 30 ms from the datasheet?

Thanks,
Thorsten