feat(libSceLibreSsl3): implement AES_wrap_key #17124

Merged
fishingpvalues merged 15 commits from upstream-pr/17124 into main 2026-11-11 21:51:05 +00:00
fishingpvalues commented 2026-11-08 10:46:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fishingpvalues on 2026-11-08T10:46:05Z.


What

Follow-up to #14612, which implements AES_unwrap_key only; this adds the opposite direction. Implements AES_wrap_key (GjO5Ap3E-t4), previously exported only as Unknown_GjO5Ap3E_minus_t4, following RFC 3394:

  • uses the default IV A6A6A6A6A6A6A6A6 when iv is null, six rounds and the 64-bit counter XORed into the A block, matching OpenSSL's AES_wrap_key;
  • encrypts each 16-byte block through libSceLibreSsl's AES_encrypt, so no AES implementation is duplicated;
  • returns inlen + 8 and writes A | R[1..n];
  • returns -1 for an input below 16 bytes or not a multiple of 8, as LibreSSL does (OpenSSL 1.1 returns 0), and throws for a null key, input or output where OpenSSL dereferences it.

The Unknown_* stub and its APS5_EXPORT alias leave AdditionalExports.cpp; the real signature is declared once in include/Api.hpp; the code is in new src/AesWrap.cpp with new tests/AesWrap.cpp registered as guest_libressl3_aes_wrap.

docs/dev/debt/libSceLibreSsl3.md loses the NID-only row (125 to 124 callable exports, 116 named and 9 to 8 NID-only) and gains one Functional entry for the unverified PS5 ABI, custom-IV handling and console behaviour. The branch was rebased onto current main 4ab649f4d8; the inventory counts were recalculated on that base. #15854 was closed as a presumed duplicate of #14612, but #14612 only defines AES_unwrap_key_nid_postfix; this PR keeps the wrap NID.

Tested

Linux x86-64 (CachyOS, GCC 16.2.1), Release, BUILD_TESTING=ON, base main 828e44310e:

  • guest_libressl3_aes_wrap checks the RFC 3394 128-bit known-answer vector (1FA68B0A…71D2CFE5), first verifies the parent AES-128 ECB vector (69C4E0D8…70B4C55A), and covers the short and non-multiple-of-8 rejections and the null-output throw.
  • ctest --test-dir build -R '^guest_libressl3_aes_wrap$' --output-on-failure: 1/1 passed.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch pr-15854 --jobs 4: exit 2 only because it cannot confirm shared AdditionalExports.cpp/Api.hpp/debt files from conflicting PR heads; it reports no same exports.
  • Not run: Windows, a console, or a title.

