wep.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Software WEP encryption implementation
  4. * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi>
  5. * Copyright 2003, Instant802 Networks, Inc.
  6. */
  7. #include <linux/netdevice.h>
  8. #include <linux/types.h>
  9. #include <linux/random.h>
  10. #include <linux/compiler.h>
  11. #include <linux/crc32.h>
  12. #include <linux/crypto.h>
  13. #include <linux/err.h>
  14. #include <linux/mm.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/slab.h>
  17. #include <asm/unaligned.h>
  18. #include <net/mac80211.h>
  19. #include "ieee80211_i.h"
  20. #include "wep.h"
  21. void ieee80211_wep_init(struct ieee80211_local *local)
  22. {
  23. /* start WEP IV from a random value */
  24. get_random_bytes(&local->wep_iv, IEEE80211_WEP_IV_LEN);
  25. }
  26. static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen)
  27. {
  28. /*
  29. * Fluhrer, Mantin, and Shamir have reported weaknesses in the
  30. * key scheduling algorithm of RC4. At least IVs (KeyByte + 3,
  31. * 0xff, N) can be used to speedup attacks, so avoid using them.
  32. */
  33. if ((iv & 0xff00) == 0xff00) {
  34. u8 B = (iv >> 16) & 0xff;
  35. if (B >= 3 && B < 3 + keylen)
  36. return true;
  37. }
  38. return false;
  39. }
  40. static void ieee80211_wep_get_iv(struct ieee80211_local *local,
  41. int keylen, int keyidx, u8 *iv)
  42. {
  43. local->wep_iv++;
  44. if (ieee80211_wep_weak_iv(local->wep_iv, keylen))
  45. local->wep_iv += 0x0100;
  46. if (!iv)
  47. return;
  48. *iv++ = (local->wep_iv >> 16) & 0xff;
  49. *iv++ = (local->wep_iv >> 8) & 0xff;
  50. *iv++ = local->wep_iv & 0xff;
  51. *iv++ = keyidx << 6;
  52. }
  53. static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local,
  54. struct sk_buff *skb,
  55. int keylen, int keyidx)
  56. {
  57. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  58. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  59. unsigned int hdrlen;
  60. u8 *newhdr;
  61. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  62. if (WARN_ON(skb_headroom(skb) < IEEE80211_WEP_IV_LEN))
  63. return NULL;
  64. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  65. newhdr = skb_push(skb, IEEE80211_WEP_IV_LEN);
  66. memmove(newhdr, newhdr + IEEE80211_WEP_IV_LEN, hdrlen);
  67. /* the HW only needs room for the IV, but not the actual IV */
  68. if (info->control.hw_key &&
  69. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  70. return newhdr + hdrlen;
  71. ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen);
  72. return newhdr + hdrlen;
  73. }
  74. static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
  75. struct sk_buff *skb,
  76. struct ieee80211_key *key)
  77. {
  78. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  79. unsigned int hdrlen;
  80. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  81. memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen);
  82. skb_pull(skb, IEEE80211_WEP_IV_LEN);
  83. }
  84. /* Perform WEP encryption using given key. data buffer must have tailroom
  85. * for 4-byte ICV. data_len must not include this ICV. Note: this function
  86. * does _not_ add IV. data = RC4(data | CRC32(data)) */
  87. int ieee80211_wep_encrypt_data(struct arc4_ctx *ctx, u8 *rc4key,
  88. size_t klen, u8 *data, size_t data_len)
  89. {
  90. __le32 icv;
  91. icv = cpu_to_le32(~crc32_le(~0, data, data_len));
  92. put_unaligned(icv, (__le32 *)(data + data_len));
  93. arc4_setkey(ctx, rc4key, klen);
  94. arc4_crypt(ctx, data, data, data_len + IEEE80211_WEP_ICV_LEN);
  95. memzero_explicit(ctx, sizeof(*ctx));
  96. return 0;
  97. }
  98. /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the
  99. * beginning of the buffer 4 bytes of extra space (ICV) in the end of the
  100. * buffer will be added. Both IV and ICV will be transmitted, so the
  101. * payload length increases with 8 bytes.
  102. *
  103. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  104. */
  105. int ieee80211_wep_encrypt(struct ieee80211_local *local,
  106. struct sk_buff *skb,
  107. const u8 *key, int keylen, int keyidx)
  108. {
  109. u8 *iv;
  110. size_t len;
  111. u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
  112. if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN))
  113. return -1;
  114. iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx);
  115. if (!iv)
  116. return -1;
  117. len = skb->len - (iv + IEEE80211_WEP_IV_LEN - skb->data);
  118. /* Prepend 24-bit IV to RC4 key */
  119. memcpy(rc4key, iv, 3);
  120. /* Copy rest of the WEP key (the secret part) */
  121. memcpy(rc4key + 3, key, keylen);
  122. /* Add room for ICV */
  123. skb_put(skb, IEEE80211_WEP_ICV_LEN);
  124. return ieee80211_wep_encrypt_data(&local->wep_tx_ctx, rc4key, keylen + 3,
  125. iv + IEEE80211_WEP_IV_LEN, len);
  126. }
  127. /* Perform WEP decryption using given key. data buffer includes encrypted
  128. * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
  129. * Return 0 on success and -1 on ICV mismatch. */
  130. int ieee80211_wep_decrypt_data(struct arc4_ctx *ctx, u8 *rc4key,
  131. size_t klen, u8 *data, size_t data_len)
  132. {
  133. __le32 crc;
  134. arc4_setkey(ctx, rc4key, klen);
  135. arc4_crypt(ctx, data, data, data_len + IEEE80211_WEP_ICV_LEN);
  136. memzero_explicit(ctx, sizeof(*ctx));
  137. crc = cpu_to_le32(~crc32_le(~0, data, data_len));
  138. if (memcmp(&crc, data + data_len, IEEE80211_WEP_ICV_LEN) != 0)
  139. /* ICV mismatch */
  140. return -1;
  141. return 0;
  142. }
  143. /* Perform WEP decryption on given skb. Buffer includes whole WEP part of
  144. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  145. * ICV (4 bytes). skb->len includes both IV and ICV.
  146. *
  147. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  148. * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload
  149. * is moved to the beginning of the skb and skb length will be reduced.
  150. */
  151. static int ieee80211_wep_decrypt(struct ieee80211_local *local,
  152. struct sk_buff *skb,
  153. struct ieee80211_key *key)
  154. {
  155. u32 klen;
  156. u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
  157. u8 keyidx;
  158. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  159. unsigned int hdrlen;
  160. size_t len;
  161. int ret = 0;
  162. if (!ieee80211_has_protected(hdr->frame_control))
  163. return -1;
  164. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  165. if (skb->len < hdrlen + IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN)
  166. return -1;
  167. len = skb->len - hdrlen - IEEE80211_WEP_IV_LEN - IEEE80211_WEP_ICV_LEN;
  168. keyidx = skb->data[hdrlen + 3] >> 6;
  169. if (!key || keyidx != key->conf.keyidx)
  170. return -1;
  171. klen = 3 + key->conf.keylen;
  172. /* Prepend 24-bit IV to RC4 key */
  173. memcpy(rc4key, skb->data + hdrlen, 3);
  174. /* Copy rest of the WEP key (the secret part) */
  175. memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
  176. if (ieee80211_wep_decrypt_data(&local->wep_rx_ctx, rc4key, klen,
  177. skb->data + hdrlen +
  178. IEEE80211_WEP_IV_LEN, len))
  179. ret = -1;
  180. /* Trim ICV */
  181. skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN);
  182. /* Remove IV */
  183. memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen);
  184. skb_pull(skb, IEEE80211_WEP_IV_LEN);
  185. return ret;
  186. }
  187. ieee80211_rx_result
  188. ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx)
  189. {
  190. struct sk_buff *skb = rx->skb;
  191. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  192. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  193. __le16 fc = hdr->frame_control;
  194. if (!ieee80211_is_data(fc) && !ieee80211_is_auth(fc))
  195. return RX_CONTINUE;
  196. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  197. if (skb_linearize(rx->skb))
  198. return RX_DROP_UNUSABLE;
  199. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key))
  200. return RX_DROP_UNUSABLE;
  201. } else if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  202. if (!pskb_may_pull(rx->skb, ieee80211_hdrlen(fc) +
  203. IEEE80211_WEP_IV_LEN))
  204. return RX_DROP_UNUSABLE;
  205. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  206. /* remove ICV */
  207. if (!(status->flag & RX_FLAG_ICV_STRIPPED) &&
  208. pskb_trim(rx->skb, rx->skb->len - IEEE80211_WEP_ICV_LEN))
  209. return RX_DROP_UNUSABLE;
  210. }
  211. return RX_CONTINUE;
  212. }
  213. static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  214. {
  215. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  216. struct ieee80211_key_conf *hw_key = info->control.hw_key;
  217. if (!hw_key) {
  218. if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key,
  219. tx->key->conf.keylen,
  220. tx->key->conf.keyidx))
  221. return -1;
  222. } else if ((hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
  223. (hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  224. if (!ieee80211_wep_add_iv(tx->local, skb,
  225. tx->key->conf.keylen,
  226. tx->key->conf.keyidx))
  227. return -1;
  228. }
  229. return 0;
  230. }
  231. ieee80211_tx_result
  232. ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx)
  233. {
  234. struct sk_buff *skb;
  235. ieee80211_tx_set_protected(tx);
  236. skb_queue_walk(&tx->skbs, skb) {
  237. if (wep_encrypt_skb(tx, skb) < 0) {
  238. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  239. return TX_DROP;
  240. }
  241. }
  242. return TX_CONTINUE;
  243. }