Skip to contentSkip to conversation

net: make net_random_ethaddr() more random

PATCH In review Michael Walle 3 participants 4 messages Aug 27, 2019 – Sep 4, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: make net_random_ethaddr() more random

The net_random_ethaddr() tries to get some entropy from different startup times of a board. The seed is initialized with get_timer() which has only a granularity of milliseconds. We can do better if we use get_ticks() which returns the raw timer ticks. Using this we have a higher chance of getting different values at startup.

---
since v1:
- Removed the and-mask. Since there were no more comments I did some tests and apparently there are no compiler warnings.

 include/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net.h +1 -1
index a54d5eeac5..75a16e4c8f 100644
@@ -816,7 +816,7 @@ static inline int is_valid_ethaddr(const u8 *addr)
 static inline void net_random_ethaddr(uchar *addr)
 {
 	int i;
-	unsigned int seed = get_timer(0);
+	unsigned int seed = get_ticks();
 
 	for (i = 0; i < 6; i++)
 		addr[i] = rand_r(&seed);
Signature · 1 line
2.11.0
#1 lore raw

PATCH v2 1/1 net: make net_random_ethaddr() more random

Quoted 7 lines · Michael Walle

The net_random_ethaddr() tries to get some entropy from different startup times of a board. The seed is initialized with get_timer() which has only a granularity of milliseconds. We can do better if we use get_ticks() which returns the raw timer ticks. Using this we have a higher chance of getting different values at startup.

Quoted 9 lines

---
since v1:
- Removed the and-mask. Since there were no more comments I did some tests and apparently there are no compiler warnings.

 include/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#2 lore raw

PATCH v2 1/1 net: make net_random_ethaddr() more random

In reply to Michael Walle
Quoted 8 lines · Michael Walle

The net_random_ethaddr() tries to get some entropy from different startup times of a board. The seed is initialized with get_timer() which has only a granularity of milliseconds. We can do better if we use get_ticks() which returns the raw timer ticks. Using this we have a higher chance of getting different values at startup.