crypt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* crypt.c -- 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. This program is distributed under the terms of the same license as zlib.
  9. See the accompanying LICENSE file for the full text of the license.
  10. This encryption code is a direct transcription of the algorithm from
  11. Roger Schlafly, described by Phil Katz in the file appnote.txt. This
  12. file (appnote.txt) is distributed with the PKZIP program (even in the
  13. version without encryption capabilities).
  14. If you don't need crypting in your application, just define symbols
  15. NOCRYPT and NOUNCRYPT.
  16. */
  17. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
  18. #define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0
  19. #endif
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <time.h>
  24. #ifdef _WIN32
  25. # include <windows.h>
  26. # include <wincrypt.h>
  27. #else
  28. # include <sys/stat.h>
  29. # include <fcntl.h>
  30. # include <unistd.h>
  31. #endif
  32. #include "zlib.h"
  33. #include "crypt.h"
  34. #ifdef _MSC_VER
  35. # pragma warning(push)
  36. # pragma warning(disable : 4244)
  37. #endif // _MSC_VER
  38. /***************************************************************************/
  39. #define CRC32(c, b) ((*(pcrc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
  40. #ifndef ZCR_SEED2
  41. # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
  42. #endif
  43. /***************************************************************************/
  44. uint8_t decrypt_byte(uint32_t *pkeys)
  45. {
  46. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
  47. * unpredictable manner on 16-bit systems; not a problem
  48. * with any known compiler so far, though */
  49. temp = ((uint32_t)(*(pkeys+2)) & 0xffff) | 2;
  50. return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
  51. }
  52. uint8_t update_keys(uint32_t *pkeys, const z_crc_t *pcrc_32_tab, int32_t c)
  53. {
  54. (*(pkeys+0)) = (uint32_t)CRC32((*(pkeys+0)), c);
  55. (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
  56. (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
  57. {
  58. register int32_t keyshift = (int32_t)((*(pkeys + 1)) >> 24);
  59. (*(pkeys+2)) = (uint32_t)CRC32((*(pkeys+2)), keyshift);
  60. }
  61. return c;
  62. }
  63. void init_keys(const char *passwd, uint32_t *pkeys, const z_crc_t *pcrc_32_tab)
  64. {
  65. *(pkeys+0) = 305419896L;
  66. *(pkeys+1) = 591751049L;
  67. *(pkeys+2) = 878082192L;
  68. while (*passwd != 0)
  69. {
  70. update_keys(pkeys, pcrc_32_tab, *passwd);
  71. passwd += 1;
  72. }
  73. }
  74. /***************************************************************************/
  75. int cryptrand(unsigned char *buf, unsigned int len)
  76. {
  77. static unsigned calls = 0;
  78. int rlen = 0;
  79. #ifdef _WIN32
  80. HCRYPTPROV provider;
  81. unsigned __int64 pentium_tsc[1];
  82. int result = 0;
  83. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  84. {
  85. result = CryptGenRandom(provider, len, buf);
  86. CryptReleaseContext(provider, 0);
  87. if (result)
  88. return len;
  89. }
  90. for (rlen = 0; rlen < (int)len; ++rlen)
  91. {
  92. if (rlen % 8 == 0)
  93. QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
  94. buf[rlen] = ((unsigned char*)pentium_tsc)[rlen % 8];
  95. }
  96. #else
  97. int frand = open("/dev/urandom", O_RDONLY);
  98. if (frand != -1)
  99. {
  100. rlen = (int)read(frand, buf, len);
  101. close(frand);
  102. }
  103. #endif
  104. if (rlen < (int)len)
  105. {
  106. /* Ensure different random header each time */
  107. if (++calls == 1)
  108. srand((unsigned)(time(NULL) ^ ZCR_SEED2));
  109. while (rlen < (int)len)
  110. buf[rlen++] = (rand() >> 7) & 0xff;
  111. }
  112. return rlen;
  113. }
  114. int crypthead(const char *passwd, uint8_t *buf, int buf_size, uint32_t *pkeys,
  115. const z_crc_t *pcrc_32_tab, uint8_t verify1, uint8_t verify2)
  116. {
  117. uint8_t n = 0; /* index in random header */
  118. uint8_t header[RAND_HEAD_LEN-2]; /* random header */
  119. uint16_t t = 0; /* temporary */
  120. if (buf_size < RAND_HEAD_LEN)
  121. return 0;
  122. init_keys(passwd, pkeys, pcrc_32_tab);
  123. /* First generate RAND_HEAD_LEN-2 random bytes. */
  124. cryptrand(header, RAND_HEAD_LEN-2);
  125. /* Encrypt random header (last two bytes is high word of crc) */
  126. init_keys(passwd, pkeys, pcrc_32_tab);
  127. for (n = 0; n < RAND_HEAD_LEN-2; n++)
  128. buf[n] = (uint8_t)zencode(pkeys, pcrc_32_tab, header[n], t);
  129. buf[n++] = (uint8_t)zencode(pkeys, pcrc_32_tab, verify1, t);
  130. buf[n++] = (uint8_t)zencode(pkeys, pcrc_32_tab, verify2, t);
  131. return n;
  132. }
  133. #ifdef _MSC_VER
  134. # pragma warning(pop)
  135. #endif // _MSC_VER
  136. /***************************************************************************/