This commit is contained in:
Igor Pavlov
2003-12-11 00:00:00 +00:00
committed by Kornel Lesiński
commit 8c1b5c7b7e
982 changed files with 118799 additions and 0 deletions

355
7zip/Crypto/7zAES/7zAES.cpp Executable file
View File

@@ -0,0 +1,355 @@
// 7z_AES.h
#include "StdAfx.h"
#include "Windows/Defs.h"
#include "Windows/Synchronization.h"
#include "../../Common/StreamObjects.h"
#include "7zAES.h"
// #include "../../Hash/Common/CryptoHashInterface.h"
#ifdef CRYPTO_AES
#include "../AES/MyAES.h"
#endif
#include "SHA256.h"
using namespace NWindows;
#ifndef CRYPTO_AES
extern HINSTANCE g_hInstance;
#endif
namespace NCrypto {
namespace NSevenZ {
bool CKeyInfo::IsEqualTo(const CKeyInfo &a) const
{
if (SaltSize != a.SaltSize || NumCyclesPower != a.NumCyclesPower)
return false;
for (UINT32 i = 0; i < SaltSize; i++)
if (Salt[i] != a.Salt[i])
return false;
return (Password == a.Password);
}
void CKeyInfo::CalculateDigest()
{
if (NumCyclesPower == 0x3F)
{
UINT32 pos;
for (pos = 0; pos < SaltSize; pos++)
Key[pos] = Salt[pos];
for (UINT32 i = 0; i < Password.GetCapacity() && pos < kKeySize; i++)
Key[pos++] = Password[i];
for (; pos < kKeySize; pos++)
Key[pos] = 0;
}
else
{
/*
CMyComPtr<ICryptoHash> sha;
RINOK(sha.CoCreateInstance(CLSID_CCrypto_Hash_SHA256));
RINOK(sha->Init());
*/
NCrypto::NSHA256::SHA256 sha;
const UINT64 numRounds = UINT64(1) << (NumCyclesPower);
for (UINT64 round = 0; round < numRounds; round++)
{
/*
RINOK(sha->Update(Salt, SaltSize));
RINOK(sha->Update(Password, Password.GetCapacity()));
// change it if big endian;
RINOK(sha->Update(&round, sizeof(round)));
*/
// sha.Update(Salt, sizeof(Salt));
sha.Update(Salt, SaltSize);
sha.Update(Password, Password.GetCapacity());
// change it if big endian;
sha.Update((const BYTE *)&round, sizeof(round));
}
// return sha->GetDigest(Key);
sha.Final(Key);
}
}
bool CKeyInfoCache::Find(CKeyInfo &key)
{
for (int i = 0; i < Keys.Size(); i++)
{
const CKeyInfo &cached = Keys[i];
if (key.IsEqualTo(cached))
{
for (int j = 0; j < kKeySize; j++)
key.Key[j] = cached.Key[j];
if (i != 0)
{
Keys.Insert(0, cached);
Keys.Delete(i+1);
}
return true;
}
}
return false;
}
void CKeyInfoCache::Add(CKeyInfo &key)
{
if (Find(key))
return;
if (Keys.Size() >= Size)
Keys.DeleteBack();
Keys.Insert(0, key);
}
static CKeyInfoCache g_GlobalKeyCache(32);
static NSynchronization::CCriticalSection g_GlobalKeyCacheCriticalSection;
CBase::CBase():
_cachedKeys(16)
{
for (int i = 0; i < sizeof(_iv); i++)
_iv[i] = 0;
}
void CBase::CalculateDigest()
{
NSynchronization::CCriticalSectionLock lock(g_GlobalKeyCacheCriticalSection);
if (_cachedKeys.Find(_key))
g_GlobalKeyCache.Add(_key);
else
{
if (!g_GlobalKeyCache.Find(_key))
{
_key.CalculateDigest();
g_GlobalKeyCache.Add(_key);
}
_cachedKeys.Add(_key);
}
}
/*
static void GetRandomData(BYTE *data)
{
// probably we don't need truly random.
// it's enough to prevent dictionary attack;
// but it gives some info about time when compressing
// was made.
UINT64 tempValue;
SYSTEMTIME systemTime;
FILETIME fileTime;
::GetSystemTime(&systemTime);
::SystemTimeToFileTime(&systemTime, &fileTime);
tempValue = *(const UINT64 *)&fileTime;
LARGE_INTEGER counter;
::QueryPerformanceCounter(&counter);
tempValue += *(const UINT64 *)&counter;
tempValue += (UINT64)(GetTickCount()) << 32;
*(UINT64 *)data = tempValue;
}
*/
STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
{
_key.Init();
for (UINT32 i = 0; i < sizeof(_iv); i++)
_iv[i] = 0;
_key.SaltSize = 0;
// _key.SaltSize = 8;
// GetRandomData(_key.Salt);
int ivSize = 0;
// _key.NumCyclesPower = 0x3F;
_key.NumCyclesPower = 18;
BYTE firstByte = _key.NumCyclesPower |
(((_key.SaltSize == 0) ? 0 : 1) << 7) |
(((ivSize == 0) ? 0 : 1) << 6);
RINOK(outStream->Write(&firstByte, sizeof(firstByte), NULL));
if (_key.SaltSize == 0 && ivSize == 0)
return S_OK;
BYTE saltSizeSpec = (_key.SaltSize == 0) ? 0 : (_key.SaltSize - 1);
BYTE ivSizeSpec = (ivSize == 0) ? 0 : (ivSize - 1);
BYTE secondByte = ((saltSizeSpec) << 4) | ivSizeSpec;
RINOK(outStream->Write(&secondByte, sizeof(secondByte), NULL));
if (_key.SaltSize > 0)
{
RINOK(outStream->Write(_key.Salt, _key.SaltSize, NULL));
}
if (ivSize > 0)
{
RINOK(outStream->Write(_iv, ivSize, NULL));
}
return S_OK;
}
STDMETHODIMP CEncoder::SetDecoderProperties(ISequentialInStream *inStream)
{
return S_OK;
}
STDMETHODIMP CDecoder::SetDecoderProperties(ISequentialInStream *inStream)
{
_key.Init();
for (int i = 0; i < sizeof(_iv); i++)
_iv[i] = 0;
UINT32 processedSize;
BYTE firstByte;
RINOK(inStream->Read(&firstByte, sizeof(firstByte), &processedSize));
if (processedSize == 0)
return S_OK;
_key.NumCyclesPower = firstByte & 0x3F;
if ((firstByte & 0xC0) == 0)
return S_OK;
_key.SaltSize = (firstByte >> 7) & 1;
UINT32 ivSize = (firstByte >> 6) & 1;
BYTE secondByte;
RINOK(inStream->Read(&secondByte, sizeof(secondByte), &processedSize));
if (processedSize == 0)
return E_INVALIDARG;
_key.SaltSize += (secondByte >> 4);
ivSize += (secondByte & 0x0F);
RINOK(inStream->Read(_key.Salt,
_key.SaltSize, &processedSize));
if (processedSize != _key.SaltSize)
return E_INVALIDARG;
RINOK(inStream->Read(_iv, ivSize, &processedSize));
if (processedSize != ivSize)
return E_INVALIDARG;
return S_OK;
}
STDMETHODIMP CEncoder::CryptoSetPassword(const BYTE *data, UINT32 size)
{
_key.Password.SetCapacity(size);
memcpy(_key.Password, data, size);
return S_OK;
}
STDMETHODIMP CDecoder::CryptoSetPassword(const BYTE *data, UINT32 size)
{
_key.Password.SetCapacity(size);
memcpy(_key.Password, data, size);
return S_OK;
}
/*
static BYTE *WideToRaw(const wchar_t *src, BYTE *dest, int destSize=0x10000000)
{
for (int i = 0; i < destSize; i++, src++)
{
dest[i * 2] = (BYTE)*src;
dest[i * 2 + 1]= (BYTE)(*src >> 8);
if (*src == 0)
break;
}
return(dest);
}
*/
#ifndef CRYPTO_AES
bool GetAESLibPath(TCHAR *path)
{
TCHAR fullPath[MAX_PATH + 1];
if (::GetModuleFileName(g_hInstance, fullPath, MAX_PATH) == 0)
return false;
LPTSTR fileNamePointer;
DWORD needLength = ::GetFullPathName(fullPath, MAX_PATH + 1,
path, &fileNamePointer);
if (needLength == 0 || needLength >= MAX_PATH)
return false;
lstrcpy(fileNamePointer, TEXT("AES.dll"));
return true;
}
#endif
STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, UINT64 const *inSize,
const UINT64 *outSize,ICompressProgressInfo *progress)
{
CalculateDigest();
if (_aesEncoder == 0)
{
#ifdef CRYPTO_AES
_aesEncoder = new CAES256_CBC_Encoder;
#else
if ((HMODULE)_aesEncoderLibrary == 0)
{
TCHAR filePath[MAX_PATH + 2];
if (!GetAESLibPath(filePath))
return ::GetLastError();
RINOK(_aesEncoderLibrary.LoadAndCreateCoder2(filePath,
CLSID_CCrypto_AES256_Encoder, &_aesEncoder));
}
#endif
}
CSequentialInStreamImp *ivStreamSpec = new CSequentialInStreamImp;
CMyComPtr<ISequentialInStream> ivStream(ivStreamSpec);
ivStreamSpec->Init(_iv, sizeof(_iv));
CSequentialInStreamImp *keyStreamSpec = new CSequentialInStreamImp;
CMyComPtr<ISequentialInStream> keyStream(keyStreamSpec);
keyStreamSpec->Init(_key.Key, sizeof(_key.Key));
ISequentialInStream *inStreams[3] = { inStream, ivStream, keyStream };
UINT64 ivSize = sizeof(_iv);
UINT64 keySize = sizeof(_key.Key);
const UINT64 *inSizes[3] = { inSize, &ivSize, &ivSize, };
return _aesEncoder->Code(inStreams, inSizes, 3,
&outStream, &outSize, 1, progress);
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, UINT64 const *inSize,
const UINT64 *outSize,ICompressProgressInfo *progress)
{
CalculateDigest();
if (_aesDecoder == 0)
{
#ifdef CRYPTO_AES
_aesDecoder = new CAES256_CBC_Decoder;
#else
if ((HMODULE)_aesDecoderLibrary == 0)
{
TCHAR filePath[MAX_PATH + 2];
if (!GetAESLibPath(filePath))
return ::GetLastError();
RINOK(_aesDecoderLibrary.LoadAndCreateCoder2(filePath,
CLSID_CCrypto_AES256_Decoder, &_aesDecoder));
}
#endif
}
CSequentialInStreamImp *ivStreamSpec = new CSequentialInStreamImp;
CMyComPtr<ISequentialInStream> ivStream(ivStreamSpec);
ivStreamSpec->Init(_iv, sizeof(_iv));
CSequentialInStreamImp *keyStreamSpec = new CSequentialInStreamImp;
CMyComPtr<ISequentialInStream> keyStream(keyStreamSpec);
keyStreamSpec->Init(_key.Key, sizeof(_key.Key));
ISequentialInStream *inStreams[3] = { inStream, ivStream, keyStream };
UINT64 ivSize = sizeof(_iv);
UINT64 keySize = sizeof(_key.Key);
const UINT64 *inSizes[3] = { inSize, &ivSize, &ivSize, };
return _aesDecoder->Code(inStreams, inSizes, 3,
&outStream, &outSize, 1, progress);
}
}}

