Skip to content

fix(libSceLibreSsl): store the AES_KEY schedule as little-endian words as the console does - #22842

Merged
oneandonlydean merged 4 commits into
boykopovar:mainfrom
xXPBSXx:fix/libressl-aes-key-little-endian
Nov 18, 2026
Merged

oneandonlydean merged 4 commits into
boykopovar:mainfrom
xXPBSXx:fix/libressl-aes-key-little-endian

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

What

AES_set_encrypt_key and AES_set_decrypt_key in libSceLibreSsl/src/Aes.cpp expand the key on the host and store the schedule in the rd_key words of AES_KEY as big-endian words (LoadBig32, round constant in the top byte), the layout of LibreSSL's generic C code. A PS5 payload process on firmware 11.60 called the console's libSceLibreSsl.sprx with the FIPS-197 example keys 00 01 02 ... of 128, 192 and 256 bits and printed every word of the schedule, for both directions: the words are little-endian, the layout of the amd64 assembly build (aes-x86_64), which copies the key bytes without swapping. For the 128-bit key rd_key[0..3] of the encryption schedule is 03020100 07060504 0b0a0908 0f0e0d0c and rd_key[4] is fd74aad6 (case enc128.w0, line 4); this library stored 00010203 ... and d6aa74fd. Return value, rounds (10, 12, 14), the number of words written (44, 52, 60) and the untouched rest of the 244-byte structure were already the same. A title that only calls AES_set_*_key and then AES_encrypt or AES_decrypt gets the same results either way; the words matter to a title that reads, copies or serializes an AES_KEY, or that fills one itself.

The fix (Aes.cpp, 16 lines) loads the key bytes as little-endian words, rotates and applies the round constant in that word order (temp >> 8 | temp << 24, constant in the low byte), and reads state bytes from the round key with the matching shifts in InverseMixWord, LoadState and AddRoundKey; the decryption schedule is still the encryption schedule in reverse order with the middle rounds inverse-mixed, now also in little-endian words. Encryption and decryption results do not change: guest_libressl_aes (the FIPS-197, CBC and CTR vectors) and guest_libressl_evp_aes pass before and after. Only libSceLibreSsl is changed. libSceLibreSsl3 has no AES_set_*_key of its own; it forwards both to libSceLibreSsl (ParentExports.cpp), and a second probe through the console's libSceLibreSsl3 printed the same words; whether the console's module forwards or keeps its own copy is not shown by the probe.

The test commit changes GuestLibreSslAes.cpp: the helper Word reads little-endian, the pins of the FIPS-197 appendix A.1 words rd_key[4] and rd_key[43] become 0x17fefaa0 and 0xa60c63b6, and six new RequireSchedule checks pin the complete 44, 52 and 60 words of the encryption and the decryption schedules of the key 00 01 02 ... for 128, 192 and 256 bits, written as the bytes of the words the probe printed. I checked these six pins against the logged words (byte for byte equal) and the logged encryption schedules against a plain FIPS-197 key expansion with each word stored little-endian (the script is in the first comment). The older schedule pins stay as byte sequences and hold for either layout. The Oracle commit changes one line in the optional comparison test libSceLibreSsl/tests/Oracle.cpp (built only with -DANYPS5_LIBRESSL_ORACLE=ON, against LibreSSL 2.5.4): the reference schedule is byte-swapped word by word before the comparison, because the reference is the big-endian C layout. The documentation commit changes one sentence in docs/dev/debt/changes/e-docs-libressl-aes-measured.md, the fragment of #22017: the AES_KEY words are little-endian as measured, the old sentence that a title's schedule differs here is gone, and it says that libSceLibreSsl3 forwards here and what the console's module did.

Not measured: other keys than 00 01 02 ..., the EVP path, AES_encrypt with a schedule from another source on the console, a title process, and whether the console's libSceLibreSsl3 forwards or keeps its own copy. The probe ran in a payload process.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loads libSceLibreSsl.sprx and takes AES_set_encrypt_key and AES_set_decrypt_key with dlsym (line 2), on 2026-11-17 at 16:34 UTC; a second run through libSceLibreSsl3.sprx on 2026-11-17 at 19:05 UTC gave the same case lines (the module name in the PROBE line and in case 0 aside). A title process may differ. The cases are enc128 to dec256 and the invalid key length, lines 3 to 51 of the raw output in the first comment.

Call (key 00 01 02 ...) PS5 payload process, firmware 11.60 AnyPS5 main before (code read; the test commit fails on it) here (test, fix, Oracle)
encryption, 128 bits: rd_key[0..3], rd_key[4] 03020100 07060504 0b0a0908 0f0e0d0c, fd74aad6 (lines 4, 4) 00010203 04050607 08090a0b 0c0d0e0f, d6aa74fd as the console
decryption, 128 bits: rd_key[0..3] 7f1d1113 174a94e3 8ba707f3 c5302b4d (line 11) the same four words byte-swapped as the console
192 and 256 bits, both directions: all 52 and 60 words (lines 17 to 50) little-endian words of the FIPS-197 schedule big-endian words as the console
rounds, words written, bytes beyond them (lines 3, 10, 17, 25, 33, 42) 10/12/14, 44/52/60, 0 changed same same
100-bit key (line 51) -2, rounds untouched same same

