md5_crypt.c 4.2 KB

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