1189251Ssam/* 2189251Ssam * SHA1 hash implementation and interface functions 3189251Ssam * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> 4189251Ssam * 5189251Ssam * This program is free software; you can redistribute it and/or modify 6189251Ssam * it under the terms of the GNU General Public License version 2 as 7189251Ssam * published by the Free Software Foundation. 8189251Ssam * 9189251Ssam * Alternatively, this software may be distributed under the terms of BSD 10189251Ssam * license. 11189251Ssam * 12189251Ssam * See README and COPYING for more details. 13189251Ssam */ 14189251Ssam 15189251Ssam#include "includes.h" 16189251Ssam 17189251Ssam#include "common.h" 18189251Ssam#include "sha1.h" 19189251Ssam#include "crypto.h" 20189251Ssam 21189251Ssam 22189251Ssam/** 23189251Ssam * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104) 24189251Ssam * @key: Key for HMAC operations 25189251Ssam * @key_len: Length of the key in bytes 26189251Ssam * @num_elem: Number of elements in the data vector 27189251Ssam * @addr: Pointers to the data areas 28189251Ssam * @len: Lengths of the data blocks 29189251Ssam * @mac: Buffer for the hash (20 bytes) 30214734Srpaulo * Returns: 0 on success, -1 on failure 31189251Ssam */ 32214734Srpauloint hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, 33214734Srpaulo const u8 *addr[], const size_t *len, u8 *mac) 34189251Ssam{ 35189251Ssam unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */ 36189251Ssam unsigned char tk[20]; 37189251Ssam const u8 *_addr[6]; 38189251Ssam size_t _len[6], i; 39189251Ssam 40189251Ssam if (num_elem > 5) { 41189251Ssam /* 42189251Ssam * Fixed limit on the number of fragments to avoid having to 43189251Ssam * allocate memory (which could fail). 44189251Ssam */ 45214734Srpaulo return -1; 46189251Ssam } 47189251Ssam 48189251Ssam /* if key is longer than 64 bytes reset it to key = SHA1(key) */ 49189251Ssam if (key_len > 64) { 50214734Srpaulo if (sha1_vector(1, &key, &key_len, tk)) 51214734Srpaulo return -1; 52189251Ssam key = tk; 53189251Ssam key_len = 20; 54189251Ssam } 55189251Ssam 56189251Ssam /* the HMAC_SHA1 transform looks like: 57189251Ssam * 58189251Ssam * SHA1(K XOR opad, SHA1(K XOR ipad, text)) 59189251Ssam * 60189251Ssam * where K is an n byte key 61189251Ssam * ipad is the byte 0x36 repeated 64 times 62189251Ssam * opad is the byte 0x5c repeated 64 times 63189251Ssam * and text is the data being protected */ 64189251Ssam 65189251Ssam /* start out by storing key in ipad */ 66189251Ssam os_memset(k_pad, 0, sizeof(k_pad)); 67189251Ssam os_memcpy(k_pad, key, key_len); 68189251Ssam /* XOR key with ipad values */ 69189251Ssam for (i = 0; i < 64; i++) 70189251Ssam k_pad[i] ^= 0x36; 71189251Ssam 72189251Ssam /* perform inner SHA1 */ 73189251Ssam _addr[0] = k_pad; 74189251Ssam _len[0] = 64; 75189251Ssam for (i = 0; i < num_elem; i++) { 76189251Ssam _addr[i + 1] = addr[i]; 77189251Ssam _len[i + 1] = len[i]; 78189251Ssam } 79214734Srpaulo if (sha1_vector(1 + num_elem, _addr, _len, mac)) 80214734Srpaulo return -1; 81189251Ssam 82189251Ssam os_memset(k_pad, 0, sizeof(k_pad)); 83189251Ssam os_memcpy(k_pad, key, key_len); 84189251Ssam /* XOR key with opad values */ 85189251Ssam for (i = 0; i < 64; i++) 86189251Ssam k_pad[i] ^= 0x5c; 87189251Ssam 88189251Ssam /* perform outer SHA1 */ 89189251Ssam _addr[0] = k_pad; 90189251Ssam _len[0] = 64; 91189251Ssam _addr[1] = mac; 92189251Ssam _len[1] = SHA1_MAC_LEN; 93214734Srpaulo return sha1_vector(2, _addr, _len, mac); 94189251Ssam} 95189251Ssam 96189251Ssam 97189251Ssam/** 98189251Ssam * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104) 99189251Ssam * @key: Key for HMAC operations 100189251Ssam * @key_len: Length of the key in bytes 101189251Ssam * @data: Pointers to the data area 102189251Ssam * @data_len: Length of the data area 103189251Ssam * @mac: Buffer for the hash (20 bytes) 104214734Srpaulo * Returns: 0 on success, -1 of failure 105189251Ssam */ 106214734Srpauloint hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, 107189251Ssam u8 *mac) 108189251Ssam{ 109214734Srpaulo return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); 110189251Ssam} 111189251Ssam 112189251Ssam 113189251Ssam/** 114189251Ssam * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1) 115189251Ssam * @key: Key for PRF 116189251Ssam * @key_len: Length of the key in bytes 117189251Ssam * @label: A unique label for each purpose of the PRF 118189251Ssam * @data: Extra data to bind into the key 119189251Ssam * @data_len: Length of the data 120189251Ssam * @buf: Buffer for the generated pseudo-random key 121189251Ssam * @buf_len: Number of bytes of key to generate 122214734Srpaulo * Returns: 0 on success, -1 of failure 123189251Ssam * 124189251Ssam * This function is used to derive new, cryptographically separate keys from a 125189251Ssam * given key (e.g., PMK in IEEE 802.11i). 126189251Ssam */ 127214734Srpauloint sha1_prf(const u8 *key, size_t key_len, const char *label, 128214734Srpaulo const u8 *data, size_t data_len, u8 *buf, size_t buf_len) 129189251Ssam{ 130189251Ssam u8 counter = 0; 131189251Ssam size_t pos, plen; 132189251Ssam u8 hash[SHA1_MAC_LEN]; 133189251Ssam size_t label_len = os_strlen(label) + 1; 134189251Ssam const unsigned char *addr[3]; 135189251Ssam size_t len[3]; 136189251Ssam 137189251Ssam addr[0] = (u8 *) label; 138189251Ssam len[0] = label_len; 139189251Ssam addr[1] = data; 140189251Ssam len[1] = data_len; 141189251Ssam addr[2] = &counter; 142189251Ssam len[2] = 1; 143189251Ssam 144189251Ssam pos = 0; 145189251Ssam while (pos < buf_len) { 146189251Ssam plen = buf_len - pos; 147189251Ssam if (plen >= SHA1_MAC_LEN) { 148214734Srpaulo if (hmac_sha1_vector(key, key_len, 3, addr, len, 149214734Srpaulo &buf[pos])) 150214734Srpaulo return -1; 151189251Ssam pos += SHA1_MAC_LEN; 152189251Ssam } else { 153214734Srpaulo if (hmac_sha1_vector(key, key_len, 3, addr, len, 154214734Srpaulo hash)) 155214734Srpaulo return -1; 156189251Ssam os_memcpy(&buf[pos], hash, plen); 157189251Ssam break; 158189251Ssam } 159189251Ssam counter++; 160189251Ssam } 161189251Ssam 162189251Ssam return 0; 163189251Ssam} 164