1189251Ssam/* 2189251Ssam * SHA1 hash implementation and interface functions 3189251Ssam * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> 4189251Ssam * 5252726Srpaulo * This software may be distributed under the terms of the BSD license. 6252726Srpaulo * See README for more details. 7189251Ssam */ 8189251Ssam 9189251Ssam#include "includes.h" 10189251Ssam 11189251Ssam#include "common.h" 12189251Ssam#include "sha1.h" 13189251Ssam#include "crypto.h" 14189251Ssam 15189251Ssam 16189251Ssam/** 17189251Ssam * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104) 18189251Ssam * @key: Key for HMAC operations 19189251Ssam * @key_len: Length of the key in bytes 20189251Ssam * @num_elem: Number of elements in the data vector 21189251Ssam * @addr: Pointers to the data areas 22189251Ssam * @len: Lengths of the data blocks 23189251Ssam * @mac: Buffer for the hash (20 bytes) 24214734Srpaulo * Returns: 0 on success, -1 on failure 25189251Ssam */ 26214734Srpauloint hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, 27214734Srpaulo const u8 *addr[], const size_t *len, u8 *mac) 28189251Ssam{ 29189251Ssam unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */ 30189251Ssam unsigned char tk[20]; 31189251Ssam const u8 *_addr[6]; 32189251Ssam size_t _len[6], i; 33281806Srpaulo int ret; 34189251Ssam 35189251Ssam if (num_elem > 5) { 36189251Ssam /* 37189251Ssam * Fixed limit on the number of fragments to avoid having to 38189251Ssam * allocate memory (which could fail). 39189251Ssam */ 40214734Srpaulo return -1; 41189251Ssam } 42189251Ssam 43189251Ssam /* if key is longer than 64 bytes reset it to key = SHA1(key) */ 44189251Ssam if (key_len > 64) { 45214734Srpaulo if (sha1_vector(1, &key, &key_len, tk)) 46214734Srpaulo return -1; 47189251Ssam key = tk; 48189251Ssam key_len = 20; 49189251Ssam } 50189251Ssam 51189251Ssam /* the HMAC_SHA1 transform looks like: 52189251Ssam * 53189251Ssam * SHA1(K XOR opad, SHA1(K XOR ipad, text)) 54189251Ssam * 55189251Ssam * where K is an n byte key 56189251Ssam * ipad is the byte 0x36 repeated 64 times 57189251Ssam * opad is the byte 0x5c repeated 64 times 58189251Ssam * and text is the data being protected */ 59189251Ssam 60189251Ssam /* start out by storing key in ipad */ 61189251Ssam os_memset(k_pad, 0, sizeof(k_pad)); 62189251Ssam os_memcpy(k_pad, key, key_len); 63189251Ssam /* XOR key with ipad values */ 64189251Ssam for (i = 0; i < 64; i++) 65189251Ssam k_pad[i] ^= 0x36; 66189251Ssam 67189251Ssam /* perform inner SHA1 */ 68189251Ssam _addr[0] = k_pad; 69189251Ssam _len[0] = 64; 70189251Ssam for (i = 0; i < num_elem; i++) { 71189251Ssam _addr[i + 1] = addr[i]; 72189251Ssam _len[i + 1] = len[i]; 73189251Ssam } 74214734Srpaulo if (sha1_vector(1 + num_elem, _addr, _len, mac)) 75214734Srpaulo return -1; 76189251Ssam 77189251Ssam os_memset(k_pad, 0, sizeof(k_pad)); 78189251Ssam os_memcpy(k_pad, key, key_len); 79189251Ssam /* XOR key with opad values */ 80189251Ssam for (i = 0; i < 64; i++) 81189251Ssam k_pad[i] ^= 0x5c; 82189251Ssam 83189251Ssam /* perform outer SHA1 */ 84189251Ssam _addr[0] = k_pad; 85189251Ssam _len[0] = 64; 86189251Ssam _addr[1] = mac; 87189251Ssam _len[1] = SHA1_MAC_LEN; 88281806Srpaulo ret = sha1_vector(2, _addr, _len, mac); 89351611Scy forced_memzero(k_pad, sizeof(k_pad)); 90351611Scy forced_memzero(tk, sizeof(tk)); 91281806Srpaulo return ret; 92189251Ssam} 93189251Ssam 94189251Ssam 95189251Ssam/** 96189251Ssam * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104) 97189251Ssam * @key: Key for HMAC operations 98189251Ssam * @key_len: Length of the key in bytes 99189251Ssam * @data: Pointers to the data area 100189251Ssam * @data_len: Length of the data area 101189251Ssam * @mac: Buffer for the hash (20 bytes) 102214734Srpaulo * Returns: 0 on success, -1 of failure 103189251Ssam */ 104214734Srpauloint hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, 105189251Ssam u8 *mac) 106189251Ssam{ 107214734Srpaulo return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); 108189251Ssam} 109