summaryrefslogtreecommitdiff
path: root/contrib/minizip/crypt.h
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/minizip/crypt.h')
-rw-r--r--contrib/minizip/crypt.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/contrib/minizip/crypt.h b/contrib/minizip/crypt.h
new file mode 100644
index 0000000..e5bc627
--- /dev/null
+++ b/contrib/minizip/crypt.h
@@ -0,0 +1,104 @@
1
2#define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
3
4/***********************************************************************
5 * Return the next byte in the pseudo-random sequence
6 */
7static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
8{
9 unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
10 * unpredictable manner on 16-bit systems; not a problem
11 * with any known compiler so far, though */
12
13 temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
14 return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
15}
16
17/***********************************************************************
18 * Update the encryption keys with the next byte of plain text
19 */
20static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
21{
22 (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
23 (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
24 (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
25 {
26 register int keyshift = (int)((*(pkeys+1)) >> 24);
27 (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
28 }
29 return c;
30}
31
32
33/***********************************************************************
34 * Initialize the encryption keys and the random header according to
35 * the given password.
36 */
37static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
38{
39 *(pkeys+0) = 305419896L;
40 *(pkeys+1) = 591751049L;
41 *(pkeys+2) = 878082192L;
42 while (*passwd != '\0') {
43 update_keys(pkeys,pcrc_32_tab,(int)*passwd);
44 passwd++;
45 }
46}
47
48#define zdecode(pkeys,pcrc_32_tab,c) \
49 (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
50
51#define zencode(pkeys,pcrc_32_tab,c,t) \
52 (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
53
54#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
55
56#define RAND_HEAD_LEN 12
57 /* "last resort" source for second part of crypt seed pattern */
58# ifndef ZCR_SEED2
59# define ZCR_SEED2 (unsigned long)3141592654L /* use PI as default pattern */
60# endif
61
62static int crypthead(passwd, buf, bufSize, pkeys, pcrc_32_tab, crcForCrypting)
63 const char *passwd; /* password string */
64 unsigned char *buf; /* where to write header */
65 int bufSize;
66 unsigned long* pkeys;
67 const unsigned long* pcrc_32_tab;
68 unsigned long crcForCrypting;
69{
70 int n; /* index in random header */
71 int t; /* temporary */
72 int c; /* random byte */
73 unsigned char header[RAND_HEAD_LEN-2]; /* random header */
74 static unsigned calls = 0; /* ensure different random header each time */
75
76 if (bufSize<RAND_HEAD_LEN)
77 return 0;
78
79 /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
80 * output of rand() to get less predictability, since rand() is
81 * often poorly implemented.
82 */
83 if (++calls == 1)
84 {
85 srand((unsigned)(time(NULL) ^ ZCR_SEED2));
86 }
87 init_keys(passwd, pkeys, pcrc_32_tab);
88 for (n = 0; n < RAND_HEAD_LEN-2; n++)
89 {
90 c = (rand() >> 7) & 0xff;
91 header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
92 }
93 /* Encrypt random header (last two bytes is high word of crc) */
94 init_keys(passwd, pkeys, pcrc_32_tab);
95 for (n = 0; n < RAND_HEAD_LEN-2; n++)
96 {
97 buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
98 }
99 buf[n++] = zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
100 buf[n++] = zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
101 return n;
102}
103
104#endif