Coverage Report

Created: 2025-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/minizip-ng/mz_strm_pkcrypt.c
Line
Count
Source
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
959k
    (mz_stream_pkcrypt_update_keys(strm, c ^= mz_stream_pkcrypt_decrypt_byte(strm)))
58
59
#define mz_stream_pkcrypt_encode(strm, c, t)                                                                           \
60
0
    (t = mz_stream_pkcrypt_decrypt_byte(strm), mz_stream_pkcrypt_update_keys(strm, (uint8_t)c), (uint8_t)(t ^ (c)))
61
62
/***************************************************************************/
63
64
957k
static uint8_t mz_stream_pkcrypt_decrypt_byte(void *stream) {
65
957k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
66
67
957k
    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
957k
    temp = pkcrypt->keys[2] | 2;
72
957k
    return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
73
957k
}
74
75
974k
static uint8_t mz_stream_pkcrypt_update_keys(void *stream, uint8_t c) {
76
974k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
77
974k
    uint8_t buf = c;
78
79
974k
    pkcrypt->keys[0] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[0], &buf, 1);
80
81
974k
    pkcrypt->keys[1] += pkcrypt->keys[0] & 0xff;
82
974k
    pkcrypt->keys[1] *= 134775813L;
83
974k
    pkcrypt->keys[1] += 1;
84
85
974k
    buf = (uint8_t)(pkcrypt->keys[1] >> 24);
86
974k
    pkcrypt->keys[2] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[2], &buf, 1);
87
88
974k
    return (uint8_t)c;
89
974k
}
90
91
2.44k
static void mz_stream_pkcrypt_init_keys(void *stream, const char *password) {
92
2.44k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
93
94
2.44k
    pkcrypt->keys[0] = 305419896L;
95
2.44k
    pkcrypt->keys[1] = 591751049L;
96
2.44k
    pkcrypt->keys[2] = 878082192L;
97
98
19.5k
    while (*password != 0) {
99
17.0k
        mz_stream_pkcrypt_update_keys(stream, (uint8_t)*password);
100
17.0k
        password += 1;
101
17.0k
    }
102
2.44k
}
103
104
/***************************************************************************/
105
106
2.44k
int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode) {
107
2.44k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
108
2.44k
    uint16_t t = 0;
109
2.44k
    int16_t i = 0;
110
2.44k
    uint8_t verify1 = 0;
111
2.44k
    uint8_t verify2 = 0;
112
2.44k
    uint8_t header[MZ_PKCRYPT_HEADER_SIZE];
113
2.44k
    const char *password = path;
114
115
2.44k
    pkcrypt->total_in = 0;
116
2.44k
    pkcrypt->total_out = 0;
117
2.44k
    pkcrypt->initialized = 0;
118
119
2.44k
    if (mz_stream_is_open(pkcrypt->stream.base) != MZ_OK)
120
0
        return MZ_OPEN_ERROR;
121
122
2.44k
    if (!password)
123
2.44k
        password = pkcrypt->password;
124
2.44k
    if (!password)
125
0
        return MZ_PARAM_ERROR;
126
127
2.44k
    mz_stream_pkcrypt_init_keys(stream, password);
128
129
2.44k
    if (mode & MZ_OPEN_MODE_WRITE) {
130
        /* First generate RAND_HEAD_LEN - 2 random bytes. */
131
0
        mz_crypt_rand(header, MZ_PKCRYPT_HEADER_SIZE - 2);
132
133
        /* Encrypt random header (last two bytes is high word of crc) */
134
0
        for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
135
0
            header[i] = mz_stream_pkcrypt_encode(stream, header[i], t);
136
137
0
        header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify1, t);
138
0
        header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify2, t);
139
140
0
        if (mz_stream_write(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
141
0
            return MZ_WRITE_ERROR;
142
143
0
        pkcrypt->total_out += MZ_PKCRYPT_HEADER_SIZE;
144
2.44k
    } else if (mode & MZ_OPEN_MODE_READ) {
145
2.44k
        if (mz_stream_read(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
146
24
            return MZ_READ_ERROR;
147
148
26.5k
        for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
149
24.1k
            header[i] = mz_stream_pkcrypt_decode(stream, header[i]);
150
151
2.41k
        verify1 = mz_stream_pkcrypt_decode(stream, header[i++]);
152
2.41k
        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
2.41k
        if (verify2 != pkcrypt->verify2)
157
61
            return MZ_PASSWORD_ERROR;
158
2.35k
        if (pkcrypt->verify_version < 2) {
159
405
            if (verify1 != pkcrypt->verify1)
160
12
                return MZ_PASSWORD_ERROR;
161
405
        }
162
163
2.34k
        pkcrypt->total_in += MZ_PKCRYPT_HEADER_SIZE;
164
2.34k
    }
165
166
2.34k
    pkcrypt->initialized = 1;
167
2.34k
    return MZ_OK;
168
2.44k
}
169
170
6.53k
int32_t mz_stream_pkcrypt_is_open(void *stream) {
171
6.53k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
172
6.53k
    if (!pkcrypt->initialized)
173
0
        return MZ_OPEN_ERROR;
174
6.53k
    return MZ_OK;
175
6.53k
}
176
177
2.09k
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size) {
178
2.09k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
179
2.09k
    uint8_t *buf_ptr = (uint8_t *)buf;
180
2.09k
    int32_t bytes_to_read = size;
181
2.09k
    int32_t read = 0;
182
2.09k
    int32_t i = 0;
183
184
2.09k
    if ((int64_t)bytes_to_read > (pkcrypt->max_total_in - pkcrypt->total_in))
185
813
        bytes_to_read = (int32_t)(pkcrypt->max_total_in - pkcrypt->total_in);
186
187
2.09k
    read = mz_stream_read(pkcrypt->stream.base, buf, bytes_to_read);
188
189
930k
    for (i = 0; i < read; i++)
190
928k
        buf_ptr[i] = mz_stream_pkcrypt_decode(stream, buf_ptr[i]);
191
192
2.09k
    if (read > 0)
193
1.24k
        pkcrypt->total_in += read;
194
195
2.09k
    return read;
196
2.09k
}
197
198
0
int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size) {
199
0
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
200
0
    const uint8_t *buf_ptr = (const uint8_t *)buf;
201
0
    int32_t bytes_to_write = sizeof(pkcrypt->buffer);
202
0
    int32_t total_written = 0;
203
0
    int32_t written = 0;
204
0
    int32_t i = 0;
205
0
    uint16_t t = 0;
206
207
0
    if (size < 0)
208
0
        return MZ_PARAM_ERROR;
209
210
0
    do {
211
0
        if (bytes_to_write > (size - total_written))
212
0
            bytes_to_write = (size - total_written);
213
214
0
        for (i = 0; i < bytes_to_write; i += 1) {
215
0
            pkcrypt->buffer[i] = mz_stream_pkcrypt_encode(stream, *buf_ptr, t);
216
0
            buf_ptr += 1;
217
0
        }
218
219
0
        written = mz_stream_write(pkcrypt->stream.base, pkcrypt->buffer, bytes_to_write);
220
0
        if (written < 0)
221
0
            return written;
222
223
0
        total_written += written;
224
0
    } while (total_written < size && written > 0);
225
226
0
    pkcrypt->total_out += total_written;
227
0
    return total_written;
228
0
}
229
230
0
int64_t mz_stream_pkcrypt_tell(void *stream) {
231
0
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
232
0
    return mz_stream_tell(pkcrypt->stream.base);
233
0
}
234
235
0
int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin) {
236
0
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
237
0
    return mz_stream_seek(pkcrypt->stream.base, offset, origin);
238
0
}
239
240
0
int32_t mz_stream_pkcrypt_close(void *stream) {
241
0
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
242
0
    pkcrypt->initialized = 0;
243
0
    return MZ_OK;
244
0
}
245
246
0
int32_t mz_stream_pkcrypt_error(void *stream) {
247
0
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
248
0
    return pkcrypt->error;
249
0
}
250
251
2.44k
void mz_stream_pkcrypt_set_password(void *stream, const char *password) {
252
2.44k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
253
2.44k
    pkcrypt->password = password;
254
2.44k
}
255
256
2.44k
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2, uint16_t version) {
257
2.44k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
258
2.44k
    pkcrypt->verify1 = verify1;
259
2.44k
    pkcrypt->verify2 = verify2;
260
2.44k
    pkcrypt->verify_version = version;
261
2.44k
}
262
263
0
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2, uint16_t *version) {
264
0
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
265
0
    *verify1 = pkcrypt->verify1;
266
0
    *verify2 = pkcrypt->verify2;
267
0
    *version = pkcrypt->verify_version;
268
0
}
269
270
4.69k
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
271
4.69k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
272
4.69k
    switch (prop) {
273
0
    case MZ_STREAM_PROP_TOTAL_IN:
274
0
        *value = pkcrypt->total_in;
275
0
        break;
276
0
    case MZ_STREAM_PROP_TOTAL_OUT:
277
0
        *value = pkcrypt->total_out;
278
0
        break;
279
0
    case MZ_STREAM_PROP_TOTAL_IN_MAX:
280
0
        *value = pkcrypt->max_total_in;
281
0
        break;
282
2.34k
    case MZ_STREAM_PROP_HEADER_SIZE:
283
2.34k
        *value = MZ_PKCRYPT_HEADER_SIZE;
284
2.34k
        break;
285
2.34k
    case MZ_STREAM_PROP_FOOTER_SIZE:
286
2.34k
        *value = 0;
287
2.34k
        break;
288
0
    default:
289
0
        return MZ_EXIST_ERROR;
290
4.69k
    }
291
4.69k
    return MZ_OK;
292
4.69k
}
293
294
2.34k
int32_t mz_stream_pkcrypt_set_prop_int64(void *stream, int32_t prop, int64_t value) {
295
2.34k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
296
2.34k
    switch (prop) {
297
2.34k
    case MZ_STREAM_PROP_TOTAL_IN_MAX:
298
2.34k
        pkcrypt->max_total_in = value;
299
2.34k
        break;
300
0
    default:
301
0
        return MZ_EXIST_ERROR;
302
2.34k
    }
303
2.34k
    return MZ_OK;
304
2.34k
}
305
306
2.44k
void *mz_stream_pkcrypt_create(void) {
307
2.44k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)calloc(1, sizeof(mz_stream_pkcrypt));
308
2.44k
    if (pkcrypt)
309
2.44k
        pkcrypt->stream.vtbl = &mz_stream_pkcrypt_vtbl;
310
2.44k
    return pkcrypt;
311
2.44k
}
312
313
2.44k
void mz_stream_pkcrypt_delete(void **stream) {
314
2.44k
    mz_stream_pkcrypt *pkcrypt = NULL;
315
2.44k
    if (!stream)
316
0
        return;
317
2.44k
    pkcrypt = (mz_stream_pkcrypt *)*stream;
318
2.44k
    free(pkcrypt);
319
2.44k
    *stream = NULL;
320
2.44k
}
321
322
0
void *mz_stream_pkcrypt_get_interface(void) {
323
0
    return (void *)&mz_stream_pkcrypt_vtbl;
324
0
}