With the test commit alone, guest_libressl_aes fails on main (Windows: exit code 0xc0000409; Linux: subprocess aborted) and guest_libressl_evp_aes passes. Windows 10 Pro 10.0.19045, base upstream/main a8fc237550a, 2026-11-18 from 09:40 UTC; one build per commit, ctest for guest_libressl_aes, guest_libressl_evp_aes and guest_libressl_oracle (the Oracle option on, against the LibreSSL 2.5.4 source tarball, hash checked, no new download): on the base both AES tests pass and the Oracle compares 17 cases each of AES_set_decrypt_key and AES_set_encrypt_key with 0 differences; with the test commit guest_libressl_aes fails; with the fix commit both AES tests pass and the Oracle shows 3 differences each (the words, for 128, 192 and 256 bits, return value equal), which is why the Oracle commit is needed; with the Oracle commit and with the documentation commit both AES tests pass and the Oracle shows 0 differences for both functions. The whole guest_libressl_oracle test fails in every phase, the base included, in SSL_CTX_set_cipher_list (799 of 3500 cases differ, the same number each time): that comes from before this change and is not touched here. Linux (WSL Ubuntu, g++ 13.3, Release, from 08:56 UTC, git am of the four patches onto the same base, trees 3bb0a5b58f42, 8592078b950a, c82437963263 and 856ddeface77 equal to the four commits; without the Oracle option): 179 tests per run; the test commit alone fails 1 of 179 (guest_libressl_aes, subprocess aborted), the other three commits pass 179 of 179 in three runs each; tools/check_conventions.py passed for each commit in that run, for the documentation commit with the one length warning named below. The Oracle commit was not run on Linux. Not tested: macOS, the Oracle on Linux, a title process, other firmware, and the cases listed above as not measured.

tools/check_conventions.py --base upstream/main passes with no error and one warning that the entry in the fragment is over 1000 characters (now 2305, up from 2237, counted the same way; the warning was already there). The change is 4 files, +46/-11, with no new comments in code.

Overlap: the other open pull requests that change the files of this branch (core/libs/prx/libSceLibreSsl/src/Aes.cpp, core/libs/prx/libSceLibreSsl/tests/Oracle.cpp, core/libs/tests/GuestLibreSslAes.cpp and the fragment docs/dev/debt/changes/e-docs-libressl-aes-measured.md, already on main from #22017 and not yet folded) or the debt entry for AES_set_encrypt_key and AES_set_decrypt_key, found by reading the changed files of all 1757 open pull requests at about 11:33 to 11:50 UTC, by reading what they change in docs/dev/debt/libSceLibreSsl.md and the fragments under docs/dev/debt/changes/ for AES_set_encrypt_key, AES_set_decrypt_key, AES_KEY, ExpandKey and LoadLittle32, and by searching their titles and branch names for the same names (no hit), were checked on 2026-11-18 at about 12:02 UTC against main 0c1bb03d10d together with this head: the branch is based on a8fc237550a, an ancestor of it, merges into main without a conflict, and tools/debt_fold.py --check applies the changed fragment together with every other fragment on main without an error. No open pull request changes Aes.cpp, GuestLibreSslAes.cpp or the fragment. Thirteen pull requests list one of the files or libSceLibreSsl.md; the eight that list Oracle.cpp or change libSceLibreSsl debt text (#21701, #13877, #13547, #13546, #13285, #13266, #12315 and #11466) were each merged with and without this branch against main 0c1bb03d10d, and none conflicts only with this branch: #13285, #12315 and #11466 conflict with main by themselves, the other five merge cleanly with and without it. Six of them (#21701, #13877, #13547, #13546, #13285 and #13266) add cases to the Oracle test for other functions, the file in which the Oracle commit here adds one line. None of them changes a line that names AES_set_encrypt_key or AES_set_decrypt_key. The aliases of #17299 and #17303 are other names. The merge with main was not built or run; the runs below are on the base a8fc237550a.

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 (no graphics change)
  • AI-assisted: yes

xXPBSXx and others added 4 commits November 18, 2026 09:53
… on the console

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…s as the console does

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e byte-swapped reference schedule

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Probe source (built with ps5-payload-sdk v0.45; PROBE_BUILD is the first 12 hex digits of the source's SHA-256, 9409c7bfa90a) and its raw output from the run on 2026-11-17 at 16:34 UTC. The clock in the PROBE line is the console's own clock. The probe loads libSceLibreSsl.sprx with dlopen, takes AES_set_encrypt_key and AES_set_decrypt_key with dlsym and calls them with the public FIPS-197 example keys (the bytes 00 01 02 ... of 16, 24 and 32 bytes, never a real key) into a structure pre-filled with 0xaa; for each key size and direction it prints every used word of rd_key as 8 hex digits, the return value, rounds and the number of bytes beyond the used words that changed; the last call is a key length of 100 bits. Every call is guarded against signals. An internal probe number and a remark on an earlier run in the header comment were removed; the code is unchanged, and the build ID is the hash of the original file.

A second run of the same probe with the module libSceLibreSsl3.sprx (build 42d809d5b113, 2026-11-17 at 19:05 UTC; the source differs from this one only in the module name) printed the same case lines as this run, apart from the module name in the PROBE line and in case 0; its raw output is the third block below.

The script fips_check.py (fourth block, run in the folder of the raw output saved as stdout.log) rebuilds the encryption schedules of the keys 00 01 02 ... with a plain FIPS-197 key expansion, stores each 32-bit word little-endian and compares them with the logged words. Its output: 128, 192 and 256 bits all True (44, 52 and 60 words).

probe.c
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h>   /* only for kernel_get_fw_version() */

#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: on timeout the runner can only close the connection, not end the process on the console.
   The probe ends itself after at most PROBE_MAX_S seconds (the build script sets timeout - 10 s). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static void probe_alarm(int sig) {
  static const char msg[] = "TIMEOUT\tprobe self limit\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(124);
}

static int cases = 0;

static void probe_header(void) {
  char utc[32];
  time_t now = time(0);
  struct tm tmv;
  gmtime_r(&now, &tmv);
  strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
  printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
         getpid(), utc);
  fflush(stdout);
}

/* CASE: id, description of the call, return value as long, errno (0 if not set), extra (hex/bytes). */
static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
  printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
  fflush(stdout);
  cases++;
}

