mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys/luid: shuffle bytes
The CPU ID can have large parts that are identical between devices. To add some more entropy to the generated IDs, shuffle the bytes.
This commit is contained in:
parent
df913bf9bf
commit
28d1936fae
@ -35,12 +35,33 @@ void __attribute__((weak)) luid_base(void *buf, size_t len)
|
||||
memset(buf, LUID_BACKUP_SEED, len);
|
||||
|
||||
#if CPUID_LEN
|
||||
uint8_t *out = (uint8_t *)buf;
|
||||
uint8_t *out = buf;
|
||||
uint8_t cid[CPUID_LEN];
|
||||
uint8_t sum = 0;
|
||||
|
||||
cpuid_get(cid);
|
||||
|
||||
/* CPU ID is XORed with LUID_BACKUP_SEED.
|
||||
* Then Fisher–Yates shuffle is performed to mix
|
||||
* the bytes, as often large continuous chunks of
|
||||
* CPU ID are equal on machines with the same CPU.
|
||||
*/
|
||||
for (size_t i = 0; i < MAX(len, CPUID_LEN); i++) {
|
||||
out[i % len] ^= cid[i % CPUID_LEN];
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user