From 1f44a4e644386eae5ef9a587cb6fb8d872e76930 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Thu, 1 Oct 2026 00:58:39 +0000 Subject: [PATCH] ATERM 14/26 (fuzzy 92 -> 96) (worker 8f2e6497) --- README.md | 4 +- src/scene/setting/ATERM.c | 368 ++++++++++++++------------ tools/decomp-assist/ATERM.attempts.md | 184 +++++++++++++ 3 files changed, 378 insertions(+), 178 deletions(-) create mode 100644 tools/decomp-assist/ATERM.attempts.md diff --git a/README.md b/README.md index 44a1c4e4..ec3a1e42 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 98.85% | 86.18% | 60.34% | 90.91% | +| 98.88% | 86.27% | 60.34% | 90.91% | -units 902/1027 complete, functions 12129/12563 matched. +units 902/1027 complete, functions 12132/12563 matched. decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data diff --git a/src/scene/setting/ATERM.c b/src/scene/setting/ATERM.c index 7f89d438..a3dc09c3 100644 --- a/src/scene/setting/ATERM.c +++ b/src/scene/setting/ATERM.c @@ -542,7 +542,7 @@ s32 gAtermState; void ATERM_8140684C(OSAlarm* alarm, OSContext* context); s32 ATERM_814038C8(void); -void ATERM_81405ACC(AtermMd5Context* context, const void* input, u32 length); +void ATERM_81405ACC(AtermMd5Context* context, void* input, u32 length); extern u8 gAtermDigestFill[64]; extern u8* gAtermRequestOptions; int ATERM_81404844(u16* destination, u16* source, u32 length, void* key, u32 keyLength); @@ -616,7 +616,7 @@ int ATERM_814021BC(void) { gAtermSocketReady = waitCount; } if (gAtermSocketReady != 0) { - while (SOHtoNl(0) == SOGetHostID()) { + while (SOGetHostID() == SOHtoNl(0)) { OSInitMessageQueue(&hostQueue, &hostQueueBuffer, 1); OSCreateAlarm(&hostAlarm); OSSetAlarmTag(&hostAlarm, (u32)&hostQueue); @@ -875,7 +875,6 @@ int ATERM_81402A24(void) { u8* rawScanBuffer = NULL; u8* scanBuffer; u16* firstDescriptor; - AtermApRecord* recordCursor; u32 recordBytes = gAtermScanLimit * sizeof(AtermApRecord) + sizeof(AtermApRecord) + sizeof(u32); u32 scanBufferBytes; u32 scanCount; @@ -901,7 +900,7 @@ int ATERM_81402A24(void) { } scanBufferBytes = gAtermScanLimit * 0x100; rawScanBuffer = (u8*)gAtermAllocate(scanBufferBytes + 0x40); - scanBuffer = (u8*)(((u32)rawScanBuffer + 0x1F) & ~0x1F); + scanBuffer = (u8*)(u32)(((u64)(u32)rawScanBuffer + 0x1F) & ~0x1FULL); firstDescriptor = (u16*)(scanBuffer + sizeof(u16)); while (iteration < 300 && gAtermCancelRequested == 0) { @@ -928,24 +927,25 @@ int ATERM_81402A24(void) { u16* descriptorWords = firstDescriptor; char* ssidCursor = (char*)currentRecords->entries[0].ssid; u8* bssidCursor = currentRecords->entries[0].bssid; - recordCursor = currentRecords->entries; recordIndex = 0; while ((s32)recordIndex < result) { WDBssDesc* descriptor = (WDBssDesc*)descriptorWords; - memcpy(ssidCursor, descriptor->ssid, sizeof(recordCursor->ssid)); - if (descriptor->ssidLength > sizeof(recordCursor->ssid)) { - recordCursor->ssidLength = 0; + memcpy(ssidCursor, descriptor->ssid, + sizeof(currentRecords->entries[recordIndex].ssid)); + if (descriptor->ssidLength > sizeof(currentRecords->entries[recordIndex].ssid)) { + currentRecords->entries[recordIndex].ssidLength = 0; } else { - recordCursor->ssidLength = descriptor->ssidLength; + currentRecords->entries[recordIndex].ssidLength = descriptor->ssidLength; } - recordCursor->ssid[recordCursor->ssidLength] = '\0'; - recordCursor->status = (descriptor->capabilities >> 4) & 1; - memcpy(bssidCursor, descriptor->bssid, sizeof(recordCursor->bssid)); + currentRecords->entries[recordIndex].ssid[ + currentRecords->entries[recordIndex].ssidLength] = '\0'; + currentRecords->entries[recordIndex].status = (descriptor->capabilities >> 4) & 1; + memcpy(bssidCursor, descriptor->bssid, + sizeof(currentRecords->entries[recordIndex].bssid)); descriptorWords += descriptor->length; ssidCursor += sizeof(AtermApRecord); bssidCursor += sizeof(AtermApRecord); - recordCursor++; recordIndex++; } } @@ -1646,26 +1646,23 @@ s32 ATERM_814038C8(void) { (__mulhwu(reciprocal, OS_BUS_CLOCK >> 2) >> 6)); u32 index; u32 digestBytes; - u32 bitCount[2]; memcpy(response->authentication.challenge, optionValue, 8); + digestContext.bitCountHigh = 0; + digestContext.bitCountLow = 0; digestContext.state[0] = 0x67452301; digestContext.state[1] = 0xEFCDAB89; digestContext.state[2] = 0x98BADCFE; digestContext.state[3] = 0x10325476; - digestContext.bitCountLow = 0; - digestContext.bitCountHigh = 0; ATERM_81405ACC(&digestContext, &authenticationTime, sizeof(authenticationTime)); - bitCount[0] = digestContext.bitCountLow; - bitCount[1] = digestContext.bitCountHigh; - digestLength[0] = bitCount[0]; - digestLength[1] = bitCount[0] >> 8; - digestLength[2] = bitCount[0] >> 16; - digestLength[3] = bitCount[0] >> 24; - digestLength[4] = bitCount[1]; - digestLength[5] = bitCount[1] >> 8; - digestLength[6] = bitCount[1] >> 16; - digestLength[7] = bitCount[1] >> 24; + digestLength[0] = digestContext.bitCountLow; + digestLength[1] = digestContext.bitCountLow >> 8; + digestLength[2] = digestContext.bitCountLow >> 16; + digestLength[3] = digestContext.bitCountLow >> 24; + digestLength[4] = digestContext.bitCountHigh; + digestLength[5] = digestContext.bitCountHigh >> 8; + digestLength[6] = digestContext.bitCountHigh >> 16; + digestLength[7] = digestContext.bitCountHigh >> 24; digestBytes = (digestContext.bitCountLow >> 3) & 0x3F; ATERM_81405ACC(&digestContext, gAtermDigestFill, digestBytes < 56 ? 56 - digestBytes : 120 - digestBytes); @@ -2095,39 +2092,26 @@ int ATERM_81404BFC(u32* expandedKey, const void* key, u32 keyBits) { int ATERM_8140502C(u32* expandedKey, const void* key, u32 keyBits) { int rounds = ATERM_81404BFC(expandedKey, key, keyBits); - u32* firstRound = expandedKey; - u32* lastRound = expandedKey + rounds * 4; u32* roundKey; s32 roundCount; + s32 firstIndex; + s32 lastIndex; + u32 word; - s32 lastIndex = rounds * 4; - s32 firstIndex = 0; - - while (firstIndex < lastIndex) { - firstIndex += 4; - lastIndex -= 4; - { - u32 word = firstRound[0]; - firstRound[0] = lastRound[0]; - lastRound[0] = word; - } - { - u32 word = firstRound[1]; - firstRound[1] = lastRound[1]; - lastRound[1] = word; - } - { - u32 word = firstRound[2]; - firstRound[2] = lastRound[2]; - lastRound[2] = word; - } - { - u32 word = firstRound[3]; - firstRound[3] = lastRound[3]; - lastRound[3] = word; - } - firstRound += 4; - lastRound -= 4; + for (firstIndex = 0, lastIndex = rounds * 4; + firstIndex < lastIndex; firstIndex += 4, lastIndex -= 4) { + word = expandedKey[firstIndex]; + expandedKey[firstIndex] = expandedKey[lastIndex]; + expandedKey[lastIndex] = word; + word = expandedKey[firstIndex + 1]; + expandedKey[firstIndex + 1] = expandedKey[lastIndex + 1]; + expandedKey[lastIndex + 1] = word; + word = expandedKey[firstIndex + 2]; + expandedKey[firstIndex + 2] = expandedKey[lastIndex + 2]; + expandedKey[lastIndex + 2] = word; + word = expandedKey[firstIndex + 3]; + expandedKey[firstIndex + 3] = expandedKey[lastIndex + 3]; + expandedKey[lastIndex + 3] = word; } { @@ -2156,151 +2140,183 @@ int ATERM_8140502C(u32* expandedKey, const void* key, u32 keyBits) { #undef ATERM_AES_TRANSFORM_KEY void ATERM_81405254(const u32* expandedKey, u32 rounds, const u8* input, u8* output) { - const u32* roundTable3 = gAtermAesEncryptTable3; - const u32* roundTable2 = gAtermAesEncryptTable2; - const u32* roundTable0 = gAtermAesEncryptTable0; - const u32* roundTable1 = gAtermAesEncryptTable1; - s32 roundPairs = (s32)rounds >> 1; - u32 state0 = (((u32)input[2] << 8) ^ input[3]) ^ - (((u32)input[0] << 24) ^ ((u32)input[1] << 16)) ^ expandedKey[0]; - u32 state1 = (((u32)input[6] << 8) ^ input[7]) ^ - (((u32)input[4] << 24) ^ ((u32)input[5] << 16)) ^ expandedKey[1]; - u32 state2 = (((u32)input[10] << 8) ^ input[11]) ^ - (((u32)input[8] << 24) ^ ((u32)input[9] << 16)) ^ expandedKey[2]; - u32 state3 = (((u32)input[14] << 8) ^ input[15]) ^ - (((u32)input[12] << 24) ^ ((u32)input[13] << 16)) ^ expandedKey[3]; + u32 state0, state1, state2, state3; u32 next0, next1, next2, next3; + s32 roundPairs; + state0 = ((u32)input[0] << 24) ^ ((u32)input[1] << 16) ^ + ((u32)input[2] << 8) ^ (u32)input[3] ^ expandedKey[0]; + state1 = ((u32)input[4] << 24) ^ ((u32)input[5] << 16) ^ + ((u32)input[6] << 8) ^ (u32)input[7] ^ expandedKey[1]; + state2 = ((u32)input[8] << 24) ^ ((u32)input[9] << 16) ^ + ((u32)input[10] << 8) ^ (u32)input[11] ^ expandedKey[2]; + state3 = ((u32)input[12] << 24) ^ ((u32)input[13] << 16) ^ + ((u32)input[14] << 8) ^ (u32)input[15] ^ expandedKey[3]; + roundPairs = (s32)rounds >> 1; for (;;) { - next0 = (roundTable3[state3 & 0xFF] ^ roundTable2[(state2 >> 8) & 0xFF]) ^ - (roundTable0[state0 >> 24] ^ roundTable1[(state1 >> 16) & 0xFF]) ^ expandedKey[4]; - next1 = (roundTable3[state0 & 0xFF] ^ roundTable2[(state3 >> 8) & 0xFF]) ^ - (roundTable0[state1 >> 24] ^ roundTable1[(state2 >> 16) & 0xFF]) ^ expandedKey[5]; - next2 = (roundTable3[state1 & 0xFF] ^ roundTable2[(state0 >> 8) & 0xFF]) ^ - (roundTable0[state2 >> 24] ^ roundTable1[(state3 >> 16) & 0xFF]) ^ expandedKey[6]; - next3 = (roundTable3[state2 & 0xFF] ^ roundTable2[(state1 >> 8) & 0xFF]) ^ - (roundTable0[state3 >> 24] ^ roundTable1[(state0 >> 16) & 0xFF]) ^ expandedKey[7]; + next0 = (gAtermAesEncryptTable3[state3 & 0xFF] ^ + gAtermAesEncryptTable2[(state2 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[state0 >> 24] ^ + gAtermAesEncryptTable1[(state1 >> 16) & 0xFF]) ^ expandedKey[4]; + next1 = (gAtermAesEncryptTable3[state0 & 0xFF] ^ + gAtermAesEncryptTable2[(state3 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[state1 >> 24] ^ + gAtermAesEncryptTable1[(state2 >> 16) & 0xFF]) ^ expandedKey[5]; + next2 = (gAtermAesEncryptTable3[state1 & 0xFF] ^ + gAtermAesEncryptTable2[(state0 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[state2 >> 24] ^ + gAtermAesEncryptTable1[(state3 >> 16) & 0xFF]) ^ expandedKey[6]; + next3 = (gAtermAesEncryptTable3[state2 & 0xFF] ^ + gAtermAesEncryptTable2[(state1 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[state3 >> 24] ^ + gAtermAesEncryptTable1[(state0 >> 16) & 0xFF]) ^ expandedKey[7]; expandedKey += 8; if (--roundPairs == 0) { break; } - state0 = (roundTable3[next3 & 0xFF] ^ roundTable2[(next2 >> 8) & 0xFF]) ^ - (roundTable0[next0 >> 24] ^ roundTable1[(next1 >> 16) & 0xFF]) ^ expandedKey[0]; - state1 = (roundTable3[next0 & 0xFF] ^ roundTable2[(next3 >> 8) & 0xFF]) ^ - (roundTable0[next1 >> 24] ^ roundTable1[(next2 >> 16) & 0xFF]) ^ expandedKey[1]; - state2 = (roundTable3[next1 & 0xFF] ^ roundTable2[(next0 >> 8) & 0xFF]) ^ - (roundTable0[next2 >> 24] ^ roundTable1[(next3 >> 16) & 0xFF]) ^ expandedKey[2]; - state3 = (roundTable3[next2 & 0xFF] ^ roundTable2[(next1 >> 8) & 0xFF]) ^ - (roundTable0[next3 >> 24] ^ roundTable1[(next0 >> 16) & 0xFF]) ^ expandedKey[3]; + state0 = (gAtermAesEncryptTable3[next3 & 0xFF] ^ + gAtermAesEncryptTable2[(next2 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[next0 >> 24] ^ + gAtermAesEncryptTable1[(next1 >> 16) & 0xFF]) ^ expandedKey[0]; + state1 = (gAtermAesEncryptTable3[next0 & 0xFF] ^ + gAtermAesEncryptTable2[(next3 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[next1 >> 24] ^ + gAtermAesEncryptTable1[(next2 >> 16) & 0xFF]) ^ expandedKey[1]; + state2 = (gAtermAesEncryptTable3[next1 & 0xFF] ^ + gAtermAesEncryptTable2[(next0 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[next2 >> 24] ^ + gAtermAesEncryptTable1[(next3 >> 16) & 0xFF]) ^ expandedKey[2]; + state3 = (gAtermAesEncryptTable3[next2 & 0xFF] ^ + gAtermAesEncryptTable2[(next1 >> 8) & 0xFF]) ^ + (gAtermAesEncryptTable0[next3 >> 24] ^ + gAtermAesEncryptTable1[(next0 >> 16) & 0xFF]) ^ expandedKey[3]; } - { - const u32* finalTable = gAtermAesSubstitutionTable; - u32 result0 = (((finalTable[next0 >> 24] & 0xFF000000) ^ - (finalTable[(next1 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next2 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next3 & 0xFF] & 0x000000FF)) ^ expandedKey[0]); - u32 result1 = (((finalTable[next1 >> 24] & 0xFF000000) ^ - (finalTable[(next2 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next3 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next0 & 0xFF] & 0x000000FF)) ^ expandedKey[1]); - u32 result2 = (((finalTable[next2 >> 24] & 0xFF000000) ^ - (finalTable[(next3 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next0 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next1 & 0xFF] & 0x000000FF)) ^ expandedKey[2]); - u32 result3 = (((finalTable[next3 >> 24] & 0xFF000000) ^ - (finalTable[(next0 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next1 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next2 & 0xFF] & 0x000000FF)) ^ expandedKey[3]); - - output[0] = result0 >> 24; - output[1] = result0 >> 16; - output[2] = result0 >> 8; - output[3] = result0; - output[4] = result1 >> 24; - output[5] = result1 >> 16; - output[6] = result1 >> 8; - output[7] = result1; - output[8] = result2 >> 24; - output[9] = result2 >> 16; - output[10] = result2 >> 8; - output[11] = result2; - output[12] = result3 >> 24; - output[13] = result3 >> 16; - output[14] = result3 >> 8; - output[15] = result3; - } + state0 = (gAtermAesSubstitutionTable[next0 >> 24] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(next1 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[(next2 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[next3 & 0xFF] & 0x000000FF) ^ expandedKey[0]; + output[0] = state0 >> 24; + output[1] = state0 >> 16; + output[2] = state0 >> 8; + output[3] = state0; + state1 = (gAtermAesSubstitutionTable[next1 >> 24] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(next2 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[(next3 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[next0 & 0xFF] & 0x000000FF) ^ expandedKey[1]; + output[4] = state1 >> 24; + output[5] = state1 >> 16; + output[6] = state1 >> 8; + output[7] = state1; + state2 = (gAtermAesSubstitutionTable[next2 >> 24] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(next3 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[(next0 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[next1 & 0xFF] & 0x000000FF) ^ expandedKey[2]; + output[8] = state2 >> 24; + output[9] = state2 >> 16; + output[10] = state2 >> 8; + output[11] = state2; + state3 = (gAtermAesSubstitutionTable[next3 >> 24] & 0xFF000000) ^ + (gAtermAesSubstitutionTable[(next0 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesSubstitutionTable[(next1 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesSubstitutionTable[next2 & 0xFF] & 0x000000FF) ^ expandedKey[3]; + output[12] = state3 >> 24; + output[13] = state3 >> 16; + output[14] = state3 >> 8; + output[15] = state3; } void ATERM_81405690(const u32* expandedKey, u32 rounds, const u8* input, u8* output) { - s32 roundPairs = (s32)rounds >> 1; - u32 state0 = (((u32)input[0] << 24) ^ ((u32)input[1] << 16) ^ ((u32)input[2] << 8) ^ input[3]) ^ expandedKey[0]; - u32 state1 = (((u32)input[4] << 24) ^ ((u32)input[5] << 16) ^ ((u32)input[6] << 8) ^ input[7]) ^ expandedKey[1]; - u32 state2 = (((u32)input[8] << 24) ^ ((u32)input[9] << 16) ^ ((u32)input[10] << 8) ^ input[11]) ^ expandedKey[2]; - u32 state3 = (((u32)input[12] << 24) ^ ((u32)input[13] << 16) ^ ((u32)input[14] << 8) ^ input[15]) ^ expandedKey[3]; + u32 state0, state1, state2, state3; u32 next0, next1, next2, next3; - const u32* roundTable3 = gAtermAesDecryptTable3; - const u32* roundTable2 = gAtermAesDecryptTable2; - const u32* roundTable0 = gAtermAesDecryptTable0; - const u32* roundTable1 = gAtermAesDecryptTable1; + s32 roundPairs; + state0 = ((u32)input[0] << 24) ^ ((u32)input[1] << 16) ^ + ((u32)input[2] << 8) ^ (u32)input[3] ^ expandedKey[0]; + state1 = ((u32)input[4] << 24) ^ ((u32)input[5] << 16) ^ + ((u32)input[6] << 8) ^ (u32)input[7] ^ expandedKey[1]; + state2 = ((u32)input[8] << 24) ^ ((u32)input[9] << 16) ^ + ((u32)input[10] << 8) ^ (u32)input[11] ^ expandedKey[2]; + state3 = ((u32)input[12] << 24) ^ ((u32)input[13] << 16) ^ + ((u32)input[14] << 8) ^ (u32)input[15] ^ expandedKey[3]; + roundPairs = (s32)rounds >> 1; for (;;) { - next0 = roundTable3[state1 & 0xFF] ^ roundTable2[(state2 >> 8) & 0xFF] ^ roundTable0[state0 >> 24] ^ roundTable1[(state3 >> 16) & 0xFF] ^ expandedKey[4]; - next1 = roundTable3[state2 & 0xFF] ^ roundTable2[(state3 >> 8) & 0xFF] ^ roundTable0[state1 >> 24] ^ roundTable1[(state0 >> 16) & 0xFF] ^ expandedKey[5]; - next2 = roundTable3[state3 & 0xFF] ^ roundTable2[(state0 >> 8) & 0xFF] ^ roundTable0[state2 >> 24] ^ roundTable1[(state1 >> 16) & 0xFF] ^ expandedKey[6]; - next3 = roundTable3[state0 & 0xFF] ^ roundTable2[(state1 >> 8) & 0xFF] ^ roundTable0[state3 >> 24] ^ roundTable1[(state2 >> 16) & 0xFF] ^ expandedKey[7]; + next0 = (gAtermAesDecryptTable3[state1 & 0xFF] ^ + gAtermAesDecryptTable2[(state2 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[state0 >> 24] ^ + gAtermAesDecryptTable1[(state3 >> 16) & 0xFF]) ^ expandedKey[4]; + next1 = (gAtermAesDecryptTable3[state2 & 0xFF] ^ + gAtermAesDecryptTable2[(state3 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[state1 >> 24] ^ + gAtermAesDecryptTable1[(state0 >> 16) & 0xFF]) ^ expandedKey[5]; + next2 = (gAtermAesDecryptTable3[state3 & 0xFF] ^ + gAtermAesDecryptTable2[(state0 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[state2 >> 24] ^ + gAtermAesDecryptTable1[(state1 >> 16) & 0xFF]) ^ expandedKey[6]; + next3 = (gAtermAesDecryptTable3[state0 & 0xFF] ^ + gAtermAesDecryptTable2[(state1 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[state3 >> 24] ^ + gAtermAesDecryptTable1[(state2 >> 16) & 0xFF]) ^ expandedKey[7]; expandedKey += 8; if (--roundPairs == 0) { break; } - state0 = roundTable3[next1 & 0xFF] ^ roundTable2[(next2 >> 8) & 0xFF] ^ roundTable0[next0 >> 24] ^ roundTable1[(next3 >> 16) & 0xFF] ^ expandedKey[0]; - state1 = roundTable3[next2 & 0xFF] ^ roundTable2[(next3 >> 8) & 0xFF] ^ roundTable0[next1 >> 24] ^ roundTable1[(next0 >> 16) & 0xFF] ^ expandedKey[1]; - state2 = roundTable3[next3 & 0xFF] ^ roundTable2[(next0 >> 8) & 0xFF] ^ roundTable0[next2 >> 24] ^ roundTable1[(next1 >> 16) & 0xFF] ^ expandedKey[2]; - state3 = roundTable3[next0 & 0xFF] ^ roundTable2[(next1 >> 8) & 0xFF] ^ roundTable0[next3 >> 24] ^ roundTable1[(next2 >> 16) & 0xFF] ^ expandedKey[3]; + state0 = (gAtermAesDecryptTable3[next1 & 0xFF] ^ + gAtermAesDecryptTable2[(next2 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[next0 >> 24] ^ + gAtermAesDecryptTable1[(next3 >> 16) & 0xFF]) ^ expandedKey[0]; + state1 = (gAtermAesDecryptTable3[next2 & 0xFF] ^ + gAtermAesDecryptTable2[(next3 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[next1 >> 24] ^ + gAtermAesDecryptTable1[(next0 >> 16) & 0xFF]) ^ expandedKey[1]; + state2 = (gAtermAesDecryptTable3[next3 & 0xFF] ^ + gAtermAesDecryptTable2[(next0 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[next2 >> 24] ^ + gAtermAesDecryptTable1[(next1 >> 16) & 0xFF]) ^ expandedKey[2]; + state3 = (gAtermAesDecryptTable3[next0 & 0xFF] ^ + gAtermAesDecryptTable2[(next1 >> 8) & 0xFF]) ^ + (gAtermAesDecryptTable0[next3 >> 24] ^ + gAtermAesDecryptTable1[(next2 >> 16) & 0xFF]) ^ expandedKey[3]; } - { - const u32* finalTable = gAtermAesInverseSubstitutionTable; - u32 result0 = (((finalTable[next0 >> 24] & 0xFF000000) ^ - (finalTable[(next3 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next2 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next1 & 0xFF] & 0x000000FF)) ^ expandedKey[0]); - u32 result1 = (((finalTable[next1 >> 24] & 0xFF000000) ^ - (finalTable[(next0 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next3 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next2 & 0xFF] & 0x000000FF)) ^ expandedKey[1]); - u32 result2 = (((finalTable[next2 >> 24] & 0xFF000000) ^ - (finalTable[(next1 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next0 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next3 & 0xFF] & 0x000000FF)) ^ expandedKey[2]); - u32 result3 = (((finalTable[next3 >> 24] & 0xFF000000) ^ - (finalTable[(next2 >> 16) & 0xFF] & 0x00FF0000) ^ - (finalTable[(next1 >> 8) & 0xFF] & 0x0000FF00) ^ - (finalTable[next0 & 0xFF] & 0x000000FF)) ^ expandedKey[3]); - - output[0] = result0 >> 24; - output[1] = result0 >> 16; - output[2] = result0 >> 8; - output[3] = result0; - output[4] = result1 >> 24; - output[5] = result1 >> 16; - output[6] = result1 >> 8; - output[7] = result1; - output[8] = result2 >> 24; - output[9] = result2 >> 16; - output[10] = result2 >> 8; - output[11] = result2; - output[12] = result3 >> 24; - output[13] = result3 >> 16; - output[14] = result3 >> 8; - output[15] = result3; - } + state0 = (gAtermAesInverseSubstitutionTable[next0 >> 24] & 0xFF000000) ^ + (gAtermAesInverseSubstitutionTable[(next3 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesInverseSubstitutionTable[(next2 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesInverseSubstitutionTable[next1 & 0xFF] & 0x000000FF) ^ expandedKey[0]; + output[0] = state0 >> 24; + output[1] = state0 >> 16; + output[2] = state0 >> 8; + output[3] = state0; + state1 = (gAtermAesInverseSubstitutionTable[next1 >> 24] & 0xFF000000) ^ + (gAtermAesInverseSubstitutionTable[(next0 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesInverseSubstitutionTable[(next3 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesInverseSubstitutionTable[next2 & 0xFF] & 0x000000FF) ^ expandedKey[1]; + output[4] = state1 >> 24; + output[5] = state1 >> 16; + output[6] = state1 >> 8; + output[7] = state1; + state2 = (gAtermAesInverseSubstitutionTable[next2 >> 24] & 0xFF000000) ^ + (gAtermAesInverseSubstitutionTable[(next1 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesInverseSubstitutionTable[(next0 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesInverseSubstitutionTable[next3 & 0xFF] & 0x000000FF) ^ expandedKey[2]; + output[8] = state2 >> 24; + output[9] = state2 >> 16; + output[10] = state2 >> 8; + output[11] = state2; + state3 = (gAtermAesInverseSubstitutionTable[next3 >> 24] & 0xFF000000) ^ + (gAtermAesInverseSubstitutionTable[(next2 >> 16) & 0xFF] & 0x00FF0000) ^ + (gAtermAesInverseSubstitutionTable[(next1 >> 8) & 0xFF] & 0x0000FF00) ^ + (gAtermAesInverseSubstitutionTable[next0 & 0xFF] & 0x000000FF) ^ expandedKey[3]; + output[12] = state3 >> 24; + output[13] = state3 >> 16; + output[14] = state3 >> 8; + output[15] = state3; } void ATERM_81405D0C(u32 state[4], const u8 block[64]); -void ATERM_81405ACC(AtermMd5Context* context, const void* input, u32 length) { - const u8* data = (const u8*)input; +void ATERM_81405ACC(AtermMd5Context* context, void* input, u32 length) { + u8* data = (u8*)input; u32 bufferIndex = (context->bitCountLow >> 3) & 0x3F; u32 bytesToFill; u32 copiedBytes; diff --git a/tools/decomp-assist/ATERM.attempts.md b/tools/decomp-assist/ATERM.attempts.md new file mode 100644 index 00000000..50f8dd46 --- /dev/null +++ b/tools/decomp-assist/ATERM.attempts.md @@ -0,0 +1,184 @@ +# ATERM matching attempts + +Baseline: 3bfbb0c9. Target: Wii Menu 4.3U. All sizes are bytes. + +Objdiff exact functions: 12/26 -> 15/26. Gate instruction-exact functions: 11/26 -> 14/26. Matched code: 6576/19204 -> 9296/19204. Matched data: 18504/18864 -> 18504/18864. Fuzzy code: 91.736305% -> 96.46365%. + +Three functions reached exact matching. Eleven remain open; each has at least three distinct source attempts below. Failed and unchanged experiments were reverted. The remaining functions are not all registers-only. + +| Function | Target bytes | Compiled bytes before -> after | Objdiff % before -> after | Final difference | +| --- | ---: | ---: | ---: | --- | +| ATERM_814021BC | 616 | 616 -> 616 | 96.61688 -> 97.91558 | Global address formation and load scheduling. | +| ATERM_8140276C | 696 | 696 -> 696 | 99.166664 -> 99.166664 | Register allocation only. | +| ATERM_81402A24 | 1052 | 1040 -> 1044 | 92.43346 -> 94.73384 | Two instructions short; scan loop and record addressing still differ. | +| ATERM_81402E40 | 384 | 384 -> 384 | 99.791664 -> 99.791664 | Two entry moves exchanged; CR1 branch differences in ctxdiff are an address-normalization artifact. | +| ATERM_814031DC | 532 | 516 -> 516 | 90.766914 -> 90.766914 | Four instructions short; formatting loops use branches rather than target CTR loops. | +| ATERM_814033F0 | 548 | 548 -> 548 | 99.56204 -> 99.56204 | Key cursor and zero constant swap r22/r23. | +| ATERM_814038C8 | 3804 | 3888 -> 3888 | 87.2755 -> 87.43323 | Twenty-one extra instructions; global base spills and local stack offsets differ. | +| ATERMi_AutoConfigThread | 160 | 160 -> 160 | 94.75 -> 94.75 | NOR destination register and global reload/shift scheduling. | +| ATERM_81404844 | 468 | 468 -> 468 | 99.0 -> 99.0 | Register allocation and block index/offset initialization order. | +| ATERM_81404BFC | 1072 | 1072 -> 1072 | 98.94403 -> 98.94403 | Initial byte-load/store scheduling and AES-128 table registers. | +| ATERM_8140502C | 552 | 552 -> 552 | 99.565216 -> 100.0 | Exact: 138 instructions, ctxdiff diffs 0. | +| ATERM_81405254 | 1084 | 1084 -> 1084 | 62.110703 -> 100.0 | Exact: 271 instructions, ctxdiff diffs 0. | +| ATERM_81405690 | 1084 | 1084 -> 1084 | 59.309963 -> 100.0 | Exact: 271 instructions, ctxdiff diffs 0. | +| ATERM_81405ACC | 576 | 556 -> 556 | 88.583336 -> 91.263885 | Five instructions short; byte-copy loops still use indexed rather than target pointer induction. | + +Every retained function has the target stack-frame size. The main function still assigns different offsets to some locals. + +## ATERM_814021BC + +- use one network-settings aggregate pointer: 96.61688% (154/154 instructions, compiled/target). +- IP view scoped after clear: 96.61688% (154/154 instructions, compiled/target). +- host ID call before network zero conversion: 97.91558% (154/154 instructions, compiled/target). +- Retained result: Global address formation and load scheduling. + +## ATERM_8140276C + +- found/result/index before record pointer views: 98.56322% (174/174 instructions, compiled/target). +- initialize result before found: 98.965515% (174/174 instructions, compiled/target). +- signed traversal locals with original unsigned bounds: 99.166664% (174/174 instructions, compiled/target). +- Retained result: Register allocation only. + +## ATERM_81402A24 + +- scan buffer alignment through wide unsigned address arithmetic: 93.78327% (261/263 instructions, compiled/target). +- retranslate descriptors with indexed record fields rather than interior cursor: 94.73384% (261/263 instructions, compiled/target). +- retranslate deadline comparison and counted MAC loop: 85.50571% (292/263 instructions, compiled/target). +- Retained result: Two instructions short; scan loop and record addressing still differ. + +## ATERM_81402E40 + +- wire payload length unsigned int: 99.791664% (96/96 instructions, compiled/target). +- opaque payload formal with writable halfword view: 99.791664% (96/96 instructions, compiled/target). +- byte encryption key formal: 99.791664% (96/96 instructions, compiled/target). +- payload-header-typed-view: 99.583336% (96/96 instructions, compiled/target). +- Retained result: Two entry moves exchanged; CR1 branch differences in ctxdiff are an address-normalization artifact. + +## ATERM_814031DC + +- retranslate both MAC loops as six iterations with conditional separator: 53.969925% (191/133 instructions, compiled/target). +- MAC loops with final-iteration continue: 53.969925% (191/133 instructions, compiled/target). +- MAC loop bound from actual address field size: 53.969925% (191/133 instructions, compiled/target). +- mac-encoding-do-while-six-bytes: 90.22556% (131/133 instructions, compiled/target). +- mac-do-while-with-separate-index-increment: 90.22556% (131/133 instructions, compiled/target). +- mac-counted-loop-with-unsigned-bound-comparison: 53.969925% (191/133 instructions, compiled/target). +- Retained result: Four instructions short; formatting loops use branches rather than target CTR loops. + +## ATERM_814033F0 + +- SSID-found flag initialized before cursor: 98.24818% (137/137 instructions, compiled/target). +- key pointer local to key options: 99.56204% (137/137 instructions, compiled/target). +- unsigned byte key view and unsigned found flag: 99.56204% (137/137 instructions, compiled/target). +- result-declared-before-key-with-explicit-init: 99.56204% (137/137 instructions, compiled/target). +- named-key-text-terminator-before-key: 99.56204% (137/137 instructions, compiled/target). +- indexed-source-byte-for-key-encoding: 95.985405% (139/137 instructions, compiled/target). +- Retained result: Key cursor and zero constant swap r22/r23. + +## ATERM_814038C8 + +- block1 defer response and session pointer initialization after protocol state: 87.2755% (972/951 instructions, compiled/target). +- block1 direct global response and session key at each use: 86.11251% (974/951 instructions, compiled/target). +- block1 session key direct global while response stays typed local: 86.66246% (972/951 instructions, compiled/target). +- block1 packet buffer local view shared by send and receive paths: 84.303894% (979/951 instructions, compiled/target). +- retranslate MD5 count initialization and length encoding without snapshot array: 87.43323% (972/951 instructions, compiled/target). +- Retained result: Twenty-one extra instructions; global base spills and local stack offsets differ. + +## ATERMi_AutoConfigThread + +- signed equality intermediate and arithmetic: 94.75% (40/40 instructions, compiled/target). +- result reload before final state calculation: 91.45% (40/40 instructions, compiled/target). +- natural conditional completed state: 94.0% (40/40 instructions, compiled/target). +- completed state subtracts equality: 72.1% (39/40 instructions, compiled/target). +- state from nested signed expression: 94.75% (40/40 instructions, compiled/target). +- Retained result: NOR destination register and global reload/shift scheduling. + +## ATERM_81404844 + +- signed AES round-count local: 99.0% (117/117 instructions, compiled/target). +- initialize wrapped-block index before byte offset: 98.97436% (117/117 instructions, compiled/target). +- destination byte view initialized with declarations: 98.74359% (117/117 instructions, compiled/target). +- wrapping-destination-index-derived-from-block-number: 95.44444% (116/117 instructions, compiled/target). +- wrapping-pass-base-computed-before-block-loop: 98.97436% (117/117 instructions, compiled/target). +- Retained result: Register allocation and block index/offset initialization order. + +## ATERM_81404BFC + +- initial key words as canonical big-endian XOR packing: 98.94403% (268/268 instructions, compiled/target). +- store each packed key word immediately: 98.01492% (268/268 instructions, compiled/target). +- initialize substitution view before round constants: 98.77612% (268/268 instructions, compiled/target). +- Retained result: Initial byte-load/store scheduling and AES-128 table registers. + +## ATERM_8140502C + +- canonical indexed round-key reversal: 99.565216% (138/138 instructions, compiled/target). +- reversal first index declared before last: 99.42029% (138/138 instructions, compiled/target). +- advance reversal indices after swaps: 99.565216% (138/138 instructions, compiled/target). +- canonical-indexed-reversal-with-shared-word: 100.0% (138/138 instructions, compiled/target). +- Retained result: Exact: 138 instructions, ctxdiff diffs 0. + +## ATERM_81405254 + +- input word initialization in assembly scheduling order 1 2 0 3: 59.16236% (271/271 instructions, compiled/target). +- key xor precedes balanced big-endian packing and counter follows states: 53.553505% (271/271 instructions, compiled/target). +- full canonical AES input declarations then packing and direct round tables: 91.11808% (271/271 instructions, compiled/target). +- final AES round balanced low/high substitution pairs in target order: 91.11808% (271/271 instructions, compiled/target). +- final-interleaved-word-output: 100.0% (271/271 instructions, compiled/target). +- Retained result: Exact: 271 instructions, ctxdiff diffs 0. + +## ATERM_81405690 + +- input word initialization in assembly scheduling order 1 2 0 3: 60.564575% (271/271 instructions, compiled/target). +- key xor precedes balanced big-endian packing and counter follows states: 55.583027% (271/271 instructions, compiled/target). +- full canonical AES input declarations then packing and direct round tables: 87.070114% (271/271 instructions, compiled/target). +- balanced inverse round and final substitution pairs in target order: 91.11808% (271/271 instructions, compiled/target). +- final-interleaved-word-output: 100.0% (271/271 instructions, compiled/target). +- Retained result: Exact: 271 instructions, ctxdiff diffs 0. + +## ATERM_81405ACC + +- retranslation copy blocks through destination/source byte pointers: 72.854164% (138/144 instructions, compiled/target). +- retranslation independent scoped byte copy views and lengths: 72.75% (138/144 instructions, compiled/target). +- retranslation incrementing copy pointers in each byte loop: 74.729164% (135/144 instructions, compiled/target). +- md5-byte-typed-input-formal: 88.583336% (139/144 instructions, compiled/target). +- md5-eight-byte-copy-blocks-followed-by-tail: objdiff score unavailable (267/144 instructions, compiled/target). +- md5-writable-input-formal: 91.263885% (139/144 instructions, compiled/target). +- md5-writable-input-and-scoped-copy-views: 74.6875% (138/144 instructions, compiled/target). +- md5-writable-copy-pointer-increments: 76.875% (135/144 instructions, compiled/target). +- md5-independent-copy-and-block-counters: 91.263885% (139/144 instructions, compiled/target). +- md5-copy-equality-bounds: 69.486115% (117/144 instructions, compiled/target). +- Retained result: Five instructions short; byte-copy loops still use indexed rather than target pointer induction. + +## Exact translation evidence + +- Encryption and decryption input packing and paired round bodies reached zero differences through instruction 198 before changing the final substitution block. +- Calculating and storing one final output word at a time made both AES functions instruction-exact. +- Indexed round-key reversal with one shared word temporary made ATERM_8140502C instruction-exact. + +## Verification + +```text +full build: ok +main.dol sha1: 26116613f624061ba99c8d1a299aaa6efa85670d +[src/scene/setting/ATERM] pool: IDENTICAL +[src/scene/setting/ATERM] objdiff: code 9296/19204 data 18504/18864 functions 15/26 fuzzy 96.4637 linked code 0 +[src/scene/setting/ATERM] instruction-exact functions: 14/26 +regressions vs baseline: 0 +forbidden patterns added (net, per file): 0 +readability warnings (net, per file; must be 0 in the final result): 0 +GATE PASS +``` + +Full gate used a clean 43U build. Regression, forbidden-pattern and readability counts are zero. configure.py was unchanged; ATERM remains NonMatching. + +## Open questions + +- The remaining register-allocation choices and scheduling differences have no confirmed source-level fix. +- ATERM_81402FC0 is already 100% in objdiff. Correctly decoding the last branch operand confirms zero instruction differences; ctxdiff and the gate treat the CR1 operand as the destination address and undercount exact functions by one. +- The .data jump-table and .sbss scores remain below exact. No data placement changes were attempted. + +## Source commits + +- 04ea573a: host address polling order. +- 663b43b7: exact AES transforms, scan record indexing and MD5 length encoding. +- 7c0020a6: writable MD5 input formal to preserve byte-copy alias dependencies. +- e2a5fe66: exact inverse key reversal and source formatting. -- 2.49.1