static void probe_end(void) {
  printf("END\t%d\n", cases);
  fflush(stdout);
}

#include <dlfcn.h>
#include <setjmp.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>

static sigjmp_buf guard_env;
static volatile int guard_sig = 0;
static volatile int guard_active = 0;
static volatile int guard_hits = 0;
static void guard_handler(int s) {
  static const char msg[] = "FAULT\tprobe guard: fault outside a guarded call or too many faults\n";
  if(!guard_active || ++guard_hits > 8) {
    (void)!write(1, msg, sizeof msg - 1);
    _exit(125);
  }
  guard_sig = s;
  guard_active = 0;
  siglongjmp(guard_env, 1);
}
static void guard_arm(void) {
  struct sigaction sa;
  memset(&sa, 0, sizeof sa);
  sa.sa_handler = guard_handler;
  sigaction(SIGSEGV, &sa, 0);
  sigaction(SIGBUS, &sa, 0);
  sigaction(SIGABRT, &sa, 0);
}
#define CALL(rvar, evar, expr) do { errno = 0; guard_sig = 0; rvar = -1; if(sigsetjmp(guard_env, 1) == 0) { guard_active = 1; rvar = (expr); } guard_active = 0; evar = errno; } while(0)

/* the COMPLETE AES key schedule of AES_set_encrypt_key and AES_set_decrypt_key of libSceLibreSsl.sprx (dlopen, dlsym) on firmware 11.60
   (follow-up of an earlier run that measured only rd_key[0..3] of a 128 bit key). Only the public FIPS-197 example keys are used (bytes 00 01 02 ...
   of 16, 24 and 32 bytes), never a real key. Prototype int AES_set_encrypt_key(const unsigned char* key, int bits, AesKey*) and the same for decrypt, AesKey
   = {uint32 rd_key[60]; int rounds} (244 bytes); no SSL context or connection function is called. The key struct is pre-filled with 0xaa; per key size and
   direction every used word (4*(rounds+1): 44, 52, 60) is printed as 8 hex digits, eight words per CASE line, followed by the return value, rounds and the
   number of bytes beyond the used words that were changed (untouched = 0xaa). 6 key set calls plus an invalid key length 100 as the last call, all guarded. */
