libcoap 4.3.2rc2

coap_prng.h
Go to the documentation of this file.
1/*
2 * coap_prng.h -- Pseudo Random Numbers
3 *
4 * Copyright (C) 2010-2023 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see README for terms
9 * of use.
10 */
11
17#ifndef COAP_PRNG_H_
18#define COAP_PRNG_H_
19
27#if defined(WITH_LWIP) && defined(LWIP_RAND)
28
29 COAP_STATIC_INLINE int
30lwip_prng_impl(unsigned char *buf, size_t len) {
31 u32_t v = LWIP_RAND();
32 while (len > sizeof(v)) {
33 memcpy(buf, &v, sizeof(v));
34 len -= sizeof(v);
35 buf += sizeof(v);
36 v = LWIP_RAND();
37 }
38
39 memcpy(buf, &v, len);
40 return 1;
41}
42
43#define coap_prng(Buf,Length) lwip_prng_impl((Buf), (Length))
44#define coap_prng_init(Value) (void)Value
45
46#else
47
53 typedef int (*coap_rand_func_t)(void *out, size_t len);
54
61 void coap_set_prng(coap_rand_func_t rng);
62
70 void coap_prng_init(unsigned int seed);
71
83 int coap_prng(void *buf, size_t len);
84
85#endif /* POSIX */
86
89#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:53
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:123
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:140
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:128
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:53