feat(libSceLibreSsl3): implement the EVP AES-128-CCM cipher #16255

Merged
Ducengarth merged 10 commits from upstream-pr/16255 into main 2026-11-09 02:35:24 +00:00
Ducengarth commented 2026-11-06 00:54:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ducengarth on 2026-11-06T00:54:45Z.


What

Implement EVP_aes_128_ccm (NID 896, uM9UnC8KFjY) in libSceLibreSsl3, replacing its parameterless throwing placeholder in AdditionalExports.cpp. Source: LibreSSL 4.0.0 at ce488b78, the revision the library's debt file already uses: evp/e_aes.c (aes_ccm_ctrl, aes_ccm_init_key, aes_ccm_cipher) and modes/ccm128.c.

  • CRYPTO_ccm128_* is ported byte for byte over the parent's AES_set_encrypt_key and AES_encrypt, in the same way libSceLibreSsl's EvpAes.cpp ports gcm128.c. mbedtls's CCM is not used, because it rejects 0xFF00 bytes of AAD or more and does not reproduce ccm128.c's behaviour:
    • L and M are encoded in the nonce flags byte when the key is set, so a later EVP_CTRL_CCM_SET_L or SET_TAG changes the nonce length or tag length the EVP layer passes, not the encoding.
    • A failed encrypt leaves the flags byte without M.
    • Only one AAD call per message is supported.
  • The EVP flow matches 4.0.0:
    • A null output with a null input sets the message length; a null output with an input is the AAD, and is rejected before the length.
    • A null input is the final call, which writes nothing.
    • A decrypt checks the tag in the update, zeroes the output when the tag is wrong, and leaves the final call failing, as in LibreSSL.
  • The controls are EVP_CTRL_INIT (L 8, M 12), AEAD_GET_IVLEN (15 - L), AEAD_SET_IVLEN, CCM_SET_L, CCM_SET_TAG (even, 4 to 16; a tag only when decrypting), CCM_GET_TAG (encrypt only, after the message) and EVP_CTRL_COPY (rebinds the key schedule pointer), with the same return values.
  • The cipher object copies LibreSSL's fields: block size 1, key length 16, IV length 12, flags 0x501477 and ctx_size 336, the LP64 size of EVP_AES_CCM_CTX. It uses libSceLibreSsl's 88-byte EVP_CIPHER, so the existing EVP_CIPHER_key_length and EVP_CIPHER_iv_length work on it.

It runs inside libSceLibreSsl's EVP_CipherInit_ex, EVP_*Update and EVP_*Final_ex, which libSceLibreSsl3 forwards. For CCM, 4.0.0's EVP_*Update passes a zero-length update to the cipher, as 2.5.4 does, so the length-setting call works unchanged. One change there: EVP_CipherInit_ex now takes the cipher data from calloc instead of malloc. LibreSSL 4.0.0 zeroes it, and an IV-only init before any key relies on that: with a zero flags byte, the length call fails as in LibreSSL instead of reading garbage. This is the same one-line change as in #16249. In 2.5.4 the contents were unspecified, so it is still valid for libSceLibreSsl's own ciphers.

Known gap, recorded in docs/dev/debt/libSceLibreSsl3.md: when the cipher fails, the forwarded 2.5.4 EVP_EncryptUpdate, EVP_EncryptFinal_ex and encrypting EVP_Cipher* calls leave *outl unset and accept a null outl, where 4.0.0 sets it to 0 first. The decrypt calls already match. A negative length returns 0 with the state untouched, as in 4.0.0.

EVP_aes_192_ccm and EVP_aes_256_ccm are not among libSceLibreSsl3's exports, so they are not added.

No title's trace or disassembly was used, and the export was not 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:

  • #14890, #15031, #15289, #15303 and #15591 edit the export inventory count in the debt file.
  • #16186 and #16249 edit the same places in CMakeLists.txt and the debt file. #16249 also makes the same calloc change, so whichever merges second resolves only the neighbouring lines.

