mz_strm_pkcrypt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* mz_strm_pkcrypt.c -- Code for traditional PKWARE encryption
  2. Version 2.9.2, February 12, 2020
  3. part of the MiniZip project
  4. Copyright (C) 2010-2020 Nathan Moinvaziri
  5. https://github.com/nmoinvaz/minizip
  6. Copyright (C) 1998-2005 Gilles Vollant
  7. Modifications for Info-ZIP crypting
  8. https://www.winimage.com/zLibDll/minizip.html
  9. Copyright (C) 2003 Terry Thorsen
  10. This code is a modified version of crypting code in Info-ZIP distribution
  11. Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
  12. This program is distributed under the terms of the same license as zlib.
  13. See the accompanying LICENSE file for the full text of the license.
  14. This encryption code is a direct transcription of the algorithm from
  15. Roger Schlafly, described by Phil Katz in the file appnote.txt. This
  16. file (appnote.txt) is distributed with the PKZIP program (even in the
  17. version without encryption capabilities).
  18. */
  19. #include "mz.h"
  20. #include "mz_crypt.h"
  21. #include "mz_strm.h"
  22. #include "mz_strm_pkcrypt.h"
  23. /***************************************************************************/
  24. static mz_stream_vtbl mz_stream_pkcrypt_vtbl = {
  25. mz_stream_pkcrypt_open,
  26. mz_stream_pkcrypt_is_open,
  27. mz_stream_pkcrypt_read,
  28. mz_stream_pkcrypt_write,
  29. mz_stream_pkcrypt_tell,
  30. mz_stream_pkcrypt_seek,
  31. mz_stream_pkcrypt_close,
  32. mz_stream_pkcrypt_error,
  33. mz_stream_pkcrypt_create,
  34. mz_stream_pkcrypt_delete,
  35. mz_stream_pkcrypt_get_prop_int64,
  36. mz_stream_pkcrypt_set_prop_int64
  37. };
  38. /***************************************************************************/
  39. typedef struct mz_stream_pkcrypt_s {
  40. mz_stream stream;
  41. int32_t error;
  42. int16_t initialized;
  43. uint8_t buffer[UINT16_MAX];
  44. int64_t total_in;
  45. int64_t max_total_in;
  46. int64_t total_out;
  47. uint32_t keys[3]; /* keys defining the pseudo-random sequence */
  48. uint8_t verify1;
  49. uint8_t verify2;
  50. const char *password;
  51. } mz_stream_pkcrypt;
  52. /***************************************************************************/
  53. #define mz_stream_pkcrypt_decode(strm, c) \
  54. (mz_stream_pkcrypt_update_keys(strm, \
  55. c ^= mz_stream_pkcrypt_decrypt_byte(strm)))
  56. #define mz_stream_pkcrypt_encode(strm, c, t) \
  57. (t = mz_stream_pkcrypt_decrypt_byte(strm), \
  58. mz_stream_pkcrypt_update_keys(strm, (uint8_t)c), (uint8_t)(t^(c)))
  59. /***************************************************************************/
  60. static uint8_t mz_stream_pkcrypt_decrypt_byte(void *stream)
  61. {
  62. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  63. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an */
  64. /* unpredictable manner on 16-bit systems; not a problem */
  65. /* with any known compiler so far, though. */
  66. temp = pkcrypt->keys[2] | 2;
  67. return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
  68. }
  69. static uint8_t mz_stream_pkcrypt_update_keys(void *stream, uint8_t c)
  70. {
  71. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  72. uint8_t buf = c;
  73. pkcrypt->keys[0] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[0], &buf, 1);
  74. pkcrypt->keys[1] += pkcrypt->keys[0] & 0xff;
  75. pkcrypt->keys[1] *= 134775813L;
  76. pkcrypt->keys[1] += 1;
  77. buf = (uint8_t)(pkcrypt->keys[1] >> 24);
  78. pkcrypt->keys[2] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[2], &buf, 1);
  79. return (uint8_t)c;
  80. }
  81. static void mz_stream_pkcrypt_init_keys(void *stream, const char *password)
  82. {
  83. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  84. pkcrypt->keys[0] = 305419896L;
  85. pkcrypt->keys[1] = 591751049L;
  86. pkcrypt->keys[2] = 878082192L;
  87. while (*password != 0)
  88. {
  89. mz_stream_pkcrypt_update_keys(stream, (uint8_t)*password);
  90. password += 1;
  91. }
  92. }
  93. /***************************************************************************/
  94. int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
  95. {
  96. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  97. uint16_t t = 0;
  98. int16_t i = 0;
  99. uint8_t verify1 = 0;
  100. uint8_t verify2 = 0;
  101. uint8_t header[MZ_PKCRYPT_HEADER_SIZE];
  102. const char *password = path;
  103. pkcrypt->total_in = 0;
  104. pkcrypt->total_out = 0;
  105. pkcrypt->initialized = 0;
  106. if (mz_stream_is_open(pkcrypt->stream.base) != MZ_OK)
  107. return MZ_OPEN_ERROR;
  108. if (password == NULL)
  109. password = pkcrypt->password;
  110. if (password == NULL)
  111. return MZ_PARAM_ERROR;
  112. mz_stream_pkcrypt_init_keys(stream, password);
  113. if (mode & MZ_OPEN_MODE_WRITE)
  114. {
  115. #ifdef MZ_ZIP_NO_COMPRESSION
  116. MZ_UNUSED(t);
  117. MZ_UNUSED(i);
  118. return MZ_SUPPORT_ERROR;
  119. #else
  120. /* First generate RAND_HEAD_LEN - 2 random bytes. */
  121. mz_crypt_rand(header, MZ_PKCRYPT_HEADER_SIZE - 2);
  122. /* Encrypt random header (last two bytes is high word of crc) */
  123. for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
  124. header[i] = mz_stream_pkcrypt_encode(stream, header[i], t);
  125. header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify1, t);
  126. header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify2, t);
  127. if (mz_stream_write(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
  128. return MZ_WRITE_ERROR;
  129. pkcrypt->total_out += MZ_PKCRYPT_HEADER_SIZE;
  130. #endif
  131. }
  132. else if (mode & MZ_OPEN_MODE_READ)
  133. {
  134. #ifdef MZ_ZIP_NO_DECOMPRESSION
  135. MZ_UNUSED(t);
  136. MZ_UNUSED(i);
  137. MZ_UNUSED(verify1);
  138. MZ_UNUSED(verify2);
  139. return MZ_SUPPORT_ERROR;
  140. #else
  141. if (mz_stream_read(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
  142. return MZ_READ_ERROR;
  143. for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
  144. header[i] = mz_stream_pkcrypt_decode(stream, header[i]);
  145. verify1 = mz_stream_pkcrypt_decode(stream, header[i++]);
  146. verify2 = mz_stream_pkcrypt_decode(stream, header[i++]);
  147. /* Older versions used 2 byte check, newer versions use 1 byte check. */
  148. MZ_UNUSED(verify1);
  149. if ((verify2 != 0) && (verify2 != pkcrypt->verify2))
  150. return MZ_PASSWORD_ERROR;
  151. pkcrypt->total_in += MZ_PKCRYPT_HEADER_SIZE;
  152. #endif
  153. }
  154. pkcrypt->initialized = 1;
  155. return MZ_OK;
  156. }
  157. int32_t mz_stream_pkcrypt_is_open(void *stream)
  158. {
  159. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  160. if (pkcrypt->initialized == 0)
  161. return MZ_OPEN_ERROR;
  162. return MZ_OK;
  163. }
  164. int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size)
  165. {
  166. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  167. uint8_t *buf_ptr = (uint8_t *)buf;
  168. int32_t bytes_to_read = size;
  169. int32_t read = 0;
  170. int32_t i = 0;
  171. if ((int64_t)bytes_to_read > (pkcrypt->max_total_in - pkcrypt->total_in))
  172. bytes_to_read = (int32_t)(pkcrypt->max_total_in - pkcrypt->total_in);
  173. read = mz_stream_read(pkcrypt->stream.base, buf, bytes_to_read);
  174. for (i = 0; i < read; i++)
  175. buf_ptr[i] = mz_stream_pkcrypt_decode(stream, buf_ptr[i]);
  176. if (read > 0)
  177. pkcrypt->total_in += read;
  178. return read;
  179. }
  180. int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size)
  181. {
  182. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  183. const uint8_t *buf_ptr = (const uint8_t *)buf;
  184. int32_t bytes_to_write = sizeof(pkcrypt->buffer);
  185. int32_t total_written = 0;
  186. int32_t written = 0;
  187. int32_t i = 0;
  188. uint16_t t = 0;
  189. if (size < 0)
  190. return MZ_PARAM_ERROR;
  191. do
  192. {
  193. if (bytes_to_write > (size - total_written))
  194. bytes_to_write = (size - total_written);
  195. for (i = 0; i < bytes_to_write; i += 1)
  196. {
  197. pkcrypt->buffer[i] = mz_stream_pkcrypt_encode(stream, *buf_ptr, t);
  198. buf_ptr += 1;
  199. }
  200. written = mz_stream_write(pkcrypt->stream.base, pkcrypt->buffer, bytes_to_write);
  201. if (written < 0)
  202. return written;
  203. total_written += written;
  204. }
  205. while (total_written < size && written > 0);
  206. pkcrypt->total_out += total_written;
  207. return total_written;
  208. }
  209. int64_t mz_stream_pkcrypt_tell(void *stream)
  210. {
  211. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  212. return mz_stream_tell(pkcrypt->stream.base);
  213. }
  214. int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin)
  215. {
  216. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  217. return mz_stream_seek(pkcrypt->stream.base, offset, origin);
  218. }
  219. int32_t mz_stream_pkcrypt_close(void *stream)
  220. {
  221. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  222. pkcrypt->initialized = 0;
  223. return MZ_OK;
  224. }
  225. int32_t mz_stream_pkcrypt_error(void *stream)
  226. {
  227. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  228. return pkcrypt->error;
  229. }
  230. void mz_stream_pkcrypt_set_password(void *stream, const char *password)
  231. {
  232. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  233. pkcrypt->password = password;
  234. }
  235. void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2)
  236. {
  237. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  238. pkcrypt->verify1 = verify1;
  239. pkcrypt->verify2 = verify2;
  240. }
  241. void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2)
  242. {
  243. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  244. *verify1 = pkcrypt->verify1;
  245. *verify2 = pkcrypt->verify2;
  246. }
  247. int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value)
  248. {
  249. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  250. switch (prop)
  251. {
  252. case MZ_STREAM_PROP_TOTAL_IN:
  253. *value = pkcrypt->total_in;
  254. break;
  255. case MZ_STREAM_PROP_TOTAL_OUT:
  256. *value = pkcrypt->total_out;
  257. break;
  258. case MZ_STREAM_PROP_TOTAL_IN_MAX:
  259. *value = pkcrypt->max_total_in;
  260. break;
  261. case MZ_STREAM_PROP_HEADER_SIZE:
  262. *value = MZ_PKCRYPT_HEADER_SIZE;
  263. break;
  264. case MZ_STREAM_PROP_FOOTER_SIZE:
  265. *value = 0;
  266. break;
  267. default:
  268. return MZ_EXIST_ERROR;
  269. }
  270. return MZ_OK;
  271. }
  272. int32_t mz_stream_pkcrypt_set_prop_int64(void *stream, int32_t prop, int64_t value)
  273. {
  274. mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
  275. switch (prop)
  276. {
  277. case MZ_STREAM_PROP_TOTAL_IN_MAX:
  278. pkcrypt->max_total_in = value;
  279. break;
  280. default:
  281. return MZ_EXIST_ERROR;
  282. }
  283. return MZ_OK;
  284. }
  285. void *mz_stream_pkcrypt_create(void **stream)
  286. {
  287. mz_stream_pkcrypt *pkcrypt = NULL;
  288. pkcrypt = (mz_stream_pkcrypt *)MZ_ALLOC(sizeof(mz_stream_pkcrypt));
  289. if (pkcrypt != NULL)
  290. {
  291. memset(pkcrypt, 0, sizeof(mz_stream_pkcrypt));
  292. pkcrypt->stream.vtbl = &mz_stream_pkcrypt_vtbl;
  293. }
  294. if (stream != NULL)
  295. *stream = pkcrypt;
  296. return pkcrypt;
  297. }
  298. void mz_stream_pkcrypt_delete(void **stream)
  299. {
  300. mz_stream_pkcrypt *pkcrypt = NULL;
  301. if (stream == NULL)
  302. return;
  303. pkcrypt = (mz_stream_pkcrypt *)*stream;
  304. if (pkcrypt != NULL)
  305. MZ_FREE(pkcrypt);
  306. *stream = NULL;
  307. }
  308. void *mz_stream_pkcrypt_get_interface(void)
  309. {
  310. return (void *)&mz_stream_pkcrypt_vtbl;
  311. }