1 /* One way encryption based on MD5 sum.
2 Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 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.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 /* warmenhoven took this file and made it work with the md5.[ch] we
22 * already had. isn't that lovely. people should just use linux or
23 * freebsd, crypt works properly on those systems. i hate solaris */
31 /* Define our magic string to mark salt for MD5 "encryption"
32 replacement. This is meant to be the same as for other MD5 based
33 encryption implementations. */
34 static const char md5_salt_prefix
[] = "$1$";
36 /* Table with characters for base64 transformation. */
37 static const char b64t
[64] =
38 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
40 char *yahoo_crypt(char *key
, char *salt
)
42 static char *buffer
= NULL
;
43 static int buflen
= 0;
44 int needed
= 3 + strlen (salt
) + 1 + 26 + 1;
46 md5_byte_t alt_result
[16];
54 if (buflen
< needed
) {
56 if ((buffer
= g_realloc(buffer
, buflen
)) == NULL
)
60 /* Find beginning of salt string. The prefix should normally always
61 be present. Just in case it is not. */
62 if (strncmp (md5_salt_prefix
, salt
, sizeof (md5_salt_prefix
) - 1) == 0)
63 /* Skip salt prefix. */
64 salt
+= sizeof (md5_salt_prefix
) - 1;
66 salt_len
= MIN (strcspn (salt
, "$"), 8);
67 key_len
= strlen (key
);
69 /* Prepare for the real work. */
72 /* Add the key string. */
73 md5_append(&ctx
, key
, key_len
);
75 /* Because the SALT argument need not always have the salt prefix we
77 md5_append(&ctx
, md5_salt_prefix
, sizeof (md5_salt_prefix
) - 1);
79 /* The last part is the salt string. This must be at most 8
80 characters and it ends at the first `$' character (for
81 compatibility which existing solutions). */
82 md5_append(&ctx
, salt
, salt_len
);
84 /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
85 final result will be added to the first context. */
89 md5_append(&alt_ctx
, key
, key_len
);
92 md5_append(&alt_ctx
, salt
, salt_len
);
95 md5_append(&alt_ctx
, key
, key_len
);
97 /* Now get result of this (16 bytes) and add it to the other
99 md5_finish(&alt_ctx
, alt_result
);
101 /* Add for any character in the key one byte of the alternate sum. */
102 for (cnt
= key_len
; cnt
> 16; cnt
-= 16)
103 md5_append(&ctx
, alt_result
, 16);
104 md5_append(&ctx
, alt_result
, cnt
);
106 /* For the following code we need a NUL byte. */
107 alt_result
[0] = '\0';
109 /* The original implementation now does something weird: for every 1
110 bit in the key the first 0 is added to the buffer, for every 0
111 bit the first character of the key. This does not seem to be
112 what was intended but we have to follow this to be compatible. */
113 for (cnt
= key_len
; cnt
> 0; cnt
>>= 1)
114 md5_append(&ctx
, (cnt
& 1) != 0 ? alt_result
: (md5_byte_t
*)key
, 1);
116 /* Create intermediate result. */
117 md5_finish(&ctx
, alt_result
);
119 /* Now comes another weirdness. In fear of password crackers here
120 comes a quite long loop which just processes the output of the
121 previous round again. We cannot ignore this here. */
122 for (cnt
= 0; cnt
< 1000; ++cnt
) {
126 /* Add key or last result. */
128 md5_append(&ctx
, key
, key_len
);
130 md5_append(&ctx
, alt_result
, 16);
132 /* Add salt for numbers not divisible by 3. */
134 md5_append(&ctx
, salt
, salt_len
);
136 /* Add key for numbers not divisible by 7. */
138 md5_append(&ctx
, key
, key_len
);
140 /* Add key or last result. */
142 md5_append(&ctx
, alt_result
, 16);
144 md5_append(&ctx
, key
, key_len
);
146 /* Create intermediate result. */
147 md5_finish(&ctx
, alt_result
);
150 /* Now we can construct the result string. It consists of three
153 strncpy(buffer
, md5_salt_prefix
, MAX (0, buflen
));
154 cp
= buffer
+ strlen(buffer
);
155 buflen
-= sizeof (md5_salt_prefix
);
157 strncpy(cp
, salt
, MIN ((size_t) buflen
, salt_len
));
158 cp
= cp
+ strlen(cp
);
159 buflen
-= MIN ((size_t) buflen
, salt_len
);
166 #define b64_from_24bit(B2, B1, B0, N) \
168 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
170 while (n-- > 0 && buflen > 0) { \
171 *cp++ = b64t[w & 0x3f]; \
177 b64_from_24bit (alt_result
[0], alt_result
[6], alt_result
[12], 4);
178 b64_from_24bit (alt_result
[1], alt_result
[7], alt_result
[13], 4);
179 b64_from_24bit (alt_result
[2], alt_result
[8], alt_result
[14], 4);
180 b64_from_24bit (alt_result
[3], alt_result
[9], alt_result
[15], 4);
181 b64_from_24bit (alt_result
[4], alt_result
[10], alt_result
[5], 4);
182 b64_from_24bit (0, 0, alt_result
[11], 2);
187 *cp
= '\0'; /* Terminate the string. */
189 /* Clear the buffer for the intermediate result so that people
190 attaching to processes or reading core dumps cannot get any
191 information. We do it in this way to clear correct_words[]
192 inside the MD5 implementation as well. */
194 md5_finish(&ctx
, alt_result
);
195 memset (&ctx
, '\0', sizeof (ctx
));
196 memset (&alt_ctx
, '\0', sizeof (alt_ctx
));