md5.c File Reference

#include <string.h>
#include "pi-md5.h"

Include dependency graph for md5.c:

Go to the source code of this file.

Defines

#define F1(x, y, z)   (z ^ (x & (y ^ z)))
#define F2(x, y, z)   F1(z, x, y)
#define F3(x, y, z)   (x ^ y ^ z)
#define F4(x, y, z)   (y ^ (x | ~z))
#define MD5STEP(f, w, x, y, z, in, s)   (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)

Functions

void byteSwap (UINT32 *buf, unsigned words)
void MD5Init (struct MD5Context *ctx)
void MD5Update (struct MD5Context *ctx, UINT8 const *buf, unsigned len)
void MD5Final (UINT8 digest[16], struct MD5Context *ctx)
void MD5Transform (UINT32 buf[4], UINT32 const in[16])


Define Documentation

#define F1 ( x,
y,
 )     (z ^ (x & (y ^ z)))

Definition at line 136 of file md5.c.

Referenced by MD5Transform().

#define F2 ( x,
y,
 )     F1(z, x, y)

Definition at line 137 of file md5.c.

Referenced by MD5Transform().

#define F3 ( x,
y,
 )     (x ^ y ^ z)

Definition at line 138 of file md5.c.

Referenced by MD5Transform().

#define F4 ( x,
y,
 )     (y ^ (x | ~z))

Definition at line 139 of file md5.c.

Referenced by MD5Transform().

#define MD5STEP ( f,
w,
x,
y,
z,
in,
 )     (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)

Definition at line 142 of file md5.c.

Referenced by MD5Transform().


Function Documentation

void byteSwap ( UINT32 *  buf,
unsigned  words 
)

Definition at line 32 of file md5.c.

References UINT32, and UINT8.

00033 {
00034         UINT8 *p = (UINT8 *) buf;
00035 
00036         do {
00037                 *buf++ = (UINT32) ((unsigned) p[3] << 8 | p[2]) << 16 |
00038                     ((unsigned) p[1] << 8 | p[0]);
00039                 p += 4;
00040         } while (--words);
00041 }

void MD5Final ( UINT8  digest[16],
struct MD5Context ctx 
)

Definition at line 100 of file md5.c.

References MD5Context::buf, MD5Context::bytes, byteSwap(), MD5Context::in, MD5Transform(), and UINT8.

00101 {
00102         int     count = ctx->bytes[0] & 0x3f;   /* Number of bytes in ctx->in */
00103         UINT8   *p = (UINT8 *) ctx->in + count;
00104 
00105         /* Set the first char of padding to 0x80.  There is always room. */
00106         *p++ = 0x80;
00107 
00108         /* Bytes of padding needed to make 56 bytes (-8..55) */
00109         count = 56 - 1 - count;
00110 
00111         if (count < 0) {        /* Padding forces an extra block */
00112                 memset(p, 0, (size_t)(count + 8));
00113                 byteSwap(ctx->in, 16);
00114                 MD5Transform(ctx->buf, ctx->in);
00115                 p = (UINT8 *) ctx->in;
00116                 count = 56;
00117         }
00118         memset(p, 0, (size_t)count);
00119         byteSwap(ctx->in, 14);
00120 
00121         /* Append length in bits and transform */
00122         ctx->in[14] = ctx->bytes[0] << 3;
00123         ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
00124         MD5Transform(ctx->buf, ctx->in);
00125 
00126         byteSwap(ctx->buf, 4);
00127         memcpy(digest, ctx->buf, 16);
00128         memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
00129 }

Here is the call graph for this function:

void MD5Init ( struct MD5Context ctx  ) 

Definition at line 47 of file md5.c.

References MD5Context::buf, and MD5Context::bytes.

00048 {
00049         ctx->buf[0]     = 0x67452301;
00050         ctx->buf[1]     = 0xefcdab89;
00051         ctx->buf[2]     = 0x98badcfe;
00052         ctx->buf[3]     = 0x10325476;
00053         ctx->bytes[0]   = 0;
00054         ctx->bytes[1]   = 0;
00055 }

void MD5Transform ( UINT32  buf[4],
UINT32 const  in[16] 
)

Definition at line 150 of file md5.c.

References F1, F2, F3, F4, MD5STEP, and UINT32.

00151 {
00152         register UINT32 a, b, c, d;
00153 
00154         a = buf[0];
00155         b = buf[1];
00156         c = buf[2];
00157         d = buf[3];
00158 
00159         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00160         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00161         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00162         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00163         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00164         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00165         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00166         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00167         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00168         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00169         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00170         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00171         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00172         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00173         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00174         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00175 
00176         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00177         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00178         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00179         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00180         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00181         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00182         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00183         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00184         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00185         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00186         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00187         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00188         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00189         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00190         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00191         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00192 
00193         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00194         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00195         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00196         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00197         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00198         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00199         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00200         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00201         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00202         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00203         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00204         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00205         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00206         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00207         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00208         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00209 
00210         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00211         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00212         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00213         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00214         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00215         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00216         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00217         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00218         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00219         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00220         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00221         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00222         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00223         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00224         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00225         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00226 
00227         buf[0] += a;
00228         buf[1] += b;
00229         buf[2] += c;
00230         buf[3] += d;
00231 }

void MD5Update ( struct MD5Context ctx,
UINT8 const *  buf,
unsigned  len 
)

Definition at line 61 of file md5.c.

References MD5Context::buf, MD5Context::bytes, byteSwap(), MD5Context::in, MD5Transform(), UINT32, and UINT8.

00062 {
00063         UINT32 t;
00064 
00065         /* Update byte count */
00066 
00067         t = ctx->bytes[0];
00068         if ((ctx->bytes[0] = t + len) < t)
00069                 ctx->bytes[1]++;        /* Carry from low to high */
00070 
00071         t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
00072         if (t > len) {
00073                 memcpy((UINT8 *) ctx->in + 64 - t, buf, len);
00074                 return;
00075         }
00076         /* First chunk is an odd size */
00077         memcpy((UINT8 *) ctx->in + 64 - t, buf, t);
00078         byteSwap(ctx->in, 16);
00079         MD5Transform(ctx->buf, ctx->in);
00080         buf += t;
00081         len -= t;
00082 
00083         /* Process data in 64-byte chunks */
00084         while (len >= 64) {
00085                 memcpy(ctx->in, buf, 64);
00086                 byteSwap(ctx->in, 16);
00087                 MD5Transform(ctx->buf, ctx->in);
00088                 buf += 64;
00089                 len -= 64;
00090         }
00091 
00092         /* Handle any remaining bytes of data. */
00093         memcpy(ctx->in, buf, len);
00094 }

Here is the call graph for this function:


© 1996-2007 by pilot-link.org. All rights reserved.