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