feat(libSceLibreSsl): implement the low-level AES functions #9033

Merged
HQasper merged 2 commits from upstream-pr/9033 into main 2026-10-24 09:13:36 +00:00
HQasper commented 2026-10-21 10:13:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T10:13:19Z.


What

365 libSceLibreSsl exports on main still throw. This implements the 8 low-level AES exports, whose signatures and caller-allocated AES_KEY are public in LibreSSL: AES_set_encrypt_key, AES_set_decrypt_key, AES_encrypt, AES_decrypt, AES_ecb_encrypt, AES_cbc_encrypt, AES_cfb128_encrypt and AES_ctr128_encrypt.

  • Signatures and the 244-byte AES_KEY (unsigned int rd_key[60]; int rounds, checked with static_assert) follow aes.h. Mode semantics follow the modes, and key setup follows LibreSSL 2.5.4's aes_core.c, the version the X509_verify_cert_error_string debt entry names:
    • Key setup returns -1 for a null key or schedule and -2 for a length other than 128, 192 or 256 bits, before it writes anything, so a refused call leaves the AES_KEY unchanged. On success it writes only the 44, 52 or 60 words the key length needs and leaves the rest of rd_key as it was.
    • AES_ecb_encrypt encrypts for AES_ENCRYPT (1) and decrypts otherwise.
    • CBC (cbc128.c) pads a partial final block with the IV and writes the whole block when encrypting. When decrypting it reads the whole input block but writes only the requested length. The IV holds the last cipher block afterwards.
    • CFB-128 (cfb128.c) and CTR (ctr128.c) resume from *num. The CTR counter carries across all 128 bits.
  • The cipher is written from FIPS-197. The S-box is generated at compile time from the field inverse and the affine map, and it is checked with static_assert. The decryption schedule is the encryption schedule reversed, with InvMixColumns applied to the inner round keys, as in LibreSSL's generic code (equivalent inverse cipher). No third-party code is added.
  • The debt entry records the remaining gaps:
    • The schedule uses the generic C layout of 2.5.4's aes_core.c (big-endian words). Its amd64 build uses aes-x86_64 instead, which copies the key bytes without swapping, so each word is byte-reversed compared with the C layout (OpenSSL 3.0.13's x86-64 code gives the same schedules byte-reversed). The console's build options are unknown, so a schedule the title reads or fills itself is unverified.
    • ecount_buf after a CTR call that ends on a block boundary holds the last keystream block.
    • Null pointers, a schedule whose rounds is not 10, 12 or 14, and a CFB or CTR position outside 0-15 throw. LibreSSL does not check these and would crash or read out of bounds.
  • The pending-export count goes from 365 to 357.

Tested

Linux (Ubuntu 24.04.5 container, GCC 13.3, Release, BUILD_TESTING=ON):

  • New guest_libressl_aes: passed. It covers the FIPS-197 appendix C vectors for 128-, 192- and 256-bit keys (encrypt, decrypt and ECB in place), the appendix A.1 key expansion words, and the SP 800-38A F.2.1/F.2.2 CBC, F.3.13/F.3.14 CFB-128 and F.5.1 CTR vectors, split across calls and in place. It also checks partial CBC blocks, the CTR counter wrapping from all-ones (cross-checked with OpenSSL 3.0.13), the num positions, the key setup return codes and the throwing cases. With one InvMixColumns coefficient changed, the test fails.
  • Key setup with a sentinel-filled AES_KEY (every word and rounds set to a known value first), for both AES_set_encrypt_key and AES_set_decrypt_key: key lengths 0, 64, 127, 129, 160, 224, 255, 384, 512 and -128 return -2, and a null key returns -1 (also with an invalid length), with all 244 bytes unchanged. AES-128 and AES-192 keep their 16 and 8 unused words, and every written word for all three lengths matches the full FIPS-197 appendix A.1, A.2 and A.3 schedules and the decryption schedules. The expected values were computed with a separate Python implementation of FIPS-197 and match the published appendix A words and OpenSSL 3.0.13's schedules (byte-reversed, see above). The test fails when key setup zeroes the schedule again, when it sets rounds before checking the length, when AES-128 setup clears an unused word, and when the decryption setup changes the last word.
  • patched_libraries: passed. The export set of libSceLibreSsl.prx is unchanged (394 _nid_postfix names).
  • python3 tools/check_conventions.py --base upstream/main: passed.
  • On 24bbc6d5f (on e78acf4bd, with the review fix): guest_libressl_aes, guest_libressl_digest, guest_libressl_des, guest_libressl_x509_errors, guest_libressl_runtime, guest_libressl3_digest, guest_crypto_memcmp, patched_libraries and check_conventions.py passed, and the unpatched libSceLibreSsl.prx still exports 394 _nid_postfix names.
  • Windows: not run. Aes.cpp, Export.cpp and GuestLibreSslAes.cpp pass a MinGW-w64 GCC 13 syntax check (-fsyntax-only).
  • Not tested: any title or homebrew (no title using libSceLibreSsl is known).

