random: use TinyMT instead of XorShift

This commit is contained in:
Michael Scire
2019-12-09 23:50:47 -08:00
parent 206b1a1b57
commit 1556a92a38
12 changed files with 415 additions and 175 deletions

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::os::impl {
void InitializeRandomImpl(util::TinyMT *mt);
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::os::impl {
void InitializeRandomImpl(util::TinyMT *mt) {
/* Retrieve entropy from kernel. */
u32 seed[4];
static_assert(util::size(seed) == util::TinyMT::NumStateWords);
/* Nintendo does not check the result of these invocations, but we will for safety. */
/* Nintendo uses entropy values 0, 1 to seed the public TinyMT random, and values */
/* 2, 3 to seed os::detail::RngManager's private TinyMT random. */
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&seed[0]), InfoType_RandomEntropy, INVALID_HANDLE, 0));
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&seed[2]), InfoType_RandomEntropy, INVALID_HANDLE, 1));
mt->Initialize(seed, util::size(seed));
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "impl/os_random_impl.hpp"
namespace ams::os {
namespace {
util::TinyMT g_random;
os::Mutex g_random_mutex;
bool g_initialized_random;
template<typename T>
inline T GenerateRandomTImpl(T max) {
static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value);
const T EffectiveMax = (std::numeric_limits<T>::max() / max) * max;
T cur_rnd;
while (true) {
os::GenerateRandomBytes(&cur_rnd, sizeof(T));
if (cur_rnd < EffectiveMax) {
return cur_rnd % max;
}
}
}
}
void GenerateRandomBytes(void *dst, size_t size) {
std::scoped_lock lk(g_random_mutex);
if (!g_initialized_random) {
impl::InitializeRandomImpl(&g_random);
g_initialized_random = true;
}
g_random.GenerateRandomBytes(dst, size);
}
u32 GenerateRandomU32(u32 max) {
return GenerateRandomTImpl<u32>(max);
}
u64 GenerateRandomU64(u64 max) {
return GenerateRandomTImpl<u64>(max);
}
}