libata: Implement disk shock protection support
[safe/jmp/linux-2.6] / net / ieee80211 / ieee80211_crypt_tkip.c
index 93def94..bba0152 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Host AP crypt: host-based TKIP encryption implementation for Host AP driver
  *
- * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
+ * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -9,13 +9,15 @@
  * more details.
  */
 
-#include <linux/config.h>
+#include <linux/err.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/random.h>
+#include <linux/scatterlist.h>
 #include <linux/skbuff.h>
 #include <linux/netdevice.h>
+#include <linux/mm.h>
 #include <linux/if_ether.h>
 #include <linux/if_arp.h>
 #include <asm/string.h>
@@ -23,7 +25,6 @@
 #include <net/ieee80211.h>
 
 #include <linux/crypto.h>
-#include <asm/scatterlist.h>
 #include <linux/crc32.h>
 
 MODULE_AUTHOR("Jouni Malinen");
@@ -53,8 +54,10 @@ struct ieee80211_tkip_data {
 
        int key_idx;
 
-       struct crypto_tfm *tfm_arc4;
-       struct crypto_tfm *tfm_michael;
+       struct crypto_blkcipher *rx_tfm_arc4;
+       struct crypto_hash *rx_tfm_michael;
+       struct crypto_blkcipher *tx_tfm_arc4;
+       struct crypto_hash *tx_tfm_michael;
 
        /* scratch buffers for virt_to_page() (crypto API) */
        u8 rx_hdr[16], tx_hdr[16];
@@ -86,17 +89,39 @@ static void *ieee80211_tkip_init(int key_idx)
 
        priv->key_idx = key_idx;
 
-       priv->tfm_arc4 = crypto_alloc_tfm("arc4", 0);
-       if (priv->tfm_arc4 == NULL) {
+       priv->tx_tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0,
+                                               CRYPTO_ALG_ASYNC);
+       if (IS_ERR(priv->tx_tfm_arc4)) {
                printk(KERN_DEBUG "ieee80211_crypt_tkip: could not allocate "
                       "crypto API arc4\n");
+               priv->tx_tfm_arc4 = NULL;
                goto fail;
        }
 
-       priv->tfm_michael = crypto_alloc_tfm("michael_mic", 0);
-       if (priv->tfm_michael == NULL) {
+       priv->tx_tfm_michael = crypto_alloc_hash("michael_mic", 0,
+                                                CRYPTO_ALG_ASYNC);
+       if (IS_ERR(priv->tx_tfm_michael)) {
                printk(KERN_DEBUG "ieee80211_crypt_tkip: could not allocate "
                       "crypto API michael_mic\n");
+               priv->tx_tfm_michael = NULL;
+               goto fail;
+       }
+
+       priv->rx_tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0,
+                                               CRYPTO_ALG_ASYNC);
+       if (IS_ERR(priv->rx_tfm_arc4)) {
+               printk(KERN_DEBUG "ieee80211_crypt_tkip: could not allocate "
+                      "crypto API arc4\n");
+               priv->rx_tfm_arc4 = NULL;
+               goto fail;
+       }
+
+       priv->rx_tfm_michael = crypto_alloc_hash("michael_mic", 0,
+                                                CRYPTO_ALG_ASYNC);
+       if (IS_ERR(priv->rx_tfm_michael)) {
+               printk(KERN_DEBUG "ieee80211_crypt_tkip: could not allocate "
+                      "crypto API michael_mic\n");
+               priv->rx_tfm_michael = NULL;
                goto fail;
        }
 
@@ -104,10 +129,14 @@ static void *ieee80211_tkip_init(int key_idx)
 
       fail:
        if (priv) {
-               if (priv->tfm_michael)
-                       crypto_free_tfm(priv->tfm_michael);
-               if (priv->tfm_arc4)
-                       crypto_free_tfm(priv->tfm_arc4);
+               if (priv->tx_tfm_michael)
+                       crypto_free_hash(priv->tx_tfm_michael);
+               if (priv->tx_tfm_arc4)
+                       crypto_free_blkcipher(priv->tx_tfm_arc4);
+               if (priv->rx_tfm_michael)
+                       crypto_free_hash(priv->rx_tfm_michael);
+               if (priv->rx_tfm_arc4)
+                       crypto_free_blkcipher(priv->rx_tfm_arc4);
                kfree(priv);
        }
 
@@ -117,10 +146,16 @@ static void *ieee80211_tkip_init(int key_idx)
 static void ieee80211_tkip_deinit(void *priv)
 {
        struct ieee80211_tkip_data *_priv = priv;
-       if (_priv && _priv->tfm_michael)
-               crypto_free_tfm(_priv->tfm_michael);
-       if (_priv && _priv->tfm_arc4)
-               crypto_free_tfm(_priv->tfm_arc4);
+       if (_priv) {
+               if (_priv->tx_tfm_michael)
+                       crypto_free_hash(_priv->tx_tfm_michael);
+               if (_priv->tx_tfm_arc4)
+                       crypto_free_blkcipher(_priv->tx_tfm_arc4);
+               if (_priv->rx_tfm_michael)
+                       crypto_free_hash(_priv->rx_tfm_michael);
+               if (_priv->rx_tfm_arc4)
+                       crypto_free_blkcipher(_priv->rx_tfm_arc4);
+       }
        kfree(priv);
 }
 
@@ -154,7 +189,7 @@ static inline u16 Mk16(u8 hi, u8 lo)
        return lo | (((u16) hi) << 8);
 }
 