Checklist

  • Based on current main; no other open PR implements this export
  • 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)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (independent of #14612; both touch AdditionalExports.cpp, so whichever merges second rebases)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5210667550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17124). Originally posted by [fishingpvalues](https://github.com/fishingpvalues) on 2026-11-08T10:46:05Z. --- ### What Follow-up to #14612, which implements `AES_unwrap_key` only; this adds the opposite direction. Implements `AES_wrap_key` (`GjO5Ap3E-t4`), previously exported only as `Unknown_GjO5Ap3E_minus_t4`, following [RFC 3394](https://www.rfc-editor.org/rfc/rfc3394): - uses the default IV `A6A6A6A6A6A6A6A6` when `iv` is null, six rounds and the 64-bit counter XORed into the A block, matching OpenSSL's `AES_wrap_key`; - encrypts each 16-byte block through libSceLibreSsl's `AES_encrypt`, so no AES implementation is duplicated; - returns `inlen + 8` and writes `A | R[1..n]`; - returns -1 for an input below 16 bytes or not a multiple of 8, as LibreSSL does (OpenSSL 1.1 returns 0), and throws for a null key, input or output where OpenSSL dereferences it. The `Unknown_*` stub and its `APS5_EXPORT` alias leave `AdditionalExports.cpp`; the real signature is declared once in `include/Api.hpp`; the code is in new `src/AesWrap.cpp` with new `tests/AesWrap.cpp` registered as `guest_libressl3_aes_wrap`. `docs/dev/debt/libSceLibreSsl3.md` loses the NID-only row (125 to 124 callable exports, 116 named and 9 to 8 NID-only) and gains one Functional entry for the unverified PS5 ABI, custom-IV handling and console behaviour. The branch was rebased onto current `main` `4ab649f4d8`; the inventory counts were recalculated on that base. #15854 was closed as a presumed duplicate of #14612, but #14612 only defines `AES_unwrap_key_nid_postfix`; this PR keeps the wrap NID. ### Tested Linux x86-64 (CachyOS, GCC 16.2.1), Release, `BUILD_TESTING=ON`, base `main` `828e44310e`: - `guest_libressl3_aes_wrap` checks the RFC 3394 128-bit known-answer vector (`1FA68B0A…71D2CFE5`), first verifies the parent AES-128 ECB vector (`69C4E0D8…70B4C55A`), and covers the short and non-multiple-of-8 rejections and the null-output throw. - `ctest --test-dir build -R '^guest_libressl3_aes_wrap$' --output-on-failure`: 1/1 passed. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch pr-15854 --jobs 4`: exit 2 only because it cannot confirm shared `AdditionalExports.cpp`/`Api.hpp`/debt files from conflicting PR heads; it reports no same exports. - Not run: Windows, a console, or a title. ### Checklist - [x] Based on current `main`; no other open PR implements this export - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (independent of #14612; both touch `AdditionalExports.cpp`, so whichever merges second rebases) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
gabichuuuuu commented 2026-11-08 12:02:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T12:02:13Z.

Review: COMMENTED


Windows run of head e5cfa65c4f (it already contains main 4f988389f5). Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.

  • guest_libressl3_aes_wrap passes as pushed.
  • All six RFC 3394 vectors (4.1 to 4.6), added locally to the test: 128-, 192- and 256-bit KEKs, with 128-, 192- and 256-bit key data. They all match byte for byte, as do the return values (inlen + 8). Each vector also gives the same output with an explicit A6A6A6A6A6A6A6A6 IV, with the output overlapping the input as out = in - 8, and with out == in. The last two work because the input is copied to r before anything is written.

The loop matches LibreSSL's own AES_wrap_key. That's the library this module is: t = n * j + i big-endian XORed into A, and -1 for an input under 16 bytes or not a multiple of 8. The description says "as OpenSSL does". OpenSSL 1.1 and later return 0 from CRYPTO_128_wrap there, so LibreSSL is the reference that gives this -1. Only the wording is off; the code is right.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5817795376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17124#pullrequestreview-5817795376). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T12:02:13Z. **Review:** COMMENTED --- Windows run of head `e5cfa65c4f` (it already contains `main` `4f988389f5`). Setup: Windows 11, WinLibs GCC 15.2 (`x86_64-ucrt-posix-seh`), Release, `BUILD_TESTING=ON`. - `guest_libressl3_aes_wrap` passes as pushed. - **All six RFC 3394 vectors** (4.1 to 4.6), added locally to the test: 128-, 192- and 256-bit KEKs, with 128-, 192- and 256-bit key data. They all match byte for byte, as do the return values (`inlen + 8`). Each vector also gives the same output with an explicit `A6A6A6A6A6A6A6A6` IV, with the output overlapping the input as `out = in - 8`, and with `out == in`. The last two work because the input is copied to `r` before anything is written. The loop matches LibreSSL's own `AES_wrap_key`. That's the library this module is: `t = n * j + i` big-endian XORed into `A`, and `-1` for an input under 16 bytes or not a multiple of 8. The description says "as OpenSSL does". OpenSSL 1.1 and later return 0 from `CRYPTO_128_wrap` there, so LibreSSL is the reference that gives this `-1`. Only the wording is off; the code is right.
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!17124
No description provided.