1189251Ssam/*
2189251Ssam * MD5 hash implementation and interface functions
3189251Ssam * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4189251Ssam *
5189251Ssam * This program is free software; you can redistribute it and/or modify
6189251Ssam * it under the terms of the GNU General Public License version 2 as
7189251Ssam * published by the Free Software Foundation.
8189251Ssam *
9189251Ssam * Alternatively, this software may be distributed under the terms of BSD
10189251Ssam * license.
11189251Ssam *
12189251Ssam * See README and COPYING for more details.
13189251Ssam */
14189251Ssam
15189251Ssam#include "includes.h"
16189251Ssam
17189251Ssam#include "common.h"
18189251Ssam#include "md5.h"
19189251Ssam#include "crypto.h"
20189251Ssam
21189251Ssam
22189251Ssam/**
23189251Ssam * hmac_md5_vector - HMAC-MD5 over data vector (RFC 2104)
24189251Ssam * @key: Key for HMAC operations
25189251Ssam * @key_len: Length of the key in bytes
26189251Ssam * @num_elem: Number of elements in the data vector
27189251Ssam * @addr: Pointers to the data areas
28189251Ssam * @len: Lengths of the data blocks
29189251Ssam * @mac: Buffer for the hash (16 bytes)
30214734Srpaulo * Returns: 0 on success, -1 on failure
31189251Ssam */
32214734Srpauloint hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
33214734Srpaulo		    const u8 *addr[], const size_t *len, u8 *mac)
34189251Ssam{
35189251Ssam	u8 k_pad[64]; /* padding - key XORd with ipad/opad */
36189251Ssam	u8 tk[16];
37189251Ssam	const u8 *_addr[6];
38189251Ssam	size_t i, _len[6];
39189251Ssam
40189251Ssam	if (num_elem > 5) {
41189251Ssam		/*
42189251Ssam		 * Fixed limit on the number of fragments to avoid having to
43189251Ssam		 * allocate memory (which could fail).
44189251Ssam		 */
45214734Srpaulo		return -1;
46189251Ssam	}
47189251Ssam
48189251Ssam        /* if key is longer than 64 bytes reset it to key = MD5(key) */
49189251Ssam        if (key_len > 64) {
50214734Srpaulo		if (md5_vector(1, &key, &key_len, tk))
51214734Srpaulo			return -1;
52189251Ssam		key = tk;
53189251Ssam		key_len = 16;
54189251Ssam        }
55189251Ssam
56189251Ssam	/* the HMAC_MD5 transform looks like:
57189251Ssam	 *
58189251Ssam	 * MD5(K XOR opad, MD5(K XOR ipad, text))
59189251Ssam	 *
60189251Ssam	 * where K is an n byte key
61189251Ssam	 * ipad is the byte 0x36 repeated 64 times
62189251Ssam	 * opad is the byte 0x5c repeated 64 times
63189251Ssam	 * and text is the data being protected */
64189251Ssam
65189251Ssam	/* start out by storing key in ipad */
66189251Ssam	os_memset(k_pad, 0, sizeof(k_pad));
67189251Ssam	os_memcpy(k_pad, key, key_len);
68189251Ssam
69189251Ssam	/* XOR key with ipad values */
70189251Ssam	for (i = 0; i < 64; i++)
71189251Ssam		k_pad[i] ^= 0x36;
72189251Ssam
73189251Ssam	/* perform inner MD5 */
74189251Ssam	_addr[0] = k_pad;
75189251Ssam	_len[0] = 64;
76189251Ssam	for (i = 0; i < num_elem; i++) {
77189251Ssam		_addr[i + 1] = addr[i];
78189251Ssam		_len[i + 1] = len[i];
79189251Ssam	}
80214734Srpaulo	if (md5_vector(1 + num_elem, _addr, _len, mac))
81214734Srpaulo		return -1;
82189251Ssam
83189251Ssam	os_memset(k_pad, 0, sizeof(k_pad));
84189251Ssam	os_memcpy(k_pad, key, key_len);
85189251Ssam	/* XOR key with opad values */
86189251Ssam	for (i = 0; i < 64; i++)
87189251Ssam		k_pad[i] ^= 0x5c;
88189251Ssam
89189251Ssam	/* perform outer MD5 */
90189251Ssam	_addr[0] = k_pad;
91189251Ssam	_len[0] = 64;
92189251Ssam	_addr[1] = mac;
93189251Ssam	_len[1] = MD5_MAC_LEN;
94214734Srpaulo	return md5_vector(2, _addr, _len, mac);
95189251Ssam}
96189251Ssam
97189251Ssam
98189251Ssam/**
99189251Ssam * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
100189251Ssam * @key: Key for HMAC operations
101189251Ssam * @key_len: Length of the key in bytes
102189251Ssam * @data: Pointers to the data area
103189251Ssam * @data_len: Length of the data area
104189251Ssam * @mac: Buffer for the hash (16 bytes)
105214734Srpaulo * Returns: 0 on success, -1 on failure
106189251Ssam */
107214734Srpauloint hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
108189251Ssam	      u8 *mac)
109189251Ssam{
110214734Srpaulo	return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
111189251Ssam}
112