#include <stdlib.h>
typedef struct { uint32_t rd_key[60]; int rounds; } aes_key;
typedef int (*sk_fn)(const unsigned char*, int, aes_key*);
static void dump(const char* dir, int bits, const aes_key* k, int rc) {
  int rounds = k->rounds;
  int used = (rounds >= 10 && rounds <= 14) ? 4 * (rounds + 1) : 0;
  char id[24], what[64], ex[260];
  snprintf(id, sizeof id, "%s%d.h", dir, bits);
  snprintf(what, sizeof what, "%s key %d bits: header", dir, bits);
  size_t beyond = 0;
  const unsigned char* raw = (const unsigned char*)k;
  for(size_t i = (size_t)used * 4; i < 240; i++) if(raw[i] != 0xaa) beyond++;
  snprintf(ex, sizeof ex, "rc=%d rounds=%d used_words=%d bytes_changed_beyond_used=%zu signal=%d", rc, rounds, used, beyond, (int)guard_sig);
  probe_case(id, what, guard_sig ? -1 : rc, 0, ex);
  for(int g = 0; g * 8 < used; g++) {
    char* p = ex;
    int n = 0;
    for(int j = g * 8; j < used && j < g * 8 + 8; j++) n += snprintf(p + n, sizeof ex - n, "%08x ", (unsigned)k->rd_key[j]);
    snprintf(id, sizeof id, "%s%d.w%d", dir, bits, g);
    snprintf(what, sizeof what, "%s key %d bits: rd_key[%d..%d]", dir, bits, g * 8, (g * 8 + 7 < used ? g * 8 + 7 : used - 1));
    probe_case(id, what, 0, 0, ex);
  }
}
int main(void) {
  signal(SIGALRM, probe_alarm);
  alarm(PROBE_MAX_S);
  probe_header();
  guard_arm();
  void* h = dlopen("libSceLibreSsl.sprx", RTLD_LAZY);
  sk_fn se = (sk_fn)(h ? dlsym(h, "AES_set_encrypt_key") : 0);
  sk_fn sd = (sk_fn)(h ? dlsym(h, "AES_set_decrypt_key") : 0);
  char ex[8];
  snprintf(ex, sizeof ex, "%d%d%d", !!h, !!se, !!sd);
  probe_case("0", "dlopen libSceLibreSsl.sprx, dlsym AES_set_encrypt_key AES_set_decrypt_key (1 yes, 0 no)", 0, 0, ex);
  if(!se || !sd) { probe_end(); return 0; }
  unsigned char key[32];
  for(int i = 0; i < 32; i++) key[i] = (unsigned char)i;
  static aes_key k;
  static const int sizes[3] = {128, 192, 256};
  int rc, e;
  for(int s = 0; s < 3; s++) {
    memset(&k, 0xaa, sizeof k);
    int r = -99;
    CALL(rc, e, (r = se(key, sizes[s], &k)));
    dump("enc", sizes[s], &k, r);
    memset(&k, 0xaa, sizeof k);
    r = -99;
    CALL(rc, e, (r = sd(key, sizes[s], &k)));
    dump("dec", sizes[s], &k, r);
  }
  memset(&k, 0xaa, sizeof k);
  int r = -99;
  CALL(rc, e, (r = se(key, 100, &k)));
  snprintf(ex, sizeof ex, "%d", r);
  char line[100];
  snprintf(line, sizeof line, "rc=%d rounds=%d signal=%d", r, k.rounds, (int)guard_sig);
  probe_case("bad", "AES_set_encrypt_key(key, 100 bits) (last call)", guard_sig ? -1 : r, e, line);
  probe_end();
  return 0;
}
stdout
PROBE	libSceLibreSsl_aes_keyschedule	fw=0x11600005	build=9409c7bfa90a	pid=225	utc=2026-11-17T18:16:48Z
CASE	0	dlopen libSceLibreSsl.sprx, dlsym AES_set_encrypt_key AES_set_decrypt_key (1 yes, 0 no)	0	0	111
CASE	enc128.h	enc key 128 bits: header	0	0	rc=0 rounds=10 used_words=44 bytes_changed_beyond_used=0 signal=0
CASE	enc128.w0	enc key 128 bits: rd_key[0..7]	0	0	03020100 07060504 0b0a0908 0f0e0d0c fd74aad6 fa72afd2 f178a6da fe76abd6 
CASE	enc128.w1	enc key 128 bits: rd_key[8..15]	0	0	0bcf92b6 f1bd3d64 00c59bbe feb33068 4e74ffb6 bfc9c2d2 bf0c596c 41bf6904 
CASE	enc128.w2	enc key 128 bits: rd_key[16..23]	0	0	bcf7f747 033e3595 bc326cf9 fd8d05fd e8a3aa3c eb9d9fa9 57aff350 aa22f6ad 
CASE	enc128.w3	enc key 128 bits: rd_key[24..31]	0	0	7d0f395e 9692a6f7 c13d55a7 6b1fa30a 1a70f914 8ce25fe3 4ddf0a44 26c0a94e 
CASE	enc128.w4	enc key 128 bits: rd_key[32..39]	0	0	35874347 b9651ca4 f4ba16e0 d27abfae d1329954 685785f0 9ced9310 4e972cbe 
CASE	enc128.w5	enc key 128 bits: rd_key[40..43]	0	0	7f1d1113 174a94e3 8ba707f3 c5302b4d 
CASE	dec128.h	dec key 128 bits: header	0	0	rc=0 rounds=10 used_words=44 bytes_changed_beyond_used=0 signal=0
CASE	dec128.w0	dec key 128 bits: rd_key[0..7]	0	0	7f1d1113 174a94e3 8ba707f3 c5302b4d be29aa13 f6af8f9c 80f570f7 03bff700 
CASE	dec128.w1	dec key 128 bits: rd_key[8..15]	0	0	63a46213 4886258f 765aff6b 834a87f7 74fc828d 2b22479c 3edcdae4 f510789c 
CASE	dec128.w2	dec key 128 bits: rd_key[16..23]	0	0	8d09e372 5fdec511 15fe9d78 cbcca278 2710c42e d2d72663 4a205869 de323f00 
CASE	dec128.w3	dec key 128 bits: rd_key[24..31]	0	0	04f5a2a8 f5c7e24d 98f77e0a 94126769 91e3c6c7 f13240e5 6d309c47 0ce51963 
CASE	dec128.w4	dec key 128 bits: rd_key[32..39]	0	0	9902dba0 60d18622 9c02dca2 61d58524 f0df568c f9d35d82 fcd35a80 fdd75986 
CASE	dec128.w5	dec key 128 bits: rd_key[40..43]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 
CASE	enc192.h	enc key 192 bits: header	0	0	rc=0 rounds=12 used_words=52 bytes_changed_beyond_used=0 signal=0
CASE	enc192.w0	enc key 192 bits: rd_key[0..7]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 13121110 17161514 f9f24658 fef4435c 
CASE	enc192.w1	enc key 192 bits: rd_key[8..15]	0	0	f5fe4a54 faf04758 e9e25648 fef4435c b349f940 4dbdba1c b843f048 42b3b710 
CASE	enc192.w2	enc key 192 bits: rd_key[16..23]	0	0	ab51e158 55a5a204 41b5ff7e 0c084562 b44bb52a f6f8023a 5da9e362 080c4166 
CASE	enc192.w3	enc key 192 bits: rd_key[24..31]	0	0	728501f5 7e8d4497 cac6f1bd 3c3ef387 619710e5 699b5183 9e7c1534 e0f151a3 
CASE	enc192.w4	enc key 192 bits: rd_key[32..39]	0	0	2a37a01e 16095399 779e437c 1e0512ff 880e7edd 68ff2f7e 42c88f60 54c1dcf9 
CASE	enc192.w5	enc key 192 bits: rd_key[40..47]	0	0	235f9f85 3d5a8d7a 5229c0c0 3ad6efbe 781e60de 2cdfbc27 0f8023a2 32daaed8 
CASE	enc192.w6	enc key 192 bits: rd_key[48..51]	0	0	330a97a4 09dc781a 71c218c4 5d1da4e3 
CASE	dec192.h	dec key 192 bits: header	0	0	rc=0 rounds=12 used_words=52 bytes_changed_beyond_used=0 signal=0
CASE	dec192.w0	dec key 192 bits: rd_key[0..7]	0	0	330a97a4 09dc781a 71c218c4 5d1da4e3 0dbdbed6 49ea09c2 8073b04d b91b023e 
CASE	dec192.w1	dec key 192 bits: rd_key[8..15]	0	0	c999b98f 3968b273 9dd8f9c7 728cc685 c16e7df7 ef543f42 7f317853 4457b714 
CASE	dec192.w2	dec key 192 bits: rd_key[16..23]	0	0	90654711 3b66cf47 8dce0e9b f0f10bfc b6a8c1dc 7d3f0567 4a195ccc 2e3a42b5 
CASE	dec192.w3	dec key 192 bits: rd_key[24..31]	0	0	abb0dec6 64231e79 be5f05a4 ab038856 da7c1bdd 155c8df2 1dab498a cb97c4bb 
CASE	dec192.w4	dec key 192 bits: rd_key[32..39]	0	0	08f7c478 d63c8d31 01b75596 cf93c0bf 10efdc60 ce249529 15efdb62 cf20962f 
CASE	dec192.w5	dec key 192 bits: rd_key[40..47]	0	0	dbcb4e4b dacf4d4d c7d75257 decb4949 1d181f1a 191c1b1e d7c74247 decb4949 
CASE	dec192.w6	dec key 192 bits: rd_key[48..51]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 
CASE	enc256.h	enc key 256 bits: header	0	0	rc=0 rounds=14 used_words=60 bytes_changed_beyond_used=0 signal=0
CASE	enc256.w0	enc key 256 bits: rd_key[0..7]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 13121110 17161514 1b1a1918 1f1e1d1c 
CASE	enc256.w1	enc key 256 bits: rd_key[8..15]	0	0	9fc273a5 98c476a1 93ce7fa9 9cc072a5 cda85116 dabe4402 c1a45d1a deba4006 
CASE	enc256.w2	enc key 256 bits: rd_key[16..23]	0	0	f0df87ae 681bf10f fbd58ea6 6715fc03 48f1e16d 924fa56f 53ebf875 8d51b873 
CASE	enc256.w3	enc key 256 bits: rd_key[24..31]	0	0	7f8256c6 1799a7c9 ec4c296f 8b59d56c 753ae23d e7754752 b49ebf27 39cf0754 
CASE	enc256.w4	enc key 256 bits: rd_key[32..39]	0	0	5f90dc0b 48097bc2 a44552ad 2f1c87c1 60a6f545 87d3b217 334d0d30 0a820a64 
CASE	enc256.w5	enc key 256 bits: rd_key[40..47]	0	0	1cf7cf7c 54feb4be f0bbe613 dfa761d2 fefa1af0 7929a8e7 4a64a5d7 40e6afb3 
CASE	enc256.w6	enc key 256 bits: rd_key[48..55]	0	0	71fe4125 2500f59b d5bb1388 0a1c725a 99665a4e e04ff2a9 aa2b577e eacdf8cd 
CASE	enc256.w7	enc key 256 bits: rd_key[56..59]	0	0	cc79fc24 e97909bf 3cc21a37 36de686d 
CASE	dec256.h	dec key 256 bits: header	0	0	rc=0 rounds=14 used_words=60 bytes_changed_beyond_used=0 signal=0
CASE	dec256.w0	dec key 256 bits: rd_key[0..7]	0	0	cc79fc24 e97909bf 3cc21a37 36de686d ffd1f134 2faacebf 5fe2e9fc 6e015825 
CASE	dec256.w1	dec key 256 bits: rd_key[8..15]	0	0	eb48165e 0a354c38 46b77175 84e680dc 8005a3c8 d07b3f8b 70482743 31e3b1d9 
CASE	dec256.w2	dec key 256 bits: rd_key[16..23]	0	0	138e70b5 e17d5a66 4c823d4d c251f1a9 a37bda74 507e9c43 a03318c8 41ab969a 
CASE	dec256.w3	dec key 256 bits: rd_key[24..31]	0	0	1597a63c f2f32ad3 adff672b 8ed3cce4 f3c45ff8 f3054637 f04d848b e1988e52 
CASE	dec256.w4	dec key 256 bits: rd_key[32..39]	0	0	9a4069de e7648cef 5f0c4df8 232cabcf 1658d5ae 00c119cf 0348c2bc 11d50ad9 
CASE	dec256.w5	dec key 256 bits: rd_key[40..47]	0	0	bd68c615 7d24e531 b868c117 7c20e637 0f85d77f 1699cc61 0389db73 129dc865 
CASE	dec256.w6	dec key 256 bits: rd_key[48..55]	0	0	c940282a c04c2324 c54c2426 c4482720 1d181f1a 191c1b1e 15101712 11141316 
CASE	dec256.w7	dec key 256 bits: rd_key[56..59]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 
CASE	bad	AES_set_encrypt_key(key, 100 bits) (last call)	-2	0	rc=-2 rounds=-1431655766 signal=0
END	50
stdout of the run through libSceLibreSsl3.sprx
PROBE	libSceLibreSsl3_aes_keyschedule	fw=0x11600005	build=42d809d5b113	pid=231	utc=2026-11-17T20:47:25Z
CASE	0	dlopen libSceLibreSsl3.sprx, dlsym AES_set_encrypt_key AES_set_decrypt_key (1 yes, 0 no)	0	0	111
CASE	enc128.h	enc key 128 bits: header	0	0	rc=0 rounds=10 used_words=44 bytes_changed_beyond_used=0 signal=0
CASE	enc128.w0	enc key 128 bits: rd_key[0..7]	0	0	03020100 07060504 0b0a0908 0f0e0d0c fd74aad6 fa72afd2 f178a6da fe76abd6 
CASE	enc128.w1	enc key 128 bits: rd_key[8..15]	0	0	0bcf92b6 f1bd3d64 00c59bbe feb33068 4e74ffb6 bfc9c2d2 bf0c596c 41bf6904 
CASE	enc128.w2	enc key 128 bits: rd_key[16..23]	0	0	bcf7f747 033e3595 bc326cf9 fd8d05fd e8a3aa3c eb9d9fa9 57aff350 aa22f6ad 
CASE	enc128.w3	enc key 128 bits: rd_key[24..31]	0	0	7d0f395e 9692a6f7 c13d55a7 6b1fa30a 1a70f914 8ce25fe3 4ddf0a44 26c0a94e 
CASE	enc128.w4	enc key 128 bits: rd_key[32..39]	0	0	35874347 b9651ca4 f4ba16e0 d27abfae d1329954 685785f0 9ced9310 4e972cbe 
CASE	enc128.w5	enc key 128 bits: rd_key[40..43]	0	0	7f1d1113 174a94e3 8ba707f3 c5302b4d 
CASE	dec128.h	dec key 128 bits: header	0	0	rc=0 rounds=10 used_words=44 bytes_changed_beyond_used=0 signal=0
CASE	dec128.w0	dec key 128 bits: rd_key[0..7]	0	0	7f1d1113 174a94e3 8ba707f3 c5302b4d be29aa13 f6af8f9c 80f570f7 03bff700 
CASE	dec128.w1	dec key 128 bits: rd_key[8..15]	0	0	63a46213 4886258f 765aff6b 834a87f7 74fc828d 2b22479c 3edcdae4 f510789c 
CASE	dec128.w2	dec key 128 bits: rd_key[16..23]	0	0	8d09e372 5fdec511 15fe9d78 cbcca278 2710c42e d2d72663 4a205869 de323f00 
CASE	dec128.w3	dec key 128 bits: rd_key[24..31]	0	0	04f5a2a8 f5c7e24d 98f77e0a 94126769 91e3c6c7 f13240e5 6d309c47 0ce51963 
CASE	dec128.w4	dec key 128 bits: rd_key[32..39]	0	0	9902dba0 60d18622 9c02dca2 61d58524 f0df568c f9d35d82 fcd35a80 fdd75986 
CASE	dec128.w5	dec key 128 bits: rd_key[40..43]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 
CASE	enc192.h	enc key 192 bits: header	0	0	rc=0 rounds=12 used_words=52 bytes_changed_beyond_used=0 signal=0
CASE	enc192.w0	enc key 192 bits: rd_key[0..7]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 13121110 17161514 f9f24658 fef4435c 
CASE	enc192.w1	enc key 192 bits: rd_key[8..15]	0	0	f5fe4a54 faf04758 e9e25648 fef4435c b349f940 4dbdba1c b843f048 42b3b710 
CASE	enc192.w2	enc key 192 bits: rd_key[16..23]	0	0	ab51e158 55a5a204 41b5ff7e 0c084562 b44bb52a f6f8023a 5da9e362 080c4166 
CASE	enc192.w3	enc key 192 bits: rd_key[24..31]	0	0	728501f5 7e8d4497 cac6f1bd 3c3ef387 619710e5 699b5183 9e7c1534 e0f151a3 
CASE	enc192.w4	enc key 192 bits: rd_key[32..39]	0	0	2a37a01e 16095399 779e437c 1e0512ff 880e7edd 68ff2f7e 42c88f60 54c1dcf9 
CASE	enc192.w5	enc key 192 bits: rd_key[40..47]	0	0	235f9f85 3d5a8d7a 5229c0c0 3ad6efbe 781e60de 2cdfbc27 0f8023a2 32daaed8 
CASE	enc192.w6	enc key 192 bits: rd_key[48..51]	0	0	330a97a4 09dc781a 71c218c4 5d1da4e3 
CASE	dec192.h	dec key 192 bits: header	0	0	rc=0 rounds=12 used_words=52 bytes_changed_beyond_used=0 signal=0
CASE	dec192.w0	dec key 192 bits: rd_key[0..7]	0	0	330a97a4 09dc781a 71c218c4 5d1da4e3 0dbdbed6 49ea09c2 8073b04d b91b023e 
CASE	dec192.w1	dec key 192 bits: rd_key[8..15]	0	0	c999b98f 3968b273 9dd8f9c7 728cc685 c16e7df7 ef543f42 7f317853 4457b714 
CASE	dec192.w2	dec key 192 bits: rd_key[16..23]	0	0	90654711 3b66cf47 8dce0e9b f0f10bfc b6a8c1dc 7d3f0567 4a195ccc 2e3a42b5 
CASE	dec192.w3	dec key 192 bits: rd_key[24..31]	0	0	abb0dec6 64231e79 be5f05a4 ab038856 da7c1bdd 155c8df2 1dab498a cb97c4bb 
CASE	dec192.w4	dec key 192 bits: rd_key[32..39]	0	0	08f7c478 d63c8d31 01b75596 cf93c0bf 10efdc60 ce249529 15efdb62 cf20962f 
CASE	dec192.w5	dec key 192 bits: rd_key[40..47]	0	0	dbcb4e4b dacf4d4d c7d75257 decb4949 1d181f1a 191c1b1e d7c74247 decb4949 
CASE	dec192.w6	dec key 192 bits: rd_key[48..51]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 
CASE	enc256.h	enc key 256 bits: header	0	0	rc=0 rounds=14 used_words=60 bytes_changed_beyond_used=0 signal=0
CASE	enc256.w0	enc key 256 bits: rd_key[0..7]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 13121110 17161514 1b1a1918 1f1e1d1c 
CASE	enc256.w1	enc key 256 bits: rd_key[8..15]	0	0	9fc273a5 98c476a1 93ce7fa9 9cc072a5 cda85116 dabe4402 c1a45d1a deba4006 
CASE	enc256.w2	enc key 256 bits: rd_key[16..23]	0	0	f0df87ae 681bf10f fbd58ea6 6715fc03 48f1e16d 924fa56f 53ebf875 8d51b873 
CASE	enc256.w3	enc key 256 bits: rd_key[24..31]	0	0	7f8256c6 1799a7c9 ec4c296f 8b59d56c 753ae23d e7754752 b49ebf27 39cf0754 
CASE	enc256.w4	enc key 256 bits: rd_key[32..39]	0	0	5f90dc0b 48097bc2 a44552ad 2f1c87c1 60a6f545 87d3b217 334d0d30 0a820a64 
CASE	enc256.w5	enc key 256 bits: rd_key[40..47]	0	0	1cf7cf7c 54feb4be f0bbe613 dfa761d2 fefa1af0 7929a8e7 4a64a5d7 40e6afb3 
CASE	enc256.w6	enc key 256 bits: rd_key[48..55]	0	0	71fe4125 2500f59b d5bb1388 0a1c725a 99665a4e e04ff2a9 aa2b577e eacdf8cd 
CASE	enc256.w7	enc key 256 bits: rd_key[56..59]	0	0	cc79fc24 e97909bf 3cc21a37 36de686d 
CASE	dec256.h	dec key 256 bits: header	0	0	rc=0 rounds=14 used_words=60 bytes_changed_beyond_used=0 signal=0
CASE	dec256.w0	dec key 256 bits: rd_key[0..7]	0	0	cc79fc24 e97909bf 3cc21a37 36de686d ffd1f134 2faacebf 5fe2e9fc 6e015825 
CASE	dec256.w1	dec key 256 bits: rd_key[8..15]	0	0	eb48165e 0a354c38 46b77175 84e680dc 8005a3c8 d07b3f8b 70482743 31e3b1d9 
CASE	dec256.w2	dec key 256 bits: rd_key[16..23]	0	0	138e70b5 e17d5a66 4c823d4d c251f1a9 a37bda74 507e9c43 a03318c8 41ab969a 
CASE	dec256.w3	dec key 256 bits: rd_key[24..31]	0	0	1597a63c f2f32ad3 adff672b 8ed3cce4 f3c45ff8 f3054637 f04d848b e1988e52 
CASE	dec256.w4	dec key 256 bits: rd_key[32..39]	0	0	9a4069de e7648cef 5f0c4df8 232cabcf 1658d5ae 00c119cf 0348c2bc 11d50ad9 
CASE	dec256.w5	dec key 256 bits: rd_key[40..47]	0	0	bd68c615 7d24e531 b868c117 7c20e637 0f85d77f 1699cc61 0389db73 129dc865 
CASE	dec256.w6	dec key 256 bits: rd_key[48..55]	0	0	c940282a c04c2324 c54c2426 c4482720 1d181f1a 191c1b1e 15101712 11141316 
CASE	dec256.w7	dec key 256 bits: rd_key[56..59]	0	0	03020100 07060504 0b0a0908 0f0e0d0c 
CASE	bad	AES_set_encrypt_key(key, 100 bits) (last call)	-2	0	rc=-2 rounds=-1431655766 signal=0
END	50
fips_check.py
import re, struct
# reads the raw output of the probe (file stdout.log) and compares the encryption schedules with a plain FIPS-197 key expansion
rows = [l.rstrip('\n').split('\t') for l in open('stdout.log', encoding='utf-8')]


