md5crypt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * <phk@login.dknet.dk> wrote this file. As long as you retain this
  5. * notice you can do whatever you want with this stuff. If we meet some
  6. * day, and you think this stuff is worth it, you can buy me a beer in
  7. * return. Poul-Henning Kamp
  8. * ----------------------------------------------------------------------------
  9. */
  10. #include "includes.h"
  11. #if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
  12. #include <sys/types.h>
  13. #include <string.h>
  14. #include <openssl/md5.h>
  15. /* 0 ... 63 => ascii - 64 */
  16. static unsigned char itoa64[] =
  17. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  18. static char *magic = "$1$";
  19. static char *
  20. to64(unsigned long v, int n)
  21. {
  22. static char buf[5];
  23. char *s = buf;
  24. if (n > 4)
  25. return (NULL);
  26. memset(buf, '\0', sizeof(buf));
  27. while (--n >= 0) {
  28. *s++ = itoa64[v&0x3f];
  29. v >>= 6;
  30. }
  31. return (buf);
  32. }
  33. int
  34. is_md5_salt(const char *salt)
  35. {
  36. return (strncmp(salt, magic, strlen(magic)) == 0);
  37. }
  38. char *
  39. md5_crypt(const char *pw, const char *salt)
  40. {
  41. static char passwd[120], salt_copy[9];
  42. static const char *sp, *ep;
  43. unsigned char final[16];
  44. int sl, pl, i, j;
  45. MD5_CTX ctx, ctx1;
  46. unsigned long l;
  47. /* Refine the Salt first */
  48. sp = salt;
  49. /* If it starts with the magic string, then skip that */
  50. if(strncmp(sp, magic, strlen(magic)) == 0)
  51. sp += strlen(magic);
  52. /* It stops at the first '$', max 8 chars */
  53. for (ep = sp; *ep != '$'; ep++) {
  54. if (*ep == '\0' || ep >= (sp + 8))
  55. return (NULL);
  56. }
  57. /* get the length of the true salt */
  58. sl = ep - sp;
  59. /* Stash the salt */
  60. memcpy(salt_copy, sp, sl);
  61. salt_copy[sl] = '\0';
  62. MD5_Init(&ctx);
  63. /* The password first, since that is what is most unknown */
  64. MD5_Update(&ctx, pw, strlen(pw));
  65. /* Then our magic string */
  66. MD5_Update(&ctx, magic, strlen(magic));
  67. /* Then the raw salt */
  68. MD5_Update(&ctx, sp, sl);
  69. /* Then just as many characters of the MD5(pw, salt, pw) */
  70. MD5_Init(&ctx1);
  71. MD5_Update(&ctx1, pw, strlen(pw));
  72. MD5_Update(&ctx1, sp, sl);
  73. MD5_Update(&ctx1, pw, strlen(pw));
  74. MD5_Final(final, &ctx1);
  75. for(pl = strlen(pw); pl > 0; pl -= 16)
  76. MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
  77. /* Don't leave anything around in vm they could use. */
  78. memset(final, '\0', sizeof final);
  79. /* Then something really weird... */
  80. for (j = 0, i = strlen(pw); i != 0; i >>= 1)
  81. if (i & 1)
  82. MD5_Update(&ctx, final + j, 1);
  83. else
  84. MD5_Update(&ctx, pw + j, 1);
  85. /* Now make the output string */
  86. snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
  87. MD5_Final(final, &ctx);
  88. /*
  89. * and now, just to make sure things don't run too fast
  90. * On a 60 Mhz Pentium this takes 34 msec, so you would
  91. * need 30 seconds to build a 1000 entry dictionary...
  92. */
  93. for(i = 0; i < 1000; i++) {
  94. MD5_Init(&ctx1);
  95. if (i & 1)
  96. MD5_Update(&ctx1, pw, strlen(pw));
  97. else
  98. MD5_Update(&ctx1, final, 16);
  99. if (i % 3)
  100. MD5_Update(&ctx1, sp, sl);
  101. if (i % 7)
  102. MD5_Update(&ctx1, pw, strlen(pw));
  103. if (i & 1)
  104. MD5_Update(&ctx1, final, 16);
  105. else
  106. MD5_Update(&ctx1, pw, strlen(pw));
  107. MD5_Final(final, &ctx1);
  108. }
  109. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
  110. strlcat(passwd, to64(l, 4), sizeof(passwd));
  111. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
  112. strlcat(passwd, to64(l, 4), sizeof(passwd));
  113. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
  114. strlcat(passwd, to64(l, 4), sizeof(passwd));
  115. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
  116. strlcat(passwd, to64(l, 4), sizeof(passwd));
  117. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
  118. strlcat(passwd, to64(l, 4), sizeof(passwd));
  119. l = final[11] ;
  120. strlcat(passwd, to64(l, 2), sizeof(passwd));
  121. /* Don't leave anything around in vm they could use. */
  122. memset(final, 0, sizeof(final));
  123. memset(salt_copy, 0, sizeof(salt_copy));
  124. memset(&ctx, 0, sizeof(ctx));
  125. memset(&ctx1, 0, sizeof(ctx1));
  126. (void)to64(0, 4);
  127. return (passwd);
  128. }
  129. #endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */