feat(libSceLibreSsl): implement RAND and the CRYPTO allocator functions #9041

Merged
HQasper merged 2 commits from upstream-pr/9041 into main 2026-10-23 18:28:29 +00:00
HQasper commented 2026-10-21 10:36:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T10:36:52Z.


What

373 libSceLibreSsl exports on main still throw. This implements eight runtime exports whose behaviour is identical in LibreSSL 2.6.5 and 3.4.3, and in later versions for every input except a CRYPTO_malloc size of zero or less: RAND_add, RAND_bytes, RAND_status, RAND_load_file, RAND_file_name, CRYPTO_malloc, CRYPTO_free and CRYPTO_set_mem_functions. Code that sets up OpenSSL-style crypto, such as libcurl or engine networking, calls these at startup.

  • They follow rand_lib.c and randfile.c and malloc-wrapper.c (3.4.3; compared line by line with 2.6.5 and current):
    • RAND_bytes fills num bytes from host entropy (HostEntropy_nid_no_patch, as arc4random_buf in libScePosixForWebKit does) when num is positive, and returns 1.
    • RAND_add ignores its input and RAND_status returns 1.
    • RAND_load_file returns 123456 for -1 and otherwise the requested count truncated to int, without opening the file.
    • RAND_file_name copies /dev/urandom with strlcpy semantics: it truncates and NUL-terminates when the buffer is too small and then returns null.
    • CRYPTO_malloc allocates from the guest heap (malloc_nid_postfix), so free and CRYPTO_free release the same blocks. CRYPTO_free frees through the guest heap.
    • CRYPTO_set_mem_functions returns 0 and keeps the allocator.
  • CRYPTO_malloc with a size of zero or less returns null, as LibreSSL 2.5.4, 3.4.1 and 3.4.3 do (if (num <= 0) return NULL; with an int size). Later versions take a size_t and call malloc, but the module's export table still has HMAC_CTX_init and HMAC_CTX_cleanup (in 3.4.1, gone in 3.5.2), and the firmware 13.40 measurements that the X509_verify_cert_error_string debt entry on main cites point to 2.5.4. A null buffer with a positive length in RAND_bytes or RAND_file_name throws. LibreSSL would crash there.
  • The debt entry records the source versions and these cases. The pending-export count goes from 373 to 365.

Tested

Linux (Ubuntu 24.04.5 container, GCC 13.3, Release, BUILD_TESTING=ON):

  • New guest_libressl_runtime: passed. It checks that RAND_bytes writes exactly num bytes, leaves the buffer alone for zero and negative counts, and differs across two 4099-byte draws with a byte distribution bound. It also checks the RAND_status, RAND_add and RAND_load_file results (including the 64-bit to int truncation) and RAND_file_name with buffers of 32, 13, 12 and 0 bytes. CRYPTO_malloc blocks are released through both CRYPTO_free and the guest free, and the reverse. CRYPTO_malloc returns null for sizes 0 and -1. The throwing cases are covered too.
  • patched_libraries: passed. The patched libSceLibreSsl.prx exports the same 394 _nid_postfix names as before.
  • python3 tools/check_conventions.py --base upstream/main: passed.
  • On 687e7d637 (on d5f5cd8db, with the review fix): guest_libressl_runtime, guest_libressl_digest, guest_libressl_des, guest_libressl_x509_errors, guest_libressl3_digest, guest_crypto_memcmp, patched_libraries and check_conventions.py passed, and the unpatched libSceLibreSsl.prx still exports 394 _nid_postfix names. With the previous throwing CRYPTO_malloc, guest_libressl_runtime fails.
  • Windows: not run. Runtime.cpp, Export.cpp and GuestLibreSslRuntime.cpp pass a MinGW-w64 GCC 13 syntax check (-fsyntax-only).
  • Not tested: any title or homebrew (no title using libSceLibreSsl is known).

Note: this PR and #9033 (AES) both change core/libs/prx/libSceLibreSsl/CMakeLists.txt and the pending-export count in the debt file, so whichever merges second needs a small rebase.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Duplicate and overlap check

