wep.c 8.3 KB

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