diff --git a/configure.py b/configure.py index 6bdefa33..b4f80310 100644 --- a/configure.py +++ b/configure.py @@ -880,7 +880,7 @@ config.libs = [ Object(Matching, "scene/setting/iplRakuRakuThread.cpp"), Object(NonMatching, "scene/setting/AOSS.c", extra_cflags=["-O4,p", "-inline off"]), Object(Matching, "scene/setting/AOSSData.c"), - Object(NonMatching, "scene/setting/ATERM.c", extra_cflags=["-O4,p", "-inline on"]), + Object(NonMatching, "scene/setting/ATERM.c", mw_version="GC/3.0a5", extra_cflags=["-O4,p", "-inline on"]), ] ), IPLSection("settingSelect", [ diff --git a/src/scene/setting/ATERM.c b/src/scene/setting/ATERM.c index dcff9af1..29a49560 100644 --- a/src/scene/setting/ATERM.c +++ b/src/scene/setting/ATERM.c @@ -532,7 +532,7 @@ extern u8 gAtermDigestFill[64]; extern u8* gAtermRequestOptions; int ATERMAesKeyWrap(u16* destination, u16* source, u32 length, void* key, u32 keyLength); int ATERMAesKeyUnwrap(u16* destination, u16* source, u32 length, void* key, u32 keyLength); -int ATERMAesExpandEncryptKey(u32* expandedKey, const void* key, u32 keyBits); +int ATERMAesExpandEncryptKey(u32* roundKey, const void* key, int keyBits); int ATERMAesExpandDecryptKey(u32* expandedKey, const void* key, u32 keyBits); void ATERMAesEncryptBlock(const u32* expandedKey, u32 rounds, const u8* input, u8* output); void ATERMAesDecryptBlock(const u32* expandedKey, u32 rounds, const u8* input, u8* output); @@ -1901,120 +1901,91 @@ int ATERMAesKeyUnwrap(u16* destination, u16* source, u32 length, void* key, u32 return result; } -#define ATERM_AES_SUB_BYTE(value) (gAtermAesSubstitutionTable[(value)] & 0xFF) -#define ATERM_AES_SUB_WORD(value) \ - ((ATERM_AES_SUB_BYTE(((value) >> 24) & 0xFF) << 24) | \ - (ATERM_AES_SUB_BYTE(((value) >> 16) & 0xFF) << 16) | \ - (ATERM_AES_SUB_BYTE(((value) >> 8) & 0xFF) << 8) | \ - ATERM_AES_SUB_BYTE((value) & 0xFF)) +#define ATERM_AES_GETU32(bytes) \ + (((u32)(bytes)[0] << 24) ^ ((u32)(bytes)[1] << 16) ^ ((u32)(bytes)[2] << 8) ^ ((u32)(bytes)[3])) -#define ATERM_AES_READ_KEY_WORD(index) \ - (((u32)keyBytes[(index) * 4] << 24) ^ \ - ((u32)keyBytes[(index) * 4 + 1] << 16) ^ \ - ((u32)keyBytes[(index) * 4 + 2] << 8) ^ \ - keyBytes[(index) * 4 + 3]) - -int ATERMAesExpandEncryptKey(u32* expandedKey, const void* key, u32 keyBits) { +int ATERMAesExpandEncryptKey(u32* roundKey, const void* key, int keyBits) { const u8* keyBytes = (const u8*)key; - u32* roundKey = expandedKey; - + int round = 0; u32 keyWord; - s32 generatedRounds = 0; - u32 initialKey[4]; - s32 wordIndex; - for (wordIndex = 0; wordIndex < 4; wordIndex++) { - initialKey[wordIndex] = - (((u32)keyBytes[wordIndex * 4] << 24) ^ - ((u32)keyBytes[wordIndex * 4 + 1] << 16)) ^ - ((u32)keyBytes[wordIndex * 4 + 2] << 8) ^ - keyBytes[wordIndex * 4 + 3]; - } - for (wordIndex = 0; wordIndex < 4; wordIndex++) { - expandedKey[wordIndex] = initialKey[wordIndex]; - } - - if ((s32)keyBits == 0x80) { - const u32* roundConstant; - roundConstant = gAtermAesRoundConstants; - do { + roundKey[0] = ATERM_AES_GETU32(keyBytes); + roundKey[1] = ATERM_AES_GETU32(keyBytes + 4); + roundKey[2] = ATERM_AES_GETU32(keyBytes + 8); + roundKey[3] = ATERM_AES_GETU32(keyBytes + 12); + if (keyBits == 128) { + for (;;) { keyWord = roundKey[3]; - roundKey[4] = - (*roundConstant++ ^ - ((gAtermAesSubstitutionTable[keyWord >> 24] & 0xFF) ^ - (gAtermAesSubstitutionTable[keyWord & 0xFF] & 0xFF00))) ^ - ((roundKey[0] ^ - (gAtermAesSubstitutionTable[(keyWord >> 16) & 0xFF] & 0xFF000000)) ^ - (gAtermAesSubstitutionTable[(keyWord >> 8) & 0xFF] & 0xFF0000)); + roundKey[4] = roundKey[0] ^ + (gAtermAesSubstitutionTable[(keyWord >> 16) & 0xFF] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(keyWord >> 8) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[keyWord & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[keyWord >> 24] & 0x000000FF) ^ + gAtermAesRoundConstants[round]; roundKey[5] = roundKey[1] ^ roundKey[4]; roundKey[6] = roundKey[2] ^ roundKey[5]; roundKey[7] = roundKey[3] ^ roundKey[6]; - if (++generatedRounds == 10) { + if (++round == 10) { return 10; } roundKey += 4; - } while (1); + } } - - expandedKey[4] = (((u32)keyBytes[19] ^ ((u32)keyBytes[18] << 8)) ^ (((u32)keyBytes[16] << 24) ^ ((u32)keyBytes[17] << 16))); - expandedKey[5] = (((u32)keyBytes[23] ^ ((u32)keyBytes[22] << 8)) ^ (((u32)keyBytes[20] << 24) ^ ((u32)keyBytes[21] << 16))); - if ((s32)keyBits == 0xC0) { - const u32* substitution; - const u32* roundConstant; - roundConstant = gAtermAesRoundConstants; - substitution = gAtermAesSubstitutionTable; - do { + roundKey[4] = ATERM_AES_GETU32(keyBytes + 16); + roundKey[5] = ATERM_AES_GETU32(keyBytes + 20); + if (keyBits == 192) { + for (;;) { keyWord = roundKey[5]; - roundKey[6] = (*roundConstant++ ^ ((substitution[keyWord >> 24] & 0xFF) ^ (substitution[keyWord & 0xFF] & 0xFF00))) ^ ((roundKey[0] ^ (substitution[(keyWord >> 16) & 0xFF] & 0xFF000000)) ^ (substitution[(keyWord >> 8) & 0xFF] & 0xFF0000)); + roundKey[6] = roundKey[0] ^ + (gAtermAesSubstitutionTable[(keyWord >> 16) & 0xFF] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(keyWord >> 8) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[keyWord & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[keyWord >> 24] & 0x000000FF) ^ + gAtermAesRoundConstants[round]; roundKey[7] = roundKey[1] ^ roundKey[6]; roundKey[8] = roundKey[2] ^ roundKey[7]; roundKey[9] = roundKey[3] ^ roundKey[8]; - if (++generatedRounds == 8) { + if (++round == 8) { return 12; } roundKey[10] = roundKey[4] ^ roundKey[9]; roundKey[11] = roundKey[5] ^ roundKey[10]; roundKey += 6; - } while (1); - return 12; + } } - - expandedKey[6] = (((u32)keyBytes[27] ^ ((u32)keyBytes[26] << 8)) ^ (((u32)keyBytes[24] << 24) ^ ((u32)keyBytes[25] << 16))); - expandedKey[7] = (((u32)keyBytes[31] ^ ((u32)keyBytes[30] << 8)) ^ (((u32)keyBytes[28] << 24) ^ ((u32)keyBytes[29] << 16))); - if ((s32)keyBits == 0x100) { - const u32* substitution; - const u32* roundConstant; - roundConstant = gAtermAesRoundConstants; - substitution = gAtermAesSubstitutionTable; - do { + roundKey[6] = ATERM_AES_GETU32(keyBytes + 24); + roundKey[7] = ATERM_AES_GETU32(keyBytes + 28); + if (keyBits == 256) { + for (;;) { keyWord = roundKey[7]; - roundKey[8] = (*roundConstant++ ^ ((substitution[keyWord >> 24] & 0xFF) ^ (substitution[keyWord & 0xFF] & 0xFF00))) ^ ((roundKey[0] ^ (substitution[(keyWord >> 16) & 0xFF] & 0xFF000000)) ^ (substitution[(keyWord >> 8) & 0xFF] & 0xFF0000)); + roundKey[8] = roundKey[0] ^ + (gAtermAesSubstitutionTable[(keyWord >> 16) & 0xFF] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(keyWord >> 8) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[keyWord & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[keyWord >> 24] & 0x000000FF) ^ + gAtermAesRoundConstants[round]; roundKey[9] = roundKey[1] ^ roundKey[8]; roundKey[10] = roundKey[2] ^ roundKey[9]; roundKey[11] = roundKey[3] ^ roundKey[10]; - if (++generatedRounds == 7) { + if (++round == 7) { return 14; } keyWord = roundKey[11]; roundKey[12] = roundKey[4] ^ - (substitution[keyWord >> 24] & 0xFF000000) ^ - (substitution[(keyWord >> 16) & 0xFF] & 0x00FF0000) ^ - (substitution[(keyWord >> 8) & 0xFF] & 0x0000FF00) ^ - (substitution[keyWord & 0xFF] & 0x000000FF); + (gAtermAesSubstitutionTable[keyWord >> 24] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(keyWord >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[(keyWord >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[keyWord & 0xFF] & 0x000000FF); roundKey[13] = roundKey[5] ^ roundKey[12]; roundKey[14] = roundKey[6] ^ roundKey[13]; roundKey[15] = roundKey[7] ^ roundKey[14]; roundKey += 8; - } while (1); - return 14; + } } return 0; } -#undef ATERM_AES_READ_KEY_WORD -#undef ATERM_AES_SUB_WORD -#undef ATERM_AES_SUB_BYTE -#undef ATERM_AES_SUB_BYTE +#undef ATERM_AES_GETU32 #define ATERM_AES_TRANSFORM_KEY(roundKey, index) \ diff --git a/tools/decomp-assist/opus-aterm.attempts.md b/tools/decomp-assist/opus-aterm.attempts.md new file mode 100644 index 00000000..a6a96cff --- /dev/null +++ b/tools/decomp-assist/opus-aterm.attempts.md @@ -0,0 +1,40 @@ +# opus-aterm attempts (ATERM, 2026-10-09) + +Baseline 72e9c8d6: ATERM 20/26 exact; open: BuildEncryptedMessage 2, AutoConfigThread 3, +AesExpandEncryptKey 6, DiscoverAccessPoints 17, BuildAssociationRequest 28, RunConfigProtocol 294. + +## Pragma sweep (combosweep, GC/3.0a5.2) + +Single and pairwise pragmas: no function improves (BEST = base for all six). + +## Compiler version (new finding) + +Compiled the unchanged ATERM.c with every GC/Wii compiler that accepts the unit's flags: + +| compiler | exact | total diffs | AutoConfigThread | +| --- | ---: | ---: | ---: | +| GC/3.0a3, 3.0a3.2, 3.0a3.3, 3.0a3.4, 3.0a5 | 21/26 | 347 | 0 | +| GC/3.0a5.2 (previous) | 20/26 | 350 | 3 | +| Wii/* | 7/26 | 1590+ | 5 | + +GC/3.0a5 makes ATERMi_AutoConfigThread exact with no other function changing; pool identical, +data 100%. The other third-party middleware in this project (Zi Corp eZiText, NHTTP, bte) is already +built with GC/3.0a5. Set `mw_version="GC/3.0a5"` on ATERM.c. + +## ATERMBuildEncryptedMessage (2: entry `mr r29,r7` / `mr r3,r27` swapped) + +Read MWCC's scheduler (rayanht/mwcc decomp: Scheduler.c, MachineSimulation750.c) and wrote a model +(/tmp/opus-aterm-sched/sim.py). It reproduces our entry block and six micro-tests exactly (count of +unlocked successors, then height, then opcode rank mr < li, then program order; 2 IU slots). +The entry block is scheduled once, before register allocation (block flag 8 stops the post-RA pass). +Exhaustive search over that model: no order of the same ten PCode instructions with the parameter +copies first gives the target, and no single extra/deleted instruction does either. The target order +needs the copy of r7 to come after memset's first-argument move in the scheduler input. + +## ATERMAesExpandEncryptKey (6 -> 0, EXACT) + +The function is rijndaelKeySetupEnc from the public reference rijndael-alg-fst.c (Te4 = the +substitution table, rcon = the round constants). Earlier forms (initialKey[] array + copy loop, +hand-reassociated XOR trees) left the second key word's two XOR halves scheduled in the other order. +Trials (all GC/3.0a5): array/scalar/direct stores x seven XOR associations: 9..268; reference +GETU32 + reference loops verbatim: 3 (only cmplwi vs cmpwi); with the reference `int keyBits`: 0.