This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
prng.h
89 lines (76 loc) · 1.85 KB
/
prng.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* prng.h -- Pseudo Random Numbers
*
* Copyright (C) 2010--2012 Olaf Bergmann <[email protected]>
*
* This file is part of the library tinydtls. Please see
* README for terms of use.
*/
/**
* @file prng.h
* @brief Pseudo Random Numbers
*/
#ifndef _DTLS_PRNG_H_
#define _DTLS_PRNG_H_
#include "config.h"
/**
* @defgroup prng Pseudo Random Numbers
* @{
*/
#ifndef WITH_CONTIKI
#include <stdlib.h>
/**
* Fills \p buf with \p len random bytes. This is the default
* implementation for prng(). You might want to change prng() to use
* a better PRNG on your specific platform.
*/
static inline int
dtls_prng_impl(unsigned char *buf, size_t len) {
while (len--)
*buf++ = rand() & 0xFF;
return 1;
}
#else /* WITH_CONTIKI */
#include <string.h>
#ifdef HAVE_PRNG
extern int contiki_prng_impl(unsigned char *buf, size_t len);
#else
/**
* Fills \p buf with \p len random bytes. This is the default
* implementation for prng(). You might want to change prng() to use
* a better PRNG on your specific platform.
*/
static inline int
contiki_prng_impl(unsigned char *buf, size_t len) {
unsigned short v = random_rand();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = random_rand();
}
memcpy(buf, &v, len);
return 1;
}
#endif /* HAVE_PRNG */
#define prng(Buf,Length) contiki_prng_impl((Buf), (Length))
#define prng_init(Value) random_init((unsigned short)(Value))
#endif /* WITH_CONTIKI */
#ifndef prng
/**
* Fills \p Buf with \p Length bytes of random data.
*
* @hideinitializer
*/
#define prng(Buf,Length) dtls_prng_impl((Buf), (Length))
#endif
#ifndef prng_init
/**
* Called to set the PRNG seed. You may want to re-define this to
* allow for a better PRNG.
*
* @hideinitializer
*/
#define prng_init(Value) srand((unsigned long)(Value))
#endif
/** @} */
#endif /* _DTLS_PRNG_H_ */