mac80211: fix CCMP races
[deliverable/linux.git] / net / mac80211 / wpa.c
CommitLineData
f0706e82
JB
1/*
2 * Copyright 2002-2004, Instant802 Networks, Inc.
765cb46a 3 * Copyright 2008, Jouni Malinen <j@w1.fi>
f0706e82
JB
4 *
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.
8 */
9
10#include <linux/netdevice.h>
11#include <linux/types.h>
f0706e82
JB
12#include <linux/skbuff.h>
13#include <linux/compiler.h>
f14df804 14#include <linux/ieee80211.h>
5a0e3ad6 15#include <linux/gfp.h>
f14df804 16#include <asm/unaligned.h>
f0706e82 17#include <net/mac80211.h>
aba83a0b 18#include <crypto/aes.h>
eb063c17 19
f0706e82
JB
20#include "ieee80211_i.h"
21#include "michael.h"
22#include "tkip.h"
23#include "aes_ccm.h"
765cb46a 24#include "aes_cmac.h"
f0706e82
JB
25#include "wpa.h"
26
9ae54c84 27ieee80211_tx_result
5cf121c3 28ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
f0706e82 29{
747d753d 30 u8 *data, *key, *mic;
f0706e82 31 size_t data_len;
8e8862b7
HH
32 unsigned int hdrlen;
33 struct ieee80211_hdr *hdr;
f0706e82 34 struct sk_buff *skb = tx->skb;
813d7669 35 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
23c0752a 36 int tail;
f0706e82 37
c34498b9 38 hdr = (struct ieee80211_hdr *)skb->data;
97359d12
JB
39 if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
40 skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control))
9ae54c84 41 return TX_CONTINUE;
f0706e82 42
8e8862b7
HH
43 hdrlen = ieee80211_hdrlen(hdr->frame_control);
44 if (skb->len < hdrlen)
9ae54c84 45 return TX_DROP;
f0706e82 46
8e8862b7
HH
47 data = skb->data + hdrlen;
48 data_len = skb->len - hdrlen;
49
681d1190
JM
50 if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) {
51 /* Need to use software crypto for the test */
52 info->control.hw_key = NULL;
53 }
54
813d7669 55 if (info->control.hw_key &&
5cf121c3 56 !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
813d7669
JB
57 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
58 /* hwaccel - with no need for SW-generated MMIC */
9ae54c84 59 return TX_CONTINUE;
f0706e82
JB
60 }
61
23c0752a 62 tail = MICHAEL_MIC_LEN;
813d7669 63 if (!info->control.hw_key)
23c0752a
JB
64 tail += TKIP_ICV_LEN;
65
66 if (WARN_ON(skb_tailroom(skb) < tail ||
67 skb_headroom(skb) < TKIP_IV_LEN))
68 return TX_DROP;
f0706e82 69
747d753d 70 key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
f0706e82 71 mic = skb_put(skb, MICHAEL_MIC_LEN);
8e8862b7 72 michael_mic(key, hdr, data, data_len, mic);
681d1190
JM
73 if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
74 mic[0]++;
f0706e82 75
9ae54c84 76 return TX_CONTINUE;
f0706e82
JB
77}
78
79
9ae54c84 80ieee80211_rx_result
5cf121c3 81ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
f0706e82 82{
747d753d 83 u8 *data, *key = NULL;
f0706e82 84 size_t data_len;
8e8862b7 85 unsigned int hdrlen;
f0706e82
JB
86 u8 mic[MICHAEL_MIC_LEN];
87 struct sk_buff *skb = rx->skb;
eb9fb5b8
JB
88 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
89 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
f0706e82 90
816c04fe
CL
91 /*
92 * it makes no sense to check for MIC errors on anything other
93 * than data frames.
94 */
95 if (!ieee80211_is_data_present(hdr->frame_control))
9ae54c84 96 return RX_CONTINUE;
f0706e82 97
816c04fe
CL
98 /*
99 * No way to verify the MIC if the hardware stripped it or
100 * the IV with the key index. In this case we have solely rely
101 * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
102 * MIC failure report.
103 */
104 if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
105 if (status->flag & RX_FLAG_MMIC_ERROR)
106 goto mic_fail;
107
108 if (!(status->flag & RX_FLAG_IV_STRIPPED))
109 goto update_iv;
110
111 return RX_CONTINUE;
112 }
113
114 /*
115 * Some hardware seems to generate Michael MIC failure reports; even
116 * though, the frame was not encrypted with TKIP and therefore has no
117 * MIC. Ignore the flag them to avoid triggering countermeasures.
118 */
97359d12 119 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
816c04fe 120 !(status->flag & RX_FLAG_DECRYPTED))
9ae54c84 121 return RX_CONTINUE;
f0706e82 122
816c04fe
CL
123 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
124 /*
125 * APs with pairwise keys should never receive Michael MIC
126 * errors for non-zero keyidx because these are reserved for
127 * group keys and only the AP is sending real multicast
128 * frames in the BSS. (
129 */
130 return RX_DROP_UNUSABLE;
131 }
132
133 if (status->flag & RX_FLAG_MMIC_ERROR)
134 goto mic_fail;
135
8e8862b7
HH
136 hdrlen = ieee80211_hdrlen(hdr->frame_control);
137 if (skb->len < hdrlen + MICHAEL_MIC_LEN)
e4c26add 138 return RX_DROP_UNUSABLE;
f0706e82 139
8e8862b7
HH
140 data = skb->data + hdrlen;
141 data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
747d753d 142 key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
8e8862b7 143 michael_mic(key, hdr, data, data_len, mic);
816c04fe
CL
144 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0)
145 goto mic_fail;
f0706e82 146
f0706e82
JB
147 /* remove Michael MIC from payload */
148 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
149
816c04fe 150update_iv:
50741ae0 151 /* update IV in key information to be able to detect replays */
b0f76b33
HH
152 rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
153 rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
50741ae0 154
9ae54c84 155 return RX_CONTINUE;
816c04fe
CL
156
157mic_fail:
a66b98db
AN
158 /*
159 * In some cases the key can be unset - e.g. a multicast packet, in
160 * a driver that supports HW encryption. Send up the key idx only if
161 * the key is set.
162 */
163 mac80211_ev_michael_mic_failure(rx->sdata,
164 rx->key ? rx->key->conf.keyidx : -1,
816c04fe
CL
165 (void *) skb->data, NULL, GFP_ATOMIC);
166 return RX_DROP_UNUSABLE;
f0706e82
JB
167}
168
169
e039fa4a 170static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
f0706e82
JB
171{
172 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
173 struct ieee80211_key *key = tx->key;
e039fa4a 174 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
523b02ea 175 unsigned long flags;
d5184cac
HH
176 unsigned int hdrlen;
177 int len, tail;
f0706e82
JB
178 u8 *pos;
179
813d7669
JB
180 if (info->control.hw_key &&
181 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
182 /* hwaccel - with no need for software-generated IV */
23c0752a 183 return 0;
e039fa4a
JB
184 }
185
d5184cac 186 hdrlen = ieee80211_hdrlen(hdr->frame_control);
f0706e82
JB
187 len = skb->len - hdrlen;
188
813d7669 189 if (info->control.hw_key)
23c0752a 190 tail = 0;
11a843b7 191 else
23c0752a
JB
192 tail = TKIP_ICV_LEN;
193
194 if (WARN_ON(skb_tailroom(skb) < tail ||
195 skb_headroom(skb) < TKIP_IV_LEN))
196 return -1;
f0706e82
JB
197
198 pos = skb_push(skb, TKIP_IV_LEN);
199 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
200 pos += hdrlen;
201
202 /* Increase IV for the frame */
523b02ea 203 spin_lock_irqsave(&key->u.tkip.txlock, flags);
b0f76b33
HH
204 key->u.tkip.tx.iv16++;
205 if (key->u.tkip.tx.iv16 == 0)
206 key->u.tkip.tx.iv32++;
523b02ea
JB
207 pos = ieee80211_tkip_add_iv(pos, key);
208 spin_unlock_irqrestore(&key->u.tkip.txlock, flags);
f0706e82 209
813d7669
JB
210 /* hwaccel - with software IV */
211 if (info->control.hw_key)
f0706e82 212 return 0;
f0706e82
JB
213
214 /* Add room for ICV */
215 skb_put(skb, TKIP_ICV_LEN);
216
3473187d 217 return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
523b02ea 218 key, skb, pos, len);
f0706e82
JB
219}
220
221
9ae54c84 222ieee80211_tx_result
5cf121c3 223ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
f0706e82 224{
f0706e82 225 struct sk_buff *skb = tx->skb;
f0706e82 226
5cf121c3 227 ieee80211_tx_set_protected(tx);
f0706e82 228
2de8e0d9
JB
229 do {
230 if (tkip_encrypt_skb(tx, skb) < 0)
231 return TX_DROP;
232 } while ((skb = skb->next));
f0706e82 233
9ae54c84 234 return TX_CONTINUE;
f0706e82
JB
235}
236
237
9ae54c84 238ieee80211_rx_result
5cf121c3 239ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
f0706e82
JB
240{
241 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
747d753d 242 int hdrlen, res, hwaccel = 0;
f0706e82
JB
243 struct ieee80211_key *key = rx->key;
244 struct sk_buff *skb = rx->skb;
eb9fb5b8 245 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
f0706e82 246
d5184cac 247 hdrlen = ieee80211_hdrlen(hdr->frame_control);
f0706e82 248
c34498b9 249 if (!ieee80211_is_data(hdr->frame_control))
9ae54c84 250 return RX_CONTINUE;
f0706e82
JB
251
252 if (!rx->sta || skb->len - hdrlen < 12)
e4c26add 253 return RX_DROP_UNUSABLE;
f0706e82 254
dc1580dd
JB
255 /*
256 * Let TKIP code verify IV, but skip decryption.
257 * In the case where hardware checks the IV as well,
258 * we don't even get here, see ieee80211_rx_h_decrypt()
259 */
260 if (status->flag & RX_FLAG_DECRYPTED)
f0706e82 261 hwaccel = 1;
f0706e82
JB
262
263 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
264 key, skb->data + hdrlen,
17741cdc 265 skb->len - hdrlen, rx->sta->sta.addr,
9ae4fda3 266 hdr->addr1, hwaccel, rx->queue,
5cf121c3
JB
267 &rx->tkip_iv32,
268 &rx->tkip_iv16);
747d753d 269 if (res != TKIP_DECRYPT_OK)
e4c26add 270 return RX_DROP_UNUSABLE;
f0706e82
JB
271
272 /* Trim ICV */
273 skb_trim(skb, skb->len - TKIP_ICV_LEN);
274
275 /* Remove IV */
276 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
277 skb_pull(skb, TKIP_IV_LEN);
278
9ae54c84 279 return RX_CONTINUE;
f0706e82
JB
280}
281
282
feccb466 283static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
f0706e82
JB
284 int encrypted)
285{
f14df804 286 __le16 mask_fc;
fb733336 287 int a4_included, mgmt;
f14df804 288 u8 qos_tid;
feccb466 289 u8 *b_0, *aad;
f14df804
HH
290 u16 data_len, len_a;
291 unsigned int hdrlen;
292 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
f0706e82 293
aba83a0b
JB
294 memset(scratch, 0, 6 * AES_BLOCK_LEN);
295
feccb466
HH
296 b_0 = scratch + 3 * AES_BLOCK_LEN;
297 aad = scratch + 4 * AES_BLOCK_LEN;
298
f14df804 299 /*
fb733336 300 * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
f14df804
HH
301 * Retry, PwrMgt, MoreData; set Protected
302 */
fb733336 303 mgmt = ieee80211_is_mgmt(hdr->frame_control);
f14df804 304 mask_fc = hdr->frame_control;
fb733336 305 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
f14df804 306 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
fb733336
JM
307 if (!mgmt)
308 mask_fc &= ~cpu_to_le16(0x0070);
f14df804
HH
309 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
310
311 hdrlen = ieee80211_hdrlen(hdr->frame_control);
312 len_a = hdrlen - 2;
313 a4_included = ieee80211_has_a4(hdr->frame_control);
f0706e82 314
f14df804
HH
315 if (ieee80211_is_data_qos(hdr->frame_control))
316 qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
317 else
318 qos_tid = 0;
319
320 data_len = skb->len - hdrlen - CCMP_HDR_LEN;
321 if (encrypted)
322 data_len -= CCMP_MIC_LEN;
323
324 /* First block, b_0 */
f0706e82 325 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
fb733336
JM
326 /* Nonce: Nonce Flags | A2 | PN
327 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
328 */
329 b_0[1] = qos_tid | (mgmt << 4);
73e1f7c8 330 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
f0706e82
JB
331 memcpy(&b_0[8], pn, CCMP_PN_LEN);
332 /* l(m) */
f14df804 333 put_unaligned_be16(data_len, &b_0[14]);
f0706e82
JB
334
335 /* AAD (extra authenticate-only data) / masked 802.11 header
336 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
f14df804
HH
337 put_unaligned_be16(len_a, &aad[0]);
338 put_unaligned(mask_fc, (__le16 *)&aad[2]);
73e1f7c8 339 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
f0706e82
JB
340
341 /* Mask Seq#, leave Frag# */
342 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
343 aad[23] = 0;
f14df804 344
f0706e82 345 if (a4_included) {
73e1f7c8 346 memcpy(&aad[24], hdr->addr4, ETH_ALEN);
f14df804 347 aad[30] = qos_tid;
f0706e82 348 aad[31] = 0;
f14df804 349 } else {
73e1f7c8 350 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
f14df804 351 aad[24] = qos_tid;
f0706e82
JB
352 }
353}
354
355
356static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
357{
358 hdr[0] = pn[5];
359 hdr[1] = pn[4];
360 hdr[2] = 0;
361 hdr[3] = 0x20 | (key_id << 6);
362 hdr[4] = pn[3];
363 hdr[5] = pn[2];
364 hdr[6] = pn[1];
365 hdr[7] = pn[0];
366}
367
368
c6a1fa12 369static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
f0706e82
JB
370{
371 pn[0] = hdr[7];
372 pn[1] = hdr[6];
373 pn[2] = hdr[5];
374 pn[3] = hdr[4];
375 pn[4] = hdr[1];
376 pn[5] = hdr[0];
f0706e82
JB
377}
378
379
e039fa4a 380static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
f0706e82
JB
381{
382 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
383 struct ieee80211_key *key = tx->key;
e039fa4a 384 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
23c0752a 385 int hdrlen, len, tail;
aba83a0b
JB
386 u8 *pos;
387 u8 pn[6];
388 u64 pn64;
389 u8 scratch[6 * AES_BLOCK_LEN];
f0706e82 390
813d7669
JB
391 if (info->control.hw_key &&
392 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
393 /*
394 * hwaccel has no need for preallocated room for CCMP
395 * header or MIC fields
396 */
23c0752a 397 return 0;
e039fa4a
JB
398 }
399
d5184cac 400 hdrlen = ieee80211_hdrlen(hdr->frame_control);
f0706e82
JB
401 len = skb->len - hdrlen;
402
813d7669 403 if (info->control.hw_key)
23c0752a 404 tail = 0;
11a843b7 405 else
23c0752a
JB
406 tail = CCMP_MIC_LEN;
407
408 if (WARN_ON(skb_tailroom(skb) < tail ||
409 skb_headroom(skb) < CCMP_HDR_LEN))
410 return -1;
f0706e82
JB
411
412 pos = skb_push(skb, CCMP_HDR_LEN);
413 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
414 hdr = (struct ieee80211_hdr *) pos;
415 pos += hdrlen;
416
aba83a0b 417 pn64 = atomic64_inc_return(&key->u.ccmp.tx_pn);
f0706e82 418
aba83a0b
JB
419 pn[5] = pn64;
420 pn[4] = pn64 >> 8;
421 pn[3] = pn64 >> 16;
422 pn[2] = pn64 >> 24;
423 pn[1] = pn64 >> 32;
424 pn[0] = pn64 >> 40;
f0706e82 425
8f20fc24 426 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
f0706e82 427
813d7669
JB
428 /* hwaccel - with software CCMP header */
429 if (info->control.hw_key)
f0706e82 430 return 0;
f0706e82
JB
431
432 pos += CCMP_HDR_LEN;
aba83a0b
JB
433 ccmp_special_blocks(skb, pn, scratch, 0);
434 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, pos, len,
f0706e82
JB
435 pos, skb_put(skb, CCMP_MIC_LEN));
436
437 return 0;
438}
439
440
9ae54c84 441ieee80211_tx_result
5cf121c3 442ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
f0706e82 443{
f0706e82 444 struct sk_buff *skb = tx->skb;
f0706e82 445
5cf121c3 446 ieee80211_tx_set_protected(tx);
f0706e82 447
2de8e0d9
JB
448 do {
449 if (ccmp_encrypt_skb(tx, skb) < 0)
450 return TX_DROP;
451 } while ((skb = skb->next));
f0706e82 452
9ae54c84 453 return TX_CONTINUE;
f0706e82
JB
454}
455
456
9ae54c84 457ieee80211_rx_result
5cf121c3 458ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
f0706e82 459{
c34498b9 460 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
f0706e82
JB
461 int hdrlen;
462 struct ieee80211_key *key = rx->key;
463 struct sk_buff *skb = rx->skb;
eb9fb5b8 464 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
f0706e82
JB
465 u8 pn[CCMP_PN_LEN];
466 int data_len;
9190252c 467 int queue;
f0706e82 468
d5184cac 469 hdrlen = ieee80211_hdrlen(hdr->frame_control);
f0706e82 470
fb733336
JM
471 if (!ieee80211_is_data(hdr->frame_control) &&
472 !ieee80211_is_robust_mgmt_frame(hdr))
9ae54c84 473 return RX_CONTINUE;
f0706e82
JB
474
475 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
476 if (!rx->sta || data_len < 0)
e4c26add 477 return RX_DROP_UNUSABLE;
f0706e82 478
c6a1fa12 479 ccmp_hdr2pn(pn, skb->data + hdrlen);
f0706e82 480
9190252c
JM
481 queue = ieee80211_is_mgmt(hdr->frame_control) ?
482 NUM_RX_DATA_QUEUES : rx->queue;
483
484 if (memcmp(pn, key->u.ccmp.rx_pn[queue], CCMP_PN_LEN) <= 0) {
f0706e82 485 key->u.ccmp.replays++;
e4c26add 486 return RX_DROP_UNUSABLE;
f0706e82
JB
487 }
488
eb9fb5b8 489 if (!(status->flag & RX_FLAG_DECRYPTED)) {
aba83a0b 490 u8 scratch[6 * AES_BLOCK_LEN];
7848ba7d 491 /* hardware didn't decrypt/verify MIC */
aba83a0b 492 ccmp_special_blocks(skb, pn, scratch, 1);
f0706e82
JB
493
494 if (ieee80211_aes_ccm_decrypt(
aba83a0b 495 key->u.ccmp.tfm, scratch,
f0706e82
JB
496 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
497 skb->data + skb->len - CCMP_MIC_LEN,
c6a1fa12 498 skb->data + hdrlen + CCMP_HDR_LEN))
e4c26add 499 return RX_DROP_UNUSABLE;
f0706e82
JB
500 }
501
9190252c 502 memcpy(key->u.ccmp.rx_pn[queue], pn, CCMP_PN_LEN);
f0706e82
JB
503
504 /* Remove CCMP header and MIC */
505 skb_trim(skb, skb->len - CCMP_MIC_LEN);
506 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
507 skb_pull(skb, CCMP_HDR_LEN);
508
9ae54c84 509 return RX_CONTINUE;
f0706e82 510}
765cb46a
JM
511
512
513static void bip_aad(struct sk_buff *skb, u8 *aad)
514{
515 /* BIP AAD: FC(masked) || A1 || A2 || A3 */
516
517 /* FC type/subtype */
518 aad[0] = skb->data[0];
519 /* Mask FC Retry, PwrMgt, MoreData flags to zero */
520 aad[1] = skb->data[1] & ~(BIT(4) | BIT(5) | BIT(6));
521 /* A1 || A2 || A3 */
522 memcpy(aad + 2, skb->data + 4, 3 * ETH_ALEN);
523}
524
525
526static inline void bip_ipn_swap(u8 *d, const u8 *s)
527{
528 *d++ = s[5];
529 *d++ = s[4];
530 *d++ = s[3];
531 *d++ = s[2];
532 *d++ = s[1];
533 *d = s[0];
534}
535
536
537ieee80211_tx_result
538ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
539{
540 struct sk_buff *skb = tx->skb;
541 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
542 struct ieee80211_key *key = tx->key;
543 struct ieee80211_mmie *mmie;
544 u8 *pn, aad[20];
545 int i;
546
813d7669 547 if (info->control.hw_key)
765cb46a 548 return 0;
765cb46a
JM
549
550 if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
551 return TX_DROP;
552
553 mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
554 mmie->element_id = WLAN_EID_MMIE;
555 mmie->length = sizeof(*mmie) - 2;
556 mmie->key_id = cpu_to_le16(key->conf.keyidx);
557
558 /* PN = PN + 1 */
559 pn = key->u.aes_cmac.tx_pn;
560
561 for (i = sizeof(key->u.aes_cmac.tx_pn) - 1; i >= 0; i--) {
562 pn[i]++;
563 if (pn[i])
564 break;
565 }
566 bip_ipn_swap(mmie->sequence_number, pn);
567
568 bip_aad(skb, aad);
569
570 /*
571 * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
572 */
573 ieee80211_aes_cmac(key->u.aes_cmac.tfm, key->u.aes_cmac.tx_crypto_buf,
574 aad, skb->data + 24, skb->len - 24, mmie->mic);
575
576 return TX_CONTINUE;
577}
578
579
580ieee80211_rx_result
581ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
582{
583 struct sk_buff *skb = rx->skb;
eb9fb5b8 584 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
765cb46a
JM
585 struct ieee80211_key *key = rx->key;
586 struct ieee80211_mmie *mmie;
587 u8 aad[20], mic[8], ipn[6];
588 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
589
590 if (!ieee80211_is_mgmt(hdr->frame_control))
591 return RX_CONTINUE;
592
765cb46a
JM
593 if (skb->len < 24 + sizeof(*mmie))
594 return RX_DROP_UNUSABLE;
595
596 mmie = (struct ieee80211_mmie *)
597 (skb->data + skb->len - sizeof(*mmie));
598 if (mmie->element_id != WLAN_EID_MMIE ||
599 mmie->length != sizeof(*mmie) - 2)
600 return RX_DROP_UNUSABLE; /* Invalid MMIE */
601
602 bip_ipn_swap(ipn, mmie->sequence_number);
603
604 if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
605 key->u.aes_cmac.replays++;
606 return RX_DROP_UNUSABLE;
607 }
608
eb9fb5b8 609 if (!(status->flag & RX_FLAG_DECRYPTED)) {
765cb46a
JM
610 /* hardware didn't decrypt/verify MIC */
611 bip_aad(skb, aad);
612 ieee80211_aes_cmac(key->u.aes_cmac.tfm,
613 key->u.aes_cmac.rx_crypto_buf, aad,
614 skb->data + 24, skb->len - 24, mic);
615 if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
616 key->u.aes_cmac.icverrors++;
617 return RX_DROP_UNUSABLE;
618 }
619 }
620
621 memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
622
623 /* Remove MMIE */
624 skb_trim(skb, skb->len - sizeof(*mmie));
625
626 return RX_CONTINUE;
627}
This page took 0.437499 seconds and 5 git commands to generate.