feat(libSceLibreSsl): implement the EVP message digest and HMAC functions #9448
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9448"
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-21T22:25:16Z.
What
All libSceLibreSsl exports outside the open PRs still throw. This implements the 20 EVP message digest and HMAC exports, which is what code built on LibreSSL normally calls instead of the raw
SHA256_*functions:EVP_md5,EVP_sha256,EVP_sha384,EVP_sha512,EVP_MD_size,EVP_MD_CTX_init,EVP_MD_CTX_create,EVP_MD_CTX_cleanup,EVP_MD_CTX_destroy,EVP_DigestInit_ex,EVP_DigestUpdate,EVP_DigestFinal_ex,EVP_Digest,HMAC_CTX_init,HMAC_CTX_cleanup,HMAC_Init,HMAC_Init_ex,HMAC_Update,HMAC_FinalandHMAC.mainalready cites forX509_verify_cert_error_stringidentify LibreSSL 2.5.4, while the module's export table also hasSSL_get0_paramandX509_VERIFY_PARAM_set1_host, which the Symbols.list of 2.5.4 and 2.6.3 lack and 2.7.2's has. It also hasHMAC_CTX_init/HMAC_CTX_cleanup, which 3.4.1 has and 3.5.2 dropped whenHMAC_CTXbecame opaque. The functions follow 2.5.4's digest.c and hmac.c, andsrc/Evp.cppstarts with the copyright notices, licence conditions and disclaimers of both files, verbatim from that commit. Up to 3.4.1,hmac.conly gains new functions,digest.conly changesmalloctocallocfor the digest data and drops a null check beforeENGINE_finish, and theEVP_MD,EVP_MD_CTXandHMAC_CTXstructures do not change, so the behaviour is the same for either reading.EVP_MD(120 bytes),EVP_MD_CTX(48) andHMAC_CTX(288) follow evp.h and hmac.h and are checked withstatic_asserton size and field offsets. Theunsigned longflags are a 64-bit field, so the layout also holds on Windows. A context the title allocates, copies or reads (for example through theHMAC_sizemacro, which readsctx->md) stays valid.include/Api.hppdeclared the 20 functions with the unknown no-parameter signature. It now holds the three layouts (EvpMd,EvpMdContext,HmacContext) and the real signatures, andEvp.cppand the test include it instead of local declarations, as was done for theERRand OCSP functions.ONESHOT,CLEANED,REUSEandNO_INITwork as indigest.c. Digest data is reused when a context is initialised again with the same digest, freed and replaced when the digest changes, zeroed afterEVP_DigestFinal_ex, and zeroed and freed byEVP_MD_CTX_cleanup, which then clears the context.EVP_DigestInit_exreturns 0 and leaves the context without a digest, update function or data, so calling it again allocates again. LibreSSL keeps the digest with null data there, and a retry with the same digest runs its init on a null pointer. The context copy insideHMAC_Init_exandHMAC_Finaldoes the same when its allocation fails.HMAC_Init_exhashes keys longer than the block size and stores a key of exactly the block size unhashed, ashmac.cdoes (block < len). It re-initialises with the stored key when called without key or digest, and returns 0 when the digest changes without a key, for a negative key length and when no digest was ever set.HMAC_UpdateandHMAC_Finalreturn 0 without a digest.HMAC_Initclears the context first when both key and digest are given.HMACwith a null output buffer returns a static buffer.HMAC_CTX_cleanupzeroes all 288 bytes.EVP_MDfunction table, so anEVP_MDthe title defines itself also works.EVP_MDtables carry LibreSSL's NIDs, public key types, flags (0 for MD5,EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT= 0xc for SHA-256, SHA-384 and SHA-512), block sizes and context sizes fromm_md5.candm_sha1.c.signandverify(RSA_sign,RSA_verifyin LibreSSL) are null becauseRSA_signandRSA_verifystill throw.EVP_MD_CTX_createcontexts come from the guest heap (malloc,callocandfreeof libc), as LibreSSL uses the module's own libc.NotImplemented_nid_no_patch: anENGINEargument, and a context that holds anENGINEor anEVP_PKEY_CTX. Null contexts,EVP_DigestInit_exwithout a type on a context that already has a digest, and an update or final on a context without a digest throwstd::invalid_argument; LibreSSL does not check these and would crash.mainnow has the queue andLibreSslPutError_nid_no_patch, but no libSceLibreSsl export publishes to it yet; publishing LibreSSL's codes here (for exampleERR_R_MALLOC_FAILUREfromEVP_DigestInit_ex) is left for a follow-up and recorded in the debt entry.Export.cpp.Tested
Which head each run covers: everything under Linux is on the current head 4a465be92 (on
main0836dfdf4, 2026-10-31). The Windows run is on the earlier headb2583e3cf1and has not been repeated since.Linux (Ubuntu 24.04 container, GCC 13.3, Release,
BUILD_TESTING=ON; on 4a465be92; earlier rounds on f6b83d96e, 81733adac, 2b3f66e92, ca3740481, 5994190d7,b2583e3cf, 42d887da8, b55c473a9, 092935662, 6f2687c6a, db69a66cd and334de4dec):New
guest_libressl_evp: passed. For MD5, SHA-256, SHA-384 and SHA-512 it checks:EVP_MDfields (NID, public key type, size, flags, block and context size, required key types, function pointers), with flags 0 for MD5 and 0xc for SHA-256, SHA-384 and SHA-512;EVP_DigestInit_ex/Update/Final_exandEVP_Digeston the empty string,abcand a 1000-byte message, fed in chunks of 1, block - 1, block, block + 1 and 1000 bytes;key_lengthequals the block size and that the key is stored unhashed, plus the one-shotHMAC;HMAC_CTXfields (md,key_length, the key and its zero padding), re-initialisation without key and digest,HMAC_Init, and the one-shotHMACwith and without an output buffer;Finalandcleanup, the reuse of digest data, theNO_INITflag, a title-definedEVP_MD, a digest size over 64 returning 0, the HMAC refusals listed above and the invalid-argument throws (null contexts, an update or final without a digest,EVP_DigestInit_exwithout a type on a context that has one). TheENGINEandEVP_PKEY_CTXpaths are not asserted, as they throw only because that support is missing;ApplicationHeapRegister_nid_no_patchthat fails a chosen allocation and counts live blocks:EVP_DigestInit_exon a new context and when switching digest,EVP_MD_CTX_create,EVP_Digest,HMAC_Init_exfailing at each of its three allocations with a 4-byte and a 131-byte key, andHMAC. Each returns 0 or null, frees what it allocated and leaves no context with a digest but no data (an update or final on a context left without a digest throws), and the same call again gives the right digest or MAC. No block is left allocated at the end of the test.Expected values were computed with Python
hashlibandhmac; the SHA-2 HMAC values match RFC 4231. The block-size key MACs, the stored key length and key, and the flags of all four tables were also produced by LibreSSL 2.5.4's ownhmac.c,digest.c,m_sha1.c,m_md5.c,sha256.c,sha512.candmd5_dgst.c(commit 2e2c61206, built with a small host harness) and match. With the inner pad changed to 0x37, the key padding left out, theNO_INITearly return removed or the context clearing inHMAC_Initremoved, the test fails. It also fails when a key of exactly the block size is hashed (block <= len), whenEVP_MD_FLAG_DIGALGID_ABSENTis dropped from SHA-256 or set on MD5, and with the previousEVP_DigestInit_exorder (digest set before the allocation) or the previous context copy (digest kept when its allocation fails).All other libSceLibreSsl and libSceLibreSsl3 tests on
mainpassed:guest_libressl_digest,guest_libressl_des,guest_libressl_x509_errors,guest_libressl_runtime,guest_libressl_aes,guest_libressl_asn1,guest_libressl_asn1_allocator,guest_libressl_ex_data,guest_libressl_error,guest_libressl_ocsp,guest_libressl_stack,guest_libressl_ui,guest_libressl_rsa,guest_libressl_x509_store,guest_libressl_x509_store_allocator,guest_libressl_x509_store_context,guest_libressl_x509_lookup,guest_libressl_x509_lookup_allocator,guest_libressl_x509_verify_param,guest_libressl_x509_verify_param_allocator,guest_libressl_pkey_type,guest_libressl_cipher,guest_libressl_alert,guest_libressl_version,guest_libressl_crypto_locks,guest_libressl_object_text,guest_libressl_object_allocation,guest_libressl_protocol,guest_libressl_protocol_nid,guest_libressl3_digest,guest_libressl3_digest_context,guest_libressl3_asn1,guest_libressl3_cipher,guest_libressl3_key_reference,guest_libressl3_stack,guest_libressl3_x509_verify_paramandguest_crypto_memcmp(39/39 together withguest_libressl_evpandpatched_libraries). Their sources include the changedinclude/Api.hpp. The opt-inguest_libressl_oracle(ANYPS5_LIBRESSL_ORACLE) was not enabled; it compares the AES, digest, DES parity and error-string exports, not these functions.patched_libraries: passed.libSceLibreSsl.prxexports the same 394_nid_postfixnames as onmain.python3 tools/check_conventions.py --base upstream/main: passed.The mutants above (
block <= len, the SHA-256 flags, theEVP_DigestInit_exorder and theHMAC_Init_excontext copy) were rerun on 4a465be92 and each makesguest_libressl_evpfail.MinGW-w64 13.2 (
x86_64-w64-mingw32-g++-posix -std=gnu++20 -fsyntax-onlywith the UCRT defines) compiles every libSceLibreSsl and libSceLibreSsl3 source and LibreSSL test except the opt-in oracle test on 4a465be92 without errors.Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (
x86_64-ucrt-posix-seh, r7, the toolchain of the CI Windows job), CMake 4.3.1, Ninja 1.13.2, Release,BUILD_TESTING=ON, configured as in CI (cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, submodules withgit submodule update --init --recursive --depth 1):On
b2583e3cf(2026-10-25):libSceLibreSsl,libSceLibreSsl3, the LibreSSL test executables,guest_crypto_memcmp_testsandpatched_libraries_testsbuilt with no warning in any libSceLibreSsl file;Evp.cpp,Export.cppandGuestLibreSslEvp.cppalso compile without warnings with-Wall -Wextra.tools/check_test_registration.pypassed. The same 14 tests as on Linux passed. The unpatchedlibSceLibreSsl.prxexports the same 394_nid_postfixnames as the Linux build, including the 20, andguest_libressl_evp_tests.exeimports exactly these 20 from it. With a key of exactly the block size hashed (block <= len),guest_libressl_evpfails on Windows as well.Earlier rounds:
guest_libressl_evpand the digest tests passed on f6b83d96e and 81733adac.Not rerun on Windows since
b2583e3cf1. Since then the rebases (ontoc7b4878fa,750cf6cf4,04fefebe9,3679074dd,409fe6419,d4de5943fand0836dfdf4) only changed the conflicting lines next tomain's new code inCMakeLists.txt,include/Api.hpp,Export.cpp(stubs next tomain'sOBJ_cleanupandOBJ_createchanges) and the debt file, plus the debt entry's wording aboutRSA_signandRSA_verify; the third review only prepended the licence comment toEvp.cppand removed five assertions fromGuestLibreSslEvp.cpp; the code in both is otherwise the same as onb2583e3cf1. The MinGW-w64 syntax check above is the only Windows-toolchain check on 4a465be92.Not tested: any title or homebrew (no title using libSceLibreSsl is known).
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)src/Evp.cppexists and usesEvpMdandEvpMdContext)Duplicate and overlap check
main0836dfdf4and all 1694 open pull requests (heads fetched on 2026-10-31, 20:31 UTC). No other open PR adds, changes or removes any of the 20 functions: the diffs of all open PRs were searched for theEVP_md5/sha256/sha384/sha512,EVP_MD_size,EVP_MD_CTX_*,EVP_Digest*andHMAC*names and forEvpMd,EvpMdContext,HmacContextandHmacMaxBlock. Onmain, the digest, DES, X509 error string, RAND/CRYPTO, AES, error queue, OCSP string, cipher, ASN.1, CRYPTO locking, version, TLS alert, stack, UI_METHOD, RSA, X509_STORE_CTX, EVP_PKEY_type, ex_data, object text, protocol selection, X509_STORE, X509_LOOKUP, X509_VERIFY_PARAM, B250-2 and OBJ_create work, the LibreSSL oracle test and the libSceLibreSsl3 forwarding, stack, cipher, digest-context and X509_VERIFY_PARAM work changed libSceLibreSsl since its export stubs were added; none of it touches these functions. libSceLibreSsl3 now forwards its copies of these exports to libSceLibreSsl by name, and its tests pass on this head.add_librarysource list and test block ofCMakeLists.txt, the declarations afterLibreSslX509StoreContextininclude/Api.hppand the debt count and neighbouring entries. Agreed order with its author: this PR lands first, #10395 records Depends on: #9448 and rebases after it. Both heads merged locally with both sides kept (Evp.cppthenEc.cpp, both test blocks, both declaration blocks, count 263 = the stubs left) pass all 40 LibreSSL tests incl.guest_libressl_evpandguest_libressl_ecandcheck_conventions.pyon Linux.EvpMdandEvpMdContextfrom this PR and builds its mixed libSceLibreSsl/libSceLibreSsl3 test only whensrc/Evp.cppexists. Its current head conflicts withmainon its own; on its merge with b55c473a9 (an earlier head with the same code)guest_libressl3_evp,guest_libressl3_mixed_evpand the other libSceLibreSsl3 tests passed.b2583e3cf) through a merge of #11151; #11151's current head no longer contains it.main. 66 other open PRs change libSceLibreSsl or libSceLibreSsl3: 15 already conflict withmain(among them #9446 and #10960) and 14 merge cleanly with this branch. The other 37 (among them #10395 and #10834) merge withmainbut conflict with this branch, nearly all in the debt count or neighbouring entries and most also in theCMakeLists.txtregistration lines or the declarations ininclude/Api.hpp; #10834, #11128, #11176 and #13760 also conflict inExport.cppnext to the stubs removed here. None of them implements one of the 20 (#13831 only namesPKCS5_PBKDF2_HMACin libSceLibreSsl3); as the LibreSSL PRs do with each other, whichever merges later needs a small rebase.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:36:48Z.
Review: CHANGES_REQUESTED
The test coverage is unusually thorough: digest/HMAC vectors include chunk boundaries and RFC 4231 cases, context layouts and reuse are checked, and Linux plus MinGW runs are reported. I found one failure-path defect in
DigestInit: whenmalloc_nid_postfixreturns null, it returns 0 after assigningcontext.digest = type, but leavescontext.mdDatanull. A retry with the same digest skips allocation becausecontext.digest == typeand then callstype->init(&context)with nullmdData; this can crash. Please leave the context retryable (or otherwise fail explicitly without publishing a partially initialized digest) and add coverage for allocator failure/retry if the test harness allows it.The branch is also based on
a6dcea927, 47 commits behind current main (110a18ba01); the described checks and overlap scan used the earlier base/open-PR snapshot. Please update/rebase and rerun conventions, overlap/debt and focused digest/HMAC tests on current main, preserving the declared #9025 dependency and the CMake/debt merge coordination with #9033/#9041/#9047. Hosted full Linux/Windows product jobs are skipped; the author-reported local Linux and Windows tests are useful but not hosted validation. I did not run local crypto tests.Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-22T11:27:07Z.
@MarcZX10 Thanks, fixed in f6b83d96e.
EVP_DigestInit_exnow allocates the digest data before it stores the digest. When the allocation fails it returns 0 and leavesdigest,updateandmd_datanull, so a retry with the same digest allocates again instead of runninginiton a null pointer. The context copy used byHMAC_Init_exandHMAC_Finalclearsdigestandupdatethe same way when its allocation fails. The debt entry records this as a difference from LibreSSL, which keeps the digest.guest_libressl_evpnow registers a test allocator that fails a chosen allocation and counts live blocks. It covers failure and retry forEVP_DigestInit_ex(new context and digest switch),EVP_MD_CTX_create,EVP_Digest,HMAC_Init_exfailing at each of its three allocations (4-byte key and 131-byte key) andHMAC, and checks that nothing is left allocated. With either old path restored, the test fails.main4777caca3(includes110a18ba01), still stacked on #9025 at52a439bc7. Conventions,guest_libressl_evp,guest_libressl_digestandpatched_librariespass on Linux. I rescanned all 1237 open PRs: no other PR touches these 20 functions. The conflicts with #9033, #9041, #9047, #9446 and #9529 are still only theadd_library/test block in the CMakeLists and the debt count line, the same places as #9025's; #9571 conflicts only in the debt file and #9535/#9536 not at all. The description is updated. Windows has not been re-run on the new head yet.Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-22T12:59:12Z.
Rebased onto main
4777caca3(includes110a18ba01), still stacked on #9025 at52a439bc7. The conventions check passes, and guest_libressl_evp, guest_libressl_digest and patched_libraries pass on Linux and on Windows (MinGW-w64 GCC 15.2.0). I rescanned all 1237 open PRs: no other PR touches these 20 functions. The conflicts with #9033, #9041, #9047, #9446 and #9529 are still only the add_library/test block in the CMakeLists and the debt count line, the same places as #9025's; #9571 conflicts only in the debt file and #9535/#9536 not at all. The description is updated.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T18:13:05Z.
Review: APPROVED
Checked head 5994190d7094 merged locally onto main
7a25f87620(PR base2fade7ef4e). The rebase only conflicts in the CMakeLists add_library/test block and the debt file; I kept both sides and set the pending count to 325, which matches the stubs left in Export.cpp.What I ran (Linux, GCC 13.3, Release):
guest_libressl_evp,guest_libressl_digest,guest_libressl_errorandguest_libressl_cipheron the merged tree: pass. The new test on main's stubs aborts atEVP_md5_nid_postfix not implemented.check_conventions.py --base origin/mainon the merged tree: pass.EVP_MD120,EVP_MD_CTX48,HMAC_CTX288, and every field offset matches your static_asserts. At runtime the md5/sha256/sha384/sha512 tables match 2.5.4's (NIDs, flags 0 and 0xc, required key types, block and ctx sizes). Onlysign/verifydiffer (null vsRSA_sign/RSA_verify), which the debt entry already says.HMAC_Init. Digests, MACs, return values and context state (flags,key_length, all 128 key bytes) all match. A deliberate wrong-digest run of the harness fails every case, so the comparison works.digest.candhmac.cnext to Evp.cpp. The alloc-failure retry from the earlier review is fixed, and the other differences are throws on paths where 2.5.4 would crash, or are in the debt entry.Looks good to me. Two small test suggestions, since the differential covers these but
guest_libressl_evpdoesn't:block < lengthtoblock <= lengthinHmacInitand the test still passes. A 64-byte key with SHA-256 or a 128-byte key with SHA-512 would catch it.EVP_MDflags at offset 16 aren't asserted, so anEVP_sha256table with flags 0 passes. Checking 0 for MD5 and 0xc for the SHA-2 tables would pin it.Not checked: anything on a console or with a title. I called the host exports directly, not through the loader.
Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-26T06:01:46Z.
Review: APPROVED
Re-checked head
b2583e3cf1(baseae7d59258e) since my approval on 5994190d70. Merged locally onto main14c99b52ac: no conflicts.What I ran (Linux, GCC 13.3, Release):
guest_libressl_evp,guest_libressl_digest,guest_libressl_errorandguest_libressl_cipheron the merged tree: pass.public_headers*(65 tests): pass.check_conventions.py --base origin/main: pass.block < lengthtoblock <= lengthin the HMAC key setup (Evp.cpp:224) now fails at the block-size key MAC (test line 367). Setting SHA-256's flags to 0 (Evp.cpp:80) fails at the flags check (line 211). Both pass again after reverting.hmacand LibreSSL 2.5.4'sHMAC(). Flags 0 for MD5 and 0xc for SHA-2 match 2.5.4m_md5.candm_sha1.c.sign/verify). A field probe of theApi.hppstructs against 2.5.4evp.h/hmac.h: all 27 offsets and sizes match (120, 48, 288).The
411c71drefactor: the struct block moves toApi.hppunchanged and the static_asserts stay in Evp.cpp. The function bodies only gainstatic_casts. The 20 declarations match the 2.5.4 prototypes, and none of the 20 is left as a stub in Export.cpp. The debt count goes from 342 to 322, which matches.MarcZX10's two points from 10-22 (the DigestInit retry after a failed alloc, and the rebase) both look fixed on this head.
Not checked: anything on a console or with a title. I called the host exports directly, not through the loader.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T23:03:56Z.
Review: APPROVED
Re-checked — new head, looks good
The allocation-failure retry defect is fixed on head
6f2687c6a22fe382e5a1df3636cbe9d032450d6b, and the current LibreSSL PR order is now recorded with #9448 first and #10395 depending on it.What I ran
Reviewed head
6f2687c6a22fe382e5a1df3636cbe9d032450d6bagainst PR base7a225bb303c1a452057000f32b70b56486625fd6; the merge preview against currentmaindc38224141e0b6096007b876b4c42fc9144d11b8is clean. I inspected the six changed files and the updated dependency declaration on #10395. Local checks ran in the Linux workspace; no compiler or build was invoked.Results
main1a2a3d33b7f74c53a96b87d1fbf85e091191c99e6f2687c6a22fe382e5a1df3636cbe9d032450d6bUnedited local check output
Findings
Blocking
None found in the reviewed update. The retry path now clears the digest state when allocation fails, and the added test covers failure followed by successful retry. The conflict with current
mainis resolved, and #10395 now recordsDepends on: #9448.Non-blocking
The Linux test suite and combined #10395 run are author-reported rather than independently rerun. The hosted workflow skipped Linux and the specialized jobs.
Sources
Not verified
I did not run a local LibreSSL build or tests, Windows tests, a console test, or the combined #9448/#10395 test suite. The Windows hosted job passed on the exact reviewed head; the Linux and combined-suite results above are author-reported.