coap_prng.h
Go to the documentation of this file.
/*
* coap_prng.h -- Pseudo Random Numbers
*
* Copyright (C) 2010-2020 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_PRNG_H_
#define COAP_PRNG_H_
#if defined(WITH_CONTIKI)
#include <string.h>
COAP_STATIC_INLINE int
contiki_prng_impl(unsigned char *buf, size_t len) {
uint16_t v = random_rand();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = random_rand();
}
memcpy(buf, &v, len);
return 1;
}
#define coap_prng(Buf,Length) contiki_prng_impl((Buf), (Length))
#define coap_prng_init(Value) random_init((uint16_t)(Value))
#elif defined(WITH_LWIP) && defined(LWIP_RAND)
COAP_STATIC_INLINE int
lwip_prng_impl(unsigned char *buf, size_t len) {
u32_t v = LWIP_RAND();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = LWIP_RAND();
}
memcpy(buf, &v, len);
return 1;
}
#define coap_prng(Buf,Length) lwip_prng_impl((Buf), (Length))
#define coap_prng_init(Value) (void)Value
#else
typedef int (*coap_rand_func_t)(void *out, size_t len);
void coap_set_prng(coap_rand_func_t rng);
void coap_prng_init(unsigned int seed);
int coap_prng(void *buf, size_t len);
#endif /* POSIX */
#endif /* COAP_PRNG_H_ */
coap_rand_func_t
int(* coap_rand_func_t)(void *out, size_t len)
Data type for random number generator function.
Definition: coap_prng.h:77
coap_set_prng
void coap_set_prng(coap_rand_func_t rng)
Replaces the current random number generation function with the default function rng.
Definition: coap_prng.c:71
coap_prng
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition: coap_prng.c:87
coap_prng_init
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
Definition: coap_prng.c:76
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:40