def logged(tag):
    words = []
    for r in rows:
        if r[0] == 'CASE' and re.fullmatch(tag + r'\.w\d', r[1]):
            words += r[5].split()
    return [int(w, 16) for w in words]


def gmul(a, b):
    r = 0
    for _ in range(8):
        if b & 1:
            r ^= a
        hi = a & 0x80
        a = (a << 1) & 0xff
        if hi:
            a ^= 0x1b
        b >>= 1
    return r


sbox = []
for x in range(256):
    b = next((y for y in range(1, 256) if gmul(x, y) == 1), 0) if x else 0
    s = b
    for i in range(1, 5):
        s ^= ((b << i) | (b >> (8 - i))) & 0xff
    sbox.append(s ^ 0x63)


def expand(key):
    nk = len(key) // 4
    nr = nk + 6
    w = [list(key[4 * i:4 * i + 4]) for i in range(nk)]
    rc = 1
    for i in range(nk, 4 * (nr + 1)):
        t = w[i - 1][:]
        if i % nk == 0:
            t = [sbox[t[1]] ^ rc, sbox[t[2]], sbox[t[3]], sbox[t[0]]]
            rc = gmul(rc, 2)
        elif nk > 6 and i % nk == 4:
            t = [sbox[x] for x in t]
        w.append([a ^ b for a, b in zip(w[i - nk], t)])
    return w


