packvfs
diff test/zipcat/src/minizip/crypt.h @ 3:ef6c1472607f
jesus fucking christ that was easy... written a test prog "zipcat" to try out
zlib's contrib library "minizip", to list and read files out of zip archives
directly...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Nov 2013 06:46:17 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/zipcat/src/minizip/crypt.h Mon Nov 04 06:46:17 2013 +0200 1.3 @@ -0,0 +1,131 @@ 1.4 +/* crypt.h -- base code for crypt/uncrypt ZIPfile 1.5 + 1.6 + 1.7 + Version 1.01e, February 12th, 2005 1.8 + 1.9 + Copyright (C) 1998-2005 Gilles Vollant 1.10 + 1.11 + This code is a modified version of crypting code in Infozip distribution 1.12 + 1.13 + The encryption/decryption parts of this source code (as opposed to the 1.14 + non-echoing password parts) were originally written in Europe. The 1.15 + whole source package can be freely distributed, including from the USA. 1.16 + (Prior to January 2000, re-export from the US was a violation of US law.) 1.17 + 1.18 + This encryption code is a direct transcription of the algorithm from 1.19 + Roger Schlafly, described by Phil Katz in the file appnote.txt. This 1.20 + file (appnote.txt) is distributed with the PKZIP program (even in the 1.21 + version without encryption capabilities). 1.22 + 1.23 + If you don't need crypting in your application, just define symbols 1.24 + NOCRYPT and NOUNCRYPT. 1.25 + 1.26 + This code support the "Traditional PKWARE Encryption". 1.27 + 1.28 + The new AES encryption added on Zip format by Winzip (see the page 1.29 + http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong 1.30 + Encryption is not supported. 1.31 +*/ 1.32 + 1.33 +#define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) 1.34 + 1.35 +/*********************************************************************** 1.36 + * Return the next byte in the pseudo-random sequence 1.37 + */ 1.38 +static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab) 1.39 +{ 1.40 + unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an 1.41 + * unpredictable manner on 16-bit systems; not a problem 1.42 + * with any known compiler so far, though */ 1.43 + 1.44 + temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2; 1.45 + return (int)(((temp * (temp ^ 1)) >> 8) & 0xff); 1.46 +} 1.47 + 1.48 +/*********************************************************************** 1.49 + * Update the encryption keys with the next byte of plain text 1.50 + */ 1.51 +static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c) 1.52 +{ 1.53 + (*(pkeys+0)) = CRC32((*(pkeys+0)), c); 1.54 + (*(pkeys+1)) += (*(pkeys+0)) & 0xff; 1.55 + (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; 1.56 + { 1.57 + register int keyshift = (int)((*(pkeys+1)) >> 24); 1.58 + (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); 1.59 + } 1.60 + return c; 1.61 +} 1.62 + 1.63 + 1.64 +/*********************************************************************** 1.65 + * Initialize the encryption keys and the random header according to 1.66 + * the given password. 1.67 + */ 1.68 +static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t* pcrc_32_tab) 1.69 +{ 1.70 + *(pkeys+0) = 305419896L; 1.71 + *(pkeys+1) = 591751049L; 1.72 + *(pkeys+2) = 878082192L; 1.73 + while (*passwd != '\0') { 1.74 + update_keys(pkeys,pcrc_32_tab,(int)*passwd); 1.75 + passwd++; 1.76 + } 1.77 +} 1.78 + 1.79 +#define zdecode(pkeys,pcrc_32_tab,c) \ 1.80 + (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) 1.81 + 1.82 +#define zencode(pkeys,pcrc_32_tab,c,t) \ 1.83 + (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c)) 1.84 + 1.85 +#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED 1.86 + 1.87 +#define RAND_HEAD_LEN 12 1.88 + /* "last resort" source for second part of crypt seed pattern */ 1.89 +# ifndef ZCR_SEED2 1.90 +# define ZCR_SEED2 3141592654UL /* use PI as default pattern */ 1.91 +# endif 1.92 + 1.93 +static int crypthead(const char* passwd, /* password string */ 1.94 + unsigned char* buf, /* where to write header */ 1.95 + int bufSize, 1.96 + unsigned long* pkeys, 1.97 + const z_crc_t* pcrc_32_tab, 1.98 + unsigned long crcForCrypting) 1.99 +{ 1.100 + int n; /* index in random header */ 1.101 + int t; /* temporary */ 1.102 + int c; /* random byte */ 1.103 + unsigned char header[RAND_HEAD_LEN-2]; /* random header */ 1.104 + static unsigned calls = 0; /* ensure different random header each time */ 1.105 + 1.106 + if (bufSize<RAND_HEAD_LEN) 1.107 + return 0; 1.108 + 1.109 + /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the 1.110 + * output of rand() to get less predictability, since rand() is 1.111 + * often poorly implemented. 1.112 + */ 1.113 + if (++calls == 1) 1.114 + { 1.115 + srand((unsigned)(time(NULL) ^ ZCR_SEED2)); 1.116 + } 1.117 + init_keys(passwd, pkeys, pcrc_32_tab); 1.118 + for (n = 0; n < RAND_HEAD_LEN-2; n++) 1.119 + { 1.120 + c = (rand() >> 7) & 0xff; 1.121 + header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); 1.122 + } 1.123 + /* Encrypt random header (last two bytes is high word of crc) */ 1.124 + init_keys(passwd, pkeys, pcrc_32_tab); 1.125 + for (n = 0; n < RAND_HEAD_LEN-2; n++) 1.126 + { 1.127 + buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t); 1.128 + } 1.129 + buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t); 1.130 + buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t); 1.131 + return n; 1.132 +} 1.133 + 1.134 +#endif