Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ZipCrypto.cpp « Crypto « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 77aff83bd0d1606e535ea739c3c71b5fe6efbf15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Crypto/ZipCrypto.cpp

#include "StdAfx.h"

#include "../../../C/7zCrc.h"

#include "../Common/StreamUtils.h"

#include "RandGen.h"
#include "ZipCrypto.h"

namespace NCrypto {
namespace NZip {

#define UPDATE_KEYS(b) { \
  Key0 = CRC_UPDATE_BYTE(Key0, b); \
  Key1 = (Key1 + (Key0 & 0xFF)) * 0x8088405 + 1; \
  Key2 = CRC_UPDATE_BYTE(Key2, (Byte)(Key1 >> 24)); } \

#define DECRYPT_BYTE_1 UInt32 temp = Key2 | 2;
#define DECRYPT_BYTE_2 ((Byte)((temp * (temp ^ 1)) >> 8))

STDMETHODIMP CCipher::CryptoSetPassword(const Byte *data, UInt32 size)
{
  UInt32 Key0 = 0x12345678;
  UInt32 Key1 = 0x23456789;
  UInt32 Key2 = 0x34567890;
  
  for (UInt32 i = 0; i < size; i++)
    UPDATE_KEYS(data[i]);

  KeyMem0 = Key0;
  KeyMem1 = Key1;
  KeyMem2 = Key2;
  
  return S_OK;
}

STDMETHODIMP CCipher::Init()
{
  return S_OK;
}

HRESULT CEncoder::WriteHeader_Check16(ISequentialOutStream *outStream, UInt16 crc)
{
  Byte h[kHeaderSize];
  
  /* PKZIP before 2.0 used 2 byte CRC check.
     PKZIP 2.0+ used 1 byte CRC check. It's more secure.
     We also use 1 byte CRC. */

  g_RandomGenerator.Generate(h, kHeaderSize - 1);
  // h[kHeaderSize - 2] = (Byte)(crc);
  h[kHeaderSize - 1] = (Byte)(crc >> 8);
  
  RestoreKeys();
  Filter(h, kHeaderSize);
  return WriteStream(outStream, h, kHeaderSize);
}

STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size)
{
  UInt32 Key0 = this->Key0;
  UInt32 Key1 = this->Key1;
  UInt32 Key2 = this->Key2;

  for (UInt32 i = 0; i < size; i++)
  {
    Byte b = data[i];
    DECRYPT_BYTE_1
    data[i] = (Byte)(b ^ DECRYPT_BYTE_2);
    UPDATE_KEYS(b);
  }

  this->Key0 = Key0;
  this->Key1 = Key1;
  this->Key2 = Key2;

  return size;
}

HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream)
{
  return ReadStream_FAIL(inStream, _header, kHeaderSize);
}

void CDecoder::Init_BeforeDecode()
{
  RestoreKeys();
  Filter(_header, kHeaderSize);
}

STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size)
{
  UInt32 Key0 = this->Key0;
  UInt32 Key1 = this->Key1;
  UInt32 Key2 = this->Key2;
  
  for (UInt32 i = 0; i < size; i++)
  {
    DECRYPT_BYTE_1
    Byte b = (Byte)(data[i] ^ DECRYPT_BYTE_2);
    UPDATE_KEYS(b);
    data[i] = b;
  }
  
  this->Key0 = Key0;
  this->Key1 = Key1;
  this->Key2 = Key2;
  
  return size;
}

}}