blob: 1e8a122176c8e8a8488f5254871418f3cd1c401c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * SHA-256 hash implementation and interface functions
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "sha256.h"
13#include "crypto.h"
14
15
16/**
17 * hmac_sha256_vector - HMAC-SHA256 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 (32 bytes)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070024 * Returns: 0 on success, -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
27 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028{
29 unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
30 unsigned char tk[32];
Sunil Ravi2a14cf12023-11-21 00:54:38 +000031 const u8 *_addr[HMAC_VECTOR_MAX_ELEM + 1];
32 size_t _len[HMAC_VECTOR_MAX_ELEM + 1], i;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000033 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034
Sunil Ravi2a14cf12023-11-21 00:54:38 +000035 if (num_elem > HMAC_VECTOR_MAX_ELEM) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036 /*
37 * Fixed limit on the number of fragments to avoid having to
38 * allocate memory (which could fail).
39 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070040 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041 }
42
43 /* if key is longer than 64 bytes reset it to key = SHA256(key) */
44 if (key_len > 64) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045 if (sha256_vector(1, &key, &key_len, tk) < 0)
46 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047 key = tk;
48 key_len = 32;
49 }
50
51 /* the HMAC_SHA256 transform looks like:
52 *
53 * SHA256(K XOR opad, SHA256(K XOR ipad, text))
54 *
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 */
59
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++)
65 k_pad[i] ^= 0x36;
66
67 /* perform inner SHA256 */
68 _addr[0] = k_pad;
69 _len[0] = 64;
70 for (i = 0; i < num_elem; i++) {
71 _addr[i + 1] = addr[i];
72 _len[i + 1] = len[i];
73 }
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000074 ret = sha256_vector(1 + num_elem, _addr, _len, mac);
75 if (ret < 0)
76 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077
78 os_memset(k_pad, 0, sizeof(k_pad));
79 os_memcpy(k_pad, key, key_len);
80 /* XOR key with opad values */
81 for (i = 0; i < 64; i++)
82 k_pad[i] ^= 0x5c;
83
84 /* perform outer SHA256 */
85 _addr[0] = k_pad;
86 _len[0] = 64;
87 _addr[1] = mac;
88 _len[1] = SHA256_MAC_LEN;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000089
90 ret = sha256_vector(2, _addr, _len, mac);
91
92fail:
93 forced_memzero(k_pad, sizeof(k_pad));
94 forced_memzero(tk, sizeof(tk));
95
96 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097}
98
99
100/**
101 * hmac_sha256 - HMAC-SHA256 over data buffer (RFC 2104)
102 * @key: Key for HMAC operations
103 * @key_len: Length of the key in bytes
104 * @data: Pointers to the data area
105 * @data_len: Length of the data area
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106 * @mac: Buffer for the hash (32 bytes)
107 * Returns: 0 on success, -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700109int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
110 size_t data_len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700112 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113}