Skip to content

Commit

Permalink
Update hasher, adding x64 versions of MD5, MD4 and SHA1.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpiva committed Oct 5, 2017
1 parent 5561d7a commit ea3b277
Show file tree
Hide file tree
Showing 17 changed files with 3,374 additions and 306 deletions.
2 changes: 1 addition & 1 deletion Shoko.Commons
Submodule Shoko.Commons updated 1 files
+1 −1 Shoko.Models
41 changes: 22 additions & 19 deletions hasher/Hasher.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@
</ResourceCompile>
<PreLinkEvent>
<Message>build assemblies</Message>
<Command>ml.exe md4_asm.asm /c /Cx /coff &amp;&amp; ^
ml.exe md5_asm.asm /c /Cx /coff &amp;&amp; ^
ml.exe sha_asm.asm /c /Cx /coff &amp;&amp; ^
<Command>ml.exe md4x86.asm /c /Cx /coff &amp;&amp; ^
ml.exe md5x86.asm /c /Cx /coff &amp;&amp; ^
ml.exe sha1x86.asm /c /Cx /coff &amp;&amp; ^
ml.exe crc32x86.asm /c /Cx /coff</Command>
</PreLinkEvent>
<Link>
<AdditionalDependencies>MD4_asm.obj;MD5_asm.obj;SHA_asm.obj;crc32x86.obj;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>MD4x86.obj;MD5x86.obj;SHA1x86.obj;crc32x86.obj;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>.\Debug\hasher.dll</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down Expand Up @@ -171,7 +171,7 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
</Midl>
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN64;_DEBUG;_WINDOWS;_USRDLL;HASHER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN64;_DEBUG;_WINDOWS;_USRDLL;HASHLIB_USE_ASM;HASHER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand All @@ -189,7 +189,7 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
<Culture>0x0813</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>crc32x64.obj;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>crc32x64.obj;MD5x64.obj;MD4x64.obj;sha1x64.obj;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>.\Debug_x64\hasher.dll</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -209,7 +209,7 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
</Command>
</PostBuildEvent>
<PreLinkEvent>
<Command>ml64.exe crc32x64.asm /c /nologo /W3 /Zi</Command>
<Command>ml64.exe crc32x64.asm /c /nologo /W3 /Zi &amp;&amp; ^ml64.exe MD5x64.asm /c /nologo /W3 /Zi &amp;&amp; ^ml64.exe MD4x64.asm /c /nologo /W3 /Zi &amp;&amp; ^ml64.exe sha1x64.asm /c /nologo /W3 /Zi</Command>
</PreLinkEvent>
<PreLinkEvent>
<Message>build assemblies</Message>
Expand Down Expand Up @@ -249,13 +249,13 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
</ResourceCompile>
<PreLinkEvent>
<Message>build assemblies</Message>
<Command>ml.exe md4_asm.asm /c /Cx /coff &amp;&amp; ^
ml.exe md5_asm.asm /c /Cx /coff &amp;&amp; ^
ml.exe sha_asm.asm /c /Cx /coff &amp;&amp; ^
<Command>ml.exe md4x86.asm /c /Cx /coff &amp;&amp; ^
ml.exe md5x86.asm /c /Cx /coff &amp;&amp; ^
ml.exe sha1x86.asm /c /Cx /coff &amp;&amp; ^
ml.exe crc32x86.asm /c /Cx /coff</Command>
</PreLinkEvent>
<Link>
<AdditionalDependencies>MD4_asm.obj;MD5_asm.obj;SHA_asm.obj;crc32x86.obj;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>MD4x86.obj;MD5x86.obj;SHA1x86.obj;crc32x86.obj;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>.\Release\hasher.dll</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<ProgramDatabaseFile>.\Release/hasher.pdb</ProgramDatabaseFile>
Expand Down Expand Up @@ -286,7 +286,7 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
<ClCompile>
<Optimization>Full</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<PreprocessorDefinitions>WIN64;NDEBUG;_WINDOWS;x64;_USRDLL;HASHER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN64;NDEBUG;_WINDOWS;x64;_USRDLL;HASHER_EXPORTS;HASHLIB_USE_ASM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<StringPooling>true</StringPooling>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<FunctionLevelLinking>true</FunctionLevelLinking>
Expand All @@ -306,7 +306,7 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
<Culture>0x0813</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>crc32x64.obj;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>crc32x64.obj;MD5x64.obj;MD4x64.obj;sha1x64.obj;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>.\Release_x64\hasher.dll</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<ProgramDatabaseFile>.\Release_x64/hasher.pdb</ProgramDatabaseFile>
Expand All @@ -325,7 +325,7 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
</Command>
</PostBuildEvent>
<PreLinkEvent>
<Command>ml64.exe crc32x64.asm /c /nologo /W3 /Zi</Command>
<Command>ml64.exe crc32x64.asm /c /nologo /W3 /Zi &amp;&amp; ^ml64.exe MD5x64.asm /c /nologo /W3 /Zi &amp;&amp; ^ml64.exe MD4x64.asm /c /nologo /W3 /Zi &amp;&amp; ^ml64.exe sha1x64.asm /c /nologo /W3 /Zi</Command>
</PreLinkEvent>
<PreLinkEvent>
<Message>build assemblies</Message>
Expand All @@ -336,14 +336,14 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
<ClCompile Include="hash_md5.cpp" />
<ClCompile Include="hash_sha.cpp" />
<ClCompile Include="hasher.cpp" />
<ClCompile Include="md4.cpp">
<ClCompile Include="MD4.cpp">
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<ClCompile Include="MD5.cpp" />
<ClCompile Include="SHA.cpp" />
<ClCompile Include="SHA1.cpp" />
<ClCompile Include="StdAfx.cpp">
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -367,9 +367,12 @@ ml.exe crc32x86.asm /c /Cx /coff</Command>
<ItemGroup>
<None Include="crc32x86.asm" />
<None Include="crc32x64.asm" />
<None Include="MD4_asm.asm" />
<None Include="MD5_asm.asm" />
<None Include="SHA_asm.asm" />
<None Include="MD4x86.asm" />
<None Include="MD4x64.asm" />
<None Include="MD5x86.asm" />
<None Include="MD5x64.asm" />
<None Include="SHA1x64.asm" />
<None Include="SHA1x86.asm" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
17 changes: 10 additions & 7 deletions hasher/Hasher.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
<ClCompile Include="hasher.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="md4.cpp">
<ClCompile Include="StdAfx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SHA.cpp">
<ClCompile Include="MD5.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="StdAfx.cpp">
<ClCompile Include="MD4.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MD5.cpp">
<ClCompile Include="SHA1.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down Expand Up @@ -64,10 +64,13 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="MD4_asm.asm" />
<None Include="MD5_asm.asm" />
<None Include="SHA_asm.asm" />
<None Include="crc32x86.asm" />
<None Include="crc32x64.asm" />
<None Include="MD4x64.asm" />
<None Include="MD4x86.asm" />
<None Include="MD5x86.asm" />
<None Include="MD5x64.asm" />
<None Include="SHA1x86.asm" />
<None Include="SHA1x64.asm" />
</ItemGroup>
</Project>
57 changes: 48 additions & 9 deletions hasher/MD4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
#include "MD4.h"


