From 235dfa7e14699079ab8d6d0af35b5c467a794405 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Tue, 6 Dec 2011 20:08:21 +0100 Subject: [PATCH] externalized prng --- net.h | 39 +-------------------------- prng.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 prng.h diff --git a/net.h b/net.h index e54329a5..2779405c 100644 --- a/net.h +++ b/net.h @@ -34,6 +34,7 @@ #include "option.h" #include "address.h" +#include "prng.h" #include "pdu.h" /** @@ -388,42 +389,4 @@ int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown); -/** - * @defgroup prng Pseudo Random Numbers - * @{ - */ - -/** - * 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 -coap_prng_impl(unsigned char *buf, size_t len) { - while (len--) - *buf++ = rand() & 0xFF; - return 1; -} - -#ifndef prng -/** - * Fills \p Buf with \p Length bytes of random data. - * - * @hideinitializer - */ -#define prng(Buf,Length) coap_prng_impl((Buf), (Length)) -#endif - -#ifndef prng_init -/** - * Called by dtls_new_context() 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 /* _COAP_NET_H_ */ diff --git a/prng.h b/prng.h new file mode 100644 index 00000000..e86cdc59 --- /dev/null +++ b/prng.h @@ -0,0 +1,84 @@ +/* prng.h -- Pseudo Random Numbers + * + * Copyright (C) 2010,2011 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. + */ + +/** + * @file prng.h + * @brief Pseudo Random Numbers + */ + +#ifndef _COAP_PRNG_H_ +#define _COAP_PRNG_H_ + +#include "config.h" + +/** + * @defgroup prng Pseudo Random Numbers + * @{ + */ + +#ifndef WITH_CONTIKI +#include + +/** + * 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 +coap_prng_impl(unsigned char *buf, size_t len) { + while (len--) + *buf++ = rand() & 0xFF; + return 1; +} +#else /* WITH_CONTIKI */ +#include + +/** + * 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); + } + + memcpy(buf, &v, len); + return 1; +} + +#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) coap_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 /* _COAP_PRNG_H_ */