wep.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <asm/scatterlist.h>
  19. #include <net/mac80211.h>
  20. #include "ieee80211_i.h"
  21. #include "wep.h"
  22. int ieee80211_wep_init(struct ieee80211_local *local)
  23. {
  24. /* start WEP IV from a random value */
  25. get_random_bytes(&local->wep_iv, WEP_IV_LEN);
  26. local->wep_tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
  27. CRYPTO_ALG_ASYNC);
  28. if (IS_ERR(local->wep_tx_tfm))
  29. return -ENOMEM;
  30. local->wep_rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
  31. CRYPTO_ALG_ASYNC);
  32. if (IS_ERR(local->wep_rx_tfm)) {
  33. crypto_free_blkcipher(local->wep_tx_tfm);
  34. return -ENOMEM;
  35. }
  36. return 0;
  37. }
  38. void ieee80211_wep_free(struct ieee80211_local *local)
  39. {
  40. crypto_free_blkcipher(local->wep_tx_tfm);
  41. crypto_free_blkcipher(local->wep_rx_tfm);
  42. }
  43. static inline int ieee80211_wep_weak_iv(u32 iv, int keylen)
  44. {
  45. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the
  46. * key scheduling algorithm of RC4. At least IVs (KeyByte + 3,
  47. * 0xff, N) can be used to speedup attacks, so avoid using them. */
  48. if ((iv & 0xff00) == 0xff00) {
  49. u8 B = (iv >> 16) & 0xff;
  50. if (B >= 3 && B < 3 + keylen)
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. static void ieee80211_wep_get_iv(struct ieee80211_local *local,
  56. struct ieee80211_key *key, u8 *iv)
  57. {
  58. local->wep_iv++;
  59. if (ieee80211_wep_weak_iv(local->wep_iv, key->conf.keylen))
  60. local->wep_iv += 0x0100;
  61. if (!iv)
  62. return;
  63. *iv++ = (local->wep_iv >> 16) & 0xff;
  64. *iv++ = (local->wep_iv >> 8) & 0xff;
  65. *iv++ = local->wep_iv & 0xff;
  66. *iv++ = key->conf.keyidx << 6;
  67. }
  68. static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local,
  69. struct sk_buff *skb,
  70. struct ieee80211_key *key)
  71. {
  72. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  73. u16 fc;
  74. int hdrlen;
  75. u8 *newhdr;
  76. fc = le16_to_cpu(hdr->frame_control);
  77. fc |= IEEE80211_FCTL_PROTECTED;
  78. hdr->frame_control = cpu_to_le16(fc);
  79. if ((skb_headroom(skb) < WEP_IV_LEN ||
  80. skb_tailroom(skb) < WEP_ICV_LEN)) {
  81. I802_DEBUG_INC(local->tx_expand_skb_head);
  82. if (unlikely(pskb_expand_head(skb, WEP_IV_LEN, WEP_ICV_LEN,
  83. GFP_ATOMIC)))
  84. return NULL;
  85. }
  86. hdrlen = ieee80211_get_hdrlen(fc);
  87. newhdr = skb_push(skb, WEP_IV_LEN);
  88. memmove(newhdr, newhdr + WEP_IV_LEN, hdrlen);
  89. ieee80211_wep_get_iv(local, key, newhdr + hdrlen);
  90. return newhdr + hdrlen;
  91. }
  92. static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
  93. struct sk_buff *skb,
  94. struct ieee80211_key *key)
  95. {
  96. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  97. u16 fc;
  98. int hdrlen;
  99. fc = le16_to_cpu(hdr->frame_control);
  100. hdrlen = ieee80211_get_hdrlen(fc);
  101. memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
  102. skb_pull(skb, WEP_IV_LEN);
  103. }
  104. /* Perform WEP encryption using given key. data buffer must have tailroom
  105. * for 4-byte ICV. data_len must not include this ICV. Note: this function
  106. * does _not_ add IV. data = RC4(data | CRC32(data)) */
  107. void ieee80211_wep_encrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
  108. size_t klen, u8 *data, size_t data_len)
  109. {
  110. struct blkcipher_desc desc = { .tfm = tfm };
  111. struct scatterlist sg;
  112. __le32 *icv;
  113. icv = (__le32 *)(data + data_len);
  114. *icv = cpu_to_le32(~crc32_le(~0, data, data_len));
  115. crypto_blkcipher_setkey(tfm, rc4key, klen);
  116. sg.page = virt_to_page(data);
  117. sg.offset = offset_in_page(data);
  118. sg.length = data_len + WEP_ICV_LEN;
  119. crypto_blkcipher_encrypt(&desc, &sg, &sg, sg.length);
  120. }
  121. /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the
  122. * beginning of the buffer 4 bytes of extra space (ICV) in the end of the
  123. * buffer will be added. Both IV and ICV will be transmitted, so the
  124. * payload length increases with 8 bytes.
  125. *
  126. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  127. */
  128. int ieee80211_wep_encrypt(struct ieee80211_local *local, struct sk_buff *skb,
  129. struct ieee80211_key *key)
  130. {
  131. u32 klen;
  132. u8 *rc4key, *iv;
  133. size_t len;
  134. if (!key || key->conf.alg != ALG_WEP)
  135. return -1;
  136. klen = 3 + key->conf.keylen;
  137. rc4key = kmalloc(klen, GFP_ATOMIC);
  138. if (!rc4key)
  139. return -1;
  140. iv = ieee80211_wep_add_iv(local, skb, key);
  141. if (!iv) {
  142. kfree(rc4key);
  143. return -1;
  144. }
  145. len = skb->len - (iv + WEP_IV_LEN - skb->data);
  146. /* Prepend 24-bit IV to RC4 key */
  147. memcpy(rc4key, iv, 3);
  148. /* Copy rest of the WEP key (the secret part) */
  149. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  150. /* Add room for ICV */
  151. skb_put(skb, WEP_ICV_LEN);
  152. ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, klen,
  153. iv + WEP_IV_LEN, len);
  154. kfree(rc4key);
  155. return 0;
  156. }
  157. /* Perform WEP decryption using given key. data buffer includes encrypted
  158. * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
  159. * Return 0 on success and -1 on ICV mismatch. */
  160. int ieee80211_wep_decrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
  161. size_t klen, u8 *data, size_t data_len)
  162. {
  163. struct blkcipher_desc desc = { .tfm = tfm };
  164. struct scatterlist sg;
  165. __le32 crc;
  166. crypto_blkcipher_setkey(tfm, rc4key, klen);
  167. sg.page = virt_to_page(data);
  168. sg.offset = offset_in_page(data);
  169. sg.length = data_len + WEP_ICV_LEN;
  170. crypto_blkcipher_decrypt(&desc, &sg, &sg, sg.length);
  171. crc = cpu_to_le32(~crc32_le(~0, data, data_len));
  172. if (memcmp(&crc, data + data_len, WEP_ICV_LEN) != 0)
  173. /* ICV mismatch */
  174. return -1;
  175. return 0;
  176. }
  177. /* Perform WEP decryption on given skb. Buffer includes whole WEP part of
  178. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  179. * ICV (4 bytes). skb->len includes both IV and ICV.
  180. *
  181. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  182. * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload
  183. * is moved to the beginning of the skb and skb length will be reduced.
  184. */
  185. int ieee80211_wep_decrypt(struct ieee80211_local *local, struct sk_buff *skb,
  186. struct ieee80211_key *key)
  187. {
  188. u32 klen;
  189. u8 *rc4key;
  190. u8 keyidx;
  191. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  192. u16 fc;
  193. int hdrlen;
  194. size_t len;
  195. int ret = 0;
  196. fc = le16_to_cpu(hdr->frame_control);
  197. if (!(fc & IEEE80211_FCTL_PROTECTED))
  198. return -1;
  199. hdrlen = ieee80211_get_hdrlen(fc);
  200. if (skb->len < 8 + hdrlen)
  201. return -1;
  202. len = skb->len - hdrlen - 8;
  203. keyidx = skb->data[hdrlen + 3] >> 6;
  204. if (!key || keyidx != key->conf.keyidx || key->conf.alg != ALG_WEP)
  205. return -1;
  206. klen = 3 + key->conf.keylen;
  207. rc4key = kmalloc(klen, GFP_ATOMIC);
  208. if (!rc4key)
  209. return -1;
  210. /* Prepend 24-bit IV to RC4 key */
  211. memcpy(rc4key, skb->data + hdrlen, 3);
  212. /* Copy rest of the WEP key (the secret part) */
  213. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  214. if (ieee80211_wep_decrypt_data(local->wep_rx_tfm, rc4key, klen,
  215. skb->data + hdrlen + WEP_IV_LEN,
  216. len)) {
  217. printk(KERN_DEBUG "WEP decrypt failed (ICV)\n");
  218. ret = -1;
  219. }
  220. kfree(rc4key);
  221. /* Trim ICV */
  222. skb_trim(skb, skb->len - WEP_ICV_LEN);
  223. /* Remove IV */
  224. memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
  225. skb_pull(skb, WEP_IV_LEN);
  226. return ret;
  227. }
  228. u8 * ieee80211_wep_is_weak_iv(struct sk_buff *skb, struct ieee80211_key *key)
  229. {
  230. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  231. u16 fc;
  232. int hdrlen;
  233. u8 *ivpos;
  234. u32 iv;
  235. fc = le16_to_cpu(hdr->frame_control);
  236. if (!(fc & IEEE80211_FCTL_PROTECTED))
  237. return NULL;
  238. hdrlen = ieee80211_get_hdrlen(fc);
  239. ivpos = skb->data + hdrlen;
  240. iv = (ivpos[0] << 16) | (ivpos[1] << 8) | ivpos[2];
  241. if (ieee80211_wep_weak_iv(iv, key->conf.keylen))
  242. return ivpos;
  243. return NULL;
  244. }
  245. ieee80211_txrx_result
  246. ieee80211_crypto_wep_decrypt(struct ieee80211_txrx_data *rx)
  247. {
  248. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  249. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  250. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH))
  251. return TXRX_CONTINUE;
  252. if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) {
  253. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
  254. if (net_ratelimit())
  255. printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
  256. "failed\n", rx->dev->name);
  257. return TXRX_DROP;
  258. }
  259. } else if (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED)) {
  260. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  261. /* remove ICV */
  262. skb_trim(rx->skb, rx->skb->len - 4);
  263. }
  264. return TXRX_CONTINUE;
  265. }
  266. static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
  267. {
  268. if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  269. if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
  270. return -1;
  271. } else {
  272. tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
  273. if (tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) {
  274. if (!ieee80211_wep_add_iv(tx->local, skb, tx->key))
  275. return -1;
  276. }
  277. }
  278. return 0;
  279. }
  280. ieee80211_txrx_result
  281. ieee80211_crypto_wep_encrypt(struct ieee80211_txrx_data *tx)
  282. {
  283. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  284. u16 fc;
  285. fc = le16_to_cpu(hdr->frame_control);
  286. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  287. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  288. (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
  289. return TXRX_CONTINUE;
  290. tx->u.tx.control->iv_len = WEP_IV_LEN;
  291. tx->u.tx.control->icv_len = WEP_ICV_LEN;
  292. ieee80211_tx_set_iswep(tx);
  293. if (wep_encrypt_skb(tx, tx->skb) < 0) {
  294. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  295. return TXRX_DROP;
  296. }
  297. if (tx->u.tx.extra_frag) {
  298. int i;
  299. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  300. if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
  301. I802_DEBUG_INC(tx->local->
  302. tx_handlers_drop_wep);
  303. return TXRX_DROP;
  304. }
  305. }
  306. }
  307. return TXRX_CONTINUE;
  308. }