-static inline u16 Mk16_le(u16 * v)
+static inline u16 Mk16_le(__le16 * v)
 {
        return le16_to_cpu(*v);
 }
@@ -240,15 +275,15 @@ static void tkip_mixing_phase2(u8 * WEPSeed, const u8 * TK, const u16 * TTAK,
        PPK[5] = TTAK[4] + IV16;
 
        /* Step 2 - 96-bit bijective mixing using S-box */
-       PPK[0] += _S_(PPK[5] ^ Mk16_le((u16 *) & TK[0]));
-       PPK[1] += _S_(PPK[0] ^ Mk16_le((u16 *) & TK[2]));
-       PPK[2] += _S_(PPK[1] ^ Mk16_le((u16 *) & TK[4]));
-       PPK[3] += _S_(PPK[2] ^ Mk16_le((u16 *) & TK[6]));
-       PPK[4] += _S_(PPK[3] ^ Mk16_le((u16 *) & TK[8]));
-       PPK[5] += _S_(PPK[4] ^ Mk16_le((u16 *) & TK[10]));
-
-       PPK[0] += RotR1(PPK[5] ^ Mk16_le((u16 *) & TK[12]));
-       PPK[1] += RotR1(PPK[0] ^ Mk16_le((u16 *) & TK[14]));
+       PPK[0] += _S_(PPK[5] ^ Mk16_le((__le16 *) & TK[0]));
+       PPK[1] += _S_(PPK[0] ^ Mk16_le((__le16 *) & TK[2]));
+       PPK[2] += _S_(PPK[1] ^ Mk16_le((__le16 *) & TK[4]));
+       PPK[3] += _S_(PPK[2] ^ Mk16_le((__le16 *) & TK[6]));
+       PPK[4] += _S_(PPK[3] ^ Mk16_le((__le16 *) & TK[8]));
+       PPK[5] += _S_(PPK[4] ^ Mk16_le((__le16 *) & TK[10]));
+
+       PPK[0] += RotR1(PPK[5] ^ Mk16_le((__le16 *) & TK[12]));
+       PPK[1] += RotR1(PPK[0] ^ Mk16_le((__le16 *) & TK[14]));
        PPK[2] += RotR1(PPK[1]);
        PPK[3] += RotR1(PPK[2]);
        PPK[4] += RotR1(PPK[3]);
@@ -259,7 +294,7 @@ static void tkip_mixing_phase2(u8 * WEPSeed, const u8 * TK, const u16 * TTAK,
        WEPSeed[0] = Hi8(IV16);
        WEPSeed[1] = (Hi8(IV16) | 0x20) & 0x7F;
        WEPSeed[2] = Lo8(IV16);
-       WEPSeed[3] = Lo8((PPK[5] ^ Mk16_le((u16 *) & TK[0])) >> 1);
+       WEPSeed[3] = Lo8((PPK[5] ^ Mk16_le((__le16 *) & TK[0])) >> 1);
 
 #ifdef __BIG_ENDIAN
        {
@@ -319,18 +354,20 @@ static int ieee80211_tkip_hdr(struct sk_buff *skb, int hdr_len,
 static int ieee80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
 {
        struct ieee80211_tkip_data *tkey = priv;
+       struct blkcipher_desc desc = { .tfm = tkey->tx_tfm_arc4 };
        int len;
        u8 rc4key[16], *pos, *icv;
        u32 crc;
        struct scatterlist sg;
+       DECLARE_MAC_BUF(mac);
 
        if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) {
                if (net_ratelimit()) {
                        struct ieee80211_hdr_4addr *hdr =
                            (struct ieee80211_hdr_4addr *)skb->data;
                        printk(KERN_DEBUG ": TKIP countermeasures: dropped "
-                              "TX packet to " MAC_FMT "\n",
-                              MAC_ARG(hdr->addr1));
+                              "TX packet to %s\n",
+                              print_mac(mac, hdr->addr1));
                }
                return -1;
        }
@@ -352,18 +389,28 @@ static int ieee80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
        icv[2] = crc >> 16;
        icv[3] = crc >> 24;
 
-       crypto_cipher_setkey(tkey->tfm_arc4, rc4key, 16);
-       sg.page = virt_to_page(pos);
-       sg.offset = offset_in_page(pos);
-       sg.length = len + 4;
-       crypto_cipher_encrypt(tkey->tfm_arc4, &sg, &sg, len + 4);
+       crypto_blkcipher_setkey(tkey->tx_tfm_arc4, rc4key, 16);
+       sg_init_one(&sg, pos, len + 4);
+       return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
+}
 
+/*
+ * deal with seq counter wrapping correctly.
+ * refer to timer_after() for jiffies wrapping handling
+ */
+static inline int tkip_replay_check(u32 iv32_n, u16 iv16_n,
+                                   u32 iv32_o, u16 iv16_o)
+{
+       if ((s32)iv32_n - (s32)iv32_o < 0 ||
+           (iv32_n == iv32_o && iv16_n <= iv16_o))
+               return 1;
        return 0;
 }
 
 static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
 {
        struct ieee80211_tkip_data *tkey = priv;
+       struct blkcipher_desc desc = { .tfm = tkey->rx_tfm_arc4 };
        u8 rc4key[16];
        u8 keyidx, *pos;
        u32 iv32;
@@ -373,14 +420,15 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
        u32 crc;
        struct scatterlist sg;
        int plen;
+       DECLARE_MAC_BUF(mac);
 
        hdr = (struct ieee80211_hdr_4addr *)skb->data;
 
        if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) {
                if (net_ratelimit()) {
                        printk(KERN_DEBUG ": TKIP countermeasures: dropped "
-                              "received packet from " MAC_FMT "\n",
-                              MAC_ARG(hdr->addr2));
+                              "received packet from %s\n",
+                              print_mac(mac, hdr->addr2));
                }
                return -1;
        }
@@ -393,7 +441,7 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
        if (!(keyidx & (1 << 5))) {
                if (net_ratelimit()) {
                        printk(KERN_DEBUG "TKIP: received packet without ExtIV"
-                              " flag from " MAC_FMT "\n", MAC_ARG(hdr->addr2));
+                              " flag from %s\n", print_mac(mac, hdr->addr2));
                }
                return -2;
        }
