Update to 7-Zip Version 19.00

- Encryption strength for 7z archives was increased the size of random
  initialization vector was increased from 64-bit to 128-bit, and the
  pseudo-random number generator was improved.
- Some bugs were fixed.
This commit is contained in:
Tino Reichardt
2019-02-22 17:11:17 +01:00
parent 34323d51e9
commit ccca7cd09d
76 changed files with 924 additions and 420 deletions

View File

@@ -151,10 +151,21 @@ void CBase::PrepareKey()
#ifndef EXTRACT_ONLY
/*
STDMETHODIMP CEncoder::ResetSalt()
{
_key.SaltSize = 4;
g_RandomGenerator.Generate(_key.Salt, _key.SaltSize);
return S_OK;
}
*/
STDMETHODIMP CEncoder::ResetInitVector()
{
_ivSize = sizeof(_iv);
g_RandomGenerator.Generate(_iv, sizeof(_iv));
for (unsigned i = 0; i < sizeof(_iv); i++)
_iv[i] = 0;
_ivSize = 16;
MY_RAND_GEN(_iv, _ivSize);
return S_OK;
}
@@ -234,6 +245,7 @@ STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
|| _key.NumCyclesPower == 0x3F) ? S_OK : E_NOTIMPL;
}
STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size)
{
COM_TRY_BEGIN

View File

@@ -2,19 +2,46 @@
#include "StdAfx.h"
#include "RandGen.h"
#ifndef USE_STATIC_SYSTEM_RAND
#ifndef _7ZIP_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
#else
# define RtlGenRandom SystemFunction036
extern "C" BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
#endif
#ifdef USE_POSIX_TIME
@@ -24,11 +51,9 @@ extern "C" BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLeng
#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));
@@ -37,27 +62,102 @@ void CRandomGenerator::Init()
CSha256 hash;
Sha256_Init(&hash);
unsigned numIterations = 1000;
{
#ifndef UNDER_CE
const unsigned kNumIterations_Small = 100;
const unsigned kBufSize = 32;
Byte buf[kBufSize];
#endif
#ifdef _WIN32
DWORD w = ::GetCurrentProcessId();
HASH_UPD(w);
w = ::GetCurrentThreadId();
HASH_UPD(w);
if (RtlGenRandom(&w, sizeof(DWORD)))
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)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
{
int n = read(f, buf, numBytes);
if (n <= 0)
break;
Sha256_Update(&hash, buf, n);
numBytes -= 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;
@@ -90,6 +190,8 @@ void CRandomGenerator::Init()
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
}
}
while (--numIterations);
Sha256_Final(&hash, _buff);
_needInit = false;
}
@@ -127,3 +229,5 @@ void CRandomGenerator::Generate(Byte *data, unsigned size)
}
CRandomGenerator g_RandomGenerator;
#endif

View File

@@ -5,6 +5,21 @@
#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];
@@ -18,4 +33,8 @@ public:
extern CRandomGenerator g_RandomGenerator;
#define MY_RAND_GEN(data, size) g_RandomGenerator.Generate(data, size)
#endif
#endif

View File

@@ -96,7 +96,7 @@ STDMETHODIMP CBaseCoder::Init()
HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream)
{
unsigned saltSize = _key.GetSaltSize();
g_RandomGenerator.Generate(_key.Salt, saltSize);
MY_RAND_GEN(_key.Salt, saltSize);
Init2();
RINOK(WriteStream(outStream, _key.Salt, saltSize));
return WriteStream(outStream, _key.PwdVerifComputed, kPwdVerifSize);

View File

@@ -49,7 +49,7 @@ HRESULT CEncoder::WriteHeader_Check16(ISequentialOutStream *outStream, UInt16 cr
PKZIP 2.0+ used 1 byte CRC check. It's more secure.
We also use 1 byte CRC. */
g_RandomGenerator.Generate(h, kHeaderSize - 1);
MY_RAND_GEN(h, kHeaderSize - 1);
// h[kHeaderSize - 2] = (Byte)(crc);
h[kHeaderSize - 1] = (Byte)(crc >> 8);

View File

@@ -85,13 +85,14 @@ HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream, UInt32 crc, UInt64 u
return E_NOTIMPL;
RINOK(ReadStream_FALSE(inStream, temp, 4));
_remSize = GetUi32(temp);
const UInt32 kAlign = 16;
// const UInt32 kAlign = 16;
if (_remSize < 16 || _remSize > (1 << 18))
return E_NOTIMPL;
if (_remSize + kAlign > _buf.Size())
if (_remSize > _bufAligned.Size())
{
_buf.Alloc(_remSize + kAlign);
_bufAligned = (Byte *)((ptrdiff_t)((Byte *)_buf + kAlign - 1) & ~(ptrdiff_t)(kAlign - 1));
_bufAligned.AllocAtLeast(_remSize);
if (!(Byte *)_bufAligned)
return E_OUTOFMEMORY;
}
return ReadStream_FALSE(inStream, _bufAligned, _remSize);
}

View File

@@ -3,7 +3,7 @@
#ifndef __CRYPTO_ZIP_STRONG_H
#define __CRYPTO_ZIP_STRONG_H
#include "../../Common/MyBuffer.h"
#include "../../Common/MyBuffer2.h"
#include "../IPassword.h"
@@ -35,8 +35,7 @@ class CBaseCoder:
{
protected:
CKeyInfo _key;
CByteBuffer _buf;
Byte *_bufAligned;
CAlignedBuffer _bufAligned;
public:
STDMETHOD(Init)();
STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);