dsprenkels / randombytes

A portable C library for generating cypto-secure random bytes
MIT License
96 stars 37 forks source link

Compilation fails when using musl #11

Closed dsprenkels closed 6 years ago

dsprenkels commented 6 years ago

@jaromil gets compilation errors when compiling with the musl libc:

randombytes.c: In function ‘randombytes_linux_get_entropy_avail’:
randombytes.c:134:12: error: ‘RNDGETENTCNT’ undeclared (first use in this function)
  ioctl(fd, RNDGETENTCNT, &ret);
            ^
randombytes.c:134:12: note: each undeclared identifier is reported only once for each function it appears in

Note that compilation also fails on getrandom, because linux/random.h cannot be found (header file redundant, because of sys/syscall.h?).

dsprenkels commented 6 years ago

Libsodium does not seem to actually check the entropy level of the pool. Instead, they block on /dev/random once and assume that after the kernel passes back control, the pool is sufficiently filled.

I am personally not sure about the entropy levels in this case (did not find any documentation), so to me it seems that we have two options:

  1. Create a fallback to RNDGETENTCNT, which reads from /proc/sys/kernel/random/entropy_avail.
  2. Do not use RNDGETENTCNT at all, but only read from /proc/sys/kernel/random/entropy_avail.
  3. Block on /dev/random once (using a global value to make sure we don't block again).
  4. Block on /dev/random on every call to randombytes.

~I would personally opt for 1., with a fallback that if /proc/sys/kernel/random/entropy_avail cannot be read, we will block on /dev/random (every time).~ This will not fix the include error. Linux header files are still needed in this case as build dependency. Also, including linux headers directly seems to be frowned upon.

jaromil commented 6 years ago

You may not like what you see :^) but this is how I solved it (for now?) https://github.com/DECODEproject/zenroom/blob/develop/src/randombytes.c this builds correctly on all zenroom targets (musl,osx,win,arm-gcc,arm-musl,emscripten)

dsprenkels commented 6 years ago

Yeah, bypassing the entropy check is not an option. :P

dsprenkels commented 6 years ago
  1. Define RNDGETENTCNT inline.
jaromil commented 6 years ago

my favorites are 1. and 2. - thanks for taking care of details :^)

dsprenkels commented 6 years ago

Is fixed by #12. Used option 5.