annotate src/zlib-1.2.7/contrib/minizip/crypt.h @ 4:e13257ea84a4

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