Add crypt files from NSF

This commit is contained in:
Duncan 2021-09-18 21:31:23 +01:00 committed by GitHub
parent fa57b6aea0
commit 81051f2d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 113 additions and 0 deletions

View File

@ -802,6 +802,7 @@
E6C71B6165224F65AA87E65B /* RideUseSystem.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DA720D496604387806AC168 /* RideUseSystem.h */; };
C8D612EB56BD4214BEC0F7FF /* GroupVector.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F4D523B8782E4C458AF1490D /* GroupVector.hpp */; };
B2F44E535BD14A03BE8B9D14 /* ZipStream.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F28A181D311D4E078FDB090C /* ZipStream.hpp */; };
7CDC7EE9B12E40FB9FE78546 /* Crypt.OpenRCT2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4248E4E4394842D4AF6119DA /* Crypt.OpenRCT2.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -1907,6 +1908,7 @@
2DA720D496604387806AC168 /* RideUseSystem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RideUseSystem.h; path = src/openrct2/peep/RideUseSystem.h; sourceTree = SOURCE_ROOT; };
F4D523B8782E4C458AF1490D /* GroupVector.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = GroupVector.hpp; path = src/openrct2/core/GroupVector.hpp; sourceTree = SOURCE_ROOT; };
F28A181D311D4E078FDB090C /* ZipStream.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ZipStream.hpp; path = src/openrct2/core/ZipStream.hpp; sourceTree = SOURCE_ROOT; };
4248E4E4394842D4AF6119DA /* Crypt.OpenRCT2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Crypt.OpenRCT2.cpp; path = src/openrct2/core/Crypt.OpenRCT2.cpp; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -2578,6 +2580,7 @@
BA2317BF6FB54E328DEB7055 /* EnumMap.hpp */,
F4D523B8782E4C458AF1490D /* GroupVector.hpp */,
F28A181D311D4E078FDB090C /* ZipStream.hpp */,
4248E4E4394842D4AF6119DA /* Crypt.OpenRCT2.cpp */,
);
path = core;
sourceTree = "<group>";
@ -4484,6 +4487,7 @@
5B6E418A2F264952BA0CC2F2 /* ScTileElement.cpp in Sources */,
6C90BE01D190493295071B23 /* ScTile.cpp in Sources */,
258C212125F84FA2B4C3BCAE /* RideUseSystem.cpp in Sources */,
7CDC7EE9B12E40FB9FE78546 /* Crypt.OpenRCT2.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,101 @@
/*****************************************************************************
* Copyright (c) 2014-2021 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Crypt.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>
using namespace Crypt;
class OpenRCT2FNV1aAlgorithm : public FNV1aAlgorithm
{
private:
static constexpr uint64_t Offset = 0xCBF29CE484222325ULL;
static constexpr uint64_t Prime = 0x00000100000001B3ULL;
uint64_t _data = Offset;
uint8_t _rem[8]{};
size_t _remLen{};
void ProcessRemainder()
{
if (_remLen > 0)
{
uint64_t temp{};
std::memcpy(&temp, _rem, _remLen);
_data ^= temp;
_data *= Prime;
_remLen = 0;
}
}
public:
HashAlgorithm* Clear() override
{
_data = Offset;
return this;
}
HashAlgorithm* Update(const void* data, size_t dataLen) override
{
if (dataLen == 0)
return this;
auto src = reinterpret_cast<const uint64_t*>(data);
if (_remLen > 0)
{
// We have remainder, so fill rest of it with bytes from src
auto fillLen = sizeof(uint64_t) - _remLen;
assert(_remLen + fillLen <= sizeof(uint64_t));
std::memcpy(_rem + _remLen, src, fillLen);
src = reinterpret_cast<const uint64_t*>(reinterpret_cast<const uint8_t*>(src) + fillLen);
_remLen += fillLen;
dataLen -= fillLen;
ProcessRemainder();
}
// Process every block of 8 bytes
while (dataLen >= sizeof(uint64_t))
{
auto temp = *src++;
_data ^= temp;
_data *= Prime;
dataLen -= sizeof(uint64_t);
}
// Store the remaining data (< 8 bytes)
if (dataLen > 0)
{
_remLen = dataLen;
std::memcpy(&_rem, src, dataLen);
}
return this;
}
Result Finish() override
{
ProcessRemainder();
Result res;
std::memcpy(res.data(), &_data, sizeof(_data));
return res;
}
};
namespace Crypt
{
std::unique_ptr<FNV1aAlgorithm> CreateFNV1a()
{
return std::make_unique<OpenRCT2FNV1aAlgorithm>();
}
} // namespace Crypt

View File

@ -48,13 +48,20 @@ namespace Crypt
using Sha1Algorithm = HashAlgorithm<20>;
using Sha256Algorithm = HashAlgorithm<32>;
using FNV1aAlgorithm = HashAlgorithm<8>;
// Factories
[[nodiscard]] std::unique_ptr<FNV1aAlgorithm> CreateFNV1a();
[[nodiscard]] std::unique_ptr<Sha1Algorithm> CreateSHA1();
[[nodiscard]] std::unique_ptr<Sha256Algorithm> CreateSHA256();
[[nodiscard]] std::unique_ptr<RsaAlgorithm> CreateRSA();
[[nodiscard]] std::unique_ptr<RsaKey> CreateRSAKey();
inline FNV1aAlgorithm::Result FNV1a(const void* data, size_t dataLen)
{
return CreateFNV1a()->Update(data, dataLen)->Finish();
}
inline Sha1Algorithm::Result SHA1(const void* data, size_t dataLen)
{
return CreateSHA1()->Update(data, dataLen)->Finish();

View File

@ -598,6 +598,7 @@
<ClCompile Include="core\ChecksumStream.cpp" />
<ClCompile Include="core\Console.cpp" />
<ClCompile Include="core\Crypt.CNG.cpp" />
<ClCompile Include="core\Crypt.OpenRCT2.cpp" />
<ClCompile Include="core\Crypt.OpenSSL.cpp" />
<ClCompile Include="core\Diagnostics.cpp" />
<ClCompile Include="core\File.cpp" />