From 858dcfc14cb816dbf91a3c1e18177912adb7cc72 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 30 May 2018 21:19:53 +0200 Subject: [PATCH] removed unused tempering in random number generator, added comments and license text --- libretroshare/src/util/rsrandom.cc | 27 +++++++++++++++ libretroshare/src/util/rsrandom.h | 53 ++++++++++++++---------------- 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/libretroshare/src/util/rsrandom.cc b/libretroshare/src/util/rsrandom.cc index 03c8a95d9..86f93bf5f 100644 --- a/libretroshare/src/util/rsrandom.cc +++ b/libretroshare/src/util/rsrandom.cc @@ -1,3 +1,24 @@ +/******************************************************************************* + * libretroshare/src/util: rsrandom.cc * + * * + * libretroshare: retroshare core library * + * * + * Copyright (C) 2010 Cyril Soler * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this program. If not, see . * + * * + *******************************************************************************/ #include #include #include @@ -35,6 +56,10 @@ bool RSRandom::seed(uint32_t s) for (j=1; j> 30)) + j) & 0xffffffffUL ; + // This *does not* replace the internal seed state of RAND_bytes(), but only *adds* entropy to the random pool + // So calling this method with the same value twice does not guarranty that the output of the random bytes + // will be the same. + RAND_seed((unsigned char *)&MT[0],N*sizeof(uint32_t)) ; locked_next_state() ; @@ -66,11 +91,13 @@ uint32_t RSRandom::random_u32() y = MT[index] ; } +#ifdef UNNECESSARY_CODE // Tempering y ^= (y >> 11); y ^= (y << 7 ) & 0x9d2c5680UL; y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); +#endif return y; } diff --git a/libretroshare/src/util/rsrandom.h b/libretroshare/src/util/rsrandom.h index 8cd66f7f6..59c488909 100644 --- a/libretroshare/src/util/rsrandom.h +++ b/libretroshare/src/util/rsrandom.h @@ -1,36 +1,31 @@ -/**************************************************************** - * RetroShare is distributed under the following license: - * - * Copyright (C) 2010 Cyril Soler - * - * 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 2 - * 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, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - ****************************************************************/ - +/******************************************************************************* + * libretroshare/src/util: rsrandom.h * + * * + * libretroshare: retroshare core library * + * * + * Copyright (C) 2010 Cyril Soler * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this program. If not, see . * + * * + *******************************************************************************/ #pragma once // RSRandom contains a random number generator that is // - thread safe // - system independant // - fast -// - NOT CRYPTOGRAPHICALLY SAFE -// - DO NOT USE FOR ANYTHING REQUIRING STRONG RANDOMNESS -// -// The implementation is adapted from the Mersenne Twister page of Wikipedia. -// -// http://en.wikipedia.org/wiki/Mersenne_twister +// - CRYPTOGRAPHICALLY SAFE, because it is based on openssl random number generator #include #include @@ -40,8 +35,8 @@ class RSRandom public: static uint32_t random_u32() ; static uint64_t random_u64() ; - static float random_f32() ; - static double random_f64() ; + static float random_f32() ; + static double random_f64() ; static bool seed(uint32_t s) ;