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