Rizin
unix-like reverse engineering framework and cli tools
zip_pkware.c
Go to the documentation of this file.
1 /*
2  zip_pkware.c -- Traditional PKWARE de/encryption backend routines
3  Copyright (C) 2009-2020 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 #define PKWARE_KEY0 305419896
41 #define PKWARE_KEY1 591751049
42 #define PKWARE_KEY2 878082192
43 
44 
45 static void
47  keys->key[0] = (zip_uint32_t)crc32(keys->key[0] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL;
48  keys->key[1] = (keys->key[1] + (keys->key[0] & 0xff)) * 134775813 + 1;
49  b = (zip_uint8_t)(keys->key[1] >> 24);
50  keys->key[2] = (zip_uint32_t)crc32(keys->key[2] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL;
51 }
52 
53 
54 static zip_uint8_t
57  tmp = (zip_uint16_t)(keys->key[2] | 2);
58  tmp = (zip_uint16_t)(((zip_uint32_t)tmp * (tmp ^ 1)) >> 8);
59  return (zip_uint8_t)tmp;
60 }
61 
62 
63 void
65  keys->key[0] = PKWARE_KEY0;
66  keys->key[1] = PKWARE_KEY1;
67  keys->key[2] = PKWARE_KEY2;
68 }
69 
70 
71 void
74  zip_uint8_t b;
76 
77  for (i = 0; i < len; i++) {
78  b = in[i];
79 
80  if (out != NULL) {
81  tmp = crypt_byte(keys);
82  update_keys(keys, b);
83  b ^= tmp;
84  out[i] = b;
85  }
86  else {
87  /* during initialization, we're only interested in key updates */
88  update_keys(keys, b);
89  }
90  }
91 }
92 
93 
94 void
97  zip_uint8_t b;
99 
100  for (i = 0; i < len; i++) {
101  b = in[i];
102 
103  /* during initialization, we're only interested in key updates */
104  if (out != NULL) {
105  tmp = crypt_byte(keys);
106  b ^= tmp;
107  out[i] = b;
108  }
109 
110  update_keys(keys, b);
111  }
112 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define NULL
Definition: cris-opc.c:27
static struct @218 keys[]
#define b(i)
Definition: sha256.c:42
#define PKWARE_KEY0
Definition: zip_pkware.c:40
static zip_uint8_t crypt_byte(zip_pkware_keys_t *keys)
Definition: zip_pkware.c:55
void _zip_pkware_encrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len)
Definition: zip_pkware.c:72
void _zip_pkware_decrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len)
Definition: zip_pkware.c:95
#define PKWARE_KEY1
Definition: zip_pkware.c:41
void _zip_pkware_keys_reset(zip_pkware_keys_t *keys)
Definition: zip_pkware.c:64
static void update_keys(zip_pkware_keys_t *keys, zip_uint8_t b)
Definition: zip_pkware.c:46
#define PKWARE_KEY2
Definition: zip_pkware.c:42
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint32_t zip_uint32_t
Definition: zipconf.h:37
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:1063