8
7zip/Crypto/7zAES/7zAES.def Executable file
View File

@@ -0,0 +1,8 @@
; 7zAES.def
LIBRARY 7zAES.dll
EXPORTS
CreateObject PRIVATE
GetNumberOfMethods PRIVATE
GetMethodProperty PRIVATE

209
7zip/Crypto/7zAES/7zAES.dsp Executable file
View File

@@ -0,0 +1,209 @@
# Microsoft Developer Studio Project File - Name="7zAES" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=7zAES - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "7zAES.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "7zAES.mak" CFG="7zAES - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "7zAES - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "7zAES - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "7zAES - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 1
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "7zAES_EXPORTS" /YX /FD /c
# ADD CPP /nologo /Gz /MD /W3 /GX /O1 /I "..\..\..\\" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /Yu"StdAfx.h" /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x419 /d "NDEBUG"
# ADD RSC /l 0x419 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"C:\Program Files\7-Zip\Codecs\7zAES.dll" /opt:NOWIN98
# SUBTRACT LINK32 /pdb:none
!ELSEIF "$(CFG)" == "7zAES - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 1
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "7zAES_EXPORTS" /YX /FD /GZ /c
# ADD CPP /nologo /Gz /MTd /W3 /Gm /GX /ZI /Od /I "..\..\..\\" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /Yu"StdAfx.h" /FD /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x419 /d "_DEBUG"
# ADD RSC /l 0x419 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"C:\Program Files\7-Zip\Codecs\7zAES.dll" /pdbtype:sept
!ENDIF
# Begin Target
# Name "7zAES - Win32 Release"
# Name "7zAES - Win32 Debug"
# Begin Group "Spec"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\7zAES.def
# End Source File
# Begin Source File
SOURCE=.\DllExports.cpp
# End Source File
# Begin Source File
SOURCE=.\resource.h
# End Source File
# Begin Source File
SOURCE=.\resource.rc
# End Source File
# Begin Source File
SOURCE=.\StdAfx.cpp
# ADD CPP /Yc"StdAfx.h"
# End Source File
# Begin Source File
SOURCE=.\StdAfx.h
# End Source File
# End Group
# Begin Group "Common"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\Common\NewHandler.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\NewHandler.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Vector.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Vector.h
# End Source File
# End Group
# Begin Group "7-Zip Common"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Common\StreamObjects.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\StreamObjects.h
# End Source File
# End Group
# Begin Group "Windows"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\Windows\DLL.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\DLL.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Synchronization.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Synchronization.h
# End Source File
# End Group
# Begin Source File
SOURCE=.\7zAES.cpp
!IF "$(CFG)" == "7zAES - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "7zAES - Win32 Debug"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\7zAES.h
# End Source File
# Begin Source File
SOURCE=.\SHA256.cpp
!IF "$(CFG)" == "7zAES - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "7zAES - Win32 Debug"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\SHA256.h
# End Source File
# End Target
# End Project

