From 63629b22a1299a06392460d1624641153fa29109 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 13 May 2020 20:44:13 -0700 Subject: [PATCH] exo2: use N's strategy for randcache, it's better --- .../program/source/smc/secmon_random_cache.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/exosphere2/program/source/smc/secmon_random_cache.cpp b/exosphere2/program/source/smc/secmon_random_cache.cpp index bcbed1df7..b5cdf1468 100644 --- a/exosphere2/program/source/smc/secmon_random_cache.cpp +++ b/exosphere2/program/source/smc/secmon_random_cache.cpp @@ -86,17 +86,15 @@ namespace ams::secmon::smc { u8 * const cache = GetRandomBytesCache(); u8 * cur_dst = static_cast(dst); - /* NOTE: Nintendo does not do bounds checking here, and does not do multiple reads when the get would wrap around. */ - while (size > 0) { - const size_t copy_size = std::min(size, static_cast(GetRandomBytesCacheSize() - g_random_offset_low)); - std::memcpy(cur_dst, cache + g_random_offset_low, copy_size); + /* Copy out the requested size. */ + std::memcpy(dst, cache + g_random_offset_low, size); - cur_dst += copy_size; - size -= copy_size; + /* Advance. */ + g_random_offset_low += size; - if (g_random_offset_low + copy_size >= GetRandomBytesCacheSize()) { - g_random_offset_low = 0; - } + /* Ensure that at all times g_random_offset_low is not within 0x38 bytes of the end of the pool. */ + if (g_random_offset_low + MaxRandomBytes >= GetRandomBytesCacheSize()) { + g_random_offset_low = 0; } }