void CMap :: Load( CConfig *CFG, string nCFGFile ) { m_Valid = true; m_CFGFile = nCFGFile; // load the map data m_MapLocalPath = CFG->GetString( "map_localpath", string( ) ); m_MapData.clear( ); if( !m_MapLocalPath.empty( ) ) m_MapData = UTIL_FileRead( m_GHost->m_MapPath + m_MapLocalPath ); // load the map MPQ string MapMPQFileName = m_GHost->m_MapPath + m_MapLocalPath; HANDLE MapMPQ; bool MapMPQReady = false; if( SFileOpenArchive( MapMPQFileName.c_str( ), 0, MPQ_OPEN_FORCE_MPQ_V1, &MapMPQ ) ) { CONSOLE_Print( "[MAP] loading MPQ file [" + MapMPQFileName + "]" ); MapMPQReady = true; } else CONSOLE_Print( "[MAP] warning - unable to load MPQ file [" + MapMPQFileName + "]" ); // try to calculate map_size, map_info, map_crc, map_sha1 BYTEARRAY MapSize; BYTEARRAY MapInfo; BYTEARRAY MapCRC; BYTEARRAY MapSHA1; if( !m_MapData.empty( ) ) { m_GHost->m_SHA->Reset( ); // calculate map_size MapSize = UTIL_CreateByteArray( (uint32_t)m_MapData.size( ), false ); CONSOLE_Print( "[MAP] calculated map_size = " + UTIL_ByteArrayToDecString( MapSize ) ); // calculate map_info (this is actually the CRC) MapInfo = UTIL_CreateByteArray( (uint32_t)m_GHost->m_CRC->FullCRC( (unsigned char *)m_MapData.c_str( ), m_MapData.size( ) ), false ); CONSOLE_Print( "[MAP] calculated map_info = " + UTIL_ByteArrayToDecString( MapInfo ) ); // calculate map_crc (this is not the CRC) and map_sha1 // a big thank you to Strilanc for figuring the map_crc algorithm out string CommonJ = UTIL_FileRead( m_GHost->m_MapCFGPath + "common.j" ); if( CommonJ.empty( ) ) CONSOLE_Print( "[MAP] unable to calculate map_crc/sha1 - unable to read file [" + m_GHost->m_MapCFGPath + "common.j]" ); else { string BlizzardJ = UTIL_FileRead( m_GHost->m_MapCFGPath + "blizzard.j" ); if( BlizzardJ.empty( ) ) CONSOLE_Print( "[MAP] unable to calculate map_crc/sha1 - unable to read file [" + m_GHost->m_MapCFGPath + "blizzard.j]" ); else { uint32_t Val = 0; // update: it's possible for maps to include their own copies of common.j and/or blizzard.j // this code now overrides the default copies if required bool OverrodeCommonJ = false; bool OverrodeBlizzardJ = false; if( MapMPQReady ) { HANDLE SubFile; // override common.j if( SFileOpenFileEx( MapMPQ, "Scripts\\common.j", 0, &SubFile ) ) { uint32_t FileLength = SFileGetFileSize( SubFile, NULL ); if( FileLength > 0 && FileLength != 0xFFFFFFFF ) { char *SubFileData = new char[FileLength]; DWORD BytesRead = 0; if( SFileReadFile( SubFile, SubFileData, FileLength, &BytesRead ) ) { CONSOLE_Print( "[MAP] overriding default common.j with map copy while calculating map_crc/sha1" ); OverrodeCommonJ = true; Val = Val ^ XORRotateLeft( (unsigned char *)SubFileData, BytesRead ); m_GHost->m_SHA->Update( (unsigned char *)SubFileData, BytesRead ); } delete [] SubFileData; } SFileCloseFile( SubFile ); } } if( !OverrodeCommonJ ) { Val = Val ^ XORRotateLeft( (unsigned char *)CommonJ.c_str( ), CommonJ.size( ) ); m_GHost->m_SHA->Update( (unsigned char *)CommonJ.c_str( ), CommonJ.size( ) ); } if( MapMPQReady ) { HANDLE SubFile; // override blizzard.j if( SFileOpenFileEx( MapMPQ, "Scripts\\blizzard.j", 0, &SubFile ) ) { uint32_t FileLength = SFileGetFileSize( SubFile, NULL ); if( FileLength > 0 && FileLength != 0xFFFFFFFF ) { char *SubFileData = new char[FileLength]; DWORD BytesRead = 0; if( SFileReadFile( SubFile, SubFileData, FileLength, &BytesRead ) ) { CONSOLE_Print( "[MAP] overriding default blizzard.j with map copy while calculating map_crc/sha1" ); OverrodeBlizzardJ = true; Val = Val ^ XORRotateLeft( (unsigned char *)SubFileData, BytesRead ); m_GHost->m_SHA->Update( (unsigned char *)SubFileData, BytesRead ); } delete [] SubFileData; } SFileCloseFile( SubFile ); } } if( !OverrodeBlizzardJ ) { Val = Val ^ XORRotateLeft( (unsigned char *)BlizzardJ.c_str( ), BlizzardJ.size( ) ); m_GHost->m_SHA->Update( (unsigned char *)BlizzardJ.c_str( ), BlizzardJ.size( ) ); } Val = ROTL( Val, 3 ); Val = ROTL( Val ^ 0x03F1379E, 3 ); m_GHost->m_SHA->Update( (unsigned char *)"\x9E\x37\xF1\x03", 4 ); if( MapMPQReady ) { vector<string> FileList; FileList.push_back( "war3map.j" ); FileList.push_back( "scripts\\war3map.j" ); FileList.push_back( "war3map.w3e" ); FileList.push_back( "war3map.wpm" ); FileList.push_back( "war3map.doo" ); FileList.push_back( "war3map.w3u" ); FileList.push_back( "war3map.w3b" ); FileList.push_back( "war3map.w3d" ); FileList.push_back( "war3map.w3a" ); FileList.push_back( "war3map.w3q" ); bool FoundScript = false; for( vector<string> :: iterator i = FileList.begin( ); i != FileList.end( ); i++ ) { // don't use scripts\war3map.j if we've already used war3map.j (yes, some maps have both but only war3map.j is used) if( FoundScript && *i == "scripts\\war3map.j" ) continue; HANDLE SubFile; if( SFileOpenFileEx( MapMPQ, (*i).c_str( ), 0, &SubFile ) ) { uint32_t FileLength = SFileGetFileSize( SubFile, NULL ); if( FileLength > 0 && FileLength != 0xFFFFFFFF ) { char *SubFileData = new char[FileLength]; DWORD BytesRead = 0; if( SFileReadFile( SubFile, SubFileData, FileLength, &BytesRead ) ) { if( *i == "war3map.j" || *i == "scripts\\war3map.j" ) FoundScript = true; Val = ROTL( Val ^ XORRotateLeft( (unsigned char *)SubFileData, BytesRead ), 3 ); m_GHost->m_SHA->Update( (unsigned char *)SubFileData, BytesRead ); // DEBUG_Print( "*** found: " + *i ); } delete [] SubFileData; } SFileCloseFile( SubFile ); } else { // DEBUG_Print( "*** not found: " + *i ); } } if( !FoundScript ) CONSOLE_Print( "[MAP] couldn't find war3map.j or scripts\\war3map.j in MPQ file, calculated map_crc/sha1 is probably wrong" ); MapCRC = UTIL_CreateByteArray( Val, false ); CONSOLE_Print( "[MAP] calculated map_crc = " + UTIL_ByteArrayToDecString( MapCRC ) ); m_GHost->m_SHA->Final( ); unsigned char SHA1[20]; memset( SHA1, 0, sizeof( unsigned char ) * 20 ); m_GHost->m_SHA->GetHash( SHA1 ); MapSHA1 = UTIL_CreateByteArray( SHA1, 20 ); CONSOLE_Print( "[MAP] calculated map_sha1 = " + UTIL_ByteArrayToDecString( MapSHA1 ) ); } else CONSOLE_Print( "[MAP] unable to calculate map_crc/sha1 - map MPQ file not loaded" ); } } } else CONSOLE_Print( "[MAP] no map data available, using config file for map_size, map_info, map_crc, map_sha1" ); // try to calculate map_width, map_height, map_slot<x>, map_numplayers, map_numteams uint32_t MapOptions = 0; BYTEARRAY MapWidth; BYTEARRAY MapHeight; uint32_t MapNumPlayers = 0; uint32_t MapNumTeams = 0; vector<CGameSlot> Slots; if( !m_MapData.empty( ) ) { if( MapMPQReady ) { HANDLE SubFile; if( SFileOpenFileEx( MapMPQ, "war3map.w3i", 0, &SubFile ) ) { uint32_t FileLength = SFileGetFileSize( SubFile, NULL ); if( FileLength > 0 && FileLength != 0xFFFFFFFF ) { char *SubFileData = new char[FileLength]; DWORD BytesRead = 0; if( SFileReadFile( SubFile, SubFileData, FileLength, &BytesRead ) ) { istringstream ISS( string( SubFileData, BytesRead ) ); // war3map.w3i format found at http://www.wc3campaigns.net/tools/specs/index.html by Zepir/PitzerMike string GarbageString; uint32_t FileFormat; uint32_t RawMapWidth; uint32_t RawMapHeight; uint32_t RawMapFlags; uint32_t RawMapNumPlayers; uint32_t RawMapNumTeams; ISS.read( (char *)&FileFormat, 4 ); // file format (18 = ROC, 25 = TFT) if( FileFormat == 18 || FileFormat == 25 ) { ISS.seekg( 4, ios :: cur ); // number of saves ISS.seekg( 4, ios :: cur ); // editor version getline( ISS, GarbageString, '\0' ); // map name getline( ISS, GarbageString, '\0' ); // map author getline( ISS, GarbageString, '\0' ); // map description getline( ISS, GarbageString, '\0' ); // players recommended ISS.seekg( 32, ios :: cur ); // camera bounds ISS.seekg( 16, ios :: cur ); // camera bounds complements ISS.read( (char *)&RawMapWidth, 4 ); // map width ISS.read( (char *)&RawMapHeight, 4 ); // map height ISS.read( (char *)&RawMapFlags, 4 ); // flags ISS.seekg( 1, ios :: cur ); // map main ground type if( FileFormat == 18 ) ISS.seekg( 4, ios :: cur ); // campaign background number else if( FileFormat == 25 ) { ISS.seekg( 4, ios :: cur ); // loading screen background number getline( ISS, GarbageString, '\0' ); // path of custom loading screen model } getline( ISS, GarbageString, '\0' ); // map loading screen text getline( ISS, GarbageString, '\0' ); // map loading screen title getline( ISS, GarbageString, '\0' ); // map loading screen subtitle if( FileFormat == 18 ) ISS.seekg( 4, ios :: cur ); // map loading screen number else if( FileFormat == 25 ) { ISS.seekg( 4, ios :: cur ); // used game data set getline( ISS, GarbageString, '\0' ); // prologue screen path } getline( ISS, GarbageString, '\0' ); // prologue screen text getline( ISS, GarbageString, '\0' ); // prologue screen title getline( ISS, GarbageString, '\0' ); // prologue screen subtitle if( FileFormat == 25 ) { ISS.seekg( 4, ios :: cur ); // uses terrain fog ISS.seekg( 4, ios :: cur ); // fog start z height ISS.seekg( 4, ios :: cur ); // fog end z height ISS.seekg( 4, ios :: cur ); // fog density ISS.seekg( 1, ios :: cur ); // fog red value ISS.seekg( 1, ios :: cur ); // fog green value ISS.seekg( 1, ios :: cur ); // fog blue value ISS.seekg( 1, ios :: cur ); // fog alpha value ISS.seekg( 4, ios :: cur ); // global weather id getline( ISS, GarbageString, '\0' ); // custom sound environment ISS.seekg( 1, ios :: cur ); // tileset id of the used custom light environment ISS.seekg( 1, ios :: cur ); // custom water tinting red value ISS.seekg( 1, ios :: cur ); // custom water tinting green value ISS.seekg( 1, ios :: cur ); // custom water tinting blue value ISS.seekg( 1, ios :: cur ); // custom water tinting alpha value } ISS.read( (char *)&RawMapNumPlayers, 4 ); // number of players uint32_t ClosedSlots = 0; for( uint32_t i = 0; i < RawMapNumPlayers; i++ ) { CGameSlot Slot( 0, 255, SLOTSTATUS_OPEN, 0, 0, 1, SLOTRACE_RANDOM ); uint32_t Colour; uint32_t Status; uint32_t Race; ISS.read( (char *)&Colour, 4 ); // colour Slot.SetColour( Colour ); ISS.read( (char *)&Status, 4 ); // status if( Status == 1 ) Slot.SetSlotStatus( SLOTSTATUS_OPEN ); else if( Status == 2 ) { Slot.SetSlotStatus( SLOTSTATUS_OCCUPIED ); Slot.SetComputer( 1 ); Slot.SetComputerType( SLOTCOMP_NORMAL ); } else { Slot.SetSlotStatus( SLOTSTATUS_CLOSED ); ClosedSlots++; } ISS.read( (char *)&Race, 4 ); // race if( Race == 1 ) Slot.SetRace( SLOTRACE_HUMAN ); else if( Race == 2 ) Slot.SetRace( SLOTRACE_ORC ); else if( Race == 3 ) Slot.SetRace( SLOTRACE_UNDEAD ); else if( Race == 4 ) Slot.SetRace( SLOTRACE_NIGHTELF ); else Slot.SetRace( SLOTRACE_RANDOM ); ISS.seekg( 4, ios :: cur ); // fixed start position getline( ISS, GarbageString, '\0' ); // player name ISS.seekg( 4, ios :: cur ); // start position x ISS.seekg( 4, ios :: cur ); // start position y ISS.seekg( 4, ios :: cur ); // ally low priorities ISS.seekg( 4, ios :: cur ); // ally high priorities if( Slot.GetSlotStatus( ) != SLOTSTATUS_CLOSED ) Slots.push_back( Slot ); } ISS.read( (char *)&RawMapNumTeams, 4 ); // number of teams for( uint32_t i = 0; i < RawMapNumTeams; i++ ) { uint32_t Flags; uint32_t PlayerMask; ISS.read( (char *)&Flags, 4 ); // flags ISS.read( (char *)&PlayerMask, 4 ); // player mask for( unsigned char j = 0; j < 12; j++ ) { if( PlayerMask & 1 ) { for( vector<CGameSlot> :: iterator k = Slots.begin( ); k != Slots.end( ); k++ ) { if( (*k).GetColour( ) == j ) (*k).SetTeam( i ); } } PlayerMask >>= 1; } getline( ISS, GarbageString, '\0' ); // team name } // the bot only cares about the following options: melee, fixed player settings, custom forces // let's not confuse the user by displaying erroneous map options so zero them out now MapOptions = RawMapFlags & ( MAPOPT_MELEE | MAPOPT_FIXEDPLAYERSETTINGS | MAPOPT_CUSTOMFORCES ); CONSOLE_Print( "[MAP] calculated map_options = " + UTIL_ToString( MapOptions ) ); MapWidth = UTIL_CreateByteArray( (uint16_t)RawMapWidth, false ); CONSOLE_Print( "[MAP] calculated map_width = " + UTIL_ByteArrayToDecString( MapWidth ) ); MapHeight = UTIL_CreateByteArray( (uint16_t)RawMapHeight, false ); CONSOLE_Print( "[MAP] calculated map_height = " + UTIL_ByteArrayToDecString( MapHeight ) ); MapNumPlayers = RawMapNumPlayers - ClosedSlots; CONSOLE_Print( "[MAP] calculated map_numplayers = " + UTIL_ToString( MapNumPlayers ) ); MapNumTeams = RawMapNumTeams; CONSOLE_Print( "[MAP] calculated map_numteams = " + UTIL_ToString( MapNumTeams ) ); uint32_t SlotNum = 1; for( vector<CGameSlot> :: iterator i = Slots.begin( ); i != Slots.end( ); i++ ) { CONSOLE_Print( "[MAP] calculated map_slot" + UTIL_ToString( SlotNum ) + " = " + UTIL_ByteArrayToDecString( (*i).GetByteArray( ) ) ); SlotNum++; } if( MapOptions & MAPOPT_MELEE ) { CONSOLE_Print( "[MAP] found melee map, initializing slots" ); // give each slot a different team and set the race to random unsigned char Team = 0; for( vector<CGameSlot> :: iterator i = Slots.begin( ); i != Slots.end( ); i++ ) { (*i).SetTeam( Team++ ); (*i).SetRace( SLOTRACE_RANDOM ); } } if( !( MapOptions & MAPOPT_FIXEDPLAYERSETTINGS ) ) { // make races selectable for( vector<CGameSlot> :: iterator i = Slots.begin( ); i != Slots.end( ); i++ ) (*i).SetRace( (*i).GetRace( ) | SLOTRACE_SELECTABLE ); } } } else
// address: 0x1b70 int main(int argc, char *argv[], char *envp[]) { unsigned int CR0; // r64 unsigned int CR1; // r65 unsigned int CR2; // r66 unsigned int CR3; // r67 unsigned int CR4; // r68 unsigned int CR5; // r69 unsigned int CR6; // r70 __size32 CR7; // r71 __size32 g0; // r0 __size32 g9; // r9 __size32 local0; // m[g1 - 40] __size32 local1; // m[g1 - 36] __size32 local2; // m[g1 - 32] __size32 local3; // m[g1 - 28] __size32 local4; // m[g1 - 24] __size32 local5; // m[g1 - 48] local0 = 1; if (argc <= 1) { local0 = 0; } if (local0 == 0) { g9 = *(/* machine specific */ (int) LR + 1200); local1 = g9; } else { g9 = *(/* machine specific */ (int) LR + 1204); local1 = g9; } if (local0 == 0) { g9 = *(/* machine specific */ (int) LR + 1192); local2 = g9; } else { g9 = *(/* machine specific */ (int) LR + 1196); local2 = g9; } if (local0 == 0) { g9 = *(/* machine specific */ (int) LR + 1184); local3 = g9; } else { g9 = *(/* machine specific */ (int) LR + 1188); local3 = g9; } if (local0 == 0) { g9 = *(/* machine specific */ (int) LR + 1176); local4 = g9; } else { g9 = *(/* machine specific */ (int) LR + 1180); local4 = g9; } if (argc <= 1) { local5 = 0; g0 = *(/* machine specific */ (int) LR + 1200); if (local1 == g0) { g0 = *(/* machine specific */ (int) LR + 1192); if (local2 == g0) { g0 = *(/* machine specific */ (int) LR + 1184); if (local3 == g0) { g0 = *(/* machine specific */ (int) LR + 1176); if (local4 == g0) { local5 = 1; } } } } } else { local5 = 0; g0 = *(/* machine specific */ (int) LR + 1204); if (local1 == g0) { g0 = *(/* machine specific */ (int) LR + 1196); if (local2 == g0) { g0 = *(/* machine specific */ (int) LR + 1188); if (local3 == g0) { g0 = *(/* machine specific */ (int) LR + 1180); if (local4 == g0) { local5 = 1; } } } } } if (local5 == 0) { printf(/* machine specific */ (int) LR + 1132); } else { printf(/* machine specific */ (int) LR + 1124); } return ROTL((CR0 * 0x10000000 + CR1 * 0x1000000 + CR2 * 0x100000 + CR3 * 0x10000 + CR4 * 0x1000 + CR5 * 256 + CR6 * 16 + CR7)) & 0x1; }
void RC5key(unsigned char *key, int b, word32 *ky) { int i, j; int x = ((b-1)/sizeof(word32))+1, y = 0; int mix = 3 * (MAX(t,x)); word32 *L; word32 A = 0, B = 0; fprintf(stderr,"x = %d\n",x); fprintf(stderr,"mix = %d\n",mix); L = (word32 *) calloc((x+1), sizeof(word32)); /* put char array key into word32 array */ memcpy((unsigned char *)L, key, 16); #ifdef debug_key_sched //fprintf(stderr,"L = ["); for(i=0; i<4;i++) { fprintf(stderr, "L[%d] = %X\n",i,L[i]); } //fprintf(stderr," ]\n"); #endif *ky = P; /* Prep subkeys with magic Constants */ for(i = 1; i <= t; i++) *(ky+i) = *(ky+(i-1)) + Q; #ifdef debug_key_sched fprintf(stderr,"ky=[ \n"); for(i=0; i<=t; i++) { fprintf(stderr,"%X\n",ky[i]); } fprintf(stderr,"]\n"); #endif i = j = 0; /* Mix in user key */ while(mix != y) { #ifdef debug_key_sched fprintf(stderr,"------------------------------calc A\n"); fprintf(stderr, "A = %X, B = %X\n", A, B); fprintf(stderr, "i=%X, ky[i]=%X\n",i,ky[i]); fprintf(stderr, "ky[i]+A+B = %X\n", ky[i] + A + B); #endif A = *(ky+i) = ROTL((*(ky+i) + A + B), 3&ANDVAL); #ifdef debug_key_sched fprintf(stderr, "A --> %X\n",A); //fprintf(stderr, "ky[%X] --> %X\n", i, ky[i]); #endif #ifdef debug_key_sched fprintf(stderr,"------------------------------calc B\n"); fprintf(stderr, "A = %X, B = %X\n", A, B); fprintf(stderr,"j=%X, L[j]=%X\n",j, L[j]); fprintf(stderr,"L[j]+A+B = %X\n",*(L+j)+A+B); fprintf(stderr,"A+B = %X\n",A+B); #endif B = *(L+j) = ROTL((*(L+j) + A + B), ((A + B)&ANDVAL)); #ifdef debug_key_sched fprintf(stderr, "B --> %X\n\n",B); //fprintf(stderr, "L[%X] --> %X\n", j, L[j]); #endif i++; j++; i %= t; j %= x; y++; } #ifdef debug_key_sched for(i=0; i<4;i++) { fprintf(stderr, "L[%d] = %X\n",i,L[i]); } printf("\n"); fprintf(stderr,"ky=[ \n"); for(i=0; i<=t; i++) { fprintf(stderr,"%X\n",ky[i]); } fprintf(stderr,"]\n"); #endif }
int attack_104(void) { int i, j, k, LL, T; RC5_WORD L[256/WW]; /* Based on max key size */ RC5_WORD A, B; int b = 13; /* Key Length in Bytes */ int R = 12; /* Rounds */ RC5_WORD S[26]; /* S Table For Expanded Key */ /* I need a file pointer to random */ FILE *dr; RC5_WORD K[4]; /* key storage */ RC5_WORD pt[2]; RC5_WORD iv[2]; int rslt = 0; LL = 4; /* LL is number of elements used in L. */ /* Backwards little edian style baby, owww yeah */ /* Secret Key */ L[0] = 0x00000000; L[1] = 0x00000000; L[2] = 0x00000000; L[3] = 0x00000000; /* Intilization Vector */ iv[0] = 0xc20b81a1; iv[1] = 0x56751624; /* Plain Text */ pt[0] = 0x20656854; pt[1] = 0x6e6b6e75; /* CBC so we Xor */ pt[0] = pt[0] ^ iv[0]; pt[1] = pt[1] ^ iv[1]; printf("CryptNET: RSA Labs RC5-104 Challenge Full Attack Client\n"); printf("\n"); T = 26; if(NULL == (dr = fopen("/dev/urandom", "r"))) { fprintf(stderr,"Error: Failed to open /dev/urandom device\n"); } else { rslt = fread(K,4,4,dr); if(rslt != 4) { fprintf(stderr,"Error: Failed to read from /dev/urandom device\n"); } fclose(dr); } /* We should just continue on if we fail and run with what ever value is found in K. That value is probably old data left in memory, which is most likely reasonably random. Depending on the OS, it may be zero'd or 0xff'd space, which isn't as nice, but is better than exiting and not running at all. */ /* size K[3] */ K[3] = K[3]>>24; printf("Starting at: 0x%0.8x:0x%0.8x:0x%0.8x:0x%0.8x\n", K[0], K[1], K[2], K[3]); while(1) /* crack */ { L[0] = K[0]; L[1] = K[1]; L[2] = K[2]; L[3] = K[3]; /* seed the S Array (key expansion array) with psudo random numbers */ S[0] = Pw; for (i = 1 ; i < T ; i++) { S[i] = S[i-1] + Qw; } i = j = 0; A = B = 0; k = 3 * T; /* Secret key len < expanded key. */ /* Mix the secret key in with the psudo random numbers in the S Array */ for ( ; k > 0 ; k--) { A = ROTL(S[i] + A + B, 3, W); S[i] = A; B = ROTL(L[j] + A + B, A + B, W); L[j] = B; i = (i + 1) % T; j = (j + 1) % LL; } /* Encrypt */ A = pt[0]; B = pt[1]; A = A + S[0]; B = B + S[1]; for (i = 1 ; i <= R ; i++) { A = A ^ B; A = ROTL(A, B, W) + S[2*i]; B = B ^ A; B = ROTL(B, A, W) + S[(2*i)+1]; } /* Compare to cyphertext. */ if(A == 0xb678fe73) { if(B == 0x2df9ee10) { printf("Secret Key Candidate Found:\t"); printf("0x%0.8x:0x%0.8x:0x%0.8x:0x%0.8x\n\n", K[0], K[1], K[2], K[3]); return 0; } } if(K[0]!=0xffffffff) { K[0]++; } else { if(K[1]!=0xffffffff) { K[1]++; K[0]=0x00000000; } else { if(K[2]!=0xffffffff) { K[2]++; K[1]=0x00000000; K[0]=0x00000000; } else { if(K[3]!=0x000000ff) { K[3]++; K[2]=0x00000000; K[1]=0x00000000; K[0]=0x00000000; } else { K[3]=0x00000000; K[2]=0x00000000; K[1]=0x00000000; K[0]=0x00000000; } } } } } return 0; }
//设置密钥 void RCSetKey(PRCContext pContext) { int i; unsigned int j,k,t,w; unsigned long* pL; unsigned long a,b; unsigned long p,q; unsigned long max; //密钥 unsigned char* pKey = pContext->pKey; //密钥长度 unsigned int nLen = pContext->nKeyLen; //子密钥组数 unsigned int nSubKeyLen = pContext->nSubKeyLen; //字长 w = pContext->nWordSizeByte; //将字符串转化为字长整型需要的个数 k = (nLen % w == 0) ? (nLen / w) : (nLen / w) + 1; //总字节数 t = k * w; //临时数组,密钥转换成字节长整型 pL = (unsigned long*)malloc(t); memset(pL,0,t); //将字符串数组COPY到新的字长数组(大端法) for (i=(nLen-1); i>=0; i--) { pL[i/w] = (pL[i/w] << 8) + pKey[i]; } //获取与字长相关的Pw和Qw的值 SetPwQw(w,&p,&q); //初始化subkey pContext->pSubKey[0] = p; for (i=1; i<nLen; i++) { pContext->pSubKey[i] = pContext->pSubKey[i-1] + q; } //密钥扩展 a = 0; b = 0; i = 0; j = 0; //密钥转换之后的长度 和 子密钥的长度 //字符串长 / 字长 2 * (r + 1) r为加密轮数 max = (pContext->nSubKeyLen > k) ? pContext->nSubKeyLen : k; max = 3 * max; w = 1 << pContext->nShiftCount;//最大移位次数 //max times while (max-- > 0) { pContext->pSubKey[i] = pContext->pSubKey[i] + a + b; pContext->pSubKey[i] = ROTL(pContext->pSubKey[i],3,w); a = pContext->pSubKey[i]; pL[j] = pL[j] + a + b; pL[j] = ROTL(pL[j],a+b,w); b = pL[j]; i = (i+1) % nSubKeyLen; j = (j+1) % k; } free(pL); }
void HashSHA1Block(void* hash_block, SHA1_Context* ctx) { unsigned int a,b,c,d,e,T,i; unsigned char* block = (unsigned char*)hash_block; unsigned int w[0x50]; a = ctx->h0; b = ctx->h1; c = ctx->h2; d = ctx->h3; e = ctx->h4; for (i = 0; i < 16; i++) w[i] = BSWAP(*(unsigned int*)(block + i * 4)); for (i = 16; i < 80; i++) w[i] = ROTL( w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); for (i = 0; i < 20; i++) { T = ROTL(a,5) + Ch(b,c,d) + e + sha1_constant[0] + w[i]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } for (i=20;i<40;i++) { T = ROTL(a,5) + Parity(b,c,d) + e + sha1_constant[1] + w[i]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } for (i=40;i<60;i++) { T = ROTL(a,5) + Maj(b,c,d) + e + sha1_constant[2] + w[i]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } for (i=60;i<80;i++) { T = ROTL(a,5) + Parity(b,c,d) + e + sha1_constant[3] + w[i]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } ctx->h0 += a; ctx->h1 += b; ctx->h2 += c; ctx->h3 += d; ctx->h4 += e; a = b = c = d = e = T = 0; memset(w, 0, 0x140); }
int test_104(void) { int i, j, k, LL, T; RC5_WORD L[256/WW]; /* Based on max key size */ RC5_WORD K[4]; /* Storage for Secret Key */ RC5_WORD A, B; int b = 13; /* Key Length in Bytes */ int R = 12; /* Rounds */ RC5_WORD S[26]; /* S Table For Expanded Key */ RC5_WORD pt[2]; RC5_WORD iv[2]; LL = 4; /* LL is number of elements used in L. */ /* Backwards little edian style baby, owww yeah */ /* Secret Key */ L[0] = 0x0cd53232; L[1] = 0xf48b46c6; L[2] = 0xbacbb93e; L[3] = 0x00000082; K[0] = L[0]; K[1] = L[1]; K[2] = L[2]; K[3] = L[3]; /* Intilization Vector */ iv[0] = 0x133bcedb; iv[1] = 0x9eaf81f0; /* Plain Text */ pt[0] = 0x20656854; pt[1] = 0x6e6b6e75; /* CBC so we Xor */ pt[0] = pt[0] ^ iv[0]; pt[1] = pt[1] ^ iv[1]; T = 26; /* seed the S Array (key expansion array) with psudo random numbers */ S[0] = Pw; for (i = 1 ; i < T ; i++) { S[i] = S[i-1] + Qw; } i = j = 0; A = B = 0; k = 3 * T; /* Secret key len < expanded key. */ /* Mix the secret key in with the psudo random numbers in the S Array */ for ( ; k > 0 ; k--) { A = ROTL(S[i] + A + B, 3, W); S[i] = A; B = ROTL(L[j] + A + B, A + B, W); L[j] = B; i = (i + 1) % T; j = (j + 1) % LL; } /* Encrypt */ A = pt[0]; B = pt[1]; A = A + S[0]; B = B + S[1]; for (i = 1 ; i <= R ; i++) { A = A ^ B; A = ROTL(A, B, W) + S[2*i]; B = B ^ A; B = ROTL(B, A, W) + S[(2*i)+1]; } if(A == 0x2e536350) { if(B == 0xc45ab0dc) { return 0; } } printf("Failure\n"); return 0; }
// address: 10000468 int main(int argc, char *argv[], char *envp[]) { unsigned int g0; // r0 void *g0_1; // r0 void *g1; // r1 __size32 g10; // r10 __size32 g11; // r11 __size32 g3; // r3 int local0; // m[g1 - 112] int local1; // m[g1 - 120] int local2; // m[g1 - 124] char local3; // m[g1 - 96] __size32 local4; // m[g1 - 136] char[] **local5; // m[g1 - 132] int local6; // m[g1 - 116] int local7; // m[g1 - 128] __size32 local8; // m[g1 - 136]{241} malloc(12); *(__size32*)(g3 + 4) = 0x10001df4; local4 = 2; local5 = g3 + 4; local8 = local4; local4 = local8 - 1; while (local8 - 1 != 0) { g3 = *local5; strlen(g3); local6 = g3; if (g3 > 10) { local6 = 10; } local7 = 0; L16: if (local7 <= 6) { local2 = 0; while (local2 < local6) { g0 = *(unsigned char*)(g9 + local2); g0 = (ROTL(g0) & 0xff) - 32; local0 = g0; if (g0 < 0) { local0 = 0; } local1 = 0; L2: if (local1 <= 6) { g10 = (ROTL(local2) & 0xfffffff9) + g1 + local1 - 96; g11 = (ROTL((rs * 7 + local7)) & 0xfffffffd) + 0x100120f4; g0_1 = *g11; g0 = *(unsigned char*)(rs * 7 + local1 + g0_1); *(__size8*)g10 = (char) g0; local1++; goto L2; } *(__size8*)((ROTL(local2) & 0xfffffff9) + g1 - 89) = 32; local2++; } g0 = (ROTL(local6) & 0xfffffff9) - 1; local2 = g0; while (local2 >= 0) { g0 = *(unsigned char*)(g1 + local2 - 96); if ((ROTL(g0) & 0xff) != 32) { break; } *(__size8*)(g1 + local2 - 96) = 0; local2 = local2 - 1; } puts(&local3); local7++; goto L16; } puts(""); local5++; local8 = local4; local4 = local8 - 1; } return 0; }
static void MUSH_SHA1_HashBlock(MUSH_SHA1_CONTEXT *p) { int t, j; UINT32 W[80], a, b, c, d, e, T; // Prepare Message Schedule, {W sub t}. // for (t = 0, j = 0; t <= 15; t++, j += 4) { W[t] = (p->block[j ] << 24) | (p->block[j+1] << 16) | (p->block[j+2] << 8) | (p->block[j+3] ); } for (t = 16; t <= 79; t++) { W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); } a = p->H[0]; b = p->H[1]; c = p->H[2]; d = p->H[3]; e = p->H[4]; for (t = 0; t <= 19; t++) { T = ROTL(a,5) + Ch(b,c,d) + e + 0x5A827999 + W[t]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } for (t = 20; t <= 39; t++) { T = ROTL(a,5) + Parity(b,c,d) + e + 0x6ED9EBA1 + W[t]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } for (t = 40; t <= 59; t++) { T = ROTL(a,5) + Maj(b,c,d) + e + 0x8F1BBCDC + W[t]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } for (t = 60; t <= 79; t++) { T = ROTL(a,5) + Parity(b,c,d) + e + 0xCA62C1D6 + W[t]; e = d; d = c; c = ROTL(b,30); b = a; a = T; } p->H[0] += a; p->H[1] += b; p->H[2] += c; p->H[3] += d; p->H[4] += e; }
int test_40(void) { int i, j, k, LL, T; RC5_WORD L[256/WW]; /* Based on max key size */ RC5_WORD A, B; int b = 5; /* Key Length in Bytes */ int R = 12; /* Rounds */ RC5_WORD S[26]; /* S Table For Expanded Key */ RC5_WORD pt[2]; RC5_WORD iv[2]; RC5_WORD ak[2]; LL = 2; /* LL is number of elements used in L. */ /* Backwards little edian style baby, owww yeah */ /* Secret Key */ L[0] = 0xd26dd827; L[1] = 0x00000043; /* Intilization Vector */ iv[0] = 0x1a1775f6; iv[1] = 0xd0eab759; /* Plain Text */ pt[0] = 0x20656854; pt[1] = 0x6e6b6e75; /* CBC so we Xor */ pt[0] = pt[0] ^ iv[0]; pt[1] = pt[1] ^ iv[1]; T = 26; /* seed the S Array (key expansion array) with psudo random numbers */ S[0] = Pw; for (i = 1 ; i < T ; i++) { S[i] = S[i-1] + Qw; } i = j = 0; A = B = 0; k = 3 * T; /* Secret key len < expanded key. */ /* Preserve the Secret Key */ ak[0] = L[0]; ak[1] = L[1]; /* Mix the secret key in with the psudo random numbers in the S Array */ for ( ; k > 0 ; k--) { A = ROTL(S[i] + A + B, 3, W); S[i] = A; B = ROTL(L[j] + A + B, A + B, W); L[j] = B; i = (i + 1) % T; j = (j + 1) % LL; } /* Encrypt */ A = pt[0]; B = pt[1]; A = A + S[0]; B = B + S[1]; for (i = 1 ; i <= R ; i++) { A = A ^ B; A = ROTL(A, B, W) + S[2*i]; B = B ^ A; B = ROTL(B, A, W) + S[(2*i)+1]; } /* Compare to cyphertext. Do we have a match? */ if(A == 0x88530ab4) { if(B == 0xad8238b1) { return 0; } } printf("Failure\n"); return 0; }
static int calculate_sha1(struct sha *sha1, const char *text, uint32_t length) { unsigned int i, j; unsigned char *buffer = NULL, *pbuffer = NULL; uint32_t bits = 0; uint32_t temp = 0, k = 0; uint32_t lb = length * 8; if (!sha1) { return 0; } // initialize the default digest values sha1->digest[0] = 0x67452301; sha1->digest[1] = 0xEFCDAB89; sha1->digest[2] = 0x98BADCFE; sha1->digest[3] = 0x10325476; sha1->digest[4] = 0xC3D2E1F0; sha1->a = sha1->b = sha1->c = sha1->d = sha1->e = sha1->f = 0; if (!text || !length) { return 0; } bits = padded_length_in_bits(length); buffer = (unsigned char *) malloc((bits / 8) + 8); memset(buffer, 0, (bits / 8) + 8); if (buffer == NULL) { return 1; } pbuffer = buffer; memcpy(buffer, text, length); //add 1 on the last of the message.. *(buffer + length) = 0x80; for (i = length + 1; i < (bits / 8); i++) { *(buffer + i) = 0x00; } *(buffer + (bits / 8) + 4 + 0) = (lb >> 24) & 0xFF; *(buffer + (bits / 8) + 4 + 1) = (lb >> 16) & 0xFF; *(buffer + (bits / 8) + 4 + 2) = (lb >> 8) & 0xFF; *(buffer + (bits / 8) + 4 + 3) = (lb >> 0) & 0xFF; //main loop for (i = 0; i < ((bits + 64) / 512); i++) { //first empty the block for each pass.. for (j = 0; j < 80; j++) { sha1->w[j] = 0x00; } //fill the first 16 words with the characters read directly from the buffer. for (j = 0; j < 16; j++) { sha1->w[j] = buffer[j * 4 + 0]; sha1->w[j] = sha1->w[j] << 8; sha1->w[j] |= buffer[j * 4 + 1]; sha1->w[j] = sha1->w[j] << 8; sha1->w[j] |= buffer[j * 4 + 2]; sha1->w[j] = sha1->w[j] << 8; sha1->w[j] |= buffer[j * 4 + 3]; } //fill the rest 64 words using the formula for (j = 16; j < 80; j++) { sha1->w[j] = (ROTL(1, (sha1->w[j - 3] ^ sha1->w[j - 8] ^ sha1->w[j - 14] ^ sha1->w[j - 16]))); } //initialize hash for this chunck reading that has been stored in the structure digest sha1->a = sha1->digest[0]; sha1->b = sha1->digest[1]; sha1->c = sha1->digest[2]; sha1->d = sha1->digest[3]; sha1->e = sha1->digest[4]; //for all the 80 32bit blocks calculate f and use k accordingly per specification. for (j = 0; j < 80; j++) { if ((j >= 0) && (j < 20)) { sha1->f = ((sha1->b) & (sha1->c)) | ((~(sha1->b)) & (sha1->d)); k = 0x5A827999; } else if ((j >= 20) && (j < 40)) { sha1->f = (sha1->b) ^ (sha1->c) ^ (sha1->d); k = 0x6ED9EBA1; } else if ((j >= 40) && (j < 60)) { sha1->f = ((sha1->b) & (sha1->c)) | ((sha1->b) & (sha1->d)) | ((sha1->c) & (sha1->d)); k = 0x8F1BBCDC; } else if ((j >= 60) && (j < 80)) { sha1->f = (sha1->b) ^ (sha1->c) ^ (sha1->d); k = 0xCA62C1D6; } temp = ROTL(5, (sha1->a)) + (sha1->f) + (sha1->e) + k + sha1->w[j]; sha1->e = (sha1->d); sha1->d = (sha1->c); sha1->c = ROTL(30, (sha1->b)); sha1->b = (sha1->a); sha1->a = temp; //reset temp to 0 to be in safe side only, not mandatory. temp = 0x00; } // append to total hash. sha1->digest[0] += sha1->a; sha1->digest[1] += sha1->b; sha1->digest[2] += sha1->c; sha1->digest[3] += sha1->d; sha1->digest[4] += sha1->e; //since we used 512bit size block per each pass, let us update the buffer pointer accordingly. buffer = buffer + 64; } free(pbuffer); return 0; }
static inline void xor_salsa8(uint32_t B[16], const uint32_t Bx[16]) { uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15; int i; x00 = (B[ 0] ^= Bx[ 0]); x01 = (B[ 1] ^= Bx[ 1]); x02 = (B[ 2] ^= Bx[ 2]); x03 = (B[ 3] ^= Bx[ 3]); x04 = (B[ 4] ^= Bx[ 4]); x05 = (B[ 5] ^= Bx[ 5]); x06 = (B[ 6] ^= Bx[ 6]); x07 = (B[ 7] ^= Bx[ 7]); x08 = (B[ 8] ^= Bx[ 8]); x09 = (B[ 9] ^= Bx[ 9]); x10 = (B[10] ^= Bx[10]); x11 = (B[11] ^= Bx[11]); x12 = (B[12] ^= Bx[12]); x13 = (B[13] ^= Bx[13]); x14 = (B[14] ^= Bx[14]); x15 = (B[15] ^= Bx[15]); for (i = 0; i < 8; i += 2) { /* Operate on columns. */ x04 ^= ROTL(x00 + x12, 7); x09 ^= ROTL(x05 + x01, 7); x14 ^= ROTL(x10 + x06, 7); x03 ^= ROTL(x15 + x11, 7); x08 ^= ROTL(x04 + x00, 9); x13 ^= ROTL(x09 + x05, 9); x02 ^= ROTL(x14 + x10, 9); x07 ^= ROTL(x03 + x15, 9); x12 ^= ROTL(x08 + x04, 13); x01 ^= ROTL(x13 + x09, 13); x06 ^= ROTL(x02 + x14, 13); x11 ^= ROTL(x07 + x03, 13); x00 ^= ROTL(x12 + x08, 18); x05 ^= ROTL(x01 + x13, 18); x10 ^= ROTL(x06 + x02, 18); x15 ^= ROTL(x11 + x07, 18); /* Operate on rows. */ x01 ^= ROTL(x00 + x03, 7); x06 ^= ROTL(x05 + x04, 7); x11 ^= ROTL(x10 + x09, 7); x12 ^= ROTL(x15 + x14, 7); x02 ^= ROTL(x01 + x00, 9); x07 ^= ROTL(x06 + x05, 9); x08 ^= ROTL(x11 + x10, 9); x13 ^= ROTL(x12 + x15, 9); x03 ^= ROTL(x02 + x01, 13); x04 ^= ROTL(x07 + x06, 13); x09 ^= ROTL(x08 + x11, 13); x14 ^= ROTL(x13 + x12, 13); x00 ^= ROTL(x03 + x02, 18); x05 ^= ROTL(x04 + x07, 18); x10 ^= ROTL(x09 + x08, 18); x15 ^= ROTL(x14 + x13, 18); } B[ 0] += x00; B[ 1] += x01; B[ 2] += x02; B[ 3] += x03; B[ 4] += x04; B[ 5] += x05; B[ 6] += x06; B[ 7] += x07; B[ 8] += x08; B[ 9] += x09; B[10] += x10; B[11] += x11; B[12] += x12; B[13] += x13; B[14] += x14; B[15] += x15; }
/* see inner.h */ void br_md5_round(const unsigned char *buf, uint32_t *val) { uint32_t m[16]; uint32_t a, b, c, d; int i; a = val[0]; b = val[1]; c = val[2]; d = val[3]; /* obsolete for (i = 0; i < 16; i ++) { m[i] = br_dec32le(buf + (i << 2)); } */ br_range_dec32le(m, 16, buf); for (i = 0; i < 16; i += 4) { a = b + ROTL(a + F(b, c, d) + m[i + 0] + K[i + 0], 7); d = a + ROTL(d + F(a, b, c) + m[i + 1] + K[i + 1], 12); c = d + ROTL(c + F(d, a, b) + m[i + 2] + K[i + 2], 17); b = c + ROTL(b + F(c, d, a) + m[i + 3] + K[i + 3], 22); } for (i = 16; i < 32; i += 4) { a = b + ROTL(a + G(b, c, d) + m[MP[i - 16]] + K[i + 0], 5); d = a + ROTL(d + G(a, b, c) + m[MP[i - 15]] + K[i + 1], 9); c = d + ROTL(c + G(d, a, b) + m[MP[i - 14]] + K[i + 2], 14); b = c + ROTL(b + G(c, d, a) + m[MP[i - 13]] + K[i + 3], 20); } for (i = 32; i < 48; i += 4) { a = b + ROTL(a + H(b, c, d) + m[MP[i - 16]] + K[i + 0], 4); d = a + ROTL(d + H(a, b, c) + m[MP[i - 15]] + K[i + 1], 11); c = d + ROTL(c + H(d, a, b) + m[MP[i - 14]] + K[i + 2], 16); b = c + ROTL(b + H(c, d, a) + m[MP[i - 13]] + K[i + 3], 23); } for (i = 48; i < 64; i += 4) { a = b + ROTL(a + I(b, c, d) + m[MP[i - 16]] + K[i + 0], 6); d = a + ROTL(d + I(a, b, c) + m[MP[i - 15]] + K[i + 1], 10); c = d + ROTL(c + I(d, a, b) + m[MP[i - 14]] + K[i + 2], 15); b = c + ROTL(b + I(c, d, a) + m[MP[i - 13]] + K[i + 3], 21); } val[0] += a; val[1] += b; val[2] += c; val[3] += d; }
s32 CDECL rc5_72_unit_func_ansi_1 (RC5_72UnitWork *rc5_72unitwork, u32 *iterations, void * /*memblk*/) { u32 A, B; u32 S[26]; u32 L[3]; u32 kiter = *iterations; while (kiter--) { L[2] = rc5_72unitwork->L0.hi; L[1] = rc5_72unitwork->L0.mid; L[0] = rc5_72unitwork->L0.lo; #define KEY_INIT(i) S[i] = P + i*Q; KEY_INIT(0); KEY_INIT(1); KEY_INIT(2); KEY_INIT(3); KEY_INIT(4); KEY_INIT(5); KEY_INIT(6); KEY_INIT(7); KEY_INIT(8); KEY_INIT(9); KEY_INIT(10); KEY_INIT(11); KEY_INIT(12); KEY_INIT(13); KEY_INIT(14); KEY_INIT(15); KEY_INIT(16); KEY_INIT(17); KEY_INIT(18); KEY_INIT(19); KEY_INIT(20); KEY_INIT(21); KEY_INIT(22); KEY_INIT(23); KEY_INIT(24); KEY_INIT(25); #define ROTL_BLOCK(i,j) ROTL_BLOCK_j##j (i) #define ROTL_BLOCK_i0_j1 \ S[0] = ROTL3(S[0]+(S[25]+L[0])); \ L[1] = ROTL(L[1]+(S[0]+L[0]),(S[0]+L[0])); \ #define ROTL_BLOCK_i0_j2 \ S[0] = ROTL3(S[0]+(S[25]+L[1])); \ L[2] = ROTL(L[2]+(S[0]+L[1]),(S[0]+L[1])); \ #define ROTL_BLOCK_j0(i) \ S[i] = ROTL3(S[i]+(S[i-1]+L[2])); \ L[0] = ROTL(L[0]+(S[i]+L[2]),(S[i]+L[2])); \ #define ROTL_BLOCK_j1(i) \ S[i] = ROTL3(S[i]+(S[i-1]+L[0])); \ L[1] = ROTL(L[1]+(S[i]+L[0]),(S[i]+L[0])); \ #define ROTL_BLOCK_j2(i) \ S[i] = ROTL3(S[i]+(S[i-1]+L[1])); \ L[2] = ROTL(L[2]+(S[i]+L[1]),(S[i]+L[1])); \ S[0] = ROTL3(S[0]); L[0] = ROTL(L[0]+S[0],S[0]); ROTL_BLOCK(1,1); ROTL_BLOCK(2,2); ROTL_BLOCK(3,0); ROTL_BLOCK(4,1); ROTL_BLOCK(5,2); ROTL_BLOCK(6,0); ROTL_BLOCK(7,1); ROTL_BLOCK(8,2); ROTL_BLOCK(9,0); ROTL_BLOCK(10,1); ROTL_BLOCK(11,2); ROTL_BLOCK(12,0); ROTL_BLOCK(13,1); ROTL_BLOCK(14,2); ROTL_BLOCK(15,0); ROTL_BLOCK(16,1); ROTL_BLOCK(17,2); ROTL_BLOCK(18,0); ROTL_BLOCK(19,1); ROTL_BLOCK(20,2); ROTL_BLOCK(21,0); ROTL_BLOCK(22,1); ROTL_BLOCK(23,2); ROTL_BLOCK(24,0); ROTL_BLOCK(25,1); ROTL_BLOCK_i0_j2; ROTL_BLOCK(1,0); ROTL_BLOCK(2,1); ROTL_BLOCK(3,2); ROTL_BLOCK(4,0); ROTL_BLOCK(5,1); ROTL_BLOCK(6,2); ROTL_BLOCK(7,0); ROTL_BLOCK(8,1); ROTL_BLOCK(9,2); ROTL_BLOCK(10,0); ROTL_BLOCK(11,1); ROTL_BLOCK(12,2); ROTL_BLOCK(13,0); ROTL_BLOCK(14,1); ROTL_BLOCK(15,2); ROTL_BLOCK(16,0); ROTL_BLOCK(17,1); ROTL_BLOCK(18,2); ROTL_BLOCK(19,0); ROTL_BLOCK(20,1); ROTL_BLOCK(21,2); ROTL_BLOCK(22,0); ROTL_BLOCK(23,1); ROTL_BLOCK(24,2); ROTL_BLOCK(25,0); ROTL_BLOCK_i0_j1; ROTL_BLOCK(1,2); ROTL_BLOCK(2,0); ROTL_BLOCK(3,1); ROTL_BLOCK(4,2); ROTL_BLOCK(5,0); ROTL_BLOCK(6,1); ROTL_BLOCK(7,2); ROTL_BLOCK(8,0); ROTL_BLOCK(9,1); ROTL_BLOCK(10,2); ROTL_BLOCK(11,0); ROTL_BLOCK(12,1); ROTL_BLOCK(13,2); ROTL_BLOCK(14,0); ROTL_BLOCK(15,1); ROTL_BLOCK(16,2); ROTL_BLOCK(17,0); ROTL_BLOCK(18,1); ROTL_BLOCK(19,2); ROTL_BLOCK(20,0); ROTL_BLOCK(21,1); ROTL_BLOCK(22,2); ROTL_BLOCK(23,0); ROTL_BLOCK(24,1); ROTL_BLOCK(25,2); A = rc5_72unitwork->plain.lo + S[0]; B = rc5_72unitwork->plain.hi + S[1]; #define FINAL_BLOCK(i) \ A = ROTL(A^B,B)+S[2*i]; \ B = ROTL(B^A,A)+S[2*i+1]; FINAL_BLOCK(1); FINAL_BLOCK(2); FINAL_BLOCK(3); FINAL_BLOCK(4); FINAL_BLOCK(5); FINAL_BLOCK(6); FINAL_BLOCK(7); FINAL_BLOCK(8); FINAL_BLOCK(9); FINAL_BLOCK(10); FINAL_BLOCK(11); FINAL_BLOCK(12); if (A == rc5_72unitwork->cypher.lo) { ++rc5_72unitwork->check.count; rc5_72unitwork->check.hi = rc5_72unitwork->L0.hi; rc5_72unitwork->check.mid = rc5_72unitwork->L0.mid; rc5_72unitwork->check.lo = rc5_72unitwork->L0.lo; if (B == rc5_72unitwork->cypher.hi) { *iterations -= (kiter + 1); return RESULT_FOUND; } } #define key rc5_72unitwork->L0 key.hi = (key.hi + 0x01) & 0x000000FF; if (!key.hi) { key.mid = key.mid + 0x01000000; if (!(key.mid & 0xFF000000u)) { key.mid = (key.mid + 0x00010000) & 0x00FFFFFF; if (!(key.mid & 0x00FF0000)) { key.mid = (key.mid + 0x00000100) & 0x0000FFFF; if (!(key.mid & 0x0000FF00)) { key.mid = (key.mid + 0x00000001) & 0x000000FF; if (!key.mid) { key.lo = key.lo + 0x01000000; if (!(key.lo & 0xFF000000u)) { key.lo = (key.lo + 0x00010000) & 0x00FFFFFF; if (!(key.lo & 0x00FF0000)) { key.lo = (key.lo + 0x00000100) & 0x0000FFFF; if (!(key.lo & 0x0000FF00)) { key.lo = (key.lo + 0x00000001) & 0x000000FF; } } } } } } } } #undef key } return RESULT_NOTHING; }
static void _VECTOR xor_salsa8(__m128i B[4], const __m128i Bx[4], int i) { __m128i X0, X1, X2, X3; if (i <= 128) { // a xor 0 = a X0 = B[0] = Bx[0]; X1 = B[1] = Bx[1]; X2 = B[2] = Bx[2]; X3 = B[3] = Bx[3]; } else { X0 = B[0] = _mm_xor_si128(B[0], Bx[0]); X1 = B[1] = _mm_xor_si128(B[1], Bx[1]); X2 = B[2] = _mm_xor_si128(B[2], Bx[2]); X3 = B[3] = _mm_xor_si128(B[3], Bx[3]); } for (i = 0; i < 4; i++) { /* Operate on columns. */ X1.m128i_u32[0] ^= ROTL(X0.m128i_u32[0] + X3.m128i_u32[0], 7); X2.m128i_u32[1] ^= ROTL(X1.m128i_u32[1] + X0.m128i_u32[1], 7); X3.m128i_u32[2] ^= ROTL(X2.m128i_u32[2] + X1.m128i_u32[2], 7); X0.m128i_u32[3] ^= ROTL(X3.m128i_u32[3] + X2.m128i_u32[3], 7); X2.m128i_u32[0] ^= ROTL(X1.m128i_u32[0] + X0.m128i_u32[0], 9); X3.m128i_u32[1] ^= ROTL(X2.m128i_u32[1] + X1.m128i_u32[1], 9); X0.m128i_u32[2] ^= ROTL(X3.m128i_u32[2] + X2.m128i_u32[2], 9); X1.m128i_u32[3] ^= ROTL(X0.m128i_u32[3] + X3.m128i_u32[3], 9); X3.m128i_u32[0] ^= ROTL(X2.m128i_u32[0] + X1.m128i_u32[0], 13); X0.m128i_u32[1] ^= ROTL(X3.m128i_u32[1] + X2.m128i_u32[1], 13); X1.m128i_u32[2] ^= ROTL(X0.m128i_u32[2] + X3.m128i_u32[2], 13); X2.m128i_u32[3] ^= ROTL(X1.m128i_u32[3] + X0.m128i_u32[3], 13); X0.m128i_u32[0] ^= ROTL(X3.m128i_u32[0] + X2.m128i_u32[0], 18); X1.m128i_u32[1] ^= ROTL(X0.m128i_u32[1] + X3.m128i_u32[1], 18); X2.m128i_u32[2] ^= ROTL(X1.m128i_u32[2] + X0.m128i_u32[2], 18); X3.m128i_u32[3] ^= ROTL(X2.m128i_u32[3] + X1.m128i_u32[3], 18); /* Operate on rows. */ X0.m128i_u32[1] ^= ROTL(X0.m128i_u32[0] + X0.m128i_u32[3], 7); X1.m128i_u32[2] ^= ROTL(X1.m128i_u32[1] + X1.m128i_u32[0], 7); X2.m128i_u32[3] ^= ROTL(X2.m128i_u32[2] + X2.m128i_u32[1], 7); X3.m128i_u32[0] ^= ROTL(X3.m128i_u32[3] + X3.m128i_u32[2], 7); X0.m128i_u32[2] ^= ROTL(X0.m128i_u32[1] + X0.m128i_u32[0], 9); X1.m128i_u32[3] ^= ROTL(X1.m128i_u32[2] + X1.m128i_u32[1], 9); X2.m128i_u32[0] ^= ROTL(X2.m128i_u32[3] + X2.m128i_u32[2], 9); X3.m128i_u32[1] ^= ROTL(X3.m128i_u32[0] + X3.m128i_u32[3], 9); X0.m128i_u32[3] ^= ROTL(X0.m128i_u32[2] + X0.m128i_u32[1], 13); X1.m128i_u32[0] ^= ROTL(X1.m128i_u32[3] + X1.m128i_u32[2], 13); X2.m128i_u32[1] ^= ROTL(X2.m128i_u32[0] + X2.m128i_u32[3], 13); X3.m128i_u32[2] ^= ROTL(X3.m128i_u32[1] + X3.m128i_u32[0], 13); X0.m128i_u32[0] ^= ROTL(X0.m128i_u32[3] + X0.m128i_u32[2], 18); X1.m128i_u32[1] ^= ROTL(X1.m128i_u32[0] + X1.m128i_u32[3], 18); X2.m128i_u32[2] ^= ROTL(X2.m128i_u32[1] + X2.m128i_u32[0], 18); X3.m128i_u32[3] ^= ROTL(X3.m128i_u32[2] + X3.m128i_u32[1], 18); } B[0] = _mm_add_epi32(B[0], X0); B[1] = _mm_add_epi32(B[1], X1); B[2] = _mm_add_epi32(B[2], X2); B[3] = _mm_add_epi32(B[3], X3); }
s32 CDECL rc5_72_unit_func_ansi_2 (RC5_72UnitWork *rc5_72unitwork, u32 *iterations, void * /*memblk*/) { u32 A1, A2, B1, B2; u32 S1[26], S2[26]; u32 L1[3], L2[3]; u32 kiter = *iterations/2; while (kiter--) { L1[2] = rc5_72unitwork->L0.hi; L2[2] = L1[2] + 0x01; L1[1] = L2[1] = rc5_72unitwork->L0.mid; L1[0] = L2[0] = rc5_72unitwork->L0.lo; #define KEY_INIT(i) S1[i] = S2[i] = P + i*Q; KEY_INIT(0); KEY_INIT(1); KEY_INIT(2); KEY_INIT(3); KEY_INIT(4); KEY_INIT(5); KEY_INIT(6); KEY_INIT(7); KEY_INIT(8); KEY_INIT(9); KEY_INIT(10); KEY_INIT(11); KEY_INIT(12); KEY_INIT(13); KEY_INIT(14); KEY_INIT(15); KEY_INIT(16); KEY_INIT(17); KEY_INIT(18); KEY_INIT(19); KEY_INIT(20); KEY_INIT(21); KEY_INIT(22); KEY_INIT(23); KEY_INIT(24); KEY_INIT(25); #define ROTL_BLOCK(i,j) ROTL_BLOCK_j##j (i) #define ROTL_BLOCK_i0_j1 \ S1[0] = ROTL3(S1[0]+(S1[25]+L1[0])); \ S2[0] = ROTL3(S2[0]+(S2[25]+L2[0])); \ L1[1] = ROTL(L1[1]+(S1[0]+L1[0]),(S1[0]+L1[0])); \ L2[1] = ROTL(L2[1]+(S2[0]+L2[0]),(S2[0]+L2[0])); \ #define ROTL_BLOCK_i0_j2 \ S1[0] = ROTL3(S1[0]+(S1[25]+L1[1])); \ S2[0] = ROTL3(S2[0]+(S2[25]+L2[1])); \ L1[2] = ROTL(L1[2]+(S1[0]+L1[1]),(S1[0]+L1[1])); \ L2[2] = ROTL(L2[2]+(S2[0]+L2[1]),(S2[0]+L2[1])); \ #define ROTL_BLOCK_j0(i) \ S1[i] = ROTL3(S1[i]+(S1[i-1]+L1[2])); \ S2[i] = ROTL3(S2[i]+(S2[i-1]+L2[2])); \ L1[0] = ROTL(L1[0]+(S1[i]+L1[2]),(S1[i]+L1[2])); \ L2[0] = ROTL(L2[0]+(S2[i]+L2[2]),(S2[i]+L2[2])); \ #define ROTL_BLOCK_j1(i) \ S1[i] = ROTL3(S1[i]+(S1[i-1]+L1[0])); \ S2[i] = ROTL3(S2[i]+(S2[i-1]+L2[0])); \ L1[1] = ROTL(L1[1]+(S1[i]+L1[0]),(S1[i]+L1[0])); \ L2[1] = ROTL(L2[1]+(S2[i]+L2[0]),(S2[i]+L2[0])); \ #define ROTL_BLOCK_j2(i) \ S1[i] = ROTL3(S1[i]+(S1[i-1]+L1[1])); \ S2[i] = ROTL3(S2[i]+(S2[i-1]+L2[1])); \ L1[2] = ROTL(L1[2]+(S1[i]+L1[1]),(S1[i]+L1[1])); \ L2[2] = ROTL(L2[2]+(S2[i]+L2[1]),(S2[i]+L2[1])); \ S1[0] = ROTL3(S1[0]); S2[0] = ROTL3(S2[0]); L1[0] = ROTL(L1[0]+S1[0],S1[0]); L2[0] = ROTL(L2[0]+S2[0],S2[0]); ROTL_BLOCK(1,1); ROTL_BLOCK(2,2); ROTL_BLOCK(3,0); ROTL_BLOCK(4,1); ROTL_BLOCK(5,2); ROTL_BLOCK(6,0); ROTL_BLOCK(7,1); ROTL_BLOCK(8,2); ROTL_BLOCK(9,0); ROTL_BLOCK(10,1); ROTL_BLOCK(11,2); ROTL_BLOCK(12,0); ROTL_BLOCK(13,1); ROTL_BLOCK(14,2); ROTL_BLOCK(15,0); ROTL_BLOCK(16,1); ROTL_BLOCK(17,2); ROTL_BLOCK(18,0); ROTL_BLOCK(19,1); ROTL_BLOCK(20,2); ROTL_BLOCK(21,0); ROTL_BLOCK(22,1); ROTL_BLOCK(23,2); ROTL_BLOCK(24,0); ROTL_BLOCK(25,1); ROTL_BLOCK_i0_j2; ROTL_BLOCK(1,0); ROTL_BLOCK(2,1); ROTL_BLOCK(3,2); ROTL_BLOCK(4,0); ROTL_BLOCK(5,1); ROTL_BLOCK(6,2); ROTL_BLOCK(7,0); ROTL_BLOCK(8,1); ROTL_BLOCK(9,2); ROTL_BLOCK(10,0); ROTL_BLOCK(11,1); ROTL_BLOCK(12,2); ROTL_BLOCK(13,0); ROTL_BLOCK(14,1); ROTL_BLOCK(15,2); ROTL_BLOCK(16,0); ROTL_BLOCK(17,1); ROTL_BLOCK(18,2); ROTL_BLOCK(19,0); ROTL_BLOCK(20,1); ROTL_BLOCK(21,2); ROTL_BLOCK(22,0); ROTL_BLOCK(23,1); ROTL_BLOCK(24,2); ROTL_BLOCK(25,0); ROTL_BLOCK_i0_j1; ROTL_BLOCK(1,2); ROTL_BLOCK(2,0); ROTL_BLOCK(3,1); ROTL_BLOCK(4,2); ROTL_BLOCK(5,0); ROTL_BLOCK(6,1); ROTL_BLOCK(7,2); ROTL_BLOCK(8,0); ROTL_BLOCK(9,1); ROTL_BLOCK(10,2); ROTL_BLOCK(11,0); ROTL_BLOCK(12,1); ROTL_BLOCK(13,2); ROTL_BLOCK(14,0); ROTL_BLOCK(15,1); ROTL_BLOCK(16,2); ROTL_BLOCK(17,0); ROTL_BLOCK(18,1); ROTL_BLOCK(19,2); ROTL_BLOCK(20,0); ROTL_BLOCK(21,1); ROTL_BLOCK(22,2); ROTL_BLOCK(23,0); ROTL_BLOCK(24,1); ROTL_BLOCK(25,2); A1 = rc5_72unitwork->plain.lo + S1[0]; A2 = rc5_72unitwork->plain.lo + S2[0]; B1 = rc5_72unitwork->plain.hi + S1[1]; B2 = rc5_72unitwork->plain.hi + S2[1]; #define FINAL_BLOCK(i) \ A1 = ROTL(A1^B1,B1)+S1[2*i]; \ A2 = ROTL(A2^B2,B2)+S2[2*i]; \ B1 = ROTL(B1^A1,A1)+S1[2*i+1]; \ B2 = ROTL(B2^A2,A2)+S2[2*i+1]; FINAL_BLOCK(1); FINAL_BLOCK(2); FINAL_BLOCK(3); FINAL_BLOCK(4); FINAL_BLOCK(5); FINAL_BLOCK(6); FINAL_BLOCK(7); FINAL_BLOCK(8); FINAL_BLOCK(9); FINAL_BLOCK(10); FINAL_BLOCK(11); FINAL_BLOCK(12); if (A1 == rc5_72unitwork->cypher.lo) { ++rc5_72unitwork->check.count; rc5_72unitwork->check.hi = rc5_72unitwork->L0.hi; rc5_72unitwork->check.mid = rc5_72unitwork->L0.mid; rc5_72unitwork->check.lo = rc5_72unitwork->L0.lo; if (B1 == rc5_72unitwork->cypher.hi) { *iterations -= (kiter + 1)*2; return RESULT_FOUND; } } if (A2 == rc5_72unitwork->cypher.lo) { ++rc5_72unitwork->check.count; rc5_72unitwork->check.hi = rc5_72unitwork->L0.hi + 0x01; rc5_72unitwork->check.mid = rc5_72unitwork->L0.mid; rc5_72unitwork->check.lo = rc5_72unitwork->L0.lo; if (B2 == rc5_72unitwork->cypher.hi) { *iterations -= (kiter + 1)*2 - 1; return RESULT_FOUND; } } #define key rc5_72unitwork->L0 key.hi = (key.hi + 0x02) & 0x000000FF; if (!key.hi) { key.mid = key.mid + 0x01000000; if (!(key.mid & 0xFF000000u)) { key.mid = (key.mid + 0x00010000) & 0x00FFFFFF; if (!(key.mid & 0x00FF0000)) { key.mid = (key.mid + 0x00000100) & 0x0000FFFF; if (!(key.mid & 0x0000FF00)) { key.mid = (key.mid + 0x00000001) & 0x000000FF; if (!key.mid) { key.lo = key.lo + 0x01000000; if (!(key.lo & 0xFF000000u)) { key.lo = (key.lo + 0x00010000) & 0x00FFFFFF; if (!(key.lo & 0x00FF0000)) { key.lo = (key.lo + 0x00000100) & 0x0000FFFF; if (!(key.lo & 0x0000FF00)) { key.lo = (key.lo + 0x00000001) & 0x000000FF; } } } } } } } } #undef key } return RESULT_NOTHING; }