29
7zip/Crypto/7zAES/7zAES.dsw Executable file
View File

@@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "7zAES"=.\7zAES.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

139
7zip/Crypto/7zAES/7zAES.h Executable file
View File

@@ -0,0 +1,139 @@
// 7z_AES.h
#ifndef __CRYPTO_7Z_AES_H
#define __CRYPTO_7Z_AES_H
#include "Common/MyCom.h"
#include "Common/Types.h"
#include "Common/Buffer.h"
#include "Common/Vector.h"
#include "../../ICoder.h"
#include "../../IPassword.h"
#ifndef CRYPTO_AES
#include "../../Archive/Common/CoderLoader.h"
#endif
// {23170F69-40C1-278B-0601-810000000100}
DEFINE_GUID(CLSID_CCrypto_AES256_Encoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0x01, 0x81, 0x00, 0x00, 0x00, 0x01, 0x00);
// {23170F69-40C1-278B-0601-810000000000}
DEFINE_GUID(CLSID_CCrypto_AES256_Decoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0x01, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00);
namespace NCrypto {
namespace NSevenZ {
const int kKeySize = 32;
class CKeyInfo
{
public:
int NumCyclesPower;
UINT32 SaltSize;
BYTE Salt[16];
CByteBuffer Password;
BYTE Key[kKeySize];
bool IsEqualTo(const CKeyInfo &a) const;
void CalculateDigest();
CKeyInfo()
{
Init();
}
void Init()
{
NumCyclesPower = 0;
SaltSize = 0;
for (int i = 0; i < sizeof(Salt); i++)
Salt[i] = 0;
}
};
class CKeyInfoCache
{
int Size;
CObjectVector<CKeyInfo> Keys;
public:
CKeyInfoCache(int size): Size(size) {}
bool Find(CKeyInfo &key);
// HRESULT Calculate(CKeyInfo &key);
void Add(CKeyInfo &key);
};
class CBase
{
CKeyInfoCache _cachedKeys;
protected:
CKeyInfo _key;
BYTE _iv[16];
// int _ivSize;
void CalculateDigest();
CBase();
};
class CEncoder:
public ICompressCoder,
public ICompressSetDecoderProperties,
public ICryptoSetPassword,
public ICompressWriteCoderProperties,
public CMyUnknownImp,
public CBase
{
MY_UNKNOWN_IMP3(
ICryptoSetPassword,
ICompressSetDecoderProperties,
ICompressWriteCoderProperties
)
STDMETHOD(Code)(ISequentialInStream *inStream,
ISequentialOutStream *outStream, UINT64 const *inSize,
const UINT64 *outSize,ICompressProgressInfo *progress);
STDMETHOD(CryptoSetPassword)(const BYTE *aData, UINT32 aSize);
// ICompressSetDecoderProperties
STDMETHOD(SetDecoderProperties)(ISequentialInStream *inStream);
// ICompressWriteCoderProperties
STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
#ifndef CRYPTO_AES
CCoderLibrary _aesEncoderLibrary;
#endif
CMyComPtr<ICompressCoder2> _aesEncoder;
};
class CDecoder:
public ICompressCoder,
public ICompressSetDecoderProperties,
public ICryptoSetPassword,
public CMyUnknownImp,
public CBase
{
MY_UNKNOWN_IMP2(
ICryptoSetPassword,
ICompressSetDecoderProperties
)
STDMETHOD(Code)(ISequentialInStream *inStream,
ISequentialOutStream *outStream, UINT64 const *inSize,
const UINT64 *outSize,ICompressProgressInfo *progress);
STDMETHOD(CryptoSetPassword)(const BYTE *aData, UINT32 aSize);
// ICompressSetDecoderProperties
STDMETHOD(SetDecoderProperties)(ISequentialInStream *inStream);
#ifndef CRYPTO_AES
CCoderLibrary _aesDecoderLibrary;
#endif
CMyComPtr<ICompressCoder2> _aesDecoder;
};
}}
#endif

