Remove unused pkg dependancy
[platform/upstream/iotivity.git] / extlibs / tinydtls / prng.h
1 /* prng.h -- Pseudo Random Numbers
2  *
3  * Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the library tinydtls. Please see
6  * README for terms of use. 
7  */
8
9 /** 
10  * @file prng.h
11  * @brief Pseudo Random Numbers
12  */
13
14 #ifndef _DTLS_PRNG_H_
15 #define _DTLS_PRNG_H_
16
17 #include "tinydtls.h"
18
19 /** 
20  * @defgroup prng Pseudo Random Numbers
21  * @{
22  */
23
24 #ifndef WITH_CONTIKI
25 #include <stdlib.h>
26
27 /**
28  * Fills \p buf with \p len random bytes. This is the default
29  * implementation for prng().  You might want to change prng() to use
30  * a better PRNG on your specific platform.
31  */
32 INLINE_API int
33 dtls_prng(unsigned char *buf, size_t len) {
34   while (len--)
35     *buf++ = rand() & 0xFF;
36   return 1;
37 }
38
39 INLINE_API void
40 dtls_prng_init(unsigned short seed) {
41         srand(seed);
42 }
43 #else /* WITH_CONTIKI */
44 #include <string.h>
45 #include "random.h"
46
47 #ifdef HAVE_PRNG
48 INLINE_API int
49 dtls_prng(unsigned char *buf, size_t len)
50 {
51         return contiki_prng_impl(buf, len);
52 }
53 #else
54 /**
55  * Fills \p buf with \p len random bytes. This is the default
56  * implementation for prng().  You might want to change prng() to use
57  * a better PRNG on your specific platform.
58  */
59 INLINE_API int
60 dtls_prng(unsigned char *buf, size_t len) {
61   unsigned short v = random_rand();
62   while (len > sizeof(v)) {
63     memcpy(buf, &v, sizeof(v));
64     len -= sizeof(v);
65     buf += sizeof(v);
66     v = random_rand();
67   }
68
69   memcpy(buf, &v, len);
70   return 1;
71 }
72 #endif /* HAVE_PRNG */
73
74 INLINE_API void
75 dtls_prng_init(unsigned short seed) {
76         random_init(seed);
77 }
78 #endif /* WITH_CONTIKI */
79
80 /** @} */
81
82 #endif /* _DTLS_PRNG_H_ */