Skip to content

Commit

Permalink
net: Use NDRNG device in srand_mac()
Browse files Browse the repository at this point in the history
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>
Reviewed-by: Torsten Duwe <duwe@suse.de>
  • Loading branch information
mbgg authored and trini committed Jan 19, 2021
1 parent 92fdad2 commit ea707dc
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion net/net_rand.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define __NET_RAND_H__

#include <common.h>
#include <dm/uclass.h>
#include <rng.h>

/*
* Return a seed for the PRNG derived from the eth0 MAC address.
Expand Down Expand Up @@ -37,7 +39,22 @@ static inline unsigned int seed_mac(void)
*/
static inline void srand_mac(void)
{
srand(seed_mac());
int ret;
struct udevice *devp;
u32 randv = 0;

if (IS_ENABLED(CONFIG_DM_RNG)) {
ret = uclass_get_device(UCLASS_RNG, 0, &devp);
if (ret) {
ret = dm_rng_read(devp, &randv, sizeof(randv));
if (ret < 0)
randv = 0;
}
}
if (randv)
srand(randv);
else
srand(seed_mac());
}

#endif /* __NET_RAND_H__ */

0 comments on commit ea707dc

Please sign in to comment.