crypt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* crypt.c -- base code for traditional PKWARE encryption
  2. Version 1.2.0, September 16th, 2017
  3. Copyright (C) 2012-2017 Nathan Moinvaziri
  4. https://github.com/nmoinvaz/minizip
  5. Copyright (C) 1998-2005 Gilles Vollant
  6. Modifications for Info-ZIP crypting
  7. http://www.winimage.com/zLibDll/minizip.html
  8. Copyright (C) 2003 Terry Thorsen
  9. This code is a modified version of crypting code in Info-ZIP distribution
  10. Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
  11. This program is distributed under the terms of the same license as zlib.
  12. See the accompanying LICENSE file for the full text of the license.
  13. This encryption code is a direct transcription of the algorithm from
  14. Roger Schlafly, described by Phil Katz in the file appnote.txt. This
  15. file (appnote.txt) is distributed with the PKZIP program (even in the
  16. version without encryption capabilities).
  17. If you don't need crypting in your application, just define symbols
  18. NOCRYPT and NOUNCRYPT.
  19. */
  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. /***************************************************************************/
  35. #define CRC32(c, b) ((*(pcrc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
  36. #ifndef ZCR_SEED2
  37. # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
  38. #endif
  39. /***************************************************************************/
  40. uint8_t decrypt_byte(uint32_t *pkeys)
  41. {
  42. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
  43. * unpredictable manner on 16-bit systems; not a problem
  44. * with any known compiler so far, though */
  45. temp = ((uint32_t)(*(pkeys+2)) & 0xffff) | 2;
  46. return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
  47. }
  48. uint8_t update_keys(uint32_t *pkeys, const z_crc_t *pcrc_32_tab, int32_t c)
  49. {
  50. (*(pkeys+0)) = (uint32_t)CRC32((*(pkeys+0)), c);
  51. (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
  52. (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
  53. {
  54. register int32_t keyshift = (int32_t)((*(pkeys + 1)) >> 24);
  55. (*(pkeys+2)) = (uint32_t)CRC32((*(pkeys+2)), keyshift);
  56. }
  57. return c;
  58. }
  59. void init_keys(const char *passwd, uint32_t *pkeys, const z_crc_t *pcrc_32_tab)
  60. {
  61. *(pkeys+0) = 305419896L;
  62. *(pkeys+1) = 591751049L;
  63. *(pkeys+2) = 878082192L;
  64. while (*passwd != 0)
  65. {
  66. update_keys(pkeys, pcrc_32_tab, *passwd);
  67. passwd += 1;
  68. }
  69. }
  70. /***************************************************************************/
  71. int cryptrand(unsigned char *buf, unsigned int len)
  72. {
  73. static unsigned calls = 0;
  74. int rlen = 0;
  75. #ifdef _WIN32
  76. HCRYPTPROV provider;
  77. unsigned __int64 pentium_tsc[1];
  78. int result = 0;
  79. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  80. {
  81. result = CryptGenRandom(provider, len, buf);
  82. CryptReleaseContext(provider, 0);
  83. if (result)
  84. return len;
  85. }
  86. for (rlen = 0; rlen < (int)len; ++rlen)
  87. {
  88. if (rlen % 8 == 0)
  89. QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
  90. buf[rlen] = ((unsigned char*)pentium_tsc)[rlen % 8];
  91. }
  92. #else
  93. int frand = open("/dev/urandom", O_RDONLY);
  94. if (frand != -1)
  95. {
  96. rlen = (int)read(frand, buf, len);
  97. close(frand);
  98. }
  99. #endif
  100. if (rlen < (int)len)
  101. {
  102. /* Ensure different random header each time */
  103. if (++calls == 1)
  104. srand((unsigned)(time(NULL) ^ ZCR_SEED2));
  105. while (rlen < (int)len)
  106. buf[rlen++] = (rand() >> 7) & 0xff;
  107. }
  108. return rlen;
  109. }
  110. int crypthead(const char *passwd, uint8_t *buf, int buf_size, uint32_t *pkeys,
  111. const z_crc_t *pcrc_32_tab, uint8_t verify1, uint8_t verify2)
  112. {
  113. uint8_t n = 0; /* index in random header */
  114. uint8_t header[RAND_HEAD_LEN-2]; /* random header */
  115. uint16_t t = 0; /* temporary */
  116. if (buf_size < RAND_HEAD_LEN)
  117. return 0;
  118. init_keys(passwd, pkeys, pcrc_32_tab);
  119. /* First generate RAND_HEAD_LEN-2 random bytes. */
  120. cryptrand(header, RAND_HEAD_LEN-2);
  121. /* Encrypt random header (last two bytes is high word of crc) */
  122. init_keys(passwd, pkeys, pcrc_32_tab);
  123. for (n = 0; n < RAND_HEAD_LEN-2; n++)
  124. buf[n] = (uint8_t)zencode(pkeys, pcrc_32_tab, header[n], t);
  125. buf[n++] = (uint8_t)zencode(pkeys, pcrc_32_tab, verify1, t);
  126. buf[n++] = (uint8_t)zencode(pkeys, pcrc_32_tab, verify2, t);
  127. return n;
  128. }
  129. /***************************************************************************/