|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpZip\IO\Filter\Cipher\WinZipAes; |
|
4
|
|
|
|
|
5
|
|
|
use PhpZip\Exception\RuntimeException; |
|
6
|
|
|
use PhpZip\Exception\ZipAuthenticationException; |
|
7
|
|
|
use PhpZip\Util\CryptoUtil; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* WinZip Aes Encryption. |
|
11
|
|
|
* |
|
12
|
|
|
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT APPENDIX E |
|
13
|
|
|
* @see https://www.winzip.com/win/en/aes_info.html |
|
14
|
|
|
* |
|
15
|
|
|
* @author Ne-Lexa [email protected] |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @internal |
|
19
|
|
|
*/ |
|
20
|
|
|
class WinZipAesContext |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var int AES Block size */ |
|
23
|
|
|
const BLOCK_SIZE = self::IV_SIZE; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int Footer size */ |
|
26
|
|
|
const FOOTER_SIZE = 10; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int The iteration count for the derived keys of the cipher, KLAC and MAC. */ |
|
29
|
|
|
const ITERATION_COUNT = 1000; |
|
30
|
|
|
|
|
31
|
|
|
/** @var int Password verifier size */ |
|
32
|
|
|
const PASSWORD_VERIFIER_SIZE = 2; |
|
33
|
|
|
|
|
34
|
|
|
/** @var int IV size */ |
|
35
|
|
|
const IV_SIZE = 16; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $iv; |
|
39
|
|
|
|
|
40
|
|
|
/** @var string */ |
|
41
|
|
|
private $key; |
|
42
|
|
|
|
|
43
|
|
|
/** @var \HashContext|resource */ |
|
44
|
|
|
private $hmacContext; |
|
45
|
|
|
|
|
46
|
|
|
/** @var string */ |
|
47
|
|
|
private $passwordVerifier; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* WinZipAesContext constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param int $encryptionStrengthBits |
|
53
|
|
|
* @param string $password |
|
54
|
|
|
* @param string $salt |
|
55
|
|
|
*/ |
|
56
|
9 |
|
public function __construct($encryptionStrengthBits, $password, $salt) |
|
57
|
|
|
{ |
|
58
|
9 |
|
$encryptionStrengthBits = (int) $encryptionStrengthBits; |
|
59
|
|
|
|
|
60
|
9 |
|
if ($password === '') { |
|
61
|
|
|
throw new RuntimeException('$password is empty'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
9 |
|
if (empty($salt)) { |
|
65
|
|
|
throw new RuntimeException('$salt is empty'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// WinZip 99-character limit https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/ |
|
69
|
9 |
|
$password = substr($password, 0, 99); |
|
70
|
|
|
|
|
71
|
9 |
|
$this->iv = str_repeat("\0", self::IV_SIZE); |
|
72
|
9 |
|
$keyStrengthBytes = (int) ($encryptionStrengthBits / 8); |
|
73
|
9 |
|
$hashLength = $keyStrengthBytes * 2 + self::PASSWORD_VERIFIER_SIZE * 8; |
|
74
|
|
|
|
|
75
|
9 |
|
$hash = hash_pbkdf2( |
|
76
|
9 |
|
'sha1', |
|
77
|
|
|
$password, |
|
78
|
|
|
$salt, |
|
79
|
9 |
|
self::ITERATION_COUNT, |
|
80
|
|
|
$hashLength, |
|
81
|
9 |
|
true |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
9 |
|
$this->key = substr($hash, 0, $keyStrengthBytes); |
|
85
|
9 |
|
$sha1Mac = substr($hash, $keyStrengthBytes, $keyStrengthBytes); |
|
86
|
9 |
|
$this->hmacContext = hash_init('sha1', \HASH_HMAC, $sha1Mac); |
|
87
|
9 |
|
$this->passwordVerifier = substr($hash, 2 * $keyStrengthBytes, self::PASSWORD_VERIFIER_SIZE); |
|
88
|
9 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
9 |
|
public function getPasswordVerifier() |
|
94
|
|
|
{ |
|
95
|
9 |
|
return $this->passwordVerifier; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
9 |
|
public function updateIv() |
|
99
|
|
|
{ |
|
100
|
9 |
|
for ($ivCharIndex = 0; $ivCharIndex < self::IV_SIZE; $ivCharIndex++) { |
|
101
|
9 |
|
$ivByte = \ord($this->iv[$ivCharIndex]); |
|
102
|
|
|
|
|
103
|
9 |
|
if (++$ivByte === 256) { |
|
104
|
|
|
// overflow, set this one to 0, increment next |
|
105
|
1 |
|
$this->iv[$ivCharIndex] = "\0"; |
|
106
|
|
|
} else { |
|
107
|
|
|
// no overflow, just write incremented number back and abort |
|
108
|
9 |
|
$this->iv[$ivCharIndex] = \chr($ivByte); |
|
109
|
|
|
|
|
110
|
9 |
|
break; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
9 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $data |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
8 |
|
public function decryption($data) |
|
121
|
|
|
{ |
|
122
|
8 |
|
hash_update($this->hmacContext, $data); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
8 |
|
return CryptoUtil::decryptAesCtr($data, $this->key, $this->iv); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $data |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
9 |
|
public function encrypt($data) |
|
133
|
|
|
{ |
|
134
|
9 |
|
$encryptionData = CryptoUtil::encryptAesCtr($data, $this->key, $this->iv); |
|
135
|
9 |
|
hash_update($this->hmacContext, $encryptionData); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
9 |
|
return $encryptionData; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $authCode |
|
142
|
|
|
* |
|
143
|
|
|
* @throws ZipAuthenticationException |
|
144
|
|
|
*/ |
|
145
|
8 |
|
public function checkAuthCode($authCode) |
|
146
|
|
|
{ |
|
147
|
8 |
|
$hmac = $this->getHmac(); |
|
148
|
|
|
|
|
149
|
|
|
// check authenticationCode |
|
150
|
8 |
|
if (strcmp($hmac, $authCode) !== 0) { |
|
151
|
1 |
|
throw new ZipAuthenticationException('Authenticated WinZip AES entry content has been tampered with.'); |
|
152
|
|
|
} |
|
153
|
8 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
9 |
|
public function getHmac() |
|
159
|
|
|
{ |
|
160
|
9 |
|
return substr( |
|
161
|
9 |
|
hash_final($this->hmacContext, true), |
|
|
|
|
|
|
162
|
9 |
|
0, |
|
163
|
9 |
|
self::FOOTER_SIZE |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|