4.58 beta

This commit is contained in:
Igor Pavlov
2008-05-05 00:00:00 +00:00
committed by Kornel Lesiński
parent bd1fa36322
commit 3901bf0ab8
326 changed files with 10643 additions and 14913 deletions

View File

@@ -7,9 +7,13 @@
#include "../../Common/StreamObjects.h"
#include "../../Common/StreamUtils.h"
#include "../AES/MyAES.h"
#include "../Hash/Sha256.h"
#include "7zAES.h"
extern "C"
{
#include "../../../../C/Sha256.h"
}
#ifndef EXTRACT_ONLY
#include "../Hash/RandGen.h"
#endif
@@ -43,19 +47,20 @@ void CKeyInfo::CalculateDigest()
}
else
{
NCrypto::NSha256::CContext sha;
CSha256 sha;
Sha256_Init(&sha);
const UInt64 numRounds = UInt64(1) << (NumCyclesPower);
Byte temp[8] = { 0,0,0,0,0,0,0,0 };
for (UInt64 round = 0; round < numRounds; round++)
{
sha.Update(Salt, SaltSize);
sha.Update(Password, Password.GetCapacity());
sha.Update(temp, 8);
Sha256_Update(&sha, Salt, (size_t)SaltSize);
Sha256_Update(&sha, Password, Password.GetCapacity());
Sha256_Update(&sha, temp, 8);
for (int i = 0; i < 8; i++)
if (++(temp[i]) != 0)
break;
}
sha.Final(Key);
Sha256_Final(&sha, Key);
}
}
@@ -129,7 +134,7 @@ STDMETHODIMP CEncoder::ResetSalt()
STDMETHODIMP CEncoder::ResetInitVector()
{
_ivSize = 8;
g_RandomGenerator.Generate(_iv, _ivSize);
g_RandomGenerator.Generate(_iv, (unsigned)_ivSize);
return S_OK;
}
@@ -142,7 +147,7 @@ STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
UInt32 ivSize = _ivSize;
// _key.NumCyclesPower = 0x3F;
_key.NumCyclesPower = 18;
_key.NumCyclesPower = 19;
Byte firstByte = (Byte)(_key.NumCyclesPower |
(((_key.SaltSize == 0) ? 0 : 1) << 7) |
@@ -156,11 +161,11 @@ STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
RINOK(outStream->Write(&secondByte, 1, NULL));
if (_key.SaltSize > 0)
{
RINOK(WriteStream(outStream, _key.Salt, _key.SaltSize, NULL));
RINOK(WriteStream(outStream, _key.Salt, _key.SaltSize));
}
if (ivSize > 0)
{
RINOK(WriteStream(outStream, _iv, ivSize, NULL));
RINOK(WriteStream(outStream, _iv, ivSize));
}
return S_OK;
}
@@ -208,8 +213,8 @@ STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size)
{
_key.Password.SetCapacity(size);
memcpy(_key.Password, data, size);
_key.Password.SetCapacity((size_t)size);
memcpy(_key.Password, data, (size_t)size);
return S_OK;
}

View File

