feat(libSceLibreSsl3): implement the EVP ChaCha20 and ChaCha20-Poly1305 ciphers #16249

Merged
Ducengarth merged 9 commits from upstream-pr/16249 into main 2026-11-09 01:47:09 +00:00
Ducengarth commented 2026-11-05 23:43:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ducengarth on 2026-11-05T23:43:29Z.


What

Implement EVP_chacha20 and EVP_chacha20_poly1305 in libSceLibreSsl3, replacing their parameterless throwing placeholders in AdditionalExports.cpp. Source: LibreSSL 4.0.0 at ce488b78, the revision the library's debt file already uses: evp/e_chacha.c, evp/e_chacha20poly1305.c, chacha/chacha.c and chacha/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 are EVP_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) and AEAD_SET_IV_FIXED, with the same return values. The tag check is constant-time. After a final, the Poly1305 state is zeroed as LibreSSL's CRYPTO_poly1305_finish leaves it.
  • The cipher objects copy LibreSSL's fields: block size 1, key and IV lengths, flags, and ctx_size 132 and 384, the LP64 sizes of ChaCha_ctx and struct chacha20_poly1305_ctx. They use libSceLibreSsl's 88-byte EVP_CIPHER, whose first 80 bytes are the 4.0.0 layout libSceLibreSsl3's Cipher.cpp reads, so the existing EVP_CIPHER_key_length and EVP_CIPHER_iv_length work on them.
  • The ChaCha20 block function is written here, because LibreSSL's 64-bit counter and leftover-keystream state differ from mbedtls's ChaCha20. Poly1305 comes from the existing mbedtls submodule (mbedcrypto, already linked by libSceLibreSsl). libSceLibreSsl3 links it with --exclude-libs,ALL on MinGW and Linux, as libSceLibreSsl does; without that flag, 21 mbedtls_* symbols would become guest-visible exports.

