From 7b9a420787c71624ff0e9c6bcfe1e72c74feb677 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Wed, 3 Jul 2019 11:05:01 +0200 Subject: [PATCH] Replace std::random_shuffle with std::shuffle According to [1], std::random_shuffle is deprecated in C++14 and removed in C++17. Since std::shuffle is available since C++11 as a replacement and monero already requires C++11, this is a good replacement. A cryptographically secure random number generator is used in all cases to prevent people from perhaps copying an insecure std::shuffle call over to a place where a secure one would be warranted. A form of defense-in-depth. [1]: https://en.cppreference.com/w/cpp/algorithm/random_shuffle --- src/cryptonote_core/cryptonote_tx_utils.cpp | 2 +- src/p2p/net_peerlist.h | 2 +- src/wallet/wallet2.cpp | 4 ++-- tests/unit_tests/ringct.cpp | 4 ++-- tests/unit_tests/rolling_median.cpp | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp index 854f3d1c5..4cf71e558 100644 --- a/src/cryptonote_core/cryptonote_tx_utils.cpp +++ b/src/cryptonote_core/cryptonote_tx_utils.cpp @@ -354,7 +354,7 @@ namespace cryptonote if (shuffle_outs) { - std::shuffle(destinations.begin(), destinations.end(), std::default_random_engine(crypto::rand())); + std::shuffle(destinations.begin(), destinations.end(), crypto::random_device{}); } // sort ins by their key image diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h index 883997fd6..3041e03b1 100644 --- a/src/p2p/net_peerlist.h +++ b/src/p2p/net_peerlist.h @@ -290,7 +290,7 @@ namespace nodetool if (anonymize) { - std::random_shuffle(bs_head.begin(), bs_head.end()); + std::shuffle(bs_head.begin(), bs_head.end(), crypto::random_device{}); if (bs_head.size() > depth) bs_head.resize(depth); for (auto &e: bs_head) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f25e9ad97..c21b5d040 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -7448,7 +7448,7 @@ void wallet2::light_wallet_get_outs(std::vector())); + std::shuffle(order.begin(), order.end(), crypto::random_device{}); LOG_PRINT_L2("Looking for " << (fake_outputs_count+1) << " outputs with amounts " << print_money(td.is_rct() ? 0 : td.amount())); @@ -8023,7 +8023,7 @@ void wallet2::get_outs(std::vector> order.resize(requested_outputs_count); for (size_t n = 0; n < order.size(); ++n) order[n] = n; - std::shuffle(order.begin(), order.end(), std::default_random_engine(crypto::rand())); + std::shuffle(order.begin(), order.end(), crypto::random_device{}); LOG_PRINT_L2("Looking for " << (fake_outputs_count+1) << " outputs of size " << print_money(td.is_rct() ? 0 : td.amount())); for (size_t o = 0; o < requested_outputs_count && outs.back().size() < fake_outputs_count + 1; ++o) diff --git a/tests/unit_tests/ringct.cpp b/tests/unit_tests/ringct.cpp index 4d51ec434..d2b2c3109 100644 --- a/tests/unit_tests/ringct.cpp +++ b/tests/unit_tests/ringct.cpp @@ -779,8 +779,8 @@ TEST(ringct, range_proofs_accept_very_long_simple) inputs[n] = n; outputs[n] = n; } - std::random_shuffle(inputs, inputs + N); - std::random_shuffle(outputs, outputs + N); + std::shuffle(inputs, inputs + N, crypto::random_device{}); + std::shuffle(outputs, outputs + N, crypto::random_device{}); EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); } diff --git a/tests/unit_tests/rolling_median.cpp b/tests/unit_tests/rolling_median.cpp index 547fe092f..9e4cf87b8 100644 --- a/tests/unit_tests/rolling_median.cpp +++ b/tests/unit_tests/rolling_median.cpp @@ -143,7 +143,7 @@ TEST(rolling_median, order) m.insert(random[i]); ASSERT_EQ(med, m.median()); - std::shuffle(random.begin(), random.end(), std::default_random_engine(crypto::rand())); + std::shuffle(random.begin(), random.end(), crypto::random_device{}); m.clear(); for (int i = 0; i < 1000; ++i) m.insert(random[i]);