mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-23 15:41:04 +03:00
externalized prng
This commit is contained in:
1 parent
aba1cb8461
commit
235dfa7e14
2 files changed
+85
-38
No files matched your search
@@ -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_ */
|
||||
@@ -0,0 +1,84 @@
|
||||
/* prng.h -- Pseudo Random Numbers
|
||||
*
|
||||
* Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
|
||||
/**
|
||||
* 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 <string.h>
|
||||
|
||||
/**
|
||||
* 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_ */
|
||||
Reference in new issue
Block a user