@@ -12,14 +12,14 @@ STDMETHODIMP CAesCbcEncoder::Init() { return S_OK; }
STDMETHODIMP_(UInt32) CAesCbcEncoder::Filter(Byte *data, UInt32 size)
{
return AesCbcEncode(&Aes, data, size);
return (UInt32)AesCbc_Encode(&Aes, data, size);
}
STDMETHODIMP CAesCbcEncoder::SetKey(const Byte *data, UInt32 size)
{
if ((size & 0x7) != 0 || size < 16 || size > 32)
return E_INVALIDARG;
AesSetKeyEncode(&Aes.aes, data, size);
Aes_SetKeyEncode(&Aes.aes, data, size);
return S_OK;
}
@@ -27,7 +27,7 @@ STDMETHODIMP CAesCbcEncoder::SetInitVector(const Byte *data, UInt32 size)
{
if (size != AES_BLOCK_SIZE)
return E_INVALIDARG;
AesCbcInit(&Aes, data);
AesCbc_Init(&Aes, data);
return S_OK;
}
@@ -35,14 +35,14 @@ STDMETHODIMP CAesCbcDecoder::Init() { return S_OK; }
STDMETHODIMP_(UInt32) CAesCbcDecoder::Filter(Byte *data, UInt32 size)
{
return AesCbcDecode(&Aes, data, size);
return (UInt32)AesCbc_Decode(&Aes, data, size);
}
STDMETHODIMP CAesCbcDecoder::SetKey(const Byte *data, UInt32 size)
{
if ((size & 0x7) != 0 || size < 16 || size > 32)
return E_INVALIDARG;
AesSetKeyDecode(&Aes.aes, data, size);
Aes_SetKeyDecode(&Aes.aes, data, size);
return S_OK;
}
@@ -50,7 +50,7 @@ STDMETHODIMP CAesCbcDecoder::SetInitVector(const Byte *data, UInt32 size)
{
if (size != AES_BLOCK_SIZE)
return E_INVALIDARG;
AesCbcInit(&Aes, data);
AesCbc_Init(&Aes, data);
return S_OK;
}

View File

