diff mbox series

[2/2] net: Use NDRNG device in srand_mac()

Message ID 20201216104117.10836-3-matthias.bgg@kernel.org
State Superseded
Delegated to: Tom Rini
Headers show
Series Use RNG to get random behaviour | expand

Commit Message

Matthias Brugger Dec. 16, 2020, 10:41 a.m. UTC
From: Matthias Brugger <mbrugger@suse.com>

When calling srand_mac we use a weak seed dependent on the
mac address. If present, use a RNG device instead to incerase entropy.

Signed-off-by: Matthias Brugger <mbrugger@suse.com>

---

 net/net_rand.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

Comments

Torsten Duwe Dec. 16, 2020, 1:20 p.m. UTC | #1
On Wed, 16 Dec 2020 11:41:17 +0100
matthias.bgg@kernel.org wrote:

> From: Matthias Brugger <mbrugger@suse.com>
> 
> When calling srand_mac we use a weak seed dependent on the
> mac address. If present, use a RNG device instead to incerase entropy.
> 
> Signed-off-by: Matthias Brugger <mbrugger@suse.com>
> 
> ---
> 
>  net/net_rand.h | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/net/net_rand.h b/net/net_rand.h
> index 4bf9bd817e..600c3d825e 100644
> --- a/net/net_rand.h
> +++ b/net/net_rand.h
> @@ -10,6 +10,7 @@
>  #define __NET_RAND_H__
>  
>  #include <common.h>
> +#include <rng.h>
>  
>  /*
>   * Return a seed for the PRNG derived from the eth0 MAC address.
> @@ -37,7 +38,22 @@ static inline unsigned int seed_mac(void)
>   */
>  static inline void srand_mac(void)
>  {
> -	srand(seed_mac());
> +#if defined(CONFIG_DM_RNG)
> +	int ret;
> +	struct udevice *devp;
> +	u32 randv = 0;
> +
> +	ret = uclass_get_device(UCLASS_RNG, 0, &devp);
> +	if (ret) {
> +		ret = dm_rng_read(dev, randv, sizeof(randv));
Haven't tested this (yet), but shouldn't this be
		ret = dm_rng_read(devp, &randv, sizeof(randv));
		                     ^  ^ ?
> +		if (ret < 0)
> +			randv = 0;
> +	}
> +	if (randv)
> +		srand(randv);
> +	else
> +#endif
> +		srand(seed_mac());
>  }
>  
>  #endif /* __NET_RAND_H__ */
Matthias Brugger Dec. 16, 2020, 3:56 p.m. UTC | #2
On 16/12/2020 14:20, Torsten Duwe wrote:
> On Wed, 16 Dec 2020 11:41:17 +0100
> matthias.bgg@kernel.org wrote:
> 
>> From: Matthias Brugger <mbrugger@suse.com>
>>
>> When calling srand_mac we use a weak seed dependent on the
>> mac address. If present, use a RNG device instead to incerase entropy.
>>
>> Signed-off-by: Matthias Brugger <mbrugger@suse.com>
>>
>> ---
>>
>>  net/net_rand.h | 18 +++++++++++++++++-
>>  1 file changed, 17 insertions(+), 1 deletion(-)
>>
>> diff --git a/net/net_rand.h b/net/net_rand.h
>> index 4bf9bd817e..600c3d825e 100644
>> --- a/net/net_rand.h
>> +++ b/net/net_rand.h
>> @@ -10,6 +10,7 @@
>>  #define __NET_RAND_H__
>>  
>>  #include <common.h>
>> +#include <rng.h>
>>  
>>  /*
>>   * Return a seed for the PRNG derived from the eth0 MAC address.
>> @@ -37,7 +38,22 @@ static inline unsigned int seed_mac(void)
>>   */
>>  static inline void srand_mac(void)
>>  {
>> -	srand(seed_mac());
>> +#if defined(CONFIG_DM_RNG)
>> +	int ret;
>> +	struct udevice *devp;
>> +	u32 randv = 0;
>> +
>> +	ret = uclass_get_device(UCLASS_RNG, 0, &devp);
>> +	if (ret) {
>> +		ret = dm_rng_read(dev, randv, sizeof(randv));
> Haven't tested this (yet), but shouldn't this be
> 		ret = dm_rng_read(devp, &randv, sizeof(randv));
> 		                     ^  ^ ?

Ups, yes you are right. I'll send a v2.

Regards,
Matthias
diff mbox series

Patch

diff --git a/net/net_rand.h b/net/net_rand.h
index 4bf9bd817e..600c3d825e 100644
--- a/net/net_rand.h
+++ b/net/net_rand.h
@@ -10,6 +10,7 @@ 
 #define __NET_RAND_H__
 
 #include <common.h>
+#include <rng.h>
 
 /*
  * Return a seed for the PRNG derived from the eth0 MAC address.
@@ -37,7 +38,22 @@  static inline unsigned int seed_mac(void)
  */
 static inline void srand_mac(void)
 {
-	srand(seed_mac());
+#if defined(CONFIG_DM_RNG)
+	int ret;
+	struct udevice *devp;
+	u32 randv = 0;
+
+	ret = uclass_get_device(UCLASS_RNG, 0, &devp);
+	if (ret) {
+		ret = dm_rng_read(dev, randv, sizeof(randv));
+		if (ret < 0)
+			randv = 0;
+	}
+	if (randv)
+		srand(randv);
+	else
+#endif
+		srand(seed_mac());
 }
 
 #endif /* __NET_RAND_H__ */