lib80211_crypt_wep.c 7.3 KB

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