Skip to content

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

Merged
oneandonlydean merged 9 commits into
boykopovar:mainfrom
Ducengarth:feat/libressl3-chacha20-ciphers
Nov 9, 2026
Merged

oneandonlydean merged 9 commits into
boykopovar:mainfrom
Ducengarth:feat/libressl3-chacha20-ciphers

Conversation

@Ducengarth

@Ducengarth Ducengarth commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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

@JJenkx

JJenkx commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 7cd1d9691a, merged onto main 6a120ae1b7 (c1b6880014, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_libressl3_chacha passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Progress report (df088d6 → 84caebd)

📈 System libraries: 33.27% (+0.02%, +8 implemented functions)

✅ 8 implemented
Library Name
libSceLibreSsl3 ChaCha20Cipher
libSceLibreSsl3 ChaCha20Init
libSceLibreSsl3 ChaCha20Poly1305Cipher
libSceLibreSsl3 ChaCha20Poly1305Cleanup
libSceLibreSsl3 ChaCha20Poly1305Control
libSceLibreSsl3 ChaCha20Poly1305Init
libSceLibreSsl3 EVP_chacha20_nid_postfix
libSceLibreSsl3 EVP_chacha20_poly1305_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl3 107/284 (37.68%) 115/290 (39.66%) +1.98%

@github-actions github-actions Bot added build docs prx needs rebase Conflicts with main, rebase needed labels Nov 6, 2026
@Ducengarth
Ducengarth force-pushed the feat/libressl3-chacha20-ciphers branch from 7cd1d96 to ec51479 Compare November 6, 2026 01:23
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 6, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 6, 2026
@Ducengarth
Ducengarth force-pushed the feat/libressl3-chacha20-ciphers branch from f0a27c2 to c85e2fa Compare November 7, 2026 00:59
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 1384 Allocate
PR 15591 docs/dev/debt/libSceLibreSsl3.md L76
PR 15622 Align
Allocate
PosixAlign
Realign
Reallocate
PR 15625 Align
Allocate
PosixAlign
Realign
Reallocate
PR 15627 Align
Allocate
PosixAlign
Realign
Reallocate
PR 15765 Align
Allocate
PosixAlign
Realign
Reallocate
PR 15831 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp L163-175
core/libs/prx/libSceLibreSsl3/include/Api.hpp L299-301
docs/dev/debt/libSceLibreSsl3.md L76, L88-90
PR 16543 EVP_chacha20
libreSsl3Forward_EVP_chacha20_nid_no_patch
PR 16586 docs/dev/debt/libSceLibreSsl3.md L76
PR 16588 docs/dev/debt/libSceLibreSsl3.md L76
PR 16589 docs/dev/debt/libSceLibreSsl3.md L76
PR 16649 docs/dev/debt/libSceLibreSsl.md L59
PR 16689 docs/dev/debt/libSceLibreSsl3.md L76
PR 16819 docs/dev/debt/libSceLibreSsl3.md L76 Align
Allocate
Calloc
Free
PosixAlign
Realign
Reallocate
PR 16971 Allocate
Reallocate
PR 17124 docs/dev/debt/libSceLibreSsl3.md L76
PR 17235 docs/dev/debt/libSceLibreSsl3.md L76
PR 17236 docs/dev/debt/libSceLibreSsl3.md L76
PR 17237 docs/dev/debt/libSceLibreSsl3.md L76
PR 17238 docs/dev/debt/libSceLibreSsl3.md L76
PR 17240 docs/dev/debt/libSceLibreSsl3.md L76
PR 17241 docs/dev/debt/libSceLibreSsl3.md L76
PR 17242 docs/dev/debt/libSceLibreSsl3.md L76
PR 17243 docs/dev/debt/libSceLibreSsl3.md L76
PR 17246 docs/dev/debt/libSceLibreSsl3.md L76
PR 17249 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp L168-179
docs/dev/debt/libSceLibreSsl3.md L76, L89-91
PR 17250 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp L168-179
docs/dev/debt/libSceLibreSsl3.md L76, L89-91
PR 17528 docs/dev/debt/libSceLibreSsl3.md L76
PR 17544 docs/dev/debt/libSceLibreSsl3.md L76

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 11146 docs/dev/debt/libSceLibreSsl.md
PR 11176 docs/dev/debt/libSceLibreSsl.md
PR 11466 docs/dev/debt/libSceLibreSsl.md
PR 12315 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 12729 docs/dev/debt/libSceLibreSsl.md
PR 12730 docs/dev/debt/libSceLibreSsl.md
PR 13266 docs/dev/debt/libSceLibreSsl.md
PR 13285 docs/dev/debt/libSceLibreSsl.md
PR 13546 docs/dev/debt/libSceLibreSsl.md
PR 13547 docs/dev/debt/libSceLibreSsl.md
PR 13877 docs/dev/debt/libSceLibreSsl.md
PR 14115 docs/dev/debt/libSceLibreSsl.md
PR 14212 docs/dev/debt/libSceLibreSsl.md
docs/dev/debt/libSceLibreSsl3.md
PR 14465 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14890 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15163 core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15480 docs/dev/debt/libSceLibreSsl.md
PR 15514 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15544 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15616 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15618 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15620 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15622 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15623 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15625 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15627 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15628 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15629 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15765 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16241 docs/dev/debt/libSceLibreSsl.md
PR 16529 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
docs/dev/debt/libSceLibreSsl3.md
PR 16535 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16543 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
docs/dev/debt/libSceLibreSsl3.md
PR 16742 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16918 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16926 core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16977 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16979 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16980 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16981 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16996 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 7, 2026
…05 ciphers

Implement EVP_chacha20 and EVP_chacha20_poly1305 after LibreSSL 4.0.0
e_chacha.c and e_chacha20poly1305.c: the 64-bit counter IV layout, the
streaming keystream, the left-padded AEAD nonce and the AEAD controls,
with Poly1305 from mbedtls. They run inside the forwarded libSceLibreSsl
EVP_Cipher* functions, which now zero the cipher data as LibreSSL 4.0.0
does. Record the unverified console behaviour and the zero-length update
gap as debt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ducengarth
Ducengarth force-pushed the feat/libressl3-chacha20-ciphers branch from 04ceab7 to d3f99bf Compare November 7, 2026 11:14
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 7, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 7, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 7, 2026
…/libressl3-chacha20-ciphers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 7, 2026
Ducengarth and others added 4 commits November 8, 2026 14:18
…/libressl3-chacha20-ciphers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/libressl3-chacha20-ciphers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/libressl3-chacha20-ciphers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/libressl3-chacha20-ciphers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oneandonlydean oneandonlydean self-assigned this Nov 9, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 9, 2026
@oneandonlydean
oneandonlydean merged commit 046df6f into boykopovar:main Nov 9, 2026
13 checks passed
@Ducengarth
Ducengarth deleted the feat/libressl3-chacha20-ciphers branch November 26, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants