20#include <sys/random.h>
21#elif defined(WITH_CONTIKI)
22#include "lib/csprng.h"
27#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
28#include <entropy_poll.h>
33errno_t __cdecl rand_s(_Out_
unsigned int *_RandomValue);
40coap_prng_impl(
unsigned char *buf,
size_t len) {
47 for (i = 0; i < len && i < 4; i++) {
64#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
66 return (mbedtls_hardware_poll(NULL, buf, len, NULL) ? 0 : 1);
68#elif defined(HAVE_GETRANDOM)
69 return (getrandom(buf, len, 0) > 0) ? 1 : 0;
71#elif defined(HAVE_RANDOM)
72#define RAND_BYTES (RAND_MAX >= 0xffffff ? 3 : (RAND_MAX >= 0xffff ? 2 : 1))
73 unsigned char *dst = (
unsigned char *)buf;
76 uint8_t byte_counter = RAND_BYTES;
77 uint32_t r_v = random();
88 byte_counter = RAND_BYTES;
93#elif defined(RIOT_VERSION)
95 random_bytes(buf, len);
98#elif defined(WITH_CONTIKI)
99 return csprng_rand(buf, len);
102 return coap_prng_impl(buf,len);
106#error "CVE-2021-34430: using rand() for crypto randoms is not secure!"
107#error "Please update you C-library and rerun the auto-configuration."
108 unsigned char *dst = (
unsigned char *)buf;
110 *dst++ = rand() & 0xFF;
118#if defined(WITH_LWIP) && defined(LWIP_RAND)
132#elif defined(HAVE_RANDOM)
Pulls together all the internal only header files.
static int coap_prng_default(void *buf, size_t len)
static coap_rand_func_t rand_func
int(* coap_rand_func_t)(void *out, size_t len)
Data type for random number generator function.
void coap_set_prng(coap_rand_func_t rng)
Replaces the current random number generation function with the default function rng.
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
#define COAP_STATIC_INLINE