Both ciphers run inside libSceLibreSsl's EVP_CipherInit_ex, EVP_*Update and EVP_*Final_ex, which libSceLibreSsl3 forwards. One change there: EVP_CipherInit_ex now takes the cipher data from calloc instead of malloc. 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's EVP_EncryptUpdate and EVP_DecryptUpdate return 1 without calling it. Inside the cipher, a zero-length update with a null input looks exactly like the final call. So EVP_EncryptUpdate(ctx, out, &n, NULL, 0) on EVP_chacha20_poly1305 finishes 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 own EVP_EncryptUpdate, EVP_DecryptUpdate and EVP_CipherUpdate with 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_ccm and the EVP_aes_*_wrap ciphers stay as they are. The wrap ciphers build on AES_wrap_key and AES_unwrap_key (#15854, #14612) and on EVP_CIPHER_CTX_set_flags (#15163).

No title's trace or disassembly was used, and neither export was measured on a PS5.

pr_overlap reports 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:

Whichever merges second resolves them; I will rebase this one as needed. 62 pull requests that conflict with main could not be compared by the tool. I scanned all of them, and every open pull request that touches libSceLibreSsl3 or EvpCipherCtx.cpp. None adds EVP_chacha20, EVP_chacha20_poly1305 or 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:
    • Vectors: RFC 8439 §2.4.2 (ciphertext prefix) and §2.8.2 (tag 1ae10b59…).
    • ChaCha20 streaming: 200 random messages in random chunks, and the 64-bit counter carry at 0xffffffff.
    • ChaCha20 re-init: IV-only and key-only re-init, and a key-only first init.
    • ChaCha20-Poly1305 random: 200 messages with split AAD and text.
    • ChaCha20-Poly1305 decryption: the right tag, an 8-byte tag, a flipped tag bit, flipped ciphertext, wrong AAD and no tag set.
    • ChaCha20-Poly1305 nonces and controls: an 8-byte padded nonce, SET_IV_FIXED, every control's in-range and out-of-range return values, and AAD after text on both sides.
    • ChaCha20-Poly1305 edge cases: an empty message, zero-length and negative updates, and a second final after a failed one, which accepts the all-zero tag as LibreSSL does.
    • Cipher objects: the fields, read directly and through libSceLibreSsl3's EVP_CIPHER_key_length and EVP_CIPHER_iv_length.
  • Eight deliberate bugs each make the test fail with a distinct check: no counter carry, counter and nonce swapped, a nonce not zero-padded, no pad16, the tag not checked, AAD after text accepted, no Poly1305 reset after final, and non-zeroed cipher data.
  • All 102 guest_libressl* and guest_crypto_memcmp tests pass, including libSceLibreSsl's after the calloc change.
  • objdump -p on libSceLibreSsl3.prx lists both names and no mbedtls_* symbol.
  • python3 tools/check_conventions.py --base upstream/main passes.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5180423841 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16249). Originally posted by [Ducengarth](https://github.com/Ducengarth) on 2026-11-05T23:43:29Z. --- ### What Implement `EVP_chacha20` and `EVP_chacha20_poly1305` in `libSceLibreSsl3`, replacing their parameterless throwing placeholders in `AdditionalExports.cpp`. Source: LibreSSL 4.0.0 at [`ce488b78`](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto), the revision the library's debt file already uses: `evp/e_chacha.c`, `evp/e_chacha20poly1305.c`, `chacha/chacha.c` and `chacha/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 are `EVP_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) and `AEAD_SET_IV_FIXED`, with the same return values. The tag check is constant-time. After a final, the Poly1305 state is zeroed as LibreSSL's `CRYPTO_poly1305_finish` leaves it. - The cipher objects copy LibreSSL's fields: block size 1, key and IV lengths, flags, and `ctx_size` 132 and 384, the LP64 sizes of `ChaCha_ctx` and `struct chacha20_poly1305_ctx`. They use libSceLibreSsl's 88-byte `EVP_CIPHER`, whose first 80 bytes are the 4.0.0 layout libSceLibreSsl3's `Cipher.cpp` reads, so the existing `EVP_CIPHER_key_length` and `EVP_CIPHER_iv_length` work on them. - The ChaCha20 block function is written here, because LibreSSL's 64-bit counter and leftover-keystream state differ from mbedtls's ChaCha20. Poly1305 comes from the existing mbedtls submodule (`mbedcrypto`, already linked by libSceLibreSsl). libSceLibreSsl3 links it with `--exclude-libs,ALL` on MinGW and Linux, as libSceLibreSsl does; without that flag, 21 `mbedtls_*` symbols would become guest-visible exports. Both ciphers run inside libSceLibreSsl's `EVP_CipherInit_ex`, `EVP_*Update` and `EVP_*Final_ex`, which libSceLibreSsl3 forwards. One change there: `EVP_CipherInit_ex` now takes the cipher data from `calloc` instead of `malloc`. 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's `EVP_EncryptUpdate` and `EVP_DecryptUpdate` return 1 without calling it. Inside the cipher, a zero-length update with a null input looks exactly like the final call. So `EVP_EncryptUpdate(ctx, out, &n, NULL, 0)` on `EVP_chacha20_poly1305` finishes 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 own `EVP_EncryptUpdate`, `EVP_DecryptUpdate` and `EVP_CipherUpdate` with 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_ccm` and the `EVP_aes_*_wrap` ciphers stay as they are. The wrap ciphers build on `AES_wrap_key` and `AES_unwrap_key` (#15854, #14612) and on `EVP_CIPHER_CTX_set_flags` (#15163). No title's trace or disassembly was used, and neither export was measured on a PS5. `pr_overlap` reports 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: - #14483, #14890, #15289, #15303, #15449, #15591 and #15765 edit the same debt lines or `libSceLibreSsl3.hpp`. - #16186 edits the same place in `CMakeLists.txt`. Whichever merges second resolves them; I will rebase this one as needed. 62 pull requests that conflict with `main` could not be compared by the tool. I scanned all of them, and every open pull request that touches `libSceLibreSsl3` or `EvpCipherCtx.cpp`. None adds `EVP_chacha20`, `EVP_chacha20_poly1305` or ChaCha code, or changes the cipher-data allocation line. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> 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: - **Vectors**: RFC 8439 §2.4.2 (ciphertext prefix) and §2.8.2 (tag `1ae10b59…`). - **ChaCha20 streaming**: 200 random messages in random chunks, and the 64-bit counter carry at `0xffffffff`. - **ChaCha20 re-init**: IV-only and key-only re-init, and a key-only first init. - **ChaCha20-Poly1305 random**: 200 messages with split AAD and text. - **ChaCha20-Poly1305 decryption**: the right tag, an 8-byte tag, a flipped tag bit, flipped ciphertext, wrong AAD and no tag set. - **ChaCha20-Poly1305 nonces and controls**: an 8-byte padded nonce, `SET_IV_FIXED`, every control's in-range and out-of-range return values, and AAD after text on both sides. - **ChaCha20-Poly1305 edge cases**: an empty message, zero-length and negative updates, and a second final after a failed one, which accepts the all-zero tag as LibreSSL does. - **Cipher objects**: the fields, read directly and through libSceLibreSsl3's `EVP_CIPHER_key_length` and `EVP_CIPHER_iv_length`. - Eight deliberate bugs each make the test fail with a distinct check: no counter carry, counter and nonce swapped, a nonce not zero-padded, no pad16, the tag not checked, AAD after text accepted, no Poly1305 reset after final, and non-zeroed cipher data. - All 102 `guest_libressl*` and `guest_crypto_memcmp` tests pass, including libSceLibreSsl's after the `calloc` change. - `objdump -p` on `libSceLibreSsl3.prx` lists both names and no `mbedtls_*` symbol. - `python3 tools/check_conventions.py --base upstream/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!16249
No description provided.