Debian Codegraph @

mz_strm_wzaes.c

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