#ifdef HASHLIB_USE_ASM
extern "C" void __stdcall MD4_Add_p5(CMD4::MD4State*, const void* pData, std::size_t nLength);
#endif

const unsigned char hashPadding[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -68,13 +65,55 @@ void CMD4::Finish()
Add(&bits, sizeof(bits));
}



#ifdef HASHLIB_USE_ASM

#if defined(_WIN64) || defined(__x86_64__)
extern "C" void __fastcall MD4_x64(const void *, const void* pData, std::size_t nLength);
#else
extern "C" void __stdcall MD4_Add_p5(CMD4::MD4State*, const void* pData, std::size_t nLength);
#endif

void CMD4::Add(const void* pData, std::size_t nLength)
{
#if defined(_WIN64) || defined(__x86_64__)
// Update number of bytes
const char* input = static_cast< const char* >(pData);
{
uint32 index = static_cast< uint32 >(m_State.m_nCount % m_State.blockSize);
m_State.m_nCount += nLength;
if (index)
{
// buffer has some data already - lets fill it
// before doing the rest of the transformation on the original data
if (index + nLength < m_State.blockSize)
{
std::memcpy(m_State.m_oBuffer + index, input, nLength);
return;
}
std::memcpy(m_State.m_oBuffer + index, input, m_State.blockSize - index);
nLength -= m_State.blockSize - index;
input += m_State.blockSize - index;
MD4_x64(&(m_State.m_nState[0]), m_State.m_oBuffer, 1);
}
}
// Transform as many times as possible using the original data stream
const char* const end = input + nLength - nLength % m_State.blockSize;
size_t abs = nLength / m_State.blockSize;
MD4_x64(&(m_State.m_nState[0]), input, abs);
abs *= m_State.blockSize;
input += abs;
nLength %= m_State.blockSize;
// Buffer remaining input
if (nLength)
std::memcpy(m_State.m_oBuffer, input, nLength);
#else
MD4_Add_p5(&m_State, pData, nLength);
}
#endif


}
#else // HASHLIB_USE_ASM

namespace
Expand Down Expand Up @@ -189,13 +228,12 @@ void CMD4::Transform(const uint32* data)
m_State.m_nState[2] += c;
m_State.m_nState[3] += d;
}

void CMD4::Add(const void* pData, std::size_t nLength)
{
// Update number of bytes
const char* input = static_cast< const char* >(pData);
const char* input = static_cast<const char*>(pData);
{
uint32 index = static_cast< uint32 >(m_State.m_nCount % m_State.blockSize);
uint32 index = static_cast<uint32>(m_State.m_nCount % m_State.blockSize);
m_State.m_nCount += nLength;
if (index)
{
Expand All @@ -209,19 +247,20 @@ void CMD4::Add(const void* pData, std::size_t nLength)
std::memcpy(m_State.m_oBuffer + index, input, m_State.blockSize - index);
nLength -= m_State.blockSize - index;
input += m_State.blockSize - index;
Transform(reinterpret_cast< const uint32* >(m_State.m_oBuffer));
Transform(reinterpret_cast<const uint32*>(m_State.m_oBuffer));
}
}
// Transform as many times as possible using the original data stream
const char* const end = input + nLength - nLength % m_State.blockSize;
nLength %= m_State.blockSize;
for (; input != end; input += m_State.blockSize)
Transform(reinterpret_cast< const uint32* >(input));
Transform(reinterpret_cast<const uint32*>(input));
// Buffer remaining input
if (nLength)
std::memcpy(m_State.m_oBuffer, input, nLength);
}


#endif // HASHLIB_USE_ASM

//
Expand Down
Loading

0 comments on commit ea3b277

Please sign in to comment.