libcoap  4.3.0rc1
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  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
15 #ifndef COAP_PRNG_H_
16 #define COAP_PRNG_H_
17 
24 #if defined(WITH_CONTIKI)
25 #include <string.h>
26 
33 contiki_prng_impl(unsigned char *buf, size_t len) {
34  uint16_t v = random_rand();
35  while (len > sizeof(v)) {
36  memcpy(buf, &v, sizeof(v));
37  len -= sizeof(v);
38  buf += sizeof(v);
39  v = random_rand();
40  }
41 
42  memcpy(buf, &v, len);
43  return 1;
44 }
45 
46 #define coap_prng(Buf,Length) contiki_prng_impl((Buf), (Length))
47 #define coap_prng_init(Value) random_init((uint16_t)(Value))
48 
49 #elif defined(WITH_LWIP) && defined(LWIP_RAND)
50 
52 lwip_prng_impl(unsigned char *buf, size_t len) {
53  u32_t v = LWIP_RAND();
54  while (len > sizeof(v)) {
55  memcpy(buf, &v, sizeof(v));
56  len -= sizeof(v);
57  buf += sizeof(v);
58  v = LWIP_RAND();
59  }
60 
61  memcpy(buf, &v, len);
62  return 1;
63 }
64 
65 #define coap_prng(Buf,Length) lwip_prng_impl((Buf), (Length))
66 #define coap_prng_init(Value)
67 
68 #else
69 
75 typedef int (*coap_rand_func_t)(void *out, size_t len);
76 
84 
92 void coap_prng_init(unsigned int seed);
93 
105 int coap_prng(void *buf, size_t len);
106 
107 #endif /* POSIX */
108 
111 #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:75
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:69
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:85
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
Definition: coap_prng.c:74
#define COAP_STATIC_INLINE
Definition: libcoap.h:38