annotate src/zlib-1.2.7/contrib/minizip/crypt.h @ 148:b4bfdf10c4b3

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