Browse Source

Switch custom RNG for libsodium

pull/2238/head
staphen 5 years ago committed by Anders Jenbo
parent
commit
8fbe5efd9e
  1. 1
      CMakeLists.txt
  2. 2
      Source/main.cpp
  3. 54
      Source/platform/switch/random.cpp
  4. 2
      Source/platform/switch/random.hpp

1
CMakeLists.txt

@ -485,6 +485,7 @@ if(NINTENDO_SWITCH)
Source/platform/switch/network.cpp
Source/platform/switch/keyboard.cpp
Source/platform/switch/docking.cpp
Source/platform/switch/random.cpp
Source/platform/switch/asio/net/if.c
Source/platform/switch/asio/sys/signal.c)
endif()

2
Source/main.cpp

@ -1,6 +1,7 @@
#include <SDL.h>
#ifdef __SWITCH__
#include "platform/switch/network.h"
#include "platform/switch/random.hpp"
#endif
#ifdef __3DS__
#include "platform/ctr/system.h"
@ -26,6 +27,7 @@ int main(int argc, char **argv)
{
#ifdef __SWITCH__
switch_enable_network();
randombytes_switchrandom_init();
#endif
#ifdef __3DS__
ctr_sys_init();

54
Source/platform/switch/random.cpp

@ -0,0 +1,54 @@
#include <sys/types.h>
#include <sodium.h>
extern "C" {
#include <switch/sf/service.h>
#include <switch/services/csrng.h>
}
static const char *randombytes_switchrandom_implementation_name()
{
return "switchrandom";
}
static bool randombytes_switchrandom_tryfill(void *const buf, const size_t size)
{
Result res;
Service *csrngService = csrngGetServiceSession();
if (!serviceIsActive(csrngService)) {
res = csrngInitialize();
if (!R_SUCCEEDED(res))
return false;
}
res = csrngGetRandomBytes(buf, size);
return R_SUCCEEDED(res);
}
static uint32_t randombytes_switchrandom()
{
uint32_t num;
if (!randombytes_switchrandom_tryfill(&num, sizeof(uint32_t)))
sodium_misuse();
return num;
}
static void randombytes_switchrandom_buf(void *const buf, const size_t size)
{
if (!randombytes_switchrandom_tryfill(buf, size))
sodium_misuse();
}
struct randombytes_implementation randombytes_switchrandom_implementation = {
randombytes_switchrandom_implementation_name,
randombytes_switchrandom,
nullptr,
nullptr,
randombytes_switchrandom_buf,
nullptr
};
void randombytes_switchrandom_init()
{
randombytes_set_implementation(&randombytes_switchrandom_implementation);
atexit(csrngExit);
}

2
Source/platform/switch/random.hpp

@ -0,0 +1,2 @@
#pragma once
void randombytes_switchrandom_init();
Loading…
Cancel
Save