mirror of
https://github.com/aaru-dps/Aaru.Compression.Native.git
synced 2025-12-16 19:24:31 +00:00
Move placement of lzma.
This commit is contained in:
317
3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp
vendored
Normal file
317
3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp
vendored
Normal file
@@ -0,0 +1,317 @@
|
||||
// 7zAes.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "../../../C/CpuArch.h"
|
||||
#include "../../../C/Sha256.h"
|
||||
|
||||
#include "../../Common/ComTry.h"
|
||||
#include "../../Common/MyBuffer2.h"
|
||||
|
||||
#ifndef _7ZIP_ST
|
||||
#include "../../Windows/Synchronization.h"
|
||||
#endif
|
||||
|
||||
#include "../Common/StreamUtils.h"
|
||||
|
||||
#include "7zAes.h"
|
||||
#include "MyAes.h"
|
||||
|
||||
#ifndef EXTRACT_ONLY
|
||||
#include "RandGen.h"
|
||||
#endif
|
||||
|
||||
namespace NCrypto {
|
||||
namespace N7z {
|
||||
|
||||
static const unsigned k_NumCyclesPower_Supported_MAX = 24;
|
||||
|
||||
bool CKeyInfo::IsEqualTo(const CKeyInfo &a) const
|
||||
{
|
||||
if (SaltSize != a.SaltSize || NumCyclesPower != a.NumCyclesPower)
|
||||
return false;
|
||||
for (unsigned i = 0; i < SaltSize; i++)
|
||||
if (Salt[i] != a.Salt[i])
|
||||
return false;
|
||||
return (Password == a.Password);
|
||||
}
|
||||
|
||||
void CKeyInfo::CalcKey()
|
||||
{
|
||||
if (NumCyclesPower == 0x3F)
|
||||
{
|
||||
unsigned pos;
|
||||
for (pos = 0; pos < SaltSize; pos++)
|
||||
Key[pos] = Salt[pos];
|
||||
for (unsigned i = 0; i < Password.Size() && pos < kKeySize; i++)
|
||||
Key[pos++] = Password[i];
|
||||
for (; pos < kKeySize; pos++)
|
||||
Key[pos] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
const unsigned kUnrPow = 6;
|
||||
const UInt32 numUnroll = (UInt32)1 << (NumCyclesPower <= kUnrPow ? (unsigned)NumCyclesPower : kUnrPow);
|
||||
|
||||
const size_t bufSize = 8 + SaltSize + Password.Size();
|
||||
const size_t unrollSize = bufSize * numUnroll;
|
||||
|
||||
// MY_ALIGN (16)
|
||||
// CSha256 sha;
|
||||
CAlignedBuffer sha(sizeof(CSha256) + unrollSize + bufSize * 2);
|
||||
Byte *buf = sha + sizeof(CSha256);
|
||||
|
||||
memcpy(buf, Salt, SaltSize);
|
||||
memcpy(buf + SaltSize, Password, Password.Size());
|
||||
memset(buf + bufSize - 8, 0, 8);
|
||||
|
||||
Sha256_Init((CSha256 *)(void *)(Byte *)sha);
|
||||
|
||||
{
|
||||
{
|
||||
Byte *dest = buf;
|
||||
for (UInt32 i = 1; i < numUnroll; i++)
|
||||
{
|
||||
dest += bufSize;
|
||||
memcpy(dest, buf, bufSize);
|
||||
}
|
||||
}
|
||||
|
||||
const UInt32 numRounds = (UInt32)1 << NumCyclesPower;
|
||||
UInt32 r = 0;
|
||||
do
|
||||
{
|
||||
Byte *dest = buf + bufSize - 8;
|
||||
UInt32 i = r;
|
||||
r += numUnroll;
|
||||
do
|
||||
{
|
||||
SetUi32(dest, i); i++; dest += bufSize;
|
||||
// SetUi32(dest, i); i++; dest += bufSize;
|
||||
}
|
||||
while (i < r);
|
||||
Sha256_Update((CSha256 *)(void *)(Byte *)sha, buf, unrollSize);
|
||||
}
|
||||
while (r < numRounds);
|
||||
}
|
||||
/*
|
||||
UInt64 numRounds = (UInt64)1 << NumCyclesPower;
|
||||
|
||||
do
|
||||
{
|
||||
Sha256_Update((CSha256 *)(Byte *)sha, buf, bufSize);
|
||||
for (unsigned i = 0; i < 8; i++)
|
||||
if (++(ctr[i]) != 0)
|
||||
break;
|
||||
}
|
||||
while (--numRounds != 0);
|
||||
*/
|
||||
|
||||
Sha256_Final((CSha256 *)(void *)(Byte *)sha, Key);
|
||||
memset(sha, 0, sha.Size());
|
||||
}
|
||||
}
|
||||
|
||||
bool CKeyInfoCache::GetKey(CKeyInfo &key)
|
||||
{
|
||||
FOR_VECTOR (i, Keys)
|
||||
{
|
||||
const CKeyInfo &cached = Keys[i];
|
||||
if (key.IsEqualTo(cached))
|
||||
{
|
||||
for (unsigned j = 0; j < kKeySize; j++)
|
||||
key.Key[j] = cached.Key[j];
|
||||
if (i != 0)
|
||||
Keys.MoveToFront(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CKeyInfoCache::FindAndAdd(const CKeyInfo &key)
|
||||
{
|
||||
FOR_VECTOR (i, Keys)
|
||||
{
|
||||
const CKeyInfo &cached = Keys[i];
|
||||
if (key.IsEqualTo(cached))
|
||||
{
|
||||
if (i != 0)
|
||||
Keys.MoveToFront(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Add(key);
|
||||
}
|
||||
|
||||
void CKeyInfoCache::Add(const CKeyInfo &key)
|
||||
{
|
||||
if (Keys.Size() >= Size)
|
||||
Keys.DeleteBack();
|
||||
Keys.Insert(0, key);
|
||||
}
|
||||
|
||||
static CKeyInfoCache g_GlobalKeyCache(32);
|
||||
|
||||
#ifndef _7ZIP_ST
|
||||
static NWindows::NSynchronization::CCriticalSection g_GlobalKeyCacheCriticalSection;
|
||||
#define MT_LOCK NWindows::NSynchronization::CCriticalSectionLock lock(g_GlobalKeyCacheCriticalSection);
|
||||
#else
|
||||
#define MT_LOCK
|
||||
#endif
|
||||
|
||||
CBase::CBase():
|
||||
_cachedKeys(16),
|
||||
_ivSize(0)
|
||||
{
|
||||
for (unsigned i = 0; i < sizeof(_iv); i++)
|
||||
_iv[i] = 0;
|
||||
}
|
||||
|
||||
void CBase::PrepareKey()
|
||||
{
|
||||
// BCJ2 threads use same password. So we use long lock.
|
||||
MT_LOCK
|
||||
|
||||
bool finded = false;
|
||||
if (!_cachedKeys.GetKey(_key))
|
||||
{
|
||||
finded = g_GlobalKeyCache.GetKey(_key);
|
||||
if (!finded)
|
||||
_key.CalcKey();
|
||||
_cachedKeys.Add(_key);
|
||||
}
|
||||
if (!finded)
|
||||
g_GlobalKeyCache.FindAndAdd(_key);
|
||||
}
|
||||
|
||||
#ifndef EXTRACT_ONLY
|
||||
|
||||
/*
|
||||
STDMETHODIMP CEncoder::ResetSalt()
|
||||
{
|
||||
_key.SaltSize = 4;
|
||||
g_RandomGenerator.Generate(_key.Salt, _key.SaltSize);
|
||||
return S_OK;
|
||||
}
|
||||
*/
|
||||
|
||||
STDMETHODIMP CEncoder::ResetInitVector()
|
||||
{
|
||||
for (unsigned i = 0; i < sizeof(_iv); i++)
|
||||
_iv[i] = 0;
|
||||
_ivSize = 16;
|
||||
MY_RAND_GEN(_iv, _ivSize);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
|
||||
{
|
||||
Byte props[2 + sizeof(_key.Salt) + sizeof(_iv)];
|
||||
unsigned propsSize = 1;
|
||||
|
||||
props[0] = (Byte)(_key.NumCyclesPower
|
||||
| (_key.SaltSize == 0 ? 0 : (1 << 7))
|
||||
| (_ivSize == 0 ? 0 : (1 << 6)));
|
||||
|
||||
if (_key.SaltSize != 0 || _ivSize != 0)
|
||||
{
|
||||
props[1] = (Byte)(
|
||||
((_key.SaltSize == 0 ? 0 : _key.SaltSize - 1) << 4)
|
||||
| (_ivSize == 0 ? 0 : _ivSize - 1));
|
||||
memcpy(props + 2, _key.Salt, _key.SaltSize);
|
||||
propsSize = 2 + _key.SaltSize;
|
||||
memcpy(props + propsSize, _iv, _ivSize);
|
||||
propsSize += _ivSize;
|
||||
}
|
||||
|
||||
return WriteStream(outStream, props, propsSize);
|
||||
}
|
||||
|
||||
CEncoder::CEncoder()
|
||||
{
|
||||
// _key.SaltSize = 4; g_RandomGenerator.Generate(_key.Salt, _key.SaltSize);
|
||||
// _key.NumCyclesPower = 0x3F;
|
||||
_key.NumCyclesPower = 19;
|
||||
_aesFilter = new CAesCbcEncoder(kKeySize);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
CDecoder::CDecoder()
|
||||
{
|
||||
_aesFilter = new CAesCbcDecoder(kKeySize);
|
||||
}
|
||||
|
||||
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
|
||||
{
|
||||
_key.ClearProps();
|
||||
|
||||
_ivSize = 0;
|
||||
unsigned i;
|
||||
for (i = 0; i < sizeof(_iv); i++)
|
||||
_iv[i] = 0;
|
||||
|
||||
if (size == 0)
|
||||
return S_OK;
|
||||
|
||||
Byte b0 = data[0];
|
||||
|
||||
_key.NumCyclesPower = b0 & 0x3F;
|
||||
if ((b0 & 0xC0) == 0)
|
||||
return size == 1 ? S_OK : E_INVALIDARG;
|
||||
|
||||
if (size <= 1)
|
||||
return E_INVALIDARG;
|
||||
|
||||
Byte b1 = data[1];
|
||||
|
||||
unsigned saltSize = ((b0 >> 7) & 1) + (b1 >> 4);
|
||||
unsigned ivSize = ((b0 >> 6) & 1) + (b1 & 0x0F);
|
||||
|
||||
if (size != 2 + saltSize + ivSize)
|
||||
return E_INVALIDARG;
|
||||
_key.SaltSize = saltSize;
|
||||
data += 2;
|
||||
for (i = 0; i < saltSize; i++)
|
||||
_key.Salt[i] = *data++;
|
||||
for (i = 0; i < ivSize; i++)
|
||||
_iv[i] = *data++;
|
||||
return (_key.NumCyclesPower <= k_NumCyclesPower_Supported_MAX
|
||||
|| _key.NumCyclesPower == 0x3F) ? S_OK : E_NOTIMPL;
|
||||
}
|
||||
|
||||
|
||||
STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size)
|
||||
{
|
||||
COM_TRY_BEGIN
|
||||
|
||||
_key.Password.Wipe();
|
||||
_key.Password.CopyFrom(data, (size_t)size);
|
||||
return S_OK;
|
||||
|
||||
COM_TRY_END
|
||||
}
|
||||
|
||||
STDMETHODIMP CBaseCoder::Init()
|
||||
{
|
||||
COM_TRY_BEGIN
|
||||
|
||||
PrepareKey();
|
||||
CMyComPtr<ICryptoProperties> cp;
|
||||
RINOK(_aesFilter.QueryInterface(IID_ICryptoProperties, &cp));
|
||||
if (!cp)
|
||||
return E_FAIL;
|
||||
RINOK(cp->SetKey(_key.Key, kKeySize));
|
||||
RINOK(cp->SetInitVector(_iv, sizeof(_iv)));
|
||||
return _aesFilter->Init();
|
||||
|
||||
COM_TRY_END
|
||||
}
|
||||
|
||||
STDMETHODIMP_(UInt32) CBaseCoder::Filter(Byte *data, UInt32 size)
|
||||
{
|
||||
return _aesFilter->Filter(data, size);
|
||||
}
|
||||
|
||||
}}
|
||||
129
3rdparty/lzma/CPP/7zip/Crypto/7zAes.h
vendored
Normal file
129
3rdparty/lzma/CPP/7zip/Crypto/7zAes.h
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
// 7zAes.h
|
||||
|
||||
#ifndef __CRYPTO_7Z_AES_H
|
||||
#define __CRYPTO_7Z_AES_H
|
||||
|
||||
#include "../../Common/MyBuffer.h"
|
||||
#include "../../Common/MyCom.h"
|
||||
#include "../../Common/MyVector.h"
|
||||
|
||||
#include "../ICoder.h"
|
||||
#include "../IPassword.h"
|
||||
|
||||
namespace NCrypto {
|
||||
namespace N7z {
|
||||
|
||||
const unsigned kKeySize = 32;
|
||||
const unsigned kSaltSizeMax = 16;
|
||||
const unsigned kIvSizeMax = 16; // AES_BLOCK_SIZE;
|
||||
|
||||
class CKeyInfo
|
||||
{
|
||||
public:
|
||||
unsigned NumCyclesPower;
|
||||
unsigned SaltSize;
|
||||
Byte Salt[kSaltSizeMax];
|
||||
CByteBuffer Password;
|
||||
Byte Key[kKeySize];
|
||||
|
||||
bool IsEqualTo(const CKeyInfo &a) const;
|
||||
void CalcKey();
|
||||
|
||||
CKeyInfo() { ClearProps(); }
|
||||
void ClearProps()
|
||||
{
|
||||
NumCyclesPower = 0;
|
||||
SaltSize = 0;
|
||||
for (unsigned i = 0; i < sizeof(Salt); i++)
|
||||
Salt[i] = 0;
|
||||
}
|
||||
|
||||
void Wipe()
|
||||
{
|
||||
Password.Wipe();
|
||||
NumCyclesPower = 0;
|
||||
SaltSize = 0;
|
||||
MY_memset_0_ARRAY(Salt);
|
||||
MY_memset_0_ARRAY(Key);
|
||||
}
|
||||
|
||||
~CKeyInfo() { Wipe(); }
|
||||
};
|
||||
|
||||
class CKeyInfoCache
|
||||
{
|
||||
unsigned Size;
|
||||
CObjectVector<CKeyInfo> Keys;
|
||||
public:
|
||||
CKeyInfoCache(unsigned size): Size(size) {}
|
||||
bool GetKey(CKeyInfo &key);
|
||||
void Add(const CKeyInfo &key);
|
||||
void FindAndAdd(const CKeyInfo &key);
|
||||
};
|
||||
|
||||
class CBase
|
||||
{
|
||||
CKeyInfoCache _cachedKeys;
|
||||
protected:
|
||||
CKeyInfo _key;
|
||||
Byte _iv[kIvSizeMax];
|
||||
unsigned _ivSize;
|
||||
|
||||
void PrepareKey();
|
||||
CBase();
|
||||
};
|
||||
|
||||
class CBaseCoder:
|
||||
public ICompressFilter,
|
||||
public ICryptoSetPassword,
|
||||
public CMyUnknownImp,
|
||||
public CBase
|
||||
{
|
||||
protected:
|
||||
CMyComPtr<ICompressFilter> _aesFilter;
|
||||
|
||||
public:
|
||||
INTERFACE_ICompressFilter(;)
|
||||
|
||||
STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
|
||||
};
|
||||
|
||||
#ifndef EXTRACT_ONLY
|
||||
|
||||
class CEncoder:
|
||||
public CBaseCoder,
|
||||
public ICompressWriteCoderProperties,
|
||||
// public ICryptoResetSalt,
|
||||
public ICryptoResetInitVector
|
||||
{
|
||||
public:
|
||||
MY_UNKNOWN_IMP4(
|
||||
ICompressFilter,
|
||||
ICryptoSetPassword,
|
||||
ICompressWriteCoderProperties,
|
||||
// ICryptoResetSalt,
|
||||
ICryptoResetInitVector)
|
||||
STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
|
||||
// STDMETHOD(ResetSalt)();
|
||||
STDMETHOD(ResetInitVector)();
|
||||
CEncoder();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
class CDecoder:
|
||||
public CBaseCoder,
|
||||
public ICompressSetDecoderProperties2
|
||||
{
|
||||
public:
|
||||
MY_UNKNOWN_IMP3(
|
||||
ICompressFilter,
|
||||
ICryptoSetPassword,
|
||||
ICompressSetDecoderProperties2)
|
||||
STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
|
||||
CDecoder();
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
17
3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp
vendored
Normal file
17
3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
// 7zAesRegister.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "../Common/RegisterCodec.h"
|
||||
|
||||
#include "7zAes.h"
|
||||
|
||||
namespace NCrypto {
|
||||
namespace N7z {
|
||||
|
||||
REGISTER_FILTER_E(_7zAES,
|
||||
CDecoder,
|
||||
CEncoder,
|
||||
0x6F10701, "7zAES")
|
||||
|
||||
}}
|
||||
205
3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp
vendored
Normal file
205
3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp
vendored
Normal file
@@ -0,0 +1,205 @@
|
||||
// Crypto/MyAes.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "../../../C/CpuArch.h"
|
||||
|
||||
#include "MyAes.h"
|
||||
|
||||
namespace NCrypto {
|
||||
|
||||
static struct CAesTabInit { CAesTabInit() { AesGenTables();} } g_AesTabInit;
|
||||
|
||||
CAesCoder::CAesCoder(bool encodeMode, unsigned keySize, bool ctrMode):
|
||||
_keySize(keySize),
|
||||
_keyIsSet(false),
|
||||
_encodeMode(encodeMode),
|
||||
_ctrMode(ctrMode),
|
||||
_aes(AES_NUM_IVMRK_WORDS * 4 + AES_BLOCK_SIZE * 2)
|
||||
{
|
||||
// _offset = ((0 - (unsigned)(ptrdiff_t)_aes) & 0xF) / sizeof(UInt32);
|
||||
memset(_iv, 0, AES_BLOCK_SIZE);
|
||||
/*
|
||||
// we can use the following code to test 32-bit overflow case for AES-CTR
|
||||
for (unsigned i = 0; i < 16; i++) _iv[i] = (Byte)(i + 1);
|
||||
_iv[0] = 0xFE; _iv[1] = _iv[2] = _iv[3] = 0xFF;
|
||||
*/
|
||||
SetFunctions(0);
|
||||
}
|
||||
|
||||
STDMETHODIMP CAesCoder::Init()
|
||||
{
|
||||
AesCbc_Init(Aes(), _iv);
|
||||
return _keyIsSet ? S_OK : E_NOTIMPL; // E_FAIL
|
||||
}
|
||||
|
||||
STDMETHODIMP_(UInt32) CAesCoder::Filter(Byte *data, UInt32 size)
|
||||
{
|
||||
if (!_keyIsSet)
|
||||
return 0;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
if (size < AES_BLOCK_SIZE)
|
||||
{
|
||||
#ifndef _SFX
|
||||
if (_ctrMode)
|
||||
{
|
||||
// use that code only for last block !!!
|
||||
Byte *ctr = (Byte *)(Aes() + AES_NUM_IVMRK_WORDS);
|
||||
memset(ctr, 0, AES_BLOCK_SIZE);
|
||||
memcpy(ctr, data, size);
|
||||
_codeFunc(Aes(), ctr, 1);
|
||||
memcpy(data, ctr, size);
|
||||
return size;
|
||||
}
|
||||
#endif
|
||||
return AES_BLOCK_SIZE;
|
||||
}
|
||||
size >>= 4;
|
||||
_codeFunc(Aes(), data, size);
|
||||
return size << 4;
|
||||
}
|
||||
|
||||
STDMETHODIMP CAesCoder::SetKey(const Byte *data, UInt32 size)
|
||||
{
|
||||
if ((size & 0x7) != 0 || size < 16 || size > 32)
|
||||
return E_INVALIDARG;
|
||||
if (_keySize != 0 && size != _keySize)
|
||||
return E_INVALIDARG;
|
||||
AES_SET_KEY_FUNC setKeyFunc = (_ctrMode | _encodeMode) ? Aes_SetKey_Enc : Aes_SetKey_Dec;
|
||||
setKeyFunc(Aes() + 4, data, size);
|
||||
_keyIsSet = true;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CAesCoder::SetInitVector(const Byte *data, UInt32 size)
|
||||
{
|
||||
if (size != AES_BLOCK_SIZE)
|
||||
return E_INVALIDARG;
|
||||
memcpy(_iv, data, size);
|
||||
CAesCoder::Init(); // don't call virtual function here !!!
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
#ifndef _SFX
|
||||
|
||||
#ifdef MY_CPU_X86_OR_AMD64
|
||||
#define USE_HW_AES
|
||||
#elif defined(MY_CPU_ARM_OR_ARM64) && defined(MY_CPU_LE)
|
||||
#if defined(__clang__)
|
||||
#if (__clang_major__ >= 8) // fix that check
|
||||
#define USE_HW_AES
|
||||
#endif
|
||||
#elif defined(__GNUC__)
|
||||
#if (__GNUC__ >= 6) // fix that check
|
||||
#define USE_HW_AES
|
||||
#endif
|
||||
#elif defined(_MSC_VER)
|
||||
#if _MSC_VER >= 1910
|
||||
#define USE_HW_AES
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
bool CAesCoder::SetFunctions(UInt32
|
||||
#ifndef _SFX
|
||||
algo
|
||||
#endif
|
||||
)
|
||||
{
|
||||
_codeFunc = g_AesCbc_Decode;
|
||||
|
||||
#ifdef _SFX
|
||||
|
||||
return true;
|
||||
|
||||
#else
|
||||
|
||||
if (_ctrMode)
|
||||
_codeFunc = g_AesCtr_Code;
|
||||
else if (_encodeMode)
|
||||
_codeFunc = g_AesCbc_Encode;
|
||||
|
||||
if (algo < 1)
|
||||
return true;
|
||||
|
||||
if (algo == 1)
|
||||
{
|
||||
_codeFunc = AesCbc_Decode;
|
||||
|
||||
#ifndef _SFX
|
||||
if (_ctrMode)
|
||||
_codeFunc = AesCtr_Code;
|
||||
else if (_encodeMode)
|
||||
_codeFunc = AesCbc_Encode;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef USE_HW_AES
|
||||
// if (CPU_IsSupported_AES())
|
||||
{
|
||||
if (algo == 2)
|
||||
if (g_Aes_SupportedFunctions_Flags & k_Aes_SupportedFunctions_HW)
|
||||
{
|
||||
_codeFunc = AesCbc_Decode_HW;
|
||||
#ifndef _SFX
|
||||
if (_ctrMode)
|
||||
_codeFunc = AesCtr_Code_HW;
|
||||
else if (_encodeMode)
|
||||
_codeFunc = AesCbc_Encode_HW;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(MY_CPU_X86_OR_AMD64)
|
||||
if (algo == 3)
|
||||
if (g_Aes_SupportedFunctions_Flags & k_Aes_SupportedFunctions_HW_256)
|
||||
{
|
||||
_codeFunc = AesCbc_Decode_HW_256;
|
||||
#ifndef _SFX
|
||||
if (_ctrMode)
|
||||
_codeFunc = AesCtr_Code_HW_256;
|
||||
else if (_encodeMode)
|
||||
_codeFunc = AesCbc_Encode_HW;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifndef _SFX
|
||||
|
||||
STDMETHODIMP CAesCoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps)
|
||||
{
|
||||
UInt32 algo = 0;
|
||||
for (UInt32 i = 0; i < numProps; i++)
|
||||
{
|
||||
const PROPVARIANT &prop = coderProps[i];
|
||||
if (propIDs[i] == NCoderPropID::kDefaultProp)
|
||||
{
|
||||
if (prop.vt != VT_UI4)
|
||||
return E_INVALIDARG;
|
||||
if (prop.ulVal > 3)
|
||||
return E_NOTIMPL;
|
||||
algo = prop.ulVal;
|
||||
}
|
||||
}
|
||||
if (!SetFunctions(algo))
|
||||
return E_NOTIMPL;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
79
3rdparty/lzma/CPP/7zip/Crypto/MyAes.h
vendored
Normal file
79
3rdparty/lzma/CPP/7zip/Crypto/MyAes.h
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
// Crypto/MyAes.h
|
||||
|
||||
#ifndef __CRYPTO_MY_AES_H
|
||||
#define __CRYPTO_MY_AES_H
|
||||
|
||||
#include "../../../C/Aes.h"
|
||||
|
||||
#include "../../Common/MyBuffer2.h"
|
||||
#include "../../Common/MyCom.h"
|
||||
|
||||
#include "../ICoder.h"
|
||||
|
||||
namespace NCrypto {
|
||||
|
||||
class CAesCoder:
|
||||
public ICompressFilter,
|
||||
public ICryptoProperties,
|
||||
#ifndef _SFX
|
||||
public ICompressSetCoderProperties,
|
||||
#endif
|
||||
public CMyUnknownImp
|
||||
{
|
||||
AES_CODE_FUNC _codeFunc;
|
||||
// unsigned _offset;
|
||||
unsigned _keySize;
|
||||
bool _keyIsSet;
|
||||
bool _encodeMode;
|
||||
bool _ctrMode;
|
||||
|
||||
// UInt32 _aes[AES_NUM_IVMRK_WORDS + 3];
|
||||
CAlignedBuffer _aes;
|
||||
|
||||
Byte _iv[AES_BLOCK_SIZE];
|
||||
|
||||
// UInt32 *Aes() { return _aes + _offset; }
|
||||
UInt32 *Aes() { return (UInt32 *)(void *)(Byte *)_aes; }
|
||||
|
||||
bool SetFunctions(UInt32 algo);
|
||||
|
||||
public:
|
||||
CAesCoder(bool encodeMode, unsigned keySize, bool ctrMode);
|
||||
|
||||
virtual ~CAesCoder() {}; // we need virtual destructor for derived classes
|
||||
|
||||
MY_QUERYINTERFACE_BEGIN2(ICompressFilter)
|
||||
MY_QUERYINTERFACE_ENTRY(ICryptoProperties)
|
||||
#ifndef _SFX
|
||||
MY_QUERYINTERFACE_ENTRY(ICompressSetCoderProperties)
|
||||
#endif
|
||||
MY_QUERYINTERFACE_END
|
||||
MY_ADDREF_RELEASE
|
||||
|
||||
INTERFACE_ICompressFilter(;)
|
||||
|
||||
void SetKeySize(unsigned size) { _keySize = size; }
|
||||
|
||||
STDMETHOD(SetKey)(const Byte *data, UInt32 size);
|
||||
STDMETHOD(SetInitVector)(const Byte *data, UInt32 size);
|
||||
|
||||
#ifndef _SFX
|
||||
STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifndef _SFX
|
||||
struct CAesCbcEncoder: public CAesCoder
|
||||
{
|
||||
CAesCbcEncoder(unsigned keySize = 0): CAesCoder(true, keySize, false) {}
|
||||
};
|
||||
#endif
|
||||
|
||||
struct CAesCbcDecoder: public CAesCoder
|
||||
{
|
||||
CAesCbcDecoder(unsigned keySize = 0): CAesCoder(false, keySize, false) {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
30
3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp
vendored
Normal file
30
3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// MyAesReg.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "../Common/RegisterCodec.h"
|
||||
|
||||
#include "MyAes.h"
|
||||
|
||||
namespace NCrypto {
|
||||
|
||||
#ifndef _SFX
|
||||
|
||||
#define REGISTER_AES_2(name, nameString, keySize, isCtr) \
|
||||
REGISTER_FILTER_E(name, \
|
||||
CAesCoder(false, keySize, isCtr), \
|
||||
CAesCoder(true , keySize, isCtr), \
|
||||
0x6F00100 | ((keySize - 16) * 8) | (isCtr ? 4 : 1), \
|
||||
nameString) \
|
||||
|
||||
#define REGISTER_AES(name, nameString, isCtr) \
|
||||
/* REGISTER_AES_2(AES128 ## name, "AES128" nameString, 16, isCtr) */ \
|
||||
/* REGISTER_AES_2(AES192 ## name, "AES192" nameString, 24, isCtr) */ \
|
||||
REGISTER_AES_2(AES256 ## name, "AES256" nameString, 32, isCtr) \
|
||||
|
||||
REGISTER_AES(CBC, "CBC", false)
|
||||
// REGISTER_AES(CTR, "CTR", true)
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
237
3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp
vendored
Normal file
237
3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp
vendored
Normal file
@@ -0,0 +1,237 @@
|
||||
// RandGen.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "RandGen.h"
|
||||
|
||||
#ifndef USE_STATIC_SYSTEM_RAND
|
||||
|
||||
#ifndef _7ZIP_ST
|
||||
#include "../../Windows/Synchronization.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#ifdef _WIN64
|
||||
#define USE_STATIC_RtlGenRandom
|
||||
#endif
|
||||
|
||||
#ifdef USE_STATIC_RtlGenRandom
|
||||
|
||||
// #include <NTSecAPI.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
#ifndef RtlGenRandom
|
||||
#define RtlGenRandom SystemFunction036
|
||||
BOOLEAN WINAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
|
||||
#endif
|
||||
EXTERN_C_END
|
||||
|
||||
#else
|
||||
EXTERN_C_BEGIN
|
||||
typedef BOOLEAN (WINAPI * Func_RtlGenRandom)(PVOID RandomBuffer, ULONG RandomBufferLength);
|
||||
EXTERN_C_END
|
||||
#endif
|
||||
|
||||
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#define USE_POSIX_TIME
|
||||
#define USE_POSIX_TIME2
|
||||
#endif
|
||||
|
||||
#ifdef USE_POSIX_TIME
|
||||
#include <time.h>
|
||||
#ifdef USE_POSIX_TIME2
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// The seed and first generated data block depend from processID,
|
||||
// theadID, timer and system random generator, if available.
|
||||
// Other generated data blocks depend from previous state
|
||||
|
||||
#define HASH_UPD(x) Sha256_Update(&hash, (const Byte *)&x, sizeof(x));
|
||||
|
||||
void CRandomGenerator::Init()
|
||||
{
|
||||
MY_ALIGN (16)
|
||||
CSha256 hash;
|
||||
Sha256_Init(&hash);
|
||||
|
||||
unsigned numIterations = 1000;
|
||||
|
||||
{
|
||||
#ifndef UNDER_CE
|
||||
const unsigned kNumIterations_Small = 100;
|
||||
const unsigned kBufSize = 32;
|
||||
MY_ALIGN (16)
|
||||
Byte buf[kBufSize];
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
DWORD w = ::GetCurrentProcessId();
|
||||
HASH_UPD(w);
|
||||
w = ::GetCurrentThreadId();
|
||||
HASH_UPD(w);
|
||||
|
||||
#ifdef UNDER_CE
|
||||
/*
|
||||
if (CeGenRandom(kBufSize, buf))
|
||||
{
|
||||
numIterations = kNumIterations_Small;
|
||||
Sha256_Update(&hash, buf, kBufSize);
|
||||
}
|
||||
*/
|
||||
#elif defined(USE_STATIC_RtlGenRandom)
|
||||
if (RtlGenRandom(buf, kBufSize))
|
||||
{
|
||||
numIterations = kNumIterations_Small;
|
||||
Sha256_Update(&hash, buf, kBufSize);
|
||||
}
|
||||
#else
|
||||
{
|
||||
HMODULE hModule = ::LoadLibrary(TEXT("Advapi32.dll"));
|
||||
if (hModule)
|
||||
{
|
||||
// SystemFunction036() is real name of RtlGenRandom() function
|
||||
Func_RtlGenRandom my_RtlGenRandom = (Func_RtlGenRandom)(void *)GetProcAddress(hModule, "SystemFunction036");
|
||||
if (my_RtlGenRandom)
|
||||
{
|
||||
if (my_RtlGenRandom(buf, kBufSize))
|
||||
{
|
||||
numIterations = kNumIterations_Small;
|
||||
Sha256_Update(&hash, buf, kBufSize);
|
||||
}
|
||||
}
|
||||
::FreeLibrary(hModule);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
pid_t pid = getpid();
|
||||
HASH_UPD(pid);
|
||||
pid = getppid();
|
||||
HASH_UPD(pid);
|
||||
|
||||
{
|
||||
int f = open("/dev/urandom", O_RDONLY);
|
||||
unsigned numBytes = kBufSize;
|
||||
if (f >= 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
ssize_t n = read(f, buf, numBytes);
|
||||
if (n <= 0)
|
||||
break;
|
||||
Sha256_Update(&hash, buf, (size_t)n);
|
||||
numBytes -= (unsigned)n;
|
||||
}
|
||||
while (numBytes);
|
||||
close(f);
|
||||
if (numBytes == 0)
|
||||
numIterations = kNumIterations_Small;
|
||||
}
|
||||
}
|
||||
/*
|
||||
{
|
||||
int n = getrandom(buf, kBufSize, 0);
|
||||
if (n > 0)
|
||||
{
|
||||
Sha256_Update(&hash, buf, n);
|
||||
if (n == kBufSize)
|
||||
numIterations = kNumIterations_Small;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
numIterations = 2;
|
||||
#endif
|
||||
|
||||
do
|
||||
{
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER v;
|
||||
if (::QueryPerformanceCounter(&v))
|
||||
HASH_UPD(v.QuadPart);
|
||||
#endif
|
||||
|
||||
#ifdef USE_POSIX_TIME
|
||||
#ifdef USE_POSIX_TIME2
|
||||
timeval v;
|
||||
if (gettimeofday(&v, 0) == 0)
|
||||
{
|
||||
HASH_UPD(v.tv_sec);
|
||||
HASH_UPD(v.tv_usec);
|
||||
}
|
||||
#endif
|
||||
time_t v2 = time(NULL);
|
||||
HASH_UPD(v2);
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD tickCount = ::GetTickCount();
|
||||
HASH_UPD(tickCount);
|
||||
#endif
|
||||
|
||||
for (unsigned j = 0; j < 100; j++)
|
||||
{
|
||||
Sha256_Final(&hash, _buff);
|
||||
Sha256_Init(&hash);
|
||||
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
|
||||
}
|
||||
}
|
||||
while (--numIterations);
|
||||
|
||||
Sha256_Final(&hash, _buff);
|
||||
_needInit = false;
|
||||
}
|
||||
|
||||
#ifndef _7ZIP_ST
|
||||
static NWindows::NSynchronization::CCriticalSection g_CriticalSection;
|
||||
#define MT_LOCK NWindows::NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
|
||||
#else
|
||||
#define MT_LOCK
|
||||
#endif
|
||||
|
||||
void CRandomGenerator::Generate(Byte *data, unsigned size)
|
||||
{
|
||||
MT_LOCK
|
||||
|
||||
if (_needInit)
|
||||
Init();
|
||||
while (size != 0)
|
||||
{
|
||||
MY_ALIGN (16)
|
||||
CSha256 hash;
|
||||
|
||||
Sha256_Init(&hash);
|
||||
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
|
||||
Sha256_Final(&hash, _buff);
|
||||
|
||||
Sha256_Init(&hash);
|
||||
UInt32 salt = 0xF672ABD1;
|
||||
HASH_UPD(salt);
|
||||
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
|
||||
MY_ALIGN (16)
|
||||
Byte buff[SHA256_DIGEST_SIZE];
|
||||
Sha256_Final(&hash, buff);
|
||||
for (unsigned i = 0; i < SHA256_DIGEST_SIZE && size != 0; i++, size--)
|
||||
*data++ = buff[i];
|
||||
}
|
||||
}
|
||||
|
||||
CRandomGenerator g_RandomGenerator;
|
||||
|
||||
#endif
|
||||
40
3rdparty/lzma/CPP/7zip/Crypto/RandGen.h
vendored
Normal file
40
3rdparty/lzma/CPP/7zip/Crypto/RandGen.h
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
// RandGen.h
|
||||
|
||||
#ifndef __CRYPTO_RAND_GEN_H
|
||||
#define __CRYPTO_RAND_GEN_H
|
||||
|
||||
#include "../../../C/Sha256.h"
|
||||
|
||||
#ifdef _WIN64
|
||||
// #define USE_STATIC_SYSTEM_RAND
|
||||
#endif
|
||||
|
||||
#ifdef USE_STATIC_SYSTEM_RAND
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <ntsecapi.h>
|
||||
#define MY_RAND_GEN(data, size) RtlGenRandom(data, size)
|
||||
#else
|
||||
#define MY_RAND_GEN(data, size) getrandom(data, size, 0)
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
class CRandomGenerator
|
||||
{
|
||||
Byte _buff[SHA256_DIGEST_SIZE];
|
||||
bool _needInit;
|
||||
|
||||
void Init();
|
||||
public:
|
||||
CRandomGenerator(): _needInit(true) {};
|
||||
void Generate(Byte *data, unsigned size);
|
||||
};
|
||||
|
||||
extern CRandomGenerator g_RandomGenerator;
|
||||
|
||||
#define MY_RAND_GEN(data, size) g_RandomGenerator.Generate(data, size)
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
8
3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h
vendored
Normal file
8
3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
// StdAfx.h
|
||||
|
||||
#ifndef __STDAFX_H
|
||||
#define __STDAFX_H
|
||||
|
||||
#include "../../Common/Common.h"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user