packvfs

annotate 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
rev   line source
nuclear@3 1 /* crypt.h -- base code for crypt/uncrypt ZIPfile
nuclear@3 2
nuclear@3 3
nuclear@3 4 Version 1.01e, February 12th, 2005
nuclear@3 5
nuclear@3 6 Copyright (C) 1998-2005 Gilles Vollant
nuclear@3 7
nuclear@3 8 This code is a modified version of crypting code in Infozip distribution
nuclear@3 9
nuclear@3 10 The encryption/decryption parts of this source code (as opposed to the
nuclear@3 11 non-echoing password parts) were originally written in Europe. The
nuclear@3 12 whole source package can be freely distributed, including from the USA.
nuclear@3 13 (Prior to January 2000, re-export from the US was a violation of US law.)
nuclear@3 14
nuclear@3 15 This encryption code is a direct transcription of the algorithm from
nuclear@3 16 Roger Schlafly, described by Phil Katz in the file appnote.txt. This
nuclear@3 17 file (appnote.txt) is distributed with the PKZIP program (even in the
nuclear@3 18 version without encryption capabilities).
nuclear@3 19
nuclear@3 20 If you don't need crypting in your application, just define symbols
nuclear@3 21 NOCRYPT and NOUNCRYPT.
nuclear@3 22
nuclear@3 23 This code support the "Traditional PKWARE Encryption".
nuclear@3 24
nuclear@3 25 The new AES encryption added on Zip format by Winzip (see the page
nuclear@3 26 http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong
nuclear@3 27 Encryption is not supported.
nuclear@3 28 */
nuclear@3 29
nuclear@3 30 #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
nuclear@3 31
nuclear@3 32 /***********************************************************************
nuclear@3 33 * Return the next byte in the pseudo-random sequence
nuclear@3 34 */
nuclear@3 35 static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab)
nuclear@3 36 {
nuclear@3 37 unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
nuclear@3 38 * unpredictable manner on 16-bit systems; not a problem
nuclear@3 39 * with any known compiler so far, though */
nuclear@3 40
nuclear@3 41 temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
nuclear@3 42 return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
nuclear@3 43 }
nuclear@3 44
nuclear@3 45 /***********************************************************************
nuclear@3 46 * Update the encryption keys with the next byte of plain text
nuclear@3 47 */
nuclear@3 48 static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c)
nuclear@3 49 {
nuclear@3 50 (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
nuclear@3 51 (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
nuclear@3 52 (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
nuclear@3 53 {
nuclear@3 54 register int keyshift = (int)((*(pkeys+1)) >> 24);
nuclear@3 55 (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
nuclear@3 56 }
nuclear@3 57 return c;
nuclear@3 58 }
nuclear@3 59
nuclear@3 60
nuclear@3 61 /***********************************************************************
nuclear@3 62 * Initialize the encryption keys and the random header according to
nuclear@3 63 * the given password.
nuclear@3 64 */
nuclear@3 65 static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t* pcrc_32_tab)
nuclear@3 66 {
nuclear@3 67 *(pkeys+0) = 305419896L;
nuclear@3 68 *(pkeys+1) = 591751049L;
nuclear@3 69 *(pkeys+2) = 878082192L;
nuclear@3 70 while (*passwd != '\0') {
nuclear@3 71 update_keys(pkeys,pcrc_32_tab,(int)*passwd);
nuclear@3 72 passwd++;
nuclear@3 73 }
nuclear@3 74 }
nuclear@3 75
nuclear@3 76 #define zdecode(pkeys,pcrc_32_tab,c) \
nuclear@3 77 (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
nuclear@3 78
nuclear@3 79 #define zencode(pkeys,pcrc_32_tab,c,t) \
nuclear@3 80 (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
nuclear@3 81
nuclear@3 82 #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
nuclear@3 83
nuclear@3 84 #define RAND_HEAD_LEN 12
nuclear@3 85 /* "last resort" source for second part of crypt seed pattern */
nuclear@3 86 # ifndef ZCR_SEED2
nuclear@3 87 # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
nuclear@3 88 # endif
nuclear@3 89
nuclear@3 90 static int crypthead(const char* passwd, /* password string */
nuclear@3 91 unsigned char* buf, /* where to write header */
nuclear@3 92 int bufSize,
nuclear@3 93 unsigned long* pkeys,
nuclear@3 94 const z_crc_t* pcrc_32_tab,
nuclear@3 95 unsigned long crcForCrypting)
nuclear@3 96 {
nuclear@3 97 int n; /* index in random header */
nuclear@3 98 int t; /* temporary */
nuclear@3 99 int c; /* random byte */
nuclear@3 100 unsigned char header[RAND_HEAD_LEN-2]; /* random header */
nuclear@3 101 static unsigned calls = 0; /* ensure different random header each time */
nuclear@3 102
nuclear@3 103 if (bufSize<RAND_HEAD_LEN)
nuclear@3 104 return 0;
nuclear@3 105
nuclear@3 106 /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
nuclear@3 107 * output of rand() to get less predictability, since rand() is
nuclear@3 108 * often poorly implemented.
nuclear@3 109 */
nuclear@3 110 if (++calls == 1)
nuclear@3 111 {
nuclear@3 112 srand((unsigned)(time(NULL) ^ ZCR_SEED2));
nuclear@3 113 }
nuclear@3 114 init_keys(passwd, pkeys, pcrc_32_tab);
nuclear@3 115 for (n = 0; n < RAND_HEAD_LEN-2; n++)
nuclear@3 116 {
nuclear@3 117 c = (rand() >> 7) & 0xff;
nuclear@3 118 header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
nuclear@3 119 }
nuclear@3 120 /* Encrypt random header (last two bytes is high word of crc) */
nuclear@3 121 init_keys(passwd, pkeys, pcrc_32_tab);
nuclear@3 122 for (n = 0; n < RAND_HEAD_LEN-2; n++)
nuclear@3 123 {
nuclear@3 124 buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
nuclear@3 125 }
nuclear@3 126 buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
nuclear@3 127 buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
nuclear@3 128 return n;
nuclear@3 129 }
nuclear@3 130
nuclear@3 131 #endif