blob: 241c932d3b6ce6342a6653ea27a3da4c2e1373c8 [file] [log] [blame]
Jiri Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -070016#include <net/mac80211.h>
Johannes Bergeb063c12007-08-28 17:01:53 -040017
Jiri Bencf0706e822007-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
Johannes Berg9ae54c82008-01-31 19:48:20 +010024ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +010025ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
Jiri Bencf0706e822007-05-05 11:45:53 -070026{
Harvey Harrison8e8862b2008-07-02 11:05:35 -070027 u8 *data, *key, *mic, key_offset;
Jiri Bencf0706e822007-05-05 11:45:53 -070028 size_t data_len;
Harvey Harrison8e8862b2008-07-02 11:05:35 -070029 unsigned int hdrlen;
30 struct ieee80211_hdr *hdr;
Jiri Bencf0706e822007-05-05 11:45:53 -070031 u16 fc;
32 struct sk_buff *skb = tx->skb;
33 int authenticator;
34 int wpa_test = 0;
Johannes Berg23c07522008-05-29 10:38:53 +020035 int tail;
Jiri Bencf0706e822007-05-05 11:45:53 -070036
37 fc = tx->fc;
38
Johannes Berg8f20fc22007-08-28 17:01:54 -040039 if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
Jiri Bencf0706e822007-05-05 11:45:53 -070040 !WLAN_FC_DATA_PRESENT(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +010041 return TX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -070042
Harvey Harrison8e8862b2008-07-02 11:05:35 -070043 hdr = (struct ieee80211_hdr *)skb->data;
44 hdrlen = ieee80211_hdrlen(hdr->frame_control);
45 if (skb->len < hdrlen)
Johannes Berg9ae54c82008-01-31 19:48:20 +010046 return TX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -070047
Harvey Harrison8e8862b2008-07-02 11:05:35 -070048 data = skb->data + hdrlen;
49 data_len = skb->len - hdrlen;
50
Johannes Berg11a843b2007-08-28 17:01:55 -040051 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +010052 !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
Johannes Berg7848ba72007-09-14 11:10:25 -040053 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) &&
Jiri Bencf0706e822007-05-05 11:45:53 -070054 !wpa_test) {
55 /* hwaccel - with no need for preallocated room for Michael MIC
56 */
Johannes Berg9ae54c82008-01-31 19:48:20 +010057 return TX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -070058 }
59
Johannes Berg23c07522008-05-29 10:38:53 +020060 tail = MICHAEL_MIC_LEN;
61 if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
62 tail += TKIP_ICV_LEN;
63
64 if (WARN_ON(skb_tailroom(skb) < tail ||
65 skb_headroom(skb) < TKIP_IV_LEN))
66 return TX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -070067
68#if 0
69 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
70#else
71 authenticator = 1;
72#endif
Luis R. Rodriguezffd78912008-06-21 10:02:46 -040073 /* At this point we know we're using ALG_TKIP. To get the MIC key
74 * we now will rely on the offset from the ieee80211_key_conf::key */
75 key_offset = authenticator ?
76 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY :
77 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
78 key = &tx->key->conf.key[key_offset];
Jiri Bencf0706e822007-05-05 11:45:53 -070079 mic = skb_put(skb, MICHAEL_MIC_LEN);
Harvey Harrison8e8862b2008-07-02 11:05:35 -070080 michael_mic(key, hdr, data, data_len, mic);
Jiri Bencf0706e822007-05-05 11:45:53 -070081
Johannes Berg9ae54c82008-01-31 19:48:20 +010082 return TX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -070083}
84
85
Johannes Berg9ae54c82008-01-31 19:48:20 +010086ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +010087ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
Jiri Bencf0706e822007-05-05 11:45:53 -070088{
Harvey Harrison8e8862b2008-07-02 11:05:35 -070089 u8 *data, *key = NULL, key_offset;
Jiri Bencf0706e822007-05-05 11:45:53 -070090 size_t data_len;
Harvey Harrison8e8862b2008-07-02 11:05:35 -070091 unsigned int hdrlen;
92 struct ieee80211_hdr *hdr;
Jiri Bencf0706e822007-05-05 11:45:53 -070093 u16 fc;
94 u8 mic[MICHAEL_MIC_LEN];
95 struct sk_buff *skb = rx->skb;
96 int authenticator = 1, wpa_test = 0;
Joe Perches0795af52007-10-03 17:59:30 -070097 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -070098
99 fc = rx->fc;
100
Johannes Berg3017b802007-08-28 17:01:53 -0400101 /*
102 * No way to verify the MIC if the hardware stripped it
103 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100104 if (rx->status->flag & RX_FLAG_MMIC_STRIPPED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100105 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700106
Johannes Berg8f20fc22007-08-28 17:01:54 -0400107 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
Jiri Bencf0706e822007-05-05 11:45:53 -0700108 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100109 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700110
Harvey Harrison8e8862b2008-07-02 11:05:35 -0700111 hdr = (struct ieee80211_hdr *)skb->data;
112 hdrlen = ieee80211_hdrlen(hdr->frame_control);
113 if (skb->len < hdrlen + MICHAEL_MIC_LEN)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100114 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700115
Harvey Harrison8e8862b2008-07-02 11:05:35 -0700116 data = skb->data + hdrlen;
117 data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
Jiri Bencf0706e822007-05-05 11:45:53 -0700118
119#if 0
120 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
121#else
122 authenticator = 1;
123#endif
Luis R. Rodriguezffd78912008-06-21 10:02:46 -0400124 /* At this point we know we're using ALG_TKIP. To get the MIC key
125 * we now will rely on the offset from the ieee80211_key_conf::key */
126 key_offset = authenticator ?
127 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY :
128 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
129 key = &rx->key->conf.key[key_offset];
Harvey Harrison8e8862b2008-07-02 11:05:35 -0700130 michael_mic(key, hdr, data, data_len, mic);
Jiri Bencf0706e822007-05-05 11:45:53 -0700131 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100132 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +0100133 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700134
Johannes Berg8f20fc22007-08-28 17:01:54 -0400135 mac80211_ev_michael_mic_failure(rx->dev, rx->key->conf.keyidx,
Johannes Bergeb063c12007-08-28 17:01:53 -0400136 (void *) skb->data);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100137 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700138 }
139
Jiri Bencf0706e822007-05-05 11:45:53 -0700140 /* remove Michael MIC from payload */
141 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
142
Johannes Berg50741ae2007-09-26 15:19:45 +0200143 /* update IV in key information to be able to detect replays */
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700144 rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
145 rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
Johannes Berg50741ae2007-09-26 15:19:45 +0200146
Johannes Berg9ae54c82008-01-31 19:48:20 +0100147 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700148}
149
150
Johannes Berge039fa42008-05-15 12:55:29 +0200151static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
Jiri Bencf0706e822007-05-05 11:45:53 -0700152{
153 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
154 struct ieee80211_key *key = tx->key;
Johannes Berge039fa42008-05-15 12:55:29 +0200155 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700156 unsigned int hdrlen;
157 int len, tail;
Jiri Bencf0706e822007-05-05 11:45:53 -0700158 u8 *pos;
159
Johannes Berge039fa42008-05-15 12:55:29 +0200160 info->control.icv_len = TKIP_ICV_LEN;
161 info->control.iv_len = TKIP_IV_LEN;
162
163 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
164 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
165 /* hwaccel - with no need for preallocated room for IV/ICV */
166 info->control.hw_key = &tx->key->conf;
Johannes Berg23c07522008-05-29 10:38:53 +0200167 return 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200168 }
169
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700170 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e822007-05-05 11:45:53 -0700171 len = skb->len - hdrlen;
172
Johannes Berg11a843b2007-08-28 17:01:55 -0400173 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg23c07522008-05-29 10:38:53 +0200174 tail = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400175 else
Johannes Berg23c07522008-05-29 10:38:53 +0200176 tail = TKIP_ICV_LEN;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400177
Johannes Berg23c07522008-05-29 10:38:53 +0200178 if (WARN_ON(skb_tailroom(skb) < tail ||
179 skb_headroom(skb) < TKIP_IV_LEN))
180 return -1;
Jiri Bencf0706e822007-05-05 11:45:53 -0700181
182 pos = skb_push(skb, TKIP_IV_LEN);
183 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
184 pos += hdrlen;
185
186 /* Increase IV for the frame */
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700187 key->u.tkip.tx.iv16++;
188 if (key->u.tkip.tx.iv16 == 0)
189 key->u.tkip.tx.iv32++;
Jiri Bencf0706e822007-05-05 11:45:53 -0700190
Johannes Berg11a843b2007-08-28 17:01:55 -0400191 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700192 /* hwaccel - with preallocated room for IV */
Harvey Harrisonc8012422008-06-11 14:22:00 -0700193 ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
Jiri Bencf0706e822007-05-05 11:45:53 -0700194
Johannes Berge039fa42008-05-15 12:55:29 +0200195 info->control.hw_key = &tx->key->conf;
Jiri Bencf0706e822007-05-05 11:45:53 -0700196 return 0;
197 }
198
199 /* Add room for ICV */
200 skb_put(skb, TKIP_ICV_LEN);
201
202 hdr = (struct ieee80211_hdr *) skb->data;
203 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
204 key, pos, len, hdr->addr2);
205 return 0;
206}
207
208
Johannes Berg9ae54c82008-01-31 19:48:20 +0100209ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100210ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
Jiri Bencf0706e822007-05-05 11:45:53 -0700211{
Jiri Bencf0706e822007-05-05 11:45:53 -0700212 struct sk_buff *skb = tx->skb;
Jiri Bencf0706e822007-05-05 11:45:53 -0700213
Johannes Berg5cf121c2008-02-25 16:27:43 +0100214 ieee80211_tx_set_protected(tx);
Jiri Bencf0706e822007-05-05 11:45:53 -0700215
Johannes Berge039fa42008-05-15 12:55:29 +0200216 if (tkip_encrypt_skb(tx, skb) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100217 return TX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -0700218
Johannes Berg5cf121c2008-02-25 16:27:43 +0100219 if (tx->extra_frag) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700220 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100221 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Berge039fa42008-05-15 12:55:29 +0200222 if (tkip_encrypt_skb(tx, tx->extra_frag[i]) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100223 return TX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -0700224 }
225 }
226
Johannes Berg9ae54c82008-01-31 19:48:20 +0100227 return TX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700228}
229
230
Johannes Berg9ae54c82008-01-31 19:48:20 +0100231ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100232ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
Jiri Bencf0706e822007-05-05 11:45:53 -0700233{
234 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Jiri Bencf0706e822007-05-05 11:45:53 -0700235 int hdrlen, res, hwaccel = 0, wpa_test = 0;
236 struct ieee80211_key *key = rx->key;
237 struct sk_buff *skb = rx->skb;
Joe Perches0795af52007-10-03 17:59:30 -0700238 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -0700239
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700240 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e822007-05-05 11:45:53 -0700241
Johannes Berg4f0d18e2007-09-26 15:19:40 +0200242 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100243 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700244
245 if (!rx->sta || skb->len - hdrlen < 12)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100246 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700247
Johannes Berg5cf121c2008-02-25 16:27:43 +0100248 if (rx->status->flag & RX_FLAG_DECRYPTED) {
249 if (rx->status->flag & RX_FLAG_IV_STRIPPED) {
Johannes Berg7848ba72007-09-14 11:10:25 -0400250 /*
251 * Hardware took care of all processing, including
252 * replay protection, and stripped the ICV/IV so
253 * we cannot do any checks here.
254 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100255 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700256 }
257
258 /* let TKIP code verify IV, but skip decryption */
259 hwaccel = 1;
260 }
261
262 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
263 key, skb->data + hdrlen,
264 skb->len - hdrlen, rx->sta->addr,
Emmanuel Grumbach9ae4fda2008-03-20 15:06:42 +0200265 hdr->addr1, hwaccel, rx->queue,
Johannes Berg5cf121c2008-02-25 16:27:43 +0100266 &rx->tkip_iv32,
267 &rx->tkip_iv16);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200268 if (res != TKIP_DECRYPT_OK || wpa_test)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100269 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700270
271 /* Trim ICV */
272 skb_trim(skb, skb->len - TKIP_ICV_LEN);
273
274 /* Remove IV */
275 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
276 skb_pull(skb, TKIP_IV_LEN);
277
Johannes Berg9ae54c82008-01-31 19:48:20 +0100278 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700279}
280
281
282static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
283 int encrypted)
284{
Harvey Harrisonf14df802008-07-02 11:05:35 -0700285 __le16 mask_fc;
286 int a4_included;
287 u8 qos_tid;
288 u16 data_len, len_a;
289 unsigned int hdrlen;
290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Jiri Bencf0706e822007-05-05 11:45:53 -0700291
Harvey Harrisonf14df802008-07-02 11:05:35 -0700292 /*
293 * Mask FC: zero subtype b4 b5 b6
294 * Retry, PwrMgt, MoreData; set Protected
295 */
296 mask_fc = hdr->frame_control;
297 mask_fc &= ~cpu_to_le16(0x0070 | IEEE80211_FCTL_RETRY |
298 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
299 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
Jiri Bencf0706e822007-05-05 11:45:53 -0700300
Harvey Harrisonf14df802008-07-02 11:05:35 -0700301 hdrlen = ieee80211_hdrlen(hdr->frame_control);
302 len_a = hdrlen - 2;
303 a4_included = ieee80211_has_a4(hdr->frame_control);
304
305 if (ieee80211_is_data_qos(hdr->frame_control))
306 qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
307 else
308 qos_tid = 0;
309
310 data_len = skb->len - hdrlen - CCMP_HDR_LEN;
311 if (encrypted)
312 data_len -= CCMP_MIC_LEN;
313
Jiri Bencf0706e822007-05-05 11:45:53 -0700314 /* First block, b_0 */
Jiri Bencf0706e822007-05-05 11:45:53 -0700315 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
316 /* Nonce: QoS Priority | A2 | PN */
317 b_0[1] = qos_tid;
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700318 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
Jiri Bencf0706e822007-05-05 11:45:53 -0700319 memcpy(&b_0[8], pn, CCMP_PN_LEN);
320 /* l(m) */
Harvey Harrisonf14df802008-07-02 11:05:35 -0700321 put_unaligned_be16(data_len, &b_0[14]);
Jiri Bencf0706e822007-05-05 11:45:53 -0700322
323 /* AAD (extra authenticate-only data) / masked 802.11 header
324 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
Harvey Harrisonf14df802008-07-02 11:05:35 -0700325 put_unaligned_be16(len_a, &aad[0]);
326 put_unaligned(mask_fc, (__le16 *)&aad[2]);
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700327 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
Jiri Bencf0706e822007-05-05 11:45:53 -0700328
329 /* Mask Seq#, leave Frag# */
330 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
331 aad[23] = 0;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700332
Jiri Bencf0706e822007-05-05 11:45:53 -0700333 if (a4_included) {
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700334 memcpy(&aad[24], hdr->addr4, ETH_ALEN);
Harvey Harrisonf14df802008-07-02 11:05:35 -0700335 aad[30] = qos_tid;
Jiri Bencf0706e822007-05-05 11:45:53 -0700336 aad[31] = 0;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700337 } else {
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700338 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
Harvey Harrisonf14df802008-07-02 11:05:35 -0700339 aad[24] = qos_tid;
Jiri Bencf0706e822007-05-05 11:45:53 -0700340 }
341}
342
343
344static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
345{
346 hdr[0] = pn[5];
347 hdr[1] = pn[4];
348 hdr[2] = 0;
349 hdr[3] = 0x20 | (key_id << 6);
350 hdr[4] = pn[3];
351 hdr[5] = pn[2];
352 hdr[6] = pn[1];
353 hdr[7] = pn[0];
354}
355
356
357static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
358{
359 pn[0] = hdr[7];
360 pn[1] = hdr[6];
361 pn[2] = hdr[5];
362 pn[3] = hdr[4];
363 pn[4] = hdr[1];
364 pn[5] = hdr[0];
365 return (hdr[3] >> 6) & 0x03;
366}
367
368
Johannes Berge039fa42008-05-15 12:55:29 +0200369static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
Jiri Bencf0706e822007-05-05 11:45:53 -0700370{
371 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
372 struct ieee80211_key *key = tx->key;
Johannes Berge039fa42008-05-15 12:55:29 +0200373 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg23c07522008-05-29 10:38:53 +0200374 int hdrlen, len, tail;
Jiri Bencf0706e822007-05-05 11:45:53 -0700375 u8 *pos, *pn, *b_0, *aad, *scratch;
376 int i;
377
Johannes Berge039fa42008-05-15 12:55:29 +0200378 info->control.icv_len = CCMP_MIC_LEN;
379 info->control.iv_len = CCMP_HDR_LEN;
380
381 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
382 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
383 /* hwaccel - with no need for preallocated room for CCMP "
384 * header or MIC fields */
385 info->control.hw_key = &tx->key->conf;
Johannes Berg23c07522008-05-29 10:38:53 +0200386 return 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200387 }
388
Jiri Bencf0706e822007-05-05 11:45:53 -0700389 scratch = key->u.ccmp.tx_crypto_buf;
390 b_0 = scratch + 3 * AES_BLOCK_LEN;
391 aad = scratch + 4 * AES_BLOCK_LEN;
392
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700393 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e822007-05-05 11:45:53 -0700394 len = skb->len - hdrlen;
395
Johannes Berg11a843b2007-08-28 17:01:55 -0400396 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg23c07522008-05-29 10:38:53 +0200397 tail = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400398 else
Johannes Berg23c07522008-05-29 10:38:53 +0200399 tail = CCMP_MIC_LEN;
Jiri Bencf0706e822007-05-05 11:45:53 -0700400
Johannes Berg23c07522008-05-29 10:38:53 +0200401 if (WARN_ON(skb_tailroom(skb) < tail ||
402 skb_headroom(skb) < CCMP_HDR_LEN))
403 return -1;
Jiri Bencf0706e822007-05-05 11:45:53 -0700404
405 pos = skb_push(skb, CCMP_HDR_LEN);
406 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
407 hdr = (struct ieee80211_hdr *) pos;
408 pos += hdrlen;
409
410 /* PN = PN + 1 */
411 pn = key->u.ccmp.tx_pn;
412
413 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
414 pn[i]++;
415 if (pn[i])
416 break;
417 }
418
Johannes Berg8f20fc22007-08-28 17:01:54 -0400419 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
Jiri Bencf0706e822007-05-05 11:45:53 -0700420
Johannes Berg11a843b2007-08-28 17:01:55 -0400421 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700422 /* hwaccel - with preallocated room for CCMP header */
Johannes Berge039fa42008-05-15 12:55:29 +0200423 info->control.hw_key = &tx->key->conf;
Jiri Bencf0706e822007-05-05 11:45:53 -0700424 return 0;
425 }
426
427 pos += CCMP_HDR_LEN;
428 ccmp_special_blocks(skb, pn, b_0, aad, 0);
429 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
430 pos, skb_put(skb, CCMP_MIC_LEN));
431
432 return 0;
433}
434
435
Johannes Berg9ae54c82008-01-31 19:48:20 +0100436ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100437ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
Jiri Bencf0706e822007-05-05 11:45:53 -0700438{
Jiri Bencf0706e822007-05-05 11:45:53 -0700439 struct sk_buff *skb = tx->skb;
Jiri Bencf0706e822007-05-05 11:45:53 -0700440
Johannes Berg5cf121c2008-02-25 16:27:43 +0100441 ieee80211_tx_set_protected(tx);
Jiri Bencf0706e822007-05-05 11:45:53 -0700442
Johannes Berge039fa42008-05-15 12:55:29 +0200443 if (ccmp_encrypt_skb(tx, skb) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100444 return TX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -0700445
Johannes Berg5cf121c2008-02-25 16:27:43 +0100446 if (tx->extra_frag) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700447 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100448 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Berge039fa42008-05-15 12:55:29 +0200449 if (ccmp_encrypt_skb(tx, tx->extra_frag[i]) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100450 return TX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -0700451 }
452 }
453
Johannes Berg9ae54c82008-01-31 19:48:20 +0100454 return TX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700455}
456
457
Johannes Berg9ae54c82008-01-31 19:48:20 +0100458ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100459ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
Jiri Bencf0706e822007-05-05 11:45:53 -0700460{
461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Jiri Bencf0706e822007-05-05 11:45:53 -0700462 int hdrlen;
463 struct ieee80211_key *key = rx->key;
464 struct sk_buff *skb = rx->skb;
465 u8 pn[CCMP_PN_LEN];
466 int data_len;
Joe Perches0795af52007-10-03 17:59:30 -0700467 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -0700468
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700469 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e822007-05-05 11:45:53 -0700470
Johannes Berg4f0d18e2007-09-26 15:19:40 +0200471 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100472 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700473
474 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
475 if (!rx->sta || data_len < 0)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100476 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700477
Johannes Berg5cf121c2008-02-25 16:27:43 +0100478 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
479 (rx->status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100480 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700481
482 (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
483
Johannes Berg5cf121c2008-02-25 16:27:43 +0100484 if (memcmp(pn, key->u.ccmp.rx_pn[rx->queue], CCMP_PN_LEN) <= 0) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700485 key->u.ccmp.replays++;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100486 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700487 }
488
Johannes Berg5cf121c2008-02-25 16:27:43 +0100489 if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
Johannes Berg7848ba72007-09-14 11:10:25 -0400490 /* hardware didn't decrypt/verify MIC */
Jiri Bencf0706e822007-05-05 11:45:53 -0700491 u8 *scratch, *b_0, *aad;
492
493 scratch = key->u.ccmp.rx_crypto_buf;
494 b_0 = scratch + 3 * AES_BLOCK_LEN;
495 aad = scratch + 4 * AES_BLOCK_LEN;
496
497 ccmp_special_blocks(skb, pn, b_0, aad, 1);
498
499 if (ieee80211_aes_ccm_decrypt(
500 key->u.ccmp.tfm, scratch, b_0, aad,
501 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
502 skb->data + skb->len - CCMP_MIC_LEN,
503 skb->data + hdrlen + CCMP_HDR_LEN)) {
Johannes Berge4c26ad2008-01-31 19:48:21 +0100504 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700505 }
506 }
507
Johannes Berg5cf121c2008-02-25 16:27:43 +0100508 memcpy(key->u.ccmp.rx_pn[rx->queue], pn, CCMP_PN_LEN);
Jiri Bencf0706e822007-05-05 11:45:53 -0700509
510 /* Remove CCMP header and MIC */
511 skb_trim(skb, skb->len - CCMP_MIC_LEN);
512 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
513 skb_pull(skb, CCMP_HDR_LEN);
514
Johannes Berg9ae54c82008-01-31 19:48:20 +0100515 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700516}