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