TraditionalPkwareEncryptionEngine.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. namespace PhpZip\Crypto;
  3. use PhpZip\Exception\RuntimeException;
  4. use PhpZip\Exception\ZipAuthenticationException;
  5. use PhpZip\Exception\ZipCryptoException;
  6. use PhpZip\Model\ZipEntry;
  7. use PhpZip\Util\PackUtil;
  8. /**
  9. * Traditional PKWARE Encryption Engine.
  10. *
  11. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  12. *
  13. * @author Ne-Lexa alexey@nelexa.ru
  14. * @license MIT
  15. */
  16. class TraditionalPkwareEncryptionEngine implements ZipEncryptionEngine
  17. {
  18. /** Encryption header size */
  19. const STD_DEC_HDR_SIZE = 12;
  20. /**
  21. * Crc table.
  22. *
  23. * @var array
  24. */
  25. private static $CRC_TABLE = [
  26. 0x00000000,
  27. 0x77073096,
  28. 0xee0e612c,
  29. 0x990951ba,
  30. 0x076dc419,
  31. 0x706af48f,
  32. 0xe963a535,
  33. 0x9e6495a3,
  34. 0x0edb8832,
  35. 0x79dcb8a4,
  36. 0xe0d5e91e,
  37. 0x97d2d988,
  38. 0x09b64c2b,
  39. 0x7eb17cbd,
  40. 0xe7b82d07,
  41. 0x90bf1d91,
  42. 0x1db71064,
  43. 0x6ab020f2,
  44. 0xf3b97148,
  45. 0x84be41de,
  46. 0x1adad47d,
  47. 0x6ddde4eb,
  48. 0xf4d4b551,
  49. 0x83d385c7,
  50. 0x136c9856,
  51. 0x646ba8c0,
  52. 0xfd62f97a,
  53. 0x8a65c9ec,
  54. 0x14015c4f,
  55. 0x63066cd9,
  56. 0xfa0f3d63,
  57. 0x8d080df5,
  58. 0x3b6e20c8,
  59. 0x4c69105e,
  60. 0xd56041e4,
  61. 0xa2677172,
  62. 0x3c03e4d1,
  63. 0x4b04d447,
  64. 0xd20d85fd,
  65. 0xa50ab56b,
  66. 0x35b5a8fa,
  67. 0x42b2986c,
  68. 0xdbbbc9d6,
  69. 0xacbcf940,
  70. 0x32d86ce3,
  71. 0x45df5c75,
  72. 0xdcd60dcf,
  73. 0xabd13d59,
  74. 0x26d930ac,
  75. 0x51de003a,
  76. 0xc8d75180,
  77. 0xbfd06116,
  78. 0x21b4f4b5,
  79. 0x56b3c423,
  80. 0xcfba9599,
  81. 0xb8bda50f,
  82. 0x2802b89e,
  83. 0x5f058808,
  84. 0xc60cd9b2,
  85. 0xb10be924,
  86. 0x2f6f7c87,
  87. 0x58684c11,
  88. 0xc1611dab,
  89. 0xb6662d3d,
  90. 0x76dc4190,
  91. 0x01db7106,
  92. 0x98d220bc,
  93. 0xefd5102a,
  94. 0x71b18589,
  95. 0x06b6b51f,
  96. 0x9fbfe4a5,
  97. 0xe8b8d433,
  98. 0x7807c9a2,
  99. 0x0f00f934,
  100. 0x9609a88e,
  101. 0xe10e9818,
  102. 0x7f6a0dbb,
  103. 0x086d3d2d,
  104. 0x91646c97,
  105. 0xe6635c01,
  106. 0x6b6b51f4,
  107. 0x1c6c6162,
  108. 0x856530d8,
  109. 0xf262004e,
  110. 0x6c0695ed,
  111. 0x1b01a57b,
  112. 0x8208f4c1,
  113. 0xf50fc457,
  114. 0x65b0d9c6,
  115. 0x12b7e950,
  116. 0x8bbeb8ea,
  117. 0xfcb9887c,
  118. 0x62dd1ddf,
  119. 0x15da2d49,
  120. 0x8cd37cf3,
  121. 0xfbd44c65,
  122. 0x4db26158,
  123. 0x3ab551ce,
  124. 0xa3bc0074,
  125. 0xd4bb30e2,
  126. 0x4adfa541,
  127. 0x3dd895d7,
  128. 0xa4d1c46d,
  129. 0xd3d6f4fb,
  130. 0x4369e96a,
  131. 0x346ed9fc,
  132. 0xad678846,
  133. 0xda60b8d0,
  134. 0x44042d73,
  135. 0x33031de5,
  136. 0xaa0a4c5f,
  137. 0xdd0d7cc9,
  138. 0x5005713c,
  139. 0x270241aa,
  140. 0xbe0b1010,
  141. 0xc90c2086,
  142. 0x5768b525,
  143. 0x206f85b3,
  144. 0xb966d409,
  145. 0xce61e49f,
  146. 0x5edef90e,
  147. 0x29d9c998,
  148. 0xb0d09822,
  149. 0xc7d7a8b4,
  150. 0x59b33d17,
  151. 0x2eb40d81,
  152. 0xb7bd5c3b,
  153. 0xc0ba6cad,
  154. 0xedb88320,
  155. 0x9abfb3b6,
  156. 0x03b6e20c,
  157. 0x74b1d29a,
  158. 0xead54739,
  159. 0x9dd277af,
  160. 0x04db2615,
  161. 0x73dc1683,
  162. 0xe3630b12,
  163. 0x94643b84,
  164. 0x0d6d6a3e,
  165. 0x7a6a5aa8,
  166. 0xe40ecf0b,
  167. 0x9309ff9d,
  168. 0x0a00ae27,
  169. 0x7d079eb1,
  170. 0xf00f9344,
  171. 0x8708a3d2,
  172. 0x1e01f268,
  173. 0x6906c2fe,
  174. 0xf762575d,
  175. 0x806567cb,
  176. 0x196c3671,
  177. 0x6e6b06e7,
  178. 0xfed41b76,
  179. 0x89d32be0,
  180. 0x10da7a5a,
  181. 0x67dd4acc,
  182. 0xf9b9df6f,
  183. 0x8ebeeff9,
  184. 0x17b7be43,
  185. 0x60b08ed5,
  186. 0xd6d6a3e8,
  187. 0xa1d1937e,
  188. 0x38d8c2c4,
  189. 0x4fdff252,
  190. 0xd1bb67f1,
  191. 0xa6bc5767,
  192. 0x3fb506dd,
  193. 0x48b2364b,
  194. 0xd80d2bda,
  195. 0xaf0a1b4c,
  196. 0x36034af6,
  197. 0x41047a60,
  198. 0xdf60efc3,
  199. 0xa867df55,
  200. 0x316e8eef,
  201. 0x4669be79,
  202. 0xcb61b38c,
  203. 0xbc66831a,
  204. 0x256fd2a0,
  205. 0x5268e236,
  206. 0xcc0c7795,
  207. 0xbb0b4703,
  208. 0x220216b9,
  209. 0x5505262f,
  210. 0xc5ba3bbe,
  211. 0xb2bd0b28,
  212. 0x2bb45a92,
  213. 0x5cb36a04,
  214. 0xc2d7ffa7,
  215. 0xb5d0cf31,
  216. 0x2cd99e8b,
  217. 0x5bdeae1d,
  218. 0x9b64c2b0,
  219. 0xec63f226,
  220. 0x756aa39c,
  221. 0x026d930a,
  222. 0x9c0906a9,
  223. 0xeb0e363f,
  224. 0x72076785,
  225. 0x05005713,
  226. 0x95bf4a82,
  227. 0xe2b87a14,
  228. 0x7bb12bae,
  229. 0x0cb61b38,
  230. 0x92d28e9b,
  231. 0xe5d5be0d,
  232. 0x7cdcefb7,
  233. 0x0bdbdf21,
  234. 0x86d3d2d4,
  235. 0xf1d4e242,
  236. 0x68ddb3f8,
  237. 0x1fda836e,
  238. 0x81be16cd,
  239. 0xf6b9265b,
  240. 0x6fb077e1,
  241. 0x18b74777,
  242. 0x88085ae6,
  243. 0xff0f6a70,
  244. 0x66063bca,
  245. 0x11010b5c,
  246. 0x8f659eff,
  247. 0xf862ae69,
  248. 0x616bffd3,
  249. 0x166ccf45,
  250. 0xa00ae278,
  251. 0xd70dd2ee,
  252. 0x4e048354,
  253. 0x3903b3c2,
  254. 0xa7672661,
  255. 0xd06016f7,
  256. 0x4969474d,
  257. 0x3e6e77db,
  258. 0xaed16a4a,
  259. 0xd9d65adc,
  260. 0x40df0b66,
  261. 0x37d83bf0,
  262. 0xa9bcae53,
  263. 0xdebb9ec5,
  264. 0x47b2cf7f,
  265. 0x30b5ffe9,
  266. 0xbdbdf21c,
  267. 0xcabac28a,
  268. 0x53b39330,
  269. 0x24b4a3a6,
  270. 0xbad03605,
  271. 0xcdd70693,
  272. 0x54de5729,
  273. 0x23d967bf,
  274. 0xb3667a2e,
  275. 0xc4614ab8,
  276. 0x5d681b02,
  277. 0x2a6f2b94,
  278. 0xb40bbe37,
  279. 0xc30c8ea1,
  280. 0x5a05df1b,
  281. 0x2d02ef8d,
  282. ];
  283. /**
  284. * Encryption keys.
  285. *
  286. * @var array
  287. */
  288. private $keys = [];
  289. /** @var ZipEntry */
  290. private $entry;
  291. /**
  292. * ZipCryptoEngine constructor.
  293. *
  294. * @param ZipEntry $entry
  295. */
  296. public function __construct(ZipEntry $entry)
  297. {
  298. $this->entry = $entry;
  299. }
  300. /**
  301. * Initial keys.
  302. *
  303. * @param string $password
  304. */
  305. private function initKeys($password)
  306. {
  307. $this->keys[0] = 305419896;
  308. $this->keys[1] = 591751049;
  309. $this->keys[2] = 878082192;
  310. foreach (unpack('C*', $password) as $b) {
  311. $this->updateKeys($b);
  312. }
  313. }
  314. /**
  315. * Update keys.
  316. *
  317. * @param int $charAt
  318. */
  319. private function updateKeys($charAt)
  320. {
  321. $this->keys[0] = $this->crc32($this->keys[0], $charAt);
  322. $this->keys[1] += ($this->keys[0] & 0xff);
  323. $this->keys[1] = PackUtil::toSignedInt32($this->keys[1] * 134775813 + 1);
  324. $this->keys[2] = PackUtil::toSignedInt32($this->crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff));
  325. }
  326. /**
  327. * Update crc.
  328. *
  329. * @param int $oldCrc
  330. * @param int $charAt
  331. *
  332. * @return int
  333. */
  334. private function crc32($oldCrc, $charAt)
  335. {
  336. return (($oldCrc >> 8) & 0xffffff) ^ self::$CRC_TABLE[($oldCrc ^ $charAt) & 0xff];
  337. }
  338. /**
  339. * @param string $content
  340. *
  341. * @throws ZipAuthenticationException
  342. *
  343. * @return string
  344. */
  345. public function decrypt($content)
  346. {
  347. if (\PHP_INT_SIZE === 4) {
  348. throw new RuntimeException('Traditional PKWARE Encryption is not supported in 32-bit PHP.');
  349. }
  350. $password = $this->entry->getPassword();
  351. $this->initKeys($password);
  352. $headerBytes = array_values(unpack('C*', substr($content, 0, self::STD_DEC_HDR_SIZE)));
  353. $byte = 0;
  354. for ($i = 0; $i < self::STD_DEC_HDR_SIZE; $i++) {
  355. $byte = ($headerBytes[$i] ^ $this->decryptByte()) & 0xff;
  356. $this->updateKeys($byte);
  357. }
  358. if ($this->entry->getGeneralPurposeBitFlag(ZipEntry::GPBF_DATA_DESCRIPTOR)) {
  359. // compare against the file type from extended local headers
  360. $checkByte = ($this->entry->getDosTime() >> 8) & 0xff;
  361. } else {
  362. // compare against the CRC otherwise
  363. $checkByte = ($this->entry->getCrc() >> 24) & 0xff;
  364. }
  365. if ($byte !== $checkByte) {
  366. throw new ZipAuthenticationException(
  367. sprintf(
  368. 'Invalid password for zip entry "%s"',
  369. $this->entry->getName()
  370. )
  371. );
  372. }
  373. $outputContent = '';
  374. foreach (unpack('C*', substr($content, self::STD_DEC_HDR_SIZE)) as $val) {
  375. $val = ($val ^ $this->decryptByte()) & 0xff;
  376. $this->updateKeys($val);
  377. $outputContent .= pack('c', $val);
  378. }
  379. return $outputContent;
  380. }
  381. /**
  382. * Decrypt byte.
  383. *
  384. * @return int
  385. */
  386. private function decryptByte()
  387. {
  388. $temp = $this->keys[2] | 2;
  389. return (($temp * ($temp ^ 1)) >> 8) & 0xffffff;
  390. }
  391. /**
  392. * Encryption data.
  393. *
  394. * @param string $data
  395. *
  396. * @throws ZipCryptoException
  397. *
  398. * @return string
  399. */
  400. public function encrypt($data)
  401. {
  402. if (\PHP_INT_SIZE === 4) {
  403. throw new RuntimeException('Traditional PKWARE Encryption is not supported in 32-bit PHP.');
  404. }
  405. $crc = $this->entry->isDataDescriptorRequired() ?
  406. ($this->entry->getDosTime() & 0x0000ffff) << 16 :
  407. $this->entry->getCrc();
  408. try {
  409. $headerBytes = random_bytes(self::STD_DEC_HDR_SIZE);
  410. } catch (\Exception $e) {
  411. throw new \RuntimeException('Oops, our server is bust and cannot generate any random data.', 1, $e);
  412. }
  413. // Initialize again since the generated bytes were encrypted.
  414. $password = $this->entry->getPassword();
  415. $this->initKeys($password);
  416. $headerBytes[self::STD_DEC_HDR_SIZE - 1] = pack('c', ($crc >> 24) & 0xff);
  417. $headerBytes[self::STD_DEC_HDR_SIZE - 2] = pack('c', ($crc >> 16) & 0xff);
  418. $headerBytes = $this->encryptData($headerBytes);
  419. return $headerBytes . $this->encryptData($data);
  420. }
  421. /**
  422. * @param string $content
  423. *
  424. * @throws ZipCryptoException
  425. *
  426. * @return string
  427. */
  428. private function encryptData($content)
  429. {
  430. if ($content === null) {
  431. throw new ZipCryptoException('content is null');
  432. }
  433. $buff = '';
  434. foreach (unpack('C*', $content) as $val) {
  435. $buff .= pack('c', $this->encryptByte($val));
  436. }
  437. return $buff;
  438. }
  439. /**
  440. * @param int $byte
  441. *
  442. * @return int
  443. */
  444. private function encryptByte($byte)
  445. {
  446. $tempVal = $byte ^ $this->decryptByte() & 0xff;
  447. $this->updateKeys($byte);
  448. return $tempVal;
  449. }
  450. }