wep.c 9.0 KB

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