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 
00034 int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
00035                     const u8 *addr[], const size_t *len, u8 *mac)
00036 {
00037         u8 k_pad[64]; /* padding - key XORd with ipad/opad */
00038         u8 tk[16];
00039         const u8 *_addr[6];
00040         size_t i, _len[6];
00041 
00042         if (num_elem > 5) {
00043                 /*
00044                  * Fixed limit on the number of fragments to avoid having to
00045                  * allocate memory (which could fail).
00046                  */
00047                 return -1;
00048         }
00049 
00050         /* if key is longer than 64 bytes reset it to key = MD5(key) */
00051         if (key_len > 64) {
00052                 if (md5_vector(1, &key, &key_len, tk))
00053                         return -1;
00054                 key = tk;
00055                 key_len = 16;
00056         }
00057 
00058         /* the HMAC_MD5 transform looks like:
00059          *
00060          * MD5(K XOR opad, MD5(K XOR ipad, text))
00061          *
00062          * where K is an n byte key
00063          * ipad is the byte 0x36 repeated 64 times
00064          * opad is the byte 0x5c repeated 64 times
00065          * and text is the data being protected */
00066 
00067         /* start out by storing key in ipad */
00068         os_memset(k_pad, 0, sizeof(k_pad));
00069         os_memcpy(k_pad, key, key_len);
00070 
00071         /* XOR key with ipad values */
00072         for (i = 0; i < 64; i++)
00073                 k_pad[i] ^= 0x36;
00074 
00075         /* perform inner MD5 */
00076         _addr[0] = k_pad;
00077         _len[0] = 64;
00078         for (i = 0; i < num_elem; i++) {
00079                 _addr[i + 1] = addr[i];
00080                 _len[i + 1] = len[i];
00081         }
00082         if (md5_vector(1 + num_elem, _addr, _len, mac))
00083                 return -1;
00084 
00085         os_memset(k_pad, 0, sizeof(k_pad));
00086         os_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         return md5_vector(2, _addr, _len, mac);
00097 }
00098 
00099 
00110 int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
00111               u8 *mac)
00112 {
00113         return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
00114 }
00115 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:50 2009 for hostapd by  doxygen 1.6.1