tinydtls  0.8.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
prng.h
Go to the documentation of this file.
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 
14 #ifndef _DTLS_PRNG_H_
15 #define _DTLS_PRNG_H_
16 
17 #include "tinydtls.h"
18 
24 #ifndef WITH_CONTIKI
25 #include <stdlib.h>
26 
32 static inline int
33 dtls_prng(unsigned char *buf, size_t len) {
34  while (len--)
35  *buf++ = rand() & 0xFF;
36  return 1;
37 }
38 
39 static inline 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 static inline int
49 dtls_prng(unsigned char *buf, size_t len)
50 {
51  return contiki_prng_impl(buf, len);
52 }
53 #else
54 
59 static inline 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 static inline void
75 dtls_prng_init(unsigned short seed) {
76  random_init(seed);
77 }
78 #endif /* WITH_CONTIKI */
79 
82 #endif /* _DTLS_PRNG_H_ */
public tinydtls API
static void dtls_prng_init(unsigned short seed)
Definition: prng.h:40
static int dtls_prng(unsigned char *buf, size_t len)
Definition: prng.h:33