crypt.h 4.7 KB

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