blob: 919e30ce2980649a96c1821d49b201645b4ad291 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2004, Instant802 Networks, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/netdevice.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/skbuff.h>
13#include <linux/compiler.h>
Harvey Harrisonf14df802008-07-02 11:05:35 -070014#include <linux/ieee80211.h>
15#include <asm/unaligned.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070016#include <net/mac80211.h>
Johannes Bergeb063c12007-08-28 17:01:53 -040017
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include "ieee80211_i.h"
19#include "michael.h"
20#include "tkip.h"
21#include "aes_ccm.h"
22#include "wpa.h"
23
24static int ieee80211_get_hdr_info(const struct sk_buff *skb, u8 **sa, u8 **da,
25 u8 *qos_tid, u8 **data, size_t *data_len)
26{
27 struct ieee80211_hdr *hdr;
28 size_t hdrlen;
Harvey Harrisond5184ca2008-06-11 14:21:58 -070029 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -070030
Harvey Harrisond5184ca2008-06-11 14:21:58 -070031 hdr = (struct ieee80211_hdr *)skb->data;
32 fc = hdr->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -070033
Harvey Harrisond5184ca2008-06-11 14:21:58 -070034 hdrlen = ieee80211_hdrlen(fc);
Jiri Bencf0706e82007-05-05 11:45:53 -070035
Harvey Harrisond5184ca2008-06-11 14:21:58 -070036 *sa = ieee80211_get_SA(hdr);
37 *da = ieee80211_get_DA(hdr);
Jiri Bencf0706e82007-05-05 11:45:53 -070038
39 *data = skb->data + hdrlen;
40 *data_len = skb->len - hdrlen;
41
Harvey Harrisond5184ca2008-06-11 14:21:58 -070042 if (ieee80211_is_data_qos(fc))
Harvey Harrison73e1f7c2008-07-02 11:05:34 -070043 *qos_tid = (*ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK) | 0x80;
Harvey Harrisond5184ca2008-06-11 14:21:58 -070044 else
Jiri Bencf0706e82007-05-05 11:45:53 -070045 *qos_tid = 0;
46
47 return skb->len < hdrlen ? -1 : 0;
48}
49
50
Johannes Berg9ae54c82008-01-31 19:48:20 +010051ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +010052ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
Jiri Bencf0706e82007-05-05 11:45:53 -070053{
Luis R. Rodriguezffd78912008-06-21 10:02:46 -040054 u8 *data, *sa, *da, *key, *mic, qos_tid, key_offset;
Jiri Bencf0706e82007-05-05 11:45:53 -070055 size_t data_len;
56 u16 fc;
57 struct sk_buff *skb = tx->skb;
58 int authenticator;
59 int wpa_test = 0;
Johannes Berg23c07522008-05-29 10:38:53 +020060 int tail;
Jiri Bencf0706e82007-05-05 11:45:53 -070061
62 fc = tx->fc;
63
Johannes Berg8f20fc22007-08-28 17:01:54 -040064 if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
Jiri Bencf0706e82007-05-05 11:45:53 -070065 !WLAN_FC_DATA_PRESENT(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +010066 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -070067
68 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len))
Johannes Berg9ae54c82008-01-31 19:48:20 +010069 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -070070
Johannes Berg11a843b2007-08-28 17:01:55 -040071 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +010072 !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
Johannes Berg7848ba72007-09-14 11:10:25 -040073 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) &&
Jiri Bencf0706e82007-05-05 11:45:53 -070074 !wpa_test) {
75 /* hwaccel - with no need for preallocated room for Michael MIC
76 */
Johannes Berg9ae54c82008-01-31 19:48:20 +010077 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -070078 }
79
Johannes Berg23c07522008-05-29 10:38:53 +020080 tail = MICHAEL_MIC_LEN;
81 if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
82 tail += TKIP_ICV_LEN;
83
84 if (WARN_ON(skb_tailroom(skb) < tail ||
85 skb_headroom(skb) < TKIP_IV_LEN))
86 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -070087
88#if 0
89 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
90#else
91 authenticator = 1;
92#endif
Luis R. Rodriguezffd78912008-06-21 10:02:46 -040093 /* At this point we know we're using ALG_TKIP. To get the MIC key
94 * we now will rely on the offset from the ieee80211_key_conf::key */
95 key_offset = authenticator ?
96 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY :
97 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
98 key = &tx->key->conf.key[key_offset];
Jiri Bencf0706e82007-05-05 11:45:53 -070099 mic = skb_put(skb, MICHAEL_MIC_LEN);
100 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
101
Johannes Berg9ae54c82008-01-31 19:48:20 +0100102 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700103}
104
105
Johannes Berg9ae54c82008-01-31 19:48:20 +0100106ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100107ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700108{
Luis R. Rodriguezffd78912008-06-21 10:02:46 -0400109 u8 *data, *sa, *da, *key = NULL, qos_tid, key_offset;
Jiri Bencf0706e82007-05-05 11:45:53 -0700110 size_t data_len;
111 u16 fc;
112 u8 mic[MICHAEL_MIC_LEN];
113 struct sk_buff *skb = rx->skb;
114 int authenticator = 1, wpa_test = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700115 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700116
117 fc = rx->fc;
118
Johannes Berg3017b802007-08-28 17:01:53 -0400119 /*
120 * No way to verify the MIC if the hardware stripped it
121 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100122 if (rx->status->flag & RX_FLAG_MMIC_STRIPPED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100123 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700124
Johannes Berg8f20fc22007-08-28 17:01:54 -0400125 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
Jiri Bencf0706e82007-05-05 11:45:53 -0700126 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100127 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700128
Jiri Bencf0706e82007-05-05 11:45:53 -0700129 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)
130 || data_len < MICHAEL_MIC_LEN)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100131 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700132
133 data_len -= MICHAEL_MIC_LEN;
134
135#if 0
136 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
137#else
138 authenticator = 1;
139#endif
Luis R. Rodriguezffd78912008-06-21 10:02:46 -0400140 /* At this point we know we're using ALG_TKIP. To get the MIC key
141 * we now will rely on the offset from the ieee80211_key_conf::key */
142 key_offset = authenticator ?
143 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY :
144 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
145 key = &rx->key->conf.key[key_offset];
Jiri Bencf0706e82007-05-05 11:45:53 -0700146 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
147 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100148 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +0100149 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700150
Johannes Berg8f20fc22007-08-28 17:01:54 -0400151 mac80211_ev_michael_mic_failure(rx->dev, rx->key->conf.keyidx,
Johannes Bergeb063c12007-08-28 17:01:53 -0400152 (void *) skb->data);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100153 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700154 }
155
Jiri Bencf0706e82007-05-05 11:45:53 -0700156 /* remove Michael MIC from payload */
157 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
158
Johannes Berg50741ae2007-09-26 15:19:45 +0200159 /* update IV in key information to be able to detect replays */
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700160 rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
161 rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
Johannes Berg50741ae2007-09-26 15:19:45 +0200162
Johannes Berg9ae54c82008-01-31 19:48:20 +0100163 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700164}
165
166
Johannes Berge039fa42008-05-15 12:55:29 +0200167static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700168{
169 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
170 struct ieee80211_key *key = tx->key;
Johannes Berge039fa42008-05-15 12:55:29 +0200171 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700172 unsigned int hdrlen;
173 int len, tail;
Jiri Bencf0706e82007-05-05 11:45:53 -0700174 u8 *pos;
175
Johannes Berge039fa42008-05-15 12:55:29 +0200176 info->control.icv_len = TKIP_ICV_LEN;
177 info->control.iv_len = TKIP_IV_LEN;
178
179 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
180 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
181 /* hwaccel - with no need for preallocated room for IV/ICV */
182 info->control.hw_key = &tx->key->conf;
Johannes Berg23c07522008-05-29 10:38:53 +0200183 return 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200184 }
185
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700186 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700187 len = skb->len - hdrlen;
188
Johannes Berg11a843b2007-08-28 17:01:55 -0400189 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg23c07522008-05-29 10:38:53 +0200190 tail = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400191 else
Johannes Berg23c07522008-05-29 10:38:53 +0200192 tail = TKIP_ICV_LEN;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400193
Johannes Berg23c07522008-05-29 10:38:53 +0200194 if (WARN_ON(skb_tailroom(skb) < tail ||
195 skb_headroom(skb) < TKIP_IV_LEN))
196 return -1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700197
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 */
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700203 key->u.tkip.tx.iv16++;
204 if (key->u.tkip.tx.iv16 == 0)
205 key->u.tkip.tx.iv32++;
Jiri Bencf0706e82007-05-05 11:45:53 -0700206
Johannes Berg11a843b2007-08-28 17:01:55 -0400207 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 /* hwaccel - with preallocated room for IV */
Harvey Harrisonc8012422008-06-11 14:22:00 -0700209 ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
Jiri Bencf0706e82007-05-05 11:45:53 -0700210
Johannes Berge039fa42008-05-15 12:55:29 +0200211 info->control.hw_key = &tx->key->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700212 return 0;
213 }
214
215 /* Add room for ICV */
216 skb_put(skb, TKIP_ICV_LEN);
217
218 hdr = (struct ieee80211_hdr *) skb->data;
219 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
220 key, pos, len, hdr->addr2);
221 return 0;
222}
223
224
Johannes Berg9ae54c82008-01-31 19:48:20 +0100225ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100226ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700227{
Jiri Bencf0706e82007-05-05 11:45:53 -0700228 struct sk_buff *skb = tx->skb;
Jiri Bencf0706e82007-05-05 11:45:53 -0700229
Johannes Berg5cf121c2008-02-25 16:27:43 +0100230 ieee80211_tx_set_protected(tx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700231
Johannes Berge039fa42008-05-15 12:55:29 +0200232 if (tkip_encrypt_skb(tx, skb) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100233 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700234
Johannes Berg5cf121c2008-02-25 16:27:43 +0100235 if (tx->extra_frag) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700236 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100237 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Berge039fa42008-05-15 12:55:29 +0200238 if (tkip_encrypt_skb(tx, tx->extra_frag[i]) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100239 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700240 }
241 }
242
Johannes Berg9ae54c82008-01-31 19:48:20 +0100243 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700244}
245
246
Johannes Berg9ae54c82008-01-31 19:48:20 +0100247ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100248ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700249{
250 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Jiri Bencf0706e82007-05-05 11:45:53 -0700251 int hdrlen, res, hwaccel = 0, wpa_test = 0;
252 struct ieee80211_key *key = rx->key;
253 struct sk_buff *skb = rx->skb;
Joe Perches0795af52007-10-03 17:59:30 -0700254 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700255
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700256 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700257
Johannes Berg4f0d18e2007-09-26 15:19:40 +0200258 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100259 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700260
261 if (!rx->sta || skb->len - hdrlen < 12)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100262 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700263
Johannes Berg5cf121c2008-02-25 16:27:43 +0100264 if (rx->status->flag & RX_FLAG_DECRYPTED) {
265 if (rx->status->flag & RX_FLAG_IV_STRIPPED) {
Johannes Berg7848ba72007-09-14 11:10:25 -0400266 /*
267 * Hardware took care of all processing, including
268 * replay protection, and stripped the ICV/IV so
269 * we cannot do any checks here.
270 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100271 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700272 }
273
274 /* let TKIP code verify IV, but skip decryption */
275 hwaccel = 1;
276 }
277
278 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
279 key, skb->data + hdrlen,
280 skb->len - hdrlen, rx->sta->addr,
Emmanuel Grumbach9ae4fda2008-03-20 15:06:42 +0200281 hdr->addr1, hwaccel, rx->queue,
Johannes Berg5cf121c2008-02-25 16:27:43 +0100282 &rx->tkip_iv32,
283 &rx->tkip_iv16);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200284 if (res != TKIP_DECRYPT_OK || wpa_test)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100285 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700286
287 /* Trim ICV */
288 skb_trim(skb, skb->len - TKIP_ICV_LEN);
289
290 /* Remove IV */
291 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
292 skb_pull(skb, TKIP_IV_LEN);
293
Johannes Berg9ae54c82008-01-31 19:48:20 +0100294 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700295}
296
297
298static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
299 int encrypted)
300{
Harvey Harrisonf14df802008-07-02 11:05:35 -0700301 __le16 mask_fc;
302 int a4_included;
303 u8 qos_tid;
304 u16 data_len, len_a;
305 unsigned int hdrlen;
306 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Jiri Bencf0706e82007-05-05 11:45:53 -0700307
Harvey Harrisonf14df802008-07-02 11:05:35 -0700308 /*
309 * Mask FC: zero subtype b4 b5 b6
310 * Retry, PwrMgt, MoreData; set Protected
311 */
312 mask_fc = hdr->frame_control;
313 mask_fc &= ~cpu_to_le16(0x0070 | IEEE80211_FCTL_RETRY |
314 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
315 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700316
Harvey Harrisonf14df802008-07-02 11:05:35 -0700317 hdrlen = ieee80211_hdrlen(hdr->frame_control);
318 len_a = hdrlen - 2;
319 a4_included = ieee80211_has_a4(hdr->frame_control);
320
321 if (ieee80211_is_data_qos(hdr->frame_control))
322 qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
323 else
324 qos_tid = 0;
325
326 data_len = skb->len - hdrlen - CCMP_HDR_LEN;
327 if (encrypted)
328 data_len -= CCMP_MIC_LEN;
329
Jiri Bencf0706e82007-05-05 11:45:53 -0700330 /* First block, b_0 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700331 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
332 /* Nonce: QoS Priority | A2 | PN */
333 b_0[1] = qos_tid;
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700334 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700335 memcpy(&b_0[8], pn, CCMP_PN_LEN);
336 /* l(m) */
Harvey Harrisonf14df802008-07-02 11:05:35 -0700337 put_unaligned_be16(data_len, &b_0[14]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700338
339 /* AAD (extra authenticate-only data) / masked 802.11 header
340 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
Harvey Harrisonf14df802008-07-02 11:05:35 -0700341 put_unaligned_be16(len_a, &aad[0]);
342 put_unaligned(mask_fc, (__le16 *)&aad[2]);
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700343 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700344
345 /* Mask Seq#, leave Frag# */
346 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
347 aad[23] = 0;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700348
Jiri Bencf0706e82007-05-05 11:45:53 -0700349 if (a4_included) {
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700350 memcpy(&aad[24], hdr->addr4, ETH_ALEN);
Harvey Harrisonf14df802008-07-02 11:05:35 -0700351 aad[30] = qos_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -0700352 aad[31] = 0;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700353 } else {
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700354 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
Harvey Harrisonf14df802008-07-02 11:05:35 -0700355 aad[24] = qos_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -0700356 }
357}
358
359
360static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
361{
362 hdr[0] = pn[5];
363 hdr[1] = pn[4];
364 hdr[2] = 0;
365 hdr[3] = 0x20 | (key_id << 6);
366 hdr[4] = pn[3];
367 hdr[5] = pn[2];
368 hdr[6] = pn[1];
369 hdr[7] = pn[0];
370}
371
372
373static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
374{
375 pn[0] = hdr[7];
376 pn[1] = hdr[6];
377 pn[2] = hdr[5];
378 pn[3] = hdr[4];
379 pn[4] = hdr[1];
380 pn[5] = hdr[0];
381 return (hdr[3] >> 6) & 0x03;
382}
383
384
Johannes Berge039fa42008-05-15 12:55:29 +0200385static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700386{
387 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
388 struct ieee80211_key *key = tx->key;
Johannes Berge039fa42008-05-15 12:55:29 +0200389 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg23c07522008-05-29 10:38:53 +0200390 int hdrlen, len, tail;
Jiri Bencf0706e82007-05-05 11:45:53 -0700391 u8 *pos, *pn, *b_0, *aad, *scratch;
392 int i;
393
Johannes Berge039fa42008-05-15 12:55:29 +0200394 info->control.icv_len = CCMP_MIC_LEN;
395 info->control.iv_len = CCMP_HDR_LEN;
396
397 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
398 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
399 /* hwaccel - with no need for preallocated room for CCMP "
400 * header or MIC fields */
401 info->control.hw_key = &tx->key->conf;
Johannes Berg23c07522008-05-29 10:38:53 +0200402 return 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200403 }
404
Jiri Bencf0706e82007-05-05 11:45:53 -0700405 scratch = key->u.ccmp.tx_crypto_buf;
406 b_0 = scratch + 3 * AES_BLOCK_LEN;
407 aad = scratch + 4 * AES_BLOCK_LEN;
408
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700409 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700410 len = skb->len - hdrlen;
411
Johannes Berg11a843b2007-08-28 17:01:55 -0400412 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg23c07522008-05-29 10:38:53 +0200413 tail = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400414 else
Johannes Berg23c07522008-05-29 10:38:53 +0200415 tail = CCMP_MIC_LEN;
Jiri Bencf0706e82007-05-05 11:45:53 -0700416
Johannes Berg23c07522008-05-29 10:38:53 +0200417 if (WARN_ON(skb_tailroom(skb) < tail ||
418 skb_headroom(skb) < CCMP_HDR_LEN))
419 return -1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700420
421 pos = skb_push(skb, CCMP_HDR_LEN);
422 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
423 hdr = (struct ieee80211_hdr *) pos;
424 pos += hdrlen;
425
426 /* PN = PN + 1 */
427 pn = key->u.ccmp.tx_pn;
428
429 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
430 pn[i]++;
431 if (pn[i])
432 break;
433 }
434
Johannes Berg8f20fc22007-08-28 17:01:54 -0400435 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700436
Johannes Berg11a843b2007-08-28 17:01:55 -0400437 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700438 /* hwaccel - with preallocated room for CCMP header */
Johannes Berge039fa42008-05-15 12:55:29 +0200439 info->control.hw_key = &tx->key->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700440 return 0;
441 }
442
443 pos += CCMP_HDR_LEN;
444 ccmp_special_blocks(skb, pn, b_0, aad, 0);
445 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
446 pos, skb_put(skb, CCMP_MIC_LEN));
447
448 return 0;
449}
450
451
Johannes Berg9ae54c82008-01-31 19:48:20 +0100452ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100453ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700454{
Jiri Bencf0706e82007-05-05 11:45:53 -0700455 struct sk_buff *skb = tx->skb;
Jiri Bencf0706e82007-05-05 11:45:53 -0700456
Johannes Berg5cf121c2008-02-25 16:27:43 +0100457 ieee80211_tx_set_protected(tx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700458
Johannes Berge039fa42008-05-15 12:55:29 +0200459 if (ccmp_encrypt_skb(tx, skb) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100460 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700461
Johannes Berg5cf121c2008-02-25 16:27:43 +0100462 if (tx->extra_frag) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700463 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100464 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Berge039fa42008-05-15 12:55:29 +0200465 if (ccmp_encrypt_skb(tx, tx->extra_frag[i]) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100466 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700467 }
468 }
469
Johannes Berg9ae54c82008-01-31 19:48:20 +0100470 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700471}
472
473
Johannes Berg9ae54c82008-01-31 19:48:20 +0100474ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100475ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700476{
477 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Jiri Bencf0706e82007-05-05 11:45:53 -0700478 int hdrlen;
479 struct ieee80211_key *key = rx->key;
480 struct sk_buff *skb = rx->skb;
481 u8 pn[CCMP_PN_LEN];
482 int data_len;
Joe Perches0795af52007-10-03 17:59:30 -0700483 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700484
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700485 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700486
Johannes Berg4f0d18e2007-09-26 15:19:40 +0200487 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100488 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700489
490 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
491 if (!rx->sta || data_len < 0)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100492 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700493
Johannes Berg5cf121c2008-02-25 16:27:43 +0100494 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
495 (rx->status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100496 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700497
498 (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
499
Johannes Berg5cf121c2008-02-25 16:27:43 +0100500 if (memcmp(pn, key->u.ccmp.rx_pn[rx->queue], CCMP_PN_LEN) <= 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700501 key->u.ccmp.replays++;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100502 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700503 }
504
Johannes Berg5cf121c2008-02-25 16:27:43 +0100505 if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
Johannes Berg7848ba72007-09-14 11:10:25 -0400506 /* hardware didn't decrypt/verify MIC */
Jiri Bencf0706e82007-05-05 11:45:53 -0700507 u8 *scratch, *b_0, *aad;
508
509 scratch = key->u.ccmp.rx_crypto_buf;
510 b_0 = scratch + 3 * AES_BLOCK_LEN;
511 aad = scratch + 4 * AES_BLOCK_LEN;
512
513 ccmp_special_blocks(skb, pn, b_0, aad, 1);
514
515 if (ieee80211_aes_ccm_decrypt(
516 key->u.ccmp.tfm, scratch, b_0, aad,
517 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
518 skb->data + skb->len - CCMP_MIC_LEN,
519 skb->data + hdrlen + CCMP_HDR_LEN)) {
Johannes Berge4c26ad2008-01-31 19:48:21 +0100520 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700521 }
522 }
523
Johannes Berg5cf121c2008-02-25 16:27:43 +0100524 memcpy(key->u.ccmp.rx_pn[rx->queue], pn, CCMP_PN_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700525
526 /* Remove CCMP header and MIC */
527 skb_trim(skb, skb->len - CCMP_MIC_LEN);
528 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
529 skb_pull(skb, CCMP_HDR_LEN);
530
Johannes Berg9ae54c82008-01-31 19:48:20 +0100531 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700532}