@@ -405,9 +453,9 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
        }
        if (!tkey->key_set) {
                if (net_ratelimit()) {
-                       printk(KERN_DEBUG "TKIP: received packet from " MAC_FMT
+                       printk(KERN_DEBUG "TKIP: received packet from %s"
                               " with keyid=%d that does not have a configured"
-                              " key\n", MAC_ARG(hdr->addr2), keyidx);
+                              " key\n", print_mac(mac, hdr->addr2), keyidx);
                }
                return -3;
        }
@@ -415,12 +463,11 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
        iv32 = pos[4] | (pos[5] << 8) | (pos[6] << 16) | (pos[7] << 24);
        pos += 8;
 
-       if (iv32 < tkey->rx_iv32 ||
-           (iv32 == tkey->rx_iv32 && iv16 <= tkey->rx_iv16)) {
-               if (net_ratelimit()) {
-                       printk(KERN_DEBUG "TKIP: replay detected: STA=" MAC_FMT
+       if (tkip_replay_check(iv32, iv16, tkey->rx_iv32, tkey->rx_iv16)) {
+               if (ieee80211_ratelimit_debug(IEEE80211_DL_DROP)) {
+                       IEEE80211_DEBUG_DROP("TKIP: replay detected: STA=%s"
                               " previous TSC %08x%04x received TSC "
-                              "%08x%04x\n", MAC_ARG(hdr->addr2),
+                              "%08x%04x\n", print_mac(mac, hdr->addr2),
                               tkey->rx_iv32, tkey->rx_iv16, iv32, iv16);
                }
                tkey->dot11RSNAStatsTKIPReplays++;
@@ -435,11 +482,16 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
 
        plen = skb->len - hdr_len - 12;
 
-       crypto_cipher_setkey(tkey->tfm_arc4, rc4key, 16);
-       sg.page = virt_to_page(pos);
-       sg.offset = offset_in_page(pos);
-       sg.length = plen + 4;
-       crypto_cipher_decrypt(tkey->tfm_arc4, &sg, &sg, plen + 4);
+       crypto_blkcipher_setkey(tkey->rx_tfm_arc4, rc4key, 16);
+       sg_init_one(&sg, pos, plen + 4);
+       if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4)) {
+               if (net_ratelimit()) {
+                       printk(KERN_DEBUG ": TKIP: failed to decrypt "
+                              "received packet from %s\n",
+                              print_mac(mac, hdr->addr2));
+               }
+               return -7;
+       }
 
        crc = ~crc32_le(~0, pos, plen);
        icv[0] = crc;
@@ -452,9 +504,9 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
                         * it needs to be recalculated for the next packet. */
                        tkey->rx_phase1_done = 0;
                }
-               if (net_ratelimit()) {
-                       printk(KERN_DEBUG "TKIP: ICV error detected: STA="
-                              MAC_FMT "\n", MAC_ARG(hdr->addr2));
+               if (ieee80211_ratelimit_debug(IEEE80211_DL_DROP)) {
+                       IEEE80211_DEBUG_DROP("TKIP: ICV error detected: STA="
+                              "%s\n", print_mac(mac, hdr->addr2));
                }
                tkey->dot11RSNAStatsTKIPICVErrors++;
                return -5;
@@ -473,36 +525,36 @@ static int ieee80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
        return keyidx;
 }
 
-static int michael_mic(struct ieee80211_tkip_data *tkey, u8 * key, u8 * hdr,
+static int michael_mic(struct crypto_hash *tfm_michael, u8 * key, u8 * hdr,
                       u8 * data, size_t data_len, u8 * mic)
 {
+       struct hash_desc desc;
        struct scatterlist sg[2];
 
-       if (tkey->tfm_michael == NULL) {
+       if (tfm_michael == NULL) {
                printk(KERN_WARNING "michael_mic: tfm_michael == NULL\n");
                return -1;
        }
-       sg[0].page = virt_to_page(hdr);
-       sg[0].offset = offset_in_page(hdr);
-       sg[0].length = 16;
-
-       sg[1].page = virt_to_page(data);
-       sg[1].offset = offset_in_page(data);
-       sg[1].length = data_len;
+       sg_init_table(sg, 2);
+       sg_set_buf(&sg[0], hdr, 16);
+       sg_set_buf(&sg[1], data, data_len);
 
-       crypto_digest_init(tkey->tfm_michael);
-       crypto_digest_setkey(tkey->tfm_michael, key, 8);
-       crypto_digest_update(tkey->tfm_michael, sg, 2);
-       crypto_digest_final(tkey->tfm_michael, mic);
+       if (crypto_hash_setkey(tfm_michael, key, 8))
+               return -1;
 
-       return 0;
+       desc.tfm = tfm_michael;
+       desc.flags = 0;
+       return crypto_hash_digest(&desc, sg, data_len + 16, mic);
 }
 
 static void michael_mic_hdr(struct sk_buff *skb, u8 * hdr)
 {
        struct ieee80211_hdr_4addr *hdr11;
+       u16 stype;
 
        hdr11 = (struct ieee80211_hdr_4addr *)skb->data;
+       stype  = WLAN_FC_GET_STYPE(le16_to_cpu(hdr11->frame_ctl));
+
        switch (le16_to_cpu(hdr11->frame_ctl) &
                (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
        case IEEE80211_FCTL_TODS:
@@ -523,7 +575,13 @@ static void michael_mic_hdr(struct sk_buff *skb, u8 * hdr)
                break;
        }
 
-       hdr[12] = 0;            /* priority */
+       if (stype & IEEE80211_STYPE_QOS_DATA) {
+               const struct ieee80211_hdr_3addrqos *qoshdr =
+                       (struct ieee80211_hdr_3addrqos *)skb->data;
+               hdr[12] = le16_to_cpu(qoshdr->qos_ctl) & IEEE80211_QCTL_TID;
+       } else
+               hdr[12] = 0;            /* priority */
+
        hdr[13] = hdr[14] = hdr[15] = 0;        /* reserved */
 }
 
@@ -542,7 +600,7 @@ static int ieee80211_michael_mic_add(struct sk_buff *skb, int hdr_len,
 
        michael_mic_hdr(skb, tkey->tx_hdr);
        pos = skb_put(skb, 8);
-       if (michael_mic(tkey, &tkey->key[16], tkey->tx_hdr,
+       if (michael_mic(tkey->tx_tfm_michael, &tkey->key[16], tkey->tx_hdr,
                        skb->data + hdr_len, skb->len - 8 - hdr_len, pos))
                return -1;
 
@@ -575,20 +633,21 @@ static int ieee80211_michael_mic_verify(struct sk_buff *skb, int keyidx,
 {
        struct ieee80211_tkip_data *tkey = priv;
        u8 mic[8];
+       DECLARE_MAC_BUF(mac);
 
        if (!tkey->key_set)
                return -1;
 
        michael_mic_hdr(skb, tkey->rx_hdr);
-       if (michael_mic(tkey, &tkey->key[24], tkey->rx_hdr,
+       if (michael_mic(tkey->rx_tfm_michael, &tkey->key[24], tkey->rx_hdr,
                        skb->data + hdr_len, skb->len - 8 - hdr_len, mic))
                return -1;
        if (memcmp(mic, skb->data + skb->len - 8, 8) != 0) {
                struct ieee80211_hdr_4addr *hdr;
                hdr = (struct ieee80211_hdr_4addr *)skb->data;
                printk(KERN_DEBUG "%s: Michael MIC verification failed for "
-                      "MSDU from " MAC_FMT " keyidx=%d\n",
-                      skb->dev ? skb->dev->name : "N/A", MAC_ARG(hdr->addr2),
+                      "MSDU from %s keyidx=%d\n",
+                      skb->dev ? skb->dev->name : "N/A", print_mac(mac, hdr->addr2),
                       keyidx);
                if (skb->dev)
                        ieee80211_michael_mic_failure(skb->dev, hdr, keyidx);
@@ -610,14 +669,18 @@ static int ieee80211_tkip_set_key(void *key, int len, u8 * seq, void *priv)
 {
        struct ieee80211_tkip_data *tkey = priv;
        int keyidx;
-       struct crypto_tfm *tfm = tkey->tfm_michael;
-       struct crypto_tfm *tfm2 = tkey->tfm_arc4;
+       struct crypto_hash *tfm = tkey->tx_tfm_michael;
+       struct crypto_blkcipher *tfm2 = tkey->tx_tfm_arc4;
+       struct crypto_hash *tfm3 = tkey->rx_tfm_michael;
+       struct crypto_blkcipher *tfm4 = tkey->rx_tfm_arc4;
 
        keyidx = tkey->key_idx;
        memset(tkey, 0, sizeof(*tkey));
        tkey->key_idx = keyidx;
-       tkey->tfm_michael = tfm;
-       tkey->tfm_arc4 = tfm2;
+       tkey->tx_tfm_michael = tfm;
+       tkey->tx_tfm_arc4 = tfm2;
+       tkey->rx_tfm_michael = tfm3;
+       tkey->rx_tfm_arc4 = tfm4;
        if (len == TKIP_KEY_LEN) {
                memcpy(tkey->key, key, TKIP_KEY_LEN);
                tkey->key_set = 1;