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 */
36 #include "yahoo_util.h"
40 /* Define our magic string to mark salt for MD5 "encryption"
41 replacement. This is meant to be the same as for other MD5 based
42 encryption implementations. */
43 static const char md5_salt_prefix
[] = "$1$";
45 /* Table with characters for base64 transformation. */
46 static const char b64t
[64] =
47 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
49 char *yahoo_crypt(char *key
, char *salt
)
53 int needed
= 3 + strlen(salt
) + 1 + 26 + 1;
55 md5_byte_t alt_result
[16];
63 if (buflen
< needed
) {
65 if ((buffer
= realloc(buffer
, buflen
)) == NULL
)
69 /* Find beginning of salt string. The prefix should normally always
70 be present. Just in case it is not. */
71 if (strncmp(md5_salt_prefix
, salt
, sizeof(md5_salt_prefix
) - 1) == 0)
72 /* Skip salt prefix. */
73 salt
+= sizeof(md5_salt_prefix
) - 1;
75 salt_len
= MIN(strcspn(salt
, "$"), 8);
76 key_len
= strlen(key
);
78 /* Prepare for the real work. */
81 /* Add the key string. */
82 md5_append(&ctx
, (md5_byte_t
*)key
, key_len
);
84 /* Because the SALT argument need not always have the salt prefix we
86 md5_append(&ctx
, (md5_byte_t
*)md5_salt_prefix
,
87 sizeof(md5_salt_prefix
) - 1);
89 /* The last part is the salt string. This must be at most 8
90 characters and it ends at the first `$' character (for
91 compatibility which existing solutions). */
92 md5_append(&ctx
, (md5_byte_t
*)salt
, salt_len
);
94 /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
95 final result will be added to the first context. */
99 md5_append(&alt_ctx
, (md5_byte_t
*)key
, key_len
);
102 md5_append(&alt_ctx
, (md5_byte_t
*)salt
, salt_len
);
105 md5_append(&alt_ctx
, (md5_byte_t
*)key
, key_len
);
107 /* Now get result of this (16 bytes) and add it to the other
109 md5_finish(&alt_ctx
, alt_result
);
111 /* Add for any character in the key one byte of the alternate sum. */
112 for (cnt
= key_len
; cnt
> 16; cnt
-= 16)
113 md5_append(&ctx
, alt_result
, 16);
114 md5_append(&ctx
, alt_result
, cnt
);
116 /* For the following code we need a NUL byte. */
117 alt_result
[0] = '\0';
119 /* The original implementation now does something weird: for every 1
120 bit in the key the first 0 is added to the buffer, for every 0
121 bit the first character of the key. This does not seem to be
122 what was intended but we have to follow this to be compatible. */
123 for (cnt
= key_len
; cnt
> 0; cnt
>>= 1)
125 (cnt
& 1) != 0 ? alt_result
: (md5_byte_t
*)key
, 1);
127 /* Create intermediate result. */
128 md5_finish(&ctx
, alt_result
);
130 /* Now comes another weirdness. In fear of password crackers here
131 comes a quite long loop which just processes the output of the
132 previous round again. We cannot ignore this here. */
133 for (cnt
= 0; cnt
< 1000; ++cnt
) {
137 /* Add key or last result. */
139 md5_append(&ctx
, (md5_byte_t
*)key
, key_len
);
141 md5_append(&ctx
, alt_result
, 16);
143 /* Add salt for numbers not divisible by 3. */
145 md5_append(&ctx
, (md5_byte_t
*)salt
, salt_len
);
147 /* Add key for numbers not divisible by 7. */
149 md5_append(&ctx
, (md5_byte_t
*)key
, key_len
);
151 /* Add key or last result. */
153 md5_append(&ctx
, alt_result
, 16);
155 md5_append(&ctx
, (md5_byte_t
*)key
, key_len
);
157 /* Create intermediate result. */
158 md5_finish(&ctx
, alt_result
);
161 /* Now we can construct the result string. It consists of three
164 strncpy(buffer
, md5_salt_prefix
, MAX(0, buflen
));
165 cp
= buffer
+ strlen(buffer
);
166 buflen
-= sizeof(md5_salt_prefix
);
168 strncpy(cp
, salt
, MIN((size_t) buflen
, salt_len
));
169 cp
= cp
+ strlen(cp
);
170 buflen
-= MIN((size_t) buflen
, salt_len
);
176 #define b64_from_24bit(B2, B1, B0, N) \
178 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
180 while (n-- > 0 && buflen > 0) { \
181 *cp++ = b64t[w & 0x3f]; \
187 b64_from_24bit(alt_result
[0], alt_result
[6], alt_result
[12], 4);
188 b64_from_24bit(alt_result
[1], alt_result
[7], alt_result
[13], 4);
189 b64_from_24bit(alt_result
[2], alt_result
[8], alt_result
[14], 4);
190 b64_from_24bit(alt_result
[3], alt_result
[9], alt_result
[15], 4);
191 b64_from_24bit(alt_result
[4], alt_result
[10], alt_result
[5], 4);
192 b64_from_24bit(0, 0, alt_result
[11], 2);
196 *cp
= '\0'; /* Terminate the string. */
198 /* Clear the buffer for the intermediate result so that people
199 attaching to processes or reading core dumps cannot get any
200 information. We do it in this way to clear correct_words[]
201 inside the MD5 implementation as well. */
203 md5_finish(&ctx
, alt_result
);
204 memset(&ctx
, '\0', sizeof(ctx
));
205 memset(&alt_ctx
, '\0', sizeof(alt_ctx
));