lib80211_crypt_wep.c 6.8 KB

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