blob: 7aa63caf8d50c3c7c8b2b87b77b9a3515fd5a4ca [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
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 Bencf0706e82007-05-05 11:45:53 -070026{
Harvey Harrison8e8862b2008-07-02 11:05:35 -070027 u8 *data, *key, *mic, key_offset;
Jiri Bencf0706e82007-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 Bencf0706e82007-05-05 11:45:53 -070031 struct sk_buff *skb = tx->skb;
32 int authenticator;
33 int wpa_test = 0;
Johannes Berg23c07522008-05-29 10:38:53 +020034 int tail;
Jiri Bencf0706e82007-05-05 11:45:53 -070035
Harvey Harrisonc34498b2008-07-02 16:30:52 -070036 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg8f20fc22007-08-28 17:01:54 -040037 if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
Harvey Harrisonc34498b2008-07-02 16:30:52 -070038 !ieee80211_is_data_present(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +010039 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -070040
Harvey Harrison8e8862b2008-07-02 11:05:35 -070041 hdrlen = ieee80211_hdrlen(hdr->frame_control);
42 if (skb->len < hdrlen)
Johannes Berg9ae54c82008-01-31 19:48:20 +010043 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -070044
Harvey Harrison8e8862b2008-07-02 11:05:35 -070045 data = skb->data + hdrlen;
46 data_len = skb->len - hdrlen;
47
Johannes Berg11a843b2007-08-28 17:01:55 -040048 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +010049 !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
Johannes Berg7848ba72007-09-14 11:10:25 -040050 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) &&
Jiri Bencf0706e82007-05-05 11:45:53 -070051 !wpa_test) {
Johannes Bergc6a1fa12008-10-07 12:04:32 +020052 /* hwaccel - with no need for preallocated room for MMIC */
Johannes Berg9ae54c82008-01-31 19:48:20 +010053 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -070054 }
55
Johannes Berg23c07522008-05-29 10:38:53 +020056 tail = MICHAEL_MIC_LEN;
57 if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
58 tail += TKIP_ICV_LEN;
59
60 if (WARN_ON(skb_tailroom(skb) < tail ||
61 skb_headroom(skb) < TKIP_IV_LEN))
62 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -070063
64#if 0
65 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
66#else
67 authenticator = 1;
68#endif
Luis R. Rodriguezffd78912008-06-21 10:02:46 -040069 key_offset = authenticator ?
70 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY :
71 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
72 key = &tx->key->conf.key[key_offset];
Jiri Bencf0706e82007-05-05 11:45:53 -070073 mic = skb_put(skb, MICHAEL_MIC_LEN);
Harvey Harrison8e8862b2008-07-02 11:05:35 -070074 michael_mic(key, hdr, data, data_len, mic);
Jiri Bencf0706e82007-05-05 11:45:53 -070075
Johannes Berg9ae54c82008-01-31 19:48:20 +010076 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -070077}
78
79
Johannes Berg9ae54c82008-01-31 19:48:20 +010080ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +010081ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
Jiri Bencf0706e82007-05-05 11:45:53 -070082{
Harvey Harrison8e8862b2008-07-02 11:05:35 -070083 u8 *data, *key = NULL, key_offset;
Jiri Bencf0706e82007-05-05 11:45:53 -070084 size_t data_len;
Harvey Harrison8e8862b2008-07-02 11:05:35 -070085 unsigned int hdrlen;
86 struct ieee80211_hdr *hdr;
Jiri Bencf0706e82007-05-05 11:45:53 -070087 u8 mic[MICHAEL_MIC_LEN];
88 struct sk_buff *skb = rx->skb;
89 int authenticator = 1, wpa_test = 0;
90
Johannes Bergc6a1fa12008-10-07 12:04:32 +020091 /* No way to verify the MIC if the hardware stripped it */
Johannes Berg5cf121c2008-02-25 16:27:43 +010092 if (rx->status->flag & RX_FLAG_MMIC_STRIPPED)
Johannes Berg9ae54c82008-01-31 19:48:20 +010093 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -070094
Harvey Harrisonc34498b2008-07-02 16:30:52 -070095 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg8f20fc22007-08-28 17:01:54 -040096 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
Harvey Harrisonc34498b2008-07-02 16:30:52 -070097 !ieee80211_has_protected(hdr->frame_control) ||
98 !ieee80211_is_data_present(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +010099 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700100
Harvey Harrison8e8862b2008-07-02 11:05:35 -0700101 hdrlen = ieee80211_hdrlen(hdr->frame_control);
102 if (skb->len < hdrlen + MICHAEL_MIC_LEN)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100103 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700104
Harvey Harrison8e8862b2008-07-02 11:05:35 -0700105 data = skb->data + hdrlen;
106 data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
Jiri Bencf0706e82007-05-05 11:45:53 -0700107
108#if 0
109 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
110#else
111 authenticator = 1;
112#endif
Luis R. Rodriguezffd78912008-06-21 10:02:46 -0400113 key_offset = authenticator ?
114 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY :
115 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
116 key = &rx->key->conf.key[key_offset];
Harvey Harrison8e8862b2008-07-02 11:05:35 -0700117 michael_mic(key, hdr, data, data_len, mic);
Jiri Bencf0706e82007-05-05 11:45:53 -0700118 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100119 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +0100120 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700121
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200122 mac80211_ev_michael_mic_failure(rx->sdata, rx->key->conf.keyidx,
Johannes Bergeb063c12007-08-28 17:01:53 -0400123 (void *) skb->data);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100124 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700125 }
126
Jiri Bencf0706e82007-05-05 11:45:53 -0700127 /* remove Michael MIC from payload */
128 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
129
Johannes Berg50741ae2007-09-26 15:19:45 +0200130 /* update IV in key information to be able to detect replays */
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700131 rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
132 rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
Johannes Berg50741ae2007-09-26 15:19:45 +0200133
Johannes Berg9ae54c82008-01-31 19:48:20 +0100134 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700135}
136
137
Johannes Berge039fa42008-05-15 12:55:29 +0200138static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700139{
140 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
141 struct ieee80211_key *key = tx->key;
Johannes Berge039fa42008-05-15 12:55:29 +0200142 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700143 unsigned int hdrlen;
144 int len, tail;
Jiri Bencf0706e82007-05-05 11:45:53 -0700145 u8 *pos;
146
Johannes Berge039fa42008-05-15 12:55:29 +0200147 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
148 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
149 /* hwaccel - with no need for preallocated room for IV/ICV */
150 info->control.hw_key = &tx->key->conf;
Johannes Berg23c07522008-05-29 10:38:53 +0200151 return 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200152 }
153
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700154 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700155 len = skb->len - hdrlen;
156
Johannes Berg11a843b2007-08-28 17:01:55 -0400157 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg23c07522008-05-29 10:38:53 +0200158 tail = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400159 else
Johannes Berg23c07522008-05-29 10:38:53 +0200160 tail = TKIP_ICV_LEN;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400161
Johannes Berg23c07522008-05-29 10:38:53 +0200162 if (WARN_ON(skb_tailroom(skb) < tail ||
163 skb_headroom(skb) < TKIP_IV_LEN))
164 return -1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700165
166 pos = skb_push(skb, TKIP_IV_LEN);
167 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
168 pos += hdrlen;
169
170 /* Increase IV for the frame */
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700171 key->u.tkip.tx.iv16++;
172 if (key->u.tkip.tx.iv16 == 0)
173 key->u.tkip.tx.iv32++;
Jiri Bencf0706e82007-05-05 11:45:53 -0700174
Johannes Berg11a843b2007-08-28 17:01:55 -0400175 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700176 /* hwaccel - with preallocated room for IV */
Harvey Harrisonc8012422008-06-11 14:22:00 -0700177 ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
Jiri Bencf0706e82007-05-05 11:45:53 -0700178
Johannes Berge039fa42008-05-15 12:55:29 +0200179 info->control.hw_key = &tx->key->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700180 return 0;
181 }
182
183 /* Add room for ICV */
184 skb_put(skb, TKIP_ICV_LEN);
185
186 hdr = (struct ieee80211_hdr *) skb->data;
187 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
188 key, pos, len, hdr->addr2);
189 return 0;
190}
191
192
Johannes Berg9ae54c82008-01-31 19:48:20 +0100193ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100194ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700195{
Jiri Bencf0706e82007-05-05 11:45:53 -0700196 struct sk_buff *skb = tx->skb;
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200197 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700198
Johannes Berg5cf121c2008-02-25 16:27:43 +0100199 ieee80211_tx_set_protected(tx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700200
Johannes Berge039fa42008-05-15 12:55:29 +0200201 if (tkip_encrypt_skb(tx, skb) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100202 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700203
Johannes Berg5cf121c2008-02-25 16:27:43 +0100204 if (tx->extra_frag) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100205 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200206 if (tkip_encrypt_skb(tx, tx->extra_frag[i]))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100207 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 }
209 }
210
Johannes Berg9ae54c82008-01-31 19:48:20 +0100211 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700212}
213
214
Johannes Berg9ae54c82008-01-31 19:48:20 +0100215ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100216ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700217{
218 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Jiri Bencf0706e82007-05-05 11:45:53 -0700219 int hdrlen, res, hwaccel = 0, wpa_test = 0;
220 struct ieee80211_key *key = rx->key;
221 struct sk_buff *skb = rx->skb;
222
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700223 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700224
Harvey Harrisonc34498b2008-07-02 16:30:52 -0700225 if (!ieee80211_is_data(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100226 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700227
228 if (!rx->sta || skb->len - hdrlen < 12)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100229 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700230
Johannes Berg5cf121c2008-02-25 16:27:43 +0100231 if (rx->status->flag & RX_FLAG_DECRYPTED) {
232 if (rx->status->flag & RX_FLAG_IV_STRIPPED) {
Johannes Berg7848ba72007-09-14 11:10:25 -0400233 /*
234 * Hardware took care of all processing, including
235 * replay protection, and stripped the ICV/IV so
236 * we cannot do any checks here.
237 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100238 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700239 }
240
241 /* let TKIP code verify IV, but skip decryption */
242 hwaccel = 1;
243 }
244
245 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
246 key, skb->data + hdrlen,
Johannes Berg17741cd2008-09-11 00:02:02 +0200247 skb->len - hdrlen, rx->sta->sta.addr,
Emmanuel Grumbach9ae4fda2008-03-20 15:06:42 +0200248 hdr->addr1, hwaccel, rx->queue,
Johannes Berg5cf121c2008-02-25 16:27:43 +0100249 &rx->tkip_iv32,
250 &rx->tkip_iv16);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200251 if (res != TKIP_DECRYPT_OK || wpa_test)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100252 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700253
254 /* Trim ICV */
255 skb_trim(skb, skb->len - TKIP_ICV_LEN);
256
257 /* Remove IV */
258 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
259 skb_pull(skb, TKIP_IV_LEN);
260
Johannes Berg9ae54c82008-01-31 19:48:20 +0100261 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700262}
263
264
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700265static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
Jiri Bencf0706e82007-05-05 11:45:53 -0700266 int encrypted)
267{
Harvey Harrisonf14df802008-07-02 11:05:35 -0700268 __le16 mask_fc;
269 int a4_included;
270 u8 qos_tid;
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700271 u8 *b_0, *aad;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700272 u16 data_len, len_a;
273 unsigned int hdrlen;
274 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Jiri Bencf0706e82007-05-05 11:45:53 -0700275
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700276 b_0 = scratch + 3 * AES_BLOCK_LEN;
277 aad = scratch + 4 * AES_BLOCK_LEN;
278
Harvey Harrisonf14df802008-07-02 11:05:35 -0700279 /*
280 * Mask FC: zero subtype b4 b5 b6
281 * Retry, PwrMgt, MoreData; set Protected
282 */
283 mask_fc = hdr->frame_control;
284 mask_fc &= ~cpu_to_le16(0x0070 | IEEE80211_FCTL_RETRY |
285 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
286 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700287
Harvey Harrisonf14df802008-07-02 11:05:35 -0700288 hdrlen = ieee80211_hdrlen(hdr->frame_control);
289 len_a = hdrlen - 2;
290 a4_included = ieee80211_has_a4(hdr->frame_control);
291
292 if (ieee80211_is_data_qos(hdr->frame_control))
293 qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
294 else
295 qos_tid = 0;
296
297 data_len = skb->len - hdrlen - CCMP_HDR_LEN;
298 if (encrypted)
299 data_len -= CCMP_MIC_LEN;
300
Jiri Bencf0706e82007-05-05 11:45:53 -0700301 /* First block, b_0 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700302 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
303 /* Nonce: QoS Priority | A2 | PN */
304 b_0[1] = qos_tid;
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700305 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700306 memcpy(&b_0[8], pn, CCMP_PN_LEN);
307 /* l(m) */
Harvey Harrisonf14df802008-07-02 11:05:35 -0700308 put_unaligned_be16(data_len, &b_0[14]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700309
310 /* AAD (extra authenticate-only data) / masked 802.11 header
311 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
Harvey Harrisonf14df802008-07-02 11:05:35 -0700312 put_unaligned_be16(len_a, &aad[0]);
313 put_unaligned(mask_fc, (__le16 *)&aad[2]);
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700314 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700315
316 /* Mask Seq#, leave Frag# */
317 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
318 aad[23] = 0;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700319
Jiri Bencf0706e82007-05-05 11:45:53 -0700320 if (a4_included) {
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700321 memcpy(&aad[24], hdr->addr4, ETH_ALEN);
Harvey Harrisonf14df802008-07-02 11:05:35 -0700322 aad[30] = qos_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -0700323 aad[31] = 0;
Harvey Harrisonf14df802008-07-02 11:05:35 -0700324 } else {
Harvey Harrison73e1f7c2008-07-02 11:05:34 -0700325 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
Harvey Harrisonf14df802008-07-02 11:05:35 -0700326 aad[24] = qos_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -0700327 }
328}
329
330
331static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
332{
333 hdr[0] = pn[5];
334 hdr[1] = pn[4];
335 hdr[2] = 0;
336 hdr[3] = 0x20 | (key_id << 6);
337 hdr[4] = pn[3];
338 hdr[5] = pn[2];
339 hdr[6] = pn[1];
340 hdr[7] = pn[0];
341}
342
343
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200344static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
Jiri Bencf0706e82007-05-05 11:45:53 -0700345{
346 pn[0] = hdr[7];
347 pn[1] = hdr[6];
348 pn[2] = hdr[5];
349 pn[3] = hdr[4];
350 pn[4] = hdr[1];
351 pn[5] = hdr[0];
Jiri Bencf0706e82007-05-05 11:45:53 -0700352}
353
354
Johannes Berge039fa42008-05-15 12:55:29 +0200355static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700356{
357 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
358 struct ieee80211_key *key = tx->key;
Johannes Berge039fa42008-05-15 12:55:29 +0200359 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg23c07522008-05-29 10:38:53 +0200360 int hdrlen, len, tail;
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700361 u8 *pos, *pn;
Jiri Bencf0706e82007-05-05 11:45:53 -0700362 int i;
363
Johannes Berge039fa42008-05-15 12:55:29 +0200364 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
365 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200366 /* hwaccel - with no need for preallocated room for CCMP
Johannes Berge039fa42008-05-15 12:55:29 +0200367 * header or MIC fields */
368 info->control.hw_key = &tx->key->conf;
Johannes Berg23c07522008-05-29 10:38:53 +0200369 return 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200370 }
371
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700372 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700373 len = skb->len - hdrlen;
374
Johannes Berg11a843b2007-08-28 17:01:55 -0400375 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg23c07522008-05-29 10:38:53 +0200376 tail = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400377 else
Johannes Berg23c07522008-05-29 10:38:53 +0200378 tail = CCMP_MIC_LEN;
Jiri Bencf0706e82007-05-05 11:45:53 -0700379
Johannes Berg23c07522008-05-29 10:38:53 +0200380 if (WARN_ON(skb_tailroom(skb) < tail ||
381 skb_headroom(skb) < CCMP_HDR_LEN))
382 return -1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700383
384 pos = skb_push(skb, CCMP_HDR_LEN);
385 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
386 hdr = (struct ieee80211_hdr *) pos;
387 pos += hdrlen;
388
389 /* PN = PN + 1 */
390 pn = key->u.ccmp.tx_pn;
391
392 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
393 pn[i]++;
394 if (pn[i])
395 break;
396 }
397
Johannes Berg8f20fc22007-08-28 17:01:54 -0400398 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700399
Johannes Berg11a843b2007-08-28 17:01:55 -0400400 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700401 /* hwaccel - with preallocated room for CCMP header */
Johannes Berge039fa42008-05-15 12:55:29 +0200402 info->control.hw_key = &tx->key->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700403 return 0;
404 }
405
406 pos += CCMP_HDR_LEN;
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700407 ccmp_special_blocks(skb, pn, key->u.ccmp.tx_crypto_buf, 0);
408 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, key->u.ccmp.tx_crypto_buf, pos, len,
Jiri Bencf0706e82007-05-05 11:45:53 -0700409 pos, skb_put(skb, CCMP_MIC_LEN));
410
411 return 0;
412}
413
414
Johannes Berg9ae54c82008-01-31 19:48:20 +0100415ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100416ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700417{
Jiri Bencf0706e82007-05-05 11:45:53 -0700418 struct sk_buff *skb = tx->skb;
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200419 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700420
Johannes Berg5cf121c2008-02-25 16:27:43 +0100421 ieee80211_tx_set_protected(tx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700422
Johannes Berge039fa42008-05-15 12:55:29 +0200423 if (ccmp_encrypt_skb(tx, skb) < 0)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100424 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700425
Johannes Berg5cf121c2008-02-25 16:27:43 +0100426 if (tx->extra_frag) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100427 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200428 if (ccmp_encrypt_skb(tx, tx->extra_frag[i]))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100429 return TX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700430 }
431 }
432
Johannes Berg9ae54c82008-01-31 19:48:20 +0100433 return TX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700434}
435
436
Johannes Berg9ae54c82008-01-31 19:48:20 +0100437ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100438ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
Jiri Bencf0706e82007-05-05 11:45:53 -0700439{
Harvey Harrisonc34498b2008-07-02 16:30:52 -0700440 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jiri Bencf0706e82007-05-05 11:45:53 -0700441 int hdrlen;
442 struct ieee80211_key *key = rx->key;
443 struct sk_buff *skb = rx->skb;
444 u8 pn[CCMP_PN_LEN];
445 int data_len;
446
Harvey Harrisond5184ca2008-06-11 14:21:58 -0700447 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Jiri Bencf0706e82007-05-05 11:45:53 -0700448
Harvey Harrisonc34498b2008-07-02 16:30:52 -0700449 if (!ieee80211_is_data(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100450 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700451
452 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
453 if (!rx->sta || data_len < 0)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100454 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700455
Johannes Berg5cf121c2008-02-25 16:27:43 +0100456 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
457 (rx->status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100458 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700459
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200460 ccmp_hdr2pn(pn, skb->data + hdrlen);
Jiri Bencf0706e82007-05-05 11:45:53 -0700461
Johannes Berg5cf121c2008-02-25 16:27:43 +0100462 if (memcmp(pn, key->u.ccmp.rx_pn[rx->queue], CCMP_PN_LEN) <= 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700463 key->u.ccmp.replays++;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100464 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700465 }
466
Johannes Berg5cf121c2008-02-25 16:27:43 +0100467 if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
Johannes Berg7848ba72007-09-14 11:10:25 -0400468 /* hardware didn't decrypt/verify MIC */
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700469 ccmp_special_blocks(skb, pn, key->u.ccmp.rx_crypto_buf, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -0700470
471 if (ieee80211_aes_ccm_decrypt(
Harvey Harrisonfeccb462008-07-02 16:30:52 -0700472 key->u.ccmp.tfm, key->u.ccmp.rx_crypto_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -0700473 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
474 skb->data + skb->len - CCMP_MIC_LEN,
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200475 skb->data + hdrlen + CCMP_HDR_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +0100476 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700477 }
478
Johannes Berg5cf121c2008-02-25 16:27:43 +0100479 memcpy(key->u.ccmp.rx_pn[rx->queue], pn, CCMP_PN_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700480
481 /* Remove CCMP header and MIC */
482 skb_trim(skb, skb->len - CCMP_MIC_LEN);
483 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
484 skb_pull(skb, CCMP_HDR_LEN);
485
Johannes Berg9ae54c82008-01-31 19:48:20 +0100486 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700487}