crypt.h 4.6 KB

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