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-2014 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
25 #include <sys/param.h>
33 # include <nsslowhash.h>
35 # define md5_init_ctx(ctxp, nss_ctxp) \
38 if (((nss_ctxp = NSSLOWHASH_NewContext (nss_ictx, HASH_AlgMD5)) \
41 if (nss_ctx != NULL) \
42 NSSLOWHASH_Destroy (nss_ctx); \
43 if (nss_alt_ctx != NULL) \
44 NSSLOWHASH_Destroy (nss_alt_ctx); \
47 NSSLOWHASH_Begin (nss_ctxp); \
51 # define md5_process_bytes(buf, len, ctxp, nss_ctxp) \
52 NSSLOWHASH_Update (nss_ctxp, (const unsigned char *) buf, len)
54 # define md5_finish_ctx(ctxp, nss_ctxp, result) \
58 NSSLOWHASH_End (nss_ctxp, result, &ret, sizeof (result)); \
59 assert (ret == sizeof (result)); \
60 NSSLOWHASH_Destroy (nss_ctxp); \
65 # define md5_init_ctx(ctxp, nss_ctxp) \
68 # define md5_process_bytes(buf, len, ctxp, nss_ctxp) \
69 __md5_process_bytes(buf, len, ctxp)
71 # define md5_finish_ctx(ctxp, nss_ctxp, result) \
72 __md5_finish_ctx (ctxp, result)
76 /* Define our magic string to mark salt for MD5 "encryption"
77 replacement. This is meant to be the same as for other MD5 based
78 encryption implementations. */
79 static const char md5_salt_prefix
[] = "$1$";
81 /* Table with characters for base64 transformation. */
82 static const char b64t
[64] =
83 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
86 /* Prototypes for local functions. */
87 extern char *__md5_crypt_r (const char *key
, const char *salt
,
88 char *buffer
, int buflen
);
89 extern char *__md5_crypt (const char *key
, const char *salt
);
92 b64_from_24bit (char **cp
, int *buflen
,
93 unsigned int b2
, unsigned int b1
, unsigned int b0
,
96 unsigned int w
= (b2
<< 16) | (b1
<< 8) | b0
;
97 while (n
-- > 0 && *buflen
> 0)
99 *(*cp
)++ = b64t
[w
& 0x3f];
106 /* This entry point is equivalent to the `crypt' function in Unix
109 __md5_crypt_r (key
, salt
, buffer
, buflen
)
115 unsigned char alt_result
[16]
116 __attribute__ ((__aligned__ (__alignof__ (md5_uint32
))));
121 char *copied_key
= NULL
;
122 char *copied_salt
= NULL
;
123 char *free_key
= NULL
;
124 size_t alloca_used
= 0;
126 /* Find beginning of salt string. The prefix should normally always
127 be present. Just in case it is not. */
128 if (strncmp (md5_salt_prefix
, salt
, sizeof (md5_salt_prefix
) - 1) == 0)
129 /* Skip salt prefix. */
130 salt
+= sizeof (md5_salt_prefix
) - 1;
132 salt_len
= MIN (strcspn (salt
, "$"), 8);
133 key_len
= strlen (key
);
135 if ((key
- (char *) 0) % __alignof__ (md5_uint32
) != 0)
139 if (__libc_use_alloca (alloca_used
+ key_len
+ __alignof__ (md5_uint32
)))
140 tmp
= (char *) alloca (key_len
+ __alignof__ (md5_uint32
));
143 free_key
= tmp
= (char *) malloc (key_len
+ __alignof__ (md5_uint32
));
149 memcpy (tmp
+ __alignof__ (md5_uint32
)
150 - (tmp
- (char *) 0) % __alignof__ (md5_uint32
),
152 assert ((key
- (char *) 0) % __alignof__ (md5_uint32
) == 0);
155 if ((salt
- (char *) 0) % __alignof__ (md5_uint32
) != 0)
157 char *tmp
= (char *) alloca (salt_len
+ __alignof__ (md5_uint32
));
159 memcpy (tmp
+ __alignof__ (md5_uint32
)
160 - (tmp
- (char *) 0) % __alignof__ (md5_uint32
),
162 assert ((salt
- (char *) 0) % __alignof__ (md5_uint32
) == 0);
166 /* Initialize libfreebl3. */
167 NSSLOWInitContext
*nss_ictx
= NSSLOW_Init ();
168 if (nss_ictx
== NULL
)
173 NSSLOWHASHContext
*nss_ctx
= NULL
;
174 NSSLOWHASHContext
*nss_alt_ctx
= NULL
;
177 struct md5_ctx alt_ctx
;
180 /* Prepare for the real work. */
181 md5_init_ctx (&ctx
, nss_ctx
);
183 /* Add the key string. */
184 md5_process_bytes (key
, key_len
, &ctx
, nss_ctx
);
186 /* Because the SALT argument need not always have the salt prefix we
187 add it separately. */
188 md5_process_bytes (md5_salt_prefix
, sizeof (md5_salt_prefix
) - 1,
191 /* The last part is the salt string. This must be at most 8
192 characters and it ends at the first `$' character (for
193 compatibility with existing implementations). */
194 md5_process_bytes (salt
, salt_len
, &ctx
, nss_ctx
);
197 /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
198 final result will be added to the first context. */
199 md5_init_ctx (&alt_ctx
, nss_alt_ctx
);
202 md5_process_bytes (key
, key_len
, &alt_ctx
, nss_alt_ctx
);
205 md5_process_bytes (salt
, salt_len
, &alt_ctx
, nss_alt_ctx
);
208 md5_process_bytes (key
, key_len
, &alt_ctx
, nss_alt_ctx
);
210 /* Now get result of this (16 bytes) and add it to the other
212 md5_finish_ctx (&alt_ctx
, nss_alt_ctx
, alt_result
);
214 /* Add for any character in the key one byte of the alternate sum. */
215 for (cnt
= key_len
; cnt
> 16; cnt
-= 16)
216 md5_process_bytes (alt_result
, 16, &ctx
, nss_ctx
);
217 md5_process_bytes (alt_result
, cnt
, &ctx
, nss_ctx
);
219 /* For the following code we need a NUL byte. */
222 /* The original implementation now does something weird: for every 1
223 bit in the key the first 0 is added to the buffer, for every 0
224 bit the first character of the key. This does not seem to be
225 what was intended but we have to follow this to be compatible. */
226 for (cnt
= key_len
; cnt
> 0; cnt
>>= 1)
227 md5_process_bytes ((cnt
& 1) != 0
228 ? (const void *) alt_result
: (const void *) key
, 1,
231 /* Create intermediate result. */
232 md5_finish_ctx (&ctx
, nss_ctx
, alt_result
);
234 /* Now comes another weirdness. In fear of password crackers here
235 comes a quite long loop which just processes the output of the
236 previous round again. We cannot ignore this here. */
237 for (cnt
= 0; cnt
< 1000; ++cnt
)
240 md5_init_ctx (&ctx
, nss_ctx
);
242 /* Add key or last result. */
244 md5_process_bytes (key
, key_len
, &ctx
, nss_ctx
);
246 md5_process_bytes (alt_result
, 16, &ctx
, nss_ctx
);
248 /* Add salt for numbers not divisible by 3. */
250 md5_process_bytes (salt
, salt_len
, &ctx
, nss_ctx
);
252 /* Add key for numbers not divisible by 7. */
254 md5_process_bytes (key
, key_len
, &ctx
, nss_ctx
);
256 /* Add key or last result. */
258 md5_process_bytes (alt_result
, 16, &ctx
, nss_ctx
);
260 md5_process_bytes (key
, key_len
, &ctx
, nss_ctx
);
262 /* Create intermediate result. */
263 md5_finish_ctx (&ctx
, nss_ctx
, alt_result
);
267 /* Free libfreebl3 resources. */
268 NSSLOW_Shutdown (nss_ictx
);
271 /* Now we can construct the result string. It consists of three
273 cp
= __stpncpy (buffer
, md5_salt_prefix
, MAX (0, buflen
));
274 buflen
-= sizeof (md5_salt_prefix
) - 1;
276 cp
= __stpncpy (cp
, salt
, MIN ((size_t) MAX (0, buflen
), salt_len
));
277 buflen
-= MIN ((size_t) MAX (0, buflen
), salt_len
);
285 b64_from_24bit (&cp
, &buflen
,
286 alt_result
[0], alt_result
[6], alt_result
[12], 4);
287 b64_from_24bit (&cp
, &buflen
,
288 alt_result
[1], alt_result
[7], alt_result
[13], 4);
289 b64_from_24bit (&cp
, &buflen
,
290 alt_result
[2], alt_result
[8], alt_result
[14], 4);
291 b64_from_24bit (&cp
, &buflen
,
292 alt_result
[3], alt_result
[9], alt_result
[15], 4);
293 b64_from_24bit (&cp
, &buflen
,
294 alt_result
[4], alt_result
[10], alt_result
[5], 4);
295 b64_from_24bit (&cp
, &buflen
,
296 0, 0, alt_result
[11], 2);
299 __set_errno (ERANGE
);
303 *cp
= '\0'; /* Terminate the string. */
305 /* Clear the buffer for the intermediate result so that people
306 attaching to processes or reading core dumps cannot get any
307 information. We do it in this way to clear correct_words[]
308 inside the MD5 implementation as well. */
310 __md5_init_ctx (&ctx
);
311 __md5_finish_ctx (&ctx
, alt_result
);
312 memset (&ctx
, '\0', sizeof (ctx
));
313 memset (&alt_ctx
, '\0', sizeof (alt_ctx
));
315 if (copied_key
!= NULL
)
316 memset (copied_key
, '\0', key_len
);
317 if (copied_salt
!= NULL
)
318 memset (copied_salt
, '\0', salt_len
);
325 # define libc_freeres_ptr(decl) decl
327 libc_freeres_ptr (static char *buffer
);
330 __md5_crypt (const char *key
, const char *salt
)
332 /* We don't want to have an arbitrary limit in the size of the
333 password. We can compute the size of the result in advance and
334 so we can prepare the buffer we pass to `md5_crypt_r'. */
336 int needed
= 3 + strlen (salt
) + 1 + 26 + 1;
340 char *new_buffer
= (char *) realloc (buffer
, needed
);
341 if (new_buffer
== NULL
)
348 return __md5_crypt_r (key
, salt
, buffer
, buflen
);
353 __attribute__ ((__destructor__
))