mirror of
https://github.com/ntop/n2n.git
synced 2024-09-19 16:41:11 +02:00
Merge pull request #396 from Logan007/revRnd
cosmetics on random numbers code
This commit is contained in:
commit
22d48a3db8
|
@ -83,14 +83,12 @@
|
|||
#define N2N_CAN_NAME_IFACE 1
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <net/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
#define GRND_NONBLOCK 1
|
||||
#endif /* #ifdef __linux__ */
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
|
@ -100,10 +98,6 @@
|
|||
#include <syslog.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#if defined (__RDRND__) || defined (__RDSEED__)
|
||||
#include <immintrin.h>
|
||||
#endif
|
||||
|
||||
#define ETH_ADDR_LEN 6
|
||||
|
||||
struct ether_hdr
|
||||
|
@ -130,12 +124,12 @@ typedef struct ether_hdr ether_hdr_t;
|
|||
#include <assert.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef N2N_HAVE_AES
|
||||
#include <openssl/opensslv.h>
|
||||
#include <openssl/crypto.h>
|
||||
#endif
|
||||
|
||||
|
||||
#define closesocket(a) close(a)
|
||||
#endif /* #ifndef WIN32 */
|
||||
|
||||
|
|
|
@ -51,9 +51,6 @@
|
|||
#define TIME_STAMP_JITTER 0x0000000027100000LL /* we allow a packet to arrive 160 ms (== 0x27100 us) before another
|
||||
* set to 0x0000000000000000LL if increasing (or equal) time stamps allowed only */
|
||||
|
||||
/* parameter for random number generation */
|
||||
#define RND_RETRIES 1000 /* syscall and inquiring random number from hardware generators might fail, so we will retry */
|
||||
|
||||
/* N2N compression indicators. */
|
||||
/* Compression is disabled by default for outgoing packets if no cli
|
||||
* option is given. All edges are built with decompression support so
|
||||
|
|
|
@ -16,19 +16,46 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/* The WIN32 code is still untested and thus commented
|
||||
|
||||
#ifndef RND_H
|
||||
#define RND_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h> // time, clock
|
||||
|
||||
#include "n2n.h" // traceEvent
|
||||
|
||||
// syscall and inquiring random number from hardware generators might fail, so we will retry
|
||||
#define RND_RETRIES 1000
|
||||
|
||||
#if defined (__linux__)
|
||||
#include <sys/syscall.h> // syscall, SYS_getrandom
|
||||
#ifdef SYS_getrandom
|
||||
#define GRND_NONBLOCK 1
|
||||
#include <errno.h> // errno, EAGAIN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined (__RDRND__) || defined (__RDSEED__)
|
||||
#include <immintrin.h> // _rdrand64_step, rdseed4_step
|
||||
#endif
|
||||
|
||||
/* The WIN32 code is still untested and thus commented, also see random_numbers.c
|
||||
#if defined (WIN32)
|
||||
#include <Wincrypt.h>
|
||||
#endif
|
||||
#include <Wincrypt.h> // HCTYPTPROV, Crypt*-functions
|
||||
#endif
|
||||
*/
|
||||
|
||||
struct rn_generator_state_t {
|
||||
uint64_t a, b;
|
||||
};
|
||||
|
||||
struct splitmix64_state_t {
|
||||
typedef struct rn_generator_state_t {
|
||||
uint64_t a, b;
|
||||
} rn_generator_state_t;
|
||||
|
||||
typedef struct splitmix64_state_t {
|
||||
uint64_t s;
|
||||
};
|
||||
} splitmix64_state_t;
|
||||
|
||||
|
||||
int n2n_srand (uint64_t seed);
|
||||
|
@ -36,3 +63,6 @@ int n2n_srand (uint64_t seed);
|
|||
uint64_t n2n_rand ();
|
||||
|
||||
uint64_t n2n_seed ();
|
||||
|
||||
|
||||
#endif // RND_H
|
||||
|
|
|
@ -16,11 +16,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#ifdef SYS_getrandom
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#include "n2n.h"
|
||||
#include "random_numbers.h"
|
||||
|
||||
|
||||
/* The following code offers an alterate pseudo random number generator
|
||||
|
@ -31,13 +28,13 @@
|
|||
|
||||
/* The state must be seeded in a way that it is not all zero, choose some
|
||||
arbitrary defaults (in this case: taken from splitmix64) */
|
||||
static struct rn_generator_state_t rn_current_state = {
|
||||
static rn_generator_state_t rn_current_state = {
|
||||
.a = 0x9E3779B97F4A7C15,
|
||||
.b = 0xBF58476D1CE4E5B9 };
|
||||
|
||||
|
||||
/* used for mixing the initializing seed */
|
||||
static uint64_t splitmix64 (struct splitmix64_state_t *state) {
|
||||
static uint64_t splitmix64 (splitmix64_state_t *state) {
|
||||
|
||||
uint64_t result = state->s;
|
||||
|
||||
|
@ -51,8 +48,9 @@ static uint64_t splitmix64 (struct splitmix64_state_t *state) {
|
|||
|
||||
|
||||
int n2n_srand (uint64_t seed) {
|
||||
|
||||
uint8_t i;
|
||||
struct splitmix64_state_t smstate = {seed};
|
||||
splitmix64_state_t smstate = {seed};
|
||||
|
||||
rn_current_state.a = 0;
|
||||
rn_current_state.b = 0;
|
||||
|
@ -67,7 +65,7 @@ int n2n_srand (uint64_t seed) {
|
|||
rn_current_state.b = 0xBF58476D1CE4E5B9;
|
||||
}
|
||||
|
||||
/* stabilize in unlikely case of weak state with only a few bits set */
|
||||
// stabilize in unlikely case of weak state with only a few bits set
|
||||
for(i = 0; i < 32; i++)
|
||||
n2n_rand();
|
||||
|
||||
|
@ -160,16 +158,16 @@ uint64_t n2n_seed (void) {
|
|||
#ifdef WIN32
|
||||
HCRYPTPROV crypto_provider;
|
||||
CryptAcquireContext (&crypto_provider, NULL, (LPCWSTR)L"Microsoft Base Cryptographic Provider v1.0",
|
||||
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||||
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||||
CryptGenRandom (crypto_provider, 8, &seed);
|
||||
CryptReleaseContext (crypto_provider, 0);
|
||||
ret += seed;
|
||||
#endif */
|
||||
|
||||
seed = time(NULL); /* UTC in seconds */
|
||||
seed = time(NULL); // UTC in seconds
|
||||
ret += seed;
|
||||
|
||||
seed = clock() * 18444244737; /* clock() = ticks since program start */
|
||||
seed = clock() * 18444244737; // clock() = ticks since program start
|
||||
ret += seed;
|
||||
|
||||
return ret;
|
||||
|
|
Loading…
Reference in New Issue
Block a user