home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / ircd4652.zip / ircd-df-4.6.5-os2 / src / md5.c < prev    next >
C/C++ Source or Header  |  1997-12-29  |  6KB  |  168 lines

  1. /*    $Id: md5.c,v 1.2 1997/12/29 07:17:43 wd Exp $    */
  2.  
  3. /*
  4.  * Copyright (c) 1996 Michael Shalayeff.
  5.  *
  6.  * This software derived from one contributed by Colin Plumb.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by Colin Plumb.
  19.  * 4. Neither the name of the University nor of the Laboratory may be used
  20.  *    to endorse or promote products derived from this software without
  21.  *    specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  *
  35.  */
  36.  
  37. /*
  38.  * The code for MD5 transform was taken from Colin Plumb's
  39.  * implementation, which has been placed in the public domain.  The
  40.  * MD5 cryptographic checksum was devised by Ronald Rivest, and is
  41.  * documented in RFC 1321, "The MD5 Message Digest Algorithm".
  42.  * 
  43.  */
  44.  
  45. #include <sys/types.h>
  46.  
  47. #include "struct.h"
  48.  
  49. /*
  50.  * MD5 transform algorithm, taken from code written by Colin Plumb,
  51.  * and put into the public domain
  52.  *
  53.  * QUESTION: Replace this with SHA, which as generally received better
  54.  * reviews from the cryptographic community?
  55.  */
  56. void
  57. MD5Init(buf)
  58.     u_int32_t    buf[4];
  59. {
  60.     buf[0] = 0x67452301;
  61.     buf[1] = 0xefcdab89;
  62.     buf[2] = 0x98badcfe;
  63.     buf[3] = 0x10325476;
  64. }
  65.  
  66. /* The four core functions - F1 is optimized somewhat */
  67.  
  68. /* #define F1(x, y, z) (x & y | ~x & z) */
  69. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  70. #define F2(x, y, z) F1(z, x, y)
  71. #define F3(x, y, z) (x ^ y ^ z)
  72. #define F4(x, y, z) (y ^ (x | ~z))
  73.  
  74. /* This is the central step in the MD5 algorithm. */
  75. #define MD5STEP(f, w, x, y, z, data, s) \
  76.     ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
  77.  
  78. /*
  79.  * The core of the MD5 algorithm, this alters an existing MD5 hash to
  80.  * reflect the addition of 16 longwords of new data.
  81.  */
  82. void
  83. MD5Transform(buf, in)
  84.     u_int32_t    buf[4];
  85.     u_int32_t in[16];
  86. {
  87.     u_int32_t    a, b, c, d;
  88.  
  89.     a = buf[0];
  90.     b = buf[1];
  91.     c = buf[2];
  92.     d = buf[3];
  93.  
  94.     MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478,  7);
  95.     MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
  96.     MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
  97.     MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
  98.     MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf,  7);
  99.     MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
  100.     MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
  101.     MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
  102.     MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8,  7);
  103.     MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
  104.     MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
  105.     MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
  106.     MD5STEP(F1, a, b, c, d, in[12]+0x6b901122,  7);
  107.     MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
  108.     MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
  109.     MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
  110.  
  111.     MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562,  5);
  112.     MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340,  9);
  113.     MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
  114.     MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
  115.     MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d,  5);
  116.     MD5STEP(F2, d, a, b, c, in[10]+0x02441453,  9);
  117.     MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
  118.     MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
  119.     MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6,  5);
  120.     MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6,  9);
  121.     MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
  122.     MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
  123.     MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905,  5);
  124.     MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8,  9);
  125.     MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
  126.     MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
  127.  
  128.     MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942,  4);
  129.     MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
  130.     MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
  131.     MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
  132.     MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44,  4);
  133.     MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
  134.     MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
  135.     MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
  136.     MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6,  4);
  137.     MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
  138.     MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
  139.     MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
  140.     MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039,  4);
  141.     MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
  142.     MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
  143.     MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
  144.  
  145.     MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244,  6);
  146.     MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
  147.     MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
  148.     MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
  149.     MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3,  6);
  150.     MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
  151.     MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
  152.     MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
  153.     MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f,  6);
  154.     MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
  155.     MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
  156.     MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
  157.     MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82,  6);
  158.     MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
  159.     MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
  160.     MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
  161.  
  162.     buf[0] += a;
  163.     buf[1] += b;
  164.     buf[2] += c;
  165.     buf[3] += d;
  166. }
  167.  
  168.