sodium.h
is the only header that has to be included.sodium
(use -lsodium
to link it), and proper compilation/linker flags can be obtained using pkg-config
on systems where it is available:SODIUM_STATIC=1
and SODIUM_EXPORT=
. This is not required on other platforms.sodium_init()
initializes the library and should be called before any other function provided by Sodium. It is safe to call this function more than once and from different threads -- subsequent calls won't have any effects.sodium_init()
doesn't perform any memory allocations. However, on Unix systems, it may open /dev/urandom
and keep the descriptor open so that the device remains accessible after a chroot()
call.sodium_init()
do not cause additional descriptors to be opened.sodium_init()
returns 0
on success, -1
on failure, and 1
if the library had already been initialized.0
or a very low number (< 160
), and you are not running an obsolete kernel, this is very likely to be the case.virtio-rng
interface is available. If this is a cloud service and the hypervisor settings are out of your reach, consider switching to a different service.rng-tools
package:/proc/sys/kernel/random/entropy_avail
again. If the value didn't go any higher, install haveged
:aws_nitro_enclaves_library_seed_entropy()
function before sodium_init()
, and occasionally afterwards.RDSEED
CPU instruction to seed the kernel RNG (not recommended as a unique entropy source).random.trust_cpu=on
in the kernel command line (requires Linux kernel > 4.19).sodium_init()
using code similar to the following: