mycrypt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #include "libs/md5.h"
  7. #include "libs/md4.h"
  8. #include <string.h>
  9. #define MD5_SIZE 16
  10. #ifdef _WIN32
  11. #pragma warning (disable : 4996)
  12. #endif
  13. void tohex(unsigned char *in, unsigned char *out, int len);
  14. static unsigned char itoa64[] =
  15. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  16. void
  17. _crypt_to64(unsigned char *s, unsigned long v, int n)
  18. {
  19. while (--n >= 0) {
  20. *s++ = itoa64[v&0x3f];
  21. v >>= 6;
  22. }
  23. }
  24. unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int ctohex)
  25. {
  26. unsigned char szUnicodePass[513];
  27. unsigned int nPasswordLen;
  28. MD4_CTX ctx;
  29. unsigned int i;
  30. /*
  31. * NT passwords are unicode. Convert plain text password
  32. * to unicode by inserting a zero every other byte
  33. */
  34. nPasswordLen = (int)strlen((char *)szPassword);
  35. if(nPasswordLen > 255)nPasswordLen = 255;
  36. for (i = 0; i < nPasswordLen; i++) {
  37. szUnicodePass[i << 1] = szPassword[i];
  38. szUnicodePass[(i << 1) + 1] = 0;
  39. }
  40. /* Encrypt Unicode password to a 16-byte MD4 hash */
  41. MD4Init(&ctx);
  42. MD4Update(&ctx, szUnicodePass, (nPasswordLen<<1));
  43. MD4Final(szUnicodePass, &ctx);
  44. if (ctohex){
  45. tohex(szUnicodePass, szHash, 16);
  46. }
  47. else memcpy(szHash, szUnicodePass, 16);
  48. return szHash;
  49. }
  50. unsigned char * mycrypt(const unsigned char *pw, const unsigned char *salt, unsigned char *passwd){
  51. const unsigned char *ep;
  52. if(salt[0] == '$' && salt[1] == '1' && salt[2] == '$' && (ep = (unsigned char *)strchr((char *)salt+3, '$'))) {
  53. static unsigned char *magic = (unsigned char *)"$1$";
  54. unsigned char *p;
  55. const unsigned char *sp;
  56. unsigned char final[MD5_SIZE];
  57. int sl,pl,i;
  58. MD5_CTX ctx,ctx1;
  59. unsigned long l;
  60. /* Refine the Salt first */
  61. sp = salt +3;
  62. /* get the length of the true salt */
  63. sl = (int)(ep - sp);
  64. MD5Init(&ctx);
  65. /* The password first, since that is what is most unknown */
  66. MD5Update(&ctx,pw,strlen((char *)pw));
  67. /* Then our magic string */
  68. MD5Update(&ctx,magic,strlen((char *)magic));
  69. /* Then the raw salt */
  70. MD5Update(&ctx,sp,sl);
  71. /* Then just as many unsigned characters of the MD5(pw,salt,pw) */
  72. MD5Init(&ctx1);
  73. MD5Update(&ctx1,pw,strlen((char *)pw));
  74. MD5Update(&ctx1,sp,sl);
  75. MD5Update(&ctx1,pw,strlen((char *)pw));
  76. MD5Final(final,&ctx1);
  77. for(pl = (int)strlen((char *)pw); pl > 0; pl -= MD5_SIZE)
  78. MD5Update(&ctx,final,pl>MD5_SIZE ? MD5_SIZE : pl);
  79. /* Don't leave anything around in vm they could use. */
  80. memset(final,0,sizeof final);
  81. /* Then something really weird... */
  82. for (i = (int)strlen((char *)pw); i ; i >>= 1)
  83. if(i&1)
  84. MD5Update(&ctx, final, 1);
  85. else
  86. MD5Update(&ctx, pw, 1);
  87. /* Now make the output string */
  88. strcpy((char *)passwd,(char *)magic);
  89. strncat((char *)passwd,(char *)sp,sl);
  90. strcat((char *)passwd,"$");
  91. MD5Final(final,&ctx);
  92. /*
  93. * and now, just to make sure things don't run too fast
  94. * On a 60 Mhz Pentium this takes 34 msec, so you would
  95. * need 30 seconds to build a 1000 entry dictionary...
  96. */
  97. for(i=0;i<1000;i++) {
  98. MD5Init(&ctx1);
  99. if(i & 1)
  100. MD5Update(&ctx1,pw,strlen((char *)pw));
  101. else
  102. MD5Update(&ctx1,final,MD5_SIZE);
  103. if(i % 3)
  104. MD5Update(&ctx1,sp,sl);
  105. if(i % 7)
  106. MD5Update(&ctx1,pw,strlen((char *)pw));
  107. if(i & 1)
  108. MD5Update(&ctx1,final,MD5_SIZE);
  109. else
  110. MD5Update(&ctx1,pw,strlen((char *)pw));
  111. MD5Final(final,&ctx1);
  112. }
  113. p = passwd + strlen((char *)passwd);
  114. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
  115. _crypt_to64(p,l,4); p += 4;
  116. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
  117. _crypt_to64(p,l,4); p += 4;
  118. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
  119. _crypt_to64(p,l,4); p += 4;
  120. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
  121. _crypt_to64(p,l,4); p += 4;
  122. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
  123. _crypt_to64(p,l,4); p += 4;
  124. l = final[11] ;
  125. _crypt_to64(p,l,2); p += 2;
  126. *p = '\0';
  127. /* Don't leave anything around in vm they could use. */
  128. memset(final,0,sizeof final);
  129. }
  130. else {
  131. *passwd = 0;
  132. }
  133. return passwd;
  134. }
  135. #ifdef WITHMAIN
  136. #include <stdio.h>
  137. int main(int argc, char* argv[]){
  138. unsigned char buf[1024];
  139. unsigned i;
  140. if(argc < 2 || argc > 3) {
  141. fprintf(stderr, "usage: \n"
  142. "\t%s <password>\n"
  143. "\t%s <salt> <password>\n"
  144. "Performs NT crypt if no salt specified, MD5 crypt with salt\n"
  145. "This software uses:\n"
  146. " RSA Data Security, Inc. MD4 Message-Digest Algorithm\n"
  147. " RSA Data Security, Inc. MD5 Message-Digest Algorithm\n",
  148. argv[0],
  149. argv[0]);
  150. return 1;
  151. }
  152. if(argc == 2) {
  153. printf("NT:%s\n", ntpwdhash(buf, (unsigned char *)argv[1], 1));
  154. }
  155. else {
  156. i = (int)strlen((char *)argv[1]);
  157. if (i > 64) argv[1][64] = 0;
  158. sprintf((char *)buf, "$1$%s$", argv[1]);
  159. printf("CR:%s\n", mycrypt((unsigned char *)argv[2], buf, buf+256));
  160. }
  161. return 0;
  162. }
  163. #endif