Skip to content

sys/luid: shuffle bytes#14986

Merged
benpicco merged 1 commit intoRIOT-OS:masterfrom
benpicco:sys/luid-shuffle
Sep 11, 2020
Merged

sys/luid: shuffle bytes#14986
benpicco merged 1 commit intoRIOT-OS:masterfrom
benpicco:sys/luid-shuffle

Conversation

@benpicco
Copy link
Contributor

@benpicco benpicco commented Sep 9, 2020

Contribution description

The CPU ID can have large parts that are identical between devices.
To add some more entropy to the generated IDs, shuffle the bytes.

Testing procedure

#include <stdio.h>
#include "luid.h"

int main(void)
{
    eui64_t addr;
    luid_get_eui64(&addr);

    for (size_t i = 0; i < sizeof(addr); ++i) {
        printf("%02x ", addr.uint8[i]);
    }
    puts("");

    return 0;
}

On two weact-f411ce boards (same stm32 CPU) this generates

1a 14 15 22 72 17 78 10

and

12 21 15 23 53 29 72 1b

Issues/PRs references

#14634 (comment)

@benpicco benpicco added Area: sys Area: System Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation labels Sep 9, 2020
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 10, 2020
@fjmolinas
Copy link
Contributor

This does fix the issue for me:

iotlab-m3 1:

2020-09-11 11:41:02,622 # ifconfig
2020-09-11 11:41:02,622 # Iface  6  HWaddr: 12:6B  Channel: 26  Page: 0  NID: 0x23  PHY: O-QPSK
2020-09-11 11:41:02,622 #
2020-09-11 11:41:02,623 #           Long HWaddr: B6:25:28:56:11:FF:12:6B
2020-09-11 11:41:02,634 #            TX-Power: 0dBm  State: IDLE  max. Retrans.: 3  CSMA Retries: 4
2020-09-11 11:41:02,637 #           AUTOACK  ACK_REQ  CSMA  L2-PDU:102  MTU:1280  HL:64  RTR
2020-09-11 11:41:02,638 #           6LO  IPHC
2020-09-11 11:41:02,639 #           Source address length: 8
2020-09-11 11:41:02,641 #           Link type: wireless
2020-09-11 11:41:02,643 #           inet6 addr: fe80::b425:2856:11ff:126b  scope: link  VAL
2020-09-11 11:41:02,644 #           inet6 group: ff02::2
2020-09-11 11:41:02,645 #           inet6 group: ff02::1
2020-09-11 11:41:02,647 #           inet6 group: ff02::1:ffff:126b
2020-09-11 11:41:02,647 #
2020-09-11 11:41:02,648 #           Statistics for Layer 2
2020-09-11 11:41:02,648 #             RX packets 0  bytes 0
2020-09-11 11:41:02,649 #             TX packets 2 (Multicast: 2)  bytes 86
2020-09-11 11:41:02,650 #             TX succeeded 2 errors 0
2020-09-11 11:41:02,650 #           Statistics for IPv6
2020-09-11 11:41:02,650 #             RX packets 0  bytes 0
2020-09-11 11:41:02,651 #             TX packets 2 (Multicast: 2)  bytes 128
2020-09-11 11:41:02,651 #             TX succeeded 2 errors 0
2020-09-11 11:41:02,651 #14030 
  • iotlab-m3 2
2020-09-11 11:40:53,043 # ifconfig
2020-09-11 11:40:53,044 # Iface  6  HWaddr: 5E:11  Channel: 26  Page: 0  NID: 0x23  PHY: O-QPSK
2020-09-11 11:40:53,044 #
2020-09-11 11:40:53,046 #           Long HWaddr: 66:69:37:AB:15:10:DE:11
2020-09-11 11:40:53,047 #            TX-Power: 0dBm  State: IDLE  max. Retrans.: 3  CSMA Retries: 4
2020-09-11 11:40:53,047 #           AUTOACK  ACK_REQ  CSMA  L2-PDU:102  MTU:1280  HL:64  RTR
2020-09-11 11:40:53,047 #           6LO  IPHC
2020-09-11 11:40:53,048 #           Source address length: 8
2020-09-11 11:40:53,059 #           Link type: wireless
2020-09-11 11:40:53,060 #           inet6 addr: fe80::6469:37ab:1510:de11  scope: link  VAL
2020-09-11 11:40:53,061 #           inet6 group: ff02::2
2020-09-11 11:40:53,061 #           inet6 group: ff02::1
2020-09-11 11:40:53,062 #           inet6 group: ff02::1:ff10:de11
2020-09-11 11:40:53,062 #
2020-09-11 11:40:53,063 #           Statistics for Layer 2
2020-09-11 11:40:53,063 #             RX packets 0  bytes 0
2020-09-11 11:40:53,064 #             TX packets 4 (Multicast: 4)  bytes 172
2020-09-11 11:40:53,065 #             TX succeeded 4 errors 0
2020-09-11 11:40:53,065 #           Statistics for IPv6
2020-09-11 11:40:53,066 #             RX packets 0  bytes 0
2020-09-11 11:40:53,067 #             TX packets 4 (Multicast: 4)  bytes 256
2020-09-11 11:40:53,068 #             TX succeeded 4 errors 0
2020-09-11 11:40:53,068 #
> ping6  fe80::b425:2856:11ff:126b
2020-09-11 11:41:52,303 #  ping6  fe80::b425:2856:11ff:126b
2020-09-11 11:41:52,320 # 12 bytes from fe80::b425:2856:11ff:126b%6: icmp_seq=0 ttl=64 rssi=-43 dBm time=9.165 ms
2020-09-11 11:41:53,311 # 12 bytes from fe80::b425:2856:11ff:126b%6: icmp_seq=1 ttl=64 rssi=-43 dBm time=10.125 ms
2020-09-11 11:41:54,320 # 12 bytes from fe80::b425:2856:11ff:126b%6: icmp_seq=2 ttl=64 rssi=-44 dBm time=9.806 ms
2020-09-11 11:41:54,320 #
2020-09-11 11:41:54,321 # --- fe80::b425:2856:11ff:126b PING statistics ---
2020-09-11 11:41:54,323 # 3 packets transmitted, 3 packets received, 0% packet loss
2020-09-11 11:41:54,324 # round-trip min/avg/max = 9.165/9.698/10.125 ms

Comment on lines +44 to +64
uint8_t tmp, j, k;
j = i % len;
k = i % CPUID_LEN;

/* xor with CPU ID */
out[j] ^= cid[k];

/* get 'random' position */
sum += out[j];
k = sum % (j + 1);

/* shuffle bytes */
tmp = out[j];
out[j] = out[k];
out[k] = tmp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a common shuffling alorithm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's basically Fisher–Yates shuffle

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice, mind adding a reference? You can squash right away!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment.

The CPU ID can have large parts that are identical between devices.
To add some more entropy to the generated IDs, shuffle the bytes.
Copy link
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants