Coverage Report

Created: 2025-11-27 06:27

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
954k
    (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
951k
static uint8_t mz_stream_pkcrypt_decrypt_byte(void *stream) {
65
951k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
66
67
951k
    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
951k
    temp = pkcrypt->keys[2] | 2;
72
951k
    return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
73
951k
}
74
75
971k
static uint8_t mz_stream_pkcrypt_update_keys(void *stream, uint8_t c) {
76
971k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
77
971k
    uint8_t buf = c;
78
79
971k
    pkcrypt->keys[0] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[0], &buf, 1);
80
81
971k
    pkcrypt->keys[1] += pkcrypt->keys[0] & 0xff;
82
971k
    pkcrypt->keys[1] *= 134775813L;
83
971k
    pkcrypt->keys[1] += 1;
84
85
971k
    buf = (uint8_t)(pkcrypt->keys[1] >> 24);
86
971k
    pkcrypt->keys[2] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[2], &buf, 1);
87
88
971k
    return (uint8_t)c;
89
971k
}
90
91
2.78k
static void mz_stream_pkcrypt_init_keys(void *stream, const char *password) {
92
2.78k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
93
94
2.78k
    pkcrypt->keys[0] = 305419896L;
95
2.78k
    pkcrypt->keys[1] = 591751049L;
96
2.78k
    pkcrypt->keys[2] = 878082192L;
97
98
22.2k
    while (*password != 0) {
99
19.5k
        mz_stream_pkcrypt_update_keys(stream, (uint8_t)*password);
100
19.5k
        password += 1;
101
19.5k
    }
102
2.78k
}
103
104
/***************************************************************************/
105
106
2.78k
int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode) {
107
2.78k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
108
2.78k
    uint16_t t = 0;
109
2.78k
    int16_t i = 0;
110
2.78k
    uint8_t verify1 = 0;
111
2.78k
    uint8_t verify2 = 0;
112
2.78k
    uint8_t header[MZ_PKCRYPT_HEADER_SIZE];
113
2.78k
    const char *password = path;
114
115
2.78k
    pkcrypt->total_in = 0;
116
2.78k
    pkcrypt->total_out = 0;
117
2.78k
    pkcrypt->initialized = 0;
118
119
2.78k
    if (mz_stream_is_open(pkcrypt->stream.base) != MZ_OK)
120
0
        return MZ_OPEN_ERROR;
121
122
2.78k
    if (!password)
123
2.78k
        password = pkcrypt->password;
124
2.78k
    if (!password)
125
0
        return MZ_PARAM_ERROR;
126
127
2.78k
    mz_stream_pkcrypt_init_keys(stream, password);
128
129
2.78k
    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.78k
    } else if (mode & MZ_OPEN_MODE_READ) {
145
2.78k
        if (mz_stream_read(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
146
11
            return MZ_READ_ERROR;
147
148
30.5k
        for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
149
27.7k
            header[i] = mz_stream_pkcrypt_decode(stream, header[i]);
150
151
2.77k
        verify1 = mz_stream_pkcrypt_decode(stream, header[i++]);
152
2.77k
        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.77k
        if (verify2 != pkcrypt->verify2)
157
111
            return MZ_PASSWORD_ERROR;
158
2.66k
        if (pkcrypt->verify_version < 2) {
159
435
            if (verify1 != pkcrypt->verify1)
160
15
                return MZ_PASSWORD_ERROR;
161
435
        }
162
163
2.64k
        pkcrypt->total_in += MZ_PKCRYPT_HEADER_SIZE;
164
2.64k
    }
165
166
2.64k
    pkcrypt->initialized = 1;
167
2.64k
    return MZ_OK;
168
2.78k
}
169
170
7.45k
int32_t mz_stream_pkcrypt_is_open(void *stream) {
171
7.45k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
172
7.45k
    if (!pkcrypt->initialized)
173
0
        return MZ_OPEN_ERROR;
174
7.45k
    return MZ_OK;
175
7.45k
}
176
177
2.40k
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size) {
178
2.40k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
179
2.40k
    uint8_t *buf_ptr = (uint8_t *)buf;
180
2.40k
    int32_t bytes_to_read = size;
181
2.40k
    int32_t read = 0;
182
2.40k
    int32_t i = 0;
183
184
2.40k
    if ((int64_t)bytes_to_read > (pkcrypt->max_total_in - pkcrypt->total_in))
185
1.09k
        bytes_to_read = (int32_t)(pkcrypt->max_total_in - pkcrypt->total_in);
186
187
2.40k
    read = mz_stream_read(pkcrypt->stream.base, buf, bytes_to_read);
188
189
920k
    for (i = 0; i < read; i++)
190
918k
        buf_ptr[i] = mz_stream_pkcrypt_decode(stream, buf_ptr[i]);
191
192
2.40k
    if (read > 0)
193
1.27k
        pkcrypt->total_in += read;
194
195
2.40k
    return read;
196
2.40k
}
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.78k
void mz_stream_pkcrypt_set_password(void *stream, const char *password) {
252
2.78k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
253
2.78k
    pkcrypt->password = password;
254
2.78k
}
255
256
2.78k
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2, uint16_t version) {
257
2.78k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
258
2.78k
    pkcrypt->verify1 = verify1;
259
2.78k
    pkcrypt->verify2 = verify2;
260
2.78k
    pkcrypt->verify_version = version;
261
2.78k
}
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
5.29k
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
271
5.29k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
272
5.29k
    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.64k
    case MZ_STREAM_PROP_HEADER_SIZE:
283
2.64k
        *value = MZ_PKCRYPT_HEADER_SIZE;
284
2.64k
        break;
285
2.64k
    case MZ_STREAM_PROP_FOOTER_SIZE:
286
2.64k
        *value = 0;
287
2.64k
        break;
288
0
    default:
289
0
        return MZ_EXIST_ERROR;
290
5.29k
    }
291
5.29k
    return MZ_OK;
292
5.29k
}
293
294
2.64k
int32_t mz_stream_pkcrypt_set_prop_int64(void *stream, int32_t prop, int64_t value) {
295
2.64k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
296
2.64k
    switch (prop) {
297
2.64k
    case MZ_STREAM_PROP_TOTAL_IN_MAX:
298
2.64k
        pkcrypt->max_total_in = value;
299
2.64k
        break;
300
0
    default:
301
0
        return MZ_EXIST_ERROR;
302
2.64k
    }
303
2.64k
    return MZ_OK;
304
2.64k
}
305
306
2.78k
void *mz_stream_pkcrypt_create(void) {
307
2.78k
    mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)calloc(1, sizeof(mz_stream_pkcrypt));
308
2.78k
    if (pkcrypt)
309
2.78k
        pkcrypt->stream.vtbl = &mz_stream_pkcrypt_vtbl;
310
2.78k
    return pkcrypt;
311
2.78k
}
312
313
2.78k
void mz_stream_pkcrypt_delete(void **stream) {
314
2.78k
    mz_stream_pkcrypt *pkcrypt = NULL;
315
2.78k
    if (!stream)
316
0
        return;
317
2.78k
    pkcrypt = (mz_stream_pkcrypt *)*stream;
318
2.78k
    free(pkcrypt);
319
2.78k
    *stream = NULL;
320
2.78k
}
321
322
0
void *mz_stream_pkcrypt_get_interface(void) {
323
0
    return (void *)&mz_stream_pkcrypt_vtbl;
324
0
}