annotate src/zlib-1.2.8/contrib/minizip/crypt.h @ 155:54abead6ecce

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