MCPcopy Create free account
hub / github.com/diasurgical/devilution / DetectFileKeyByKnownContent

Function DetectFileKeyByKnownContent

3rdParty/StormLib/src/SBaseCommon.cpp:509–551  ·  view source on GitHub ↗

Function tries to detect file encryption key based on expected file content It is the same function like before, except that we know the value of the second DWORD

Source from the content-addressed store, hash-verified

507// Function tries to detect file encryption key based on expected file content
508// It is the same function like before, except that we know the value of the second DWORD
509DWORD DetectFileKeyByKnownContent(void * pvEncryptedData, DWORD dwDecrypted0, DWORD dwDecrypted1)
510{
511 LPDWORD EncryptedData = (LPDWORD)pvEncryptedData;
512 DWORD dwKey1PlusKey2;
513 DWORD DataBlock[2];
514
515 // Get the value of the combined encryption key
516 dwKey1PlusKey2 = (EncryptedData[0] ^ dwDecrypted0) - 0xEEEEEEEE;
517
518 // Try all 256 combinations of dwKey1
519 for(DWORD i = 0; i < 0x100; i++)
520 {
521 DWORD dwSaveKey1;
522 DWORD dwKey1 = dwKey1PlusKey2 - StormBuffer[MPQ_HASH_KEY2_MIX + i];
523 DWORD dwKey2 = 0xEEEEEEEE;
524
525 // Modify the second key and decrypt the first DWORD
526 dwKey2 += StormBuffer[MPQ_HASH_KEY2_MIX + (dwKey1 & 0xFF)];
527 DataBlock[0] = EncryptedData[0] ^ (dwKey1 + dwKey2);
528
529 // Did we obtain the same value like dwDecrypted0?
530 if(DataBlock[0] == dwDecrypted0)
531 {
532 // Save this key value
533 dwSaveKey1 = dwKey1;
534
535 // Rotate both keys
536 dwKey1 = ((~dwKey1 << 0x15) + 0x11111111) | (dwKey1 >> 0x0B);
537 dwKey2 = DataBlock[0] + dwKey2 + (dwKey2 << 5) + 3;
538
539 // Modify the second key again and decrypt the second DWORD
540 dwKey2 += StormBuffer[MPQ_HASH_KEY2_MIX + (dwKey1 & 0xFF)];
541 DataBlock[1] = EncryptedData[1] ^ (dwKey1 + dwKey2);
542
543 // Now compare the results
544 if(DataBlock[1] == dwDecrypted1)
545 return dwSaveKey1;
546 }
547 }
548
549 // Key not found
550 return 0;
551}
552
553DWORD DetectFileKeyByContent(void * pvEncryptedData, DWORD dwSectorSize, DWORD dwFileSize)
554{

Callers 1

DetectFileKeyByContentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected