Fossil SCM

fossil-scm / compat / zlib / contrib / minizip / crypt.h
Blame History Raw 126 lines
1
/* crypt.h -- base code for crypt/uncrypt ZIPfile
2
3
Copyright (C) 1998-2026 Gilles Vollant
4
5
This code is a modified version of crypting code in Infozip distribution
6
7
The encryption/decryption parts of this source code (as opposed to the
8
non-echoing password parts) were originally written in Europe. The
9
whole source package can be freely distributed, including from the USA.
10
(Prior to January 2000, re-export from the US was a violation of US law.)
11
12
This encryption code is a direct transcription of the algorithm from
13
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
14
file (appnote.txt) is distributed with the PKZIP program (even in the
15
version without encryption capabilities).
16
17
If you don't need crypting in your application, just define symbols
18
NOCRYPT and NOUNCRYPT.
19
20
This code support the "Traditional PKWARE Encryption".
21
22
The new AES encryption added on Zip format by Winzip (see the page
23
https://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong
24
Encryption is not supported.
25
*/
26
27
#define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
28
29
/***********************************************************************
30
* Return the next byte in the pseudo-random sequence
31
*/
32
static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab) {
33
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
34
* unpredictable manner on 16-bit systems; not a problem
35
* with any known compiler so far, though */
36
37
(void)pcrc_32_tab;
38
temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
39
return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
40
}
41
42
/***********************************************************************
43
* Update the encryption keys with the next byte of plain text
44
*/
45
static int update_keys(unsigned long* pkeys, const z_crc_t* pcrc_32_tab, int c) {
46
(*(pkeys+0)) = CRC32((*(pkeys+0)), c);
47
(*(pkeys+1)) += (*(pkeys+0)) & 0xff;
48
(*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
49
{
50
int keyshift = (int)((*(pkeys+1)) >> 24);
51
(*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
52
}
53
return c;
54
}
55
56
57
/***********************************************************************
58
* Initialize the encryption keys and the random header according to
59
* the given password.
60
*/
61
static void init_keys(const char* passwd, unsigned long* pkeys, const z_crc_t* pcrc_32_tab) {
62
*(pkeys+0) = 305419896L;
63
*(pkeys+1) = 591751049L;
64
*(pkeys+2) = 878082192L;
65
while (*passwd != '\0') {
66
update_keys(pkeys,pcrc_32_tab,(int)*passwd);
67
passwd++;
68
}
69
}
70
71
#define zdecode(pkeys,pcrc_32_tab,c) \
72
(update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
73
74
#define zencode(pkeys,pcrc_32_tab,c,t) \
75
(t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), (Byte)t^(c))
76
77
#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
78
79
#define RAND_HEAD_LEN 12
80
/* "last resort" source for second part of crypt seed pattern */
81
# ifndef ZCR_SEED2
82
# define ZCR_SEED2 3141592654UL /* use PI as default pattern */
83
# endif
84
85
static unsigned crypthead(const char* passwd, /* password string */
86
unsigned char* buf, /* where to write header */
87
int bufSize,
88
unsigned long* pkeys,
89
const z_crc_t* pcrc_32_tab,
90
unsigned long crcForCrypting) {
91
unsigned n; /* index in random header */
92
int t; /* temporary */
93
int c; /* random byte */
94
unsigned char header[RAND_HEAD_LEN-2]; /* random header */
95
static unsigned calls = 0; /* ensure different random header each time */
96
97
if (bufSize<RAND_HEAD_LEN)
98
return 0;
99
100
/* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
101
* output of rand() to get less predictability, since rand() is
102
* often poorly implemented.
103
*/
104
if (++calls == 1)
105
{
106
srand((unsigned)time(NULL) ^ ZCR_SEED2);
107
}
108
init_keys(passwd, pkeys, pcrc_32_tab);
109
for (n = 0; n < RAND_HEAD_LEN-2; n++)
110
{
111
c = (rand() >> 7) & 0xff;
112
header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
113
}
114
/* Encrypt random header (last two bytes is high word of crc) */
115
init_keys(passwd, pkeys, pcrc_32_tab);
116
for (n = 0; n < RAND_HEAD_LEN-2; n++)
117
{
118
buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
119
}
120
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
121
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
122
return n;
123
}
124
125
#endif
126

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button