/src/libzip/lib/zip_pkware.c
Line | Count | Source |
1 | | /* |
2 | | zip_pkware.c -- Traditional PKWARE de/encryption backend routines |
3 | | Copyright (C) 2009-2024 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | |
35 | | #include <stdlib.h> |
36 | | #include <zlib.h> |
37 | | |
38 | | #include "zipint.h" |
39 | | |
40 | 0 | #define PKWARE_KEY0 305419896 |
41 | 0 | #define PKWARE_KEY1 591751049 |
42 | 0 | #define PKWARE_KEY2 878082192 |
43 | | |
44 | | |
45 | 0 | static void update_keys(zip_pkware_keys_t *keys, zip_uint8_t b) { |
46 | 0 | keys->key[0] = (zip_uint32_t)crc32(keys->key[0] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL; |
47 | 0 | keys->key[1] = (keys->key[1] + (keys->key[0] & 0xff)) * 134775813 + 1; |
48 | 0 | b = (zip_uint8_t)(keys->key[1] >> 24); |
49 | 0 | keys->key[2] = (zip_uint32_t)crc32(keys->key[2] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL; |
50 | 0 | } |
51 | | |
52 | | |
53 | 0 | static zip_uint8_t crypt_byte(zip_pkware_keys_t *keys) { |
54 | 0 | zip_uint16_t tmp; |
55 | 0 | tmp = (zip_uint16_t)(keys->key[2] | 2); |
56 | 0 | tmp = (zip_uint16_t)(((zip_uint32_t)tmp * (tmp ^ 1)) >> 8); |
57 | 0 | return (zip_uint8_t)tmp; |
58 | 0 | } |
59 | | |
60 | | |
61 | 0 | void _zip_pkware_keys_reset(zip_pkware_keys_t *keys) { |
62 | 0 | keys->key[0] = PKWARE_KEY0; |
63 | 0 | keys->key[1] = PKWARE_KEY1; |
64 | 0 | keys->key[2] = PKWARE_KEY2; |
65 | 0 | } |
66 | | |
67 | | |
68 | 0 | void _zip_pkware_encrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len) { |
69 | 0 | zip_uint64_t i; |
70 | 0 | zip_uint8_t b; |
71 | 0 | zip_uint8_t tmp; |
72 | |
|
73 | 0 | for (i = 0; i < len; i++) { |
74 | 0 | b = in[i]; |
75 | |
|
76 | 0 | if (out != NULL) { |
77 | 0 | tmp = crypt_byte(keys); |
78 | 0 | update_keys(keys, b); |
79 | 0 | b ^= tmp; |
80 | 0 | out[i] = b; |
81 | 0 | } |
82 | 0 | else { |
83 | | /* during initialization, we're only interested in key updates */ |
84 | 0 | update_keys(keys, b); |
85 | 0 | } |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | |
90 | 0 | void _zip_pkware_decrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len) { |
91 | 0 | zip_uint64_t i; |
92 | 0 | zip_uint8_t b; |
93 | 0 | zip_uint8_t tmp; |
94 | |
|
95 | 0 | for (i = 0; i < len; i++) { |
96 | 0 | b = in[i]; |
97 | | |
98 | | /* during initialization, we're only interested in key updates */ |
99 | 0 | if (out != NULL) { |
100 | 0 | tmp = crypt_byte(keys); |
101 | 0 | b ^= tmp; |
102 | 0 | out[i] = b; |
103 | 0 | } |
104 | |
|
105 | 0 | update_keys(keys, b); |
106 | 0 | } |
107 | 0 | } |