wep.c 8.8 KB

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