暫無描述

crypt.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* crypt.h -- base code for traditional PKWARE encryption
  2. Version 1.01e, February 12th, 2005
  3. Copyright (C) 1998-2005 Gilles Vollant
  4. Modifications for Info-ZIP crypting
  5. Copyright (C) 2003 Terry Thorsen
  6. This code is a modified version of crypting code in Info-ZIP distribution
  7. Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
  8. See the Info-ZIP LICENSE file version 2000-Apr-09 or later for terms of use
  9. which also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  10. The encryption/decryption parts of this source code (as opposed to the
  11. non-echoing password parts) were originally written in Europe. The
  12. whole source package can be freely distributed, including from the USA.
  13. (Prior to January 2000, re-export from the US was a violation of US law.)
  14. This encryption code is a direct transcription of the algorithm from
  15. Roger Schlafly, described by Phil Katz in the file appnote.txt. This
  16. file (appnote.txt) is distributed with the PKZIP program (even in the
  17. version without encryption capabilities).
  18. If you don't need crypting in your application, just define symbols
  19. NOCRYPT and NOUNCRYPT.
  20. */
  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 unsigned long* pcrc_32_tab)
  26. {
  27. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
  28. * unpredictable manner on 16-bit systems; not a problem
  29. * with any known compiler so far, though */
  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 unsigned long* 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 unsigned long* 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), 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 3141592654UL /* use PI as default pattern */
  70. # endif
  71. static int crypthead(const char* passwd, /* password string */
  72. unsigned char* buf, /* where to write header */
  73. int bufSize,
  74. unsigned long* pkeys,
  75. const unsigned long* pcrc_32_tab,
  76. unsigned long crcForCrypting)
  77. {
  78. int 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