for bits in (128, 192, 256):
    words = logged('enc%d' % bits)
    expected = [struct.unpack('<I', bytes(x))[0] for x in expand(bytes(range(bits // 8)))]
    print(bits, 'bits:', len(words), 'words logged, equal to the FIPS-197 expansion with every word stored little-endian:', words == expected)

AI-assisted (Claude Code).

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against f996fa29d5:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl_aes_tests built cleanly.
  • CTest (guest_libressl_aes):
    1/1 Test #4317: guest_libressl_aes ...............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl/src/Aes.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4317: guest_libressl_aes ...............Subprocess aborted***Exception:   0.12 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ExpandKey and LoadLittle32 in core/libs/prx/libSceLibreSsl/src/Aes.cpp for 128, 192, and 256-bit key schedule generation matching PS5 console little-endian word layout
    • Verified InverseMixWord byte unpacking and reconstruction shift orders (8 * row) in core/libs/prx/libSceLibreSsl/src/Aes.cpp for decrypt key inversion correctness
    • Verified LoadState and AddRoundKey shift logic (8 * row) in core/libs/prx/libSceLibreSsl/src/Aes.cpp ensuring roundKey bits correspond to the state bytes identically for both encryption and decryption
    • Verified error handling and return codes in SetKey (null userKey/key returns -1, invalid bit length returns -2) and memory safety with GuestCopyIn/GuestCopyOut and fault boundaries
    • Verified technical debt documentation fragment in docs/dev/debt/changes/e-docs-libressl-aes-measured.md and Oracle/guest test coverage against AGENTS.md rules

Looks Good To Me

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