summaryrefslogtreecommitdiffstats
path: root/other-licenses/7zstub/src/CPP/7zip/Crypto
diff options
context:
space:
mode:
Diffstat (limited to 'other-licenses/7zstub/src/CPP/7zip/Crypto')
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.cpp280
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.h118
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/7zAesRegister.cpp17
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.cpp112
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.h57
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/MyAesReg.cpp16
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.cpp124
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.h21
-rw-r--r--other-licenses/7zstub/src/CPP/7zip/Crypto/StdAfx.h8
9 files changed, 753 insertions, 0 deletions
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.cpp b/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.cpp
new file mode 100644
index 0000000000..f412bf9d14
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.cpp
@@ -0,0 +1,280 @@
+// 7zAes.cpp
+
+#include "StdAfx.h"
+
+#include "../../../C/Sha256.h"
+
+#include "../../Common/ComTry.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
+ {
+ size_t bufSize = 8 + SaltSize + Password.Size();
+ CObjArray<Byte> buf(bufSize);
+ memcpy(buf, Salt, SaltSize);
+ memcpy(buf + SaltSize, Password, Password.Size());
+
+ CSha256 sha;
+ Sha256_Init(&sha);
+
+ Byte *ctr = buf + SaltSize + Password.Size();
+
+ for (unsigned i = 0; i < 8; i++)
+ ctr[i] = 0;
+
+ UInt64 numRounds = (UInt64)1 << NumCyclesPower;
+
+ do
+ {
+ Sha256_Update(&sha, buf, bufSize);
+ for (unsigned i = 0; i < 8; i++)
+ if (++(ctr[i]) != 0)
+ break;
+ }
+ while (--numRounds != 0);
+
+ Sha256_Final(&sha, Key);
+ }
+}
+
+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 = 8;
+ g_RandomGenerator.Generate(_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.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);
+}
+
+}}
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.h b/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.h
new file mode 100644
index 0000000000..5a09436079
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAes.h
@@ -0,0 +1,118 @@
+// 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;
+ }
+};
+
+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
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAesRegister.cpp b/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAesRegister.cpp
new file mode 100644
index 0000000000..c0b2060939
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/7zAesRegister.cpp
@@ -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")
+
+}}
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.cpp b/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.cpp
new file mode 100644
index 0000000000..1d399d707f
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.cpp
@@ -0,0 +1,112 @@
+// Crypto/MyAes.cpp
+
+#include "StdAfx.h"
+
+#include "../../../C/CpuArch.h"
+
+#include "MyAes.h"
+
+namespace NCrypto {
+
+static struct CAesTabInit { CAesTabInit() { AesGenTables();} } g_AesTabInit;
+
+CAesCbcCoder::CAesCbcCoder(bool encodeMode, unsigned keySize):
+ _keySize(keySize),
+ _keyIsSet(false),
+ _encodeMode(encodeMode)
+{
+ _offset = ((0 - (unsigned)(ptrdiff_t)_aes) & 0xF) / sizeof(UInt32);
+ memset(_iv, 0, AES_BLOCK_SIZE);
+ SetFunctions(0);
+}
+
+STDMETHODIMP CAesCbcCoder::Init()
+{
+ AesCbc_Init(_aes + _offset, _iv);
+ return _keyIsSet ? S_OK : E_FAIL;
+}
+
+STDMETHODIMP_(UInt32) CAesCbcCoder::Filter(Byte *data, UInt32 size)
+{
+ if (!_keyIsSet)
+ return 0;
+ if (size == 0)
+ return 0;
+ if (size < AES_BLOCK_SIZE)
+ return AES_BLOCK_SIZE;
+ size >>= 4;
+ _codeFunc(_aes + _offset, data, size);
+ return size << 4;
+}
+
+STDMETHODIMP CAesCbcCoder::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);
+ _keyIsSet = true;
+ return S_OK;
+}
+
+STDMETHODIMP CAesCbcCoder::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 !!!
+ 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);
+
+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
+
+bool CAesCbcCoder::SetFunctions(UInt32 algo)
+{
+ _codeFunc = _encodeMode ?
+ g_AesCbc_Encode :
+ g_AesCbc_Decode;
+ if (algo == 1)
+ {
+ _codeFunc = _encodeMode ?
+ AesCbc_Encode:
+ AesCbc_Decode;
+ }
+ if (algo == 2)
+ {
+ #ifdef MY_CPU_X86_OR_AMD64
+ if (g_AesCbc_Encode != AesCbc_Encode_Intel)
+ #endif
+ return false;
+ }
+ return true;
+}
+
+STDMETHODIMP CAesCbcCoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps)
+{
+ 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 (!SetFunctions(prop.ulVal))
+ return E_NOTIMPL;
+ }
+ }
+ return S_OK;
+}
+
+}
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.h b/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.h
new file mode 100644
index 0000000000..8d5ed98c3f
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAes.h
@@ -0,0 +1,57 @@
+// Crypto/MyAes.h
+
+#ifndef __CRYPTO_MY_AES_H
+#define __CRYPTO_MY_AES_H
+
+#include "../../../C/Aes.h"
+
+#include "../../Common/MyCom.h"
+
+#include "../ICoder.h"
+
+namespace NCrypto {
+
+class CAesCbcCoder:
+ public ICompressFilter,
+ public ICryptoProperties,
+ public ICompressSetCoderProperties,
+ public CMyUnknownImp
+{
+ AES_CODE_FUNC _codeFunc;
+ unsigned _offset;
+ unsigned _keySize;
+ bool _keyIsSet;
+ bool _encodeMode;
+ UInt32 _aes[AES_NUM_IVMRK_WORDS + 3];
+ Byte _iv[AES_BLOCK_SIZE];
+
+ bool SetFunctions(UInt32 algo);
+
+public:
+ CAesCbcCoder(bool encodeMode, unsigned keySize);
+
+ virtual ~CAesCbcCoder() {}; // we need virtual destructor for derived classes
+
+ 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);
+};
+
+struct CAesCbcEncoder: public CAesCbcCoder
+{
+ CAesCbcEncoder(unsigned keySize = 0): CAesCbcCoder(true, keySize) {}
+};
+
+struct CAesCbcDecoder: public CAesCbcCoder
+{
+ CAesCbcDecoder(unsigned keySize = 0): CAesCbcCoder(false, keySize) {}
+};
+
+}
+
+#endif
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAesReg.cpp b/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAesReg.cpp
new file mode 100644
index 0000000000..3427ad6257
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/MyAesReg.cpp
@@ -0,0 +1,16 @@
+// MyAesReg.cpp
+
+#include "StdAfx.h"
+
+#include "../Common/RegisterCodec.h"
+
+#include "MyAes.h"
+
+namespace NCrypto {
+
+REGISTER_FILTER_E(AES256CBC,
+ CAesCbcDecoder(32),
+ CAesCbcEncoder(32),
+ 0x6F00181, "AES256CBC")
+
+}
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.cpp b/other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.cpp
new file mode 100644
index 0000000000..542f39bd56
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.cpp
@@ -0,0 +1,124 @@
+// RandGen.cpp
+
+#include "StdAfx.h"
+
+#ifndef _7ZIP_ST
+#include "../../Windows/Synchronization.h"
+#endif
+
+#include "RandGen.h"
+
+#ifndef _WIN32
+#include <unistd.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
+
+// This is not very good random number generator.
+// Please use it only for salt.
+// First generated data block depends from timer and processID.
+// 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()
+{
+ CSha256 hash;
+ Sha256_Init(&hash);
+
+ #ifdef _WIN32
+ DWORD w = ::GetCurrentProcessId();
+ HASH_UPD(w);
+ w = ::GetCurrentThreadId();
+ HASH_UPD(w);
+ #else
+ pid_t pid = getpid();
+ HASH_UPD(pid);
+ pid = getppid();
+ HASH_UPD(pid);
+ #endif
+
+ for (unsigned i = 0; i <
+ #ifdef _DEBUG
+ 2;
+ #else
+ 1000;
+ #endif
+ i++)
+ {
+ #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);
+ }
+ }
+ 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)
+ {
+ 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);
+ 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;
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.h b/other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.h
new file mode 100644
index 0000000000..ff44450f8f
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/RandGen.h
@@ -0,0 +1,21 @@
+// RandGen.h
+
+#ifndef __CRYPTO_RAND_GEN_H
+#define __CRYPTO_RAND_GEN_H
+
+#include "../../../C/Sha256.h"
+
+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;
+
+#endif
diff --git a/other-licenses/7zstub/src/CPP/7zip/Crypto/StdAfx.h b/other-licenses/7zstub/src/CPP/7zip/Crypto/StdAfx.h
new file mode 100644
index 0000000000..42a088f121
--- /dev/null
+++ b/other-licenses/7zstub/src/CPP/7zip/Crypto/StdAfx.h
@@ -0,0 +1,8 @@
+// StdAfx.h
+
+#ifndef __STDAFX_H
+#define __STDAFX_H
+
+#include "../../Common/Common.h"
+
+#endif