mycrypt.c 4.9 KB

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