md5.c

Go to the documentation of this file.
00001 
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019 
00020 #include "common.h"
00021 #include "md5.h"
00022 #include "crypto.h"
00023 
00024 
00035 void hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
00036                      const u8 *addr[], const size_t *len, u8 *mac)
00037 {
00038         u8 k_pad[64]; /* padding - key XORd with ipad/opad */
00039         u8 tk[16];
00040         int i;
00041         const u8 *_addr[6];
00042         size_t _len[6];
00043 
00044         if (num_elem > 5) {
00045                 /*
00046                  * Fixed limit on the number of fragments to avoid having to
00047                  * allocate memory (which could fail).
00048                  */
00049                 return;
00050         }
00051 
00052         /* if key is longer than 64 bytes reset it to key = MD5(key) */
00053         if (key_len > 64) {
00054                 md5_vector(1, &key, &key_len, tk);
00055                 key = tk;
00056                 key_len = 16;
00057         }
00058 
00059         /* the HMAC_MD5 transform looks like:
00060          *
00061          * MD5(K XOR opad, MD5(K XOR ipad, text))
00062          *
00063          * where K is an n byte key
00064          * ipad is the byte 0x36 repeated 64 times
00065          * opad is the byte 0x5c repeated 64 times
00066          * and text is the data being protected */
00067 
00068         /* start out by storing key in ipad */
00069         memset(k_pad, 0, sizeof(k_pad));
00070         memcpy(k_pad, key, key_len);
00071 
00072         /* XOR key with ipad values */
00073         for (i = 0; i < 64; i++)
00074                 k_pad[i] ^= 0x36;
00075 
00076         /* perform inner MD5 */
00077         _addr[0] = k_pad;
00078         _len[0] = 64;
00079         for (i = 0; i < num_elem; i++) {
00080                 _addr[i + 1] = addr[i];
00081                 _len[i + 1] = len[i];
00082         }
00083         md5_vector(1 + num_elem, _addr, _len, mac);
00084 
00085         memset(k_pad, 0, sizeof(k_pad));
00086         memcpy(k_pad, key, key_len);
00087         /* XOR key with opad values */
00088         for (i = 0; i < 64; i++)
00089                 k_pad[i] ^= 0x5c;
00090 
00091         /* perform outer MD5 */
00092         _addr[0] = k_pad;
00093         _len[0] = 64;
00094         _addr[1] = mac;
00095         _len[1] = MD5_MAC_LEN;
00096         md5_vector(2, _addr, _len, mac);
00097 }
00098 
00099 
00109 void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
00110               u8 *mac)
00111 {
00112         hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
00113 }
00114 
00115 
00116 #ifndef EAP_TLS_FUNCS
00117 
00118 struct MD5Context {
00119         u32 buf[4];
00120         u32 bits[2];
00121         u8 in[64];
00122 };
00123 
00124 static void MD5Init(struct MD5Context *context);
00125 static void MD5Update(struct MD5Context *context, unsigned char const *buf,
00126                       unsigned len);
00127 static void MD5Final(unsigned char digest[16], struct MD5Context *context);
00128 static void MD5Transform(u32 buf[4], u32 const in[16]);
00129 
00130 typedef struct MD5Context MD5_CTX;
00131 
00132 
00141 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00142 {
00143         MD5_CTX ctx;
00144         int i;
00145 
00146         MD5Init(&ctx);
00147         for (i = 0; i < num_elem; i++)
00148                 MD5Update(&ctx, addr[i], len[i]);
00149         MD5Final(mac, &ctx);
00150 }
00151 
00152 
00153 /* ===== start - public domain MD5 implementation ===== */
00154 /*
00155  * This code implements the MD5 message-digest algorithm.
00156  * The algorithm is due to Ron Rivest.  This code was
00157  * written by Colin Plumb in 1993, no copyright is claimed.
00158  * This code is in the public domain; do with it what you wish.
00159  *
00160  * Equivalent code is available from RSA Data Security, Inc.
00161  * This code has been tested against that, and is equivalent,
00162  * except that you don't need to include two pages of legalese
00163  * with every copy.
00164  *
00165  * To compute the message digest of a chunk of bytes, declare an
00166  * MD5Context structure, pass it to MD5Init, call MD5Update as
00167  * needed on buffers full of bytes, and then call MD5Final, which
00168  * will fill a supplied 16-byte array with the digest.
00169  */
00170 
00171 #ifndef WORDS_BIGENDIAN
00172 #define byteReverse(buf, len)   /* Nothing */
00173 #else
00174 /*
00175  * Note: this code is harmless on little-endian machines.
00176  */
00177 static void byteReverse(unsigned char *buf, unsigned longs)
00178 {
00179     u32 t;
00180     do {
00181         t = (u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
00182             ((unsigned) buf[1] << 8 | buf[0]);
00183         *(u32 *) buf = t;
00184         buf += 4;
00185     } while (--longs);
00186 }
00187 #endif
00188 
00189 /*
00190  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
00191  * initialization constants.
00192  */
00193 static void MD5Init(struct MD5Context *ctx)
00194 {
00195     ctx->buf[0] = 0x67452301;
00196     ctx->buf[1] = 0xefcdab89;
00197     ctx->buf[2] = 0x98badcfe;
00198     ctx->buf[3] = 0x10325476;
00199 
00200     ctx->bits[0] = 0;
00201     ctx->bits[1] = 0;
00202 }
00203 
00204 /*
00205  * Update context to reflect the concatenation of another buffer full
00206  * of bytes.
00207  */
00208 static void MD5Update(struct MD5Context *ctx, unsigned char const *buf,
00209                       unsigned len)
00210 {
00211     u32 t;
00212 
00213     /* Update bitcount */
00214 
00215     t = ctx->bits[0];
00216     if ((ctx->bits[0] = t + ((u32) len << 3)) < t)
00217         ctx->bits[1]++;         /* Carry from low to high */
00218     ctx->bits[1] += len >> 29;
00219 
00220     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
00221 
00222     /* Handle any leading odd-sized chunks */
00223 
00224     if (t) {
00225         unsigned char *p = (unsigned char *) ctx->in + t;
00226 
00227         t = 64 - t;
00228         if (len < t) {
00229             memcpy(p, buf, len);
00230             return;
00231         }
00232         memcpy(p, buf, t);
00233         byteReverse(ctx->in, 16);
00234         MD5Transform(ctx->buf, (u32 *) ctx->in);
00235         buf += t;
00236         len -= t;
00237     }
00238     /* Process data in 64-byte chunks */
00239 
00240     while (len >= 64) {
00241         memcpy(ctx->in, buf, 64);
00242         byteReverse(ctx->in, 16);
00243         MD5Transform(ctx->buf, (u32 *) ctx->in);
00244         buf += 64;
00245         len -= 64;
00246     }
00247 
00248     /* Handle any remaining bytes of data. */
00249 
00250     memcpy(ctx->in, buf, len);
00251 }
00252 
00253 /*
00254  * Final wrapup - pad to 64-byte boundary with the bit pattern
00255  * 1 0* (64-bit count of bits processed, MSB-first)
00256  */
00257 static void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
00258 {
00259     unsigned count;
00260     unsigned char *p;
00261 
00262     /* Compute number of bytes mod 64 */
00263     count = (ctx->bits[0] >> 3) & 0x3F;
00264 
00265     /* Set the first char of padding to 0x80.  This is safe since there is
00266        always at least one byte free */
00267     p = ctx->in + count;
00268     *p++ = 0x80;
00269 
00270     /* Bytes of padding needed to make 64 bytes */
00271     count = 64 - 1 - count;
00272 
00273     /* Pad out to 56 mod 64 */
00274     if (count < 8) {
00275         /* Two lots of padding:  Pad the first block to 64 bytes */
00276         memset(p, 0, count);
00277         byteReverse(ctx->in, 16);
00278         MD5Transform(ctx->buf, (u32 *) ctx->in);
00279 
00280         /* Now fill the next block with 56 bytes */
00281         memset(ctx->in, 0, 56);
00282     } else {
00283         /* Pad block to 56 bytes */
00284         memset(p, 0, count - 8);
00285     }
00286     byteReverse(ctx->in, 14);
00287 
00288     /* Append length in bits and transform */
00289     ((u32 *) ctx->in)[14] = ctx->bits[0];
00290     ((u32 *) ctx->in)[15] = ctx->bits[1];
00291 
00292     MD5Transform(ctx->buf, (u32 *) ctx->in);
00293     byteReverse((unsigned char *) ctx->buf, 4);
00294     memcpy(digest, ctx->buf, 16);
00295     memset(ctx, 0, sizeof(ctx));        /* In case it's sensitive */
00296 }
00297 
00298 /* The four core functions - F1 is optimized somewhat */
00299 
00300 /* #define F1(x, y, z) (x & y | ~x & z) */
00301 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00302 #define F2(x, y, z) F1(z, x, y)
00303 #define F3(x, y, z) (x ^ y ^ z)
00304 #define F4(x, y, z) (y ^ (x | ~z))
00305 
00306 /* This is the central step in the MD5 algorithm. */
00307 #define MD5STEP(f, w, x, y, z, data, s) \
00308         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
00309 
00310 /*
00311  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00312  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00313  * the data and converts bytes into longwords for this routine.
00314  */
00315 static void MD5Transform(u32 buf[4], u32 const in[16])
00316 {
00317     register u32 a, b, c, d;
00318 
00319     a = buf[0];
00320     b = buf[1];
00321     c = buf[2];
00322     d = buf[3];
00323 
00324     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00325     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00326     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00327     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00328     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00329     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00330     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00331     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00332     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00333     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00334     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00335     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00336     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00337     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00338     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00339     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00340 
00341     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00342     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00343     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00344     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00345     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00346     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00347     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00348     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00349     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00350     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00351     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00352     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00353     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00354     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00355     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00356     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00357 
00358     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00359     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00360     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00361     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00362     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00363     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00364     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00365     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00366     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00367     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00368     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00369     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00370     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00371     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00372     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00373     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00374 
00375     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00376     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00377     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00378     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00379     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00380     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00381     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00382     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00383     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00384     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00385     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00386     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00387     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00388     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00389     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00390     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00391 
00392     buf[0] += a;
00393     buf[1] += b;
00394     buf[2] += c;
00395     buf[3] += d;
00396 }
00397 /* ===== end - public domain MD5 implementation ===== */
00398 
00399 #endif /* !EAP_TLS_FUNCS */
00400 

Generated on Sat May 6 21:13:37 2006 for wpa_supplicant by  doxygen 1.4.2