Note: this PR and #9448 (EVP/HMAC) both change core/libs/prx/libSceLibreSsl/CMakeLists.txt and the pending-export count in the debt file, so whichever merges second needs a small rebase.

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

Duplicate and overlap check

Checked on 2026-10-23 against upstream main e78acf4bd and the heads of all 1306 open PRs.

  • No open PR or commit on main implements AES_set_encrypt_key, AES_set_decrypt_key, AES_encrypt, AES_decrypt, AES_ecb_encrypt, AES_cbc_encrypt, AES_cfb128_encrypt or AES_ctr128_encrypt.
  • 24bbc6d5f merges cleanly with main. #9041 (RAND and CRYPTO allocator) merged since the last rebase; this rebase keeps its source, test block and debt entry.
  • 14 other open PRs change libSceLibreSsl. #9448 (EVP/HMAC) merges cleanly with main but conflicts with this PR in core/libs/prx/libSceLibreSsl/CMakeLists.txt (the add_library sources and the test blocks) and the pending-export count in docs/dev/debt/libSceLibreSsl.md, so whichever merges second needs a small rebase. #9446, #9529, #9571, #9745, #9870, #9949, #10005, #10071, #10078 and #10248 already conflict with main in the same files; #10005 also has a text conflict with this PR in Export.cpp, because its ASN1 stub deletions sit next to the AES stubs. #9535, #9536 and #9785 merge cleanly with this PR. None of them implements these functions.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4949930707 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9033). Originally posted by [HQasper](https://github.com/HQasper) on 2026-10-21T10:13:19Z. --- ### What 365 libSceLibreSsl exports on `main` still throw. This implements the 8 low-level AES exports, whose signatures and caller-allocated `AES_KEY` are public in LibreSSL: `AES_set_encrypt_key`, `AES_set_decrypt_key`, `AES_encrypt`, `AES_decrypt`, `AES_ecb_encrypt`, `AES_cbc_encrypt`, `AES_cfb128_encrypt` and `AES_ctr128_encrypt`. - Signatures and the 244-byte `AES_KEY` (`unsigned int rd_key[60]; int rounds`, checked with `static_assert`) follow [aes.h](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/aes/aes.h). Mode semantics follow [the modes](https://github.com/libressl/openbsd/tree/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/modes), and key setup follows [LibreSSL 2.5.4's aes_core.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/aes/aes_core.c), the version the `X509_verify_cert_error_string` debt entry names: - Key setup returns -1 for a null key or schedule and -2 for a length other than 128, 192 or 256 bits, before it writes anything, so a refused call leaves the `AES_KEY` unchanged. On success it writes only the 44, 52 or 60 words the key length needs and leaves the rest of `rd_key` as it was. - `AES_ecb_encrypt` encrypts for `AES_ENCRYPT` (1) and decrypts otherwise. - CBC (`cbc128.c`) pads a partial final block with the IV and writes the whole block when encrypting. When decrypting it reads the whole input block but writes only the requested length. The IV holds the last cipher block afterwards. - CFB-128 (`cfb128.c`) and CTR (`ctr128.c`) resume from `*num`. The CTR counter carries across all 128 bits. - The cipher is written from FIPS-197. The S-box is generated at compile time from the field inverse and the affine map, and it is checked with `static_assert`. The decryption schedule is the encryption schedule reversed, with InvMixColumns applied to the inner round keys, as in LibreSSL's generic code (equivalent inverse cipher). No third-party code is added. - The debt entry records the remaining gaps: - The schedule uses the generic C layout of 2.5.4's `aes_core.c` (big-endian words). Its amd64 build uses [aes-x86_64](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/aes/asm/aes-x86_64.pl) instead, which copies the key bytes without swapping, so each word is byte-reversed compared with the C layout (OpenSSL 3.0.13's x86-64 code gives the same schedules byte-reversed). The console's build options are unknown, so a schedule the title reads or fills itself is unverified. - `ecount_buf` after a CTR call that ends on a block boundary holds the last keystream block. - Null pointers, a schedule whose `rounds` is not 10, 12 or 14, and a CFB or CTR position outside 0-15 throw. LibreSSL does not check these and would crash or read out of bounds. - The pending-export count goes from 365 to 357. ### Tested Linux (Ubuntu 24.04.5 container, GCC 13.3, Release, `BUILD_TESTING=ON`): - New `guest_libressl_aes`: passed. It covers the FIPS-197 appendix C vectors for 128-, 192- and 256-bit keys (encrypt, decrypt and ECB in place), the appendix A.1 key expansion words, and the SP 800-38A F.2.1/F.2.2 CBC, F.3.13/F.3.14 CFB-128 and F.5.1 CTR vectors, split across calls and in place. It also checks partial CBC blocks, the CTR counter wrapping from all-ones (cross-checked with OpenSSL 3.0.13), the `num` positions, the key setup return codes and the throwing cases. With one InvMixColumns coefficient changed, the test fails. - Key setup with a sentinel-filled `AES_KEY` (every word and `rounds` set to a known value first), for both `AES_set_encrypt_key` and `AES_set_decrypt_key`: key lengths 0, 64, 127, 129, 160, 224, 255, 384, 512 and -128 return -2, and a null key returns -1 (also with an invalid length), with all 244 bytes unchanged. AES-128 and AES-192 keep their 16 and 8 unused words, and every written word for all three lengths matches the full FIPS-197 appendix A.1, A.2 and A.3 schedules and the decryption schedules. The expected values were computed with a separate Python implementation of FIPS-197 and match the published appendix A words and OpenSSL 3.0.13's schedules (byte-reversed, see above). The test fails when key setup zeroes the schedule again, when it sets `rounds` before checking the length, when AES-128 setup clears an unused word, and when the decryption setup changes the last word. - `patched_libraries`: passed. The export set of `libSceLibreSsl.prx` is unchanged (394 `_nid_postfix` names). - `python3 tools/check_conventions.py --base upstream/main`: passed. - On 24bbc6d5f (on e78acf4bd, with the review fix): `guest_libressl_aes`, `guest_libressl_digest`, `guest_libressl_des`, `guest_libressl_x509_errors`, `guest_libressl_runtime`, `guest_libressl3_digest`, `guest_crypto_memcmp`, `patched_libraries` and `check_conventions.py` passed, and the unpatched `libSceLibreSsl.prx` still exports 394 `_nid_postfix` names. - Windows: not run. `Aes.cpp`, `Export.cpp` and `GuestLibreSslAes.cpp` pass a MinGW-w64 GCC 13 syntax check (`-fsyntax-only`). - Not tested: any title or homebrew (no title using libSceLibreSsl is known). Note: this PR and #9448 (EVP/HMAC) both change core/libs/prx/libSceLibreSsl/CMakeLists.txt and the pending-export count in the debt file, so whichever merges second needs a small rebase. ### Checklist - [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 ### Duplicate and overlap check Checked on 2026-10-23 against upstream main e78acf4bd and the heads of all 1306 open PRs. - No open PR or commit on main implements `AES_set_encrypt_key`, `AES_set_decrypt_key`, `AES_encrypt`, `AES_decrypt`, `AES_ecb_encrypt`, `AES_cbc_encrypt`, `AES_cfb128_encrypt` or `AES_ctr128_encrypt`. - 24bbc6d5f merges cleanly with main. #9041 (RAND and CRYPTO allocator) merged since the last rebase; this rebase keeps its source, test block and debt entry. - 14 other open PRs change libSceLibreSsl. #9448 (EVP/HMAC) merges cleanly with main but conflicts with this PR in `core/libs/prx/libSceLibreSsl/CMakeLists.txt` (the `add_library` sources and the test blocks) and the pending-export count in `docs/dev/debt/libSceLibreSsl.md`, so whichever merges second needs a small rebase. #9446, #9529, #9571, #9745, #9870, #9949, #10005, #10071, #10078 and #10248 already conflict with main in the same files; #10005 also has a text conflict with this PR in `Export.cpp`, because its ASN1 stub deletions sit next to the AES stubs. #9535, #9536 and #9785 merge cleanly with this PR. None of them implements these functions.
ycbuzi commented 2026-10-21 17:51:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T17:51:13Z.


Windows run of 936a7dd186 merged onto main 745b394629 (90ab65fa15, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libSceLibreSsl and guest_libressl_aes_tests with no errors or warnings. ctest --repeat until-fail:2 on guest_libressl_aes: passes on both repeats. Not run: a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322740950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9033#issuecomment-6322740950). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T17:51:13Z. --- Windows run of 936a7dd186 merged onto `main` 745b394629 (90ab65fa15, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`. Built `libSceLibreSsl` and `guest_libressl_aes_tests` with no errors or warnings. `ctest --repeat until-fail:2` on `guest_libressl_aes`: passes on both repeats. Not run: a title.
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!9033
No description provided.