]> jfr.im git - irc.git/blob - software/!RELEASES/ircservices/achurch.org/services/lists/ircservices-coding/2007/003291.html
RELEASE -> !RELEASE
[irc.git] / software / !RELEASES / ircservices / achurch.org / services / lists / ircservices-coding / 2007 / 003291.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
2 <HTML>
3 <HEAD>
4 <TITLE> [IRCServices Coding] md5 licensing
5 </TITLE>
6 <LINK REL="Index" HREF="index.html" >
7 <LINK REL="made" HREF="mailto:ircservices-coding%40ircservices.za.net?Subject=%5BIRCServices%20Coding%5D%20md5%20licensing&In-Reply-To=1179320110.6934.18.camel%40regulus.retout.co.uk">
8 <META NAME="robots" CONTENT="index,nofollow">
9 <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
10 <LINK REL="Previous" HREF="003289.html">
11 <LINK REL="Next" HREF="003299.html">
12 </HEAD>
13 <BODY BGCOLOR="#ffffff">
14 <H1>[IRCServices Coding] md5 licensing</H1>
15 <B>Andrew Church</B>
16 <A HREF="mailto:ircservices-coding%40ircservices.za.net?Subject=%5BIRCServices%20Coding%5D%20md5%20licensing&In-Reply-To=1179320110.6934.18.camel%40regulus.retout.co.uk"
17 TITLE="[IRCServices Coding] md5 licensing">achurch at achurch.org
18 </A><BR>
19 <I>Thu May 17 04:48:47 PDT 2007</I>
20 <P><UL>
21 <LI>Previous message: <A HREF="003289.html">[IRCServices Coding] md5 licensing
22 </A></li>
23 <LI>Next message: <A HREF="003299.html">[IRCServices Coding] md5 licensing
24 </A></li>
25 <LI> <B>Messages sorted by:</B>
26 <a href="date.html#3291">[ date ]</a>
27 <a href="thread.html#3291">[ thread ]</a>
28 <a href="subject.html#3291">[ subject ]</a>
29 <a href="author.html#3291">[ author ]</a>
30 </LI>
31 </UL>
32 <HR>
33 <!--beginarticle-->
34 <PRE>&gt;<i>The copyright notice on the md5 hashing code used in ircservices does
35 </I>&gt;<i>not grant a licence to redistribute modified versions. The clause about
36 </I>&gt;<i>mentioning &quot;RSA Data Security, Inc.&quot; looks GPL-incompatible to me as
37 </I>&gt;<i>well. The same code was removed from Apache last year:
38 </I>&gt;<i><A HREF="http://bugs.debian.org/340538">http://bugs.debian.org/340538</A>
39 </I>&gt;<i>
40 </I>&gt;<i>Please review this patch (against 5.1pre1) to port the md5 code to an
41 </I>&gt;<i>implementation licensed under a revised-BSD-style licence, found at:
42 </I>&gt;<i><A HREF="http://sourceforge.net/project/showfiles.php?group_id=42360">http://sourceforge.net/project/showfiles.php?group_id=42360</A>
43 </I>
44 Thanks for pointing this out. I've applied the patch below to
45 5.1pre1; let me know if you see any problems.
46
47 --Andrew Church
48 <A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">achurch at achurch.org</A>
49 <A HREF="http://achurch.org/">http://achurch.org/</A>
50
51 ------------------------------------------------------------------------
52
53 Index: docs/3.html
54 ===================================================================
55 RCS file: /var/local/cvsroot/ircservices/docs/3.html,v
56 retrieving revision 2.74
57 diff -u -r2.74 3.html
58 --- docs/3.html 6 May 2007 06:49:48 -0000 2.74
59 +++ docs/3.html 16 May 2007 19:58:14 -0000
60 @@ -2279,10 +2279,10 @@
61 &lt;p&gt;The available encryption modules are as follows:
62
63 &lt;p&gt;&lt;ul&gt;
64 -&lt;li&gt;&lt;b&gt;&lt;tt&gt;encryption/md5&lt;/tt&gt;&lt;/b&gt;: Uses the MD5 hashing
65 -algorithm&lt;sup&gt;1&lt;/sup&gt; to encrypt passwords. This is a one-way encryption
66 -algorithm which generates a 128-bit &quot;digest&quot; of the password, and is used
67 -by several Unix systems to encrypt user passwords as well.
68 +&lt;li&gt;&lt;b&gt;&lt;tt&gt;encryption/md5&lt;/tt&gt;&lt;/b&gt;: Uses the MD5 hashing algorithm to
69 +encrypt passwords. This is a one-way encryption algorithm which generates
70 +a 128-bit &quot;digest&quot; of the password, and is used by several Unix systems to
71 +encrypt user passwords as well.
72 &lt;p&gt;&lt;li&gt;&lt;b&gt;&lt;tt&gt;encryption/unix-crypt&lt;/tt&gt;&lt;/b&gt;: &lt;b&gt;DISCOURAGED.&lt;/b&gt; Uses the
73 &lt;tt&gt;crypt()&lt;/tt&gt; function available in most Unix systems, typically a
74 variant of the DES encryption algorithm, to encrypt passwords. This is a
75 @@ -2300,9 +2300,6 @@
76 older passwords can still be used (provided the appropriate module is
77 loaded).
78
79 -&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;font size=-1&gt;Technically, the &quot;RSA Data Security, Inc. MD5
80 -Message-Digest Algorithm&quot;.&lt;/font&gt;
81 -
82 &lt;p align=right&gt;&lt;font size=&quot;-1&quot;&gt;&lt;a href=&quot;#top&quot;&gt;Back to top&lt;/a&gt;&lt;/font&gt;
83
84 &lt;!------------------------------------------------------------------------&gt;
85 Index: docs/Changes
86 ===================================================================
87 RCS file: /var/local/cvsroot/ircservices/docs/Changes,v
88 retrieving revision 2.7
89 diff -u -r2.7 Changes
90 --- docs/Changes 13 May 2007 16:40:05 -0000 2.7
91 +++ docs/Changes 16 May 2007 19:58:14 -0000
92 @@ -1,5 +1,8 @@
93 Version 5.1
94 -----------
95 +2007/05/17 Replaced RSA's MD5 implementation with one licensed under
96 + more lenient terms. Suggested by Tim Retout
97 + &lt;<A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">tim at retout.co.uk</A>&gt;
98 2007/05/14 pre1 Fixed a bug in XML import that caused channel mode locks to
99 be lost. Reported by &lt;<A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">loverboy at irc.doruk.net.tr</A>&gt;
100 2007/05/14 Fixed Services being unable to start if both the compatibility
101 Index: modules/encryption/md5.c
102 ===================================================================
103 RCS file: /var/local/cvsroot/ircservices/modules/encryption/md5.c,v
104 retrieving revision 2.24
105 diff -u -r2.24 md5.c
106 --- modules/encryption/md5.c 15 Feb 2007 14:53:30 -0000 2.24
107 +++ modules/encryption/md5.c 16 May 2007 19:58:14 -0000
108 @@ -22,322 +22,483 @@
109 #define XTOI(c) ((c)&gt;9 ? (c)-'A'+10 : (c)-'0')
110
111 /*************************************************************************/
112 -/*======== Beginning of RSA's md5c.c ========*/
113 -
114 -/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
115 -rights reserved.
116 -
117 -License to copy and use this software is granted provided that it
118 -is identified as the &quot;RSA Data Security, Inc. MD5 Message-Digest
119 -Algorithm&quot; in all material mentioning or referencing this software
120 -or this function.
121 -
122 -License is also granted to make and use derivative works provided
123 -that such works are identified as &quot;derived from the RSA Data
124 -Security, Inc. MD5 Message-Digest Algorithm&quot; in all material
125 -mentioning or referencing the derived work.
126 -
127 -RSA Data Security, Inc. makes no representations concerning either
128 -the merchantability of this software or the suitability of this
129 -software for any particular purpose. It is provided &quot;as is&quot;
130 -without express or implied warranty of any kind.
131 +/*======== Beginning of L. Peter Deutsch's md5.h ========*/
132 +/*
133 + Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
134
135 -These notices must be retained in any copies of any part of this
136 -documentation and/or software.
137 - */
138 + This software is provided 'as-is', without any express or implied
139 + warranty. In no event will the authors be held liable for any damages
140 + arising from the use of this software.
141
142 -#include &lt;string.h&gt;
143 + Permission is granted to anyone to use this software for any purpose,
144 + including commercial applications, and to alter it and redistribute it
145 + freely, subject to the following restrictions:
146
147 -typedef unsigned int UINT4;
148 + 1. The origin of this software must not be misrepresented; you must not
149 + claim that you wrote the original software. If you use this software
150 + in a product, an acknowledgment in the product documentation would be
151 + appreciated but is not required.
152 + 2. Altered source versions must be plainly marked as such, and must not be
153 + misrepresented as being the original software.
154 + 3. This notice may not be removed or altered from any source distribution.
155
156 -/* MD5 context. */
157 -typedef struct {
158 - UINT4 state[4]; /* state (ABCD) */
159 - UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
160 - unsigned char buffer[64]; /* input buffer */
161 -} MD5_CTX;
162 + L. Peter Deutsch
163 + <A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">ghost at aladdin.com</A>
164
165 -/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
166 */
167 +/* $Id: md5.h,v 1.4 2002/04/13 19:20:28 lpd Exp $ */
168 +/*
169 + Independent implementation of MD5 (RFC 1321).
170
171 -typedef void *POINTER;
172 -
173 -/* Constants for MD5Transform routine.
174 + This code implements the MD5 Algorithm defined in RFC 1321, whose
175 + text is available at
176 + <A HREF="http://www.ietf.org/rfc/rfc1321.txt">http://www.ietf.org/rfc/rfc1321.txt</A>
177 + The code is derived from the text of the RFC, including the test suite
178 + (section A.5) but excluding the rest of Appendix A. It does not include
179 + any code or documentation that is identified in the RFC as being
180 + copyrighted.
181 +
182 + The original and principal author of md5.h is L. Peter Deutsch
183 + &lt;<A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">ghost at aladdin.com</A>&gt;. Other authors are noted in the change history
184 + that follows (in reverse chronological order):
185 +
186 + 2002-04-13 lpd Removed support for non-ANSI compilers; removed
187 + references to Ghostscript; clarified derivation from RFC 1321;
188 + now handles byte order either statically or dynamically.
189 + 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
190 + 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
191 + added conditionalization for C++ compilation from Martin
192 + Purschke &lt;<A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">purschke at bnl.gov</A>&gt;.
193 + 1999-05-03 lpd Original version.
194 */
195 -#define S11 7
196 -#define S12 12
197 -#define S13 17
198 -#define S14 22
199 -#define S21 5
200 -#define S22 9
201 -#define S23 14
202 -#define S24 20
203 -#define S31 4
204 -#define S32 11
205 -#define S33 16
206 -#define S34 23
207 -#define S41 6
208 -#define S42 10
209 -#define S43 15
210 -#define S44 21
211 -
212 -static void MD5Transform (UINT4 [4], unsigned char [64]);
213 -static void Encode (unsigned char *, UINT4 *, unsigned int);
214 -static void Decode (UINT4 *, unsigned char *, unsigned int);
215 -
216 -static unsigned char PADDING[64] = {
217 - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
220 -};
221
222 -/* F, G, H and I are basic MD5 functions.
223 - */
224 -#define F(x, y, z) (((x) &amp; (y)) | ((~x) &amp; (z)))
225 -#define G(x, y, z) (((x) &amp; (z)) | ((y) &amp; (~z)))
226 -#define H(x, y, z) ((x) ^ (y) ^ (z))
227 -#define I(x, y, z) ((y) ^ ((x) | (~z)))
228 +#ifndef md5_INCLUDED
229 +# define md5_INCLUDED
230
231 -/* ROTATE_LEFT rotates x left n bits.
232 +/*
233 + * This package supports both compile-time and run-time determination of CPU
234 + * byte order. If ARCH_IS_BIG_ENDIAN is defined as 0, the code will be
235 + * compiled to run only on little-endian CPUs; if ARCH_IS_BIG_ENDIAN is
236 + * defined as non-zero, the code will be compiled to run only on big-endian
237 + * CPUs; if ARCH_IS_BIG_ENDIAN is not defined, the code will be compiled to
238 + * run on either big- or little-endian CPUs, but will run slightly less
239 + * efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined.
240 */
241 -#define ROTATE_LEFT(x, n) (((x) &lt;&lt; (n)) | ((x) &gt;&gt; (32-(n))))
242
243 -/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
244 -Rotation is separate from addition to prevent recomputation.
245 - */
246 -#define FF(a, b, c, d, x, s, ac) { \
247 - (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
248 - (a) = ROTATE_LEFT ((a), (s)); \
249 - (a) += (b); \
250 - }
251 -#define GG(a, b, c, d, x, s, ac) { \
252 - (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
253 - (a) = ROTATE_LEFT ((a), (s)); \
254 - (a) += (b); \
255 - }
256 -#define HH(a, b, c, d, x, s, ac) { \
257 - (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
258 - (a) = ROTATE_LEFT ((a), (s)); \
259 - (a) += (b); \
260 - }
261 -#define II(a, b, c, d, x, s, ac) { \
262 - (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
263 - (a) = ROTATE_LEFT ((a), (s)); \
264 - (a) += (b); \
265 - }
266 +typedef unsigned char md5_byte_t; /* 8-bit byte */
267 +typedef unsigned int md5_word_t; /* 32-bit word */
268
269 -/* MD5 initialization. Begins an MD5 operation, writing a new context.
270 - */
271 -static void MD5Init (context)
272 -MD5_CTX *context; /* context */
273 -{
274 - context-&gt;count[0] = context-&gt;count[1] = 0;
275 - /* Load magic initialization constants.
276 -*/
277 - context-&gt;state[0] = 0x67452301;
278 - context-&gt;state[1] = 0xefcdab89;
279 - context-&gt;state[2] = 0x98badcfe;
280 - context-&gt;state[3] = 0x10325476;
281 -}
282 +/* Define the state of the MD5 Algorithm. */
283 +typedef struct md5_state_s {
284 + md5_word_t count[2]; /* message length in bits, lsw first */
285 + md5_word_t abcd[4]; /* digest buffer */
286 + md5_byte_t buf[64]; /* accumulate block */
287 +} md5_state_t;
288
289 -/* MD5 block update operation. Continues an MD5 message-digest
290 - operation, processing another message block, and updating the
291 - context.
292 - */
293 -static void MD5Update (context, input, inputLen)
294 -MD5_CTX *context; /* context */
295 -unsigned char *input; /* input block */
296 -unsigned int inputLen; /* length of input block */
297 +#ifdef __cplusplus
298 +extern &quot;C&quot;
299 {
300 - unsigned int i, index, partLen;
301 +#endif
302
303 - /* Compute number of bytes mod 64 */
304 - index = (unsigned int)((context-&gt;count[0] &gt;&gt; 3) &amp; 0x3F);
305 +/* Initialize the algorithm. */
306 +void md5_init(md5_state_t *pms);
307
308 - /* Update number of bits */
309 - if ((context-&gt;count[0] += ((UINT4)inputLen &lt;&lt; 3))
310 - &lt; ((UINT4)inputLen &lt;&lt; 3))
311 - context-&gt;count[1]++;
312 - context-&gt;count[1] += ((UINT4)inputLen &gt;&gt; 29);
313 -
314 - partLen = 64 - index;
315 -
316 - /* Transform as many times as possible.
317 -*/
318 - if (inputLen &gt;= partLen) {
319 - memcpy
320 - ((POINTER)&amp;context-&gt;buffer[index], (POINTER)input, partLen);
321 - MD5Transform (context-&gt;state, context-&gt;buffer);
322 -
323 - for (i = partLen; i + 63 &lt; inputLen; i += 64)
324 - MD5Transform (context-&gt;state, &amp;input[i]);
325 -
326 - index = 0;
327 - }
328 - else
329 - i = 0;
330 -
331 - /* Buffer remaining input */
332 - memcpy
333 - ((POINTER)&amp;context-&gt;buffer[index], (POINTER)&amp;input[i],
334 - inputLen-i);
335 -}
336 +/* Append a string to the message. */
337 +void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
338
339 -/* MD5 finalization. Ends an MD5 message-digest operation, writing the
340 - the message digest and zeroizing the context.
341 - */
342 -static void MD5Final (digest, context)
343 -unsigned char digest[16]; /* message digest */
344 -MD5_CTX *context; /* context */
345 -{
346 - unsigned char bits[8];
347 - unsigned int index, padLen;
348 +/* Finish the message and return the digest. */
349 +void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
350
351 - /* Save number of bits */
352 - Encode (bits, context-&gt;count, 8);
353 +#ifdef __cplusplus
354 +} /* end extern &quot;C&quot; */
355 +#endif
356
357 - /* Pad out to 56 mod 64.
358 -*/
359 - index = (unsigned int)((context-&gt;count[0] &gt;&gt; 3) &amp; 0x3f);
360 - padLen = (index &lt; 56) ? (56 - index) : (120 - index);
361 - MD5Update (context, PADDING, padLen);
362 -
363 - /* Append length (before padding) */
364 - MD5Update (context, bits, 8);
365 - /* Store state in digest */
366 - Encode (digest, context-&gt;state, 16);
367 -
368 - /* Zeroize sensitive information.
369 -*/
370 - memset ((POINTER)context, 0, sizeof (*context));
371 -}
372 +#endif /* md5_INCLUDED */
373 +/*======== End of L. Peter Deutsch's md5.h ========*/
374
375 -/* MD5 basic transformation. Transforms state based on block.
376 - */
377 -static void MD5Transform (state, block)
378 -UINT4 state[4];
379 -unsigned char block[64];
380 -{
381 - UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
382 +/*======== Beginning of L. Peter Deutsch's md5.c (with the line ========
383 + ======== [#include &quot;md5.h&quot;] removed) ========*/
384 +/*
385 + Copyright (C) 1999, 2000, 2002 Aladdin Enterprises. All rights reserved.
386
387 - Decode (x, block, 64);
388 + This software is provided 'as-is', without any express or implied
389 + warranty. In no event will the authors be held liable for any damages
390 + arising from the use of this software.
391
392 - /* Round 1 */
393 - FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
394 - FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
395 - FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
396 - FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
397 - FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
398 - FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
399 - FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
400 - FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
401 - FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
402 - FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
403 - FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
404 - FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
405 - FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
406 - FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
407 - FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
408 - FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
409 -
410 - /* Round 2 */
411 - GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
412 - GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
413 - GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
414 - GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
415 - GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
416 - GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
417 - GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
418 - GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
419 - GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
420 - GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
421 - GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
422 - GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
423 - GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
424 - GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
425 - GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
426 - GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
427 -
428 - /* Round 3 */
429 - HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
430 - HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
431 - HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
432 - HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
433 - HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
434 - HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
435 - HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
436 - HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
437 - HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
438 - HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
439 - HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
440 - HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
441 - HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
442 - HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
443 - HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
444 - HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
445 -
446 - /* Round 4 */
447 - II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
448 - II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
449 - II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
450 - II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
451 - II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
452 - II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
453 - II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
454 - II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
455 - II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
456 - II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
457 - II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
458 - II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
459 - II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
460 - II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
461 - II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
462 - II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
463 -
464 - state[0] += a;
465 - state[1] += b;
466 - state[2] += c;
467 - state[3] += d;
468 -
469 - /* Zeroize sensitive information.
470 -*/
471 - memset ((POINTER)x, 0, sizeof (x));
472 -}
473 + Permission is granted to anyone to use this software for any purpose,
474 + including commercial applications, and to alter it and redistribute it
475 + freely, subject to the following restrictions:
476
477 -/* Encodes input (UINT4) into output (unsigned char). Assumes len is
478 - a multiple of 4.
479 - */
480 -static void Encode (output, input, len)
481 -unsigned char *output;
482 -UINT4 *input;
483 -unsigned int len;
484 -{
485 - unsigned int i, j;
486 + 1. The origin of this software must not be misrepresented; you must not
487 + claim that you wrote the original software. If you use this software
488 + in a product, an acknowledgment in the product documentation would be
489 + appreciated but is not required.
490 + 2. Altered source versions must be plainly marked as such, and must not be
491 + misrepresented as being the original software.
492 + 3. This notice may not be removed or altered from any source distribution.
493
494 - for (i = 0, j = 0; j &lt; len; i++, j += 4) {
495 - output[j] = (unsigned char)(input[i] &amp; 0xff);
496 - output[j+1] = (unsigned char)((input[i] &gt;&gt; 8) &amp; 0xff);
497 - output[j+2] = (unsigned char)((input[i] &gt;&gt; 16) &amp; 0xff);
498 - output[j+3] = (unsigned char)((input[i] &gt;&gt; 24) &amp; 0xff);
499 - }
500 -}
501 + L. Peter Deutsch
502 + <A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">ghost at aladdin.com</A>
503
504 -/* Decodes input (unsigned char) into output (UINT4). Assumes len is
505 - a multiple of 4.
506 */
507 -static void Decode (output, input, len)
508 -UINT4 *output;
509 -unsigned char *input;
510 -unsigned int len;
511 -{
512 - unsigned int i, j;
513 +/* $Id: md5.c,v 1.6 2002/04/13 19:20:28 lpd Exp $ */
514 +/*
515 + Independent implementation of MD5 (RFC 1321).
516
517 - for (i = 0, j = 0; j &lt; len; i++, j += 4)
518 - output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) &lt;&lt; 8) |
519 - (((UINT4)input[j+2]) &lt;&lt; 16) | (((UINT4)input[j+3]) &lt;&lt; 24);
520 -}
521 + This code implements the MD5 Algorithm defined in RFC 1321, whose
522 + text is available at
523 + <A HREF="http://www.ietf.org/rfc/rfc1321.txt">http://www.ietf.org/rfc/rfc1321.txt</A>
524 + The code is derived from the text of the RFC, including the test suite
525 + (section A.5) but excluding the rest of Appendix A. It does not include
526 + any code or documentation that is identified in the RFC as being
527 + copyrighted.
528 +
529 + The original and principal author of md5.c is L. Peter Deutsch
530 + &lt;<A HREF="http://lists.ircservices.za.net/mailman/listinfo/ircservices-coding">ghost at aladdin.com</A>&gt;. Other authors are noted in the change history
531 + that follows (in reverse chronological order):
532 +
533 + 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order
534 + either statically or dynamically; added missing #include &lt;string.h&gt;
535 + in library.
536 + 2002-03-11 lpd Corrected argument list for main(), and added int return
537 + type, in test program and T value program.
538 + 2002-02-21 lpd Added missing #include &lt;stdio.h&gt; in test program.
539 + 2000-07-03 lpd Patched to eliminate warnings about &quot;constant is
540 + unsigned in ANSI C, signed in traditional&quot;; made test program
541 + self-checking.
542 + 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
543 + 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
544 + 1999-05-03 lpd Original version.
545 + */
546
547 -/*======== End of md5c.c ========*/
548 +#include &lt;string.h&gt;
549 +
550 +#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
551 +#ifdef ARCH_IS_BIG_ENDIAN
552 +# define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
553 +#else
554 +# define BYTE_ORDER 0
555 +#endif
556 +
557 +#define T_MASK ((md5_word_t)~0)
558 +#define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
559 +#define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
560 +#define T3 0x242070db
561 +#define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
562 +#define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
563 +#define T6 0x4787c62a
564 +#define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
565 +#define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
566 +#define T9 0x698098d8
567 +#define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
568 +#define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
569 +#define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
570 +#define T13 0x6b901122
571 +#define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
572 +#define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
573 +#define T16 0x49b40821
574 +#define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
575 +#define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
576 +#define T19 0x265e5a51
577 +#define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
578 +#define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
579 +#define T22 0x02441453
580 +#define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
581 +#define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
582 +#define T25 0x21e1cde6
583 +#define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
584 +#define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
585 +#define T28 0x455a14ed
586 +#define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
587 +#define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
588 +#define T31 0x676f02d9
589 +#define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
590 +#define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
591 +#define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
592 +#define T35 0x6d9d6122
593 +#define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
594 +#define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
595 +#define T38 0x4bdecfa9
596 +#define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
597 +#define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
598 +#define T41 0x289b7ec6
599 +#define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
600 +#define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
601 +#define T44 0x04881d05
602 +#define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
603 +#define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
604 +#define T47 0x1fa27cf8
605 +#define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
606 +#define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
607 +#define T50 0x432aff97
608 +#define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
609 +#define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
610 +#define T53 0x655b59c3
611 +#define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
612 +#define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
613 +#define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
614 +#define T57 0x6fa87e4f
615 +#define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
616 +#define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
617 +#define T60 0x4e0811a1
618 +#define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
619 +#define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
620 +#define T63 0x2ad7d2bb
621 +#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
622 +
623 +
624 +static void
625 +md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
626 +{
627 + md5_word_t
628 + a = pms-&gt;abcd[0], b = pms-&gt;abcd[1],
629 + c = pms-&gt;abcd[2], d = pms-&gt;abcd[3];
630 + md5_word_t t;
631 +#if BYTE_ORDER &gt; 0
632 + /* Define storage only for big-endian CPUs. */
633 + md5_word_t X[16];
634 +#else
635 + /* Define storage for little-endian or both types of CPUs. */
636 + md5_word_t xbuf[16];
637 + const md5_word_t *X;
638 +#endif
639 +
640 + {
641 +#if BYTE_ORDER == 0
642 + /*
643 + * Determine dynamically whether this is a big-endian or
644 + * little-endian machine, since we can use a more efficient
645 + * algorithm on the latter.
646 + */
647 + static const int w = 1;
648 +
649 + if (*((const md5_byte_t *)&amp;w)) /* dynamic little-endian */
650 +#endif
651 +#if BYTE_ORDER &lt;= 0 /* little-endian */
652 + {
653 + /*
654 + * On little-endian machines, we can process properly aligned
655 + * data without copying it.
656 + */
657 + if (!((data - (const md5_byte_t *)0) &amp; 3)) {
658 + /* data are properly aligned */
659 + X = (const md5_word_t *)data;
660 + } else {
661 + /* not aligned */
662 + memcpy(xbuf, data, 64);
663 + X = xbuf;
664 + }
665 + }
666 +#endif
667 +#if BYTE_ORDER == 0
668 + else /* dynamic big-endian */
669 +#endif
670 +#if BYTE_ORDER &gt;= 0 /* big-endian */
671 + {
672 + /*
673 + * On big-endian machines, we must arrange the bytes in the
674 + * right order.
675 + */
676 + const md5_byte_t *xp = data;
677 + int i;
678 +
679 +# if BYTE_ORDER == 0
680 + X = xbuf; /* (dynamic only) */
681 +# else
682 +# define xbuf X /* (static only) */
683 +# endif
684 + for (i = 0; i &lt; 16; ++i, xp += 4)
685 + xbuf[i] = xp[0] + (xp[1] &lt;&lt; 8) + (xp[2] &lt;&lt; 16) + (xp[3] &lt;&lt; 24);
686 + }
687 +#endif
688 + }
689 +
690 +#define ROTATE_LEFT(x, n) (((x) &lt;&lt; (n)) | ((x) &gt;&gt; (32 - (n))))
691 +
692 + /* Round 1. */
693 + /* Let [abcd k s i] denote the operation
694 + a = b + ((a + F(b,c,d) + X[k] + T[i]) &lt;&lt;&lt; s). */
695 +#define F(x, y, z) (((x) &amp; (y)) | (~(x) &amp; (z)))
696 +#define SET(a, b, c, d, k, s, Ti)\
697 + t = a + F(b,c,d) + X[k] + Ti;\
698 + a = ROTATE_LEFT(t, s) + b
699 + /* Do the following 16 operations. */
700 + SET(a, b, c, d, 0, 7, T1);
701 + SET(d, a, b, c, 1, 12, T2);
702 + SET(c, d, a, b, 2, 17, T3);
703 + SET(b, c, d, a, 3, 22, T4);
704 + SET(a, b, c, d, 4, 7, T5);
705 + SET(d, a, b, c, 5, 12, T6);
706 + SET(c, d, a, b, 6, 17, T7);
707 + SET(b, c, d, a, 7, 22, T8);
708 + SET(a, b, c, d, 8, 7, T9);
709 + SET(d, a, b, c, 9, 12, T10);
710 + SET(c, d, a, b, 10, 17, T11);
711 + SET(b, c, d, a, 11, 22, T12);
712 + SET(a, b, c, d, 12, 7, T13);
713 + SET(d, a, b, c, 13, 12, T14);
714 + SET(c, d, a, b, 14, 17, T15);
715 + SET(b, c, d, a, 15, 22, T16);
716 +#undef SET
717 +
718 + /* Round 2. */
719 + /* Let [abcd k s i] denote the operation
720 + a = b + ((a + G(b,c,d) + X[k] + T[i]) &lt;&lt;&lt; s). */
721 +#define G(x, y, z) (((x) &amp; (z)) | ((y) &amp; ~(z)))
722 +#define SET(a, b, c, d, k, s, Ti)\
723 + t = a + G(b,c,d) + X[k] + Ti;\
724 + a = ROTATE_LEFT(t, s) + b
725 + /* Do the following 16 operations. */
726 + SET(a, b, c, d, 1, 5, T17);
727 + SET(d, a, b, c, 6, 9, T18);
728 + SET(c, d, a, b, 11, 14, T19);
729 + SET(b, c, d, a, 0, 20, T20);
730 + SET(a, b, c, d, 5, 5, T21);
731 + SET(d, a, b, c, 10, 9, T22);
732 + SET(c, d, a, b, 15, 14, T23);
733 + SET(b, c, d, a, 4, 20, T24);
734 + SET(a, b, c, d, 9, 5, T25);
735 + SET(d, a, b, c, 14, 9, T26);
736 + SET(c, d, a, b, 3, 14, T27);
737 + SET(b, c, d, a, 8, 20, T28);
738 + SET(a, b, c, d, 13, 5, T29);
739 + SET(d, a, b, c, 2, 9, T30);
740 + SET(c, d, a, b, 7, 14, T31);
741 + SET(b, c, d, a, 12, 20, T32);
742 +#undef SET
743 +
744 + /* Round 3. */
745 + /* Let [abcd k s t] denote the operation
746 + a = b + ((a + H(b,c,d) + X[k] + T[i]) &lt;&lt;&lt; s). */
747 +#define H(x, y, z) ((x) ^ (y) ^ (z))
748 +#define SET(a, b, c, d, k, s, Ti)\
749 + t = a + H(b,c,d) + X[k] + Ti;\
750 + a = ROTATE_LEFT(t, s) + b
751 + /* Do the following 16 operations. */
752 + SET(a, b, c, d, 5, 4, T33);
753 + SET(d, a, b, c, 8, 11, T34);
754 + SET(c, d, a, b, 11, 16, T35);
755 + SET(b, c, d, a, 14, 23, T36);
756 + SET(a, b, c, d, 1, 4, T37);
757 + SET(d, a, b, c, 4, 11, T38);
758 + SET(c, d, a, b, 7, 16, T39);
759 + SET(b, c, d, a, 10, 23, T40);
760 + SET(a, b, c, d, 13, 4, T41);
761 + SET(d, a, b, c, 0, 11, T42);
762 + SET(c, d, a, b, 3, 16, T43);
763 + SET(b, c, d, a, 6, 23, T44);
764 + SET(a, b, c, d, 9, 4, T45);
765 + SET(d, a, b, c, 12, 11, T46);
766 + SET(c, d, a, b, 15, 16, T47);
767 + SET(b, c, d, a, 2, 23, T48);
768 +#undef SET
769 +
770 + /* Round 4. */
771 + /* Let [abcd k s t] denote the operation
772 + a = b + ((a + I(b,c,d) + X[k] + T[i]) &lt;&lt;&lt; s). */
773 +#define I(x, y, z) ((y) ^ ((x) | ~(z)))
774 +#define SET(a, b, c, d, k, s, Ti)\
775 + t = a + I(b,c,d) + X[k] + Ti;\
776 + a = ROTATE_LEFT(t, s) + b
777 + /* Do the following 16 operations. */
778 + SET(a, b, c, d, 0, 6, T49);
779 + SET(d, a, b, c, 7, 10, T50);
780 + SET(c, d, a, b, 14, 15, T51);
781 + SET(b, c, d, a, 5, 21, T52);
782 + SET(a, b, c, d, 12, 6, T53);
783 + SET(d, a, b, c, 3, 10, T54);
784 + SET(c, d, a, b, 10, 15, T55);
785 + SET(b, c, d, a, 1, 21, T56);
786 + SET(a, b, c, d, 8, 6, T57);
787 + SET(d, a, b, c, 15, 10, T58);
788 + SET(c, d, a, b, 6, 15, T59);
789 + SET(b, c, d, a, 13, 21, T60);
790 + SET(a, b, c, d, 4, 6, T61);
791 + SET(d, a, b, c, 11, 10, T62);
792 + SET(c, d, a, b, 2, 15, T63);
793 + SET(b, c, d, a, 9, 21, T64);
794 +#undef SET
795 +
796 + /* Then perform the following additions. (That is increment each
797 + of the four registers by the value it had before this block
798 + was started.) */
799 + pms-&gt;abcd[0] += a;
800 + pms-&gt;abcd[1] += b;
801 + pms-&gt;abcd[2] += c;
802 + pms-&gt;abcd[3] += d;
803 +}
804 +
805 +void
806 +md5_init(md5_state_t *pms)
807 +{
808 + pms-&gt;count[0] = pms-&gt;count[1] = 0;
809 + pms-&gt;abcd[0] = 0x67452301;
810 + pms-&gt;abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
811 + pms-&gt;abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
812 + pms-&gt;abcd[3] = 0x10325476;
813 +}
814 +
815 +void
816 +md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
817 +{
818 + const md5_byte_t *p = data;
819 + int left = nbytes;
820 + int offset = (pms-&gt;count[0] &gt;&gt; 3) &amp; 63;
821 + md5_word_t nbits = (md5_word_t)(nbytes &lt;&lt; 3);
822 +
823 + if (nbytes &lt;= 0)
824 + return;
825 +
826 + /* Update the message length. */
827 + pms-&gt;count[1] += nbytes &gt;&gt; 29;
828 + pms-&gt;count[0] += nbits;
829 + if (pms-&gt;count[0] &lt; nbits)
830 + pms-&gt;count[1]++;
831 +
832 + /* Process an initial partial block. */
833 + if (offset) {
834 + int copy = (offset + nbytes &gt; 64 ? 64 - offset : nbytes);
835 +
836 + memcpy(pms-&gt;buf + offset, p, copy);
837 + if (offset + copy &lt; 64)
838 + return;
839 + p += copy;
840 + left -= copy;
841 + md5_process(pms, pms-&gt;buf);
842 + }
843 +
844 + /* Process full blocks. */
845 + for (; left &gt;= 64; p += 64, left -= 64)
846 + md5_process(pms, p);
847 +
848 + /* Process a final partial block. */
849 + if (left)
850 + memcpy(pms-&gt;buf, p, left);
851 +}
852 +
853 +void
854 +md5_finish(md5_state_t *pms, md5_byte_t digest[16])
855 +{
856 + static const md5_byte_t pad[64] = {
857 + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
858 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
859 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
860 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
861 + };
862 + md5_byte_t data[8];
863 + int i;
864 +
865 + /* Save the length before padding. */
866 + for (i = 0; i &lt; 8; ++i)
867 + data[i] = (md5_byte_t)(pms-&gt;count[i &gt;&gt; 2] &gt;&gt; ((i &amp; 3) &lt;&lt; 3));
868 + /* Pad to 56 bytes mod 64. */
869 + md5_append(pms, pad, ((55 - (pms-&gt;count[0] &gt;&gt; 3)) &amp; 63) + 1);
870 + /* Append the length. */
871 + md5_append(pms, data, 8);
872 + for (i = 0; i &lt; 16; ++i)
873 + digest[i] = (md5_byte_t)(pms-&gt;abcd[i &gt;&gt; 2] &gt;&gt; ((i &amp; 3) &lt;&lt; 3));
874 +}
875 +/*======== End of L. Peter Deutsch's md5.c ========*/
876 /*************************************************************************/
877 /*************************************************************************/
878
879 @@ -347,13 +508,13 @@
880
881 static int md5_encrypt(const char *src, int len, char *dest, int size)
882 {
883 - MD5_CTX context;
884 + md5_state_t context;
885
886 if (size &lt; 16)
887 return 16;
888 - MD5Init(&amp;context);
889 - MD5Update(&amp;context, (unsigned char *)src, len);
890 - MD5Final((unsigned char *)dest, &amp;context);
891 + md5_init(&amp;context);
892 + md5_append(&amp;context, (unsigned char *)src, len);
893 + md5_finish(&amp;context, (unsigned char *)dest);
894 return 0;
895 }
896
897 </PRE>
898
899
900
901
902
903
904
905 <!--endarticle-->
906 <HR>
907 <P><UL>
908 <!--threads-->
909 <LI>Previous message: <A HREF="003289.html">[IRCServices Coding] md5 licensing
910 </A></li>
911 <LI>Next message: <A HREF="003299.html">[IRCServices Coding] md5 licensing
912 </A></li>
913 <LI> <B>Messages sorted by:</B>
914 <a href="date.html#3291">[ date ]</a>
915 <a href="thread.html#3291">[ thread ]</a>
916 <a href="subject.html#3291">[ subject ]</a>
917 <a href="author.html#3291">[ author ]</a>
918 </LI>
919 </UL>
920
921 </body></html>