|
17 | 17 | # endif |
18 | 18 | #endif |
19 | 19 |
|
| 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 | + |
20 | 27 | #ifdef Py_DEBUG |
21 | 28 | int _Py_HashSecret_Initialized = 0; |
22 | 29 | #else |
23 | 30 | static int _Py_HashSecret_Initialized = 0; |
24 | 31 | #endif |
25 | 32 |
|
| 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 | + |
26 | 103 | #ifdef MS_WINDOWS |
27 | 104 | static HCRYPTPROV hCryptProv = 0; |
28 | 105 |
|
@@ -418,6 +495,8 @@ _PyOS_URandom(void *buffer, Py_ssize_t size) |
418 | 495 |
|
419 | 496 | #ifdef MS_WINDOWS |
420 | 497 | return win32_urandom((unsigned char *)buffer, size, 1); |
| 498 | +#elif defined(SWITCH) |
| 499 | + return switch_urandom(buffer, size, 0); |
421 | 500 | #elif defined(PY_GETENTROPY) |
422 | 501 | return py_getentropy(buffer, size, 0); |
423 | 502 | #else |
@@ -465,6 +544,8 @@ _PyRandom_Init(void) |
465 | 544 | else { |
466 | 545 | #ifdef MS_WINDOWS |
467 | 546 | (void)win32_urandom(secret, secret_size, 0); |
| 547 | +#elif defined(SWITCH) |
| 548 | + (void)switch_urandom(secret, secret_size, 0); |
468 | 549 | #elif defined(PY_GETENTROPY) |
469 | 550 | (void)py_getentropy(secret, secret_size, 1); |
470 | 551 | #else |
|
0 commit comments