summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/lzma/CPP/7zip/Crypto
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/lzma/CPP/7zip/Crypto')
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp91
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/7zAes.h48
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp6
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp220
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/MyAes.h113
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp21
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp173
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/RandGen.h26
-rw-r--r--3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h7
9 files changed, 554 insertions, 151 deletions
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp b/3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp
index d33b562a3f6..6b1c648d5c1 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp
+++ b/3rdparty/lzma/CPP/7zip/Crypto/7zAes.cpp
@@ -2,11 +2,13 @@
#include "StdAfx.h"
+#include "../../../C/CpuArch.h"
#include "../../../C/Sha256.h"
#include "../../Common/ComTry.h"
+#include "../../Common/MyBuffer2.h"
-#ifndef _7ZIP_ST
+#ifndef Z7_ST
#include "../../Windows/Synchronization.h"
#endif
@@ -15,7 +17,7 @@
#include "7zAes.h"
#include "MyAes.h"
-#ifndef EXTRACT_ONLY
+#ifndef Z7_EXTRACT_ONLY
#include "RandGen.h"
#endif
@@ -48,31 +50,65 @@ void CKeyInfo::CalcKey()
}
else
{
- size_t bufSize = 8 + SaltSize + Password.Size();
- CObjArray<Byte> buf(bufSize);
+ 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);
- CSha256 sha;
- Sha256_Init(&sha);
-
- Byte *ctr = buf + SaltSize + Password.Size();
-
- for (unsigned i = 0; i < 8; i++)
- ctr[i] = 0;
+ 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(&sha, buf, bufSize);
+ Sha256_Update((CSha256 *)(Byte *)sha, buf, bufSize);
for (unsigned i = 0; i < 8; i++)
if (++(ctr[i]) != 0)
break;
}
while (--numRounds != 0);
+ */
- Sha256_Final(&sha, Key);
+ Sha256_Final((CSha256 *)(void *)(Byte *)sha, Key);
+ memset(sha, 0, sha.Size());
}
}
@@ -117,7 +153,7 @@ void CKeyInfoCache::Add(const CKeyInfo &key)
static CKeyInfoCache g_GlobalKeyCache(32);
-#ifndef _7ZIP_ST
+#ifndef Z7_ST
static NWindows::NSynchronization::CCriticalSection g_GlobalKeyCacheCriticalSection;
#define MT_LOCK NWindows::NSynchronization::CCriticalSectionLock lock(g_GlobalKeyCacheCriticalSection);
#else
@@ -149,10 +185,10 @@ void CBase::PrepareKey()
g_GlobalKeyCache.FindAndAdd(_key);
}
-#ifndef EXTRACT_ONLY
+#ifndef Z7_EXTRACT_ONLY
/*
-STDMETHODIMP CEncoder::ResetSalt()
+Z7_COM7F_IMF(CEncoder::ResetSalt())
{
_key.SaltSize = 4;
g_RandomGenerator.Generate(_key.Salt, _key.SaltSize);
@@ -160,16 +196,16 @@ STDMETHODIMP CEncoder::ResetSalt()
}
*/
-STDMETHODIMP CEncoder::ResetInitVector()
+Z7_COM7F_IMF(CEncoder::ResetInitVector())
{
for (unsigned i = 0; i < sizeof(_iv); i++)
_iv[i] = 0;
- _ivSize = 8;
- g_RandomGenerator.Generate(_iv, _ivSize);
+ _ivSize = 16;
+ MY_RAND_GEN(_iv, _ivSize);
return S_OK;
}
-STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
+Z7_COM7F_IMF(CEncoder::WriteCoderProperties(ISequentialOutStream *outStream))
{
Byte props[2 + sizeof(_key.Salt) + sizeof(_iv)];
unsigned propsSize = 1;
@@ -207,7 +243,7 @@ CDecoder::CDecoder()
_aesFilter = new CAesCbcDecoder(kKeySize);
}
-STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
+Z7_COM7F_IMF(CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size))
{
_key.ClearProps();
@@ -246,33 +282,34 @@ STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
}
-STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size)
+Z7_COM7F_IMF(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()
+Z7_COM7F_IMF(CBaseCoder::Init())
{
COM_TRY_BEGIN
PrepareKey();
CMyComPtr<ICryptoProperties> cp;
- RINOK(_aesFilter.QueryInterface(IID_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)));
+ 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)
+Z7_COM7F_IMF2(UInt32, CBaseCoder::Filter(Byte *data, UInt32 size))
{
return _aesFilter->Filter(data, size);
}
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/7zAes.h b/3rdparty/lzma/CPP/7zip/Crypto/7zAes.h
index 84e07ac9613..8f7bf03eb29 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/7zAes.h
+++ b/3rdparty/lzma/CPP/7zip/Crypto/7zAes.h
@@ -1,7 +1,7 @@
// 7zAes.h
-#ifndef __CRYPTO_7Z_AES_H
-#define __CRYPTO_7Z_AES_H
+#ifndef ZIP7_INC_CRYPTO_7Z_AES_H
+#define ZIP7_INC_CRYPTO_7Z_AES_H
#include "../../Common/MyBuffer.h"
#include "../../Common/MyCom.h"
@@ -37,6 +37,20 @@ public:
for (unsigned i = 0; i < sizeof(Salt); i++)
Salt[i] = 0;
}
+
+ void Wipe()
+ {
+ Password.Wipe();
+ NumCyclesPower = 0;
+ SaltSize = 0;
+ Z7_memset_0_ARRAY(Salt);
+ Z7_memset_0_ARRAY(Key);
+ }
+
+#ifdef Z7_CPP_IS_SUPPORTED_default
+ CKeyInfo(const CKeyInfo &) = default;
+#endif
+ ~CKeyInfo() { Wipe(); }
};
class CKeyInfoCache
@@ -68,48 +82,46 @@ class CBaseCoder:
public CMyUnknownImp,
public CBase
{
+ Z7_IFACE_COM7_IMP(ICompressFilter)
+ Z7_IFACE_COM7_IMP(ICryptoSetPassword)
protected:
+ virtual ~CBaseCoder() {}
CMyComPtr<ICompressFilter> _aesFilter;
-
-public:
- INTERFACE_ICompressFilter(;)
-
- STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
};
-#ifndef EXTRACT_ONLY
+#ifndef Z7_EXTRACT_ONLY
-class CEncoder:
+class CEncoder Z7_final:
public CBaseCoder,
public ICompressWriteCoderProperties,
// public ICryptoResetSalt,
public ICryptoResetInitVector
{
-public:
- MY_UNKNOWN_IMP4(
+ Z7_COM_UNKNOWN_IMP_4(
ICompressFilter,
ICryptoSetPassword,
ICompressWriteCoderProperties,
// ICryptoResetSalt,
ICryptoResetInitVector)
- STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
- // STDMETHOD(ResetSalt)();
- STDMETHOD(ResetInitVector)();
+ Z7_IFACE_COM7_IMP(ICompressWriteCoderProperties)
+ // Z7_IFACE_COM7_IMP(ICryptoResetSalt)
+ Z7_IFACE_COM7_IMP(ICryptoResetInitVector)
+public:
CEncoder();
};
#endif
-class CDecoder:
+class CDecoder Z7_final:
public CBaseCoder,
public ICompressSetDecoderProperties2
{
-public:
- MY_UNKNOWN_IMP3(
+ Z7_COM_UNKNOWN_IMP_3(
ICompressFilter,
ICryptoSetPassword,
ICompressSetDecoderProperties2)
- STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
+ Z7_IFACE_COM7_IMP(ICompressSetDecoderProperties2)
+public:
CDecoder();
};
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp b/3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp
index f9d59699b7c..69d0890a40b 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp
+++ b/3rdparty/lzma/CPP/7zip/Crypto/7zAesRegister.cpp
@@ -9,9 +9,9 @@
namespace NCrypto {
namespace N7z {
-REGISTER_FILTER_E(7zAES,
- CDecoder(),
- CEncoder(),
+REGISTER_FILTER_E(SzAES,
+ CDecoder,
+ CEncoder,
0x6F10701, "7zAES")
}}
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp b/3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp
index 52eaab7ae2c..6abc38812b3 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp
+++ b/3rdparty/lzma/CPP/7zip/Crypto/MyAes.cpp
@@ -10,103 +10,237 @@ namespace NCrypto {
static struct CAesTabInit { CAesTabInit() { AesGenTables();} } g_AesTabInit;
-CAesCbcCoder::CAesCbcCoder(bool encodeMode, unsigned keySize):
- _keySize(keySize),
+CAesCoder::CAesCoder(
+ // bool encodeMode,
+ unsigned keySize
+ // , bool ctrMode
+ ):
_keyIsSet(false),
- _encodeMode(encodeMode)
+ // _encodeMode(encodeMode),
+ // _ctrMode(ctrMode),
+ _keySize(keySize),
+ // _ctrPos(0), // _ctrPos =0 will be set in Init()
+ _aes(AES_NUM_IVMRK_WORDS * 4 + AES_BLOCK_SIZE * 2)
{
- _offset = ((0 - (unsigned)(ptrdiff_t)_aes) & 0xF) / sizeof(UInt32);
+ // _offset = ((0 - (unsigned)(ptrdiff_t)_aes) & 0xF) / sizeof(UInt32);
memset(_iv, 0, AES_BLOCK_SIZE);
- SetFunctions(0);
+ /*
+ // 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;
+ */
}
-STDMETHODIMP CAesCbcCoder::Init()
+Z7_COM7F_IMF(CAesCoder::Init())
{
- AesCbc_Init(_aes + _offset, _iv);
- return _keyIsSet ? S_OK : E_FAIL;
+ _ctrPos = 0;
+ AesCbc_Init(Aes(), _iv);
+ return _keyIsSet ? S_OK : E_NOTIMPL; // E_FAIL
}
-STDMETHODIMP_(UInt32) CAesCbcCoder::Filter(Byte *data, UInt32 size)
+Z7_COM7F_IMF2(UInt32, CAesCoder::Filter(Byte *data, UInt32 size))
{
if (!_keyIsSet)
return 0;
- if (size == 0)
- return 0;
if (size < AES_BLOCK_SIZE)
+ {
+ if (size == 0)
+ return 0;
return AES_BLOCK_SIZE;
+ }
size >>= 4;
- _codeFunc(_aes + _offset, data, size);
+ // (data) must be aligned for 16-bytes here
+ _codeFunc(Aes(), data, size);
return size << 4;
}
-STDMETHODIMP CAesCbcCoder::SetKey(const Byte *data, UInt32 size)
+
+Z7_COM7F_IMF(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 = _encodeMode ? Aes_SetKey_Enc : Aes_SetKey_Dec;
- setKeyFunc(_aes + _offset + 4, data, size);
+ _setKeyFunc(Aes() + 4, data, size);
_keyIsSet = true;
return S_OK;
}
-STDMETHODIMP CAesCbcCoder::SetInitVector(const Byte *data, UInt32 size)
+Z7_COM7F_IMF(CAesCoder::SetInitVector(const Byte *data, UInt32 size))
{
if (size != AES_BLOCK_SIZE)
return E_INVALIDARG;
memcpy(_iv, data, size);
- CAesCbcCoder::Init(); // don't call virtual function here !!!
+ /* we allow SetInitVector() call before SetKey() call.
+ so we ignore possible error in Init() here */
+ CAesCoder::Init(); // don't call virtual function here !!!
return S_OK;
}
-EXTERN_C_BEGIN
-void MY_FAST_CALL AesCbc_Encode(UInt32 *ivAes, Byte *data, size_t numBlocks);
-void MY_FAST_CALL AesCbc_Decode(UInt32 *ivAes, Byte *data, size_t numBlocks);
-void MY_FAST_CALL AesCtr_Code(UInt32 *ivAes, Byte *data, size_t numBlocks);
+#ifndef Z7_SFX
-void MY_FAST_CALL AesCbc_Encode_Intel(UInt32 *ivAes, Byte *data, size_t numBlocks);
-void MY_FAST_CALL AesCbc_Decode_Intel(UInt32 *ivAes, Byte *data, size_t numBlocks);
-void MY_FAST_CALL AesCtr_Code_Intel(UInt32 *ivAes, Byte *data, size_t numBlocks);
-
-EXTERN_C_END
+/*
+Z7_COM7F_IMF(CAesCtrCoder::Init())
+{
+ _ctrPos = 0;
+ return CAesCoder::Init();
+}
+*/
-bool CAesCbcCoder::SetFunctions(UInt32 algo)
+Z7_COM7F_IMF2(UInt32, CAesCtrCoder::Filter(Byte *data, UInt32 size))
{
- _codeFunc = _encodeMode ?
- g_AesCbc_Encode :
- g_AesCbc_Decode;
- if (algo == 1)
+ if (!_keyIsSet)
+ return 0;
+ if (size == 0)
+ return 0;
+
+ if (_ctrPos != 0)
{
- _codeFunc = _encodeMode ?
- AesCbc_Encode:
- AesCbc_Decode;
+ /* Optimized caller will not call here */
+ const Byte *ctr = (Byte *)(Aes() + AES_NUM_IVMRK_WORDS);
+ unsigned num = 0;
+ for (unsigned i = _ctrPos; i != AES_BLOCK_SIZE; i++)
+ {
+ if (num == size)
+ {
+ _ctrPos = i;
+ return num;
+ }
+ data[num++] ^= ctr[i];
+ }
+ _ctrPos = 0;
+ /* if (num < size) {
+ we can filter more data with _codeFunc().
+ But it's supposed that the caller can work correctly,
+ even if we do only partial filtering here.
+ So we filter data only for current 16-byte block. }
+ */
+ /*
+ size -= num;
+ size >>= 4;
+ // (data) must be aligned for 16-bytes here
+ _codeFunc(Aes(), data + num, size);
+ return num + (size << 4);
+ */
+ return num;
}
- if (algo == 2)
+
+ if (size < AES_BLOCK_SIZE)
{
- #ifdef MY_CPU_X86_OR_AMD64
- if (g_AesCbc_Encode != AesCbc_Encode_Intel)
- #endif
- return false;
+ /* The good optimized caller can call here only in last Filter() call.
+ But we support also non-optimized callers,
+ where another Filter() calls are allowed after this call.
+ */
+ 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);
+ _ctrPos = size;
+ return size;
}
- return true;
+
+ size >>= 4;
+ // (data) must be aligned for 16-bytes here
+ _codeFunc(Aes(), data, size);
+ return size << 4;
}
-STDMETHODIMP CAesCbcCoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps)
+#endif // Z7_SFX
+
+
+#ifndef Z7_EXTRACT_ONLY
+
+#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
+
+#ifdef USE_HW_AES
+ #define SET_AES_FUNC_2(f2) \
+ if (algo == 2) if (g_Aes_SupportedFunctions_Flags & k_Aes_SupportedFunctions_HW) \
+ { f = f2; }
+ #ifdef MY_CPU_X86_OR_AMD64
+ #define SET_AES_FUNC_23(f2, f3) \
+ SET_AES_FUNC_2(f2) \
+ if (algo == 3) if (g_Aes_SupportedFunctions_Flags & k_Aes_SupportedFunctions_HW_256) \
+ { f = f3; }
+ #else // MY_CPU_X86_OR_AMD64
+ #define SET_AES_FUNC_23(f2, f3) \
+ SET_AES_FUNC_2(f2)
+ #endif // MY_CPU_X86_OR_AMD64
+#else // USE_HW_AES
+ #define SET_AES_FUNC_23(f2, f3)
+#endif // USE_HW_AES
+
+#define SET_AES_FUNCS(c, f0, f1, f2, f3) \
+ bool c::SetFunctions(UInt32 algo) { \
+ _codeFunc = f0; if (algo < 1) return true; \
+ AES_CODE_FUNC f = NULL; \
+ if (algo == 1) { f = f1; } \
+ SET_AES_FUNC_23(f2, f3) \
+ if (f) { _codeFunc = f; return true; } \
+ return false; }
+
+
+
+#ifndef Z7_SFX
+SET_AES_FUNCS(
+ CAesCtrCoder,
+ g_AesCtr_Code,
+ AesCtr_Code,
+ AesCtr_Code_HW,
+ AesCtr_Code_HW_256)
+#endif
+
+SET_AES_FUNCS(
+ CAesCbcEncoder,
+ g_AesCbc_Encode,
+ AesCbc_Encode,
+ AesCbc_Encode_HW,
+ AesCbc_Encode_HW)
+
+SET_AES_FUNCS(
+ CAesCbcDecoder,
+ g_AesCbc_Decode,
+ AesCbc_Decode,
+ AesCbc_Decode_HW,
+ AesCbc_Decode_HW_256)
+
+Z7_COM7F_IMF(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)
{
+ const PROPVARIANT &prop = coderProps[i];
if (prop.vt != VT_UI4)
return E_INVALIDARG;
- if (!SetFunctions(prop.ulVal))
+ if (prop.ulVal > 3)
return E_NOTIMPL;
+ algo = prop.ulVal;
}
}
+ if (!SetFunctions(algo))
+ return E_NOTIMPL;
return S_OK;
}
+#endif // Z7_EXTRACT_ONLY
+
}
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/MyAes.h b/3rdparty/lzma/CPP/7zip/Crypto/MyAes.h
index 84c21de30e7..a3c1c27faa5 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/MyAes.h
+++ b/3rdparty/lzma/CPP/7zip/Crypto/MyAes.h
@@ -1,55 +1,122 @@
// Crypto/MyAes.h
-#ifndef __CRYPTO_MY_AES_H
-#define __CRYPTO_MY_AES_H
+#ifndef ZIP7_INC_CRYPTO_MY_AES_H
+#define ZIP7_INC_CRYPTO_MY_AES_H
#include "../../../C/Aes.h"
+#include "../../Common/MyBuffer2.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
namespace NCrypto {
-class CAesCbcCoder:
+#ifdef Z7_EXTRACT_ONLY
+#define Z7_IFACEN_IAesCoderSetFunctions(x)
+#else
+#define Z7_IFACEN_IAesCoderSetFunctions(x) \
+ virtual bool SetFunctions(UInt32 algo) x
+#endif
+
+
+class CAesCoder:
public ICompressFilter,
public ICryptoProperties,
+ #ifndef Z7_EXTRACT_ONLY
public ICompressSetCoderProperties,
+ #endif
public CMyUnknownImp
{
- AES_CODE_FUNC _codeFunc;
- unsigned _offset;
- unsigned _keySize;
+ Z7_COM_QI_BEGIN2(ICompressFilter)
+ Z7_COM_QI_ENTRY(ICryptoProperties)
+ #ifndef Z7_EXTRACT_ONLY
+ Z7_COM_QI_ENTRY(ICompressSetCoderProperties)
+ #endif
+ Z7_COM_QI_END
+ Z7_COM_ADDREF_RELEASE
+
+public:
+ Z7_IFACE_COM7_IMP_NONFINAL(ICompressFilter)
+ Z7_IFACE_COM7_IMP(ICryptoProperties)
+private:
+ #ifndef Z7_EXTRACT_ONLY
+ Z7_IFACE_COM7_IMP(ICompressSetCoderProperties)
+ #endif
+
+protected:
bool _keyIsSet;
- bool _encodeMode;
- UInt32 _aes[AES_NUM_IVMRK_WORDS + 3];
+ // bool _encodeMode;
+ // bool _ctrMode;
+ // unsigned _offset;
+ unsigned _keySize;
+ unsigned _ctrPos; // we need _ctrPos here for Init() / SetInitVector()
+ AES_CODE_FUNC _codeFunc;
+ AES_SET_KEY_FUNC _setKeyFunc;
+private:
+ // UInt32 _aes[AES_NUM_IVMRK_WORDS + 3];
+ CAlignedBuffer1 _aes;
+
Byte _iv[AES_BLOCK_SIZE];
- bool SetFunctions(UInt32 algo);
+ // UInt32 *Aes() { return _aes + _offset; }
+protected:
+ UInt32 *Aes() { return (UInt32 *)(void *)(Byte *)_aes; }
+
+ Z7_IFACE_PURE(IAesCoderSetFunctions)
public:
- CAesCbcCoder(bool encodeMode, unsigned keySize);
-
- MY_UNKNOWN_IMP3(ICompressFilter, ICryptoProperties, ICompressSetCoderProperties)
-
- INTERFACE_ICompressFilter(;)
-
- STDMETHOD(SetKey)(const Byte *data, UInt32 size);
- STDMETHOD(SetInitVector)(const Byte *data, UInt32 size);
-
- STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
+ CAesCoder(
+ // bool encodeMode,
+ unsigned keySize
+ // , bool ctrMode
+ );
+ virtual ~CAesCoder() {} // we need virtual destructor for derived classes
+ void SetKeySize(unsigned size) { _keySize = size; }
};
-struct CAesCbcEncoder: public CAesCbcCoder
+
+#ifndef Z7_EXTRACT_ONLY
+struct CAesCbcEncoder: public CAesCoder
{
- CAesCbcEncoder(unsigned keySize = 0): CAesCbcCoder(true, keySize) {}
+ CAesCbcEncoder(unsigned keySize = 0): CAesCoder(keySize)
+ {
+ _setKeyFunc = Aes_SetKey_Enc;
+ _codeFunc = g_AesCbc_Encode;
+ }
+ Z7_IFACE_IMP(IAesCoderSetFunctions)
};
+#endif
-struct CAesCbcDecoder: public CAesCbcCoder
+struct CAesCbcDecoder: public CAesCoder
{
- CAesCbcDecoder(unsigned keySize = 0): CAesCbcCoder(false, keySize) {}
+ CAesCbcDecoder(unsigned keySize = 0): CAesCoder(keySize)
+ {
+ _setKeyFunc = Aes_SetKey_Dec;
+ _codeFunc = g_AesCbc_Decode;
+ }
+ Z7_IFACE_IMP(IAesCoderSetFunctions)
};
+#ifndef Z7_SFX
+struct CAesCtrCoder: public CAesCoder
+{
+private:
+ // unsigned _ctrPos;
+ // Z7_IFACE_COM7_IMP(ICompressFilter)
+ // Z7_COM7F_IMP(Init())
+ Z7_COM7F_IMP2(UInt32, Filter(Byte *data, UInt32 size))
+public:
+ CAesCtrCoder(unsigned keySize = 0): CAesCoder(keySize)
+ {
+ _ctrPos = 0;
+ _setKeyFunc = Aes_SetKey_Enc;
+ _codeFunc = g_AesCtr_Code;
+ }
+ Z7_IFACE_IMP(IAesCoderSetFunctions)
+};
+#endif
+
}
#endif
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp b/3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp
index 28006835549..e2b4f72dbdc 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp
+++ b/3rdparty/lzma/CPP/7zip/Crypto/MyAesReg.cpp
@@ -8,9 +8,22 @@
namespace NCrypto {
-REGISTER_FILTER_E(AES256CBC,
- CAesCbcDecoder(32),
- CAesCbcEncoder(32),
- 0x6F00181, "AES256CBC")
+#ifndef Z7_SFX
+
+#define REGISTER_AES_2(name, nameString, keySize) \
+ REGISTER_FILTER_E(name, \
+ CAesCbcDecoder(keySize), \
+ CAesCbcEncoder(keySize), \
+ 0x6F00100 | ((keySize - 16) * 8) | (/* isCtr */ 0 ? 4 : 1), \
+ nameString) \
+
+#define REGISTER_AES(name, nameString) \
+ /* REGISTER_AES_2(AES128 ## name, "AES128" nameString, 16) */ \
+ /* REGISTER_AES_2(AES192 ## name, "AES192" nameString, 24) */ \
+ REGISTER_AES_2(AES256 ## name, "AES256" nameString, 32) \
+
+REGISTER_AES(CBC, "CBC")
+
+#endif
}
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp b/3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp
index 408f73f1686..05a6c06d4c3 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp
+++ b/3rdparty/lzma/CPP/7zip/Crypto/RandGen.cpp
@@ -2,14 +2,44 @@
#include "StdAfx.h"
-#ifndef _7ZIP_ST
+#include "RandGen.h"
+
+#ifndef USE_STATIC_SYSTEM_RAND
+
+#ifndef Z7_ST
#include "../../Windows/Synchronization.h"
#endif
-#include "RandGen.h"
-#ifndef _WIN32
+#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
@@ -21,61 +51,141 @@
#endif
#endif
-// This is not very good random number generator.
-// Please use it only for salt.
-// First generated data block depends from timer and processID.
+// 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
-// Maybe it's possible to restore original timer value from generated value.
#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);
+ HASH_UPD(w)
w = ::GetCurrentThreadId();
- HASH_UPD(w);
+ 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
+ {
+ const HMODULE hModule = ::LoadLibrary(TEXT("advapi32.dll"));
+ if (hModule)
+ {
+ // SystemFunction036() is real name of RtlGenRandom() function
+ const
+ Func_RtlGenRandom
+ my_RtlGenRandom = Z7_GET_PROC_ADDRESS(
+ Func_RtlGenRandom, 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);
+ HASH_UPD(pid)
pid = getppid();
- HASH_UPD(pid);
+ 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
- for (unsigned i = 0; i <
- #ifdef _DEBUG
- 2;
- #else
- 1000;
- #endif
- i++)
+ do
{
#ifdef _WIN32
LARGE_INTEGER v;
if (::QueryPerformanceCounter(&v))
- HASH_UPD(v.QuadPart);
+ HASH_UPD(v.QuadPart)
#endif
#ifdef USE_POSIX_TIME
#ifdef USE_POSIX_TIME2
timeval v;
- if (gettimeofday(&v, 0) == 0)
+ if (gettimeofday(&v, NULL) == 0)
{
- HASH_UPD(v.tv_sec);
- HASH_UPD(v.tv_usec);
+ HASH_UPD(v.tv_sec)
+ HASH_UPD(v.tv_usec)
}
#endif
- time_t v2 = time(NULL);
- HASH_UPD(v2);
+ const time_t v2 = time(NULL);
+ HASH_UPD(v2)
#endif
#ifdef _WIN32
- DWORD tickCount = ::GetTickCount();
- HASH_UPD(tickCount);
+ const DWORD tickCount = ::GetTickCount();
+ HASH_UPD(tickCount)
#endif
for (unsigned j = 0; j < 100; j++)
@@ -85,11 +195,13 @@ void CRandomGenerator::Init()
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
}
}
+ while (--numIterations);
+
Sha256_Final(&hash, _buff);
_needInit = false;
}
-#ifndef _7ZIP_ST
+#ifndef Z7_ST
static NWindows::NSynchronization::CCriticalSection g_CriticalSection;
#define MT_LOCK NWindows::NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
#else
@@ -104,6 +216,7 @@ void CRandomGenerator::Generate(Byte *data, unsigned size)
Init();
while (size != 0)
{
+ MY_ALIGN (16)
CSha256 hash;
Sha256_Init(&hash);
@@ -112,8 +225,9 @@ void CRandomGenerator::Generate(Byte *data, unsigned size)
Sha256_Init(&hash);
UInt32 salt = 0xF672ABD1;
- HASH_UPD(salt);
+ 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--)
@@ -121,4 +235,7 @@ void CRandomGenerator::Generate(Byte *data, unsigned size)
}
}
+MY_ALIGN (16)
CRandomGenerator g_RandomGenerator;
+
+#endif
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/RandGen.h b/3rdparty/lzma/CPP/7zip/Crypto/RandGen.h
index cfdcd60d012..3bd69ec2d16 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/RandGen.h
+++ b/3rdparty/lzma/CPP/7zip/Crypto/RandGen.h
@@ -1,10 +1,25 @@
// RandGen.h
-#ifndef __CRYPTO_RAND_GEN_H
-#define __CRYPTO_RAND_GEN_H
+#ifndef ZIP7_INC_CRYPTO_RAND_GEN_H
+#define ZIP7_INC_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];
@@ -12,10 +27,15 @@ class CRandomGenerator
void Init();
public:
- CRandomGenerator(): _needInit(true) {};
+ CRandomGenerator(): _needInit(true) {}
void Generate(Byte *data, unsigned size);
};
+MY_ALIGN (16)
extern CRandomGenerator g_RandomGenerator;
+#define MY_RAND_GEN(data, size) g_RandomGenerator.Generate(data, size)
+
+#endif
+
#endif
diff --git a/3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h b/3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h
index 1cbd7feaeec..80866550d1f 100644
--- a/3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h
+++ b/3rdparty/lzma/CPP/7zip/Crypto/StdAfx.h
@@ -1,8 +1,11 @@
// StdAfx.h
-#ifndef __STDAFX_H
-#define __STDAFX_H
+#ifndef ZIP7_INC_STDAFX_H
+#define ZIP7_INC_STDAFX_H
+#if defined(_MSC_VER) && _MSC_VER >= 1800
+#pragma warning(disable : 4464) // relative include path contains '..'
+#endif
#include "../../Common/Common.h"
#endif