#include "TEABlockEncryptor.h" #include "CheckSum.h" #include "Rand.h" #include #include #ifdef _WIN32 #include #else #define OutputDebugString NULL #endif #define TEA_ROUNDS 32 #define TEA_XOR_MASK 0x5E94A3CF unsigned int TEABlockEncryptor::initObsDelta = (0x9E3779B9 ^ TEA_XOR_MASK); #ifdef _DEBUG void DumpMemory(const char *preText, const unsigned char *data, unsigned int count) { return; char buffer[2000]; char temp[5]; sprintf(buffer, "%s: ", preText); for(unsigned int i=0; i(output+i)); unsigned int &V1 = *(reinterpret_cast(output+i+sizeof(unsigned int))); EncryptBlock(V0, V1); //*(reinterpret_cast(output+i)+0) = V0; //*(reinterpret_cast(output+i)+1) = V1; } #ifdef _DEBUG DumpMemory("PostEncrypt", output, *outputLength); #endif } bool TEABlockEncryptor::Decrypt( unsigned char *input, int inputLength, unsigned char *output, int *outputLength ) { unsigned char checkSum; unsigned int checkSumInt; unsigned char paddingBytes; unsigned char encodedPad; CheckSum checkSumCalculator; #ifdef _DEBUG assert( keySet ); #endif if ( input == 0 || inputLength < 8 || ( inputLength % 8 ) != 0 ) { return false; } #ifdef _DEBUG DumpMemory("Pre Decrypt", input, inputLength); #endif for(int i=0; i(input+i)); unsigned int &V1 = *(reinterpret_cast(input+i+sizeof(unsigned int))); DecryptBlock(V0, V1); //*(reinterpret_cast(input+i)+0) = V0; //*(reinterpret_cast(input+i)+1) = V1; } #ifdef _DEBUG DumpMemory("PostDecrypt", input, inputLength); #endif // Read checksum checkSum = *(output); // Read the pad size variable encodedPad = *(output + sizeof( checkSum )); // Only care about the last 4 bits paddingBytes = encodedPad & 0x0F; // Get the data length *outputLength = inputLength - sizeof( checkSum ) - sizeof( encodedPad ) - paddingBytes; // Calculate the checksum on the data. checkSumCalculator.Add( input + sizeof( checkSum ), *outputLength + sizeof( encodedPad ) + paddingBytes ); checkSumInt = checkSumCalculator.Get(); checkSumInt = ( (checkSumInt << 4) ^ (checkSumInt) ) & 0xFF; if ( (unsigned char)checkSumInt != checkSum ) return false; // Read the data if ( input == output ) memmove( output, input + sizeof( checkSum ) + sizeof( encodedPad ) + paddingBytes, *outputLength ); else memcpy( output, input + sizeof( checkSum ) + sizeof( encodedPad ) + paddingBytes, *outputLength ); return true; } void TEABlockEncryptor::EncryptBlock(unsigned int &V0, unsigned int &V1) { unsigned int sum = 0; for(unsigned int i=0; i> 5) + V1) ^ (sum + key[sum & 3]); sum += initDelta; V1 += ((V0 << 4 ^ V0 >> 5) + V0) ^ (sum + key[sum>>11 & 3]); } } void TEABlockEncryptor::DecryptBlock(unsigned int &V0, unsigned int &V1) { unsigned int sum = initSum; for(unsigned int i=0; i> 5) + V0) ^ (sum + key[sum>>11 & 3]); sum -= initDelta; V0 -= ((V1 << 4 ^ V1 >> 5) + V1) ^ (sum + key[sum & 3]); } }