2 * SHA1 hash implementation and interface functions
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
23 * hmac_sha1_vector - HMAC-SHA1 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 (20 bytes)
30 * Returns: 0 on success, -1 on failure
32 int hmac_sha1_vector(const u8
*key
, size_t key_len
, size_t num_elem
,
33 const u8
*addr
[], const size_t *len
, u8
*mac
)
35 unsigned char k_pad
[64]; /* padding - key XORd with ipad/opad */
42 * Fixed limit on the number of fragments to avoid having to
43 * allocate memory (which could fail).
48 /* if key is longer than 64 bytes reset it to key = SHA1(key) */
50 if (sha1_vector(1, &key
, &key_len
, tk
))
56 /* the HMAC_SHA1 transform looks like:
58 * SHA1(K XOR opad, SHA1(K XOR ipad, text))
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 */
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 /* XOR key with ipad values */
69 for (i
= 0; i
< 64; i
++)
72 /* perform inner SHA1 */
75 for (i
= 0; i
< num_elem
; i
++) {
76 _addr
[i
+ 1] = addr
[i
];
79 if (sha1_vector(1 + num_elem
, _addr
, _len
, mac
))
82 os_memset(k_pad
, 0, sizeof(k_pad
));
83 os_memcpy(k_pad
, key
, key_len
);
84 /* XOR key with opad values */
85 for (i
= 0; i
< 64; i
++)
88 /* perform outer SHA1 */
92 _len
[1] = SHA1_MAC_LEN
;
93 return sha1_vector(2, _addr
, _len
, mac
);
98 * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
99 * @key: Key for HMAC operations
100 * @key_len: Length of the key in bytes
101 * @data: Pointers to the data area
102 * @data_len: Length of the data area
103 * @mac: Buffer for the hash (20 bytes)
104 * Returns: 0 on success, -1 of failure
106 int hmac_sha1(const u8
*key
, size_t key_len
, const u8
*data
, size_t data_len
,
109 return hmac_sha1_vector(key
, key_len
, 1, &data
, &data_len
, mac
);
114 * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
116 * @key_len: Length of the key in bytes
117 * @label: A unique label for each purpose of the PRF
118 * @data: Extra data to bind into the key
119 * @data_len: Length of the data
120 * @buf: Buffer for the generated pseudo-random key
121 * @buf_len: Number of bytes of key to generate
122 * Returns: 0 on success, -1 of failure
124 * This function is used to derive new, cryptographically separate keys from a
125 * given key (e.g., PMK in IEEE 802.11i).
127 int sha1_prf(const u8
*key
, size_t key_len
, const char *label
,
128 const u8
*data
, size_t data_len
, u8
*buf
, size_t buf_len
)
132 u8 hash
[SHA1_MAC_LEN
];
133 size_t label_len
= os_strlen(label
) + 1;
134 const unsigned char *addr
[3];
137 addr
[0] = (u8
*) label
;
145 while (pos
< buf_len
) {
146 plen
= buf_len
- pos
;
147 if (plen
>= SHA1_MAC_LEN
) {
148 if (hmac_sha1_vector(key
, key_len
, 3, addr
, len
,
153 if (hmac_sha1_vector(key
, key_len
, 3, addr
, len
,
156 os_memcpy(&buf
[pos
], hash
, plen
);