wep.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <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 (WARN_ON(skb_tailroom(skb) < WEP_ICV_LEN ||
  80. skb_headroom(skb) < WEP_IV_LEN))
  81. return NULL;
  82. hdrlen = ieee80211_get_hdrlen(fc);
  83. newhdr = skb_push(skb, WEP_IV_LEN);
  84. memmove(newhdr, newhdr + WEP_IV_LEN, hdrlen);
  85. ieee80211_wep_get_iv(local, key, newhdr + hdrlen);
  86. return newhdr + hdrlen;
  87. }
  88. static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
  89. struct sk_buff *skb,
  90. struct ieee80211_key *key)
  91. {
  92. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  93. u16 fc;
  94. int hdrlen;
  95. fc = le16_to_cpu(hdr->frame_control);
  96. hdrlen = ieee80211_get_hdrlen(fc);
  97. memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
  98. skb_pull(skb, WEP_IV_LEN);
  99. }
  100. /* Perform WEP encryption using given key. data buffer must have tailroom
  101. * for 4-byte ICV. data_len must not include this ICV. Note: this function
  102. * does _not_ add IV. data = RC4(data | CRC32(data)) */
  103. void ieee80211_wep_encrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
  104. size_t klen, u8 *data, size_t data_len)
  105. {
  106. struct blkcipher_desc desc = { .tfm = tfm };
  107. struct scatterlist sg;
  108. __le32 *icv;
  109. icv = (__le32 *)(data + data_len);
  110. *icv = cpu_to_le32(~crc32_le(~0, data, data_len));
  111. crypto_blkcipher_setkey(tfm, rc4key, klen);
  112. sg_init_one(&sg, data, data_len + WEP_ICV_LEN);
  113. crypto_blkcipher_encrypt(&desc, &sg, &sg, sg.length);
  114. }
  115. /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the
  116. * beginning of the buffer 4 bytes of extra space (ICV) in the end of the
  117. * buffer will be added. Both IV and ICV will be transmitted, so the
  118. * payload length increases with 8 bytes.
  119. *
  120. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  121. */
  122. int ieee80211_wep_encrypt(struct ieee80211_local *local, struct sk_buff *skb,
  123. struct ieee80211_key *key)
  124. {
  125. u32 klen;
  126. u8 *rc4key, *iv;
  127. size_t len;
  128. if (!key || key->conf.alg != ALG_WEP)
  129. return -1;
  130. klen = 3 + key->conf.keylen;
  131. rc4key = kmalloc(klen, GFP_ATOMIC);
  132. if (!rc4key)
  133. return -1;
  134. iv = ieee80211_wep_add_iv(local, skb, key);
  135. if (!iv) {
  136. kfree(rc4key);
  137. return -1;
  138. }
  139. len = skb->len - (iv + WEP_IV_LEN - skb->data);
  140. /* Prepend 24-bit IV to RC4 key */
  141. memcpy(rc4key, iv, 3);
  142. /* Copy rest of the WEP key (the secret part) */
  143. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  144. /* Add room for ICV */
  145. skb_put(skb, WEP_ICV_LEN);
  146. ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, klen,
  147. iv + WEP_IV_LEN, len);
  148. kfree(rc4key);
  149. return 0;
  150. }
  151. /* Perform WEP decryption using given key. data buffer includes encrypted
  152. * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
  153. * Return 0 on success and -1 on ICV mismatch. */
  154. int ieee80211_wep_decrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
  155. size_t klen, u8 *data, size_t data_len)
  156. {
  157. struct blkcipher_desc desc = { .tfm = tfm };
  158. struct scatterlist sg;
  159. __le32 crc;
  160. crypto_blkcipher_setkey(tfm, rc4key, klen);
  161. sg_init_one(&sg, data, data_len + WEP_ICV_LEN);
  162. crypto_blkcipher_decrypt(&desc, &sg, &sg, sg.length);
  163. crc = cpu_to_le32(~crc32_le(~0, data, data_len));
  164. if (memcmp(&crc, data + data_len, WEP_ICV_LEN) != 0)
  165. /* ICV mismatch */
  166. return -1;
  167. return 0;
  168. }
  169. /* Perform WEP decryption on given skb. Buffer includes whole WEP part of
  170. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  171. * ICV (4 bytes). skb->len includes both IV and ICV.
  172. *
  173. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  174. * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload
  175. * is moved to the beginning of the skb and skb length will be reduced.
  176. */
  177. int ieee80211_wep_decrypt(struct ieee80211_local *local, struct sk_buff *skb,
  178. struct ieee80211_key *key)
  179. {
  180. u32 klen;
  181. u8 *rc4key;
  182. u8 keyidx;
  183. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  184. u16 fc;
  185. int hdrlen;
  186. size_t len;
  187. int ret = 0;
  188. fc = le16_to_cpu(hdr->frame_control);
  189. if (!(fc & IEEE80211_FCTL_PROTECTED))
  190. return -1;
  191. hdrlen = ieee80211_get_hdrlen(fc);
  192. if (skb->len < 8 + hdrlen)
  193. return -1;
  194. len = skb->len - hdrlen - 8;
  195. keyidx = skb->data[hdrlen + 3] >> 6;
  196. if (!key || keyidx != key->conf.keyidx || key->conf.alg != ALG_WEP)
  197. return -1;
  198. klen = 3 + key->conf.keylen;
  199. rc4key = kmalloc(klen, GFP_ATOMIC);
  200. if (!rc4key)
  201. return -1;
  202. /* Prepend 24-bit IV to RC4 key */
  203. memcpy(rc4key, skb->data + hdrlen, 3);
  204. /* Copy rest of the WEP key (the secret part) */
  205. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  206. if (ieee80211_wep_decrypt_data(local->wep_rx_tfm, rc4key, klen,
  207. skb->data + hdrlen + WEP_IV_LEN,
  208. len)) {
  209. if (net_ratelimit())
  210. printk(KERN_DEBUG "WEP decrypt failed (ICV)\n");
  211. ret = -1;
  212. }
  213. kfree(rc4key);
  214. /* Trim ICV */
  215. skb_trim(skb, skb->len - WEP_ICV_LEN);
  216. /* Remove IV */
  217. memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
  218. skb_pull(skb, WEP_IV_LEN);
  219. return ret;
  220. }
  221. u8 * ieee80211_wep_is_weak_iv(struct sk_buff *skb, struct ieee80211_key *key)
  222. {
  223. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  224. u16 fc;
  225. int hdrlen;
  226. u8 *ivpos;
  227. u32 iv;
  228. fc = le16_to_cpu(hdr->frame_control);
  229. if (!(fc & IEEE80211_FCTL_PROTECTED))
  230. return NULL;
  231. hdrlen = ieee80211_get_hdrlen(fc);
  232. ivpos = skb->data + hdrlen;
  233. iv = (ivpos[0] << 16) | (ivpos[1] << 8) | ivpos[2];
  234. if (ieee80211_wep_weak_iv(iv, key->conf.keylen))
  235. return ivpos;
  236. return NULL;
  237. }
  238. ieee80211_rx_result
  239. ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx)
  240. {
  241. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  242. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  243. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH))
  244. return RX_CONTINUE;
  245. if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
  246. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
  247. #ifdef CONFIG_MAC80211_DEBUG
  248. if (net_ratelimit())
  249. printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
  250. "failed\n", rx->dev->name);
  251. #endif /* CONFIG_MAC80211_DEBUG */
  252. return RX_DROP_UNUSABLE;
  253. }
  254. } else if (!(rx->status->flag & RX_FLAG_IV_STRIPPED)) {
  255. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  256. /* remove ICV */
  257. skb_trim(rx->skb, rx->skb->len - 4);
  258. }
  259. return RX_CONTINUE;
  260. }
  261. static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  262. {
  263. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  264. info->control.iv_len = WEP_IV_LEN;
  265. info->control.icv_len = WEP_ICV_LEN;
  266. if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  267. info->control.hw_key = &tx->key->conf;
  268. if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
  269. return -1;
  270. } else {
  271. if (tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) {
  272. if (!ieee80211_wep_add_iv(tx->local, skb, tx->key))
  273. return -1;
  274. }
  275. }
  276. return 0;
  277. }
  278. ieee80211_tx_result
  279. ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx)
  280. {
  281. ieee80211_tx_set_protected(tx);
  282. if (wep_encrypt_skb(tx, tx->skb) < 0) {
  283. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  284. return TX_DROP;
  285. }
  286. if (tx->extra_frag) {
  287. int i;
  288. for (i = 0; i < tx->num_extra_frag; i++) {
  289. if (wep_encrypt_skb(tx, tx->extra_frag[i]) < 0) {
  290. I802_DEBUG_INC(tx->local->
  291. tx_handlers_drop_wep);
  292. return TX_DROP;
  293. }
  294. }
  295. }
  296. return TX_CONTINUE;
  297. }