php_crypt_r.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Pierre Alain Joye <pajoye@php.net |
  14. +----------------------------------------------------------------------+
  15. */
  16. /*
  17. * License for the Unix md5crypt implementation (md5_crypt):
  18. *
  19. * ----------------------------------------------------------------------------
  20. * "THE BEER-WARE LICENSE" (Revision 42):
  21. * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
  22. * can do whatever you want with this stuff. If we meet some day, and you think
  23. * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  24. * ----------------------------------------------------------------------------
  25. *
  26. * from FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp
  27. * via OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp
  28. * via NetBSD: md5crypt.c,v 1.4.2.1 2002/01/22 19:31:59 he Exp
  29. *
  30. */
  31. #include "php.h"
  32. #include <string.h>
  33. #ifdef PHP_WIN32
  34. # include <windows.h>
  35. # include <Wincrypt.h>
  36. #endif
  37. #include "php_crypt_r.h"
  38. #include "crypt_freesec.h"
  39. #include "ext/standard/md5.h"
  40. #ifdef ZTS
  41. MUTEX_T php_crypt_extended_init_lock;
  42. #endif
  43. void php_init_crypt_r(void)
  44. {
  45. #ifdef ZTS
  46. php_crypt_extended_init_lock = tsrm_mutex_alloc();
  47. #endif
  48. }
  49. void php_shutdown_crypt_r(void)
  50. {
  51. #ifdef ZTS
  52. tsrm_mutex_free(php_crypt_extended_init_lock);
  53. #endif
  54. }
  55. void _crypt_extended_init_r(void)
  56. {
  57. static int initialized = 0;
  58. #ifdef ZTS
  59. tsrm_mutex_lock(php_crypt_extended_init_lock);
  60. #endif
  61. if (!initialized) {
  62. initialized = 1;
  63. _crypt_extended_init();
  64. }
  65. #ifdef ZTS
  66. tsrm_mutex_unlock(php_crypt_extended_init_lock);
  67. #endif
  68. }
  69. /* MD5 crypt implementation using the windows CryptoApi */
  70. #define MD5_MAGIC "$1$"
  71. #define MD5_MAGIC_LEN 3
  72. static const unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
  73. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  74. /* Convert a 16/32 bit integer to Base64 string representation */
  75. static void to64(char *s, int32_t v, int n)
  76. {
  77. while (--n >= 0) {
  78. *s++ = itoa64[v & 0x3f];
  79. v >>= 6;
  80. }
  81. }
  82. /*
  83. * MD5 password encryption.
  84. */
  85. char * php_md5_crypt_r(const char *pw, const char *salt, char *out)
  86. {
  87. ZEND_TLS char passwd[MD5_HASH_MAX_LEN], *p;
  88. const char *sp, *ep;
  89. unsigned char final[16];
  90. unsigned int i, sl, pwl;
  91. PHP_MD5_CTX ctx, ctx1;
  92. uint32_t l;
  93. int pl;
  94. pwl = strlen(pw);
  95. /* Refine the salt first */
  96. sp = salt;
  97. /* If it starts with the magic string, then skip that */
  98. if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0)
  99. sp += MD5_MAGIC_LEN;
  100. /* It stops at the first '$', max 8 chars */
  101. for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++);
  102. /* get the length of the true salt */
  103. sl = ep - sp;
  104. PHP_MD5Init(&ctx);
  105. /* The password first, since that is what is most unknown */
  106. PHP_MD5Update(&ctx, (const unsigned char *)pw, pwl);
  107. /* Then our magic string */
  108. PHP_MD5Update(&ctx, (const unsigned char *)MD5_MAGIC, MD5_MAGIC_LEN);
  109. /* Then the raw salt */
  110. PHP_MD5Update(&ctx, (const unsigned char *)sp, sl);
  111. /* Then just as many characters of the MD5(pw,salt,pw) */
  112. PHP_MD5Init(&ctx1);
  113. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  114. PHP_MD5Update(&ctx1, (const unsigned char *)sp, sl);
  115. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  116. PHP_MD5Final(final, &ctx1);
  117. for (pl = pwl; pl > 0; pl -= 16)
  118. PHP_MD5Update(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
  119. /* Don't leave anything around in vm they could use. */
  120. ZEND_SECURE_ZERO(final, sizeof(final));
  121. /* Then something really weird... */
  122. for (i = pwl; i != 0; i >>= 1)
  123. if ((i & 1) != 0)
  124. PHP_MD5Update(&ctx, final, 1);
  125. else
  126. PHP_MD5Update(&ctx, (const unsigned char *)pw, 1);
  127. /* Now make the output string */
  128. memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
  129. strlcpy(passwd + MD5_MAGIC_LEN, sp, sl + 1);
  130. strcat(passwd, "$");
  131. PHP_MD5Final(final, &ctx);
  132. /*
  133. * And now, just to make sure things don't run too fast. On a 60 MHz
  134. * Pentium this takes 34 msec, so you would need 30 seconds to build
  135. * a 1000 entry dictionary...
  136. */
  137. for (i = 0; i < 1000; i++) {
  138. PHP_MD5Init(&ctx1);
  139. if ((i & 1) != 0)
  140. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  141. else
  142. PHP_MD5Update(&ctx1, final, 16);
  143. if ((i % 3) != 0)
  144. PHP_MD5Update(&ctx1, (const unsigned char *)sp, sl);
  145. if ((i % 7) != 0)
  146. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  147. if ((i & 1) != 0)
  148. PHP_MD5Update(&ctx1, final, 16);
  149. else
  150. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  151. PHP_MD5Final(final, &ctx1);
  152. }
  153. p = passwd + sl + MD5_MAGIC_LEN + 1;
  154. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
  155. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
  156. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
  157. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
  158. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
  159. l = final[11] ; to64(p,l,2); p += 2;
  160. *p = '\0';
  161. /* Don't leave anything around in vm they could use. */
  162. ZEND_SECURE_ZERO(final, sizeof(final));
  163. return (passwd);
  164. }