Debian Codegraph @

mz_strm_pkcrypt.c

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