md5-crypt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* One way encryption based on MD5 sum.
  2. Compatible with the behavior of MD5 crypt introduced in FreeBSD 2.0.
  3. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/param.h>
  22. #include "md5.h"
  23. #include "crypt-private.h"
  24. #ifdef USE_NSS
  25. typedef int PRBool;
  26. # include <hasht.h>
  27. # include <nsslowhash.h>
  28. # define md5_init_ctx(ctxp, nss_ctxp) \
  29. do \
  30. { \
  31. if (((nss_ctxp = NSSLOWHASH_NewContext (nss_ictx, HASH_AlgMD5)) \
  32. == NULL)) \
  33. { \
  34. if (nss_ctx != NULL) \
  35. NSSLOWHASH_Destroy (nss_ctx); \
  36. if (nss_alt_ctx != NULL) \
  37. NSSLOWHASH_Destroy (nss_alt_ctx); \
  38. return NULL; \
  39. } \
  40. NSSLOWHASH_Begin (nss_ctxp); \
  41. } \
  42. while (0)
  43. # define md5_process_bytes(buf, len, ctxp, nss_ctxp) \
  44. NSSLOWHASH_Update (nss_ctxp, (const unsigned char *) buf, len)
  45. # define md5_finish_ctx(ctxp, nss_ctxp, result) \
  46. do \
  47. { \
  48. unsigned int ret; \
  49. NSSLOWHASH_End (nss_ctxp, result, &ret, sizeof (result)); \
  50. assert (ret == sizeof (result)); \
  51. NSSLOWHASH_Destroy (nss_ctxp); \
  52. nss_ctxp = NULL; \
  53. } \
  54. while (0)
  55. #else
  56. # define md5_init_ctx(ctxp, nss_ctxp) \
  57. __md5_init_ctx (ctxp)
  58. # define md5_process_bytes(buf, len, ctxp, nss_ctxp) \
  59. __md5_process_bytes(buf, len, ctxp)
  60. # define md5_finish_ctx(ctxp, nss_ctxp, result) \
  61. __md5_finish_ctx (ctxp, result)
  62. #endif
  63. /* Define our magic string to mark salt for MD5 "encryption"
  64. replacement. This is meant to be the same as for other MD5 based
  65. encryption implementations. */
  66. static const char md5_salt_prefix[] = "$1$";
  67. /* Prototypes for local functions. */
  68. extern char *__md5_crypt_r (const char *key, const char *salt,
  69. char *buffer, int buflen);
  70. extern char *__md5_crypt (const char *key, const char *salt);
  71. /* This entry point is equivalent to the `crypt' function in Unix
  72. libcs. */
  73. char *
  74. __md5_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
  75. {
  76. unsigned char alt_result[16]
  77. __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
  78. size_t salt_len;
  79. size_t key_len;
  80. size_t cnt;
  81. char *cp;
  82. char *copied_key = NULL;
  83. char *copied_salt = NULL;
  84. char *free_key = NULL;
  85. size_t alloca_used = 0;
  86. /* Find beginning of salt string. The prefix should normally always
  87. be present. Just in case it is not. */
  88. if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
  89. /* Skip salt prefix. */
  90. salt += sizeof (md5_salt_prefix) - 1;
  91. salt_len = MIN (strcspn (salt, "$"), 8);
  92. key_len = strlen (key);
  93. if ((key - (char *) 0) % __alignof__ (md5_uint32) != 0)
  94. {
  95. char *tmp;
  96. if (__libc_use_alloca (alloca_used + key_len + __alignof__ (md5_uint32)))
  97. tmp = (char *) alloca (key_len + __alignof__ (md5_uint32));
  98. else
  99. {
  100. free_key = tmp = (char *) malloc (key_len + __alignof__ (md5_uint32));
  101. if (tmp == NULL)
  102. return NULL;
  103. }
  104. key = copied_key =
  105. memcpy (tmp + __alignof__ (md5_uint32)
  106. - (tmp - (char *) 0) % __alignof__ (md5_uint32),
  107. key, key_len);
  108. assert ((key - (char *) 0) % __alignof__ (md5_uint32) == 0);
  109. }
  110. if ((salt - (char *) 0) % __alignof__ (md5_uint32) != 0)
  111. {
  112. char *tmp = (char *) alloca (salt_len + __alignof__ (md5_uint32));
  113. salt = copied_salt =
  114. memcpy (tmp + __alignof__ (md5_uint32)
  115. - (tmp - (char *) 0) % __alignof__ (md5_uint32),
  116. salt, salt_len);
  117. assert ((salt - (char *) 0) % __alignof__ (md5_uint32) == 0);
  118. }
  119. #ifdef USE_NSS
  120. /* Initialize libfreebl3. */
  121. NSSLOWInitContext *nss_ictx = NSSLOW_Init ();
  122. if (nss_ictx == NULL)
  123. {
  124. free (free_key);
  125. return NULL;
  126. }
  127. NSSLOWHASHContext *nss_ctx = NULL;
  128. NSSLOWHASHContext *nss_alt_ctx = NULL;
  129. #else
  130. struct md5_ctx ctx;
  131. struct md5_ctx alt_ctx;
  132. #endif
  133. /* Prepare for the real work. */
  134. md5_init_ctx (&ctx, nss_ctx);
  135. /* Add the key string. */
  136. md5_process_bytes (key, key_len, &ctx, nss_ctx);
  137. /* Because the SALT argument need not always have the salt prefix we
  138. add it separately. */
  139. md5_process_bytes (md5_salt_prefix, sizeof (md5_salt_prefix) - 1,
  140. &ctx, nss_ctx);
  141. /* The last part is the salt string. This must be at most 8
  142. characters and it ends at the first `$' character (for
  143. compatibility with existing implementations). */
  144. md5_process_bytes (salt, salt_len, &ctx, nss_ctx);
  145. /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
  146. final result will be added to the first context. */
  147. md5_init_ctx (&alt_ctx, nss_alt_ctx);
  148. /* Add key. */
  149. md5_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx);
  150. /* Add salt. */
  151. md5_process_bytes (salt, salt_len, &alt_ctx, nss_alt_ctx);
  152. /* Add key again. */
  153. md5_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx);
  154. /* Now get result of this (16 bytes) and add it to the other
  155. context. */
  156. md5_finish_ctx (&alt_ctx, nss_alt_ctx, alt_result);
  157. /* Add for any character in the key one byte of the alternate sum. */
  158. for (cnt = key_len; cnt > 16; cnt -= 16)
  159. md5_process_bytes (alt_result, 16, &ctx, nss_ctx);
  160. md5_process_bytes (alt_result, cnt, &ctx, nss_ctx);
  161. /* For the following code we need a NUL byte. */
  162. *alt_result = '\0';
  163. /* The original implementation now does something weird: for every 1
  164. bit in the key the first 0 is added to the buffer, for every 0
  165. bit the first character of the key. This does not seem to be
  166. what was intended but we have to follow this to be compatible. */
  167. for (cnt = key_len; cnt > 0; cnt >>= 1)
  168. md5_process_bytes ((cnt & 1) != 0
  169. ? (const void *) alt_result : (const void *) key, 1,
  170. &ctx, nss_ctx);
  171. /* Create intermediate result. */
  172. md5_finish_ctx (&ctx, nss_ctx, alt_result);
  173. /* Now comes another weirdness. In fear of password crackers here
  174. comes a quite long loop which just processes the output of the
  175. previous round again. We cannot ignore this here. */
  176. for (cnt = 0; cnt < 1000; ++cnt)
  177. {
  178. /* New context. */
  179. md5_init_ctx (&ctx, nss_ctx);
  180. /* Add key or last result. */
  181. if ((cnt & 1) != 0)
  182. md5_process_bytes (key, key_len, &ctx, nss_ctx);
  183. else
  184. md5_process_bytes (alt_result, 16, &ctx, nss_ctx);
  185. /* Add salt for numbers not divisible by 3. */
  186. if (cnt % 3 != 0)
  187. md5_process_bytes (salt, salt_len, &ctx, nss_ctx);
  188. /* Add key for numbers not divisible by 7. */
  189. if (cnt % 7 != 0)
  190. md5_process_bytes (key, key_len, &ctx, nss_ctx);
  191. /* Add key or last result. */
  192. if ((cnt & 1) != 0)
  193. md5_process_bytes (alt_result, 16, &ctx, nss_ctx);
  194. else
  195. md5_process_bytes (key, key_len, &ctx, nss_ctx);
  196. /* Create intermediate result. */
  197. md5_finish_ctx (&ctx, nss_ctx, alt_result);
  198. }
  199. #ifdef USE_NSS
  200. /* Free libfreebl3 resources. */
  201. NSSLOW_Shutdown (nss_ictx);
  202. #endif
  203. /* Now we can construct the result string. It consists of three
  204. parts. */
  205. cp = __stpncpy (buffer, md5_salt_prefix, MAX (0, buflen));
  206. buflen -= sizeof (md5_salt_prefix) - 1;
  207. cp = __stpncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
  208. buflen -= MIN ((size_t) MAX (0, buflen), salt_len);
  209. if (buflen > 0)
  210. {
  211. *cp++ = '$';
  212. --buflen;
  213. }
  214. __b64_from_24bit (&cp, &buflen,
  215. alt_result[0], alt_result[6], alt_result[12], 4);
  216. __b64_from_24bit (&cp, &buflen,
  217. alt_result[1], alt_result[7], alt_result[13], 4);
  218. __b64_from_24bit (&cp, &buflen,
  219. alt_result[2], alt_result[8], alt_result[14], 4);
  220. __b64_from_24bit (&cp, &buflen,
  221. alt_result[3], alt_result[9], alt_result[15], 4);
  222. __b64_from_24bit (&cp, &buflen,
  223. alt_result[4], alt_result[10], alt_result[5], 4);
  224. __b64_from_24bit (&cp, &buflen,
  225. 0, 0, alt_result[11], 2);
  226. if (buflen <= 0)
  227. {
  228. __set_errno (ERANGE);
  229. buffer = NULL;
  230. }
  231. else
  232. *cp = '\0'; /* Terminate the string. */
  233. /* Clear the buffer for the intermediate result so that people
  234. attaching to processes or reading core dumps cannot get any
  235. information. We do it in this way to clear correct_words[]
  236. inside the MD5 implementation as well. */
  237. #ifndef USE_NSS
  238. __md5_init_ctx (&ctx);
  239. __md5_finish_ctx (&ctx, alt_result);
  240. explicit_bzero (&ctx, sizeof (ctx));
  241. explicit_bzero (&alt_ctx, sizeof (alt_ctx));
  242. #endif
  243. if (copied_key != NULL)
  244. explicit_bzero (copied_key, key_len);
  245. if (copied_salt != NULL)
  246. explicit_bzero (copied_salt, salt_len);
  247. free (free_key);
  248. return buffer;
  249. }
  250. #ifndef _LIBC
  251. # define libc_freeres_ptr(decl) decl
  252. #endif
  253. libc_freeres_ptr (static char *buffer);
  254. char *
  255. __md5_crypt (const char *key, const char *salt)
  256. {
  257. /* We don't want to have an arbitrary limit in the size of the
  258. password. We can compute the size of the result in advance and
  259. so we can prepare the buffer we pass to `md5_crypt_r'. */
  260. static int buflen;
  261. int needed = 3 + strlen (salt) + 1 + 26 + 1;
  262. if (buflen < needed)
  263. {
  264. char *new_buffer = (char *) realloc (buffer, needed);
  265. if (new_buffer == NULL)
  266. return NULL;
  267. buffer = new_buffer;
  268. buflen = needed;
  269. }
  270. return __md5_crypt_r (key, salt, buffer, buflen);
  271. }
  272. #ifndef _LIBC
  273. static void
  274. __attribute__ ((__destructor__))
  275. free_mem (void)
  276. {
  277. free (buffer);
  278. }
  279. #endif