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++) {
75#if defined(WITH_LWIP) && defined(LWIP_RAND)
84 unsigned char *buf = (
unsigned char *)bufp;
85 u32_t v = LWIP_RAND();
87 while (len >
sizeof(v)) {
88 memcpy(buf, &v,
sizeof(v));
106#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
108 return (mbedtls_hardware_poll(NULL, buf, len, NULL) ? 0 : 1);
110#elif defined(HAVE_GETRANDOM)
111 return (getrandom(buf, len, 0) > 0) ? 1 : 0;
113#elif defined(HAVE_RANDOM)
114#define RAND_BYTES (RAND_MAX >= 0xffffff ? 3 : (RAND_MAX >= 0xffff ? 2 : 1))
115 unsigned char *dst = (
unsigned char *)buf;
118 uint8_t byte_counter = RAND_BYTES;
119 uint32_t r_v = random();
126 if (--byte_counter) {
130 byte_counter = RAND_BYTES;
135#elif defined(RIOT_VERSION)
137 random_bytes(buf, len);
140#elif defined(WITH_CONTIKI)
141 return csprng_rand(buf, len);
144 return coap_prng_impl(buf,len);
148#error "CVE-2021-34430: using rand() for crypto randoms is not secure!"
149#error "Please update you C-library and rerun the auto-configuration."
150 unsigned char *dst = (
unsigned char *)buf;
152 *dst++ = rand() & 0xFF;
170#elif defined(HAVE_RANDOM)
Library specific build wrapper for coap_internal.h.
static int coap_prng_default(void *buf, size_t len)
static coap_rand_func_t rand_func
void coap_prng_init_lkd(unsigned int seed)
Seeds the default random number generation function with the given seed.
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
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.
COAP_API int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
COAP_API void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
#define coap_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define COAP_STATIC_INLINE