View File

@@ -0,0 +1,96 @@
// DLLExports.cpp
#include "StdAfx.h"
#define INITGUID
#include "Common/ComTry.h"
#include "7zAES.h"
/*
// {23170F69-40C1-278B-0703-000000000000}
DEFINE_GUID(CLSID_CCrypto_Hash_SHA256,
0x23170F69, 0x40C1, 0x278B, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
*/
// {23170F69-40C1-278B-06F1-070100000100}
DEFINE_GUID(CLSID_CCrypto7zAESEncoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0xF1, 0x07, 0x01, 0x00, 0x00, 0x01, 0x00);
// {23170F69-40C1-278B-06F1-070100000000}
DEFINE_GUID(CLSID_CCrypto7zAESDecoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0xF1, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00);
HINSTANCE g_hInstance;
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
if (dwReason == DLL_PROCESS_ATTACH)
g_hInstance = hInstance;
return TRUE;
}
STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject)
{
COM_TRY_BEGIN
*outObject = 0;
int correctInterface = (*iid == IID_ICompressCoder);
CMyComPtr<ICompressCoder> coder;
if (*clsid == CLSID_CCrypto7zAESDecoder)
{
if (!correctInterface)
return E_NOINTERFACE;
coder = (ICompressCoder *)new NCrypto::NSevenZ::CDecoder();
}
else if (*clsid == CLSID_CCrypto7zAESEncoder)
{
if (!correctInterface)
return E_NOINTERFACE;
coder = (ICompressCoder *)new NCrypto::NSevenZ::CEncoder();
}
else
return CLASS_E_CLASSNOTAVAILABLE;
*outObject = coder.Detach();
COM_TRY_END
return S_OK;
}
STDAPI GetNumberOfMethods(UINT32 *numMethods)
{
*numMethods = 1;
return S_OK;
}
STDAPI GetMethodProperty(UINT32 index, PROPID propID, PROPVARIANT *value)
{
if (index != 0)
return E_INVALIDARG;
::VariantClear((tagVARIANT *)value);
switch(propID)
{
case NMethodPropID::kID:
{
const char id[] = { 0x06, (char)0xF1, 0x07, 0x01 };
if ((value->bstrVal = ::SysAllocStringByteLen(id, sizeof(id))) != 0)
value->vt = VT_BSTR;
return S_OK;
}
case NMethodPropID::kName:
if ((value->bstrVal = ::SysAllocString(L"7zAES")) != 0)
value->vt = VT_BSTR;
return S_OK;
case NMethodPropID::kDecoder:
if ((value->bstrVal = ::SysAllocStringByteLen(
(const char *)&CLSID_CCrypto7zAESDecoder, sizeof(GUID))) != 0)
value->vt = VT_BSTR;
return S_OK;
case NMethodPropID::kEncoder:
if ((value->bstrVal = ::SysAllocStringByteLen(
(const char *)&CLSID_CCrypto7zAESEncoder, sizeof(GUID))) != 0)
value->vt = VT_BSTR;
return S_OK;
}
return S_OK;
}

