blob: 274d81fa82e7d5d43a4a2351973d11dad646ea59 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * SHA1 hash implementation and interface functions
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4 *
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 "sha1.h"
13#include "crypto.h"
14
15
16/**
17 * hmac_sha1_vector - HMAC-SHA1 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 (20 bytes)
24 * Returns: 0 on success, -1 on failure
25 */
26int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
27 const u8 *addr[], const size_t *len, u8 *mac)
28{
29 unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
30 unsigned char tk[20];
31 const u8 *_addr[6];
32 size_t _len[6], i;
33
34 if (num_elem > 5) {
35 /*
36 * Fixed limit on the number of fragments to avoid having to
37 * allocate memory (which could fail).
38 */
39 return -1;
40 }
41
42 /* if key is longer than 64 bytes reset it to key = SHA1(key) */
43 if (key_len > 64) {
44 if (sha1_vector(1, &key, &key_len, tk))
45 return -1;
46 key = tk;
47 key_len = 20;
48 }
49
50 /* the HMAC_SHA1 transform looks like:
51 *
52 * SHA1(K XOR opad, SHA1(K XOR ipad, text))
53 *
54 * where K is an n byte key
55 * ipad is the byte 0x36 repeated 64 times
56 * opad is the byte 0x5c repeated 64 times
57 * and text is the data being protected */
58
59 /* start out by storing key in ipad */
60 os_memset(k_pad, 0, sizeof(k_pad));
61 os_memcpy(k_pad, key, key_len);
62 /* XOR key with ipad values */
63 for (i = 0; i < 64; i++)
64 k_pad[i] ^= 0x36;
65
66 /* perform inner SHA1 */
67 _addr[0] = k_pad;
68 _len[0] = 64;
69 for (i = 0; i < num_elem; i++) {
70 _addr[i + 1] = addr[i];
71 _len[i + 1] = len[i];
72 }
73 if (sha1_vector(1 + num_elem, _addr, _len, mac))
74 return -1;
75
76 os_memset(k_pad, 0, sizeof(k_pad));
77 os_memcpy(k_pad, key, key_len);
78 /* XOR key with opad values */
79 for (i = 0; i < 64; i++)
80 k_pad[i] ^= 0x5c;
81
82 /* perform outer SHA1 */
83 _addr[0] = k_pad;
84 _len[0] = 64;
85 _addr[1] = mac;
86 _len[1] = SHA1_MAC_LEN;
87 return sha1_vector(2, _addr, _len, mac);
88}
89
90
91/**
92 * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
93 * @key: Key for HMAC operations
94 * @key_len: Length of the key in bytes
95 * @data: Pointers to the data area
96 * @data_len: Length of the data area
97 * @mac: Buffer for the hash (20 bytes)
98 * Returns: 0 on success, -1 of failure
99 */
100int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
101 u8 *mac)
102{
103 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
104}
105
106
107/**
108 * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
109 * @key: Key for PRF
110 * @key_len: Length of the key in bytes
111 * @label: A unique label for each purpose of the PRF
112 * @data: Extra data to bind into the key
113 * @data_len: Length of the data
114 * @buf: Buffer for the generated pseudo-random key
115 * @buf_len: Number of bytes of key to generate
116 * Returns: 0 on success, -1 of failure
117 *
118 * This function is used to derive new, cryptographically separate keys from a
119 * given key (e.g., PMK in IEEE 802.11i).
120 */
121int sha1_prf(const u8 *key, size_t key_len, const char *label,
122 const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
123{
124 u8 counter = 0;
125 size_t pos, plen;
126 u8 hash[SHA1_MAC_LEN];
127 size_t label_len = os_strlen(label) + 1;
128 const unsigned char *addr[3];
129 size_t len[3];
130
131 addr[0] = (u8 *) label;
132 len[0] = label_len;
133 addr[1] = data;
134 len[1] = data_len;
135 addr[2] = &counter;
136 len[2] = 1;
137
138 pos = 0;
139 while (pos < buf_len) {
140 plen = buf_len - pos;
141 if (plen >= SHA1_MAC_LEN) {
142 if (hmac_sha1_vector(key, key_len, 3, addr, len,
143 &buf[pos]))
144 return -1;
145 pos += SHA1_MAC_LEN;
146 } else {
147 if (hmac_sha1_vector(key, key_len, 3, addr, len,
148 hash))
149 return -1;
150 os_memcpy(&buf[pos], hash, plen);
151 break;
152 }
153 counter++;
154 }
155
156 return 0;
157}