/src/libzip/lib/zip_pkware.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | zip_pkware.c -- Traditional PKWARE de/encryption backend routines |
3 | | Copyright (C) 2009-2022 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 | | static void |
46 | 0 | update_keys(zip_pkware_keys_t *keys, zip_uint8_t b) { |
47 | 0 | keys->key[0] = (zip_uint32_t)crc32(keys->key[0] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL; |
48 | 0 | keys->key[1] = (keys->key[1] + (keys->key[0] & 0xff)) * 134775813 + 1; |
49 | 0 | b = (zip_uint8_t)(keys->key[1] >> 24); |
50 | 0 | keys->key[2] = (zip_uint32_t)crc32(keys->key[2] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL; |
51 | 0 | } |
52 | | |
53 | | |
54 | | static zip_uint8_t |
55 | 0 | crypt_byte(zip_pkware_keys_t *keys) { |
56 | 0 | zip_uint16_t tmp; |
57 | 0 | tmp = (zip_uint16_t)(keys->key[2] | 2); |
58 | 0 | tmp = (zip_uint16_t)(((zip_uint32_t)tmp * (tmp ^ 1)) >> 8); |
59 | 0 | return (zip_uint8_t)tmp; |
60 | 0 | } |
61 | | |
62 | | |
63 | | void |
64 | 0 | _zip_pkware_keys_reset(zip_pkware_keys_t *keys) { |
65 | 0 | keys->key[0] = PKWARE_KEY0; |
66 | 0 | keys->key[1] = PKWARE_KEY1; |
67 | 0 | keys->key[2] = PKWARE_KEY2; |
68 | 0 | } |
69 | | |
70 | | |
71 | | void |
72 | 0 | _zip_pkware_encrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len) { |
73 | 0 | zip_uint64_t i; |
74 | 0 | zip_uint8_t b; |
75 | 0 | zip_uint8_t tmp; |
76 | |
|
77 | 0 | for (i = 0; i < len; i++) { |
78 | 0 | b = in[i]; |
79 | |
|
80 | 0 | if (out != NULL) { |
81 | 0 | tmp = crypt_byte(keys); |
82 | 0 | update_keys(keys, b); |
83 | 0 | b ^= tmp; |
84 | 0 | out[i] = b; |
85 | 0 | } |
86 | 0 | else { |
87 | | /* during initialization, we're only interested in key updates */ |
88 | 0 | update_keys(keys, b); |
89 | 0 | } |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | |
94 | | void |
95 | 0 | _zip_pkware_decrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len) { |
96 | 0 | zip_uint64_t i; |
97 | 0 | zip_uint8_t b; |
98 | 0 | zip_uint8_t tmp; |
99 | |
|
100 | 0 | for (i = 0; i < len; i++) { |
101 | 0 | b = in[i]; |
102 | | |
103 | | /* during initialization, we're only interested in key updates */ |
104 | 0 | if (out != NULL) { |
105 | 0 | tmp = crypt_byte(keys); |
106 | 0 | b ^= tmp; |
107 | 0 | out[i] = b; |
108 | 0 | } |
109 | |
|
110 | 0 | update_keys(keys, b); |
111 | 0 | } |
112 | 0 | } |