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