Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / crypto / sha1.c
blobfe00bdbc586991da598a8da00d27f41bcc44ec4c
1 /*
2 * SHA1 hash implementation and interface functions
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "sha1.h"
19 #include "crypto.h"
22 /**
23 * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104)
24 * @key: Key for HMAC operations
25 * @key_len: Length of the key in bytes
26 * @num_elem: Number of elements in the data vector
27 * @addr: Pointers to the data areas
28 * @len: Lengths of the data blocks
29 * @mac: Buffer for the hash (20 bytes)
30 * Returns: 0 on success, -1 on failure
32 int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
33 const u8 *addr[], const size_t *len, u8 *mac)
35 unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
36 unsigned char tk[20];
37 const u8 *_addr[6];
38 size_t _len[6], i;
40 if (num_elem > 5) {
42 * Fixed limit on the number of fragments to avoid having to
43 * allocate memory (which could fail).
45 return -1;
48 /* if key is longer than 64 bytes reset it to key = SHA1(key) */
49 if (key_len > 64) {
50 if (sha1_vector(1, &key, &key_len, tk))
51 return -1;
52 key = tk;
53 key_len = 20;
56 /* the HMAC_SHA1 transform looks like:
58 * SHA1(K XOR opad, SHA1(K XOR ipad, text))
60 * where K is an n byte key
61 * ipad is the byte 0x36 repeated 64 times
62 * opad is the byte 0x5c repeated 64 times
63 * and text is the data being protected */
65 /* start out by storing key in ipad */
66 os_memset(k_pad, 0, sizeof(k_pad));
67 os_memcpy(k_pad, key, key_len);
68 /* XOR key with ipad values */
69 for (i = 0; i < 64; i++)
70 k_pad[i] ^= 0x36;
72 /* perform inner SHA1 */
73 _addr[0] = k_pad;
74 _len[0] = 64;
75 for (i = 0; i < num_elem; i++) {
76 _addr[i + 1] = addr[i];
77 _len[i + 1] = len[i];
79 if (sha1_vector(1 + num_elem, _addr, _len, mac))
80 return -1;
82 os_memset(k_pad, 0, sizeof(k_pad));
83 os_memcpy(k_pad, key, key_len);
84 /* XOR key with opad values */
85 for (i = 0; i < 64; i++)
86 k_pad[i] ^= 0x5c;
88 /* perform outer SHA1 */
89 _addr[0] = k_pad;
90 _len[0] = 64;
91 _addr[1] = mac;
92 _len[1] = SHA1_MAC_LEN;
93 return sha1_vector(2, _addr, _len, mac);
97 /**
98 * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
99 * @key: Key for HMAC operations
100 * @key_len: Length of the key in bytes
101 * @data: Pointers to the data area
102 * @data_len: Length of the data area
103 * @mac: Buffer for the hash (20 bytes)
104 * Returns: 0 on success, -1 of failure
106 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
107 u8 *mac)
109 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
114 * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
115 * @key: Key for PRF
116 * @key_len: Length of the key in bytes
117 * @label: A unique label for each purpose of the PRF
118 * @data: Extra data to bind into the key
119 * @data_len: Length of the data
120 * @buf: Buffer for the generated pseudo-random key
121 * @buf_len: Number of bytes of key to generate
122 * Returns: 0 on success, -1 of failure
124 * This function is used to derive new, cryptographically separate keys from a
125 * given key (e.g., PMK in IEEE 802.11i).
127 int sha1_prf(const u8 *key, size_t key_len, const char *label,
128 const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
130 u8 counter = 0;
131 size_t pos, plen;
132 u8 hash[SHA1_MAC_LEN];
133 size_t label_len = os_strlen(label) + 1;
134 const unsigned char *addr[3];
135 size_t len[3];
137 addr[0] = (u8 *) label;
138 len[0] = label_len;
139 addr[1] = data;
140 len[1] = data_len;
141 addr[2] = &counter;
142 len[2] = 1;
144 pos = 0;
145 while (pos < buf_len) {
146 plen = buf_len - pos;
147 if (plen >= SHA1_MAC_LEN) {
148 if (hmac_sha1_vector(key, key_len, 3, addr, len,
149 &buf[pos]))
150 return -1;
151 pos += SHA1_MAC_LEN;
152 } else {
153 if (hmac_sha1_vector(key, key_len, 3, addr, len,
154 hash))
155 return -1;
156 os_memcpy(&buf[pos], hash, plen);
157 break;
159 counter++;
162 return 0;