16d49e1aeSJan Lentfer /*
26d49e1aeSJan Lentfer  * SHA1 hash implementation and interface functions
36d49e1aeSJan Lentfer  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
46d49e1aeSJan Lentfer  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
76d49e1aeSJan Lentfer  */
86d49e1aeSJan Lentfer 
96d49e1aeSJan Lentfer #include "includes.h"
106d49e1aeSJan Lentfer 
116d49e1aeSJan Lentfer #include "common.h"
126d49e1aeSJan Lentfer #include "sha1.h"
136d49e1aeSJan Lentfer #include "crypto.h"
146d49e1aeSJan Lentfer 
156d49e1aeSJan Lentfer 
166d49e1aeSJan Lentfer /**
176d49e1aeSJan Lentfer  * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104)
186d49e1aeSJan Lentfer  * @key: Key for HMAC operations
196d49e1aeSJan Lentfer  * @key_len: Length of the key in bytes
206d49e1aeSJan Lentfer  * @num_elem: Number of elements in the data vector
216d49e1aeSJan Lentfer  * @addr: Pointers to the data areas
226d49e1aeSJan Lentfer  * @len: Lengths of the data blocks
236d49e1aeSJan Lentfer  * @mac: Buffer for the hash (20 bytes)
243ff40c12SJohn Marino  * Returns: 0 on success, -1 on failure
256d49e1aeSJan Lentfer  */
hmac_sha1_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)263ff40c12SJohn Marino int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
276d49e1aeSJan Lentfer 		     const u8 *addr[], const size_t *len, u8 *mac)
286d49e1aeSJan Lentfer {
296d49e1aeSJan Lentfer 	unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
306d49e1aeSJan Lentfer 	unsigned char tk[20];
316d49e1aeSJan Lentfer 	const u8 *_addr[6];
326d49e1aeSJan Lentfer 	size_t _len[6], i;
33*a1157835SDaniel Fojt 	int ret;
346d49e1aeSJan Lentfer 
356d49e1aeSJan Lentfer 	if (num_elem > 5) {
366d49e1aeSJan Lentfer 		/*
376d49e1aeSJan Lentfer 		 * Fixed limit on the number of fragments to avoid having to
386d49e1aeSJan Lentfer 		 * allocate memory (which could fail).
396d49e1aeSJan Lentfer 		 */
403ff40c12SJohn Marino 		return -1;
416d49e1aeSJan Lentfer 	}
426d49e1aeSJan Lentfer 
436d49e1aeSJan Lentfer         /* if key is longer than 64 bytes reset it to key = SHA1(key) */
446d49e1aeSJan Lentfer         if (key_len > 64) {
453ff40c12SJohn Marino 		if (sha1_vector(1, &key, &key_len, tk))
463ff40c12SJohn Marino 			return -1;
476d49e1aeSJan Lentfer 		key = tk;
486d49e1aeSJan Lentfer 		key_len = 20;
496d49e1aeSJan Lentfer         }
506d49e1aeSJan Lentfer 
516d49e1aeSJan Lentfer 	/* the HMAC_SHA1 transform looks like:
526d49e1aeSJan Lentfer 	 *
536d49e1aeSJan Lentfer 	 * SHA1(K XOR opad, SHA1(K XOR ipad, text))
546d49e1aeSJan Lentfer 	 *
556d49e1aeSJan Lentfer 	 * where K is an n byte key
566d49e1aeSJan Lentfer 	 * ipad is the byte 0x36 repeated 64 times
576d49e1aeSJan Lentfer 	 * opad is the byte 0x5c repeated 64 times
586d49e1aeSJan Lentfer 	 * and text is the data being protected */
596d49e1aeSJan Lentfer 
606d49e1aeSJan Lentfer 	/* start out by storing key in ipad */
616d49e1aeSJan Lentfer 	os_memset(k_pad, 0, sizeof(k_pad));
626d49e1aeSJan Lentfer 	os_memcpy(k_pad, key, key_len);
636d49e1aeSJan Lentfer 	/* XOR key with ipad values */
646d49e1aeSJan Lentfer 	for (i = 0; i < 64; i++)
656d49e1aeSJan Lentfer 		k_pad[i] ^= 0x36;
666d49e1aeSJan Lentfer 
676d49e1aeSJan Lentfer 	/* perform inner SHA1 */
686d49e1aeSJan Lentfer 	_addr[0] = k_pad;
696d49e1aeSJan Lentfer 	_len[0] = 64;
706d49e1aeSJan Lentfer 	for (i = 0; i < num_elem; i++) {
716d49e1aeSJan Lentfer 		_addr[i + 1] = addr[i];
726d49e1aeSJan Lentfer 		_len[i + 1] = len[i];
736d49e1aeSJan Lentfer 	}
743ff40c12SJohn Marino 	if (sha1_vector(1 + num_elem, _addr, _len, mac))
753ff40c12SJohn Marino 		return -1;
766d49e1aeSJan Lentfer 
776d49e1aeSJan Lentfer 	os_memset(k_pad, 0, sizeof(k_pad));
786d49e1aeSJan Lentfer 	os_memcpy(k_pad, key, key_len);
796d49e1aeSJan Lentfer 	/* XOR key with opad values */
806d49e1aeSJan Lentfer 	for (i = 0; i < 64; i++)
816d49e1aeSJan Lentfer 		k_pad[i] ^= 0x5c;
826d49e1aeSJan Lentfer 
836d49e1aeSJan Lentfer 	/* perform outer SHA1 */
846d49e1aeSJan Lentfer 	_addr[0] = k_pad;
856d49e1aeSJan Lentfer 	_len[0] = 64;
866d49e1aeSJan Lentfer 	_addr[1] = mac;
876d49e1aeSJan Lentfer 	_len[1] = SHA1_MAC_LEN;
88*a1157835SDaniel Fojt 	ret = sha1_vector(2, _addr, _len, mac);
89*a1157835SDaniel Fojt 	forced_memzero(k_pad, sizeof(k_pad));
90*a1157835SDaniel Fojt 	forced_memzero(tk, sizeof(tk));
91*a1157835SDaniel Fojt 	return ret;
926d49e1aeSJan Lentfer }
936d49e1aeSJan Lentfer 
946d49e1aeSJan Lentfer 
956d49e1aeSJan Lentfer /**
966d49e1aeSJan Lentfer  * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
976d49e1aeSJan Lentfer  * @key: Key for HMAC operations
986d49e1aeSJan Lentfer  * @key_len: Length of the key in bytes
996d49e1aeSJan Lentfer  * @data: Pointers to the data area
1006d49e1aeSJan Lentfer  * @data_len: Length of the data area
1016d49e1aeSJan Lentfer  * @mac: Buffer for the hash (20 bytes)
1023ff40c12SJohn Marino  * Returns: 0 on success, -1 of failure
1036d49e1aeSJan Lentfer  */
hmac_sha1(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1043ff40c12SJohn Marino int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1056d49e1aeSJan Lentfer 	       u8 *mac)
1066d49e1aeSJan Lentfer {
1073ff40c12SJohn Marino 	return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1086d49e1aeSJan Lentfer }
109