fw4spl
crypt.h
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 /* crypt.h -- base code for crypt/uncrypt ZIPfile
8 
9 
10  Version 1.01e, February 12th, 2005
11 
12  Copyright (C) 1998-2005 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 
36 #ifndef __MINIZIP_CRYPT_H__
37 #define __MINIZIP_CRYPT_H__
38 
39 #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
40 
41 /***********************************************************************
42  * Return the next byte in the pseudo-random sequence
43  */
44 static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
45 {
46  unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
47  * unpredictable manner on 16-bit systems; not a problem
48  * with any known compiler so far, though */
49 
50  temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
51  return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
52 }
53 
54 /***********************************************************************
55  * Update the encryption keys with the next byte of plain text
56  */
57 static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
58 {
59  (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
60  (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
61  (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
62  {
63  register int keyshift = (int)((*(pkeys+1)) >> 24);
64  (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
65  }
66  return c;
67 }
68 
69 
70 /***********************************************************************
71  * Initialize the encryption keys and the random header according to
72  * the given password.
73  */
74 static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
75 {
76  *(pkeys+0) = 305419896L;
77  *(pkeys+1) = 591751049L;
78  *(pkeys+2) = 878082192L;
79  while (*passwd != '\0')
80  {
81  update_keys(pkeys,pcrc_32_tab,(int)*passwd);
82  passwd++;
83  }
84 }
85 
86 #define zdecode(pkeys,pcrc_32_tab,c) \
87  (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
88 
89 #define zencode(pkeys,pcrc_32_tab,c,t) \
90  (t = decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
91 
92 #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
93 
94 #define RAND_HEAD_LEN 12
95 /* "last resort" source for second part of crypt seed pattern */
96 # ifndef ZCR_SEED2
97 # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
98 # endif
99 
100 static int crypthead(const char* passwd, /* password string */
101  unsigned char* buf, /* where to write header */
102  int bufSize,
103  unsigned long* pkeys,
104  const unsigned long* pcrc_32_tab,
105  unsigned long crcForCrypting)
106 {
107  int n; /* index in random header */
108  int t; /* temporary */
109  int c; /* random byte */
110  unsigned char header[RAND_HEAD_LEN-2]; /* random header */
111  static unsigned calls = 0; /* ensure different random header each time */
112 
113  if (bufSize<RAND_HEAD_LEN)
114  {
115  return 0;
116  }
117 
118  /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
119  * output of rand() to get less predictability, since rand() is
120  * often poorly implemented.
121  */
122  if (++calls == 1)
123  {
124  srand((unsigned)(time(NULL) ^ ZCR_SEED2));
125  }
126  init_keys(passwd, pkeys, pcrc_32_tab);
127  for (n = 0; n < RAND_HEAD_LEN-2; n++)
128  {
129  c = (rand() >> 7) & 0xff;
130  header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
131  }
132  /* Encrypt random header (last two bytes is high word of crc) */
133  init_keys(passwd, pkeys, pcrc_32_tab);
134  for (n = 0; n < RAND_HEAD_LEN-2; n++)
135  {
136  buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
137  }
138  buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
139  buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
140  return n;
141 }
142 
143 #endif
144 
145 #endif //__MINIZIP_CRYPT_H__