189
7zip/Crypto/7zAES/SHA256.cpp Executable file
View File

@@ -0,0 +1,189 @@
// Crypto/HASH/SHA256/SHA256.cpp
// This code is based on code from Wei Dai's Crypto++ library.
#include "StdAfx.h"
#include "SHA256.h"
#include "Windows/Defs.h"
const int kBufferSize = 1 << 17;
namespace NCrypto {
namespace NSHA256 {
template <class T> static inline T rotrFixed(T x, unsigned int y)
{
// assert(y < sizeof(T)*8);
return (x>>y) | (x<<(sizeof(T)*8-y));
}
#define blk0(i) (W[i] = data[i])
void SHA256::Init()
{
m_digest[0] = 0x6a09e667;
m_digest[1] = 0xbb67ae85;
m_digest[2] = 0x3c6ef372;
m_digest[3] = 0xa54ff53a;
m_digest[4] = 0x510e527f;
m_digest[5] = 0x9b05688c;
m_digest[6] = 0x1f83d9ab;
m_digest[7] = 0x5be0cd19;
m_count = 0;
}
#define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
#define Ch(x,y,z) (z^(x&(y^z)))
#define Maj(x,y,z) ((x&y)|(z&(x|y)))
#define a(i) T[(0-i)&7]
#define b(i) T[(1-i)&7]
#define c(i) T[(2-i)&7]
#define d(i) T[(3-i)&7]
#define e(i) T[(4-i)&7]
#define f(i) T[(5-i)&7]
#define g(i) T[(6-i)&7]
#define h(i) T[(7-i)&7]
#define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\
d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
// for SHA256
#define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
#define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
#define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
#define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
void SHA256::Transform(UINT32 *state, const UINT32 *data)
{
UINT32 W[16];
UINT32 T[8];
/* Copy context->state[] to working vars */
// memcpy(T, state, sizeof(T));
for (int s = 0; s < 8; s++)
T[s] = state[s];
/* 64 operations, partially loop unrolled */
for (unsigned int j = 0; j < 64; j += 16)
{
for (unsigned int i = 0; i < 16; i++)
{
R(i);
}
/*
R( 0); R( 1); R( 2); R( 3);
R( 4); R( 5); R( 6); R( 7);
R( 8); R( 9); R(10); R(11);
R(12); R(13); R(14); R(15);
*/
}
/* Add the working vars back into context.state[] */
/*
state[0] += a(0);
state[1] += b(0);
state[2] += c(0);
state[3] += d(0);
state[4] += e(0);
state[5] += f(0);
state[6] += g(0);
state[7] += h(0);
*/
for (int i = 0; i < 8; i++)
state[i] += T[i];
/* Wipe variables */
// memset(W, 0, sizeof(W));
// memset(T, 0, sizeof(T));
}
const UINT32 SHA256::K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
#undef S0
#undef S1
#undef s0
#undef s1
void SHA256::WriteByteBlock()
{
UINT32 data32[16];
for (int i = 0; i < 16; i++)
{
data32[i] = (UINT32(_buffer[i * 4]) << 24) +
(UINT32(_buffer[i * 4 + 1]) << 16) +
(UINT32(_buffer[i * 4 + 2]) << 8) +
UINT32(_buffer[i * 4 + 3]);
}
Transform(m_digest, data32);
}
void SHA256::Update(const BYTE *data, UINT32 size)
{
UINT32 curBufferPos = UINT32(m_count) & 0x3F;
while (size > 0)
{
while(curBufferPos < 64 && size > 0)
{
_buffer[curBufferPos++] = *data++;
m_count++;
size--;
}
if (curBufferPos == 64)
{
curBufferPos = 0;
WriteByteBlock();
}
}
}
void SHA256::Final(BYTE *digest)
{
UINT64 lenInBits = (m_count << 3);
UINT32 curBufferPos = UINT32(m_count) & 0x3F;
_buffer[curBufferPos++] = 0x80;
while (curBufferPos != (64 - 8))
{
curBufferPos &= 0x3F;
if (curBufferPos == 0)
WriteByteBlock();
_buffer[curBufferPos++] = 0;
}
for (int i = 0; i < 8; i++)
{
_buffer[curBufferPos++] = BYTE(lenInBits >> 56);
lenInBits <<= 8;
}
WriteByteBlock();
for (int j = 0; j < 8; j++)
{
*digest++ = m_digest[j] >> 24;
*digest++ = m_digest[j] >> 16;
*digest++ = m_digest[j] >> 8;
*digest++ = m_digest[j];
}
Init();
}
}}

