/[cvs]/eggdrop1.8/src/md5/md5c.c
ViewVC logotype

Contents of /eggdrop1.8/src/md5/md5c.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (as text) (vendor branch)
Mon Jul 26 21:11:06 2010 UTC (8 years, 11 months ago) by simple
Branch: eggheads
CVS Tags: v1
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
Imported Eggdrop 1.6.20

1 /*
2 * This is an OpenSSL-compatible implementation of the RSA Data Security,
3 * Inc. MD5 Message-Digest Algorithm.
4 *
5 * Written by Solar Designer <solar@openwall.com> in 2001, and placed in
6 * the public domain.
7 *
8 * This differs from Colin Plumb's older public domain implementation in
9 * that no 32-bit integer data type is required, there's no compile-time
10 * endianness configuration, and the function prototypes match OpenSSL's.
11 * The primary goals are portability and ease of use.
12 *
13 * This implementation is meant to be fast, but not as fast as possible.
14 * Some known optimizations are not included to reduce source code size
15 * and avoid compile-time configuration.
16 */
17
18 #include <string.h>
19
20 #include "md5.h"
21 #include "compat/compat.h"
22
23 /*
24 * The basic MD5 functions.
25 *
26 * F is optimized compared to its RFC 1321 definition just like in Colin
27 * Plumb's implementation.
28 */
29 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
30 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
31 #define H(x, y, z) ((x) ^ (y) ^ (z))
32 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
33
34 /*
35 * The MD5 transformation for all four rounds.
36 */
37 #define STEP(f, a, b, c, d, x, t, s) \
38 (a) += f((b), (c), (d)) + (x) + (t); \
39 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
40 (a) += (b);
41
42 /*
43 * SET reads 4 input bytes in little-endian byte order and stores them
44 * in a properly aligned word in host byte order.
45 *
46 * The check for little-endian architectures which tolerate unaligned
47 * memory accesses is just an optimization. Nothing will break if it
48 * doesn't work.
49 */
50 #if defined(__i386__) || defined(__vax__)
51 #define SET(n) \
52 (*(MD5_u32plus *)&ptr[(n) * 4])
53 #define GET(n) \
54 SET(n)
55 #else
56 #define SET(n) \
57 (ctx->block[(n)] = \
58 (MD5_u32plus)ptr[(n) * 4] | \
59 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
60 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
61 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
62 #define GET(n) \
63 (ctx->block[(n)])
64 #endif
65
66 /*
67 * This processes one or more 64-byte data blocks, but does NOT update
68 * the bit counters. There're no alignment requirements.
69 */
70 static void *body(MD5_CTX *ctx, void *data, unsigned long size)
71 {
72 unsigned char *ptr;
73 MD5_u32plus a, b, c, d;
74 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
75
76 ptr = data;
77
78 a = ctx->a;
79 b = ctx->b;
80 c = ctx->c;
81 d = ctx->d;
82
83 do {
84 saved_a = a;
85 saved_b = b;
86 saved_c = c;
87 saved_d = d;
88
89 /* Round 1 */
90 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
91 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
92 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
93 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
94 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
95 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
96 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
97 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
98 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
99 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
100 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
101 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
102 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
103 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
104 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
105 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
106
107 /* Round 2 */
108 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
109 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
110 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
111 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
112 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
113 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
114 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
115 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
116 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
117 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
118 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
119 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
120 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
121 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
122 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
123 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
124
125 /* Round 3 */
126 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
127 STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
128 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
129 STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
130 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
131 STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
132 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
133 STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
134 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
135 STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
136 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
137 STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
138 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
139 STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
140 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
141 STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
142
143 /* Round 4 */
144 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
145 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
146 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
147 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
148 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
149 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
150 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
151 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
152 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
153 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
154 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
155 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
156 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
157 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
158 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
159 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
160
161 a += saved_a;
162 b += saved_b;
163 c += saved_c;
164 d += saved_d;
165
166 ptr += 64;
167 } while (size -= 64);
168
169 ctx->a = a;
170 ctx->b = b;
171 ctx->c = c;
172 ctx->d = d;
173
174 return ptr;
175 }
176
177 void MD5_Init(MD5_CTX *ctx)
178 {
179 ctx->a = 0x67452301;
180 ctx->b = 0xefcdab89;
181 ctx->c = 0x98badcfe;
182 ctx->d = 0x10325476;
183
184 ctx->lo = 0;
185 ctx->hi = 0;
186 }
187
188 void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
189 {
190 MD5_u32plus saved_lo;
191 unsigned long used, free;
192
193 saved_lo = ctx->lo;
194 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
195 ctx->hi++;
196 ctx->hi += size >> 29;
197
198 used = saved_lo & 0x3f;
199
200 if (used) {
201 free = 64 - used;
202
203 if (size < free) {
204 egg_memcpy(&ctx->buffer[used], data, size);
205 return;
206 }
207
208 egg_memcpy(&ctx->buffer[used], data, free);
209 data = ((unsigned char *)data) + free;
210 size -= free;
211 body(ctx, ctx->buffer, 64);
212 }
213
214 if (size >= 64) {
215 data = body(ctx, data, size & ~(unsigned long)0x3f);
216 size &= 0x3f;
217 }
218
219 egg_memcpy(ctx->buffer, data, size);
220 }
221
222 void MD5_Final(unsigned char *result, MD5_CTX *ctx)
223 {
224 unsigned long used, free;
225
226 used = ctx->lo & 0x3f;
227
228 ctx->buffer[used++] = 0x80;
229
230 free = 64 - used;
231
232 if (free < 8) {
233 egg_memset(&ctx->buffer[used], 0, free);
234 body(ctx, ctx->buffer, 64);
235 used = 0;
236 free = 64;
237 }
238
239 egg_memset(&ctx->buffer[used], 0, free - 8);
240
241 ctx->lo <<= 3;
242 ctx->buffer[56] = ctx->lo;
243 ctx->buffer[57] = ctx->lo >> 8;
244 ctx->buffer[58] = ctx->lo >> 16;
245 ctx->buffer[59] = ctx->lo >> 24;
246 ctx->buffer[60] = ctx->hi;
247 ctx->buffer[61] = ctx->hi >> 8;
248 ctx->buffer[62] = ctx->hi >> 16;
249 ctx->buffer[63] = ctx->hi >> 24;
250
251 body(ctx, ctx->buffer, 64);
252
253 result[0] = ctx->a;
254 result[1] = ctx->a >> 8;
255 result[2] = ctx->a >> 16;
256 result[3] = ctx->a >> 24;
257 result[4] = ctx->b;
258 result[5] = ctx->b >> 8;
259 result[6] = ctx->b >> 16;
260 result[7] = ctx->b >> 24;
261 result[8] = ctx->c;
262 result[9] = ctx->c >> 8;
263 result[10] = ctx->c >> 16;
264 result[11] = ctx->c >> 24;
265 result[12] = ctx->d;
266 result[13] = ctx->d >> 8;
267 result[14] = ctx->d >> 16;
268 result[15] = ctx->d >> 24;
269
270 egg_memset(ctx, 0, sizeof(ctx));
271 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23