@@ -10,7 +10,7 @@
extern "C"
{
#include "../../../../C/Crypto/Aes.h"
#include "../../../../C/Aes.h"
}
namespace NCrypto {

View File

@@ -1,19 +0,0 @@
// RotateDefs.h
#ifndef __ROTATEDEFS_H
#define __ROTATEDEFS_H
#ifdef _MSC_VER
#include <stddef.h>
#define rotlFixed(x, n) _rotl((x), (n))
#define rotrFixed(x, n) _rotr((x), (n))
#else
#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
#endif
#endif

View File

@@ -5,7 +5,10 @@
#include "StdAfx.h"
#include "Sha1.h"
#include "RotateDefs.h"
extern "C"
{
#include "../../../../C/RotateDefs.h"
}
namespace NCrypto {
namespace NSha1 {

View File

@@ -1,210 +0,0 @@
// Crypto/Sha256.cpp
// This code is based on code from Wei Dai's Crypto++ library.
#include "StdAfx.h"
#include "Sha256.h"
#include "RotateDefs.h"
namespace NCrypto {
namespace NSha256 {
// define it for speed optimization
// #define _SHA256_UNROLL
// #define _SHA256_UNROLL2
void CContext::Init()
{
_state[0] = 0x6a09e667;
_state[1] = 0xbb67ae85;
_state[2] = 0x3c6ef372;
_state[3] = 0xa54ff53a;
_state[4] = 0x510e527f;
_state[5] = 0x9b05688c;
_state[6] = 0x1f83d9ab;
_state[7] = 0x5be0cd19;
_count = 0;
}
#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))
#define blk0(i) (W[i] = data[i])
#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]
#ifdef _SHA256_UNROLL2
#define R(a,b,c,d,e,f,g,h, i) h += S1(e) + Ch(e,f,g) + K[i+j] + (j?blk2(i):blk0(i));\
d += h; h += S0(a) + Maj(a, b, c)
#define RX_8(i) \
R(a,b,c,d,e,f,g,h, i); \
R(h,a,b,c,d,e,f,g, i+1); \
R(g,h,a,b,c,d,e,f, i+2); \
R(f,g,h,a,b,c,d,e, i+3); \
R(e,f,g,h,a,b,c,d, i+4); \
R(d,e,f,g,h,a,b,c, i+5); \
R(c,d,e,f,g,h,a,b, i+6); \
R(b,c,d,e,f,g,h,a, i+7)
#else
#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))
#ifdef _SHA256_UNROLL
#define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7);
#endif
#endif
void CContext::Transform(UInt32 *state, const UInt32 *data)
{
UInt32 W[16];
#ifdef _SHA256_UNROLL2
UInt32 a,b,c,d,e,f,g,h;
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
f = state[5];
g = state[6];
h = state[7];
#else
UInt32 T[8];
for (int s = 0; s < 8; s++)
T[s] = state[s];
#endif
for (unsigned int j = 0; j < 64; j += 16)
{
#if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2)
RX_8(0); RX_8(8);
#else
for (unsigned int i = 0; i < 16; i++) { R(i); }
#endif
}
#ifdef _SHA256_UNROLL2
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
state[5] += f;
state[6] += g;
state[7] += h;
#else
for (int i = 0; i < 8; i++)
state[i] += T[i];
#endif
// Wipe variables
// memset(W, 0, sizeof(W));
// memset(T, 0, sizeof(T));
}
const UInt32 CContext::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 CContext::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(_state, data32);
}
void CContext::Update(const Byte *data, size_t size)
{
UInt32 curBufferPos = (UInt32)_count & 0x3F;
while (size > 0)
{
_buffer[curBufferPos++] = *data++;
_count++;
size--;
if (curBufferPos == 64)
{
curBufferPos = 0;
WriteByteBlock();
}
}
}
void CContext::Final(Byte *digest)
{
UInt64 lenInBits = (_count << 3);
UInt32 curBufferPos = (UInt32)_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++ = (Byte)(_state[j] >> 24);
*digest++ = (Byte)(_state[j] >> 16);
*digest++ = (Byte)(_state[j] >> 8);
*digest++ = (Byte)(_state[j]);
}
Init();
}
}}

View File

@@ -1,30 +0,0 @@
// Crypto/Sha256.h
#ifndef __CRYPTO_SHA256_H
#define __CRYPTO_SHA256_H
#include "Common/Types.h"
namespace NCrypto {
namespace NSha256 {
class CContext
{
static const UInt32 K[64];
UInt32 _state[8];
UInt64 _count;
Byte _buffer[64];
static void Transform(UInt32 *digest, const UInt32 *data);
void WriteByteBlock();
public:
enum {DIGESTSIZE = 32};
CContext() { Init(); } ;
void Init();
void Update(const Byte *data, size_t size);
void Final(Byte *digest);
};
}}
#endif

View File

@@ -76,14 +76,14 @@ STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size)
STDMETHODIMP CDecoder::Init()
{
Calculate();
AesSetKeyDecode(&Aes.aes, aesKey, kRarAesKeySize);
AesCbcInit(&Aes, aesInit);
Aes_SetKeyDecode(&Aes.aes, aesKey, kRarAesKeySize);
AesCbc_Init(&Aes, aesInit);
return S_OK;
}
STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size)
{
return AesCbcDecode(&Aes, data, size);
return (UInt32)AesCbc_Decode(&Aes, data, size);
}
void CDecoder::Calculate()

View File

