blob: 7f14e9b2ec16beb2fc7a1dc76d414e4444da0db2 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * MD5 hash implementation and interface functions
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "md5.h"
19#include "crypto.h"
20
21
22/**
23 * hmac_md5_vector - HMAC-MD5 over data vector (RFC 2104)
24 * @key: Key for HMAC operations
25 * @key_len: Length of the key in bytes
26 * @num_elem: Number of elements in the data vector
27 * @addr: Pointers to the data areas
28 * @len: Lengths of the data blocks
29 * @mac: Buffer for the hash (16 bytes)
30 * Returns: 0 on success, -1 on failure
31 */
32int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
33 const u8 *addr[], const size_t *len, u8 *mac)
34{
35 u8 k_pad[64]; /* padding - key XORd with ipad/opad */
36 u8 tk[16];
37 const u8 *_addr[6];
38 size_t i, _len[6];
39
40 if (num_elem > 5) {
41 /*
42 * Fixed limit on the number of fragments to avoid having to
43 * allocate memory (which could fail).
44 */
45 return -1;
46 }
47
48 /* if key is longer than 64 bytes reset it to key = MD5(key) */
49 if (key_len > 64) {
50 if (md5_vector(1, &key, &key_len, tk))
51 return -1;
52 key = tk;
53 key_len = 16;
54 }
55
56 /* the HMAC_MD5 transform looks like:
57 *
58 * MD5(K XOR opad, MD5(K XOR ipad, text))
59 *
60 * where K is an n byte key
61 * ipad is the byte 0x36 repeated 64 times
62 * opad is the byte 0x5c repeated 64 times
63 * and text is the data being protected */
64
65 /* start out by storing key in ipad */
66 os_memset(k_pad, 0, sizeof(k_pad));
67 os_memcpy(k_pad, key, key_len);
68
69 /* XOR key with ipad values */
70 for (i = 0; i < 64; i++)
71 k_pad[i] ^= 0x36;
72
73 /* perform inner MD5 */
74 _addr[0] = k_pad;
75 _len[0] = 64;
76 for (i = 0; i < num_elem; i++) {
77 _addr[i + 1] = addr[i];
78 _len[i + 1] = len[i];
79 }
80 if (md5_vector(1 + num_elem, _addr, _len, mac))
81 return -1;
82
83 os_memset(k_pad, 0, sizeof(k_pad));
84 os_memcpy(k_pad, key, key_len);
85 /* XOR key with opad values */
86 for (i = 0; i < 64; i++)
87 k_pad[i] ^= 0x5c;
88
89 /* perform outer MD5 */
90 _addr[0] = k_pad;
91 _len[0] = 64;
92 _addr[1] = mac;
93 _len[1] = MD5_MAC_LEN;
94 return md5_vector(2, _addr, _len, mac);
95}
96
97
98/**
99 * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
100 * @key: Key for HMAC operations
101 * @key_len: Length of the key in bytes
102 * @data: Pointers to the data area
103 * @data_len: Length of the data area
104 * @mac: Buffer for the hash (16 bytes)
105 * Returns: 0 on success, -1 on failure
106 */
107int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
108 u8 *mac)
109{
110 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
111}