hatlabs/SH-wg-firmware

Serial number not unique

Closed this issue · 2 comments

Using the first 4 bytes of the MAC address as a serial number has an issue.
These 4 bytes are largely identical across a batch of chips.
In a batch of 6 boards I had 3 with the same serial number.
I'd suggest using bytes 2,3,4,5 instead of bytes 0,1,2,3
Or find a way to reduce the 6 bytes to 4 bytes. Maybe XOR byte0 with 1 and 2 with 3

uint32_t GetBoardSerialNumber() {
uint8_t chipid[6];
esp_efuse_mac_get_default(chipid);
return chipid[0] + (chipid[1] << 8) + (chipid[2] << 16) + (chipid[3] << 24);

Good catch, thanks! My intention was to use the four last bytes. The first 3 bytes of MAC address are the Organisationally Unique Identifier. I don't know how many OUIs a big manufacturer gets, but definitely there's very little entropy in the current implementation. I quite like your XOR suggestion, but I'd take it a bit (pun intended) further: Calculate the checksum of the first three bytes and use it as the output first byte, and copy the last three bytes as is.

Actually, it gets more interesting than this. In HALMET's example codebase, I already use a variant that returns a 64-bit serial number. Obviously, that's the correct thing to do rather than throwing information away. And in SH-wg's context, there's no good reason to not use the full 48-bit MAC as the serial number.

I'll implement the change in the repo and in the factory image but I'll refrain from pushing an update because the issue is not relevant for most users and I hope to make a major update with SensESP v3 in a couple of months or so.

I'll let you double-check the PR if you want.