md5.c
00001 /*
00002  * This code implements the MD5 message-digest algorithm.
00003  * The algorithm is due to Ron Rivest.  This code was
00004  * written by Colin Plumb in 1993, no copyright is claimed.
00005  * This code is in the public domain; do with it what you wish.
00006  *
00007  * Equivalent code is available from RSA Data Security, Inc.
00008  * This code has been tested against that, and is equivalent,
00009  * except that you don't need to include two pages of legalese
00010  * with every copy.
00011  *
00012  * To compute the message digest of a chunk of bytes, declare an
00013  * MD5Context structure, pass it to MD5Init, call MD5Update as
00014  * needed on buffers full of bytes, and then call MD5Final, which
00015  * will fill a supplied 16-byte array with the digest.
00016  */
00017
00018 #include "md5.h"
00019 #include <sec/util.h>
00020 #include <cpu/byteorder.h>
00021 #include <string.h>
00022
00023 static void MD5Transform(uint32_t buf[4], uint32_t in[16]);
00024
00025 static void byteReverse(uint32_t *buf, unsigned longs)
00026 {
00027     do {
00028         *buf = le32_to_cpu(*buf);
00029         ++buf;
00030     } while (--longs);
00031 }
00032
00033 /*
00034  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
00035  * initialization constants.
00036  */
00037 static void MD5_begin(Hash *h)
00038 {
00039     MD5_Context *ctx = (MD5_Context *)h;
00040
00041     ctx->buf[0] = 0x67452301;
00042     ctx->buf[1] = 0xefcdab89;
00043     ctx->buf[2] = 0x98badcfe;
00044     ctx->buf[3] = 0x10325476;
00045
00046     ctx->bits = 0;
00047 }
00048
00049 /*
00050  * Update context to reflect the concatenation of another buffer full
00051  * of bytes.
00052  */
00053 static void MD5_update(Hash *h, const void* vbuf, size_t len)
00054 {
00055     MD5_Context *ctx = (MD5_Context *)h;
00056     const char *buf = (const char *)vbuf;
00057     uint32_t *aligned_ptr = NULL;
00058     if (is_aligned(ctx->in, sizeof(uint32_t)))
00059         aligned_ptr = (uint32_t *)((size_t)ctx->in);
00060     else
00061         ASSERT2(0, "Unaligned memory");
00062     uint32_t t;
00063
00064     /* Update bitcount */
00065     t = (ctx->bits >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
00066     ctx->bits += len*8;
00067
00068     /* Handle any leading odd-sized chunks */
00069
00070     if (t)
00071     {
00072         uint8_t *p = (uint8_t*) ctx->in + t;
00073
00074         t = 64 - t;
00075         if (len < t)
00076         {
00077             memcpy(p, buf, len);
00078             return;
00079         }
00080         memcpy(p, buf, t);
00081         byteReverse(aligned_ptr, 16);
00082         MD5Transform(ctx->buf, aligned_ptr);
00083         buf += t;
00084         len -= t;
00085     }
00086     /* Process data in 64-byte chunks */
00087
00088     while (len >= 64)
00089     {
00090         memcpy(ctx->in, buf, 64);
00091         byteReverse(aligned_ptr, 16);
00092         MD5Transform(ctx->buf, aligned_ptr);
00093         buf += 64;
00094         len -= 64;
00095     }
00096
00097     /* Handle any remaining bytes of data. */
00098     memcpy(ctx->in, buf, len);
00099 }
00100
00101 /*
00102  * Final wrapup - pad to 64-byte boundary with the bit pattern
00103  * 1 0* (64-bit count of bits processed, MSB-first)
00104  */
00105 static uint8_t* MD5_final(struct Hash *h)
00106 {
00107     MD5_Context *ctx = (MD5_Context *)h;
00108     unsigned count;
00109     unsigned char *p;
00110     uint32_t *aligned_ptr = NULL;
00111     if (is_aligned(ctx->in, sizeof(uint32_t)))
00112         aligned_ptr = (uint32_t *)((size_t)ctx->in);
00113     else
00114         ASSERT2(0, "Unaligned memory");
00115
00116     /* Compute number of bytes mod 64 */
00117     count = (ctx->bits >> 3) & 0x3F;
00118
00119     /* Set the first char of padding to 0x80.  This is safe since there is
00120        always at least one byte free */
00121     p = ctx->in + count;
00122     *p++ = 0x80;
00123
00124     /* Bytes of padding needed to make 64 bytes */
00125     count = 64 - 1 - count;
00126
00127     /* Pad out to 56 mod 64 */
00128     if (count < 8)
00129     {
00130         /* Two lots of padding:  Pad the first block to 64 bytes */
00131         memset(p, 0, count);
00132         byteReverse(aligned_ptr, 16);
00133         MD5Transform(ctx->buf, aligned_ptr);
00134
00135         /* Now fill the next block with 56 bytes */
00136         memset(ctx->in, 0, 56);
00137     }
00138     else
00139     {
00140         /* Pad block to 56 bytes */
00141         memset(p, 0, count - 8);
00142     }
00143
00144     byteReverse(aligned_ptr, 14);
00145
00146     /* Append length in bits and transform */
00147     aligned_ptr[14] = (uint32_t)ctx->bits;
00148     aligned_ptr[15] = (uint32_t)(ctx->bits >> 32);
00149
00150     MD5Transform(ctx->buf, aligned_ptr);
00151     byteReverse((uint32_t*)ctx->buf, 4);
00152
00153     PURGE(ctx->in);
00154     PURGE(ctx->bits);
00155
00156     return (uint8_t *)ctx->buf;
00157 }
00158
00159
00160 /* The four core functions - F1 is optimized somewhat */
00161
00162 /* #define F1(x, y, z) (x & y | ~x & z) */
00163 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00164 #define F2(x, y, z) F1(z, x, y)
00165 #define F3(x, y, z) (x ^ y ^ z)
00166 #define F4(x, y, z) (y ^ (x | ~z))
00167 
00168 /* This is the central step in the MD5 algorithm. */
00169 #define MD5STEP(f, w, x, y, z, data, s) \
00170         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
00171 
00172 /*
00173  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00174  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00175  * the data and converts bytes into longwords for this routine.
00176  */
00177 static void MD5Transform(uint32_t buf[4], uint32_t in[16])
00178 {
00179     register uint32_t a, b, c, d;
00180
00181     a = buf[0];
00182     b = buf[1];
00183     c = buf[2];
00184     d = buf[3];
00185
00186     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00187     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00188     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00189     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00190     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00191     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00192     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00193     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00194     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00195     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00196     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00197     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00198     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00199     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00200     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00201     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00202
00203     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00204     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00205     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00206     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00207     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00208     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00209     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00210     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00211     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00212     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00213     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00214     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00215     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00216     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00217     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00218     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00219
00220     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00221     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00222     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00223     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00224     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00225     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00226     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00227     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00228     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00229     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00230     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00231     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00232     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00233     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00234     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00235     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00236
00237     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00238     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00239     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00240     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00241     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00242     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00243     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00244     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00245     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00246     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00247     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00248     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00249     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00250     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00251     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00252     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00253
00254     buf[0] += a;
00255     buf[1] += b;
00256     buf[2] += c;
00257     buf[3] += d;
00258 }
00259
00260 /*******************************************************************/
00261
00262 void MD5_init(MD5_Context *ctx)
00263 {
00264     ctx->h.begin = MD5_begin;
00265     ctx->h.update = MD5_update;
00266     ctx->h.final = MD5_final;
00267     ctx->h.digest_len = 16;
00268     ctx->h.block_len = 64;
00269 }