crypt-md5.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2003 Poul-Henning Kamp
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/types.h>
  29. #include <err.h>
  30. #include <md5.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "crypt.h"
  35. /*
  36. * UNIX password
  37. */
  38. int
  39. crypt_md5(const char *pw, const char *salt, char *buffer)
  40. {
  41. MD5_CTX ctx,ctx1;
  42. unsigned long l;
  43. int sl, pl;
  44. u_int i;
  45. u_char final[MD5_SIZE];
  46. const char *ep;
  47. static const char *magic = "$1$";
  48. /* If the salt starts with the magic string, skip that. */
  49. if (!strncmp(salt, magic, strlen(magic)))
  50. salt += strlen(magic);
  51. /* It stops at the first '$', max 8 chars */
  52. for (ep = salt; *ep && *ep != '$' && ep < salt + 8; ep++)
  53. continue;
  54. /* get the length of the true salt */
  55. sl = ep - salt;
  56. MD5Init(&ctx);
  57. /* The password first, since that is what is most unknown */
  58. MD5Update(&ctx, (const u_char *)pw, strlen(pw));
  59. /* Then our magic string */
  60. MD5Update(&ctx, (const u_char *)magic, strlen(magic));
  61. /* Then the raw salt */
  62. MD5Update(&ctx, (const u_char *)salt, (u_int)sl);
  63. /* Then just as many characters of the MD5(pw,salt,pw) */
  64. MD5Init(&ctx1);
  65. MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
  66. MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
  67. MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
  68. MD5Final(final, &ctx1);
  69. for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)
  70. MD5Update(&ctx, (const u_char *)final,
  71. (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl));
  72. /* Don't leave anything around in vm they could use. */
  73. memset(final, 0, sizeof(final));
  74. /* Then something really weird... */
  75. for (i = strlen(pw); i; i >>= 1)
  76. if(i & 1)
  77. MD5Update(&ctx, (const u_char *)final, 1);
  78. else
  79. MD5Update(&ctx, (const u_char *)pw, 1);
  80. /* Now make the output string */
  81. buffer = stpcpy(buffer, magic);
  82. buffer = stpncpy(buffer, salt, (u_int)sl);
  83. *buffer++ = '$';
  84. MD5Final(final, &ctx);
  85. /*
  86. * and now, just to make sure things don't run too fast
  87. * On a 60 Mhz Pentium this takes 34 msec, so you would
  88. * need 30 seconds to build a 1000 entry dictionary...
  89. */
  90. for(i = 0; i < 1000; i++) {
  91. MD5Init(&ctx1);
  92. if(i & 1)
  93. MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
  94. else
  95. MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
  96. if(i % 3)
  97. MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
  98. if(i % 7)
  99. MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
  100. if(i & 1)
  101. MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
  102. else
  103. MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
  104. MD5Final(final, &ctx1);
  105. }
  106. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
  107. _crypt_to64(buffer, l, 4); buffer += 4;
  108. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
  109. _crypt_to64(buffer, l, 4); buffer += 4;
  110. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
  111. _crypt_to64(buffer, l, 4); buffer += 4;
  112. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
  113. _crypt_to64(buffer, l, 4); buffer += 4;
  114. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
  115. _crypt_to64(buffer, l, 4); buffer += 4;
  116. l = final[11];
  117. _crypt_to64(buffer, l, 2); buffer += 2;
  118. *buffer = '\0';
  119. /* Don't leave anything around in vm they could use. */
  120. memset(final, 0, sizeof(final));
  121. return (0);
  122. }