ieee80211_crypt_wep.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Host AP crypt: host-based WEP encryption implementation for Host AP driver
  3. *
  4. * Copyright (c) 2002-2004, Jouni Malinen <j@w1.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. See README and COPYING for
  9. * more details.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/random.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/mm.h>
  18. #include <asm/string.h>
  19. #include <net/ieee80211.h>
  20. #include <linux/crypto.h>
  21. #include <asm/scatterlist.h>
  22. #include <linux/crc32.h>
  23. MODULE_AUTHOR("Jouni Malinen");
  24. MODULE_DESCRIPTION("Host AP crypt: WEP");
  25. MODULE_LICENSE("GPL");
  26. struct prism2_wep_data {
  27. u32 iv;
  28. #define WEP_KEY_LEN 13
  29. u8 key[WEP_KEY_LEN + 1];
  30. u8 key_len;
  31. u8 key_idx;
  32. struct crypto_blkcipher *tx_tfm;
  33. struct crypto_blkcipher *rx_tfm;
  34. };
  35. static void *prism2_wep_init(int keyidx)
  36. {
  37. struct prism2_wep_data *priv;
  38. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  39. if (priv == NULL)
  40. goto fail;
  41. priv->key_idx = keyidx;
  42. priv->tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  43. if (IS_ERR(priv->tx_tfm)) {
  44. printk(KERN_DEBUG "ieee80211_crypt_wep: could not allocate "
  45. "crypto API arc4\n");
  46. priv->tx_tfm = NULL;
  47. goto fail;
  48. }
  49. priv->rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  50. if (IS_ERR(priv->rx_tfm)) {
  51. printk(KERN_DEBUG "ieee80211_crypt_wep: could not allocate "
  52. "crypto API arc4\n");
  53. priv->rx_tfm = NULL;
  54. goto fail;
  55. }
  56. /* start WEP IV from a random value */
  57. get_random_bytes(&priv->iv, 4);
  58. return priv;
  59. fail:
  60. if (priv) {
  61. if (priv->tx_tfm)
  62. crypto_free_blkcipher(priv->tx_tfm);
  63. if (priv->rx_tfm)
  64. crypto_free_blkcipher(priv->rx_tfm);
  65. kfree(priv);
  66. }
  67. return NULL;
  68. }
  69. static void prism2_wep_deinit(void *priv)
  70. {
  71. struct prism2_wep_data *_priv = priv;
  72. if (_priv) {
  73. if (_priv->tx_tfm)
  74. crypto_free_blkcipher(_priv->tx_tfm);
  75. if (_priv->rx_tfm)
  76. crypto_free_blkcipher(_priv->rx_tfm);
  77. }
  78. kfree(priv);
  79. }
  80. /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
  81. static int prism2_wep_build_iv(struct sk_buff *skb, int hdr_len,
  82. u8 *key, int keylen, void *priv)
  83. {
  84. struct prism2_wep_data *wep = priv;
  85. u32 klen, len;
  86. u8 *pos;
  87. if (skb_headroom(skb) < 4 || skb->len < hdr_len)
  88. return -1;
  89. len = skb->len - hdr_len;
  90. pos = skb_push(skb, 4);
  91. memmove(pos, pos + 4, hdr_len);
  92. pos += hdr_len;
  93. klen = 3 + wep->key_len;
  94. wep->iv++;
  95. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
  96. * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
  97. * can be used to speedup attacks, so avoid using them. */
  98. if ((wep->iv & 0xff00) == 0xff00) {
  99. u8 B = (wep->iv >> 16) & 0xff;
  100. if (B >= 3 && B < klen)
  101. wep->iv += 0x0100;
  102. }
  103. /* Prepend 24-bit IV to RC4 key and TX frame */
  104. *pos++ = (wep->iv >> 16) & 0xff;
  105. *pos++ = (wep->iv >> 8) & 0xff;
  106. *pos++ = wep->iv & 0xff;
  107. *pos++ = wep->key_idx << 6;
  108. return 0;
  109. }
  110. /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
  111. * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
  112. * so the payload length increases with 8 bytes.
  113. *
  114. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  115. */
  116. static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  117. {
  118. struct prism2_wep_data *wep = priv;
  119. struct blkcipher_desc desc = { .tfm = wep->tx_tfm };
  120. u32 crc, klen, len;
  121. u8 *pos, *icv;
  122. struct scatterlist sg;
  123. u8 key[WEP_KEY_LEN + 3];
  124. /* other checks are in prism2_wep_build_iv */
  125. if (skb_tailroom(skb) < 4)
  126. return -1;
  127. /* add the IV to the frame */
  128. if (prism2_wep_build_iv(skb, hdr_len, NULL, 0, priv))
  129. return -1;
  130. /* Copy the IV into the first 3 bytes of the key */
  131. skb_copy_from_linear_data_offset(skb, hdr_len, key, 3);
  132. /* Copy rest of the WEP key (the secret part) */
  133. memcpy(key + 3, wep->key, wep->key_len);
  134. len = skb->len - hdr_len - 4;
  135. pos = skb->data + hdr_len + 4;
  136. klen = 3 + wep->key_len;
  137. /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
  138. crc = ~crc32_le(~0, pos, len);
  139. icv = skb_put(skb, 4);
  140. icv[0] = crc;
  141. icv[1] = crc >> 8;
  142. icv[2] = crc >> 16;
  143. icv[3] = crc >> 24;
  144. crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
  145. sg.page = virt_to_page(pos);
  146. sg.offset = offset_in_page(pos);
  147. sg.length = len + 4;
  148. return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
  149. }
  150. /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
  151. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  152. * ICV (4 bytes). len includes both IV and ICV.
  153. *
  154. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  155. * failure. If frame is OK, IV and ICV will be removed.
  156. */
  157. static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  158. {
  159. struct prism2_wep_data *wep = priv;
  160. struct blkcipher_desc desc = { .tfm = wep->rx_tfm };
  161. u32 crc, klen, plen;
  162. u8 key[WEP_KEY_LEN + 3];
  163. u8 keyidx, *pos, icv[4];
  164. struct scatterlist sg;
  165. if (skb->len < hdr_len + 8)
  166. return -1;
  167. pos = skb->data + hdr_len;
  168. key[0] = *pos++;
  169. key[1] = *pos++;
  170. key[2] = *pos++;
  171. keyidx = *pos++ >> 6;
  172. if (keyidx != wep->key_idx)
  173. return -1;
  174. klen = 3 + wep->key_len;
  175. /* Copy rest of the WEP key (the secret part) */
  176. memcpy(key + 3, wep->key, wep->key_len);
  177. /* Apply RC4 to data and compute CRC32 over decrypted data */
  178. plen = skb->len - hdr_len - 8;
  179. crypto_blkcipher_setkey(wep->rx_tfm, key, klen);
  180. sg.page = virt_to_page(pos);
  181. sg.offset = offset_in_page(pos);
  182. sg.length = plen + 4;
  183. if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4))
  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 prism2_wep_set_key(void *key, int len, u8 * seq, void *priv)
  201. {
  202. struct prism2_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 prism2_wep_get_key(void *key, int len, u8 * seq, void *priv)
  210. {
  211. struct prism2_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 char *prism2_wep_print_stats(char *p, void *priv)
  218. {
  219. struct prism2_wep_data *wep = priv;
  220. p += sprintf(p, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  221. return p;
  222. }
  223. static struct ieee80211_crypto_ops ieee80211_crypt_wep = {
  224. .name = "WEP",
  225. .init = prism2_wep_init,
  226. .deinit = prism2_wep_deinit,
  227. .build_iv = prism2_wep_build_iv,
  228. .encrypt_mpdu = prism2_wep_encrypt,
  229. .decrypt_mpdu = prism2_wep_decrypt,
  230. .encrypt_msdu = NULL,
  231. .decrypt_msdu = NULL,
  232. .set_key = prism2_wep_set_key,
  233. .get_key = prism2_wep_get_key,
  234. .print_stats = prism2_wep_print_stats,
  235. .extra_mpdu_prefix_len = 4, /* IV */
  236. .extra_mpdu_postfix_len = 4, /* ICV */
  237. .owner = THIS_MODULE,
  238. };
  239. static int __init ieee80211_crypto_wep_init(void)
  240. {
  241. return ieee80211_register_crypto_ops(&ieee80211_crypt_wep);
  242. }
  243. static void __exit ieee80211_crypto_wep_exit(void)
  244. {
  245. ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep);
  246. }
  247. module_init(ieee80211_crypto_wep_init);
  248. module_exit(ieee80211_crypto_wep_exit);