From fd222d9fa9449ea036449a06e0dfd6b69a69cafc Mon Sep 17 00:00:00 2001 From: ragnarok-clanker Date: Tue, 10 Nov 2026 06:53:37 +0000 Subject: [PATCH] Decompile 6 functions in Source/Utility/Cipher/Rijndael.cpp, Source/Utility/Cipher/Rijndael.h Batch #15566 by Yommy: 6 byte-identical, 0 confirmed by peer review. - fn_04405 CRijndael::EncryptBlock: byte-identical - fn_04406 CRijndael::DefEncryptBlock: byte-identical - fn_04407 CRijndael::DefDecryptBlock: byte-identical - fn_04408 CRijndael::DecryptBlock: byte-identical - fn_04409 CRijndael::MakeKey: byte-identical - fn_04410 CRijndael::Xor: byte-identical Merged automatically: every function is byte-identical to the original. --- Source/Utility/Cipher/Rijndael.cpp | 293 ++++++++++++++++------------- Source/Utility/Cipher/Rijndael.h | 1 + 2 files changed, 160 insertions(+), 134 deletions(-) diff --git a/Source/Utility/Cipher/Rijndael.cpp b/Source/Utility/Cipher/Rijndael.cpp index a5593a5c0..b7afc2acb 100644 --- a/Source/Utility/Cipher/Rijndael.cpp +++ b/Source/Utility/Cipher/Rijndael.cpp @@ -52,6 +52,9 @@ void DecryptRijndael256(const char* input, char* output) { const char* CRijndael::sm_chain0 = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; +// GLOBAL: gl_25571 +const char* CRijndael::sm_szErrorMsg1 = s_errorMsg1; + const int CRijndael::sm_shifts[3][4][2] = {{{0, 0}, {1, 3}, {2, 2}, {3, 1}}, {{0, 0}, {1, 5}, {2, 4}, {3, 3}}, {{0, 0}, {1, 7}, {3, 5}, {4, 4}}}; const signed char CRijndael::sm_S[256] = { @@ -399,11 +402,11 @@ void CRijndael::MakeKey(const char* key, const char* chain, int keyLength, int b throw std::exception(s_errorEmptyKey); } - if (keyLength != 16 && keyLength != 24 && keyLength != 32) { + if (!(16 == keyLength || 24 == keyLength || 32 == keyLength)) { throw std::exception(s_errorKeyLength); } - if (blockSize != 16 && blockSize != 24 && blockSize != 32) { + if (!(16 == blockSize || 24 == blockSize || 32 == blockSize)) { throw std::exception(s_errorBlockLength); } @@ -413,82 +416,88 @@ void CRijndael::MakeKey(const char* key, const char* chain, int keyLength, int b std::memcpy(this->m_chain0, chain, this->m_blockSize); std::memcpy(this->m_chain, chain, this->m_blockSize); - if (this->m_keylength == 16) { - this->m_iROUNDS = this->m_blockSize == 16 ? 10 : (this->m_blockSize == 24 ? 12 : 14); - } else if (this->m_keylength == 24) { - this->m_iROUNDS = this->m_blockSize != 32 ? 12 : 14; - } else { + switch (this->m_keylength) { + case 16: + this->m_iROUNDS = (this->m_blockSize == 16) ? 10 : (this->m_blockSize == 24 ? 12 : 14); + break; + case 24: + this->m_iROUNDS = (this->m_blockSize != 32) ? 12 : 14; + break; + default: this->m_iROUNDS = 14; } - const int BC = this->m_blockSize / 4; + int BC = this->m_blockSize / 4; + int i; + int j; - for (int i = 0; i <= this->m_iROUNDS; ++i) { - for (int j = 0; j < BC; ++j) { + for (i = 0; i <= this->m_iROUNDS; i++) { + for (j = 0; j < BC; j++) { this->m_Ke[i][j] = 0; } } - for (int i = 0; i <= this->m_iROUNDS; ++i) { - for (int j = 0; j < BC; ++j) { + for (i = 0; i <= this->m_iROUNDS; i++) { + for (j = 0; j < BC; j++) { this->m_Kd[i][j] = 0; } } - const int roundKeyCount = (this->m_iROUNDS + 1) * BC; - const int KC = this->m_keylength / 4; + int ROUND_KEY_COUNT = (this->m_iROUNDS + 1) * BC; + int KC = this->m_keylength / 4; int* pi = this->tk; const char* pc = key; - for (int i = 0; i < KC; ++i) { - *pi = static_cast(*pc++) << 24; - *pi |= static_cast(*pc++) << 16; - *pi |= static_cast(*pc++) << 8; - *pi++ |= static_cast(*pc++); + for (i = 0; i < KC; i++) { + *pi = static_cast(*(pc++)) << 24; + *pi |= static_cast(*(pc++)) << 16; + *pi |= static_cast(*(pc++)) << 8; + *(pi++) |= static_cast(*(pc++)); } int t = 0; - for (int j = 0; j < KC && t < roundKeyCount; ++j, ++t) { + for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) { this->m_Ke[t / BC][t % BC] = this->tk[j]; this->m_Kd[this->m_iROUNDS - (t / BC)][t % BC] = this->tk[j]; } - int rconPointer = 0; + int tt; + int rconpointer = 0; - while (t < roundKeyCount) { - int tt = this->tk[KC - 1]; - this->tk[0] ^= ((sm_S[(tt >> 16) & 0xFF] & 0xFF) << 24) ^ ((sm_S[(tt >> 8) & 0xFF] & 0xFF) << 16) ^ ((sm_S[tt & 0xFF] & 0xFF) << 8) ^ - (sm_S[(tt >> 24) & 0xFF] & 0xFF) ^ ((sm_rcon[rconPointer++] & 0xFF) << 24); + while (t < ROUND_KEY_COUNT) { + tt = this->tk[KC - 1]; + this->tk[0] ^= (sm_S[(tt >> 16) & 0xFF] & 0xFF) << 24 ^ (sm_S[(tt >> 8) & 0xFF] & 0xFF) << 16 ^ (sm_S[tt & 0xFF] & 0xFF) << 8 ^ + (sm_S[(tt >> 24) & 0xFF] & 0xFF) ^ (sm_rcon[rconpointer++] & 0xFF) << 24; if (KC != 8) { - for (int i = 1, j = 0; i < KC;) { + for (i = 1, j = 0; i < KC;) { this->tk[i++] ^= this->tk[j++]; } } else { - for (int i = 1, j = 0; i < KC / 2;) { + for (i = 1, j = 0; i < KC / 2;) { this->tk[i++] ^= this->tk[j++]; } tt = this->tk[KC / 2 - 1]; - this->tk[KC / 2] ^= - (sm_S[tt & 0xFF] & 0xFF) ^ ((sm_S[(tt >> 8) & 0xFF] & 0xFF) << 8) ^ ((sm_S[(tt >> 16) & 0xFF] & 0xFF) << 16) ^ ((sm_S[(tt >> 24) & 0xFF] & 0xFF) << 24); + this->tk[KC / 2] ^= (sm_S[tt & 0xFF] & 0xFF) ^ (sm_S[(tt >> 8) & 0xFF] & 0xFF) << 8 ^ (sm_S[(tt >> 16) & 0xFF] & 0xFF) << 16 ^ + (sm_S[(tt >> 24) & 0xFF] & 0xFF) << 24; - for (int j = KC / 2, i = j + 1; i < KC;) { + for (j = KC / 2, i = j + 1; i < KC;) { this->tk[i++] ^= this->tk[j++]; } } - for (int j = 0; j < KC && t < roundKeyCount; ++j, ++t) { + for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) { this->m_Ke[t / BC][t % BC] = this->tk[j]; this->m_Kd[this->m_iROUNDS - (t / BC)][t % BC] = this->tk[j]; } } - for (int r = 1; r < this->m_iROUNDS; ++r) { - for (int j = 0; j < BC; ++j) { - int tt = this->m_Kd[r][j]; + for (int r = 1; r < this->m_iROUNDS; r++) { + for (j = 0; j < BC; j++) { + tt = this->m_Kd[r][j]; this->m_Kd[r][j] = sm_U1[(tt >> 24) & 0xFF] ^ sm_U2[(tt >> 16) & 0xFF] ^ sm_U3[(tt >> 8) & 0xFF] ^ sm_U4[tt & 0xFF]; } } @@ -496,14 +505,14 @@ void CRijndael::MakeKey(const char* key, const char* chain, int keyLength, int b this->m_bKeyInit = true; } -// OFFSET: 0x00618530 -// FUNCTION: fn_04406 -void CRijndael::DefEncryptBlock(const char* in, char* result) { +// OFFSET: 0x006188D0 +// FUNCTION: fn_04407 +void CRijndael::DefDecryptBlock(const char* in, char* result) { if (!this->m_bKeyInit) { - throw std::exception(s_errorMsg1); + throw std::exception(sm_szErrorMsg1); } - const int* Ker = this->m_Ke[0]; + int* Ker = this->m_Ke[0]; int t0 = (static_cast(*in++) << 24); t0 |= (static_cast(*in++) << 16); t0 |= (static_cast(*in++) << 8); @@ -524,12 +533,17 @@ void CRijndael::DefEncryptBlock(const char* in, char* result) { t3 |= (static_cast(*in++) << 8); (t3 |= static_cast(*in++)) ^= Ker[3]; + int a0; + int a1; + int a2; + int a3; + for (int r = 1; r < this->m_iROUNDS; ++r) { Ker = this->m_Ke[r]; - int a0 = (sm_T1[(t0 >> 24) & 0xFF] ^ sm_T2[(t1 >> 16) & 0xFF] ^ sm_T3[(t2 >> 8) & 0xFF] ^ sm_T4[t3 & 0xFF]) ^ Ker[0]; - int a1 = (sm_T1[(t1 >> 24) & 0xFF] ^ sm_T2[(t2 >> 16) & 0xFF] ^ sm_T3[(t3 >> 8) & 0xFF] ^ sm_T4[t0 & 0xFF]) ^ Ker[1]; - int a2 = (sm_T1[(t2 >> 24) & 0xFF] ^ sm_T2[(t3 >> 16) & 0xFF] ^ sm_T3[(t0 >> 8) & 0xFF] ^ sm_T4[t1 & 0xFF]) ^ Ker[2]; - int a3 = (sm_T1[(t3 >> 24) & 0xFF] ^ sm_T2[(t0 >> 16) & 0xFF] ^ sm_T3[(t1 >> 8) & 0xFF] ^ sm_T4[t2 & 0xFF]) ^ Ker[3]; + a0 = (sm_T1[(t0 >> 24) & 0xFF] ^ sm_T2[(t1 >> 16) & 0xFF] ^ sm_T3[(t2 >> 8) & 0xFF] ^ sm_T4[t3 & 0xFF]) ^ Ker[0]; + a1 = (sm_T1[(t1 >> 24) & 0xFF] ^ sm_T2[(t2 >> 16) & 0xFF] ^ sm_T3[(t3 >> 8) & 0xFF] ^ sm_T4[t0 & 0xFF]) ^ Ker[1]; + a2 = (sm_T1[(t2 >> 24) & 0xFF] ^ sm_T2[(t3 >> 16) & 0xFF] ^ sm_T3[(t0 >> 8) & 0xFF] ^ sm_T4[t1 & 0xFF]) ^ Ker[2]; + a3 = (sm_T1[(t3 >> 24) & 0xFF] ^ sm_T2[(t0 >> 16) & 0xFF] ^ sm_T3[(t1 >> 8) & 0xFF] ^ sm_T4[t2 & 0xFF]) ^ Ker[3]; t0 = a0; t1 = a1; @@ -539,64 +553,69 @@ void CRijndael::DefEncryptBlock(const char* in, char* result) { Ker = this->m_Ke[this->m_iROUNDS]; int tt = Ker[0]; - result[0] = sm_S[(t0 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[1] = sm_S[(t1 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[2] = sm_S[(t2 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[3] = sm_S[t3 & 0xFF] ^ static_cast(tt); + result[0] = static_cast(sm_S[(t0 >> 24) & 0xFF] ^ (tt >> 24)); + result[1] = static_cast(sm_S[(t1 >> 16) & 0xFF] ^ (tt >> 16)); + result[2] = static_cast(sm_S[(t2 >> 8) & 0xFF] ^ (tt >> 8)); + result[3] = static_cast(sm_S[t3 & 0xFF] ^ tt); tt = Ker[1]; - result[4] = sm_S[(t1 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[5] = sm_S[(t2 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[6] = sm_S[(t3 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[7] = sm_S[t0 & 0xFF] ^ static_cast(tt); + result[4] = static_cast(sm_S[(t1 >> 24) & 0xFF] ^ (tt >> 24)); + result[5] = static_cast(sm_S[(t2 >> 16) & 0xFF] ^ (tt >> 16)); + result[6] = static_cast(sm_S[(t3 >> 8) & 0xFF] ^ (tt >> 8)); + result[7] = static_cast(sm_S[t0 & 0xFF] ^ tt); tt = Ker[2]; - result[8] = sm_S[(t2 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[9] = sm_S[(t3 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[10] = sm_S[(t0 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[11] = sm_S[t1 & 0xFF] ^ static_cast(tt); + result[8] = static_cast(sm_S[(t2 >> 24) & 0xFF] ^ (tt >> 24)); + result[9] = static_cast(sm_S[(t3 >> 16) & 0xFF] ^ (tt >> 16)); + result[10] = static_cast(sm_S[(t0 >> 8) & 0xFF] ^ (tt >> 8)); + result[11] = static_cast(sm_S[t1 & 0xFF] ^ tt); tt = Ker[3]; - result[12] = sm_S[(t3 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[13] = sm_S[(t0 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[14] = sm_S[(t1 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[15] = sm_S[t2 & 0xFF] ^ static_cast(tt); + result[12] = static_cast(sm_S[(t3 >> 24) & 0xFF] ^ (tt >> 24)); + result[13] = static_cast(sm_S[(t0 >> 16) & 0xFF] ^ (tt >> 16)); + result[14] = static_cast(sm_S[(t1 >> 8) & 0xFF] ^ (tt >> 8)); + result[15] = static_cast(sm_S[t2 & 0xFF] ^ tt); } -// OFFSET: 0x006188D0 -// FUNCTION: fn_04407 -void CRijndael::DefDecryptBlock(const char* in, char* result) { +// OFFSET: 0x00618530 +// FUNCTION: fn_04406 +void CRijndael::DefEncryptBlock(const char* in, char* result) { if (!this->m_bKeyInit) { - throw std::exception(s_errorMsg1); + throw std::exception(sm_szErrorMsg1); } - const int* Kdr = this->m_Kd[0]; + int* Ker = this->m_Kd[0]; int t0 = (static_cast(*in++) << 24); t0 |= (static_cast(*in++) << 16); t0 |= (static_cast(*in++) << 8); - (t0 |= static_cast(*in++)) ^= Kdr[0]; + (t0 |= static_cast(*in++)) ^= Ker[0]; int t1 = (static_cast(*in++) << 24); t1 |= (static_cast(*in++) << 16); t1 |= (static_cast(*in++) << 8); - (t1 |= static_cast(*in++)) ^= Kdr[1]; + (t1 |= static_cast(*in++)) ^= Ker[1]; int t2 = (static_cast(*in++) << 24); t2 |= (static_cast(*in++) << 16); t2 |= (static_cast(*in++) << 8); - (t2 |= static_cast(*in++)) ^= Kdr[2]; + (t2 |= static_cast(*in++)) ^= Ker[2]; int t3 = (static_cast(*in++) << 24); t3 |= (static_cast(*in++) << 16); t3 |= (static_cast(*in++) << 8); - (t3 |= static_cast(*in++)) ^= Kdr[3]; + (t3 |= static_cast(*in++)) ^= Ker[3]; + + int a0; + int a1; + int a2; + int a3; for (int r = 1; r < this->m_iROUNDS; ++r) { - Kdr = this->m_Kd[r]; - int a0 = (sm_T5[(t0 >> 24) & 0xFF] ^ sm_T6[(t3 >> 16) & 0xFF] ^ sm_T7[(t2 >> 8) & 0xFF] ^ sm_T8[t1 & 0xFF]) ^ Kdr[0]; - int a1 = (sm_T5[(t1 >> 24) & 0xFF] ^ sm_T6[(t0 >> 16) & 0xFF] ^ sm_T7[(t3 >> 8) & 0xFF] ^ sm_T8[t2 & 0xFF]) ^ Kdr[1]; - int a2 = (sm_T5[(t2 >> 24) & 0xFF] ^ sm_T6[(t1 >> 16) & 0xFF] ^ sm_T7[(t0 >> 8) & 0xFF] ^ sm_T8[t3 & 0xFF]) ^ Kdr[2]; - int a3 = (sm_T5[(t3 >> 24) & 0xFF] ^ sm_T6[(t2 >> 16) & 0xFF] ^ sm_T7[(t1 >> 8) & 0xFF] ^ sm_T8[t0 & 0xFF]) ^ Kdr[3]; + Ker = this->m_Kd[r]; + a0 = (sm_T5[(t0 >> 24) & 0xFF] ^ sm_T6[(t3 >> 16) & 0xFF] ^ sm_T7[(t2 >> 8) & 0xFF] ^ sm_T8[t1 & 0xFF]) ^ Ker[0]; + a1 = (sm_T5[(t1 >> 24) & 0xFF] ^ sm_T6[(t0 >> 16) & 0xFF] ^ sm_T7[(t3 >> 8) & 0xFF] ^ sm_T8[t2 & 0xFF]) ^ Ker[1]; + a2 = (sm_T5[(t2 >> 24) & 0xFF] ^ sm_T6[(t1 >> 16) & 0xFF] ^ sm_T7[(t0 >> 8) & 0xFF] ^ sm_T8[t3 & 0xFF]) ^ Ker[2]; + a3 = (sm_T5[(t3 >> 24) & 0xFF] ^ sm_T6[(t2 >> 16) & 0xFF] ^ sm_T7[(t1 >> 8) & 0xFF] ^ sm_T8[t0 & 0xFF]) ^ Ker[3]; t0 = a0; t1 = a1; @@ -604,53 +623,54 @@ void CRijndael::DefDecryptBlock(const char* in, char* result) { t3 = a3; } - Kdr = this->m_Kd[this->m_iROUNDS]; - int tt = Kdr[0]; - result[0] = sm_Si[(t0 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[1] = sm_Si[(t3 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[2] = sm_Si[(t2 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[3] = sm_Si[t1 & 0xFF] ^ static_cast(tt); + Ker = this->m_Kd[this->m_iROUNDS]; + int tt = Ker[0]; + result[0] = static_cast(sm_Si[(t0 >> 24) & 0xFF] ^ (tt >> 24)); + result[1] = static_cast(sm_Si[(t3 >> 16) & 0xFF] ^ (tt >> 16)); + result[2] = static_cast(sm_Si[(t2 >> 8) & 0xFF] ^ (tt >> 8)); + result[3] = static_cast(sm_Si[t1 & 0xFF] ^ tt); - tt = Kdr[1]; - result[4] = sm_Si[(t1 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[5] = sm_Si[(t0 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[6] = sm_Si[(t3 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[7] = sm_Si[t2 & 0xFF] ^ static_cast(tt); + tt = Ker[1]; + result[4] = static_cast(sm_Si[(t1 >> 24) & 0xFF] ^ (tt >> 24)); + result[5] = static_cast(sm_Si[(t0 >> 16) & 0xFF] ^ (tt >> 16)); + result[6] = static_cast(sm_Si[(t3 >> 8) & 0xFF] ^ (tt >> 8)); + result[7] = static_cast(sm_Si[t2 & 0xFF] ^ tt); - tt = Kdr[2]; - result[8] = sm_Si[(t2 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[9] = sm_Si[(t3 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[10] = sm_Si[(t0 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[11] = sm_Si[t3 & 0xFF] ^ static_cast(tt); + tt = Ker[2]; + result[8] = static_cast(sm_Si[(t2 >> 24) & 0xFF] ^ (tt >> 24)); + result[9] = static_cast(sm_Si[(t1 >> 16) & 0xFF] ^ (tt >> 16)); + result[10] = static_cast(sm_Si[(t0 >> 8) & 0xFF] ^ (tt >> 8)); + result[11] = static_cast(sm_Si[t3 & 0xFF] ^ tt); - tt = Kdr[3]; - result[12] = sm_Si[(t3 >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[13] = sm_Si[(t2 >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[14] = sm_Si[(t1 >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[15] = sm_Si[t0 & 0xFF] ^ static_cast(tt); + tt = Ker[3]; + result[12] = static_cast(sm_Si[(t3 >> 24) & 0xFF] ^ (tt >> 24)); + result[13] = static_cast(sm_Si[(t2 >> 16) & 0xFF] ^ (tt >> 16)); + result[14] = static_cast(sm_Si[(t1 >> 8) & 0xFF] ^ (tt >> 8)); + result[15] = static_cast(sm_Si[t0 & 0xFF] ^ tt); } -// OFFSET: 0x00618230 -// FUNCTION: fn_04405 -void CRijndael::EncryptBlock(const char* in, char* result) { +// OFFSET: 0x00618E00 +// FUNCTION: fn_04408 +__declspec(noinline) void CRijndael::DecryptBlock(const char* in, char* result) { if (!this->m_bKeyInit) { - throw std::exception(s_errorMsg1); + throw std::exception(sm_szErrorMsg1); } if (this->m_blockSize == DEFAULT_BLOCK_SIZE) { - this->DefEncryptBlock(in, result); + this->DefDecryptBlock(in, result); return; } - const int BC = this->m_blockSize / 4; - const int SC = BC == 4 ? 0 : (BC == 6 ? 1 : 2); - const int s1 = sm_shifts[SC][1][0]; - const int s2 = sm_shifts[SC][2][0]; - const int s3 = sm_shifts[SC][3][0]; - + int BC = this->m_blockSize / 4; + int SC = BC == 4 ? 0 : (BC == 6 ? 1 : 2); + int s1 = sm_shifts[SC][1][0]; + int s2 = sm_shifts[SC][2][0]; + int s3 = sm_shifts[SC][3][0]; + int i; + int tt; int* pi = this->t; - for (int i = 0; i < BC; ++i) { + for (i = 0; i < BC; ++i) { *pi = static_cast(*in++) << 24; *pi |= static_cast(*in++) << 16; *pi |= static_cast(*in++) << 8; @@ -658,7 +678,7 @@ void CRijndael::EncryptBlock(const char* in, char* result) { } for (int r = 1; r < this->m_iROUNDS; ++r) { - for (int i = 0; i < BC; ++i) { + for (i = 0; i < BC; ++i) { this->a[i] = (sm_T1[(this->t[i] >> 24) & 0xFF] ^ sm_T2[(this->t[(i + s1) % BC] >> 16) & 0xFF] ^ sm_T3[(this->t[(i + s2) % BC] >> 8) & 0xFF] ^ sm_T4[this->t[(i + s3) % BC] & 0xFF]) ^ this->m_Ke[r][i]; @@ -667,36 +687,39 @@ void CRijndael::EncryptBlock(const char* in, char* result) { std::memcpy(this->t, this->a, 4 * BC); } - for (int i = 0, j = 0; i < BC; ++i) { - const int tt = this->m_Ke[this->m_iROUNDS][i]; - result[j++] = sm_S[(this->t[i] >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[j++] = sm_S[(this->t[(i + s1) % BC] >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[j++] = sm_S[(this->t[(i + s2) % BC] >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[j++] = sm_S[this->t[(i + s3) % BC] & 0xFF] ^ static_cast(tt); + int j; + + for (i = 0, j = 0; i < BC; ++i) { + tt = this->m_Ke[this->m_iROUNDS][i]; + result[j++] = static_cast(sm_S[(this->t[i] >> 24) & 0xFF] ^ (tt >> 24)); + result[j++] = static_cast(sm_S[(this->t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16)); + result[j++] = static_cast(sm_S[(this->t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8)); + result[j++] = static_cast(sm_S[this->t[(i + s3) % BC] & 0xFF] ^ tt); } } -// OFFSET: 0x00618E00 -// FUNCTION: fn_04408 -__declspec(noinline) void CRijndael::DecryptBlock(const char* in, char* result) { +// OFFSET: 0x00618230 +// FUNCTION: fn_04405 +void CRijndael::EncryptBlock(const char* in, char* result) { if (!this->m_bKeyInit) { - throw std::exception(s_errorMsg1); + throw std::exception(sm_szErrorMsg1); } if (this->m_blockSize == DEFAULT_BLOCK_SIZE) { - this->DefDecryptBlock(in, result); + this->DefEncryptBlock(in, result); return; } - const int BC = this->m_blockSize / 4; - const int SC = BC == 4 ? 0 : (BC == 6 ? 1 : 2); - const int s1 = sm_shifts[SC][1][1]; - const int s2 = sm_shifts[SC][2][1]; - const int s3 = sm_shifts[SC][3][1]; - + int BC = this->m_blockSize / 4; + int SC = BC == 4 ? 0 : (BC == 6 ? 1 : 2); + int s1 = sm_shifts[SC][1][1]; + int s2 = sm_shifts[SC][2][1]; + int s3 = sm_shifts[SC][3][1]; + int i; + int tt; int* pi = this->t; - for (int i = 0; i < BC; ++i) { + for (i = 0; i < BC; ++i) { *pi = static_cast(*in++) << 24; *pi |= static_cast(*in++) << 16; *pi |= static_cast(*in++) << 8; @@ -704,7 +727,7 @@ __declspec(noinline) void CRijndael::DecryptBlock(const char* in, char* result) } for (int r = 1; r < this->m_iROUNDS; ++r) { - for (int i = 0; i < BC; ++i) { + for (i = 0; i < BC; ++i) { this->a[i] = (sm_T5[(this->t[i] >> 24) & 0xFF] ^ sm_T6[(this->t[(i + s1) % BC] >> 16) & 0xFF] ^ sm_T7[(this->t[(i + s2) % BC] >> 8) & 0xFF] ^ sm_T8[this->t[(i + s3) % BC] & 0xFF]) ^ this->m_Kd[r][i]; @@ -713,12 +736,14 @@ __declspec(noinline) void CRijndael::DecryptBlock(const char* in, char* result) std::memcpy(this->t, this->a, 4 * BC); } - for (int i = 0, j = 0; i < BC; ++i) { - const int tt = this->m_Kd[this->m_iROUNDS][i]; - result[j++] = sm_Si[(this->t[i] >> 24) & 0xFF] ^ static_cast(tt >> 24); - result[j++] = sm_Si[(this->t[(i + s1) % BC] >> 16) & 0xFF] ^ static_cast(tt >> 16); - result[j++] = sm_Si[(this->t[(i + s2) % BC] >> 8) & 0xFF] ^ static_cast(tt >> 8); - result[j++] = sm_Si[this->t[(i + s3) % BC] & 0xFF] ^ static_cast(tt); + int j; + + for (i = 0, j = 0; i < BC; ++i) { + tt = this->m_Kd[this->m_iROUNDS][i]; + result[j++] = static_cast(sm_Si[(this->t[i] >> 24) & 0xFF] ^ (tt >> 24)); + result[j++] = static_cast(sm_Si[(this->t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16)); + result[j++] = static_cast(sm_Si[(this->t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8)); + result[j++] = static_cast(sm_Si[this->t[(i + s3) % BC] & 0xFF] ^ tt); } } @@ -726,10 +751,10 @@ __declspec(noinline) void CRijndael::DecryptBlock(const char* in, char* result) // FUNCTION: fn_04410 void CRijndael::Xor(unsigned char* destination, const unsigned char* source) { if (!this->m_bKeyInit) { - throw std::exception(s_errorMsg1); + throw std::exception(sm_szErrorMsg1); } for (int i = 0; i < this->m_blockSize; ++i) { - destination[i] ^= source[i]; + *(destination++) ^= *(source++); } } diff --git a/Source/Utility/Cipher/Rijndael.h b/Source/Utility/Cipher/Rijndael.h index f70420369..0584676c3 100644 --- a/Source/Utility/Cipher/Rijndael.h +++ b/Source/Utility/Cipher/Rijndael.h @@ -63,6 +63,7 @@ private: static const int sm_U4[256]; static const signed char sm_rcon[30]; static const int sm_shifts[3][4][2]; + static const char* sm_szErrorMsg1; /* 0004 */ bool m_bKeyInit; /* 0008 */ int m_Ke[MAX_ROUNDS + 1][MAX_BC];