Browse Source

Merge pull request #396 from Logan007/revRnd

cosmetics on random numbers code
pull/398/head
Luca Deri 4 years ago
committed by GitHub
parent
commit
22d48a3db8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      include/n2n.h
  2. 3
      include/n2n_define.h
  3. 42
      include/random_numbers.h
  4. 18
      src/random_numbers.c

8
include/n2n.h

@ -83,14 +83,12 @@
#define N2N_CAN_NAME_IFACE 1 #define N2N_CAN_NAME_IFACE 1
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
#include <net/if_arp.h> #include <net/if_arp.h>
#include <net/if.h> #include <net/if.h>
#include <linux/if_tun.h> #include <linux/if_tun.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#define GRND_NONBLOCK 1
#endif /* #ifdef __linux__ */ #endif /* #ifdef __linux__ */
#ifdef __FreeBSD__ #ifdef __FreeBSD__
@ -100,10 +98,6 @@
#include <syslog.h> #include <syslog.h>
#include <sys/wait.h> #include <sys/wait.h>
#if defined (__RDRND__) || defined (__RDSEED__)
#include <immintrin.h>
#endif
#define ETH_ADDR_LEN 6 #define ETH_ADDR_LEN 6
struct ether_hdr struct ether_hdr
@ -130,12 +124,12 @@ typedef struct ether_hdr ether_hdr_t;
#include <assert.h> #include <assert.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdint.h> #include <stdint.h>
#ifdef N2N_HAVE_AES #ifdef N2N_HAVE_AES
#include <openssl/opensslv.h> #include <openssl/opensslv.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#endif #endif
#define closesocket(a) close(a) #define closesocket(a) close(a)
#endif /* #ifndef WIN32 */ #endif /* #ifndef WIN32 */

3
include/n2n_define.h

@ -51,9 +51,6 @@
#define TIME_STAMP_JITTER 0x0000000027100000LL /* we allow a packet to arrive 160 ms (== 0x27100 us) before another #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 */ * 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. */ /* N2N compression indicators. */
/* Compression is disabled by default for outgoing packets if no cli /* Compression is disabled by default for outgoing packets if no cli
* option is given. All edges are built with decompression support so * option is given. All edges are built with decompression support so

42
include/random_numbers.h

@ -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) #if defined (WIN32)
#include <Wincrypt.h> #include <Wincrypt.h> // HCTYPTPROV, Crypt*-functions
#endif #endif
*/ */
struct rn_generator_state_t {
typedef struct rn_generator_state_t {
uint64_t a, b; uint64_t a, b;
}; } rn_generator_state_t;
struct splitmix64_state_t { typedef struct splitmix64_state_t {
uint64_t s; uint64_t s;
}; } splitmix64_state_t;
int n2n_srand (uint64_t seed); int n2n_srand (uint64_t seed);
@ -36,3 +63,6 @@ int n2n_srand (uint64_t seed);
uint64_t n2n_rand (); uint64_t n2n_rand ();
uint64_t n2n_seed (); uint64_t n2n_seed ();
#endif // RND_H

18
src/random_numbers.c

@ -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 /* 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 /* The state must be seeded in a way that it is not all zero, choose some
arbitrary defaults (in this case: taken from splitmix64) */ 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, .a = 0x9E3779B97F4A7C15,
.b = 0xBF58476D1CE4E5B9 }; .b = 0xBF58476D1CE4E5B9 };
/* used for mixing the initializing seed */ /* 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; uint64_t result = state->s;
@ -51,8 +48,9 @@ static uint64_t splitmix64 (struct splitmix64_state_t *state) {
int n2n_srand (uint64_t seed) { int n2n_srand (uint64_t seed) {
uint8_t i; uint8_t i;
struct splitmix64_state_t smstate = {seed}; splitmix64_state_t smstate = {seed};
rn_current_state.a = 0; rn_current_state.a = 0;
rn_current_state.b = 0; rn_current_state.b = 0;
@ -67,7 +65,7 @@ int n2n_srand (uint64_t seed) {
rn_current_state.b = 0xBF58476D1CE4E5B9; 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++) for(i = 0; i < 32; i++)
n2n_rand(); n2n_rand();
@ -166,10 +164,10 @@ uint64_t n2n_seed (void) {
ret += seed; ret += seed;
#endif */ #endif */
seed = time(NULL); /* UTC in seconds */ seed = time(NULL); // UTC in seconds
ret += seed; ret += seed;
seed = clock() * 18444244737; /* clock() = ticks since program start */ seed = clock() * 18444244737; // clock() = ticks since program start
ret += seed; ret += seed;
return ret; return ret;

Loading…
Cancel
Save