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