diff --git a/include/maxscale/random_jkiss.h b/include/maxscale/random.h similarity index 75% rename from include/maxscale/random_jkiss.h rename to include/maxscale/random.h index cfffa77e6..8b0afcb58 100644 --- a/include/maxscale/random_jkiss.h +++ b/include/maxscale/random.h @@ -16,13 +16,6 @@ MXS_BEGIN_DECLS -/** - * @brief Initialize the random number generator - * - * Uses /dev/urandom if available, and warms the generator up with 1000 iterations. - */ -void random_jkiss_init(void); - /** * @brief Return a pseudo-random number * @@ -30,6 +23,6 @@ void random_jkiss_init(void); * * @return A random number */ -unsigned int random_jkiss(void); +unsigned int mxs_random(void); MXS_END_DECLS diff --git a/server/core/CMakeLists.txt b/server/core/CMakeLists.txt index 5cc1e6f19..44713b0a7 100644 --- a/server/core/CMakeLists.txt +++ b/server/core/CMakeLists.txt @@ -36,7 +36,7 @@ add_library(maxscale-common SHARED poll.cc queryclassifier.cc query_classifier.cc - random_jkiss.cc + random.cc resultset.cc resource.cc router.cc diff --git a/server/core/gateway.cc b/server/core/gateway.cc index edbc4573c..62f898a51 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -53,7 +53,7 @@ #include #include #include -#include +#include #include "internal/admin.hh" #include "internal/config.hh" @@ -1783,9 +1783,6 @@ int main(int argc, char **argv) goto return_main; } - /** Initialize the random number generator */ - random_jkiss_init(); - if (!utils_init()) { const char* logerr = "Failed to initialise utility library."; diff --git a/server/core/maxkeys.c b/server/core/maxkeys.c index 613fd1cd3..8eeaff5aa 100644 --- a/server/core/maxkeys.c +++ b/server/core/maxkeys.c @@ -29,7 +29,7 @@ #include #include -#include +#include #ifdef HAVE_GLIBC struct option options[] = @@ -101,7 +101,6 @@ int main(int argc, char **argv) } mxs_log_init(NULL, NULL, MXS_LOG_TARGET_DEFAULT); - random_jkiss_init(); if (secrets_write_keys(directory) != 0) { diff --git a/server/core/maxpasswd.c b/server/core/maxpasswd.c index ee0f0b7a4..87174d4d2 100644 --- a/server/core/maxpasswd.c +++ b/server/core/maxpasswd.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include "internal/secrets.h" @@ -163,8 +163,6 @@ int main(int argc, char **argv) mxs_log_set_priority_enabled(LOG_INFO, false); mxs_log_set_priority_enabled(LOG_DEBUG, false); - random_jkiss_init(); - size_t len = strlen(password); if (len > MXS_PASSWORD_MAXLEN) diff --git a/server/core/random.cc b/server/core/random.cc new file mode 100644 index 000000000..6aa4b6a6b --- /dev/null +++ b/server/core/random.cc @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include + +#include + +namespace +{ + +thread_local std::mt19937 generator(std::random_device{}()); + +} + +unsigned int mxs_random(void) +{ + return generator(); +} diff --git a/server/core/random_jkiss.cc b/server/core/random_jkiss.cc deleted file mode 100644 index c9652a18c..000000000 --- a/server/core/random_jkiss.cc +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2016 MariaDB Corporation Ab - * - * Use of this software is governed by the Business Source License included - * in the LICENSE.TXT file and at www.mariadb.com/bsl11. - * - * Change Date: 2022-01-01 - * - * On the date above, in accordance with the Business Source License, use - * of this software will be governed by version 2 or later of the General - * Public License. - */ - -/** - * @file random_jkiss.c - Random number generator for the MariaDB Corporation MaxScale - * - * See http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf for discussion of random - * number generators (RNGs). - */ - -#include -#include -#include -#include -#include -#include -#include - -/* Public domain code for JKISS RNG - Comment header added */ - -/* If possible, the seed variables will be set from /dev/urandom but - * should that fail, these arbitrary numbers will be used as a last resort. - */ -static unsigned int x = 123456789, y = 987654321, z = 43219876, c = 6543217; /* Seed variables */ -static bool init = false; - -unsigned int random_jkiss(void) -{ - unsigned long long t; - unsigned int result; - ss_dassert(init); - - x = 314527869 * x + 1234567; - y ^= y << 5; - y ^= y >> 7; - y ^= y << 22; - t = 4294584393ULL * z + c; - c = t >> 32; - z = t; - result = x + y + z; - return result; -} - -/* Own code adapted from http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf */ - -/*** - * - * Obtain a seed random number from /dev/urandom if available. - * - * @return uint Random number - * - */ -static unsigned int random_jkiss_devrand(void) -{ - int fn; - unsigned int r; - - if ((fn = open("/dev/urandom", O_RDONLY)) == -1) - { - return 0; - } - - if (read(fn, &r, sizeof(r)) != sizeof(r)) - { - r = 0; - } - close(fn); - return r; -} - -void random_jkiss_init(void) -{ - if (!init) - { - int newrand, i; - - if ((newrand = random_jkiss_devrand()) != 0) - { - x = newrand; - } - - if ((newrand = random_jkiss_devrand()) != 0) - { - y = newrand; - } - - if ((newrand = random_jkiss_devrand()) != 0) - { - z = newrand; - } - - if ((newrand = random_jkiss_devrand()) != 0) - { - c = newrand % 698769068 + 1; /* Should be less than 698769069 */ - } - - init = true; - } -} diff --git a/server/core/secrets.cc b/server/core/secrets.cc index b867be025..558f49dcc 100644 --- a/server/core/secrets.cc +++ b/server/core/secrets.cc @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include "internal/secrets.h" @@ -35,7 +35,7 @@ static unsigned char secrets_randomchar() { - return (char) ((random_jkiss() % ('~' - ' ')) + ' '); + return (char) ((mxs_random() % ('~' - ' ')) + ' '); } static int diff --git a/server/core/test/test_logthrottling.cc b/server/core/test/test_logthrottling.cc index 2c802c2e8..3bda14d46 100644 --- a/server/core/test/test_logthrottling.cc +++ b/server/core/test/test_logthrottling.cc @@ -21,7 +21,7 @@ #include #include #include -#include +#include using std::cerr; using std::cout; @@ -161,7 +161,6 @@ int main(int argc, char* argv[]) int rc; std::ios::sync_with_stdio(); - random_jkiss_init(); rc = sem_init(&u_semstart, 0, 0); ensure(rc == 0); diff --git a/server/core/utils.cc b/server/core/utils.cc index 9e66bcfa8..abe456428 100644 --- a/server/core/utils.cc +++ b/server/core/utils.cc @@ -48,7 +48,7 @@ #include #include #include -#include +#include #include #include @@ -161,7 +161,7 @@ char *gw_strend(register const char *s) *****************************************/ static char gw_randomchar() { - return (char)((random_jkiss() % 78) + 30); + return (char)((mxs_random() % 78) + 30); } /***************************************** diff --git a/server/modules/routing/avrorouter/avro.cc b/server/modules/routing/avrorouter/avro.cc index 1e1e63cd4..e0e719fb1 100644 --- a/server/modules/routing/avrorouter/avro.cc +++ b/server/modules/routing/avrorouter/avro.cc @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/server/modules/routing/avrorouter/avro_main.cc b/server/modules/routing/avrorouter/avro_main.cc index e95fd2881..7a82ce350 100644 --- a/server/modules/routing/avrorouter/avro_main.cc +++ b/server/modules/routing/avrorouter/avro_main.cc @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include