]>
git.ipfire.org Git - thirdparty/hostap.git/blob - src/crypto/sha1.c
2 * SHA1 hash implementation and interface functions
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 * hmac_sha1_vector - HMAC-SHA1 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 (20 bytes)
24 * Returns: 0 on success, -1 on failure
26 int hmac_sha1_vector(const u8
*key
, size_t key_len
, size_t num_elem
,
27 const u8
*addr
[], const size_t *len
, u8
*mac
)
29 unsigned char k_pad
[64]; /* padding - key XORd with ipad/opad */
37 * Fixed limit on the number of fragments to avoid having to
38 * allocate memory (which could fail).
43 /* if key is longer than 64 bytes reset it to key = SHA1(key) */
45 if (sha1_vector(1, &key
, &key_len
, tk
))
51 /* the HMAC_SHA1 transform looks like:
53 * SHA1(K XOR opad, SHA1(K XOR ipad, text))
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 */
60 /* start out by storing key in ipad */
61 os_memset(k_pad
, 0, sizeof(k_pad
));
62 os_memcpy(k_pad
, key
, key_len
);
63 /* XOR key with ipad values */
64 for (i
= 0; i
< 64; i
++)
67 /* perform inner SHA1 */
70 for (i
= 0; i
< num_elem
; i
++) {
71 _addr
[i
+ 1] = addr
[i
];
74 if (sha1_vector(1 + num_elem
, _addr
, _len
, mac
))
77 os_memset(k_pad
, 0, sizeof(k_pad
));
78 os_memcpy(k_pad
, key
, key_len
);
79 /* XOR key with opad values */
80 for (i
= 0; i
< 64; i
++)
83 /* perform outer SHA1 */
87 _len
[1] = SHA1_MAC_LEN
;
88 ret
= sha1_vector(2, _addr
, _len
, mac
);
89 forced_memzero(k_pad
, sizeof(k_pad
));
90 forced_memzero(tk
, sizeof(tk
));
96 * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
97 * @key: Key for HMAC operations
98 * @key_len: Length of the key in bytes
99 * @data: Pointers to the data area
100 * @data_len: Length of the data area
101 * @mac: Buffer for the hash (20 bytes)
102 * Returns: 0 on success, -1 of failure
104 int hmac_sha1(const u8
*key
, size_t key_len
, const u8
*data
, size_t data_len
,
107 return hmac_sha1_vector(key
, key_len
, 1, &data
, &data_len
, mac
);