lib80211_crypt_wep.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lib80211 crypt: host-based WEP encryption implementation for lib80211
  4. *
  5. * Copyright (c) 2002-2004, Jouni Malinen <j@w1.fi>
  6. * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/fips.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/random.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/mm.h>
  17. #include <asm/string.h>
  18. #include <net/lib80211.h>
  19. #include <crypto/arc4.h>
  20. #include <linux/crc32.h>
  21. MODULE_AUTHOR("Jouni Malinen");
  22. MODULE_DESCRIPTION("lib80211 crypt: WEP");
  23. MODULE_LICENSE("GPL");
  24. struct lib80211_wep_data {
  25. u32 iv;
  26. #define WEP_KEY_LEN 13
  27. u8 key[WEP_KEY_LEN + 1];
  28. u8 key_len;
  29. u8 key_idx;
  30. struct arc4_ctx tx_ctx;
  31. struct arc4_ctx rx_ctx;
  32. };
  33. static void *lib80211_wep_init(int keyidx)
  34. {
  35. struct lib80211_wep_data *priv;
  36. if (fips_enabled)
  37. return NULL;
  38. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  39. if (priv == NULL)
  40. return NULL;
  41. priv->key_idx = keyidx;
  42. /* start WEP IV from a random value */
  43. get_random_bytes(&priv->iv, 4);
  44. return priv;
  45. }
  46. static void lib80211_wep_deinit(void *priv)
  47. {
  48. kfree_sensitive(priv);
  49. }
  50. /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
  51. static int lib80211_wep_build_iv(struct sk_buff *skb, int hdr_len,
  52. u8 *key, int keylen, void *priv)
  53. {
  54. struct lib80211_wep_data *wep = priv;
  55. u32 klen;
  56. u8 *pos;
  57. if (skb_headroom(skb) < 4 || skb->len < hdr_len)
  58. return -1;
  59. pos = skb_push(skb, 4);
  60. memmove(pos, pos + 4, hdr_len);
  61. pos += hdr_len;
  62. klen = 3 + wep->key_len;
  63. wep->iv++;
  64. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
  65. * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
  66. * can be used to speedup attacks, so avoid using them. */
  67. if ((wep->iv & 0xff00) == 0xff00) {
  68. u8 B = (wep->iv >> 16) & 0xff;
  69. if (B >= 3 && B < klen)
  70. wep->iv += 0x0100;
  71. }
  72. /* Prepend 24-bit IV to RC4 key and TX frame */
  73. *pos++ = (wep->iv >> 16) & 0xff;
  74. *pos++ = (wep->iv >> 8) & 0xff;
  75. *pos++ = wep->iv & 0xff;
  76. *pos++ = wep->key_idx << 6;
  77. return 0;
  78. }
  79. /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
  80. * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
  81. * so the payload length increases with 8 bytes.
  82. *
  83. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  84. */
  85. static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  86. {
  87. struct lib80211_wep_data *wep = priv;
  88. u32 crc, klen, len;
  89. u8 *pos, *icv;
  90. u8 key[WEP_KEY_LEN + 3];
  91. /* other checks are in lib80211_wep_build_iv */
  92. if (skb_tailroom(skb) < 4)
  93. return -1;
  94. /* add the IV to the frame */
  95. if (lib80211_wep_build_iv(skb, hdr_len, NULL, 0, priv))
  96. return -1;
  97. /* Copy the IV into the first 3 bytes of the key */
  98. skb_copy_from_linear_data_offset(skb, hdr_len, key, 3);
  99. /* Copy rest of the WEP key (the secret part) */
  100. memcpy(key + 3, wep->key, wep->key_len);
  101. len = skb->len - hdr_len - 4;
  102. pos = skb->data + hdr_len + 4;
  103. klen = 3 + wep->key_len;
  104. /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
  105. crc = ~crc32_le(~0, pos, len);
  106. icv = skb_put(skb, 4);
  107. icv[0] = crc;
  108. icv[1] = crc >> 8;
  109. icv[2] = crc >> 16;
  110. icv[3] = crc >> 24;
  111. arc4_setkey(&wep->tx_ctx, key, klen);
  112. arc4_crypt(&wep->tx_ctx, pos, pos, len + 4);
  113. return 0;
  114. }
  115. /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
  116. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  117. * ICV (4 bytes). len includes both IV and ICV.
  118. *
  119. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  120. * failure. If frame is OK, IV and ICV will be removed.
  121. */
  122. static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  123. {
  124. struct lib80211_wep_data *wep = priv;
  125. u32 crc, klen, plen;
  126. u8 key[WEP_KEY_LEN + 3];
  127. u8 keyidx, *pos, icv[4];
  128. if (skb->len < hdr_len + 8)
  129. return -1;
  130. pos = skb->data + hdr_len;
  131. key[0] = *pos++;
  132. key[1] = *pos++;
  133. key[2] = *pos++;
  134. keyidx = *pos++ >> 6;
  135. if (keyidx != wep->key_idx)
  136. return -1;
  137. klen = 3 + wep->key_len;
  138. /* Copy rest of the WEP key (the secret part) */
  139. memcpy(key + 3, wep->key, wep->key_len);
  140. /* Apply RC4 to data and compute CRC32 over decrypted data */
  141. plen = skb->len - hdr_len - 8;
  142. arc4_setkey(&wep->rx_ctx, key, klen);
  143. arc4_crypt(&wep->rx_ctx, pos, pos, plen + 4);
  144. crc = ~crc32_le(~0, pos, plen);
  145. icv[0] = crc;
  146. icv[1] = crc >> 8;
  147. icv[2] = crc >> 16;
  148. icv[3] = crc >> 24;
  149. if (memcmp(icv, pos + plen, 4) != 0) {
  150. /* ICV mismatch - drop frame */
  151. return -2;
  152. }
  153. /* Remove IV and ICV */
  154. memmove(skb->data + 4, skb->data, hdr_len);
  155. skb_pull(skb, 4);
  156. skb_trim(skb, skb->len - 4);
  157. return 0;
  158. }
  159. static int lib80211_wep_set_key(void *key, int len, u8 * seq, void *priv)
  160. {
  161. struct lib80211_wep_data *wep = priv;
  162. if (len < 0 || len > WEP_KEY_LEN)
  163. return -1;
  164. memcpy(wep->key, key, len);
  165. wep->key_len = len;
  166. return 0;
  167. }
  168. static int lib80211_wep_get_key(void *key, int len, u8 * seq, void *priv)
  169. {
  170. struct lib80211_wep_data *wep = priv;
  171. if (len < wep->key_len)
  172. return -1;
  173. memcpy(key, wep->key, wep->key_len);
  174. return wep->key_len;
  175. }
  176. static void lib80211_wep_print_stats(struct seq_file *m, void *priv)
  177. {
  178. struct lib80211_wep_data *wep = priv;
  179. seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  180. }
  181. static struct lib80211_crypto_ops lib80211_crypt_wep = {
  182. .name = "WEP",
  183. .init = lib80211_wep_init,
  184. .deinit = lib80211_wep_deinit,
  185. .encrypt_mpdu = lib80211_wep_encrypt,
  186. .decrypt_mpdu = lib80211_wep_decrypt,
  187. .encrypt_msdu = NULL,
  188. .decrypt_msdu = NULL,
  189. .set_key = lib80211_wep_set_key,
  190. .get_key = lib80211_wep_get_key,
  191. .print_stats = lib80211_wep_print_stats,
  192. .extra_mpdu_prefix_len = 4, /* IV */
  193. .extra_mpdu_postfix_len = 4, /* ICV */
  194. .owner = THIS_MODULE,
  195. };
  196. static int __init lib80211_crypto_wep_init(void)
  197. {
  198. return lib80211_register_crypto_ops(&lib80211_crypt_wep);
  199. }
  200. static void __exit lib80211_crypto_wep_exit(void)
  201. {
  202. lib80211_unregister_crypto_ops(&lib80211_crypt_wep);
  203. }
  204. module_init(lib80211_crypto_wep_init);
  205. module_exit(lib80211_crypto_wep_exit);