• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 /**
13  * @file coap_prng.h
14  * @brief Pseudo Random Numbers
15  */
16 
17 #ifndef COAP_PRNG_H_
18 #define COAP_PRNG_H_
19 
20 /**
21  * @defgroup coap_prng Pseudo Random Numbers
22  * API functions for gerating pseudo random numbers
23  * @{
24  */
25 
26 #if defined(WITH_CONTIKI)
27 #include <string.h>
28 
29 /**
30  * Fills \p buf with \p len random bytes. This is the default implementation for
31  * coap_prng(). You might want to change contiki_prng_impl() to use a better
32  * PRNG on your specific platform.
33  */
34 COAP_STATIC_INLINE int
contiki_prng_impl(unsigned char * buf,size_t len)35 contiki_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 
53 COAP_STATIC_INLINE int
lwip_prng_impl(unsigned char * buf,size_t len)54 lwip_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 
72 /**
73  * Data type for random number generator function. The function must
74  * fill @p len bytes of random data into the buffer starting at @p
75  * out.  On success, the function should return 1, zero otherwise.
76  */
77 typedef int (*coap_rand_func_t)(void *out, size_t len);
78 
79 /**
80  * Replaces the current random number generation function with the
81  * default function @p rng.
82  *
83  * @param rng  The random number generation function to use.
84  */
85 void coap_set_prng(coap_rand_func_t rng);
86 
87 /**
88  * Seeds the default random number generation function with the given
89  * @p seed. The default random number generation function will use
90  * getrandom() if available, ignoring the seed.
91  *
92  * @param seed  The seed for the pseudo random number generator.
93  */
94 void coap_prng_init(unsigned int seed);
95 
96 /**
97  * Fills @p buf with @p len random bytes using the default pseudo
98  * random number generator. The default PRNG can be changed with
99  * coap_set_prng(). This function returns 1 when @p len random bytes
100  * have been written to @p buf, zero otherwise.
101  *
102  * @param buf  The buffer to fill with random bytes.
103  * @param len  The number of random bytes to write into @p buf.
104  *
105  * @return 1 on success, 0 otherwise.
106  */
107 int coap_prng(void *buf, size_t len);
108 
109 #endif /* POSIX */
110 
111 /** @} */
112 
113 #endif /* COAP_PRNG_H_ */
114