feat(libSceLibreSsl3): implement the EVP ChaCha20 and ChaCha20-Poly1305 ciphers #16249
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16249"
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 Ducengarth on 2026-11-05T23:43:29Z.
What
Implement
EVP_chacha20andEVP_chacha20_poly1305inlibSceLibreSsl3, replacing their parameterless throwing placeholders inAdditionalExports.cpp. Source: LibreSSL 4.0.0 atce488b78, the revision the library's debt file already uses:evp/e_chacha.c,evp/e_chacha20poly1305.c,chacha/chacha.candchacha/chacha-merged.c.EVP_chacha20(NID 937): stream cipher, 32-byte key, 16-byte IV. As in LibreSSL, IV bytes 0 to 7 are a 64-bit little-endian block counter and bytes 8 to 15 the nonce, which differs from the IETF layout. The keystream left over from a partial block carries across updates, and the counter carries from the low word into the high word. A key-only init keeps the counter, and an IV-only init restarts the keystream.EVP_chacha20_poly1305(NID 967): RFC 8439 AEAD through the EVP cipher interface, as LibreSSL 3.6 and later provide it. A null output means AAD, a null input means final, and AAD after text is rejected. A nonce of 1 to 11 bytes is left-padded with zeros to 12. The controls areEVP_CTRL_INIT,AEAD_GET_IVLEN,AEAD_SET_IVLEN,AEAD_SET_TAG(decrypt only, 1 to 16 bytes, so truncated tags work),AEAD_GET_TAG(encrypt only) andAEAD_SET_IV_FIXED, with the same return values. The tag check is constant-time. After a final, the Poly1305 state is zeroed as LibreSSL'sCRYPTO_poly1305_finishleaves it.ctx_size132 and 384, the LP64 sizes ofChaCha_ctxandstruct chacha20_poly1305_ctx. They use libSceLibreSsl's 88-byteEVP_CIPHER, whose first 80 bytes are the 4.0.0 layout libSceLibreSsl3'sCipher.cppreads, so the existingEVP_CIPHER_key_lengthandEVP_CIPHER_iv_lengthwork on them.mbedcrypto, already linked by libSceLibreSsl). libSceLibreSsl3 links it with--exclude-libs,ALLon MinGW and Linux, as libSceLibreSsl does; without that flag, 21mbedtls_*symbols would become guest-visible exports.Both ciphers run inside libSceLibreSsl's
EVP_CipherInit_ex,EVP_*UpdateandEVP_*Final_ex, which libSceLibreSsl3 forwards. One change there:EVP_CipherInit_exnow takes the cipher data fromcallocinstead ofmalloc. LibreSSL 4.0.0 zeroes it, and a ChaCha20 init with a key and no IV relies on that for a zero counter and nonce. In 2.5.4 the contents were unspecified, so this is still valid for libSceLibreSsl's own ciphers.Known gap, recorded in
docs/dev/debt/libSceLibreSsl3.md: the forwarded 2.5.4 update hands a zero-length update to the cipher, where LibreSSL 4.0.0'sEVP_EncryptUpdateandEVP_DecryptUpdatereturn 1 without calling it. Inside the cipher, a zero-length update with a null input looks exactly like the final call. SoEVP_EncryptUpdate(ctx, out, &n, NULL, 0)onEVP_chacha20_poly1305finishes the message early. The same already happens with AES-GCM through libSceLibreSsl3. A zero-length update with an input, or a negative length, now leaves the state alone and returns as 4.0.0 does. A full fix means giving libSceLibreSsl3 its ownEVP_EncryptUpdate,EVP_DecryptUpdateandEVP_CipherUpdatewith the 4.0.0 prologue. That changes how a NID shared with libSceLibreSsl is resolved on each platform, so it belongs in its own PR.EVP_aes_128_ccmand theEVP_aes_*_wrapciphers stay as they are. The wrap ciphers build onAES_wrap_keyandAES_unwrap_key(#15854, #14612) and onEVP_CIPHER_CTX_set_flags(#15163).No title's trace or disassembly was used, and neither export was measured on a PS5.
pr_overlapreports no pull request with the same exports or new files. The "same exports" it lists (Allocate,Align,Calloc,Free,PosixAlign,Realign,Reallocate) are the test's file-local guest-heap callbacks, which other tests also define. The remaining hits are line conflicts:libSceLibreSsl3.hpp.CMakeLists.txt.Whichever merges second resolves them; I will rebase this one as needed. 62 pull requests that conflict with
maincould not be compared by the tool. I scanned all of them, and every open pull request that toucheslibSceLibreSsl3orEvpCipherCtx.cpp. None addsEVP_chacha20,EVP_chacha20_poly1305or ChaCha code, or changes the cipher-data allocation line.Tested
Windows 11, MinGW-w64 GCC (WinLibs), Release build. No title or homebrew, and not built on Linux or macOS.
guest_libressl3_chacha(new) passes. It drives both ciphers through the forwarded EVP functions and compares them with mbedtls's independent IETF ChaCha20 and ChaCha20-Poly1305:1ae10b59…).0xffffffff.SET_IV_FIXED, every control's in-range and out-of-range return values, and AAD after text on both sides.EVP_CIPHER_key_lengthandEVP_CIPHER_iv_length.guest_libressl*andguest_crypto_memcmptests pass, including libSceLibreSsl's after thecallocchange.objdump -ponlibSceLibreSsl3.prxlists both names and nombedtls_*symbol.python3 tools/check_conventions.py --base upstream/mainpasses.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)🤖 Generated with Claude Code