md5.c
Go to the documentation of this file.
00001 /*
00002  * MD5 hash implementation and interface functions
00003  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 
00017 #include "common.h"
00018 #include "md5.h"
00019 #include "crypto.h"
00020 
00021 
00032 int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
00033                     const u8 *addr[], const size_t *len, u8 *mac)
00034 {
00035         u8 k_pad[64]; /* padding - key XORd with ipad/opad */
00036         u8 tk[16];
00037         const u8 *_addr[6];
00038         size_t i, _len[6];
00039 
00040         if (num_elem > 5) {
00041                 /*
00042                  * Fixed limit on the number of fragments to avoid having to
00043                  * allocate memory (which could fail).
00044                  */
00045                 return -1;
00046         }
00047 
00048         /* if key is longer than 64 bytes reset it to key = MD5(key) */
00049         if (key_len > 64) {
00050                 if (md5_vector(1, &key, &key_len, tk))
00051                         return -1;
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         if (md5_vector(1 + num_elem, _addr, _len, mac))
00081                 return -1;
00082 
00083         os_memset(k_pad, 0, sizeof(k_pad));
00084         os_memcpy(k_pad, key, key_len);
00085         /* XOR key with opad values */
00086         for (i = 0; i < 64; i++)
00087                 k_pad[i] ^= 0x5c;
00088 
00089         /* perform outer MD5 */
00090         _addr[0] = k_pad;
00091         _len[0] = 64;
00092         _addr[1] = mac;
00093         _len[1] = MD5_MAC_LEN;
00094         return md5_vector(2, _addr, _len, mac);
00095 }
00096 
00097 
00107 int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
00108               u8 *mac)
00109 {
00110         return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
00111 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:25:14