annotate src/protocols/yahoo/crypt.c @ 2795:536bb833fdeb

[gaim-migrate @ 2808] i hate solaris committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Mon, 26 Nov 2001 23:18:36 +0000
parents
children e46efd264489
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2795
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
1 /* One way encryption based on MD5 sum.
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
2 Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
3 This file is part of the GNU C Library.
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
5
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
6 The GNU C Library is free software; you can redistribute it and/or
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
7 modify it under the terms of the GNU Lesser General Public
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
8 License as published by the Free Software Foundation; either
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
9 version 2.1 of the License, or (at your option) any later version.
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
10
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
11 The GNU C Library is distributed in the hope that it will be useful,
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
14 Lesser General Public License for more details.
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
15
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
16 You should have received a copy of the GNU Lesser General Public
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
17 License along with the GNU C Library; if not, write to the Free
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
19 02111-1307 USA. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
20
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
21 /* warmenhoven took this file and made it work with the md5.[ch] we
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
22 * already had. isn't that lovely. people should just use linux or
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
23 * freebsd, crypt works properly on those systems. i hate solaris */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
24
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
25 #include <string.h>
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
26 #include <stdlib.h>
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
27 #include <glib.h>
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
28
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
29 #include "md5.h"
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
30
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
31 /* Define our magic string to mark salt for MD5 "encryption"
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
32 replacement. This is meant to be the same as for other MD5 based
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
33 encryption implementations. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
34 static const char md5_salt_prefix[] = "$1$";
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
35
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
36 /* Table with characters for base64 transformation. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
37 static const char b64t[64] =
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
38 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
39
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
40 char *yahoo_crypt(char *key, char *salt)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
41 {
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
42 static char *buffer = NULL;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
43 static int buflen = 0;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
44 int needed = 3 + strlen (salt) + 1 + 26 + 1;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
45
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
46 md5_byte_t alt_result[16];
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
47 md5_state_t ctx;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
48 md5_state_t alt_ctx;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
49 size_t salt_len;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
50 size_t key_len;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
51 size_t cnt;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
52 char *cp;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
53
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
54 if (buflen < needed) {
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
55 buflen = needed;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
56 if ((buffer = g_realloc(buffer, buflen)) == NULL)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
57 return NULL;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
58 }
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
59
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
60 /* Find beginning of salt string. The prefix should normally always
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
61 be present. Just in case it is not. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
62 if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
63 /* Skip salt prefix. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
64 salt += sizeof (md5_salt_prefix) - 1;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
65
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
66 salt_len = MIN (strcspn (salt, "$"), 8);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
67 key_len = strlen (key);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
68
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
69 /* Prepare for the real work. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
70 md5_init(&ctx);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
71
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
72 /* Add the key string. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
73 md5_append(&ctx, key, key_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
74
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
75 /* Because the SALT argument need not always have the salt prefix we
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
76 add it separately. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
77 md5_append(&ctx, md5_salt_prefix, sizeof (md5_salt_prefix) - 1);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
78
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
79 /* The last part is the salt string. This must be at most 8
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
80 characters and it ends at the first `$' character (for
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
81 compatibility which existing solutions). */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
82 md5_append(&ctx, salt, salt_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
83
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
84 /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
85 final result will be added to the first context. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
86 md5_init(&alt_ctx);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
87
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
88 /* Add key. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
89 md5_append(&alt_ctx, key, key_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
90
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
91 /* Add salt. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
92 md5_append(&alt_ctx, salt, salt_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
93
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
94 /* Add key again. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
95 md5_append(&alt_ctx, key, key_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
96
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
97 /* Now get result of this (16 bytes) and add it to the other
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
98 context. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
99 md5_finish(&alt_ctx, alt_result);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
100
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
101 /* Add for any character in the key one byte of the alternate sum. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
102 for (cnt = key_len; cnt > 16; cnt -= 16)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
103 md5_append(&ctx, alt_result, 16);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
104 md5_append(&ctx, alt_result, cnt);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
105
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
106 /* For the following code we need a NUL byte. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
107 alt_result[0] = '\0';
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
108
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
109 /* The original implementation now does something weird: for every 1
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
110 bit in the key the first 0 is added to the buffer, for every 0
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
111 bit the first character of the key. This does not seem to be
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
112 what was intended but we have to follow this to be compatible. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
113 for (cnt = key_len; cnt > 0; cnt >>= 1)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
114 md5_append(&ctx, (cnt & 1) != 0 ? alt_result : (md5_byte_t *)key, 1);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
115
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
116 /* Create intermediate result. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
117 md5_finish(&ctx, alt_result);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
118
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
119 /* Now comes another weirdness. In fear of password crackers here
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
120 comes a quite long loop which just processes the output of the
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
121 previous round again. We cannot ignore this here. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
122 for (cnt = 0; cnt < 1000; ++cnt) {
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
123 /* New context. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
124 md5_init(&ctx);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
125
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
126 /* Add key or last result. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
127 if ((cnt & 1) != 0)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
128 md5_append(&ctx, key, key_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
129 else
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
130 md5_append(&ctx, alt_result, 16);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
131
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
132 /* Add salt for numbers not divisible by 3. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
133 if (cnt % 3 != 0)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
134 md5_append(&ctx, salt, salt_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
135
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
136 /* Add key for numbers not divisible by 7. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
137 if (cnt % 7 != 0)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
138 md5_append(&ctx, key, key_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
139
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
140 /* Add key or last result. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
141 if ((cnt & 1) != 0)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
142 md5_append(&ctx, alt_result, 16);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
143 else
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
144 md5_append(&ctx, key, key_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
145
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
146 /* Create intermediate result. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
147 md5_finish(&ctx, alt_result);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
148 }
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
149
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
150 /* Now we can construct the result string. It consists of three
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
151 parts. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
152
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
153 strncpy(buffer, md5_salt_prefix, MAX (0, buflen));
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
154 cp = buffer + strlen(buffer);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
155 buflen -= sizeof (md5_salt_prefix);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
156
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
157 strncpy(cp, salt, MIN ((size_t) buflen, salt_len));
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
158 cp = cp + strlen(cp);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
159 buflen -= MIN ((size_t) buflen, salt_len);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
160
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
161 if (buflen > 0) {
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
162 *cp++ = '$';
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
163 --buflen;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
164 }
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
165
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
166 #define b64_from_24bit(B2, B1, B0, N) \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
167 do { \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
168 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
169 int n = (N); \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
170 while (n-- > 0 && buflen > 0) { \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
171 *cp++ = b64t[w & 0x3f]; \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
172 --buflen; \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
173 w >>= 6; \
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
174 }\
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
175 } while (0)
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
176
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
177 b64_from_24bit (alt_result[0], alt_result[6], alt_result[12], 4);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
178 b64_from_24bit (alt_result[1], alt_result[7], alt_result[13], 4);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
179 b64_from_24bit (alt_result[2], alt_result[8], alt_result[14], 4);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
180 b64_from_24bit (alt_result[3], alt_result[9], alt_result[15], 4);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
181 b64_from_24bit (alt_result[4], alt_result[10], alt_result[5], 4);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
182 b64_from_24bit (0, 0, alt_result[11], 2);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
183 if (buflen <= 0) {
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
184 g_free(buffer);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
185 buffer = NULL;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
186 } else
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
187 *cp = '\0'; /* Terminate the string. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
188
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
189 /* Clear the buffer for the intermediate result so that people
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
190 attaching to processes or reading core dumps cannot get any
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
191 information. We do it in this way to clear correct_words[]
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
192 inside the MD5 implementation as well. */
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
193 md5_init(&ctx);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
194 md5_finish(&ctx, alt_result);
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
195 memset (&ctx, '\0', sizeof (ctx));
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
196 memset (&alt_ctx, '\0', sizeof (alt_ctx));
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
197
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
198 return buffer;
536bb833fdeb [gaim-migrate @ 2808]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
199 }