wep.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Software WEP encryption implementation
  3. * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi>
  4. * Copyright 2003, Instant802 Networks, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/netdevice.h>
  11. #include <linux/types.h>
  12. #include <linux/random.h>
  13. #include <linux/compiler.h>
  14. #include <linux/crc32.h>
  15. #include <linux/crypto.h>
  16. #include <linux/err.h>
  17. #include <linux/mm.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/slab.h>
  20. #include <asm/unaligned.h>
  21. #include <net/mac80211.h>
  22. #include "ieee80211_i.h"
  23. #include "wep.h"
  24. int ieee80211_wep_init(struct ieee80211_local *local)
  25. {
  26. /* start WEP IV from a random value */
  27. get_random_bytes(&local->wep_iv, IEEE80211_WEP_IV_LEN);
  28. local->wep_tx_tfm = crypto_alloc_cipher("arc4", 0, CRYPTO_ALG_ASYNC);
  29. if (IS_ERR(local->wep_tx_tfm)) {
  30. local->wep_rx_tfm = ERR_PTR(-EINVAL);
  31. return PTR_ERR(local->wep_tx_tfm);
  32. }
  33. local->wep_rx_tfm = crypto_alloc_cipher("arc4", 0, CRYPTO_ALG_ASYNC);
  34. if (IS_ERR(local->wep_rx_tfm)) {
  35. crypto_free_cipher(local->wep_tx_tfm);
  36. local->wep_tx_tfm = ERR_PTR(-EINVAL);
  37. return PTR_ERR(local->wep_rx_tfm);
  38. }
  39. return 0;
  40. }
  41. void ieee80211_wep_free(struct ieee80211_local *local)
  42. {
  43. if (!IS_ERR(local->wep_tx_tfm))
  44. crypto_free_cipher(local->wep_tx_tfm);
  45. if (!IS_ERR(local->wep_rx_tfm))
  46. crypto_free_cipher(local->wep_rx_tfm);
  47. }
  48. static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen)
  49. {
  50. /*
  51. * Fluhrer, Mantin, and Shamir have reported weaknesses in the
  52. * key scheduling algorithm of RC4. At least IVs (KeyByte + 3,
  53. * 0xff, N) can be used to speedup attacks, so avoid using them.
  54. */
  55. if ((iv & 0xff00) == 0xff00) {
  56. u8 B = (iv >> 16) & 0xff;
  57. if (B >= 3 && B < 3 + keylen)
  58. return true;
  59. }
  60. return false;
  61. }
  62. static void ieee80211_wep_get_iv(struct ieee80211_local *local,
  63. int keylen, int keyidx, u8 *iv)
  64. {
  65. local->wep_iv++;
  66. if (ieee80211_wep_weak_iv(local->wep_iv, keylen))
  67. local->wep_iv += 0x0100;
  68. if (!iv)
  69. return;
  70. *iv++ = (local->wep_iv >> 16) & 0xff;
  71. *iv++ = (local->wep_iv >> 8) & 0xff;
  72. *iv++ = local->wep_iv & 0xff;
  73. *iv++ = keyidx << 6;
  74. }
  75. static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local,
  76. struct sk_buff *skb,
  77. int keylen, int keyidx)
  78. {
  79. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  80. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  81. unsigned int hdrlen;
  82. u8 *newhdr;
  83. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  84. if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN ||
  85. skb_headroom(skb) < IEEE80211_WEP_IV_LEN))
  86. return NULL;
  87. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  88. newhdr = skb_push(skb, IEEE80211_WEP_IV_LEN);
  89. memmove(newhdr, newhdr + IEEE80211_WEP_IV_LEN, hdrlen);
  90. /* the HW only needs room for the IV, but not the actual IV */
  91. if (info->control.hw_key &&
  92. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  93. return newhdr + hdrlen;
  94. skb_set_network_header(skb, skb_network_offset(skb) +
  95. IEEE80211_WEP_IV_LEN);
  96. ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen);
  97. return newhdr + hdrlen;
  98. }
  99. static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
  100. struct sk_buff *skb,
  101. struct ieee80211_key *key)
  102. {
  103. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  104. unsigned int hdrlen;
  105. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  106. memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen);
  107. skb_pull(skb, IEEE80211_WEP_IV_LEN);
  108. }
  109. /* Perform WEP encryption using given key. data buffer must have tailroom
  110. * for 4-byte ICV. data_len must not include this ICV. Note: this function
  111. * does _not_ add IV. data = RC4(data | CRC32(data)) */
  112. int ieee80211_wep_encrypt_data(struct crypto_cipher *tfm, u8 *rc4key,
  113. size_t klen, u8 *data, size_t data_len)
  114. {
  115. __le32 icv;
  116. int i;
  117. if (IS_ERR(tfm))
  118. return -1;
  119. icv = cpu_to_le32(~crc32_le(~0, data, data_len));
  120. put_unaligned(icv, (__le32 *)(data + data_len));
  121. crypto_cipher_setkey(tfm, rc4key, klen);
  122. for (i = 0; i < data_len + IEEE80211_WEP_ICV_LEN; i++)
  123. crypto_cipher_encrypt_one(tfm, data + i, data + i);
  124. return 0;
  125. }
  126. /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the
  127. * beginning of the buffer 4 bytes of extra space (ICV) in the end of the
  128. * buffer will be added. Both IV and ICV will be transmitted, so the
  129. * payload length increases with 8 bytes.
  130. *
  131. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  132. */
  133. int ieee80211_wep_encrypt(struct ieee80211_local *local,
  134. struct sk_buff *skb,
  135. const u8 *key, int keylen, int keyidx)
  136. {
  137. u8 *iv;
  138. size_t len;
  139. u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
  140. iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx);
  141. if (!iv)
  142. return -1;
  143. len = skb->len - (iv + IEEE80211_WEP_IV_LEN - skb->data);
  144. /* Prepend 24-bit IV to RC4 key */
  145. memcpy(rc4key, iv, 3);
  146. /* Copy rest of the WEP key (the secret part) */
  147. memcpy(rc4key + 3, key, keylen);
  148. /* Add room for ICV */
  149. skb_put(skb, IEEE80211_WEP_ICV_LEN);
  150. return ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, keylen + 3,
  151. iv + IEEE80211_WEP_IV_LEN, len);
  152. }
  153. /* Perform WEP decryption using given key. data buffer includes encrypted
  154. * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
  155. * Return 0 on success and -1 on ICV mismatch. */
  156. int ieee80211_wep_decrypt_data(struct crypto_cipher *tfm, u8 *rc4key,
  157. size_t klen, u8 *data, size_t data_len)
  158. {
  159. __le32 crc;
  160. int i;
  161. if (IS_ERR(tfm))
  162. return -1;
  163. crypto_cipher_setkey(tfm, rc4key, klen);
  164. for (i = 0; i < data_len + IEEE80211_WEP_ICV_LEN; i++)
  165. crypto_cipher_decrypt_one(tfm, data + i, data + i);
  166. crc = cpu_to_le32(~crc32_le(~0, data, data_len));
  167. if (memcmp(&crc, data + data_len, IEEE80211_WEP_ICV_LEN) != 0)
  168. /* ICV mismatch */
  169. return -1;
  170. return 0;
  171. }
  172. /* Perform WEP decryption on given skb. Buffer includes whole WEP part of
  173. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  174. * ICV (4 bytes). skb->len includes both IV and ICV.
  175. *
  176. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  177. * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload
  178. * is moved to the beginning of the skb and skb length will be reduced.
  179. */
  180. static int ieee80211_wep_decrypt(struct ieee80211_local *local,
  181. struct sk_buff *skb,
  182. struct ieee80211_key *key)
  183. {
  184. u32 klen;
  185. u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
  186. u8 keyidx;
  187. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  188. unsigned int hdrlen;
  189. size_t len;
  190. int ret = 0;
  191. if (!ieee80211_has_protected(hdr->frame_control))
  192. return -1;
  193. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  194. if (skb->len < hdrlen + IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN)
  195. return -1;
  196. len = skb->len - hdrlen - IEEE80211_WEP_IV_LEN - IEEE80211_WEP_ICV_LEN;
  197. keyidx = skb->data[hdrlen + 3] >> 6;
  198. if (!key || keyidx != key->conf.keyidx)
  199. return -1;
  200. klen = 3 + key->conf.keylen;
  201. /* Prepend 24-bit IV to RC4 key */
  202. memcpy(rc4key, skb->data + hdrlen, 3);
  203. /* Copy rest of the WEP key (the secret part) */
  204. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  205. if (ieee80211_wep_decrypt_data(local->wep_rx_tfm, rc4key, klen,
  206. skb->data + hdrlen +
  207. IEEE80211_WEP_IV_LEN, len))
  208. ret = -1;
  209. /* Trim ICV */
  210. skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN);
  211. /* Remove IV */
  212. memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen);
  213. skb_pull(skb, IEEE80211_WEP_IV_LEN);
  214. return ret;
  215. }
  216. static bool ieee80211_wep_is_weak_iv(struct sk_buff *skb,
  217. struct ieee80211_key *key)
  218. {
  219. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  220. unsigned int hdrlen;
  221. u8 *ivpos;
  222. u32 iv;
  223. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  224. ivpos = skb->data + hdrlen;
  225. iv = (ivpos[0] << 16) | (ivpos[1] << 8) | ivpos[2];
  226. return ieee80211_wep_weak_iv(iv, key->conf.keylen);
  227. }
  228. ieee80211_rx_result
  229. ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx)
  230. {
  231. struct sk_buff *skb = rx->skb;
  232. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  233. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  234. __le16 fc = hdr->frame_control;
  235. if (!ieee80211_is_data(fc) && !ieee80211_is_auth(fc))
  236. return RX_CONTINUE;
  237. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  238. if (skb_linearize(rx->skb))
  239. return RX_DROP_UNUSABLE;
  240. if (rx->sta && ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  241. rx->sta->wep_weak_iv_count++;
  242. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key))
  243. return RX_DROP_UNUSABLE;
  244. } else if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  245. if (!pskb_may_pull(rx->skb, ieee80211_hdrlen(fc) +
  246. IEEE80211_WEP_IV_LEN))
  247. return RX_DROP_UNUSABLE;
  248. if (rx->sta && ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  249. rx->sta->wep_weak_iv_count++;
  250. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  251. /* remove ICV */
  252. if (pskb_trim(rx->skb, rx->skb->len - IEEE80211_WEP_ICV_LEN))
  253. return RX_DROP_UNUSABLE;
  254. }
  255. return RX_CONTINUE;
  256. }
  257. static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  258. {
  259. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  260. struct ieee80211_key_conf *hw_key = info->control.hw_key;
  261. if (!hw_key) {
  262. if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key,
  263. tx->key->conf.keylen,
  264. tx->key->conf.keyidx))
  265. return -1;
  266. } else if ((hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
  267. (hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  268. if (!ieee80211_wep_add_iv(tx->local, skb,
  269. tx->key->conf.keylen,
  270. tx->key->conf.keyidx))
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. ieee80211_tx_result
  276. ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx)
  277. {
  278. struct sk_buff *skb;
  279. ieee80211_tx_set_protected(tx);
  280. skb_queue_walk(&tx->skbs, skb) {
  281. if (wep_encrypt_skb(tx, skb) < 0) {
  282. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  283. return TX_DROP;
  284. }
  285. }
  286. return TX_CONTINUE;
  287. }