mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-28 15:52:39 +02:00
Implement CSRNG services
This commit is contained in:
parent
24b42cb6a3
commit
836ec09917
@ -41,6 +41,7 @@ extern "C" {
|
|||||||
#include "switch/services/applet.h"
|
#include "switch/services/applet.h"
|
||||||
#include "switch/services/audin.h"
|
#include "switch/services/audin.h"
|
||||||
#include "switch/services/audout.h"
|
#include "switch/services/audout.h"
|
||||||
|
#include "switch/services/csrng.h"
|
||||||
//#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead
|
//#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead
|
||||||
#include "switch/services/fatal.h"
|
#include "switch/services/fatal.h"
|
||||||
#include "switch/services/time.h"
|
#include "switch/services/time.h"
|
||||||
|
13
nx/include/switch/services/csrng.h
Normal file
13
nx/include/switch/services/csrng.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* @file csrng.h
|
||||||
|
* @brief Cryptographically-Secure Random Number Generation (csrng) service IPC wrapper.
|
||||||
|
* @author SciresM
|
||||||
|
* @copyright libnx Authors
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include "../types.h"
|
||||||
|
|
||||||
|
Result csrngInitialize(void);
|
||||||
|
void csrngExit(void);
|
||||||
|
|
||||||
|
Result csrngGetRandomBytes(void *out, size_t out_size);
|
59
nx/source/services/csrng.c
Normal file
59
nx/source/services/csrng.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// Copyright 2018 SciresM
|
||||||
|
#include <string.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "result.h"
|
||||||
|
#include "arm/atomics.h"
|
||||||
|
#include "kernel/ipc.h"
|
||||||
|
#include "kernel/detect.h"
|
||||||
|
#include "services/sm.h"
|
||||||
|
#include "services/spl.h"
|
||||||
|
|
||||||
|
static Service g_csrngSrv;
|
||||||
|
static u64 g_csrngRefCnt;
|
||||||
|
|
||||||
|
Result csrngInitialize(void) {
|
||||||
|
atomicIncrement64(&g_csrngRefCnt);
|
||||||
|
|
||||||
|
if (serviceIsActive(&g_csrngSrv))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return smGetService(&g_csrngSrv, "csrng");
|
||||||
|
}
|
||||||
|
|
||||||
|
void csrngExit(void) {
|
||||||
|
if (atomicDecrement64(&g_csrngRefCnt) == 0)
|
||||||
|
serviceClose(&g_csrngSrv);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result csrngGetRandomBytes(void *out, size_t out_size) {
|
||||||
|
IpcCommand c;
|
||||||
|
ipcInitialize(&c);
|
||||||
|
|
||||||
|
ipcAddRecvBuffer(&c, out, out_size, BufferType_Normal);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 cmd_id;
|
||||||
|
} *raw;
|
||||||
|
|
||||||
|
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||||
|
|
||||||
|
raw->magic = SFCI_MAGIC;
|
||||||
|
raw->cmd_id = 0;
|
||||||
|
|
||||||
|
Result rc = serviceIpcDispatch(&g_csrngSrv);
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(rc)) {
|
||||||
|
IpcParsedCommand r;
|
||||||
|
ipcParse(&r);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
u64 magic;
|
||||||
|
u64 result;
|
||||||
|
} *resp = r.Raw;
|
||||||
|
|
||||||
|
rc = resp->result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user