@@ -12,7 +12,7 @@
extern "C"
{
#include "../../../../C/Crypto/Aes.h"
#include "../../../../C/Aes.h"
}
namespace NCrypto {

View File

@@ -47,12 +47,12 @@ void CBaseCoder::EncryptData(Byte *data, UInt32 size)
{
if (++_counter[0] == 0)
_counter[1]++;
UInt32 outBuf[4];
AesEncode32(_counter, outBuf, Aes.rkey, Aes.numRounds2);
SetUi32(_buffer, outBuf[0]);
SetUi32(_buffer + 4, outBuf[1]);
SetUi32(_buffer + 8, outBuf[2]);
SetUi32(_buffer + 12, outBuf[3]);
UInt32 temp[4];
Aes_Encode32(&Aes, temp, _counter);
SetUi32(_buffer, temp[0]);
SetUi32(_buffer + 4, temp[1]);
SetUi32(_buffer + 8, temp[2]);
SetUi32(_buffer + 12, temp[3]);
pos = 0;
}
*data++ ^= _buffer[pos++];
@@ -115,17 +115,10 @@ STDMETHODIMP CBaseCoder::Init()
for (int i = 0; i < 4; i++)
_counter[i] = 0;
AesSetKeyEncode(&Aes, buf, keySize);
Aes_SetKeyEncode(&Aes, buf, keySize);
return S_OK;
}
static HRESULT SafeWrite(ISequentialOutStream *outStream, const Byte *data, UInt32 size)
{
UInt32 processedSize;
RINOK(WriteStream(outStream, data, size, &processedSize));
return ((processedSize == size) ? S_OK : E_FAIL);
}
/*
STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
{
@@ -139,15 +132,15 @@ HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream)
UInt32 saltSize = _key.GetSaltSize();
g_RandomGenerator.Generate(_key.Salt, saltSize);
Init();
RINOK(SafeWrite(outStream, _key.Salt, saltSize));
return SafeWrite(outStream, _key.PwdVerifComputed, kPwdVerifCodeSize);
RINOK(WriteStream(outStream, _key.Salt, saltSize));
return WriteStream(outStream, _key.PwdVerifComputed, kPwdVerifCodeSize);
}
HRESULT CEncoder::WriteFooter(ISequentialOutStream *outStream)
{
Byte mac[kMacSize];
_hmac.Final(mac, kMacSize);
return SafeWrite(outStream, mac, kMacSize);
return WriteStream(outStream, mac, kMacSize);
}
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
@@ -167,10 +160,7 @@ HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream)
UInt32 saltSize = _key.GetSaltSize();
UInt32 extraSize = saltSize + kPwdVerifCodeSize;
Byte temp[kSaltSizeMax + kPwdVerifCodeSize];
UInt32 processedSize;
RINOK(ReadStream(inStream, temp, extraSize, &processedSize));
if (processedSize != extraSize)
return E_FAIL;
RINOK(ReadStream_FAIL(inStream, temp, extraSize));
UInt32 i;
for (i = 0; i < saltSize; i++)
_key.Salt[i] = temp[i];
@@ -195,11 +185,8 @@ bool CDecoder::CheckPasswordVerifyCode()
HRESULT CDecoder::CheckMac(ISequentialInStream *inStream, bool &isOK)
{
isOK = false;
UInt32 processedSize;
Byte mac1[kMacSize];
RINOK(ReadStream(inStream, mac1, kMacSize, &processedSize));
if (processedSize != kMacSize)
return E_FAIL;
RINOK(ReadStream_FAIL(inStream, mac1, kMacSize));
Byte mac2[kMacSize];
_hmac.Final(mac2, kMacSize);
isOK = CompareArrays(mac1, mac2, kMacSize);

View File

@@ -23,7 +23,7 @@ specified in password Based File Encryption Utility:
extern "C"
{
#include "../../../../C/Crypto/Aes.h"
#include "../../../../C/Aes.h"
}
namespace NCrypto {

View File

@@ -36,12 +36,8 @@ HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream)
header[kHeaderSize - 1] = Byte(_crc >> 24);
header[kHeaderSize - 2] = Byte(_crc >> 16);
UInt32 processedSize;
_cipher.EncryptHeader(header);
RINOK(WriteStream(outStream, header, kHeaderSize, &processedSize));
if (processedSize != kHeaderSize)
return E_FAIL;
return S_OK;
return WriteStream(outStream, header, kHeaderSize);
}
STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size)
@@ -61,10 +57,7 @@ STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size)
HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream)
{
Byte header[kHeaderSize];
UInt32 processedSize;
RINOK(ReadStream(inStream, header, kHeaderSize, &processedSize));
if (processedSize != kHeaderSize)
return E_FAIL;
RINOK(ReadStream_FAIL(inStream, header, kHeaderSize));
_cipher.DecryptHeader(header);
return S_OK;
}