mirror of https://github.com/ntop/n2n.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.8 KiB
134 lines
3.8 KiB
4 years ago
|
/**
|
||
|
* (C) 2007-20 - ntop.org and contributors
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not see see <http://www.gnu.org/licenses/>
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include "n2n.h"
|
||
|
|
||
4 years ago
|
/* The following code offers an alterate pseudo random number generator
|
||
|
namely XORSHIFT128+ to use instead of C's rand(). Its performance is
|
||
|
on par with C's rand().
|
||
4 years ago
|
*/
|
||
4 years ago
|
|
||
|
|
||
|
/* 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 = {
|
||
4 years ago
|
.a = 0x9E3779B97F4A7C15,
|
||
|
.b = 0xBF58476D1CE4E5B9 };
|
||
4 years ago
|
|
||
|
|
||
|
/* used for mixing the initializing seed */
|
||
|
static uint64_t splitmix64 (struct splitmix64_state_t *state) {
|
||
|
|
||
4 years ago
|
uint64_t result = state->s;
|
||
4 years ago
|
|
||
4 years ago
|
state->s = result + 0x9E3779B97F4A7C15;
|
||
4 years ago
|
|
||
4 years ago
|
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
|
||
|
result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
|
||
4 years ago
|
|
||
4 years ago
|
return result ^ (result >> 31);
|
||
4 years ago
|
}
|
||
|
|
||
|
|
||
|
int n2n_srand (uint64_t seed) {
|
||
4 years ago
|
uint8_t i;
|
||
|
struct splitmix64_state_t smstate = {seed};
|
||
4 years ago
|
|
||
4 years ago
|
rn_current_state.a = 0;
|
||
|
rn_current_state.b = 0;
|
||
4 years ago
|
|
||
4 years ago
|
rn_current_state.a = splitmix64 (&smstate);
|
||
|
rn_current_state.b = splitmix64 (&smstate);
|
||
4 years ago
|
|
||
4 years ago
|
/* the following lines could be deleted as soon as it is formally prooved that
|
||
|
there is no seed leading to (a == b == 0). Until then, just to be safe: */
|
||
|
if ( (rn_current_state.a == 0) && (rn_current_state.b == 0) ) {
|
||
|
rn_current_state.a = 0x9E3779B97F4A7C15;
|
||
|
rn_current_state.b = 0xBF58476D1CE4E5B9;
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
/* stabilize in unlikely case of weak state with only a few bits set */
|
||
|
for(i = 0; i < 32; i++)
|
||
|
n2n_rand();
|
||
4 years ago
|
|
||
4 years ago
|
return 0;
|
||
4 years ago
|
}
|
||
|
|
||
|
|
||
|
/* The following code of xorshift128p was taken from
|
||
|
https://en.wikipedia.org/wiki/Xorshift as of July, 2019
|
||
|
and thus is considered public domain. */
|
||
|
uint64_t n2n_rand () {
|
||
|
|
||
4 years ago
|
uint64_t t = rn_current_state.a;
|
||
|
uint64_t const s = rn_current_state.b;
|
||
4 years ago
|
|
||
4 years ago
|
rn_current_state.a = s;
|
||
|
t ^= t << 23;
|
||
|
t ^= t >> 17;
|
||
|
t ^= s ^ (s >> 26);
|
||
|
rn_current_state.b = t;
|
||
4 years ago
|
|
||
4 years ago
|
return t + s;
|
||
4 years ago
|
}
|
||
|
|
||
|
|
||
|
/* The following code tries to gather some entropy from several sources
|
||
|
for use as seed. Note, that this code does not set the random generator
|
||
|
state yet, a call to n2n_srand ( n2n_seed() ) would do. */
|
||
|
uint64_t n2n_seed (void) {
|
||
|
|
||
|
uint64_t seed = 0;
|
||
|
uint64_t ret = 0;
|
||
|
|
||
|
#ifdef SYS_getrandom
|
||
4 years ago
|
syscall (SYS_getrandom, &seed, sizeof(seed), GRND_NONBLOCK);
|
||
|
ret += seed;
|
||
4 years ago
|
#endif
|
||
|
|
||
4 years ago
|
// __RDRND__ is set only if architecturual feature is set, e.g. compile with -march=native
|
||
4 years ago
|
#ifdef __RDRND__
|
||
4 years ago
|
_rdrand64_step ((unsigned long long*)&seed);
|
||
|
ret += seed;
|
||
4 years ago
|
#endif
|
||
|
|
||
4 years ago
|
// __RDSEED__ ist set only if architecturual feature is set, e.g. compile with -march=native
|
||
4 years ago
|
#ifdef __RDSEED__
|
||
4 years ago
|
_rdseed64_step((unsigned long long*)&seed);
|
||
|
ret += seed;
|
||
4 years ago
|
#endif
|
||
|
|
||
4 years ago
|
/* The WIN32 code is still untested and thus commented
|
||
|
#ifdef WIN32
|
||
|
HCRYPTPROV crypto_provider;
|
||
|
CryptAcquireContext (&crypto_provider, NULL, (LPCWSTR)L"Microsoft Base Cryptographic Provider v1.0",
|
||
|
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||
|
CryptGenRandom (crypto_provider, 8, &seed);
|
||
|
CryptReleaseContext (crypto_provider, 0);
|
||
|
ret += seed;
|
||
|
#endif */
|
||
4 years ago
|
|
||
4 years ago
|
seed = time(NULL); /* UTC in seconds */
|
||
|
ret += seed;
|
||
4 years ago
|
|
||
4 years ago
|
seed = clock() * 8996146197; /* clock() = ticks since program start */
|
||
|
ret += seed;
|
||
4 years ago
|
|
||
4 years ago
|
return ret;
|
||
4 years ago
|
}
|