libcoap 4.3.1
coap_prng.c
Go to the documentation of this file.
1/*
2 * coap_prng.c -- random number generation
3 *
4 * Copyright (C) 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
9 * for terms of use.
10 */
11
17#include "coap3/coap_internal.h"
18
19#ifdef HAVE_GETRANDOM
20#include <sys/random.h>
21#else /* !HAVE_GETRANDOM */
22#include <stdlib.h>
23#endif /* !HAVE_GETRANDOM */
24
25#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
26#include <entropy_poll.h>
27#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
28
29#if defined(_WIN32)
30
31errno_t __cdecl rand_s( _Out_ unsigned int* _RandomValue );
38coap_prng_impl( unsigned char *buf, size_t len ) {
39 while ( len != 0 ) {
40 uint32_t r = 0;
41 size_t i;
42 if ( rand_s( &r ) != 0 )
43 return 0;
44 for ( i = 0; i < len && i < 4; i++ ) {
45 *buf++ = (uint8_t)r;
46 r >>= 8;
47 }
48 len -= i;
49 }
50 return 1;
51}
52
53#endif /* _WIN32 */
54
55/*
56 * This, or any user provided alternative, function is expected to
57 * return 0 on failure and 1 on success.
58 */
59static int
60coap_prng_default(void *buf, size_t len) {
61#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
62 /* mbedtls_hardware_poll() returns 0 on success */
63 return (mbedtls_hardware_poll(NULL, buf, len, NULL) ? 0 : 1);
64#else /* !MBEDTLS_ENTROPY_HARDWARE_ALT */
65#ifdef HAVE_GETRANDOM
66 return (getrandom(buf, len, 0) > 0) ? 1 : 0;
67#else /* !HAVE_GETRANDOM */
68#if defined(_WIN32)
69 return coap_prng_impl(buf,len);
70#else /* !_WIN32 */
71 unsigned char *dst = (unsigned char *)buf;
72 while (len--)
73 *dst++ = rand() & 0xFF;
74 return 1;
75#endif /* !_WIN32 */
76#endif /* !HAVE_GETRANDOM */
77#endif /* !MBEDTLS_ENTROPY_HARDWARE_ALT */
78}
79
81
82#if defined(WITH_CONTIKI)
83
84#elif defined(WITH_LWIP) && defined(LWIP_RAND)
85
86#else
87
88void
90 rand_func = rng;
91}
92
93void
94coap_prng_init(unsigned int seed) {
95#ifdef HAVE_GETRANDOM
96 /* No seed to seed the random source if getrandom() is used,
97 * see dtls_prng(). */
98 (void)seed;
99#else /* !HAVE_GETRANDOM */
100 srand(seed);
101#endif /* !HAVE_GETRANDOM */
102}
103
104int
105coap_prng(void *buf, size_t len) {
106 if (!rand_func) {
107 return 0;
108 }
109
110 return rand_func(buf, len);
111}
112
113#endif
Pulls together all the internal only header files.
static int coap_prng_default(void *buf, size_t len)
Definition: coap_prng.c:60
static coap_rand_func_t rand_func
Definition: coap_prng.c:80
int(* coap_rand_func_t)(void *out, size_t len)
Data type for random number generator function.
Definition: coap_prng.h:78
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:89
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:105
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
Definition: coap_prng.c:94
#define COAP_STATIC_INLINE
Definition: libcoap.h:45