feat(libSceLibreSsl): implement the low-level AES functions #9033
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9033"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T10:13:19Z.
What
365 libSceLibreSsl exports on
mainstill throw. This implements the 8 low-level AES exports, whose signatures and caller-allocatedAES_KEYare public in LibreSSL:AES_set_encrypt_key,AES_set_decrypt_key,AES_encrypt,AES_decrypt,AES_ecb_encrypt,AES_cbc_encrypt,AES_cfb128_encryptandAES_ctr128_encrypt.AES_KEY(unsigned int rd_key[60]; int rounds, checked withstatic_assert) follow aes.h. Mode semantics follow the modes, and key setup follows LibreSSL 2.5.4's aes_core.c, the version theX509_verify_cert_error_stringdebt entry names:AES_KEYunchanged. On success it writes only the 44, 52 or 60 words the key length needs and leaves the rest ofrd_keyas it was.AES_ecb_encryptencrypts forAES_ENCRYPT(1) and decrypts otherwise.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.cfb128.c) and CTR (ctr128.c) resume from*num. The CTR counter carries across all 128 bits.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.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_bufafter a CTR call that ends on a block boundary holds the last keystream block.roundsis 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.Tested
Linux (Ubuntu 24.04.5 container, GCC 13.3, Release,
BUILD_TESTING=ON):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), thenumpositions, the key setup return codes and the throwing cases. With one InvMixColumns coefficient changed, the test fails.AES_KEY(every word androundsset to a known value first), for bothAES_set_encrypt_keyandAES_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 setsroundsbefore 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 oflibSceLibreSsl.prxis unchanged (394_nid_postfixnames).python3 tools/check_conventions.py --base upstream/main: passed.24bbc6d5f(one78acf4bd, 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_librariesandcheck_conventions.pypassed, and the unpatchedlibSceLibreSsl.prxstill exports 394_nid_postfixnames.Aes.cpp,Export.cppandGuestLibreSslAes.cpppass a MinGW-w64 GCC 13 syntax check (-fsyntax-only).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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Duplicate and overlap check
Checked on 2026-10-23 against upstream main
e78acf4bdand the heads of all 1306 open PRs.AES_set_encrypt_key,AES_set_decrypt_key,AES_encrypt,AES_decrypt,AES_ecb_encrypt,AES_cbc_encrypt,AES_cfb128_encryptorAES_ctr128_encrypt.24bbc6d5fmerges cleanly with main. #9041 (RAND and CRYPTO allocator) merged since the last rebase; this rebase keeps its source, test block and debt entry.core/libs/prx/libSceLibreSsl/CMakeLists.txt(theadd_librarysources and the test blocks) and the pending-export count indocs/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 inExport.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.Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T17:51:13Z.
Windows run of 936a7dd186 merged onto
main745b394629(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
libSceLibreSslandguest_libressl_aes_testswith no errors or warnings.ctest --repeat until-fail:2onguest_libressl_aes: passes on both repeats. Not run: a title.