I will rebase this one as needed. 65 pull requests that conflict with main could not be compared by the tool. I scanned the diffs of all 1892 open pull requests for aes_128_ccm, ccm128 and CCM helpers, and none adds them.

Tested

Windows 11, MinGW-w64 GCC (WinLibs), Release build. No title or homebrew, and not built on Linux or macOS.

  • guest_libressl3_aes_ccm (new) passes. It drives the cipher through the forwarded EVP functions:
    • Vectors: NIST SP 800-38C examples C.1 to C.4, sealed and opened. C.4 has 65536 bytes of AAD, which exercises the 0xFFFE length encoding.
    • Random, against mbedtls's independent CCM: 400 messages over every L (2 to 8) and even M (4 to 16), AAD up to 0xFEFF bytes, ten 65535-byte messages with L = 2, and empty AAD and messages. The length is set with and without the explicit call, using SET_IVLEN or SET_L. Each message is also opened with a flipped tag bit, ciphertext and AAD, and the output must be zeroed.
    • Defaults: the 7-byte nonce and 12-byte tag of an unconfigured context.
    • Controls: every control's in-range and out-of-range return values, including SET_IVLEN with INT_MIN and INT_MAX.
    • Parameters fixed at the key: a tag length set after the key, and a longer or shorter nonce length set after the key.
    • Sequencing: no key and no IV, an IV only, AAD before the length, zero and negative lengths, a length mismatch, a second update, in-place encryption and decryption, and a decrypt without a tag.
    • Empty message, and a final call without an update.
    • EVP_CTRL_COPY: a copied context still encrypts after the source is freed.
    • Cipher object: the fields, read directly and through EVP_CIPHER_key_length and EVP_CIPHER_iv_length.
    • The test's guest malloc fills memory with 0xFF so that the calloc dependence is checked.
  • Of 16 deliberate bugs, the test catches 14. The other two are equivalent at guest sizes: they only differ for messages of 2^32 blocks or more.
  • All 102 guest_libressl* and guest_crypto_memcmp tests pass, including libSceLibreSsl's after the calloc change.
  • objdump -p on libSceLibreSsl3.prx lists EVP_aes_128_ccm_nid_postfix 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:5181089967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16255). Originally posted by [Ducengarth](https://github.com/Ducengarth) on 2026-11-06T00:54:45Z. --- ### What Implement `EVP_aes_128_ccm` (NID 896, `uM9UnC8KFjY`) in `libSceLibreSsl3`, replacing its parameterless throwing placeholder 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_aes.c` (`aes_ccm_ctrl`, `aes_ccm_init_key`, `aes_ccm_cipher`) and `modes/ccm128.c`. - `CRYPTO_ccm128_*` is ported byte for byte over the parent's `AES_set_encrypt_key` and `AES_encrypt`, in the same way libSceLibreSsl's `EvpAes.cpp` ports `gcm128.c`. mbedtls's CCM is not used, because it rejects 0xFF00 bytes of AAD or more and does not reproduce `ccm128.c`'s behaviour: - L and M are encoded in the nonce flags byte when the key is set, so a later `EVP_CTRL_CCM_SET_L` or `SET_TAG` changes the nonce length or tag length the EVP layer passes, not the encoding. - A failed encrypt leaves the flags byte without M. - Only one AAD call per message is supported. - The EVP flow matches 4.0.0: - A null output with a null input sets the message length; a null output with an input is the AAD, and is rejected before the length. - A null input is the final call, which writes nothing. - A decrypt checks the tag in the update, zeroes the output when the tag is wrong, and leaves the final call failing, as in LibreSSL. - The controls are `EVP_CTRL_INIT` (L 8, M 12), `AEAD_GET_IVLEN` (15 - L), `AEAD_SET_IVLEN`, `CCM_SET_L`, `CCM_SET_TAG` (even, 4 to 16; a tag only when decrypting), `CCM_GET_TAG` (encrypt only, after the message) and `EVP_CTRL_COPY` (rebinds the key schedule pointer), with the same return values. - The cipher object copies LibreSSL's fields: block size 1, key length 16, IV length 12, flags 0x501477 and `ctx_size` 336, the LP64 size of `EVP_AES_CCM_CTX`. It uses libSceLibreSsl's 88-byte `EVP_CIPHER`, so the existing `EVP_CIPHER_key_length` and `EVP_CIPHER_iv_length` work on it. It runs inside libSceLibreSsl's `EVP_CipherInit_ex`, `EVP_*Update` and `EVP_*Final_ex`, which libSceLibreSsl3 forwards. For CCM, 4.0.0's `EVP_*Update` passes a zero-length update to the cipher, as 2.5.4 does, so the length-setting call works unchanged. One change there: `EVP_CipherInit_ex` now takes the cipher data from `calloc` instead of `malloc`. LibreSSL 4.0.0 zeroes it, and an IV-only init before any key relies on that: with a zero flags byte, the length call fails as in LibreSSL instead of reading garbage. This is the same one-line change as in #16249. In 2.5.4 the contents were unspecified, so it is still valid for libSceLibreSsl's own ciphers. Known gap, recorded in `docs/dev/debt/libSceLibreSsl3.md`: when the cipher fails, the forwarded 2.5.4 `EVP_EncryptUpdate`, `EVP_EncryptFinal_ex` and encrypting `EVP_Cipher*` calls leave `*outl` unset and accept a null `outl`, where 4.0.0 sets it to 0 first. The decrypt calls already match. A negative length returns 0 with the state untouched, as in 4.0.0. `EVP_aes_192_ccm` and `EVP_aes_256_ccm` are not among libSceLibreSsl3's exports, so they are not added. No title's trace or disassembly was used, and the export was not 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: - #14890, #15031, #15289, #15303 and #15591 edit the export inventory count in the debt file. - #16186 and #16249 edit the same places in `CMakeLists.txt` and the debt file. #16249 also makes the same `calloc` change, so whichever merges second resolves only the neighbouring lines. I will rebase this one as needed. 65 pull requests that conflict with `main` could not be compared by the tool. I scanned the diffs of all 1892 open pull requests for `aes_128_ccm`, `ccm128` and CCM helpers, and none adds them. ### 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_aes_ccm` (new) passes. It drives the cipher through the forwarded EVP functions: - **Vectors**: NIST SP 800-38C examples C.1 to C.4, sealed and opened. C.4 has 65536 bytes of AAD, which exercises the `0xFFFE` length encoding. - **Random, against mbedtls's independent CCM**: 400 messages over every L (2 to 8) and even M (4 to 16), AAD up to 0xFEFF bytes, ten 65535-byte messages with L = 2, and empty AAD and messages. The length is set with and without the explicit call, using `SET_IVLEN` or `SET_L`. Each message is also opened with a flipped tag bit, ciphertext and AAD, and the output must be zeroed. - **Defaults**: the 7-byte nonce and 12-byte tag of an unconfigured context. - **Controls**: every control's in-range and out-of-range return values, including `SET_IVLEN` with `INT_MIN` and `INT_MAX`. - **Parameters fixed at the key**: a tag length set after the key, and a longer or shorter nonce length set after the key. - **Sequencing**: no key and no IV, an IV only, AAD before the length, zero and negative lengths, a length mismatch, a second update, in-place encryption and decryption, and a decrypt without a tag. - **Empty message**, and a final call without an update. - **`EVP_CTRL_COPY`**: a copied context still encrypts after the source is freed. - **Cipher object**: the fields, read directly and through `EVP_CIPHER_key_length` and `EVP_CIPHER_iv_length`. - The test's guest `malloc` fills memory with 0xFF so that the `calloc` dependence is checked. - Of 16 deliberate bugs, the test catches 14. The other two are equivalent at guest sizes: they only differ for messages of 2^32 blocks or more. - All 102 `guest_libressl*` and `guest_crypto_memcmp` tests pass, including libSceLibreSsl's after the `calloc` change. - `objdump -p` on `libSceLibreSsl3.prx` lists `EVP_aes_128_ccm_nid_postfix` 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!16255
No description provided.