Checked on 2026-10-23 against upstream main d5f5cd8db and the heads of all 1250 open PRs.

  • No open PR or commit on main implements RAND_add, RAND_bytes, RAND_status, RAND_load_file, RAND_file_name, CRYPTO_malloc, CRYPTO_free or CRYPTO_set_mem_functions.
  • 687e7d637 merges cleanly with main. #9047 (DES) and #10065 (X509 error strings) merged since the last rebase; this rebase keeps their sources, test blocks and debt entries.
  • 15 other open PRs change libSceLibreSsl. #9033 (AES), #9448 (EVP/HMAC), #9529 and #9942 merge cleanly with main but conflict with this PR in core/libs/prx/libSceLibreSsl/CMakeLists.txt (the add_library sources and the test blocks) and/or the pending-export count in docs/dev/debt/libSceLibreSsl.md, so whichever merges second needs a small rebase. #9446, #9571, #9745, #9870, #9949, #10005, #10071 and #10078 already conflict with main in the same files. #9535, #9536 and #9785 merge cleanly with this PR. None of them implements these functions.
  • #10005's debt entry says CRYPTO_free remains unimplemented and that its ASN1 conversion buffers come from the guest libc allocator. CRYPTO_free here frees through the same guest free, so those buffers can be released with it once both are on main; only that sentence would need updating.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4950204264 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9041). Originally posted by [HQasper](https://github.com/HQasper) on 2026-10-21T10:36:52Z. --- ### What 373 libSceLibreSsl exports on `main` still throw. This implements eight runtime exports whose behaviour is identical in LibreSSL 2.6.5 and 3.4.3, and in later versions for every input except a `CRYPTO_malloc` size of zero or less: `RAND_add`, `RAND_bytes`, `RAND_status`, `RAND_load_file`, `RAND_file_name`, `CRYPTO_malloc`, `CRYPTO_free` and `CRYPTO_set_mem_functions`. Code that sets up OpenSSL-style crypto, such as libcurl or engine networking, calls these at startup. - They follow [rand_lib.c and randfile.c](https://github.com/libressl/openbsd/tree/0f0b16dc977f6ca499f0f4540078d992c96da597/src/lib/libcrypto/rand) and [malloc-wrapper.c](https://github.com/libressl/openbsd/blob/0f0b16dc977f6ca499f0f4540078d992c96da597/src/lib/libcrypto/malloc-wrapper.c) (3.4.3; compared line by line with 2.6.5 and current): - `RAND_bytes` fills `num` bytes from host entropy (`HostEntropy_nid_no_patch`, as `arc4random_buf` in libScePosixForWebKit does) when `num` is positive, and returns 1. - `RAND_add` ignores its input and `RAND_status` returns 1. - `RAND_load_file` returns 123456 for `-1` and otherwise the requested count truncated to `int`, without opening the file. - `RAND_file_name` copies `/dev/urandom` with `strlcpy` semantics: it truncates and NUL-terminates when the buffer is too small and then returns null. - `CRYPTO_malloc` allocates from the guest heap (`malloc_nid_postfix`), so `free` and `CRYPTO_free` release the same blocks. `CRYPTO_free` frees through the guest heap. - `CRYPTO_set_mem_functions` returns 0 and keeps the allocator. - `CRYPTO_malloc` with a size of zero or less returns null, as LibreSSL 2.5.4, 3.4.1 and 3.4.3 do (`if (num <= 0) return NULL;` with an `int` size). Later versions take a `size_t` and call `malloc`, but the module's export table still has `HMAC_CTX_init` and `HMAC_CTX_cleanup` (in 3.4.1, gone in 3.5.2), and the firmware 13.40 measurements that the `X509_verify_cert_error_string` debt entry on `main` cites point to 2.5.4. A null buffer with a positive length in `RAND_bytes` or `RAND_file_name` throws. LibreSSL would crash there. - The debt entry records the source versions and these cases. The pending-export count goes from 373 to 365. ### Tested Linux (Ubuntu 24.04.5 container, GCC 13.3, Release, `BUILD_TESTING=ON`): - New `guest_libressl_runtime`: passed. It checks that `RAND_bytes` writes exactly `num` bytes, leaves the buffer alone for zero and negative counts, and differs across two 4099-byte draws with a byte distribution bound. It also checks the `RAND_status`, `RAND_add` and `RAND_load_file` results (including the 64-bit to `int` truncation) and `RAND_file_name` with buffers of 32, 13, 12 and 0 bytes. `CRYPTO_malloc` blocks are released through both `CRYPTO_free` and the guest `free`, and the reverse. `CRYPTO_malloc` returns null for sizes 0 and -1. The throwing cases are covered too. - `patched_libraries`: passed. The patched `libSceLibreSsl.prx` exports the same 394 `_nid_postfix` names as before. - `python3 tools/check_conventions.py --base upstream/main`: passed. - On 687e7d637 (on d5f5cd8db, with the review fix): `guest_libressl_runtime`, `guest_libressl_digest`, `guest_libressl_des`, `guest_libressl_x509_errors`, `guest_libressl3_digest`, `guest_crypto_memcmp`, `patched_libraries` and `check_conventions.py` passed, and the unpatched `libSceLibreSsl.prx` still exports 394 `_nid_postfix` names. With the previous throwing `CRYPTO_malloc`, `guest_libressl_runtime` fails. - Windows: not run. `Runtime.cpp`, `Export.cpp` and `GuestLibreSslRuntime.cpp` pass a MinGW-w64 GCC 13 syntax check (`-fsyntax-only`). - Not tested: any title or homebrew (no title using libSceLibreSsl is known). Note: this PR and #9033 (AES) both change core/libs/prx/libSceLibreSsl/CMakeLists.txt and the pending-export count in the debt file, so whichever merges second needs a small rebase. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes ### Duplicate and overlap check Checked on 2026-10-23 against upstream main d5f5cd8db and the heads of all 1250 open PRs. - No open PR or commit on main implements `RAND_add`, `RAND_bytes`, `RAND_status`, `RAND_load_file`, `RAND_file_name`, `CRYPTO_malloc`, `CRYPTO_free` or `CRYPTO_set_mem_functions`. - 687e7d637 merges cleanly with main. #9047 (DES) and #10065 (X509 error strings) merged since the last rebase; this rebase keeps their sources, test blocks and debt entries. - 15 other open PRs change libSceLibreSsl. #9033 (AES), #9448 (EVP/HMAC), #9529 and #9942 merge cleanly with main but conflict with this PR in `core/libs/prx/libSceLibreSsl/CMakeLists.txt` (the `add_library` sources and the test blocks) and/or the pending-export count in `docs/dev/debt/libSceLibreSsl.md`, so whichever merges second needs a small rebase. #9446, #9571, #9745, #9870, #9949, #10005, #10071 and #10078 already conflict with main in the same files. #9535, #9536 and #9785 merge cleanly with this PR. None of them implements these functions. - #10005's debt entry says `CRYPTO_free` remains unimplemented and that its ASN1 conversion buffers come from the guest libc allocator. `CRYPTO_free` here frees through the same guest `free`, so those buffers can be released with it once both are on `main`; only that sentence would need updating.
ycbuzi commented 2026-10-21 17:51:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T17:51:11Z.


Windows run of 102b9df3c6 merged onto main 745b394629 (e325baead3, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libSceLibreSsl and guest_libressl_runtime_tests with no errors or warnings. ctest --repeat until-fail:2 on guest_libressl_runtime: passes on both repeats. Not run: a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322740290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9041#issuecomment-6322740290). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T17:51:11Z. --- Windows run of 102b9df3c6 merged onto `main` 745b394629 (e325baead3, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`. Built `libSceLibreSsl` and `guest_libressl_runtime_tests` with no errors or warnings. `ctest --repeat until-fail:2` on `guest_libressl_runtime`: passes on both repeats. Not run: a title.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9041
No description provided.