dsprenkels / randombytes

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

Adds support for GNU/Hurd and GNU/kFreeBSD #39

Closed bfabiszewski closed 2 years ago

bfabiszewski commented 2 years ago

I noticed that the library fails on two niche GNU flavors: GNU/Hurd and GNU/kFreeBSD. This PR is proposed fix. I tested that it works on both systems. In case you want to handle this some other way, these are my observations.

Fixing GNU/Hurd just requires that it is treated as linux. It is detected by __GNU__ macro. The only tricky point is that hurd defines BSD macro in its sys/param.h header. So I unset it. As it has glibc it finally uses getrandom function call.

GNU/kFreeBSD uses BSD kernel but does not have arc4random like other BSD family members. It does not have SYS_getrandom system call either. It has getrandom function but it is just unusable stub (linker spits: "warning: getrandom is not implemented and will always fail"). The only option left is /dev/urandom but we have to skip linux-specific part. I skipped whole /dev/random polling and entropy related stuff as this is basically what libsodium does for systems other than linux.

BTW. Pragma message is treated as warning by clang and it breaks -Werror builds. In my repo I wrapped it in a macro so that I can enable it only when I need to debug.

Thanks for the library!

dsprenkels commented 2 years ago

Thank you so much. All the code looks good to me. :)

/And good catch that I did not check the return value of fopen!

dsprenkels commented 2 years ago

I think we might have reached a point where we maybe should consider refactoring all the macro stuff.