Skip to content

Commit 355ac16

Browse files
UNTESTED: GetRandomBytes implementation
1 parent 8d9c9fa commit 355ac16

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

python_build/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ soospatchPY: compilePY
8686
cp $(PYDIR)/Python/pytime.c $(PYDIR)/Python/pytime.c_old
8787
cat $(PYDIR)/Python/pytime.c_old | sed 's/CLOCK_MONOTONIC/(clockid_t)4/g' >$(PYDIR)/Python/pytime.c
8888
cp $(PYDIR)/Makefile $(PYDIR)/Makefile_old
89-
cat $(PYDIR)/Makefile_old | sed 's/Python\/$$(DYNLOADFILE) \\/\\/' >$(PYDIR)/Makefile
89+
cat $(PYDIR)/Makefile_old | sed 's/-Werror=declaration-after-statement//' | sed 's/Python\/$$(DYNLOADFILE) \\/\\/' >$(PYDIR)/Makefile
9090
touch soospatchPY
9191

9292
compilePY: extractedPY patchPY

python_config/random.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,89 @@
1717
# endif
1818
#endif
1919

20+
#ifdef SWITCH
21+
#include "switch/types.h"
22+
#include "switch/result.h"
23+
#include "switch/ipc.h"
24+
#include "switch/services/sm.h"
25+
#endif
26+
2027
#ifdef Py_DEBUG
2128
int _Py_HashSecret_Initialized = 0;
2229
#else
2330
static int _Py_HashSecret_Initialized = 0;
2431
#endif
2532

33+
#ifdef SWITCH
34+
static Service g_csrngSrv;
35+
36+
static int
37+
switch_urandom_init(int raise)
38+
{
39+
Result rc;
40+
41+
rc = smGetService(&g_csrngSrv, "csrng");
42+
if (R_SUCCEEDED(rc))
43+
return 0;
44+
45+
if (raise)
46+
PyErr_SetString(PyExc_RuntimeError,
47+
"SWITCH: Failed to get csrng service handle.");
48+
else
49+
Py_FatalError("SWITCH: Failed to get csrng service handle.");
50+
return -1;
51+
}
52+
53+
static int
54+
switch_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
55+
{
56+
IpcCommand c;
57+
IpcParsedCommand r;
58+
Result rc;
59+
60+
struct {
61+
u64 magic;
62+
u64 cmd_id;
63+
} *raw;
64+
65+
struct {
66+
u64 magic;
67+
u64 result;
68+
} *resp;
69+
70+
if (g_csrngSrv.handle == 0)
71+
switch_urandom_init(raise);
72+
73+
ipcInitialize(&c);
74+
ipcAddRecvBuffer(&c, buffer, size, 0);
75+
76+
raw = ipcPrepareHeader(&c, sizeof(*raw));
77+
raw->magic = SFCI_MAGIC;
78+
raw->cmd_id = 0;
79+
80+
rc = serviceIpcDispatch(&g_csrngSrv);
81+
82+
if (!R_SUCCEEDED(rc))
83+
goto error;
84+
85+
ipcParse(&r);
86+
87+
resp = r.Raw;
88+
rc = resp->result;
89+
90+
if (R_SUCCEEDED(rc))
91+
return 0;
92+
93+
error:
94+
if (raise)
95+
PyErr_SetString(PyExc_RuntimeError,
96+
"SWITCH: GetRandomBytes failed");
97+
else
98+
Py_FatalError("SWITCH: GetRandomBytes failed");
99+
return -1;
100+
}
101+
#endif
102+
26103
#ifdef MS_WINDOWS
27104
static HCRYPTPROV hCryptProv = 0;
28105

@@ -418,6 +495,8 @@ _PyOS_URandom(void *buffer, Py_ssize_t size)
418495

419496
#ifdef MS_WINDOWS
420497
return win32_urandom((unsigned char *)buffer, size, 1);
498+
#elif defined(SWITCH)
499+
return switch_urandom(buffer, size, 0);
421500
#elif defined(PY_GETENTROPY)
422501
return py_getentropy(buffer, size, 0);
423502
#else
@@ -465,6 +544,8 @@ _PyRandom_Init(void)
465544
else {
466545
#ifdef MS_WINDOWS
467546
(void)win32_urandom(secret, secret_size, 0);
547+
#elif defined(SWITCH)
548+
(void)switch_urandom(secret, secret_size, 0);
468549
#elif defined(PY_GETENTROPY)
469550
(void)py_getentropy(secret, secret_size, 1);
470551
#else

0 commit comments

Comments
 (0)