wep.c 9.4 KB

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