PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trf2.1.4/md5-crypt/md5-crypt.c

#
C | 250 lines | 132 code | 51 blank | 67 comment | 22 complexity | 556ef8c10931f8e3a0f1f06d715064a2 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0, 0BSD
  1. /* One way encryption based on MD5 sum.
  2. Copyright (C) 1996, 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <tcl.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifndef WIN32
  22. #include <sys/param.h>
  23. #endif
  24. #include "md5.h"
  25. #ifndef MAX
  26. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  27. #endif
  28. #ifndef MIN
  29. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  30. #endif
  31. #ifndef __set_errno
  32. # define __set_errno(val) errno = (val)
  33. #endif
  34. # define __md5_crypt_r md5_crypt_r
  35. # define __md5_crypt md5_crypt
  36. # define __stpncpy stpncpy
  37. # define __set_errno(val) errno = (val)
  38. char * __stpncpy _ANSI_ARGS_((char *dest, const char *src, size_t n));
  39. /* Define our magic string to mark salt for MD5 "encryption"
  40. replacement. This is meant to be the same as for other MD5 based
  41. encryption implementations. */
  42. static const char md5_salt_prefix[] = "$1$";
  43. /* Table with characters for base64 transformation. */
  44. static const char b64t[64] =
  45. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  46. /* Prototypes for local functions. */
  47. extern char *__md5_crypt_r _ANSI_ARGS_ ((const char *key, const char *salt,
  48. char *buffer, int buflen));
  49. extern char *__md5_crypt _ANSI_ARGS_ ((const char *key, const char *salt));
  50. /* This entry point is equivalent to the `crypt' function in Unix
  51. libcs. */
  52. char *
  53. __md5_crypt_r (key, salt, buffer, buflen)
  54. const char *key;
  55. const char *salt;
  56. char *buffer;
  57. int buflen;
  58. {
  59. unsigned char alt_result[16];
  60. struct md5_ctx ctx;
  61. struct md5_ctx alt_ctx;
  62. size_t salt_len;
  63. size_t key_len;
  64. size_t cnt;
  65. char *cp;
  66. /* Find beginning of salt string. The prefix should normally always
  67. be present. Just in case it is not. */
  68. if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
  69. /* Skip salt prefix. */
  70. salt += sizeof (md5_salt_prefix) - 1;
  71. salt_len = MIN (strcspn (salt, "$"), 8);
  72. key_len = strlen (key);
  73. /* Prepare for the real work. */
  74. md5_init_ctx (&ctx);
  75. /* Add the key string. */
  76. md5_process_bytes (key, key_len, &ctx);
  77. /* Because the SALT argument need not always have the salt prefix we
  78. add it separately. */
  79. md5_process_bytes (md5_salt_prefix, sizeof (md5_salt_prefix) - 1, &ctx);
  80. /* The last part is the salt string. This must be at most 8
  81. characters and it ends at the first `$' character (for
  82. compatibility which existing solutions). */
  83. md5_process_bytes (salt, salt_len, &ctx);
  84. /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
  85. final result will be added to the first context. */
  86. md5_init_ctx (&alt_ctx);
  87. /* Add key. */
  88. md5_process_bytes (key, key_len, &alt_ctx);
  89. /* Add salt. */
  90. md5_process_bytes (salt, salt_len, &alt_ctx);
  91. /* Add key again. */
  92. md5_process_bytes (key, key_len, &alt_ctx);
  93. /* Now get result of this (16 bytes) and add it to the other
  94. context. */
  95. md5_finish_ctx (&alt_ctx, alt_result);
  96. /* Add for any character in the key one byte of the alternate sum. */
  97. for (cnt = key_len; cnt > 16; cnt -= 16)
  98. md5_process_bytes (alt_result, 16, &ctx);
  99. md5_process_bytes (alt_result, cnt, &ctx);
  100. /* For the following code we need a NUL byte. */
  101. *alt_result = '\0';
  102. /* The original implementation now does something weird: for every 1
  103. bit in the key the first 0 is added to the buffer, for every 0
  104. bit the first character of the key. This does not seem to be
  105. what was intended but we have to follow this to be compatible. */
  106. for (cnt = key_len; cnt > 0; cnt >>= 1)
  107. md5_process_bytes ((cnt & 1) != 0 ? (const char *) alt_result : key, 1,
  108. &ctx);
  109. /* Create intermediate result. */
  110. md5_finish_ctx (&ctx, alt_result);
  111. /* Now comes another weirdness. In fear of password crackers here
  112. comes a quite long loop which just processes the output of the
  113. previous round again. We cannot ignore this here. */
  114. for (cnt = 0; cnt < 1000; ++cnt)
  115. {
  116. /* New context. */
  117. md5_init_ctx (&ctx);
  118. /* Add key or last result. */
  119. if ((cnt & 1) != 0)
  120. md5_process_bytes (key, key_len, &ctx);
  121. else
  122. md5_process_bytes (alt_result, 16, &ctx);
  123. /* Add salt for numbers not divisible by 3. */
  124. if (cnt % 3 != 0)
  125. md5_process_bytes (salt, salt_len, &ctx);
  126. /* Add key for numbers not divisible by 7. */
  127. if (cnt % 7 != 0)
  128. md5_process_bytes (key, key_len, &ctx);
  129. /* Add key or last result. */
  130. if ((cnt & 1) != 0)
  131. md5_process_bytes (alt_result, 16, &ctx);
  132. else
  133. md5_process_bytes (key, key_len, &ctx);
  134. /* Create intermediate result. */
  135. md5_finish_ctx (&ctx, alt_result);
  136. }
  137. /* Now we can construct the result string. It consists of three
  138. parts. */
  139. cp = __stpncpy (buffer, md5_salt_prefix, MAX (0, buflen));
  140. buflen -= sizeof (md5_salt_prefix);
  141. cp = __stpncpy (cp, salt, MIN ((size_t) buflen, salt_len));
  142. buflen -= MIN ((size_t) buflen, salt_len);
  143. if (buflen > 0)
  144. {
  145. *cp++ = '$';
  146. --buflen;
  147. }
  148. #define b64_from_24bit(B2, B1, B0, N) \
  149. do { \
  150. unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
  151. int n = (N); \
  152. while (n-- > 0 && buflen > 0) \
  153. { \
  154. *cp++ = b64t[w & 0x3f]; \
  155. --buflen; \
  156. w >>= 6; \
  157. } \
  158. } while (0)
  159. b64_from_24bit (alt_result[0], alt_result[6], alt_result[12], 4);
  160. b64_from_24bit (alt_result[1], alt_result[7], alt_result[13], 4);
  161. b64_from_24bit (alt_result[2], alt_result[8], alt_result[14], 4);
  162. b64_from_24bit (alt_result[3], alt_result[9], alt_result[15], 4);
  163. b64_from_24bit (alt_result[4], alt_result[10], alt_result[5], 4);
  164. b64_from_24bit (0, 0, alt_result[11], 2);
  165. if (buflen <= 0)
  166. {
  167. __set_errno (ERANGE);
  168. buffer = NULL;
  169. }
  170. else
  171. *cp = '\0'; /* Terminate the string. */
  172. /* Clear the buffer for the intermediate result so that people
  173. attaching to processes or reading core dumps cannot get any
  174. information. */
  175. memset (alt_result, '\0', sizeof (alt_result));
  176. return buffer;
  177. }
  178. char *
  179. __md5_crypt (key, salt)
  180. const char *key;
  181. const char *salt;
  182. {
  183. /* We don't want to have an arbitrary limit in the size of the
  184. password. We can compute the size of the result in advance and
  185. so we can prepare the buffer we pass to `md5_crypt_r'. */
  186. static char *buffer = NULL;
  187. static int buflen = 0;
  188. int needed = 3 + strlen (salt) + 1 + 26 + 1;
  189. if (buflen < needed)
  190. {
  191. buflen = needed;
  192. if ((buffer = realloc (buffer, buflen)) == NULL)
  193. return NULL;
  194. }
  195. return __md5_crypt_r (key, salt, buffer, buflen);
  196. }