libcoap 4.3.0
coap_prng.h
Go to the documentation of this file.
1/*
2 * coap_prng.h -- Pseudo Random Numbers
3 *
4 * Copyright (C) 2010-2020 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
26#if defined(WITH_CONTIKI)
27#include <string.h>
28
35contiki_prng_impl(unsigned char *buf, size_t len) {
36 uint16_t v = random_rand();
37 while (len > sizeof(v)) {
38 memcpy(buf, &v, sizeof(v));
39 len -= sizeof(v);
40 buf += sizeof(v);
41 v = random_rand();
42 }
43
44 memcpy(buf, &v, len);
45 return 1;
46}
47
48#define coap_prng(Buf,Length) contiki_prng_impl((Buf), (Length))
49#define coap_prng_init(Value) random_init((uint16_t)(Value))
50
51#elif defined(WITH_LWIP) && defined(LWIP_RAND)
52
54lwip_prng_impl(unsigned char *buf, size_t len) {
55 u32_t v = LWIP_RAND();
56 while (len > sizeof(v)) {
57 memcpy(buf, &v, sizeof(v));
58 len -= sizeof(v);
59 buf += sizeof(v);
60 v = LWIP_RAND();
61 }
62
63 memcpy(buf, &v, len);
64 return 1;
65}
66
67#define coap_prng(Buf,Length) lwip_prng_impl((Buf), (Length))
68#define coap_prng_init(Value) (void)Value
69
70#else
71
77typedef int (*coap_rand_func_t)(void *out, size_t len);
78
86
94void coap_prng_init(unsigned int seed);
95
107int coap_prng(void *buf, size_t len);
108
109#endif /* POSIX */
110
113#endif /* COAP_PRNG_H_ */
int(* coap_rand_func_t)(void *out, size_t len)
Data type for random number generator function.
Definition: coap_prng.h:77
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
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
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
Definition: coap_prng.c:76
#define COAP_STATIC_INLINE
Definition: libcoap.h:40