Skip to content

Commit 5d2cdcd

Browse files
egrumbachlinvjw
authored andcommitted
mac80211: get a TKIP phase key from skb
This patch makes mac80211 able to compute a TKIP key from an skb. The requested key can be a phase 1 or a phase 2 key. This is useful for drivers who need to provide tkip key to their HW to enable HW encryption. Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com> Signed-off-by: Tomas Winkler <tomas.winkler@intel.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent 17e476b commit 5d2cdcd

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎include/net/mac80211.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,21 @@ enum sta_notify_cmd {
643643
STA_NOTIFY_ADD, STA_NOTIFY_REMOVE
644644
};
645645

646+
/**
647+
* enum ieee80211_tkip_key_type - get tkip key
648+
*
649+
* Used by drivers which need to get a tkip key for skb. Some drivers need a
650+
* phase 1 key, others need a phase 2 key. A single function allows the driver
651+
* to get the key, this enum indicates what type of key is required.
652+
*
653+
* @IEEE80211_TKIP_P1_KEY: the driver needs a phase 1 key
654+
* @IEEE80211_TKIP_P2_KEY: the driver needs a phase 2 key
655+
*/
656+
enum ieee80211_tkip_key_type {
657+
IEEE80211_TKIP_P1_KEY,
658+
IEEE80211_TKIP_P2_KEY,
659+
};
660+
646661
/**
647662
* enum ieee80211_hw_flags - hardware flags
648663
*
@@ -1471,6 +1486,21 @@ int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb);
14711486
*/
14721487
int ieee80211_get_hdrlen(u16 fc);
14731488

1489+
/**
1490+
* ieee80211_get_tkip_key - get a TKIP rc4 for skb
1491+
*
1492+
* This function computes a TKIP rc4 key for an skb. It computes
1493+
* a phase 1 key if needed (iv16 wraps around). This function is to
1494+
* be used by drivers which can do HW encryption but need to compute
1495+
* to phase 1/2 key in SW.
1496+
*
1497+
* @keyconf: the parameter passed with the set key
1498+
* @skb: the skb for which the key is needed
1499+
* @rc4key: a buffer to which the key will be written
1500+
*/
1501+
void ieee80211_get_tkip_key(struct ieee80211_key_conf *keyconf,
1502+
struct sk_buff *skb,
1503+
enum ieee80211_tkip_key_type type, u8 *key);
14741504
/**
14751505
* ieee80211_wake_queue - wake specific queue
14761506
* @hw: pointer as obtained from ieee80211_alloc_hw().

‎net/mac80211/tkip.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,59 @@ void ieee80211_tkip_gen_rc4key(struct ieee80211_key *key, u8 *ta,
214214
key->u.tkip.iv16, rc4key);
215215
}
216216

217+
void ieee80211_get_tkip_key(struct ieee80211_key_conf *keyconf,
218+
struct sk_buff *skb, enum ieee80211_tkip_key_type type,
219+
u8 *outkey)
220+
{
221+
struct ieee80211_key *key = (struct ieee80211_key *)
222+
container_of(keyconf, struct ieee80211_key, conf);
223+
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
224+
u8 *data = (u8 *) hdr;
225+
u16 fc = le16_to_cpu(hdr->frame_control);
226+
int hdr_len = ieee80211_get_hdrlen(fc);
227+
u8 *ta = hdr->addr2;
228+
u16 iv16;
229+
u32 iv32;
230+
231+
iv16 = data[hdr_len] << 8;
232+
iv16 += data[hdr_len + 2];
233+
iv32 = data[hdr_len + 4] +
234+
(data[hdr_len + 5] >> 8) +
235+
(data[hdr_len + 6] >> 16) +
236+
(data[hdr_len + 7] >> 24);
237+
238+
#ifdef CONFIG_TKIP_DEBUG
239+
printk(KERN_DEBUG "TKIP encrypt: iv16 = 0x%04x, iv32 = 0x%08x\n",
240+
iv16, iv32);
241+
242+
if (iv32 != key->u.tkip.iv32) {
243+
printk(KERN_DEBUG "skb: iv32 = 0x%08x key: iv32 = 0x%08x\n",
244+
iv32, key->u.tkip.iv32);
245+
printk(KERN_DEBUG "Wrap around of iv16 in the middle of a "
246+
"fragmented packet\n");
247+
}
248+
#endif /* CONFIG_TKIP_DEBUG */
249+
250+
/* Update the p1k only when the iv16 in the packet wraps around, this
251+
* might occur after the wrap around of iv16 in the key in case of
252+
* fragmented packets. */
253+
if (iv16 == 0 || !key->u.tkip.tx_initialized) {
254+
/* IV16 wrapped around - perform TKIP phase 1 */
255+
tkip_mixing_phase1(ta, &key->conf.key[ALG_TKIP_TEMP_ENCR_KEY],
256+
iv32, key->u.tkip.p1k);
257+
key->u.tkip.tx_initialized = 1;
258+
}
259+
260+
if (type == IEEE80211_TKIP_P1_KEY) {
261+
memcpy(outkey, key->u.tkip.p1k, sizeof(u16) * 5);
262+
return;
263+
}
264+
265+
tkip_mixing_phase2(key->u.tkip.p1k,
266+
&key->conf.key[ALG_TKIP_TEMP_ENCR_KEY], iv16, outkey);
267+
}
268+
EXPORT_SYMBOL(ieee80211_get_tkip_key);
269+
217270
/* Encrypt packet payload with TKIP using @key. @pos is a pointer to the
218271
* beginning of the buffer containing payload. This payload must include
219272
* headroom of eight octets for IV and Ext. IV and taildroom of four octets

0 commit comments

Comments
 (0)