feat(libSceLibreSsl): implement RAND and the CRYPTO allocator functions #9041
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9041"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T10:36:52Z.
What
373 libSceLibreSsl exports on
mainstill 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 aCRYPTO_mallocsize of zero or less:RAND_add,RAND_bytes,RAND_status,RAND_load_file,RAND_file_name,CRYPTO_malloc,CRYPTO_freeandCRYPTO_set_mem_functions. Code that sets up OpenSSL-style crypto, such as libcurl or engine networking, calls these at startup.RAND_bytesfillsnumbytes from host entropy (HostEntropy_nid_no_patch, asarc4random_bufin libScePosixForWebKit does) whennumis positive, and returns 1.RAND_addignores its input andRAND_statusreturns 1.RAND_load_filereturns 123456 for-1and otherwise the requested count truncated toint, without opening the file.RAND_file_namecopies/dev/urandomwithstrlcpysemantics: it truncates and NUL-terminates when the buffer is too small and then returns null.CRYPTO_mallocallocates from the guest heap (malloc_nid_postfix), sofreeandCRYPTO_freerelease the same blocks.CRYPTO_freefrees through the guest heap.CRYPTO_set_mem_functionsreturns 0 and keeps the allocator.CRYPTO_mallocwith 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 anintsize). Later versions take asize_tand callmalloc, but the module's export table still hasHMAC_CTX_initandHMAC_CTX_cleanup(in 3.4.1, gone in 3.5.2), and the firmware 13.40 measurements that theX509_verify_cert_error_stringdebt entry onmaincites point to 2.5.4. A null buffer with a positive length inRAND_bytesorRAND_file_namethrows. LibreSSL would crash there.Tested
Linux (Ubuntu 24.04.5 container, GCC 13.3, Release,
BUILD_TESTING=ON):guest_libressl_runtime: passed. It checks thatRAND_byteswrites exactlynumbytes, leaves the buffer alone for zero and negative counts, and differs across two 4099-byte draws with a byte distribution bound. It also checks theRAND_status,RAND_addandRAND_load_fileresults (including the 64-bit tointtruncation) andRAND_file_namewith buffers of 32, 13, 12 and 0 bytes.CRYPTO_mallocblocks are released through bothCRYPTO_freeand the guestfree, and the reverse.CRYPTO_mallocreturns null for sizes 0 and -1. The throwing cases are covered too.patched_libraries: passed. The patchedlibSceLibreSsl.prxexports the same 394_nid_postfixnames as before.python3 tools/check_conventions.py --base upstream/main: passed.687e7d637(ond5f5cd8db, with the review fix):guest_libressl_runtime,guest_libressl_digest,guest_libressl_des,guest_libressl_x509_errors,guest_libressl3_digest,guest_crypto_memcmp,patched_librariesandcheck_conventions.pypassed, and the unpatchedlibSceLibreSsl.prxstill exports 394_nid_postfixnames. With the previous throwingCRYPTO_malloc,guest_libressl_runtimefails.Runtime.cpp,Export.cppandGuestLibreSslRuntime.cpppass a MinGW-w64 GCC 13 syntax check (-fsyntax-only).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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Duplicate and overlap check
Checked on 2026-10-23 against upstream main
d5f5cd8dband the heads of all 1250 open PRs.RAND_add,RAND_bytes,RAND_status,RAND_load_file,RAND_file_name,CRYPTO_malloc,CRYPTO_freeorCRYPTO_set_mem_functions.687e7d637merges 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.core/libs/prx/libSceLibreSsl/CMakeLists.txt(theadd_librarysources and the test blocks) and/or the pending-export count indocs/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.CRYPTO_freeremains unimplemented and that its ASN1 conversion buffers come from the guest libc allocator.CRYPTO_freehere frees through the same guestfree, so those buffers can be released with it once both are onmain; only that sentence would need updating.Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T17:51:11Z.
Windows run of 102b9df3c6 merged onto
main745b394629(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
libSceLibreSslandguest_libressl_runtime_testswith no errors or warnings.ctest --repeat until-fail:2onguest_libressl_runtime: passes on both repeats. Not run: a title.