Rizin
unix-like reverse engineering framework and cli tools
zip_pkware.c File Reference
#include <stdlib.h>
#include <zlib.h>
#include "zipint.h"

Go to the source code of this file.

Macros

#define PKWARE_KEY0   305419896
 
#define PKWARE_KEY1   591751049
 
#define PKWARE_KEY2   878082192
 

Functions

static void update_keys (zip_pkware_keys_t *keys, zip_uint8_t b)
 
static zip_uint8_t crypt_byte (zip_pkware_keys_t *keys)
 
void _zip_pkware_keys_reset (zip_pkware_keys_t *keys)
 
void _zip_pkware_encrypt (zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len)
 
void _zip_pkware_decrypt (zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len)
 

Macro Definition Documentation

◆ PKWARE_KEY0

#define PKWARE_KEY0   305419896

Definition at line 40 of file zip_pkware.c.

◆ PKWARE_KEY1

#define PKWARE_KEY1   591751049

Definition at line 41 of file zip_pkware.c.

◆ PKWARE_KEY2

#define PKWARE_KEY2   878082192

Definition at line 42 of file zip_pkware.c.

Function Documentation

◆ _zip_pkware_decrypt()

void _zip_pkware_decrypt ( zip_pkware_keys_t keys,
zip_uint8_t out,
const zip_uint8_t in,
zip_uint64_t  len 
)

Definition at line 95 of file zip_pkware.c.

95  {
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
static zip_uint8_t crypt_byte(zip_pkware_keys_t *keys)
Definition: zip_pkware.c:55
static void update_keys(zip_pkware_keys_t *keys, zip_uint8_t b)
Definition: zip_pkware.c:46
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33

References b, crypt_byte(), i, in, keys, len, NULL, out, autogen_x86imm::tmp, and update_keys().

Referenced by decrypt_header(), and pkware_decrypt().

◆ _zip_pkware_encrypt()

void _zip_pkware_encrypt ( zip_pkware_keys_t keys,
zip_uint8_t out,
const zip_uint8_t in,
zip_uint64_t  len 
)

Definition at line 72 of file zip_pkware.c.

72  {
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 }

References b, crypt_byte(), i, in, keys, len, NULL, out, autogen_x86imm::tmp, and update_keys().

Referenced by encrypt_header(), and pkware_encrypt().

◆ _zip_pkware_keys_reset()

void _zip_pkware_keys_reset ( zip_pkware_keys_t keys)

Definition at line 64 of file zip_pkware.c.

64  {
65  keys->key[0] = PKWARE_KEY0;
66  keys->key[1] = PKWARE_KEY1;
67  keys->key[2] = PKWARE_KEY2;
68 }
#define PKWARE_KEY0
Definition: zip_pkware.c:40
#define PKWARE_KEY1
Definition: zip_pkware.c:41
#define PKWARE_KEY2
Definition: zip_pkware.c:42

References keys, PKWARE_KEY0, PKWARE_KEY1, and PKWARE_KEY2.

Referenced by pkware_decrypt(), and pkware_encrypt().

◆ crypt_byte()

static zip_uint8_t crypt_byte ( zip_pkware_keys_t keys)
static

Definition at line 55 of file zip_pkware.c.

55  {
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 }
uint32_t zip_uint32_t
Definition: zipconf.h:37
uint16_t zip_uint16_t
Definition: zipconf.h:35

References keys, and autogen_x86imm::tmp.

Referenced by _zip_pkware_decrypt(), and _zip_pkware_encrypt().

◆ update_keys()

static void update_keys ( zip_pkware_keys_t keys,
zip_uint8_t  b 
)
static

Definition at line 46 of file zip_pkware.c.

46  {
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 }
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:1063

References b, crc32(), and keys.

Referenced by _zip_pkware_decrypt(), and _zip_pkware_encrypt().