|
2795
|
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.
|
|
|
5
|
|
|
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
|
|
|
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.
|
|
|
15
|
|
|
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
|
|
|
19 02111-1307 USA. */
|
|
|
20
|
|
|
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 */
|
|
|
24
|
|
|
25 #include <string.h>
|
|
|
26 #include <stdlib.h>
|
|
|
27 #include <glib.h>
|
|
|
28
|
|
10684
|
29 #include "cipher.h"
|
|
2795
|
30
|
|
|
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$";
|
|
|
35
|
|
|
36 /* Table with characters for base64 transformation. */
|
|
|
37 static const char b64t[64] =
|
|
|
38 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
39
|
|
5583
|
40 char *yahoo_crypt(const char *key, const char *salt)
|
|
2795
|
41 {
|
|
10684
|
42 GaimCipher *cipher;
|
|
|
43 GaimCipherContext *context1, *context2;
|
|
|
44 guint8 digest[16];
|
|
2795
|
45 static char *buffer = NULL;
|
|
|
46 static int buflen = 0;
|
|
|
47 int needed = 3 + strlen (salt) + 1 + 26 + 1;
|
|
|
48
|
|
|
49 size_t salt_len;
|
|
|
50 size_t key_len;
|
|
|
51 size_t cnt;
|
|
|
52 char *cp;
|
|
|
53
|
|
|
54 if (buflen < needed) {
|
|
|
55 buflen = needed;
|
|
|
56 if ((buffer = g_realloc(buffer, buflen)) == NULL)
|
|
|
57 return NULL;
|
|
|
58 }
|
|
|
59
|
|
10684
|
60 cipher = gaim_ciphers_find_cipher("md5");
|
|
|
61 context1 = gaim_cipher_context_new(cipher, NULL);
|
|
|
62 context2 = gaim_cipher_context_new(cipher, NULL);
|
|
|
63
|
|
2795
|
64 /* Find beginning of salt string. The prefix should normally always
|
|
10684
|
65 * be present. Just in case it is not.
|
|
|
66 */
|
|
2795
|
67 if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
|
|
|
68 /* Skip salt prefix. */
|
|
|
69 salt += sizeof (md5_salt_prefix) - 1;
|
|
|
70
|
|
|
71 salt_len = MIN (strcspn (salt, "$"), 8);
|
|
|
72 key_len = strlen (key);
|
|
|
73
|
|
|
74 /* Add the key string. */
|
|
10684
|
75 gaim_cipher_context_append(context1, key, key_len);
|
|
2795
|
76
|
|
|
77 /* Because the SALT argument need not always have the salt prefix we
|
|
10684
|
78 * add it separately.
|
|
|
79 */
|
|
|
80 gaim_cipher_context_append(context1, md5_salt_prefix,
|
|
|
81 sizeof(md5_salt_prefix) - 1);
|
|
2795
|
82
|
|
|
83 /* The last part is the salt string. This must be at most 8
|
|
10684
|
84 * characters and it ends at the first `$' character (for
|
|
|
85 * compatibility which existing solutions).
|
|
|
86 */
|
|
|
87 gaim_cipher_context_append(context1, salt, salt_len);
|
|
2795
|
88
|
|
|
89 /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
|
|
10684
|
90 * final result will be added to the first context.
|
|
|
91 */
|
|
2795
|
92
|
|
|
93 /* Add key. */
|
|
10684
|
94 gaim_cipher_context_append(context2, key, key_len);
|
|
2795
|
95
|
|
|
96 /* Add salt. */
|
|
10684
|
97 gaim_cipher_context_append(context2, salt, salt_len);
|
|
2795
|
98
|
|
|
99 /* Add key again. */
|
|
10684
|
100 gaim_cipher_context_append(context2, key, key_len);
|
|
2795
|
101
|
|
10684
|
102 /* Now get result of this (16 bytes) and add it to the other context. */
|
|
|
103 gaim_cipher_context_digest(context2, NULL, digest);
|
|
2795
|
104
|
|
|
105 /* Add for any character in the key one byte of the alternate sum. */
|
|
|
106 for (cnt = key_len; cnt > 16; cnt -= 16)
|
|
10684
|
107 gaim_cipher_context_append(context1, digest, 16);
|
|
|
108 gaim_cipher_context_append(context1, digest, cnt);
|
|
2795
|
109
|
|
|
110 /* For the following code we need a NUL byte. */
|
|
10684
|
111 digest[0] = '\0';
|
|
2795
|
112
|
|
|
113 /* The original implementation now does something weird: for every 1
|
|
10684
|
114 * bit in the key the first 0 is added to the buffer, for every 0
|
|
|
115 * bit the first character of the key. This does not seem to be
|
|
|
116 * what was intended but we have to follow this to be compatible.
|
|
|
117 */
|
|
2795
|
118 for (cnt = key_len; cnt > 0; cnt >>= 1)
|
|
10684
|
119 gaim_cipher_context_append(context1,
|
|
|
120 (cnt & 1) != 0 ? digest : (guint8 *)key, 1);
|
|
2795
|
121
|
|
|
122 /* Create intermediate result. */
|
|
10684
|
123 gaim_cipher_context_digest(context1, NULL, digest);
|
|
2795
|
124
|
|
|
125 /* Now comes another weirdness. In fear of password crackers here
|
|
10684
|
126 * comes a quite long loop which just processes the output of the
|
|
|
127 * previous round again. We cannot ignore this here.
|
|
|
128 */
|
|
2795
|
129 for (cnt = 0; cnt < 1000; ++cnt) {
|
|
|
130 /* New context. */
|
|
10684
|
131 gaim_cipher_context_reset(context2, NULL);
|
|
2795
|
132
|
|
|
133 /* Add key or last result. */
|
|
|
134 if ((cnt & 1) != 0)
|
|
10684
|
135 gaim_cipher_context_append(context2, key, key_len);
|
|
2795
|
136 else
|
|
10684
|
137 gaim_cipher_context_append(context2, digest, 16);
|
|
2795
|
138
|
|
|
139 /* Add salt for numbers not divisible by 3. */
|
|
|
140 if (cnt % 3 != 0)
|
|
10684
|
141 gaim_cipher_context_append(context2, salt, salt_len);
|
|
2795
|
142
|
|
|
143 /* Add key for numbers not divisible by 7. */
|
|
|
144 if (cnt % 7 != 0)
|
|
10684
|
145 gaim_cipher_context_append(context2, key, key_len);
|
|
2795
|
146
|
|
|
147 /* Add key or last result. */
|
|
|
148 if ((cnt & 1) != 0)
|
|
10684
|
149 gaim_cipher_context_append(context2, digest, 16);
|
|
2795
|
150 else
|
|
10684
|
151 gaim_cipher_context_append(context2, key, key_len);
|
|
2795
|
152
|
|
|
153 /* Create intermediate result. */
|
|
10684
|
154 gaim_cipher_context_digest(context2, NULL, digest);
|
|
2795
|
155 }
|
|
|
156
|
|
10684
|
157 /* Now we can construct the result string. It consists of three parts. */
|
|
2795
|
158 strncpy(buffer, md5_salt_prefix, MAX (0, buflen));
|
|
|
159 cp = buffer + strlen(buffer);
|
|
|
160 buflen -= sizeof (md5_salt_prefix);
|
|
|
161
|
|
|
162 strncpy(cp, salt, MIN ((size_t) buflen, salt_len));
|
|
|
163 cp = cp + strlen(cp);
|
|
|
164 buflen -= MIN ((size_t) buflen, salt_len);
|
|
|
165
|
|
|
166 if (buflen > 0) {
|
|
|
167 *cp++ = '$';
|
|
|
168 --buflen;
|
|
|
169 }
|
|
|
170
|
|
|
171 #define b64_from_24bit(B2, B1, B0, N) \
|
|
|
172 do { \
|
|
|
173 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
|
|
|
174 int n = (N); \
|
|
|
175 while (n-- > 0 && buflen > 0) { \
|
|
|
176 *cp++ = b64t[w & 0x3f]; \
|
|
|
177 --buflen; \
|
|
|
178 w >>= 6; \
|
|
|
179 }\
|
|
|
180 } while (0)
|
|
|
181
|
|
10684
|
182 b64_from_24bit (digest[0], digest[6], digest[12], 4);
|
|
|
183 b64_from_24bit (digest[1], digest[7], digest[13], 4);
|
|
|
184 b64_from_24bit (digest[2], digest[8], digest[14], 4);
|
|
|
185 b64_from_24bit (digest[3], digest[9], digest[15], 4);
|
|
|
186 b64_from_24bit (digest[4], digest[10], digest[5], 4);
|
|
|
187 b64_from_24bit (0, 0, digest[11], 2);
|
|
2795
|
188 if (buflen <= 0) {
|
|
|
189 g_free(buffer);
|
|
|
190 buffer = NULL;
|
|
|
191 } else
|
|
|
192 *cp = '\0'; /* Terminate the string. */
|
|
|
193
|
|
|
194 /* Clear the buffer for the intermediate result so that people
|
|
10684
|
195 * attaching to processes or reading core dumps cannot get any
|
|
|
196 * information. We do it in this way to clear correct_words[]
|
|
|
197 * inside the MD5 implementation as well.
|
|
|
198 */
|
|
|
199 gaim_cipher_context_reset(context1, NULL);
|
|
|
200 gaim_cipher_context_digest(context1, NULL, digest);
|
|
|
201 gaim_cipher_context_destroy(context1);
|
|
|
202 gaim_cipher_context_destroy(context2);
|
|
2795
|
203
|
|
|
204 return buffer;
|
|
|
205 }
|