wep.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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, WEP_IV_LEN);
  28. local->wep_tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
  29. CRYPTO_ALG_ASYNC);
  30. if (IS_ERR(local->wep_tx_tfm)) {
  31. local->wep_rx_tfm = ERR_PTR(-EINVAL);
  32. return PTR_ERR(local->wep_tx_tfm);
  33. }
  34. local->wep_rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
  35. CRYPTO_ALG_ASYNC);
  36. if (IS_ERR(local->wep_rx_tfm)) {
  37. crypto_free_blkcipher(local->wep_tx_tfm);
  38. local->wep_tx_tfm = ERR_PTR(-EINVAL);
  39. return PTR_ERR(local->wep_rx_tfm);
  40. }
  41. return 0;
  42. }
  43. void ieee80211_wep_free(struct ieee80211_local *local)
  44. {
  45. if (!IS_ERR(local->wep_tx_tfm))
  46. crypto_free_blkcipher(local->wep_tx_tfm);
  47. if (!IS_ERR(local->wep_rx_tfm))
  48. crypto_free_blkcipher(local->wep_rx_tfm);
  49. }
  50. static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen)
  51. {
  52. /*
  53. * Fluhrer, Mantin, and Shamir have reported weaknesses in the
  54. * key scheduling algorithm of RC4. At least IVs (KeyByte + 3,
  55. * 0xff, N) can be used to speedup attacks, so avoid using them.
  56. */
  57. if ((iv & 0xff00) == 0xff00) {
  58. u8 B = (iv >> 16) & 0xff;
  59. if (B >= 3 && B < 3 + keylen)
  60. return true;
  61. }
  62. return false;
  63. }
  64. static void ieee80211_wep_get_iv(struct ieee80211_local *local,
  65. int keylen, int keyidx, u8 *iv)
  66. {
  67. local->wep_iv++;
  68. if (ieee80211_wep_weak_iv(local->wep_iv, keylen))
  69. local->wep_iv += 0x0100;
  70. if (!iv)
  71. return;
  72. *iv++ = (local->wep_iv >> 16) & 0xff;
  73. *iv++ = (local->wep_iv >> 8) & 0xff;
  74. *iv++ = local->wep_iv & 0xff;
  75. *iv++ = keyidx << 6;
  76. }
  77. static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local,
  78. struct sk_buff *skb,
  79. int keylen, int keyidx)
  80. {
  81. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  82. unsigned int hdrlen;
  83. u8 *newhdr;
  84. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  85. if (WARN_ON(skb_tailroom(skb) < WEP_ICV_LEN ||
  86. skb_headroom(skb) < WEP_IV_LEN))
  87. return NULL;
  88. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  89. newhdr = skb_push(skb, WEP_IV_LEN);
  90. memmove(newhdr, newhdr + WEP_IV_LEN, hdrlen);
  91. ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen);
  92. return newhdr + hdrlen;
  93. }
  94. static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
  95. struct sk_buff *skb,
  96. struct ieee80211_key *key)
  97. {
  98. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  99. unsigned int hdrlen;
  100. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  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. int 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. if (IS_ERR(tfm))
  114. return -1;
  115. icv = cpu_to_le32(~crc32_le(~0, data, data_len));
  116. put_unaligned(icv, (__le32 *)(data + data_len));
  117. crypto_blkcipher_setkey(tfm, rc4key, klen);
  118. sg_init_one(&sg, data, data_len + WEP_ICV_LEN);
  119. crypto_blkcipher_encrypt(&desc, &sg, &sg, sg.length);
  120. return 0;
  121. }
  122. /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the
  123. * beginning of the buffer 4 bytes of extra space (ICV) in the end of the
  124. * buffer will be added. Both IV and ICV will be transmitted, so the
  125. * payload length increases with 8 bytes.
  126. *
  127. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  128. */
  129. int ieee80211_wep_encrypt(struct ieee80211_local *local,
  130. struct sk_buff *skb,
  131. const u8 *key, int keylen, int keyidx)
  132. {
  133. u8 *iv;
  134. size_t len;
  135. u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
  136. iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx);
  137. if (!iv)
  138. return -1;
  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, keylen);
  144. /* Add room for ICV */
  145. skb_put(skb, WEP_ICV_LEN);
  146. return ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, keylen + 3,
  147. iv + WEP_IV_LEN, len);
  148. }
  149. /* Perform WEP decryption using given key. data buffer includes encrypted
  150. * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
  151. * Return 0 on success and -1 on ICV mismatch. */
  152. int ieee80211_wep_decrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
  153. size_t klen, u8 *data, size_t data_len)
  154. {
  155. struct blkcipher_desc desc = { .tfm = tfm };
  156. struct scatterlist sg;
  157. __le32 crc;
  158. if (IS_ERR(tfm))
  159. return -1;
  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. static int ieee80211_wep_decrypt(struct ieee80211_local *local,
  178. struct sk_buff *skb,
  179. struct ieee80211_key *key)
  180. {
  181. u32 klen;
  182. u8 *rc4key;
  183. u8 keyidx;
  184. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  185. unsigned int hdrlen;
  186. size_t len;
  187. int ret = 0;
  188. if (!ieee80211_has_protected(hdr->frame_control))
  189. return -1;
  190. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  191. if (skb->len < hdrlen + WEP_IV_LEN + WEP_ICV_LEN)
  192. return -1;
  193. len = skb->len - hdrlen - WEP_IV_LEN - WEP_ICV_LEN;
  194. keyidx = skb->data[hdrlen + 3] >> 6;
  195. if (!key || keyidx != key->conf.keyidx || key->conf.alg != ALG_WEP)
  196. return -1;
  197. klen = 3 + key->conf.keylen;
  198. rc4key = kmalloc(klen, GFP_ATOMIC);
  199. if (!rc4key)
  200. return -1;
  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 + WEP_IV_LEN,
  207. len))
  208. ret = -1;
  209. kfree(rc4key);
  210. /* Trim ICV */
  211. skb_trim(skb, skb->len - WEP_ICV_LEN);
  212. /* Remove IV */
  213. memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
  214. skb_pull(skb, WEP_IV_LEN);
  215. return ret;
  216. }
  217. bool ieee80211_wep_is_weak_iv(struct sk_buff *skb, 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. if (!ieee80211_has_protected(hdr->frame_control))
  224. return false;
  225. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  226. ivpos = skb->data + hdrlen;
  227. iv = (ivpos[0] << 16) | (ivpos[1] << 8) | ivpos[2];
  228. return ieee80211_wep_weak_iv(iv, key->conf.keylen);
  229. }
  230. ieee80211_rx_result
  231. ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx)
  232. {
  233. struct sk_buff *skb = rx->skb;
  234. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  235. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  236. if (!ieee80211_is_data(hdr->frame_control) &&
  237. !ieee80211_is_auth(hdr->frame_control))
  238. return RX_CONTINUE;
  239. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  240. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key))
  241. return RX_DROP_UNUSABLE;
  242. } else if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  243. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  244. /* remove ICV */
  245. skb_trim(rx->skb, rx->skb->len - WEP_ICV_LEN);
  246. }
  247. return RX_CONTINUE;
  248. }
  249. static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  250. {
  251. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  252. if (!info->control.hw_key) {
  253. if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key,
  254. tx->key->conf.keylen,
  255. tx->key->conf.keyidx))
  256. return -1;
  257. } else if (info->control.hw_key->flags &
  258. IEEE80211_KEY_FLAG_GENERATE_IV) {
  259. if (!ieee80211_wep_add_iv(tx->local, skb,
  260. tx->key->conf.keylen,
  261. tx->key->conf.keyidx))
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. ieee80211_tx_result
  267. ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx)
  268. {
  269. struct sk_buff *skb;
  270. ieee80211_tx_set_protected(tx);
  271. skb = tx->skb;
  272. do {
  273. if (wep_encrypt_skb(tx, skb) < 0) {
  274. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  275. return TX_DROP;
  276. }
  277. } while ((skb = skb->next));
  278. return TX_CONTINUE;
  279. }