mz_strm_wzaes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* mz_strm_wzaes.c -- Stream for WinZip AES 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-2010 Brian Gladman, Worcester, UK
  7. This program is distributed under the terms of the same license as zlib.
  8. See the accompanying LICENSE file for the full text of the license.
  9. */
  10. #include "mz.h"
  11. #include "mz_crypt.h"
  12. #include "mz_strm.h"
  13. #include "mz_strm_wzaes.h"
  14. /***************************************************************************/
  15. #define MZ_AES_KEYING_ITERATIONS (1000)
  16. #define MZ_AES_SALT_LENGTH(MODE) (4 * (MODE & 3) + 4)
  17. #define MZ_AES_SALT_LENGTH_MAX (16)
  18. #define MZ_AES_PW_LENGTH_MAX (128)
  19. #define MZ_AES_PW_VERIFY_SIZE (2)
  20. #define MZ_AES_AUTHCODE_SIZE (10)
  21. /***************************************************************************/
  22. static mz_stream_vtbl mz_stream_wzaes_vtbl = {
  23. mz_stream_wzaes_open,
  24. mz_stream_wzaes_is_open,
  25. mz_stream_wzaes_read,
  26. mz_stream_wzaes_write,
  27. mz_stream_wzaes_tell,
  28. mz_stream_wzaes_seek,
  29. mz_stream_wzaes_close,
  30. mz_stream_wzaes_error,
  31. mz_stream_wzaes_create,
  32. mz_stream_wzaes_delete,
  33. mz_stream_wzaes_get_prop_int64,
  34. mz_stream_wzaes_set_prop_int64
  35. };
  36. /***************************************************************************/
  37. typedef struct mz_stream_wzaes_s {
  38. mz_stream stream;
  39. int32_t mode;
  40. int32_t error;
  41. int16_t initialized;
  42. uint8_t buffer[UINT16_MAX];
  43. int64_t total_in;
  44. int64_t max_total_in;
  45. int64_t total_out;
  46. int16_t encryption_mode;
  47. const char *password;
  48. void *aes;
  49. uint32_t crypt_pos;
  50. uint8_t crypt_block[MZ_AES_BLOCK_SIZE];
  51. void *hmac;
  52. uint8_t nonce[MZ_AES_BLOCK_SIZE];
  53. } mz_stream_wzaes;
  54. /***************************************************************************/
  55. int32_t mz_stream_wzaes_open(void *stream, const char *path, int32_t mode)
  56. {
  57. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  58. uint16_t salt_length = 0;
  59. uint16_t password_length = 0;
  60. uint16_t key_length = 0;
  61. uint8_t kbuf[2 * MZ_AES_KEY_LENGTH_MAX + MZ_AES_PW_VERIFY_SIZE];
  62. uint8_t verify[MZ_AES_PW_VERIFY_SIZE];
  63. uint8_t verify_expected[MZ_AES_PW_VERIFY_SIZE];
  64. uint8_t salt_value[MZ_AES_SALT_LENGTH_MAX];
  65. const char *password = path;
  66. wzaes->total_in = 0;
  67. wzaes->total_out = 0;
  68. wzaes->initialized = 0;
  69. if (mz_stream_is_open(wzaes->stream.base) != MZ_OK)
  70. return MZ_OPEN_ERROR;
  71. if (password == NULL)
  72. password = wzaes->password;
  73. if (password == NULL)
  74. return MZ_PARAM_ERROR;
  75. password_length = (uint16_t)strlen(password);
  76. if (password_length > MZ_AES_PW_LENGTH_MAX)
  77. return MZ_PARAM_ERROR;
  78. if (wzaes->encryption_mode < 1 || wzaes->encryption_mode > 3)
  79. return MZ_PARAM_ERROR;
  80. salt_length = MZ_AES_SALT_LENGTH(wzaes->encryption_mode);
  81. if (mode & MZ_OPEN_MODE_WRITE)
  82. {
  83. #ifdef MZ_ZIP_NO_COMPRESSION
  84. return MZ_SUPPORT_ERROR;
  85. #else
  86. mz_crypt_rand(salt_value, salt_length);
  87. #endif
  88. }
  89. else if (mode & MZ_OPEN_MODE_READ)
  90. {
  91. #ifdef MZ_ZIP_NO_DECOMPRESSION
  92. return MZ_SUPPORT_ERROR;
  93. #else
  94. if (mz_stream_read(wzaes->stream.base, salt_value, salt_length) != salt_length)
  95. return MZ_READ_ERROR;
  96. #endif
  97. }
  98. key_length = MZ_AES_KEY_LENGTH(wzaes->encryption_mode);
  99. /* Derive the encryption and authentication keys and the password verifier */
  100. mz_crypt_pbkdf2((uint8_t *)password, password_length, salt_value, salt_length,
  101. MZ_AES_KEYING_ITERATIONS, kbuf, 2 * key_length + MZ_AES_PW_VERIFY_SIZE);
  102. /* Initialize the encryption nonce and buffer pos */
  103. wzaes->crypt_pos = MZ_AES_BLOCK_SIZE;
  104. memset(wzaes->nonce, 0, sizeof(wzaes->nonce));
  105. /* Initialize for encryption using key 1 */
  106. mz_crypt_aes_reset(wzaes->aes);
  107. mz_crypt_aes_set_mode(wzaes->aes, wzaes->encryption_mode);
  108. mz_crypt_aes_set_encrypt_key(wzaes->aes, kbuf, key_length);
  109. /* Initialize for authentication using key 2 */
  110. mz_crypt_hmac_reset(wzaes->hmac);
  111. mz_crypt_hmac_set_algorithm(wzaes->hmac, MZ_HASH_SHA1);
  112. mz_crypt_hmac_init(wzaes->hmac, kbuf + key_length, key_length);
  113. memcpy(verify, kbuf + (2 * key_length), MZ_AES_PW_VERIFY_SIZE);
  114. if (mode & MZ_OPEN_MODE_WRITE)
  115. {
  116. if (mz_stream_write(wzaes->stream.base, salt_value, salt_length) != salt_length)
  117. return MZ_WRITE_ERROR;
  118. wzaes->total_out += salt_length;
  119. if (mz_stream_write(wzaes->stream.base, verify, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
  120. return MZ_WRITE_ERROR;
  121. wzaes->total_out += MZ_AES_PW_VERIFY_SIZE;
  122. }
  123. else if (mode & MZ_OPEN_MODE_READ)
  124. {
  125. wzaes->total_in += salt_length;
  126. if (mz_stream_read(wzaes->stream.base, verify_expected, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
  127. return MZ_READ_ERROR;
  128. wzaes->total_in += MZ_AES_PW_VERIFY_SIZE;
  129. if (memcmp(verify_expected, verify, MZ_AES_PW_VERIFY_SIZE) != 0)
  130. return MZ_PASSWORD_ERROR;
  131. }
  132. wzaes->mode = mode;
  133. wzaes->initialized = 1;
  134. return MZ_OK;
  135. }
  136. int32_t mz_stream_wzaes_is_open(void *stream)
  137. {
  138. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  139. if (wzaes->initialized == 0)
  140. return MZ_OPEN_ERROR;
  141. return MZ_OK;
  142. }
  143. static int32_t mz_stream_wzaes_ctr_encrypt(void *stream, uint8_t *buf, int32_t size)
  144. {
  145. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  146. uint32_t pos = wzaes->crypt_pos;
  147. uint32_t i = 0;
  148. int32_t err = MZ_OK;
  149. while (i < (uint32_t)size)
  150. {
  151. if (pos == MZ_AES_BLOCK_SIZE)
  152. {
  153. uint32_t j = 0;
  154. /* Increment encryption nonce */
  155. while (j < 8 && !++wzaes->nonce[j])
  156. j += 1;
  157. /* Encrypt the nonce to form next xor buffer */
  158. memcpy(wzaes->crypt_block, wzaes->nonce, MZ_AES_BLOCK_SIZE);
  159. mz_crypt_aes_encrypt(wzaes->aes, wzaes->crypt_block, sizeof(wzaes->crypt_block));
  160. pos = 0;
  161. }
  162. buf[i++] ^= wzaes->crypt_block[pos++];
  163. }
  164. wzaes->crypt_pos = pos;
  165. return err;
  166. }
  167. int32_t mz_stream_wzaes_read(void *stream, void *buf, int32_t size)
  168. {
  169. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  170. int64_t max_total_in = 0;
  171. int32_t bytes_to_read = size;
  172. int32_t read = 0;
  173. max_total_in = wzaes->max_total_in - MZ_AES_FOOTER_SIZE;
  174. if ((int64_t)bytes_to_read > (max_total_in - wzaes->total_in))
  175. bytes_to_read = (int32_t)(max_total_in - wzaes->total_in);
  176. read = mz_stream_read(wzaes->stream.base, buf, bytes_to_read);
  177. if (read > 0)
  178. {
  179. mz_crypt_hmac_update(wzaes->hmac, (uint8_t *)buf, read);
  180. mz_stream_wzaes_ctr_encrypt(stream, (uint8_t *)buf, read);
  181. wzaes->total_in += read;
  182. }
  183. return read;
  184. }
  185. int32_t mz_stream_wzaes_write(void *stream, const void *buf, int32_t size)
  186. {
  187. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  188. const uint8_t *buf_ptr = (const uint8_t *)buf;
  189. int32_t bytes_to_write = sizeof(wzaes->buffer);
  190. int32_t total_written = 0;
  191. int32_t written = 0;
  192. if (size < 0)
  193. return MZ_PARAM_ERROR;
  194. do
  195. {
  196. if (bytes_to_write > (size - total_written))
  197. bytes_to_write = (size - total_written);
  198. memcpy(wzaes->buffer, buf_ptr, bytes_to_write);
  199. buf_ptr += bytes_to_write;
  200. mz_stream_wzaes_ctr_encrypt(stream, (uint8_t *)wzaes->buffer, bytes_to_write);
  201. mz_crypt_hmac_update(wzaes->hmac, wzaes->buffer, bytes_to_write);
  202. written = mz_stream_write(wzaes->stream.base, wzaes->buffer, bytes_to_write);
  203. if (written < 0)
  204. return written;
  205. total_written += written;
  206. }
  207. while (total_written < size && written > 0);
  208. wzaes->total_out += total_written;
  209. return total_written;
  210. }
  211. int64_t mz_stream_wzaes_tell(void *stream)
  212. {
  213. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  214. return mz_stream_tell(wzaes->stream.base);
  215. }
  216. int32_t mz_stream_wzaes_seek(void *stream, int64_t offset, int32_t origin)
  217. {
  218. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  219. return mz_stream_seek(wzaes->stream.base, offset, origin);
  220. }
  221. int32_t mz_stream_wzaes_close(void *stream)
  222. {
  223. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  224. uint8_t expected_hash[MZ_AES_AUTHCODE_SIZE];
  225. uint8_t computed_hash[MZ_HASH_SHA1_SIZE];
  226. mz_crypt_hmac_end(wzaes->hmac, computed_hash, sizeof(computed_hash));
  227. if (wzaes->mode & MZ_OPEN_MODE_WRITE)
  228. {
  229. if (mz_stream_write(wzaes->stream.base, computed_hash, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
  230. return MZ_WRITE_ERROR;
  231. wzaes->total_out += MZ_AES_AUTHCODE_SIZE;
  232. }
  233. else if (wzaes->mode & MZ_OPEN_MODE_READ)
  234. {
  235. if (mz_stream_read(wzaes->stream.base, expected_hash, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
  236. return MZ_READ_ERROR;
  237. wzaes->total_in += MZ_AES_AUTHCODE_SIZE;
  238. /* If entire entry was not read this will fail */
  239. if (memcmp(computed_hash, expected_hash, MZ_AES_AUTHCODE_SIZE) != 0)
  240. return MZ_CRC_ERROR;
  241. }
  242. wzaes->initialized = 0;
  243. return MZ_OK;
  244. }
  245. int32_t mz_stream_wzaes_error(void *stream)
  246. {
  247. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  248. return wzaes->error;
  249. }
  250. void mz_stream_wzaes_set_password(void *stream, const char *password)
  251. {
  252. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  253. wzaes->password = password;
  254. }
  255. void mz_stream_wzaes_set_encryption_mode(void *stream, int16_t encryption_mode)
  256. {
  257. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  258. wzaes->encryption_mode = encryption_mode;
  259. }
  260. int32_t mz_stream_wzaes_get_prop_int64(void *stream, int32_t prop, int64_t *value)
  261. {
  262. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  263. switch (prop)
  264. {
  265. case MZ_STREAM_PROP_TOTAL_IN:
  266. *value = wzaes->total_in;
  267. break;
  268. case MZ_STREAM_PROP_TOTAL_OUT:
  269. *value = wzaes->total_out;
  270. break;
  271. case MZ_STREAM_PROP_TOTAL_IN_MAX:
  272. *value = wzaes->max_total_in;
  273. break;
  274. case MZ_STREAM_PROP_HEADER_SIZE:
  275. *value = MZ_AES_SALT_LENGTH((int64_t)wzaes->encryption_mode) + MZ_AES_PW_VERIFY_SIZE;
  276. break;
  277. case MZ_STREAM_PROP_FOOTER_SIZE:
  278. *value = MZ_AES_AUTHCODE_SIZE;
  279. break;
  280. default:
  281. return MZ_EXIST_ERROR;
  282. }
  283. return MZ_OK;
  284. }
  285. int32_t mz_stream_wzaes_set_prop_int64(void *stream, int32_t prop, int64_t value)
  286. {
  287. mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
  288. switch (prop)
  289. {
  290. case MZ_STREAM_PROP_TOTAL_IN_MAX:
  291. wzaes->max_total_in = value;
  292. break;
  293. default:
  294. return MZ_EXIST_ERROR;
  295. }
  296. return MZ_OK;
  297. }
  298. void *mz_stream_wzaes_create(void **stream)
  299. {
  300. mz_stream_wzaes *wzaes = NULL;
  301. wzaes = (mz_stream_wzaes *)MZ_ALLOC(sizeof(mz_stream_wzaes));
  302. if (wzaes != NULL)
  303. {
  304. memset(wzaes, 0, sizeof(mz_stream_wzaes));
  305. wzaes->stream.vtbl = &mz_stream_wzaes_vtbl;
  306. wzaes->encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
  307. mz_crypt_hmac_create(&wzaes->hmac);
  308. mz_crypt_aes_create(&wzaes->aes);
  309. }
  310. if (stream != NULL)
  311. *stream = wzaes;
  312. return wzaes;
  313. }
  314. void mz_stream_wzaes_delete(void **stream)
  315. {
  316. mz_stream_wzaes *wzaes = NULL;
  317. if (stream == NULL)
  318. return;
  319. wzaes = (mz_stream_wzaes *)*stream;
  320. if (wzaes != NULL)
  321. {
  322. mz_crypt_aes_delete(&wzaes->aes);
  323. mz_crypt_hmac_delete(&wzaes->hmac);
  324. MZ_FREE(wzaes);
  325. }
  326. *stream = NULL;
  327. }
  328. void *mz_stream_wzaes_get_interface(void)
  329. {
  330. return (void *)&mz_stream_wzaes_vtbl;
  331. }