libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
prng.h
Go to the documentation of this file.
1 /* prng.h -- Pseudo Random Numbers
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
14 #ifndef _COAP_PRNG_H_
15 #define _COAP_PRNG_H_
16 
17 #include "config.h"
18 
24 #ifndef WITH_CONTIKI
25 #include <stdlib.h>
26 
32 static inline int
33 coap_prng_impl(unsigned char *buf, size_t len) {
34  while (len--)
35  *buf++ = rand() & 0xFF;
36  return 1;
37 }
38 #else /* WITH_CONTIKI */
39 #include <string.h>
40 
46 static inline int
47 contiki_prng_impl(unsigned char *buf, size_t len) {
48  unsigned short v = random_rand();
49  while (len > sizeof(v)) {
50  memcpy(buf, &v, sizeof(v));
51  len -= sizeof(v);
52  buf += sizeof(v);
53  v = random_rand();
54  }
55 
56  memcpy(buf, &v, len);
57  return 1;
58 }
59 
60 #define prng(Buf,Length) contiki_prng_impl((Buf), (Length))
61 #define prng_init(Value) random_init((unsigned short)(Value))
62 #endif /* WITH_CONTIKI */
63 
64 #ifndef prng
65 
70 #define prng(Buf,Length) coap_prng_impl((Buf), (Length))
71 #endif
72 
73 #ifndef prng_init
74 
80 #define prng_init(Value) srand((unsigned long)(Value))
81 #endif
82 
85 #endif /* _COAP_PRNG_H_ */
static int coap_prng_impl(unsigned char *buf, size_t len)
Fills buf with len random bytes.
Definition: prng.h:33