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