crypt.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
  21. /***********************************************************************
  22. * Return the next byte in the pseudo-random sequence
  23. */
  24. static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab)
  25. {
  26. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
  27. * unpredictable manner on 16-bit systems; not a problem
  28. * with any known compiler so far, though */
  29. (void)pcrc_32_tab;
  30. temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
  31. return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
  32. }
  33. /***********************************************************************
  34. * Update the encryption keys with the next byte of plain text
  35. */
  36. static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c)
  37. {
  38. (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
  39. (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
  40. (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
  41. {
  42. register int keyshift = (int)((*(pkeys+1)) >> 24);
  43. (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
  44. }
  45. return c;
  46. }
  47. /***********************************************************************
  48. * Initialize the encryption keys and the random header according to
  49. * the given password.
  50. */
  51. static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t* pcrc_32_tab)
  52. {
  53. *(pkeys+0) = 305419896L;
  54. *(pkeys+1) = 591751049L;
  55. *(pkeys+2) = 878082192L;
  56. while (*passwd != '\0') {
  57. update_keys(pkeys,pcrc_32_tab,(int)*passwd);
  58. passwd++;
  59. }
  60. }
  61. #define zdecode(pkeys,pcrc_32_tab,c) \
  62. (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
  63. #define zencode(pkeys,pcrc_32_tab,c,t) \
  64. (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), (Byte)t^(c))
  65. #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
  66. #define RAND_HEAD_LEN 12
  67. /* "last resort" source for second part of crypt seed pattern */
  68. # ifndef ZCR_SEED2
  69. # define ZCR_SEED2 3141592654L /* use PI as default pattern */
  70. # endif
  71. static unsigned crypthead(const char* passwd, /* password string */
  72. unsigned char* buf, /* where to write header */
  73. int bufSize,
  74. unsigned long* pkeys,
  75. const z_crc_t* pcrc_32_tab,
  76. unsigned long crcForCrypting)
  77. {
  78. unsigned n; /* index in random header */
  79. int t; /* temporary */
  80. int c; /* random byte */
  81. unsigned char header[RAND_HEAD_LEN-2]; /* random header */
  82. static unsigned calls = 0; /* ensure different random header each time */
  83. if (bufSize<RAND_HEAD_LEN)
  84. return 0;
  85. /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
  86. * output of rand() to get less predictability, since rand() is
  87. * often poorly implemented.
  88. */
  89. if (++calls == 1)
  90. {
  91. srand((unsigned)(time(NULL) ^ ZCR_SEED2));
  92. }
  93. init_keys(passwd, pkeys, pcrc_32_tab);
  94. for (n = 0; n < RAND_HEAD_LEN-2; n++)
  95. {
  96. c = (rand() >> 7) & 0xff;
  97. header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
  98. }
  99. /* Encrypt random header (last two bytes is high word of crc) */
  100. init_keys(passwd, pkeys, pcrc_32_tab);
  101. for (n = 0; n < RAND_HEAD_LEN-2; n++)
  102. {
  103. buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
  104. }
  105. buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
  106. buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
  107. return n;
  108. }
  109. #endif