1189251Ssam/*
2189251Ssam * MD5 hash implementation and interface functions
3189251Ssam * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#include "includes.h"
10189251Ssam
11189251Ssam#include "common.h"
12189251Ssam#include "md5.h"
13189251Ssam#include "crypto.h"
14189251Ssam
15189251Ssam
16189251Ssam/**
17189251Ssam * hmac_md5_vector - HMAC-MD5 over data vector (RFC 2104)
18189251Ssam * @key: Key for HMAC operations
19189251Ssam * @key_len: Length of the key in bytes
20189251Ssam * @num_elem: Number of elements in the data vector
21189251Ssam * @addr: Pointers to the data areas
22189251Ssam * @len: Lengths of the data blocks
23189251Ssam * @mac: Buffer for the hash (16 bytes)
24214734Srpaulo * Returns: 0 on success, -1 on failure
25189251Ssam */
26214734Srpauloint hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
27214734Srpaulo		    const u8 *addr[], const size_t *len, u8 *mac)
28189251Ssam{
29189251Ssam	u8 k_pad[64]; /* padding - key XORd with ipad/opad */
30189251Ssam	u8 tk[16];
31189251Ssam	const u8 *_addr[6];
32189251Ssam	size_t i, _len[6];
33281806Srpaulo	int res;
34189251Ssam
35189251Ssam	if (num_elem > 5) {
36189251Ssam		/*
37189251Ssam		 * Fixed limit on the number of fragments to avoid having to
38189251Ssam		 * allocate memory (which could fail).
39189251Ssam		 */
40214734Srpaulo		return -1;
41189251Ssam	}
42189251Ssam
43189251Ssam        /* if key is longer than 64 bytes reset it to key = MD5(key) */
44189251Ssam        if (key_len > 64) {
45214734Srpaulo		if (md5_vector(1, &key, &key_len, tk))
46214734Srpaulo			return -1;
47189251Ssam		key = tk;
48189251Ssam		key_len = 16;
49189251Ssam        }
50189251Ssam
51189251Ssam	/* the HMAC_MD5 transform looks like:
52189251Ssam	 *
53189251Ssam	 * MD5(K XOR opad, MD5(K XOR ipad, text))
54189251Ssam	 *
55189251Ssam	 * where K is an n byte key
56189251Ssam	 * ipad is the byte 0x36 repeated 64 times
57189251Ssam	 * opad is the byte 0x5c repeated 64 times
58189251Ssam	 * and text is the data being protected */
59189251Ssam
60189251Ssam	/* start out by storing key in ipad */
61189251Ssam	os_memset(k_pad, 0, sizeof(k_pad));
62189251Ssam	os_memcpy(k_pad, key, key_len);
63189251Ssam
64189251Ssam	/* XOR key with ipad values */
65189251Ssam	for (i = 0; i < 64; i++)
66189251Ssam		k_pad[i] ^= 0x36;
67189251Ssam
68189251Ssam	/* perform inner MD5 */
69189251Ssam	_addr[0] = k_pad;
70189251Ssam	_len[0] = 64;
71189251Ssam	for (i = 0; i < num_elem; i++) {
72189251Ssam		_addr[i + 1] = addr[i];
73189251Ssam		_len[i + 1] = len[i];
74189251Ssam	}
75214734Srpaulo	if (md5_vector(1 + num_elem, _addr, _len, mac))
76214734Srpaulo		return -1;
77189251Ssam
78189251Ssam	os_memset(k_pad, 0, sizeof(k_pad));
79189251Ssam	os_memcpy(k_pad, key, key_len);
80189251Ssam	/* XOR key with opad values */
81189251Ssam	for (i = 0; i < 64; i++)
82189251Ssam		k_pad[i] ^= 0x5c;
83189251Ssam
84189251Ssam	/* perform outer MD5 */
85189251Ssam	_addr[0] = k_pad;
86189251Ssam	_len[0] = 64;
87189251Ssam	_addr[1] = mac;
88189251Ssam	_len[1] = MD5_MAC_LEN;
89281806Srpaulo	res = md5_vector(2, _addr, _len, mac);
90281806Srpaulo	os_memset(k_pad, 0, sizeof(k_pad));
91281806Srpaulo	os_memset(tk, 0, sizeof(tk));
92281806Srpaulo	return res;
93189251Ssam}
94189251Ssam
95189251Ssam
96189251Ssam/**
97189251Ssam * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
98189251Ssam * @key: Key for HMAC operations
99189251Ssam * @key_len: Length of the key in bytes
100189251Ssam * @data: Pointers to the data area
101189251Ssam * @data_len: Length of the data area
102189251Ssam * @mac: Buffer for the hash (16 bytes)
103214734Srpaulo * Returns: 0 on success, -1 on failure
104189251Ssam */
105214734Srpauloint hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
106189251Ssam	      u8 *mac)
107189251Ssam{
108214734Srpaulo	return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
109189251Ssam}
110