2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-06 16:57:25 +00:00

Move XChunk processors to ZoneCommon

This commit is contained in:
Jan
2021-03-16 20:42:48 +01:00
parent ca1329323b
commit f22012d282
33 changed files with 383 additions and 317 deletions

View File

@@ -0,0 +1,44 @@
#include "XChunkProcessorSalsa20Decryption.h"
#include <cassert>
#include "Crypto.h"
#include "AbstractSalsa20Processor.h"
XChunkProcessorSalsa20Decryption::XChunkProcessorSalsa20Decryption(const int streamCount, std::string& zoneName, const uint8_t* salsa20Key, size_t keySize)
: AbstractSalsa20Processor(streamCount, zoneName, salsa20Key, keySize)
{
}
size_t XChunkProcessorSalsa20Decryption::Process(const int streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize)
{
assert(streamNumber >= 0 && streamNumber < m_stream_count);
assert(input != nullptr);
assert(output != nullptr);
assert(inputLength <= outputBufferSize);
auto& streamContext = m_stream_contexts[streamNumber];
// Initialize Salsa20 with an IV of the first 8 bytes of the current hash block
streamContext.m_salsa20->SetIV(GetHashBlock(streamNumber), SALSA20_IV_SIZE);
streamContext.m_salsa20->Process(input, output, inputLength);
// Hash decrypted XChunk
uint8_t blockSha1Hash[SHA1_HASH_SIZE];
streamContext.m_sha1->Init();
streamContext.m_sha1->Process(output, inputLength);
streamContext.m_sha1->Finish(&blockSha1Hash);
// Advance index to next hash block
m_stream_block_indices[streamNumber] = (m_stream_block_indices[streamNumber] + 1) % BLOCK_HASHES_COUNT;
auto* nextHashBlock = GetHashBlock(streamNumber);
// XOR the upcoming hash block with the hash of the XChunk utilizing the previous hash block
for (unsigned int hashOffset = 0; hashOffset < sizeof(blockSha1Hash); hashOffset++)
{
nextHashBlock[hashOffset] ^= blockSha1Hash[hashOffset];
}
return inputLength;
}