wep.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. struct ieee80211_key *key, u8 *iv)
  60. {
  61. local->wep_iv++;
  62. if (ieee80211_wep_weak_iv(local->wep_iv, key->conf.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++ = key->conf.keyidx << 6;
  70. }
  71. static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local,
  72. struct sk_buff *skb,
  73. struct ieee80211_key *key)
  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, 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. 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, struct sk_buff *skb,
  121. struct ieee80211_key *key)
  122. {
  123. u32 klen;
  124. u8 *rc4key, *iv;
  125. size_t len;
  126. if (!key || key->conf.alg != ALG_WEP)
  127. return -1;
  128. klen = 3 + key->conf.keylen;
  129. rc4key = kmalloc(klen, GFP_ATOMIC);
  130. if (!rc4key)
  131. return -1;
  132. iv = ieee80211_wep_add_iv(local, skb, key);
  133. if (!iv) {
  134. kfree(rc4key);
  135. return -1;
  136. }
  137. len = skb->len - (iv + WEP_IV_LEN - skb->data);
  138. /* Prepend 24-bit IV to RC4 key */
  139. memcpy(rc4key, iv, 3);
  140. /* Copy rest of the WEP key (the secret part) */
  141. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  142. /* Add room for ICV */
  143. skb_put(skb, WEP_ICV_LEN);
  144. ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, klen,
  145. iv + WEP_IV_LEN, len);
  146. kfree(rc4key);
  147. return 0;
  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. crypto_blkcipher_setkey(tfm, rc4key, klen);
  159. sg_init_one(&sg, data, data_len + WEP_ICV_LEN);
  160. crypto_blkcipher_decrypt(&desc, &sg, &sg, sg.length);
  161. crc = cpu_to_le32(~crc32_le(~0, data, data_len));
  162. if (memcmp(&crc, data + data_len, WEP_ICV_LEN) != 0)
  163. /* ICV mismatch */
  164. return -1;
  165. return 0;
  166. }
  167. /* Perform WEP decryption on given skb. Buffer includes whole WEP part of
  168. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  169. * ICV (4 bytes). skb->len includes both IV and ICV.
  170. *
  171. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  172. * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload
  173. * is moved to the beginning of the skb and skb length will be reduced.
  174. */
  175. int ieee80211_wep_decrypt(struct ieee80211_local *local, struct sk_buff *skb,
  176. struct ieee80211_key *key)
  177. {
  178. u32 klen;
  179. u8 *rc4key;
  180. u8 keyidx;
  181. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  182. unsigned int hdrlen;
  183. size_t len;
  184. int ret = 0;
  185. if (!ieee80211_has_protected(hdr->frame_control))
  186. return -1;
  187. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  188. if (skb->len < hdrlen + WEP_IV_LEN + WEP_ICV_LEN)
  189. return -1;
  190. len = skb->len - hdrlen - WEP_IV_LEN - WEP_ICV_LEN;
  191. keyidx = skb->data[hdrlen + 3] >> 6;
  192. if (!key || keyidx != key->conf.keyidx || key->conf.alg != ALG_WEP)
  193. return -1;
  194. klen = 3 + key->conf.keylen;
  195. rc4key = kmalloc(klen, GFP_ATOMIC);
  196. if (!rc4key)
  197. return -1;
  198. /* Prepend 24-bit IV to RC4 key */
  199. memcpy(rc4key, skb->data + hdrlen, 3);
  200. /* Copy rest of the WEP key (the secret part) */
  201. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  202. if (ieee80211_wep_decrypt_data(local->wep_rx_tfm, rc4key, klen,
  203. skb->data + hdrlen + WEP_IV_LEN,
  204. len))
  205. ret = -1;
  206. kfree(rc4key);
  207. /* Trim ICV */
  208. skb_trim(skb, skb->len - WEP_ICV_LEN);
  209. /* Remove IV */
  210. memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
  211. skb_pull(skb, WEP_IV_LEN);
  212. return ret;
  213. }
  214. bool ieee80211_wep_is_weak_iv(struct sk_buff *skb, struct ieee80211_key *key)
  215. {
  216. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  217. unsigned int hdrlen;
  218. u8 *ivpos;
  219. u32 iv;
  220. if (!ieee80211_has_protected(hdr->frame_control))
  221. return false;
  222. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  223. ivpos = skb->data + hdrlen;
  224. iv = (ivpos[0] << 16) | (ivpos[1] << 8) | ivpos[2];
  225. return ieee80211_wep_weak_iv(iv, key->conf.keylen);
  226. }
  227. ieee80211_rx_result
  228. ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx)
  229. {
  230. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  231. if (!ieee80211_is_data(hdr->frame_control) &&
  232. !ieee80211_is_auth(hdr->frame_control))
  233. return RX_CONTINUE;
  234. if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
  235. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key))
  236. return RX_DROP_UNUSABLE;
  237. } else if (!(rx->status->flag & RX_FLAG_IV_STRIPPED)) {
  238. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  239. /* remove ICV */
  240. skb_trim(rx->skb, rx->skb->len - WEP_ICV_LEN);
  241. }
  242. return RX_CONTINUE;
  243. }
  244. static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  245. {
  246. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  247. if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  248. if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
  249. return -1;
  250. } else {
  251. info->control.hw_key = &tx->key->conf;
  252. if (tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) {
  253. if (!ieee80211_wep_add_iv(tx->local, skb, tx->key))
  254. return -1;
  255. }
  256. }
  257. return 0;
  258. }
  259. ieee80211_tx_result
  260. ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx)
  261. {
  262. struct sk_buff *skb;
  263. ieee80211_tx_set_protected(tx);
  264. skb = tx->skb;
  265. do {
  266. if (wep_encrypt_skb(tx, skb) < 0) {
  267. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  268. return TX_DROP;
  269. }
  270. } while ((skb = skb->next));
  271. return TX_CONTINUE;
  272. }