annotate src/zlib-1.2.8/contrib/minizip/crypt.h @ 56:af97cad61ff0

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