tinydtls  0.8.6
prng.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  *
8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
9  * and the Eclipse Distribution License is available at
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  *
12  * Contributors:
13  * Olaf Bergmann - initial API and implementation
14  * Hauke Mehrtens - memory optimization, ECC integration
15  *
16  *******************************************************************************/
17 
23 #ifndef _DTLS_PRNG_H_
24 #define _DTLS_PRNG_H_
25 
26 #include "tinydtls.h"
27 
33 #ifndef WITH_CONTIKI
34 #include <stdlib.h>
35 
41 static inline int
42 dtls_prng(unsigned char *buf, size_t len) {
43  while (len--)
44  *buf++ = rand() & 0xFF;
45  return 1;
46 }
47 
48 static inline void
49 dtls_prng_init(unsigned short seed) {
50  srand(seed);
51 }
52 #else /* WITH_CONTIKI */
53 #include <string.h>
54 #include "random.h"
55 
56 #ifdef HAVE_PRNG
57 static inline int
58 dtls_prng(unsigned char *buf, size_t len)
59 {
60  return contiki_prng_impl(buf, len);
61 }
62 #else
63 
68 static inline int
69 dtls_prng(unsigned char *buf, size_t len) {
70  unsigned short v = random_rand();
71  while (len > sizeof(v)) {
72  memcpy(buf, &v, sizeof(v));
73  len -= sizeof(v);
74  buf += sizeof(v);
75  v = random_rand();
76  }
77 
78  memcpy(buf, &v, len);
79  return 1;
80 }
81 #endif /* HAVE_PRNG */
82 
83 static inline void
84 dtls_prng_init(unsigned short seed) {
85  /* random_init() messes with the radio interface of the CC2538 and
86  * therefore must not be called after the radio has been
87  * initialized. */
88 #ifndef CONTIKI_TARGET_CC2538DK
89  random_init(seed);
90 #endif
91 }
92 #endif /* WITH_CONTIKI */
93 
96 #endif /* _DTLS_PRNG_H_ */
public tinydtls API
static void dtls_prng_init(unsigned short seed)
Definition: prng.h:49
static int dtls_prng(unsigned char *buf, size_t len)
Definition: prng.h:42