|
11 | 11 | #include <picomesh/picoclass/class.h> |
12 | 12 | #include <picomesh/picoclass/rpc.h> |
13 | 13 | #include <picomesh/picoclass/yheaders.h> |
| 14 | +#include <picomesh/platform/random.h> |
14 | 15 |
|
15 | | -#include <errno.h> |
16 | 16 | #include <stdio.h> |
17 | 17 | #include <stdlib.h> |
18 | 18 | #include <string.h> |
19 | 19 | #include <strings.h> |
20 | | -#include <sys/random.h> |
21 | 20 | #include <time.h> |
22 | 21 | #include <unistd.h> |
23 | 22 |
|
@@ -77,26 +76,13 @@ static int ytel_bytes_all_zero(const uint8_t *bytes, size_t n) { |
77 | 76 | } |
78 | 77 |
|
79 | 78 | static void ytel_fill_random(uint8_t *bytes, size_t n) { |
80 | | - /* Prefer the kernel CSPRNG; retry partial reads and EINTR. */ |
81 | | - size_t off = 0; |
82 | | - while (off < n) { |
83 | | - ssize_t got = getrandom(bytes + off, n - off, 0); |
84 | | - if (got > 0) { |
85 | | - off += (size_t)got; |
86 | | - continue; |
87 | | - } |
88 | | - if (got < 0 && errno == EINTR) |
89 | | - continue; |
90 | | - break; |
91 | | - } |
92 | | - if (off == n) |
| 79 | + /* Prefer the kernel CSPRNG. */ |
| 80 | + if (picomesh_platform_random_bytes(bytes, n) == 0) |
93 | 81 | return; |
94 | | - /* Fallback for the remainder: trace/span ids are correlation ids, not |
95 | | - * secrets, so a clock/pid-seeded PRNG is acceptable when getrandom is |
96 | | - * unavailable. */ |
97 | | - uint64_t seed = |
98 | | - ytel_mono_ns() ^ ((uint64_t)getpid() << 32) ^ (uint64_t)(off + 1); |
99 | | - for (size_t i = off; i < n; ++i) { |
| 82 | + /* Fallback: trace/span ids are correlation ids, not secrets, so a |
| 83 | + * clock/pid-seeded PRNG is acceptable when the CSPRNG is unavailable. */ |
| 84 | + uint64_t seed = ytel_mono_ns() ^ ((uint64_t)getpid() << 32) ^ 1u; |
| 85 | + for (size_t i = 0; i < n; ++i) { |
100 | 86 | seed = seed * 6364136223846793005ull + 1442695040888963407ull; |
101 | 87 | bytes[i] = (uint8_t)(seed >> 33); |
102 | 88 | } |
|
0 commit comments