md5.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "md5.h"
00020 #include "crypto.h"
00021 
00022 
00033 void hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
00034                      const u8 *addr[], const size_t *len, u8 *mac)
00035 {
00036         u8 k_pad[64]; /* padding - key XORd with ipad/opad */
00037         u8 tk[16];
00038         const u8 *_addr[6];
00039         size_t i, _len[6];
00040 
00041         if (num_elem > 5) {
00042                 /*
00043                  * Fixed limit on the number of fragments to avoid having to
00044                  * allocate memory (which could fail).
00045                  */
00046                 return;
00047         }
00048 
00049         /* if key is longer than 64 bytes reset it to key = MD5(key) */
00050         if (key_len > 64) {
00051                 md5_vector(1, &key, &key_len, tk);
00052                 key = tk;
00053                 key_len = 16;
00054         }
00055 
00056         /* the HMAC_MD5 transform looks like:
00057          *
00058          * MD5(K XOR opad, MD5(K XOR ipad, text))
00059          *
00060          * where K is an n byte key
00061          * ipad is the byte 0x36 repeated 64 times
00062          * opad is the byte 0x5c repeated 64 times
00063          * and text is the data being protected */
00064 
00065         /* start out by storing key in ipad */
00066         os_memset(k_pad, 0, sizeof(k_pad));
00067         os_memcpy(k_pad, key, key_len);
00068 
00069         /* XOR key with ipad values */
00070         for (i = 0; i < 64; i++)
00071                 k_pad[i] ^= 0x36;
00072 
00073         /* perform inner MD5 */
00074         _addr[0] = k_pad;
00075         _len[0] = 64;
00076         for (i = 0; i < num_elem; i++) {
00077                 _addr[i + 1] = addr[i];
00078                 _len[i + 1] = len[i];
00079         }
00080         md5_vector(1 + num_elem, _addr, _len, mac);
00081 
00082         os_memset(k_pad, 0, sizeof(k_pad));
00083         os_memcpy(k_pad, key, key_len);
00084         /* XOR key with opad values */
00085         for (i = 0; i < 64; i++)
00086                 k_pad[i] ^= 0x5c;
00087 
00088         /* perform outer MD5 */
00089         _addr[0] = k_pad;
00090         _len[0] = 64;
00091         _addr[1] = mac;
00092         _len[1] = MD5_MAC_LEN;
00093         md5_vector(2, _addr, _len, mac);
00094 }
00095 
00096 
00106 void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
00107               u8 *mac)
00108 {
00109         hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
00110 }
00111 
00112 
00113 #ifdef INTERNAL_MD5
00114 
00115 struct MD5Context {
00116         u32 buf[4];
00117         u32 bits[2];
00118         u8 in[64];
00119 };
00120 
00121 #ifndef CONFIG_CRYPTO_INTERNAL
00122 static void MD5Init(struct MD5Context *context);
00123 static void MD5Update(struct MD5Context *context, unsigned char const *buf,
00124                           unsigned len);
00125 static void MD5Final(unsigned char digest[16], struct MD5Context *context);
00126 #endif /* CONFIG_CRYPTO_INTERNAL */
00127 static void MD5Transform(u32 buf[4], u32 const in[16]);
00128 
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         size_t 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 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 void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
00209 {
00210     u32 t;
00211 
00212     /* Update bitcount */
00213 
00214     t = ctx->bits[0];
00215     if ((ctx->bits[0] = t + ((u32) len << 3)) < t)
00216         ctx->bits[1]++;         /* Carry from low to high */
00217     ctx->bits[1] += len >> 29;
00218 
00219     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
00220 
00221     /* Handle any leading odd-sized chunks */
00222 
00223     if (t) {
00224         unsigned char *p = (unsigned char *) ctx->in + t;
00225 
00226         t = 64 - t;
00227         if (len < t) {
00228             os_memcpy(p, buf, len);
00229             return;
00230         }
00231         os_memcpy(p, buf, t);
00232         byteReverse(ctx->in, 16);
00233         MD5Transform(ctx->buf, (u32 *) ctx->in);
00234         buf += t;
00235         len -= t;
00236     }
00237     /* Process data in 64-byte chunks */
00238 
00239     while (len >= 64) {
00240         os_memcpy(ctx->in, buf, 64);
00241         byteReverse(ctx->in, 16);
00242         MD5Transform(ctx->buf, (u32 *) ctx->in);
00243         buf += 64;
00244         len -= 64;
00245     }
00246 
00247     /* Handle any remaining bytes of data. */
00248 
00249     os_memcpy(ctx->in, buf, len);
00250 }
00251 
00252 /*
00253  * Final wrapup - pad to 64-byte boundary with the bit pattern
00254  * 1 0* (64-bit count of bits processed, MSB-first)
00255  */
00256 void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
00257 {
00258     unsigned count;
00259     unsigned char *p;
00260 
00261     /* Compute number of bytes mod 64 */
00262     count = (ctx->bits[0] >> 3) & 0x3F;
00263 
00264     /* Set the first char of padding to 0x80.  This is safe since there is
00265        always at least one byte free */
00266     p = ctx->in + count;
00267     *p++ = 0x80;
00268 
00269     /* Bytes of padding needed to make 64 bytes */
00270     count = 64 - 1 - count;
00271 
00272     /* Pad out to 56 mod 64 */
00273     if (count < 8) {
00274         /* Two lots of padding:  Pad the first block to 64 bytes */
00275         os_memset(p, 0, count);
00276         byteReverse(ctx->in, 16);
00277         MD5Transform(ctx->buf, (u32 *) ctx->in);
00278 
00279         /* Now fill the next block with 56 bytes */
00280         os_memset(ctx->in, 0, 56);
00281     } else {
00282         /* Pad block to 56 bytes */
00283         os_memset(p, 0, count - 8);
00284     }
00285     byteReverse(ctx->in, 14);
00286 
00287     /* Append length in bits and transform */
00288     ((u32 *) ctx->in)[14] = ctx->bits[0];
00289     ((u32 *) ctx->in)[15] = ctx->bits[1];
00290 
00291     MD5Transform(ctx->buf, (u32 *) ctx->in);
00292     byteReverse((unsigned char *) ctx->buf, 4);
00293     os_memcpy(digest, ctx->buf, 16);
00294     os_memset(ctx, 0, sizeof(ctx));     /* In case it's sensitive */
00295 }
00296 
00297 /* The four core functions - F1 is optimized somewhat */
00298 
00299 /* #define F1(x, y, z) (x & y | ~x & z) */
00300 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00301 #define F2(x, y, z) F1(z, x, y)
00302 #define F3(x, y, z) (x ^ y ^ z)
00303 #define F4(x, y, z) (y ^ (x | ~z))
00304 
00305 /* This is the central step in the MD5 algorithm. */
00306 #define MD5STEP(f, w, x, y, z, data, s) \
00307         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
00308 
00309 /*
00310  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00311  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00312  * the data and converts bytes into longwords for this routine.
00313  */
00314 static void MD5Transform(u32 buf[4], u32 const in[16])
00315 {
00316     register u32 a, b, c, d;
00317 
00318     a = buf[0];
00319     b = buf[1];
00320     c = buf[2];
00321     d = buf[3];
00322 
00323     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00324     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00325     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00326     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00327     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00328     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00329     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00330     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00331     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00332     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00333     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00334     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00335     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00336     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00337     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00338     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00339 
00340     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00341     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00342     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00343     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00344     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00345     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00346     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00347     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00348     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00349     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00350     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00351     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00352     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00353     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00354     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00355     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00356 
00357     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00358     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00359     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00360     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00361     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00362     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00363     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00364     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00365     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00366     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00367     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00368     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00369     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00370     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00371     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00372     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00373 
00374     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00375     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00376     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00377     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00378     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00379     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00380     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00381     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00382     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00383     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00384     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00385     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00386     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00387     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00388     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00389     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00390 
00391     buf[0] += a;
00392     buf[1] += b;
00393     buf[2] += c;
00394     buf[3] += d;
00395 }
00396 /* ===== end - public domain MD5 implementation ===== */
00397 
00398 #endif /* INTERNAL_MD5 */
00399 

Generated on Sun Dec 31 13:43:28 2006 for hostapd by  doxygen 1.4.2