lib80211_crypt_wep.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <crypto/skcipher.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_skcipher *tx_tfm;
  34. struct crypto_skcipher *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_skcipher("ecb(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_skcipher("ecb(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_skcipher(priv->tx_tfm);
  59. crypto_free_skcipher(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_skcipher(_priv->tx_tfm);
  69. crypto_free_skcipher(_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. SKCIPHER_REQUEST_ON_STACK(req, wep->tx_tfm);
  112. u32 crc, klen, len;
  113. u8 *pos, *icv;
  114. struct scatterlist sg;
  115. u8 key[WEP_KEY_LEN + 3];
  116. int err;
  117. /* other checks are in lib80211_wep_build_iv */
  118. if (skb_tailroom(skb) < 4)
  119. return -1;
  120. /* add the IV to the frame */
  121. if (lib80211_wep_build_iv(skb, hdr_len, NULL, 0, priv))
  122. return -1;
  123. /* Copy the IV into the first 3 bytes of the key */
  124. skb_copy_from_linear_data_offset(skb, hdr_len, key, 3);
  125. /* Copy rest of the WEP key (the secret part) */
  126. memcpy(key + 3, wep->key, wep->key_len);
  127. len = skb->len - hdr_len - 4;
  128. pos = skb->data + hdr_len + 4;
  129. klen = 3 + wep->key_len;
  130. /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
  131. crc = ~crc32_le(~0, pos, len);
  132. icv = skb_put(skb, 4);
  133. icv[0] = crc;
  134. icv[1] = crc >> 8;
  135. icv[2] = crc >> 16;
  136. icv[3] = crc >> 24;
  137. crypto_skcipher_setkey(wep->tx_tfm, key, klen);
  138. sg_init_one(&sg, pos, len + 4);
  139. skcipher_request_set_tfm(req, wep->tx_tfm);
  140. skcipher_request_set_callback(req, 0, NULL, NULL);
  141. skcipher_request_set_crypt(req, &sg, &sg, len + 4, NULL);
  142. err = crypto_skcipher_encrypt(req);
  143. skcipher_request_zero(req);
  144. return err;
  145. }
  146. /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
  147. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  148. * ICV (4 bytes). len includes both IV and ICV.
  149. *
  150. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  151. * failure. If frame is OK, IV and ICV will be removed.
  152. */
  153. static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  154. {
  155. struct lib80211_wep_data *wep = priv;
  156. SKCIPHER_REQUEST_ON_STACK(req, wep->rx_tfm);
  157. u32 crc, klen, plen;
  158. u8 key[WEP_KEY_LEN + 3];
  159. u8 keyidx, *pos, icv[4];
  160. struct scatterlist sg;
  161. int err;
  162. if (skb->len < hdr_len + 8)
  163. return -1;
  164. pos = skb->data + hdr_len;
  165. key[0] = *pos++;
  166. key[1] = *pos++;
  167. key[2] = *pos++;
  168. keyidx = *pos++ >> 6;
  169. if (keyidx != wep->key_idx)
  170. return -1;
  171. klen = 3 + wep->key_len;
  172. /* Copy rest of the WEP key (the secret part) */
  173. memcpy(key + 3, wep->key, wep->key_len);
  174. /* Apply RC4 to data and compute CRC32 over decrypted data */
  175. plen = skb->len - hdr_len - 8;
  176. crypto_skcipher_setkey(wep->rx_tfm, key, klen);
  177. sg_init_one(&sg, pos, plen + 4);
  178. skcipher_request_set_tfm(req, wep->rx_tfm);
  179. skcipher_request_set_callback(req, 0, NULL, NULL);
  180. skcipher_request_set_crypt(req, &sg, &sg, plen + 4, NULL);
  181. err = crypto_skcipher_decrypt(req);
  182. skcipher_request_zero(req);
  183. if (err)
  184. return -7;
  185. crc = ~crc32_le(~0, pos, plen);
  186. icv[0] = crc;
  187. icv[1] = crc >> 8;
  188. icv[2] = crc >> 16;
  189. icv[3] = crc >> 24;
  190. if (memcmp(icv, pos + plen, 4) != 0) {
  191. /* ICV mismatch - drop frame */
  192. return -2;
  193. }
  194. /* Remove IV and ICV */
  195. memmove(skb->data + 4, skb->data, hdr_len);
  196. skb_pull(skb, 4);
  197. skb_trim(skb, skb->len - 4);
  198. return 0;
  199. }
  200. static int lib80211_wep_set_key(void *key, int len, u8 * seq, void *priv)
  201. {
  202. struct lib80211_wep_data *wep = priv;
  203. if (len < 0 || len > WEP_KEY_LEN)
  204. return -1;
  205. memcpy(wep->key, key, len);
  206. wep->key_len = len;
  207. return 0;
  208. }
  209. static int lib80211_wep_get_key(void *key, int len, u8 * seq, void *priv)
  210. {
  211. struct lib80211_wep_data *wep = priv;
  212. if (len < wep->key_len)
  213. return -1;
  214. memcpy(key, wep->key, wep->key_len);
  215. return wep->key_len;
  216. }
  217. static void lib80211_wep_print_stats(struct seq_file *m, void *priv)
  218. {
  219. struct lib80211_wep_data *wep = priv;
  220. seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  221. }
  222. static struct lib80211_crypto_ops lib80211_crypt_wep = {
  223. .name = "WEP",
  224. .init = lib80211_wep_init,
  225. .deinit = lib80211_wep_deinit,
  226. .encrypt_mpdu = lib80211_wep_encrypt,
  227. .decrypt_mpdu = lib80211_wep_decrypt,
  228. .encrypt_msdu = NULL,
  229. .decrypt_msdu = NULL,
  230. .set_key = lib80211_wep_set_key,
  231. .get_key = lib80211_wep_get_key,
  232. .print_stats = lib80211_wep_print_stats,
  233. .extra_mpdu_prefix_len = 4, /* IV */
  234. .extra_mpdu_postfix_len = 4, /* ICV */
  235. .owner = THIS_MODULE,
  236. };
  237. static int __init lib80211_crypto_wep_init(void)
  238. {
  239. return lib80211_register_crypto_ops(&lib80211_crypt_wep);
  240. }
  241. static void __exit lib80211_crypto_wep_exit(void)
  242. {
  243. lib80211_unregister_crypto_ops(&lib80211_crypt_wep);
  244. }
  245. module_init(lib80211_crypto_wep_init);
  246. module_exit(lib80211_crypto_wep_exit);