30
7zip/Crypto/7zAES/SHA256.h Executable file
View File

@@ -0,0 +1,30 @@
// Crypto/SHA/SHA256.h
#ifndef __CRYPTO_SHA256_H
#define __CRYPTO_SHA256_H
#include "Common/Types.h"
namespace NCrypto {
namespace NSHA256 {
class SHA256
{
static const UINT32 K[64];
UINT32 m_digest[8];
UINT64 m_count;
BYTE _buffer[64];
static void Transform(UINT32 *digest, const UINT32 *data);
void WriteByteBlock();
public:
enum {DIGESTSIZE = 32};
SHA256() { Init(); } ;
void Init();
void Update(const BYTE *data, UINT32 size);
void Final(BYTE *digest);
};
}}
#endif

3
7zip/Crypto/7zAES/StdAfx.cpp Executable file
View File

@@ -0,0 +1,3 @@
// StdAfx.cpp
#include "stdafx.h"

8
7zip/Crypto/7zAES/StdAfx.h Executable file
View File

@@ -0,0 +1,8 @@
// stdafx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include <windows.h>
#endif

15
7zip/Crypto/7zAES/resource.h Executable file
View File

@@ -0,0 +1,15 @@
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by resource.rc
//
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

121
7zip/Crypto/7zAES/resource.rc Executable file
View File

@@ -0,0 +1,121 @@
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Russian resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
#ifdef _WIN32
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#pragma code_page(1251)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // Russian resources
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifndef _MAC
/////////////////////////////////////////////////////////////////////////////
//
// Version
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,9,2,0
PRODUCTVERSION 3,9,2,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "Comments", "\0"
VALUE "CompanyName", "Igor Pavlov\0"
VALUE "FileDescription", "7z-AES Crypto\0"
VALUE "FileVersion", "3, 9, 2, 0\0"
VALUE "InternalName", "7zAES\0"
VALUE "LegalCopyright", "Copyright (C) 1999-2003 Igor Pavlov\0"
VALUE "LegalTrademarks", "\0"
VALUE "OriginalFilename", "7zAES.dll\0"
VALUE "PrivateBuild", "\0"
VALUE "ProductName", "7-Zip\0"
VALUE "ProductVersion", "3, 9, 2, 0\0"
VALUE "SpecialBuild", "\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
#endif // !_MAC
#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED