lib80211_crypt_wep.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_blkcipher *tx_tfm;
  34. struct crypto_blkcipher *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_blkcipher("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_blkcipher("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. if (priv->tx_tfm)
  59. crypto_free_blkcipher(priv->tx_tfm);
  60. if (priv->rx_tfm)
  61. crypto_free_blkcipher(priv->rx_tfm);
  62. kfree(priv);
  63. }
  64. return NULL;
  65. }
  66. static void lib80211_wep_deinit(void *priv)
  67. {
  68. struct lib80211_wep_data *_priv = priv;
  69. if (_priv) {
  70. if (_priv->tx_tfm)
  71. crypto_free_blkcipher(_priv->tx_tfm);
  72. if (_priv->rx_tfm)
  73. crypto_free_blkcipher(_priv->rx_tfm);
  74. }
  75. kfree(priv);
  76. }
  77. /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
  78. static int lib80211_wep_build_iv(struct sk_buff *skb, int hdr_len,
  79. u8 *key, int keylen, void *priv)
  80. {
  81. struct lib80211_wep_data *wep = priv;
  82. u32 klen;
  83. u8 *pos;
  84. if (skb_headroom(skb) < 4 || skb->len < hdr_len)
  85. return -1;
  86. pos = skb_push(skb, 4);
  87. memmove(pos, pos + 4, hdr_len);
  88. pos += hdr_len;
  89. klen = 3 + wep->key_len;
  90. wep->iv++;
  91. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
  92. * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
  93. * can be used to speedup attacks, so avoid using them. */
  94. if ((wep->iv & 0xff00) == 0xff00) {
  95. u8 B = (wep->iv >> 16) & 0xff;
  96. if (B >= 3 && B < klen)
  97. wep->iv += 0x0100;
  98. }
  99. /* Prepend 24-bit IV to RC4 key and TX frame */
  100. *pos++ = (wep->iv >> 16) & 0xff;
  101. *pos++ = (wep->iv >> 8) & 0xff;
  102. *pos++ = wep->iv & 0xff;
  103. *pos++ = wep->key_idx << 6;
  104. return 0;
  105. }
  106. /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
  107. * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
  108. * so the payload length increases with 8 bytes.
  109. *
  110. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  111. */
  112. static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  113. {
  114. struct lib80211_wep_data *wep = priv;
  115. struct blkcipher_desc desc = { .tfm = wep->tx_tfm };
  116. u32 crc, klen, len;
  117. u8 *pos, *icv;
  118. struct scatterlist sg;
  119. u8 key[WEP_KEY_LEN + 3];
  120. /* other checks are in lib80211_wep_build_iv */
  121. if (skb_tailroom(skb) < 4)
  122. return -1;
  123. /* add the IV to the frame */
  124. if (lib80211_wep_build_iv(skb, hdr_len, NULL, 0, priv))
  125. return -1;
  126. /* Copy the IV into the first 3 bytes of the key */
  127. skb_copy_from_linear_data_offset(skb, hdr_len, key, 3);
  128. /* Copy rest of the WEP key (the secret part) */
  129. memcpy(key + 3, wep->key, wep->key_len);
  130. len = skb->len - hdr_len - 4;
  131. pos = skb->data + hdr_len + 4;
  132. klen = 3 + wep->key_len;
  133. /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
  134. crc = ~crc32_le(~0, pos, len);
  135. icv = skb_put(skb, 4);
  136. icv[0] = crc;
  137. icv[1] = crc >> 8;
  138. icv[2] = crc >> 16;
  139. icv[3] = crc >> 24;
  140. crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
  141. sg_init_one(&sg, pos, len + 4);
  142. return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
  143. }
  144. /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
  145. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  146. * ICV (4 bytes). 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.
  150. */
  151. static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  152. {
  153. struct lib80211_wep_data *wep = priv;
  154. struct blkcipher_desc desc = { .tfm = wep->rx_tfm };
  155. u32 crc, klen, plen;
  156. u8 key[WEP_KEY_LEN + 3];
  157. u8 keyidx, *pos, icv[4];
  158. struct scatterlist sg;
  159. if (skb->len < hdr_len + 8)
  160. return -1;
  161. pos = skb->data + hdr_len;
  162. key[0] = *pos++;
  163. key[1] = *pos++;
  164. key[2] = *pos++;
  165. keyidx = *pos++ >> 6;
  166. if (keyidx != wep->key_idx)
  167. return -1;
  168. klen = 3 + wep->key_len;
  169. /* Copy rest of the WEP key (the secret part) */
  170. memcpy(key + 3, wep->key, wep->key_len);
  171. /* Apply RC4 to data and compute CRC32 over decrypted data */
  172. plen = skb->len - hdr_len - 8;
  173. crypto_blkcipher_setkey(wep->rx_tfm, key, klen);
  174. sg_init_one(&sg, pos, plen + 4);
  175. if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4))
  176. return -7;
  177. crc = ~crc32_le(~0, pos, plen);
  178. icv[0] = crc;
  179. icv[1] = crc >> 8;
  180. icv[2] = crc >> 16;
  181. icv[3] = crc >> 24;
  182. if (memcmp(icv, pos + plen, 4) != 0) {
  183. /* ICV mismatch - drop frame */
  184. return -2;
  185. }
  186. /* Remove IV and ICV */
  187. memmove(skb->data + 4, skb->data, hdr_len);
  188. skb_pull(skb, 4);
  189. skb_trim(skb, skb->len - 4);
  190. return 0;
  191. }
  192. static int lib80211_wep_set_key(void *key, int len, u8 * seq, void *priv)
  193. {
  194. struct lib80211_wep_data *wep = priv;
  195. if (len < 0 || len > WEP_KEY_LEN)
  196. return -1;
  197. memcpy(wep->key, key, len);
  198. wep->key_len = len;
  199. return 0;
  200. }
  201. static int lib80211_wep_get_key(void *key, int len, u8 * seq, void *priv)
  202. {
  203. struct lib80211_wep_data *wep = priv;
  204. if (len < wep->key_len)
  205. return -1;
  206. memcpy(key, wep->key, wep->key_len);
  207. return wep->key_len;
  208. }
  209. static void lib80211_wep_print_stats(struct seq_file *m, void *priv)
  210. {
  211. struct lib80211_wep_data *wep = priv;
  212. seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  213. }
  214. static struct lib80211_crypto_ops lib80211_crypt_wep = {
  215. .name = "WEP",
  216. .init = lib80211_wep_init,
  217. .deinit = lib80211_wep_deinit,
  218. .encrypt_mpdu = lib80211_wep_encrypt,
  219. .decrypt_mpdu = lib80211_wep_decrypt,
  220. .encrypt_msdu = NULL,
  221. .decrypt_msdu = NULL,
  222. .set_key = lib80211_wep_set_key,
  223. .get_key = lib80211_wep_get_key,
  224. .print_stats = lib80211_wep_print_stats,
  225. .extra_mpdu_prefix_len = 4, /* IV */
  226. .extra_mpdu_postfix_len = 4, /* ICV */
  227. .owner = THIS_MODULE,
  228. };
  229. static int __init lib80211_crypto_wep_init(void)
  230. {
  231. return lib80211_register_crypto_ops(&lib80211_crypt_wep);
  232. }
  233. static void __exit lib80211_crypto_wep_exit(void)
  234. {
  235. lib80211_unregister_crypto_ops(&lib80211_crypt_wep);
  236. }
  237. module_init(lib80211_crypto_wep_init);
  238. module_exit(lib80211_crypto_wep_exit);