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