crypt.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* crypt.h -- 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. */
  14. #ifndef _MINICRYPT_H
  15. #define _MINICRYPT_H
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. # ifndef ZCR_SEED2
  20. # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
  21. # endif
  22. #if ZLIB_VERNUM < 0x1270
  23. typedef unsigned long z_crc_t;
  24. #endif
  25. #define RAND_HEAD_LEN 12
  26. /***************************************************************************/
  27. #define CRC32(c, b) ((*(pcrc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
  28. /***************************************************************************/
  29. #define zdecode(pkeys,pcrc_32_tab,c) \
  30. (update_keys(pkeys,pcrc_32_tab, c ^= decrypt_byte(pkeys)))
  31. #define zencode(pkeys,pcrc_32_tab,c,t) \
  32. (t = decrypt_byte(pkeys), update_keys(pkeys,pcrc_32_tab,c), t^(c))
  33. /***************************************************************************/
  34. /* Return the next byte in the pseudo-random sequence */
  35. static uint8_t decrypt_byte(uint32_t *pkeys)
  36. {
  37. unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
  38. * unpredictable manner on 16-bit systems; not a problem
  39. * with any known compiler so far, though */
  40. temp = ((uint32_t)(*(pkeys + 2)) & 0xffff) | 2;
  41. return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
  42. }
  43. /* Update the encryption keys with the next byte of plain text */
  44. static uint8_t update_keys(uint32_t *pkeys, const z_crc_t *pcrc_32_tab, int32_t c)
  45. {
  46. (*(pkeys + 0)) = (uint32_t)CRC32((*(pkeys + 0)), c);
  47. (*(pkeys + 1)) += (*(pkeys + 0)) & 0xff;
  48. (*(pkeys + 1)) = (*(pkeys + 1)) * 134775813L + 1;
  49. {
  50. int32_t keyshift = (int32_t)((*(pkeys + 1)) >> 24);
  51. (*(pkeys + 2)) = (uint32_t)CRC32((*(pkeys + 2)), keyshift);
  52. }
  53. return c;
  54. }
  55. /* Initialize the encryption keys and the random header according to the given password. */
  56. static void init_keys(const char *passwd, uint32_t *pkeys, const z_crc_t *pcrc_32_tab)
  57. {
  58. *(pkeys + 0) = 305419896L;
  59. *(pkeys + 1) = 591751049L;
  60. *(pkeys + 2) = 878082192L;
  61. while (*passwd != 0)
  62. {
  63. update_keys(pkeys, pcrc_32_tab, *passwd);
  64. passwd += 1;
  65. }
  66. }
  67. #ifndef NOCRYPT
  68. /* Generate cryptographically secure random numbers */
  69. static int cryptrand(unsigned char *buf, unsigned int len)
  70. {
  71. /*
  72. Important This API is deprecated. New and existing software should
  73. start using Cryptography Next Generation APIs.
  74. Microsoft may remove this API in future releases.
  75. see: https://docs.microsoft.com/zh-cn/windows/desktop/api/wincrypt/nf-wincrypt-cryptgenrandom
  76. */
  77. #define CRYPTGENRANDOM_DEPRECATED 1
  78. #if defined(_WIN32) && !defined(CRYPTGENRANDOM_DEPRECATED)
  79. HCRYPTPROV provider;
  80. unsigned __int64 pentium_tsc[1];
  81. int rlen = 0;
  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. return rlen;
  97. #else
  98. static unsigned calls = 0; /* ensure different random header each time */
  99. /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
  100. * output of rand() to get less predictability, since rand() is
  101. * often poorly implemented.
  102. */
  103. if (++calls == 1)
  104. {
  105. srand((unsigned)(time(NULL) ^ ZCR_SEED2));
  106. }
  107. for(unsigned int i = 0; i < len; ++i){
  108. buf[i] = rand() % 256;
  109. }
  110. return len;
  111. #endif
  112. }
  113. /* Create encryption header */
  114. static 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. #endif
  134. /***************************************************************************/
  135. #endif