Main Page | Data Structures | File List | Data Fields | Globals

md5.c

Go to the documentation of this file.
00001 /*
00002  * luau (Lib Update/Auto-Update): Simple Update Library
00003  * Copyright (C) 2003  David Eklund
00004  *
00005  * - This library is free software; you can redistribute it and/or             -
00006  * - modify it under the terms of the GNU Lesser General Public                -
00007  * - License as published by the Free Software Foundation; either              -
00008  * - version 2.1 of the License, or (at your option) any later version.        -
00009  * -                                                                           -
00010  * - This library is distributed in the hope that it will be useful,           -
00011  * - but WITHOUT ANY WARRANTY; without even the implied warranty of            -
00012  * - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         -
00013  * - Lesser General Public License for more details.                           -
00014  * -                                                                           -
00015  * - You should have received a copy of the GNU Lesser General Public          -
00016  * - License along with this library; if not, write to the Free Software       -
00017  * - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA -
00018  */
00019  
00020 /*
00021  * md5.h and md5.c are based off of md5hl.c, md5c.c, and md5.h from libmd, which in turn is
00022  * based off the FreeBSD libmd library.  Their respective copyright notices follow:
00023  */
00024 
00025 /*
00026  * This code implements the MD5 message-digest algorithm.
00027  * The algorithm is due to Ron Rivest.  This code was
00028  * written by Colin Plumb in 1993, no copyright is claimed.
00029  * This code is in the public domain; do with it what you wish.
00030  *
00031  * Equivalent code is available from RSA Data Security, Inc.
00032  * This code has been tested against that, and is equivalent,
00033  * except that you don't need to include two pages of legalese
00034  * with every copy.
00035  */
00036 
00037 /* ----------------------------------------------------------------------------
00038  * "THE BEER-WARE LICENSE" (Revision 42):
00039  * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
00040  * can do whatever you want with this stuff. If we meet some day, and you think
00041  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
00042  * ----------------------------------------------------------------------------
00043  *
00044  * $Id: md5.c,v 1.1.1.1 2004/04/02 05:11:38 deklund2 Exp $
00045  *
00046  */
00047 
00048 #ifdef HAVE_CONFIG_H
00049 #  include <config.h>
00050 #endif
00051 
00052 #include <sys/types.h>
00053 #include <fcntl.h>
00054 #include <unistd.h>
00055 #include <errno.h>
00056 #include <stdio.h>
00057 #include <stdlib.h>
00058 #include <string.h>
00059 
00060 #include "md5.h"
00061 #ifdef WITH_DMALLOC
00062 #  include <dmalloc.h>
00063 #endif
00064 
00065 static void MD5Init(MD5_CTX *context);
00066 static void MD5Update(MD5_CTX *context, unsigned char const *buf, unsigned len);
00067 static void MD5Final(unsigned char digest[MD5_HASHBYTES], MD5_CTX *context);
00068 static void MD5Transform(u_int32_t buf[4], u_int32_t const in[16]);
00069 static char* MD5End(MD5_CTX *, char *);
00070 
00071 
00072 #if __BYTE_ORDER == 1234
00073 #define byteReverse(buf, len)   /* Nothing */
00074 #else
00075 void byteReverse(unsigned char *buf, unsigned longs);
00076 
00077 /*
00078  * Note: this code is harmless on little-endian machines.
00079  */
00080 void byteReverse(unsigned char *buf, unsigned longs)
00081 {
00082     u_int32_t t;
00083     do {
00084         t = (u_int32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
00085             ((unsigned) buf[1] << 8 | buf[0]);
00086         *(u_int32_t *) buf = t;
00087         buf += 4;
00088     } while (--longs);
00089 }
00090 #endif /* ! __BYTE_ORDER == 1234 */
00091 
00092 
00093 
00094 
00095 char *
00096 lutil_md5_file (const char *filename, char *buf)
00097 {
00098     unsigned char buffer[BUFSIZ]; 
00099     MD5_CTX ctx;
00100     int f,i,j;
00101 
00102     MD5Init(&ctx);
00103     f = open(filename,O_RDONLY);
00104     if (f < 0) return 0;
00105     while ((i = read(f,buffer,sizeof buffer)) > 0) {
00106                 MD5Update(&ctx,buffer,i);
00107     }
00108     j = errno;
00109     close(f);
00110     errno = j;
00111     if (i < 0) return 0;
00112     return MD5End(&ctx, buf);
00113 }
00114 
00115 char *
00116 lutil_md5_data (const unsigned char *data, unsigned int len, char *buf)
00117 {
00118     MD5_CTX ctx;
00119 
00120     MD5Init(&ctx);
00121     MD5Update(&ctx,data,len);
00122     return MD5End(&ctx, buf);
00123 }
00124                                    
00125                                   
00126 /* Non-Interface Methods */
00127 
00128 /* from md5hl.c */
00129 
00130 char *
00131 MD5End(MD5_CTX *ctx, char *buf)
00132 {
00133     int i;
00134     unsigned char digest[MD5_HASHBYTES];
00135     static const char hex[]="0123456789abcdef";
00136 
00137     if (!buf)
00138         buf = malloc(33);
00139     if (!buf)
00140         return 0;
00141     MD5Final(digest,ctx);
00142     for (i=0;i<MD5_HASHBYTES;i++) {
00143         buf[i+i] = hex[digest[i] >> 4];
00144         buf[i+i+1] = hex[digest[i] & 0x0f];
00145     }
00146     buf[i+i] = '\0';
00147     return buf;
00148 }
00149 
00150 /*
00151  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
00152  * initialization constants.
00153  */
00154 void MD5Init(MD5_CTX *ctx)
00155 {
00156     ctx->buf[0] = 0x67452301;
00157     ctx->buf[1] = 0xefcdab89;
00158     ctx->buf[2] = 0x98badcfe;
00159     ctx->buf[3] = 0x10325476;
00160 
00161     ctx->bits[0] = 0;
00162     ctx->bits[1] = 0;
00163 }
00164 
00165 /*
00166  * Update context to reflect the concatenation of another buffer full
00167  * of bytes.
00168  */
00169 void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
00170 {
00171     u_int32_t t;
00172 
00173     /* Update bitcount */
00174 
00175     t = ctx->bits[0];
00176     if ((ctx->bits[0] = t + ((u_int32_t) len << 3)) < t)
00177         ctx->bits[1]++;         /* Carry from low to high */
00178     ctx->bits[1] += len >> 29;
00179 
00180     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
00181 
00182     /* Handle any leading odd-sized chunks */
00183 
00184     if (t) {
00185         unsigned char *p = (unsigned char *) ctx->in + t;
00186 
00187         t = 64 - t;
00188         if (len < t) {
00189             memcpy(p, buf, len);
00190             return;
00191         }
00192         memcpy(p, buf, t);
00193         byteReverse(ctx->in, 16);
00194         MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00195         buf += t;
00196         len -= t;
00197     }
00198     /* Process data in 64-byte chunks */
00199 
00200     while (len >= 64) {
00201         memcpy(ctx->in, buf, 64);
00202         byteReverse(ctx->in, 16);
00203         MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00204         buf += 64;
00205         len -= 64;
00206     }
00207 
00208     /* Handle any remaining bytes of data. */
00209 
00210     memcpy(ctx->in, buf, len);
00211 }
00212 
00213 /*
00214  * Final wrapup - pad to 64-byte boundary with the bit pattern 
00215  * 1 0* (64-bit count of bits processed, MSB-first)
00216  */
00217 void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
00218 {
00219     unsigned count;
00220     unsigned char *p;
00221 
00222     /* Compute number of bytes mod 64 */
00223     count = (ctx->bits[0] >> 3) & 0x3F;
00224 
00225     /* Set the first char of padding to 0x80.  This is safe since there is
00226        always at least one byte free */
00227     p = ctx->in + count;
00228     *p++ = 0x80;
00229 
00230     /* Bytes of padding needed to make 64 bytes */
00231     count = 64 - 1 - count;
00232 
00233     /* Pad out to 56 mod 64 */
00234     if (count < 8) {
00235         /* Two lots of padding:  Pad the first block to 64 bytes */
00236         memset(p, 0, count);
00237         byteReverse(ctx->in, 16);
00238         MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00239 
00240         /* Now fill the next block with 56 bytes */
00241         memset(ctx->in, 0, 56);
00242     } else {
00243         /* Pad block to 56 bytes */
00244         memset(p, 0, count - 8);
00245     }
00246     byteReverse(ctx->in, 14);
00247 
00248     /* Append length in bits and transform */
00249     ((u_int32_t *) ctx->in)[14] = ctx->bits[0];
00250     ((u_int32_t *) ctx->in)[15] = ctx->bits[1];
00251 
00252     MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00253     byteReverse((unsigned char *) ctx->buf, 4);
00254     memcpy(digest, ctx->buf, 16);
00255     memset((char *) ctx, 0, sizeof(ctx));       /* In case it's sensitive */
00256 }
00257 
00258 /* The four core functions - F1 is optimized somewhat */
00259 
00260 /* #define F1(x, y, z) (x & y | ~x & z) */
00261 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00262 #define F2(x, y, z) F1(z, x, y)
00263 #define F3(x, y, z) (x ^ y ^ z)
00264 #define F4(x, y, z) (y ^ (x | ~z))
00265 
00266 /* This is the central step in the MD5 algorithm. */
00267 #define MD5STEP(f, w, x, y, z, data, s) \
00268         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
00269 
00270 /*
00271  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00272  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00273  * the data and converts bytes into longwords for this routine.
00274  */
00275 void MD5Transform(u_int32_t buf[4], u_int32_t const in[16])
00276 {
00277     register u_int32_t a, b, c, d;
00278 
00279     a = buf[0];
00280     b = buf[1];
00281     c = buf[2];
00282     d = buf[3];
00283 
00284     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00285     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00286     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00287     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00288     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00289     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00290     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00291     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00292     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00293     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00294     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00295     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00296     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00297     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00298     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00299     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00300 
00301     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00302     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00303     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00304     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00305     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00306     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00307     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00308     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00309     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00310     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00311     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00312     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00313     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00314     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00315     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00316     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00317 
00318     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00319     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00320     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00321     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00322     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00323     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00324     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00325     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00326     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00327     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00328     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00329     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00330     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00331     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00332     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00333     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00334 
00335     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00336     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00337     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00338     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00339     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00340     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00341     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00342     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00343     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00344     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00345     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00346     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00347     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00348     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00349     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00350     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00351 
00352     buf[0] += a;
00353     buf[1] += b;
00354     buf[2] += c;
00355     buf[3] += d;
00356 }

Generated on Mon Apr 12 22:17:11 2004 for luau by doxygen 1.3.2