cannam@89: /* crypt.h -- base code for crypt/uncrypt ZIPfile cannam@89: cannam@89: cannam@89: Version 1.01e, February 12th, 2005 cannam@89: cannam@89: Copyright (C) 1998-2005 Gilles Vollant cannam@89: cannam@89: This code is a modified version of crypting code in Infozip distribution cannam@89: cannam@89: The encryption/decryption parts of this source code (as opposed to the cannam@89: non-echoing password parts) were originally written in Europe. The cannam@89: whole source package can be freely distributed, including from the USA. cannam@89: (Prior to January 2000, re-export from the US was a violation of US law.) cannam@89: cannam@89: This encryption code is a direct transcription of the algorithm from cannam@89: Roger Schlafly, described by Phil Katz in the file appnote.txt. This cannam@89: file (appnote.txt) is distributed with the PKZIP program (even in the cannam@89: version without encryption capabilities). cannam@89: cannam@89: If you don't need crypting in your application, just define symbols cannam@89: NOCRYPT and NOUNCRYPT. cannam@89: cannam@89: This code support the "Traditional PKWARE Encryption". cannam@89: cannam@89: The new AES encryption added on Zip format by Winzip (see the page cannam@89: http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong cannam@89: Encryption is not supported. cannam@89: */ cannam@89: cannam@89: #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) cannam@89: cannam@89: /*********************************************************************** cannam@89: * Return the next byte in the pseudo-random sequence cannam@89: */ cannam@89: static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) cannam@89: { cannam@89: unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an cannam@89: * unpredictable manner on 16-bit systems; not a problem cannam@89: * with any known compiler so far, though */ cannam@89: cannam@89: temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2; cannam@89: return (int)(((temp * (temp ^ 1)) >> 8) & 0xff); cannam@89: } cannam@89: cannam@89: /*********************************************************************** cannam@89: * Update the encryption keys with the next byte of plain text cannam@89: */ cannam@89: static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c) cannam@89: { cannam@89: (*(pkeys+0)) = CRC32((*(pkeys+0)), c); cannam@89: (*(pkeys+1)) += (*(pkeys+0)) & 0xff; cannam@89: (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; cannam@89: { cannam@89: register int keyshift = (int)((*(pkeys+1)) >> 24); cannam@89: (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); cannam@89: } cannam@89: return c; cannam@89: } cannam@89: cannam@89: cannam@89: /*********************************************************************** cannam@89: * Initialize the encryption keys and the random header according to cannam@89: * the given password. cannam@89: */ cannam@89: static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab) cannam@89: { cannam@89: *(pkeys+0) = 305419896L; cannam@89: *(pkeys+1) = 591751049L; cannam@89: *(pkeys+2) = 878082192L; cannam@89: while (*passwd != '\0') { cannam@89: update_keys(pkeys,pcrc_32_tab,(int)*passwd); cannam@89: passwd++; cannam@89: } cannam@89: } cannam@89: cannam@89: #define zdecode(pkeys,pcrc_32_tab,c) \ cannam@89: (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) cannam@89: cannam@89: #define zencode(pkeys,pcrc_32_tab,c,t) \ cannam@89: (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c)) cannam@89: cannam@89: #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED cannam@89: cannam@89: #define RAND_HEAD_LEN 12 cannam@89: /* "last resort" source for second part of crypt seed pattern */ cannam@89: # ifndef ZCR_SEED2 cannam@89: # define ZCR_SEED2 3141592654UL /* use PI as default pattern */ cannam@89: # endif cannam@89: cannam@89: static int crypthead(const char* passwd, /* password string */ cannam@89: unsigned char* buf, /* where to write header */ cannam@89: int bufSize, cannam@89: unsigned long* pkeys, cannam@89: const unsigned long* pcrc_32_tab, cannam@89: unsigned long crcForCrypting) cannam@89: { cannam@89: int n; /* index in random header */ cannam@89: int t; /* temporary */ cannam@89: int c; /* random byte */ cannam@89: unsigned char header[RAND_HEAD_LEN-2]; /* random header */ cannam@89: static unsigned calls = 0; /* ensure different random header each time */ cannam@89: cannam@89: if (bufSize> 7) & 0xff; cannam@89: header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); cannam@89: } cannam@89: /* Encrypt random header (last two bytes is high word of crc) */ cannam@89: init_keys(passwd, pkeys, pcrc_32_tab); cannam@89: for (n = 0; n < RAND_HEAD_LEN-2; n++) cannam@89: { cannam@89: buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t); cannam@89: } cannam@89: buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t); cannam@89: buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t); cannam@89: return n; cannam@89: } cannam@89: cannam@89: #endif