cosmetics on random numbers code

This commit is contained in:
Logan007 2020-08-30 03:05:48 +05:45
parent b3d4f21c91
commit 8d7680408f
3 changed files with 48 additions and 26 deletions

View File

@ -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 */

View File

@ -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
#ifdef SYS_getrandom
#define GRND_NONBLOCK 1
#include <errno.h> // errno
#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>
#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

View File

@ -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();
@ -166,10 +164,10 @@ uint64_t n2n_seed (void) {
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;