1189251Ssam/*
2189251Ssam * SHA-256 hash implementation and interface functions
3252726Srpaulo * Copyright (c) 2003-2012, 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 "sha256.h"
13189251Ssam#include "crypto.h"
14189251Ssam
15189251Ssam
16189251Ssam/**
17189251Ssam * hmac_sha256_vector - HMAC-SHA256 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 (32 bytes)
24252726Srpaulo * Returns: 0 on success, -1 on failure
25189251Ssam */
26252726Srpauloint hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
27252726Srpaulo		       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[32];
31189251Ssam	const u8 *_addr[6];
32189251Ssam	size_t _len[6], i;
33189251Ssam
34189251Ssam	if (num_elem > 5) {
35189251Ssam		/*
36189251Ssam		 * Fixed limit on the number of fragments to avoid having to
37189251Ssam		 * allocate memory (which could fail).
38189251Ssam		 */
39252726Srpaulo		return -1;
40189251Ssam	}
41189251Ssam
42189251Ssam        /* if key is longer than 64 bytes reset it to key = SHA256(key) */
43189251Ssam        if (key_len > 64) {
44252726Srpaulo		if (sha256_vector(1, &key, &key_len, tk) < 0)
45252726Srpaulo			return -1;
46189251Ssam		key = tk;
47189251Ssam		key_len = 32;
48189251Ssam        }
49189251Ssam
50189251Ssam	/* the HMAC_SHA256 transform looks like:
51189251Ssam	 *
52189251Ssam	 * SHA256(K XOR opad, SHA256(K XOR ipad, text))
53189251Ssam	 *
54189251Ssam	 * where K is an n byte key
55189251Ssam	 * ipad is the byte 0x36 repeated 64 times
56189251Ssam	 * opad is the byte 0x5c repeated 64 times
57189251Ssam	 * and text is the data being protected */
58189251Ssam
59189251Ssam	/* start out by storing key in ipad */
60189251Ssam	os_memset(k_pad, 0, sizeof(k_pad));
61189251Ssam	os_memcpy(k_pad, key, key_len);
62189251Ssam	/* XOR key with ipad values */
63189251Ssam	for (i = 0; i < 64; i++)
64189251Ssam		k_pad[i] ^= 0x36;
65189251Ssam
66189251Ssam	/* perform inner SHA256 */
67189251Ssam	_addr[0] = k_pad;
68189251Ssam	_len[0] = 64;
69189251Ssam	for (i = 0; i < num_elem; i++) {
70189251Ssam		_addr[i + 1] = addr[i];
71189251Ssam		_len[i + 1] = len[i];
72189251Ssam	}
73252726Srpaulo	if (sha256_vector(1 + num_elem, _addr, _len, mac) < 0)
74252726Srpaulo		return -1;
75189251Ssam
76189251Ssam	os_memset(k_pad, 0, sizeof(k_pad));
77189251Ssam	os_memcpy(k_pad, key, key_len);
78189251Ssam	/* XOR key with opad values */
79189251Ssam	for (i = 0; i < 64; i++)
80189251Ssam		k_pad[i] ^= 0x5c;
81189251Ssam
82189251Ssam	/* perform outer SHA256 */
83189251Ssam	_addr[0] = k_pad;
84189251Ssam	_len[0] = 64;
85189251Ssam	_addr[1] = mac;
86189251Ssam	_len[1] = SHA256_MAC_LEN;
87252726Srpaulo	return sha256_vector(2, _addr, _len, mac);
88189251Ssam}
89189251Ssam
90189251Ssam
91189251Ssam/**
92189251Ssam * hmac_sha256 - HMAC-SHA256 over data buffer (RFC 2104)
93189251Ssam * @key: Key for HMAC operations
94189251Ssam * @key_len: Length of the key in bytes
95189251Ssam * @data: Pointers to the data area
96189251Ssam * @data_len: Length of the data area
97252726Srpaulo * @mac: Buffer for the hash (32 bytes)
98252726Srpaulo * Returns: 0 on success, -1 on failure
99189251Ssam */
100252726Srpauloint hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
101252726Srpaulo		size_t data_len, u8 *mac)
102189251Ssam{
103252726Srpaulo	return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
104189251Ssam}
105