ieee80211_crypto.c revision 1.5.4.5 1 1.5.4.5 skrll /* $NetBSD: ieee80211_crypto.c,v 1.5.4.5 2005/03/04 16:53:17 skrll Exp $ */
2 1.5.4.2 skrll /*-
3 1.5.4.2 skrll * Copyright (c) 2001 Atsushi Onoe
4 1.5.4.2 skrll * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 1.5.4.2 skrll * All rights reserved.
6 1.5.4.2 skrll *
7 1.5.4.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.5.4.2 skrll * modification, are permitted provided that the following conditions
9 1.5.4.2 skrll * are met:
10 1.5.4.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.5.4.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.5.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.5.4.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.5.4.2 skrll * documentation and/or other materials provided with the distribution.
15 1.5.4.2 skrll * 3. The name of the author may not be used to endorse or promote products
16 1.5.4.2 skrll * derived from this software without specific prior written permission.
17 1.5.4.2 skrll *
18 1.5.4.2 skrll * Alternatively, this software may be distributed under the terms of the
19 1.5.4.2 skrll * GNU General Public License ("GPL") version 2 as published by the Free
20 1.5.4.2 skrll * Software Foundation.
21 1.5.4.2 skrll *
22 1.5.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.5.4.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.5.4.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.5.4.2 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.5.4.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.5.4.2 skrll * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.5.4.2 skrll * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.5.4.2 skrll * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.5.4.2 skrll * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.5.4.2 skrll * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.5.4.2 skrll */
33 1.5.4.2 skrll
34 1.5.4.2 skrll #include <sys/cdefs.h>
35 1.5.4.2 skrll #ifdef __FreeBSD__
36 1.5.4.2 skrll __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.3 2003/10/17 23:15:30 sam Exp $");
37 1.5.4.2 skrll #else
38 1.5.4.5 skrll __KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto.c,v 1.5.4.5 2005/03/04 16:53:17 skrll Exp $");
39 1.5.4.2 skrll #endif
40 1.5.4.2 skrll
41 1.5.4.2 skrll #include "opt_inet.h"
42 1.5.4.2 skrll
43 1.5.4.2 skrll #include <sys/param.h>
44 1.5.4.5 skrll #include <sys/systm.h>
45 1.5.4.5 skrll #include <sys/mbuf.h>
46 1.5.4.2 skrll #include <sys/malloc.h>
47 1.5.4.2 skrll #include <sys/kernel.h>
48 1.5.4.2 skrll #include <sys/socket.h>
49 1.5.4.2 skrll #include <sys/sockio.h>
50 1.5.4.2 skrll #include <sys/endian.h>
51 1.5.4.2 skrll #include <sys/errno.h>
52 1.5.4.2 skrll #ifdef __FreeBSD__
53 1.5.4.2 skrll #include <sys/bus.h>
54 1.5.4.2 skrll #endif
55 1.5.4.2 skrll #include <sys/proc.h>
56 1.5.4.2 skrll #include <sys/sysctl.h>
57 1.5.4.2 skrll
58 1.5.4.2 skrll #ifdef __FreeBSD__
59 1.5.4.2 skrll #include <machine/atomic.h>
60 1.5.4.2 skrll #endif
61 1.5.4.5 skrll
62 1.5.4.2 skrll #include <net/if.h>
63 1.5.4.2 skrll #include <net/if_dl.h>
64 1.5.4.2 skrll #include <net/if_media.h>
65 1.5.4.2 skrll #include <net/if_arp.h>
66 1.5.4.2 skrll #ifdef __FreeBSD__
67 1.5.4.2 skrll #include <net/ethernet.h>
68 1.5.4.2 skrll #else
69 1.5.4.2 skrll #include <net/if_ether.h>
70 1.5.4.2 skrll #endif
71 1.5.4.2 skrll #include <net/if_llc.h>
72 1.5.4.2 skrll
73 1.5.4.2 skrll #include <net80211/ieee80211_var.h>
74 1.5.4.2 skrll #include <net80211/ieee80211_compat.h>
75 1.5.4.2 skrll
76 1.5.4.2 skrll #include <net/bpf.h>
77 1.5.4.2 skrll
78 1.5.4.2 skrll #ifdef INET
79 1.5.4.5 skrll #include <netinet/in.h>
80 1.5.4.2 skrll #ifdef __FreeBSD__
81 1.5.4.2 skrll #include <netinet/if_ether.h>
82 1.5.4.2 skrll #else
83 1.5.4.2 skrll #include <net/if_ether.h>
84 1.5.4.2 skrll #endif
85 1.5.4.2 skrll #endif
86 1.5.4.2 skrll
87 1.5.4.2 skrll #ifdef __FreeBSD__
88 1.5.4.2 skrll #include <crypto/rc4/rc4.h>
89 1.5.4.2 skrll #define arc4_ctxlen() sizeof (struct rc4_state)
90 1.5.4.2 skrll #define arc4_setkey(_c,_k,_l) rc4_init(_c,_k,_l)
91 1.5.4.2 skrll #define arc4_encrypt(_c,_d,_s,_l) rc4_crypt(_c,_s,_d,_l)
92 1.5.4.2 skrll #else
93 1.5.4.2 skrll #include <crypto/arc4/arc4.h>
94 1.5.4.2 skrll #endif
95 1.5.4.2 skrll
96 1.5.4.2 skrll static void ieee80211_crc_init(void);
97 1.5.4.2 skrll static u_int32_t ieee80211_crc_update(u_int32_t crc, u_int8_t *buf, int len);
98 1.5.4.2 skrll
99 1.5.4.2 skrll void
100 1.5.4.2 skrll ieee80211_crypto_attach(struct ifnet *ifp)
101 1.5.4.2 skrll {
102 1.5.4.2 skrll struct ieee80211com *ic = (void *)ifp;
103 1.5.4.2 skrll
104 1.5.4.2 skrll /*
105 1.5.4.2 skrll * Setup crypto support.
106 1.5.4.2 skrll */
107 1.5.4.2 skrll ieee80211_crc_init();
108 1.5.4.2 skrll ic->ic_iv = arc4random();
109 1.5.4.2 skrll }
110 1.5.4.2 skrll
111 1.5.4.2 skrll void
112 1.5.4.2 skrll ieee80211_crypto_detach(struct ifnet *ifp)
113 1.5.4.2 skrll {
114 1.5.4.2 skrll struct ieee80211com *ic = (void *)ifp;
115 1.5.4.2 skrll
116 1.5.4.2 skrll if (ic->ic_wep_ctx != NULL) {
117 1.5.4.2 skrll free(ic->ic_wep_ctx, M_DEVBUF);
118 1.5.4.2 skrll ic->ic_wep_ctx = NULL;
119 1.5.4.2 skrll }
120 1.5.4.2 skrll }
121 1.5.4.2 skrll
122 1.5.4.2 skrll struct mbuf *
123 1.5.4.2 skrll ieee80211_wep_crypt(struct ifnet *ifp, struct mbuf *m0, int txflag)
124 1.5.4.2 skrll {
125 1.5.4.2 skrll struct ieee80211com *ic = (void *)ifp;
126 1.5.4.2 skrll struct mbuf *m, *n, *n0;
127 1.5.4.2 skrll struct ieee80211_frame *wh;
128 1.5.4.2 skrll int i, left, len, moff, noff, kid;
129 1.5.4.2 skrll u_int32_t iv, crc;
130 1.5.4.2 skrll u_int8_t *ivp;
131 1.5.4.2 skrll void *ctx;
132 1.5.4.2 skrll u_int8_t keybuf[IEEE80211_WEP_IVLEN + IEEE80211_KEYBUF_SIZE];
133 1.5.4.2 skrll u_int8_t crcbuf[IEEE80211_WEP_CRCLEN];
134 1.5.4.2 skrll
135 1.5.4.2 skrll n0 = NULL;
136 1.5.4.2 skrll if ((ctx = ic->ic_wep_ctx) == NULL) {
137 1.5.4.2 skrll ctx = malloc(arc4_ctxlen(), M_DEVBUF, M_NOWAIT);
138 1.5.4.2 skrll if (ctx == NULL) {
139 1.5.4.2 skrll ic->ic_stats.is_crypto_nomem++;
140 1.5.4.2 skrll goto fail;
141 1.5.4.2 skrll }
142 1.5.4.2 skrll ic->ic_wep_ctx = ctx;
143 1.5.4.2 skrll }
144 1.5.4.2 skrll m = m0;
145 1.5.4.2 skrll left = m->m_pkthdr.len;
146 1.5.4.2 skrll MGET(n, M_DONTWAIT, m->m_type);
147 1.5.4.2 skrll n0 = n;
148 1.5.4.2 skrll if (n == NULL) {
149 1.5.4.2 skrll if (txflag)
150 1.5.4.2 skrll ic->ic_stats.is_tx_nombuf++;
151 1.5.4.2 skrll else
152 1.5.4.2 skrll ic->ic_stats.is_rx_nombuf++;
153 1.5.4.2 skrll goto fail;
154 1.5.4.2 skrll }
155 1.5.4.2 skrll #ifdef __FreeBSD__
156 1.5.4.2 skrll M_MOVE_PKTHDR(n, m);
157 1.5.4.2 skrll #else
158 1.5.4.2 skrll M_COPY_PKTHDR(n, m);
159 1.5.4.2 skrll #endif
160 1.5.4.2 skrll len = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
161 1.5.4.2 skrll if (txflag) {
162 1.5.4.2 skrll n->m_pkthdr.len += len;
163 1.5.4.2 skrll } else {
164 1.5.4.2 skrll n->m_pkthdr.len -= len;
165 1.5.4.2 skrll left -= len;
166 1.5.4.2 skrll }
167 1.5.4.2 skrll n->m_len = MHLEN;
168 1.5.4.2 skrll if (n->m_pkthdr.len >= MINCLSIZE) {
169 1.5.4.2 skrll MCLGET(n, M_DONTWAIT);
170 1.5.4.2 skrll if (n->m_flags & M_EXT)
171 1.5.4.2 skrll n->m_len = n->m_ext.ext_size;
172 1.5.4.2 skrll }
173 1.5.4.2 skrll len = sizeof(struct ieee80211_frame);
174 1.5.4.2 skrll memcpy(mtod(n, caddr_t), mtod(m, caddr_t), len);
175 1.5.4.2 skrll wh = mtod(n, struct ieee80211_frame *);
176 1.5.4.2 skrll left -= len;
177 1.5.4.2 skrll moff = len;
178 1.5.4.2 skrll noff = len;
179 1.5.4.2 skrll if (txflag) {
180 1.5.4.2 skrll kid = ic->ic_wep_txkey;
181 1.5.4.2 skrll wh->i_fc[1] |= IEEE80211_FC1_WEP;
182 1.5.4.2 skrll iv = ic->ic_iv;
183 1.5.4.2 skrll /*
184 1.5.4.2 skrll * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
185 1.5.4.2 skrll * (B, 255, N) with 3 <= B < 8
186 1.5.4.2 skrll */
187 1.5.4.2 skrll if (iv >= 0x03ff00 &&
188 1.5.4.2 skrll (iv & 0xf8ff00) == 0x00ff00)
189 1.5.4.2 skrll iv += 0x000100;
190 1.5.4.2 skrll ic->ic_iv = iv + 1;
191 1.5.4.2 skrll /* put iv in little endian to prepare 802.11i */
192 1.5.4.2 skrll ivp = mtod(n, u_int8_t *) + noff;
193 1.5.4.2 skrll for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
194 1.5.4.2 skrll ivp[i] = iv & 0xff;
195 1.5.4.2 skrll iv >>= 8;
196 1.5.4.2 skrll }
197 1.5.4.2 skrll ivp[IEEE80211_WEP_IVLEN] = kid << 6; /* pad and keyid */
198 1.5.4.2 skrll noff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
199 1.5.4.2 skrll } else {
200 1.5.4.2 skrll wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
201 1.5.4.2 skrll ivp = mtod(m, u_int8_t *) + moff;
202 1.5.4.2 skrll kid = ivp[IEEE80211_WEP_IVLEN] >> 6;
203 1.5.4.2 skrll moff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
204 1.5.4.2 skrll }
205 1.5.4.2 skrll memcpy(keybuf, ivp, IEEE80211_WEP_IVLEN);
206 1.5.4.2 skrll memcpy(keybuf + IEEE80211_WEP_IVLEN, ic->ic_nw_keys[kid].wk_key,
207 1.5.4.2 skrll ic->ic_nw_keys[kid].wk_len);
208 1.5.4.2 skrll arc4_setkey(ctx, keybuf,
209 1.5.4.2 skrll IEEE80211_WEP_IVLEN + ic->ic_nw_keys[kid].wk_len);
210 1.5.4.2 skrll
211 1.5.4.2 skrll /* encrypt with calculating CRC */
212 1.5.4.2 skrll crc = ~0;
213 1.5.4.2 skrll while (left > 0) {
214 1.5.4.2 skrll len = m->m_len - moff;
215 1.5.4.2 skrll if (len == 0) {
216 1.5.4.2 skrll m = m->m_next;
217 1.5.4.2 skrll moff = 0;
218 1.5.4.2 skrll continue;
219 1.5.4.2 skrll }
220 1.5.4.2 skrll if (len > n->m_len - noff) {
221 1.5.4.2 skrll len = n->m_len - noff;
222 1.5.4.2 skrll if (len == 0) {
223 1.5.4.2 skrll MGET(n->m_next, M_DONTWAIT, n->m_type);
224 1.5.4.2 skrll if (n->m_next == NULL) {
225 1.5.4.2 skrll if (txflag)
226 1.5.4.2 skrll ic->ic_stats.is_tx_nombuf++;
227 1.5.4.2 skrll else
228 1.5.4.2 skrll ic->ic_stats.is_rx_nombuf++;
229 1.5.4.2 skrll goto fail;
230 1.5.4.2 skrll }
231 1.5.4.2 skrll n = n->m_next;
232 1.5.4.2 skrll n->m_len = MLEN;
233 1.5.4.2 skrll if (left >= MINCLSIZE) {
234 1.5.4.2 skrll MCLGET(n, M_DONTWAIT);
235 1.5.4.2 skrll if (n->m_flags & M_EXT)
236 1.5.4.2 skrll n->m_len = n->m_ext.ext_size;
237 1.5.4.2 skrll }
238 1.5.4.2 skrll noff = 0;
239 1.5.4.2 skrll continue;
240 1.5.4.2 skrll }
241 1.5.4.2 skrll }
242 1.5.4.2 skrll if (len > left)
243 1.5.4.2 skrll len = left;
244 1.5.4.2 skrll arc4_encrypt(ctx, mtod(n, caddr_t) + noff,
245 1.5.4.2 skrll mtod(m, caddr_t) + moff, len);
246 1.5.4.2 skrll if (txflag)
247 1.5.4.2 skrll crc = ieee80211_crc_update(crc,
248 1.5.4.2 skrll mtod(m, u_int8_t *) + moff, len);
249 1.5.4.2 skrll else
250 1.5.4.2 skrll crc = ieee80211_crc_update(crc,
251 1.5.4.2 skrll mtod(n, u_int8_t *) + noff, len);
252 1.5.4.2 skrll left -= len;
253 1.5.4.2 skrll moff += len;
254 1.5.4.2 skrll noff += len;
255 1.5.4.2 skrll }
256 1.5.4.2 skrll crc = ~crc;
257 1.5.4.2 skrll if (txflag) {
258 1.5.4.2 skrll *(u_int32_t *)crcbuf = htole32(crc);
259 1.5.4.2 skrll if (n->m_len >= noff + sizeof(crcbuf))
260 1.5.4.2 skrll n->m_len = noff + sizeof(crcbuf);
261 1.5.4.2 skrll else {
262 1.5.4.2 skrll n->m_len = noff;
263 1.5.4.2 skrll MGET(n->m_next, M_DONTWAIT, n->m_type);
264 1.5.4.2 skrll if (n->m_next == NULL) {
265 1.5.4.2 skrll ic->ic_stats.is_tx_nombuf++;
266 1.5.4.2 skrll goto fail;
267 1.5.4.2 skrll }
268 1.5.4.2 skrll n = n->m_next;
269 1.5.4.2 skrll n->m_len = sizeof(crcbuf);
270 1.5.4.2 skrll noff = 0;
271 1.5.4.2 skrll }
272 1.5.4.2 skrll arc4_encrypt(ctx, mtod(n, caddr_t) + noff, crcbuf,
273 1.5.4.2 skrll sizeof(crcbuf));
274 1.5.4.2 skrll } else {
275 1.5.4.2 skrll n->m_len = noff;
276 1.5.4.2 skrll for (noff = 0; noff < sizeof(crcbuf); noff += len) {
277 1.5.4.2 skrll len = sizeof(crcbuf) - noff;
278 1.5.4.2 skrll if (len > m->m_len - moff)
279 1.5.4.2 skrll len = m->m_len - moff;
280 1.5.4.2 skrll if (len > 0)
281 1.5.4.2 skrll arc4_encrypt(ctx, crcbuf + noff,
282 1.5.4.2 skrll mtod(m, caddr_t) + moff, len);
283 1.5.4.2 skrll m = m->m_next;
284 1.5.4.2 skrll moff = 0;
285 1.5.4.2 skrll }
286 1.5.4.2 skrll if (crc != le32toh(*(u_int32_t *)crcbuf)) {
287 1.5.4.2 skrll #ifdef IEEE80211_DEBUG
288 1.5.4.2 skrll if (ieee80211_debug) {
289 1.5.4.2 skrll if_printf(ifp, "decrypt CRC error\n");
290 1.5.4.2 skrll if (ieee80211_debug > 1)
291 1.5.4.2 skrll ieee80211_dump_pkt(n0->m_data,
292 1.5.4.2 skrll n0->m_len, -1, -1);
293 1.5.4.2 skrll }
294 1.5.4.2 skrll #endif
295 1.5.4.2 skrll ic->ic_stats.is_rx_decryptcrc++;
296 1.5.4.2 skrll goto fail;
297 1.5.4.2 skrll }
298 1.5.4.2 skrll }
299 1.5.4.2 skrll m_freem(m0);
300 1.5.4.2 skrll return n0;
301 1.5.4.2 skrll
302 1.5.4.2 skrll fail:
303 1.5.4.2 skrll m_freem(m0);
304 1.5.4.2 skrll m_freem(n0);
305 1.5.4.2 skrll return NULL;
306 1.5.4.2 skrll }
307 1.5.4.2 skrll
308 1.5.4.2 skrll /*
309 1.5.4.2 skrll * CRC 32 -- routine from RFC 2083
310 1.5.4.2 skrll */
311 1.5.4.2 skrll
312 1.5.4.2 skrll /* Table of CRCs of all 8-bit messages */
313 1.5.4.2 skrll static u_int32_t ieee80211_crc_table[256];
314 1.5.4.2 skrll
315 1.5.4.2 skrll /* Make the table for a fast CRC. */
316 1.5.4.2 skrll static void
317 1.5.4.2 skrll ieee80211_crc_init(void)
318 1.5.4.2 skrll {
319 1.5.4.2 skrll u_int32_t c;
320 1.5.4.2 skrll int n, k;
321 1.5.4.2 skrll
322 1.5.4.2 skrll for (n = 0; n < 256; n++) {
323 1.5.4.2 skrll c = (u_int32_t)n;
324 1.5.4.2 skrll for (k = 0; k < 8; k++) {
325 1.5.4.2 skrll if (c & 1)
326 1.5.4.2 skrll c = 0xedb88320UL ^ (c >> 1);
327 1.5.4.2 skrll else
328 1.5.4.2 skrll c = c >> 1;
329 1.5.4.2 skrll }
330 1.5.4.2 skrll ieee80211_crc_table[n] = c;
331 1.5.4.2 skrll }
332 1.5.4.2 skrll }
333 1.5.4.2 skrll
334 1.5.4.2 skrll /*
335 1.5.4.2 skrll * Update a running CRC with the bytes buf[0..len-1]--the CRC
336 1.5.4.2 skrll * should be initialized to all 1's, and the transmitted value
337 1.5.4.2 skrll * is the 1's complement of the final running CRC
338 1.5.4.2 skrll */
339 1.5.4.2 skrll
340 1.5.4.2 skrll static u_int32_t
341 1.5.4.2 skrll ieee80211_crc_update(u_int32_t crc, u_int8_t *buf, int len)
342 1.5.4.2 skrll {
343 1.5.4.2 skrll u_int8_t *endbuf;
344 1.5.4.2 skrll
345 1.5.4.2 skrll for (endbuf = buf + len; buf < endbuf; buf++)
346 1.5.4.2 skrll crc = ieee80211_crc_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
347 1.5.4.2 skrll return crc;
348 1.5.4.2 skrll }
349