ieee80211_crypt_wep.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Host AP crypt: host-based WEP encryption implementation for Host AP driver
  3. *
  4. * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.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/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/random.h>
  15. #include <linux/skbuff.h>
  16. #include <asm/string.h>
  17. #include <net/ieee80211.h>
  18. #include <linux/crypto.h>
  19. #include <asm/scatterlist.h>
  20. #include <linux/crc32.h>
  21. MODULE_AUTHOR("Jouni Malinen");
  22. MODULE_DESCRIPTION("Host AP crypt: WEP");
  23. MODULE_LICENSE("GPL");
  24. struct prism2_wep_data {
  25. u32 iv;
  26. #define WEP_KEY_LEN 13
  27. u8 key[WEP_KEY_LEN + 1];
  28. u8 key_len;
  29. u8 key_idx;
  30. struct crypto_tfm *tfm;
  31. };
  32. static void *prism2_wep_init(int keyidx)
  33. {
  34. struct prism2_wep_data *priv;
  35. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  36. if (priv == NULL)
  37. goto fail;
  38. priv->key_idx = keyidx;
  39. priv->tfm = crypto_alloc_tfm("arc4", 0);
  40. if (priv->tfm == NULL) {
  41. printk(KERN_DEBUG "ieee80211_crypt_wep: could not allocate "
  42. "crypto API arc4\n");
  43. goto fail;
  44. }
  45. /* start WEP IV from a random value */
  46. get_random_bytes(&priv->iv, 4);
  47. return priv;
  48. fail:
  49. if (priv) {
  50. if (priv->tfm)
  51. crypto_free_tfm(priv->tfm);
  52. kfree(priv);
  53. }
  54. return NULL;
  55. }
  56. static void prism2_wep_deinit(void *priv)
  57. {
  58. struct prism2_wep_data *_priv = priv;
  59. if (_priv && _priv->tfm)
  60. crypto_free_tfm(_priv->tfm);
  61. kfree(priv);
  62. }
  63. /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
  64. static int prism2_wep_build_iv(struct sk_buff *skb, int hdr_len,
  65. u8 *key, int keylen, void *priv)
  66. {
  67. struct prism2_wep_data *wep = priv;
  68. u32 klen, len;
  69. u8 *pos;
  70. if (skb_headroom(skb) < 4 || skb->len < hdr_len)
  71. return -1;
  72. len = skb->len - hdr_len;
  73. pos = skb_push(skb, 4);
  74. memmove(pos, pos + 4, hdr_len);
  75. pos += hdr_len;
  76. klen = 3 + wep->key_len;
  77. wep->iv++;
  78. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
  79. * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
  80. * can be used to speedup attacks, so avoid using them. */
  81. if ((wep->iv & 0xff00) == 0xff00) {
  82. u8 B = (wep->iv >> 16) & 0xff;
  83. if (B >= 3 && B < klen)
  84. wep->iv += 0x0100;
  85. }
  86. /* Prepend 24-bit IV to RC4 key and TX frame */
  87. *pos++ = (wep->iv >> 16) & 0xff;
  88. *pos++ = (wep->iv >> 8) & 0xff;
  89. *pos++ = wep->iv & 0xff;
  90. *pos++ = wep->key_idx << 6;
  91. return 0;
  92. }
  93. /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
  94. * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
  95. * so the payload length increases with 8 bytes.
  96. *
  97. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  98. */
  99. static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  100. {
  101. struct prism2_wep_data *wep = priv;
  102. u32 crc, klen, len;
  103. u8 *pos, *icv;
  104. struct scatterlist sg;
  105. u8 key[WEP_KEY_LEN + 3];
  106. /* other checks are in prism2_wep_build_iv */
  107. if (skb_tailroom(skb) < 4)
  108. return -1;
  109. /* add the IV to the frame */
  110. if (prism2_wep_build_iv(skb, hdr_len, NULL, 0, priv))
  111. return -1;
  112. /* Copy the IV into the first 3 bytes of the key */
  113. memcpy(key, skb->data + hdr_len, 3);
  114. /* Copy rest of the WEP key (the secret part) */
  115. memcpy(key + 3, wep->key, wep->key_len);
  116. len = skb->len - hdr_len - 4;
  117. pos = skb->data + hdr_len + 4;
  118. klen = 3 + wep->key_len;
  119. /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
  120. crc = ~crc32_le(~0, pos, len);
  121. icv = skb_put(skb, 4);
  122. icv[0] = crc;
  123. icv[1] = crc >> 8;
  124. icv[2] = crc >> 16;
  125. icv[3] = crc >> 24;
  126. crypto_cipher_setkey(wep->tfm, key, klen);
  127. sg.page = virt_to_page(pos);
  128. sg.offset = offset_in_page(pos);
  129. sg.length = len + 4;
  130. crypto_cipher_encrypt(wep->tfm, &sg, &sg, len + 4);
  131. return 0;
  132. }
  133. /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
  134. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  135. * ICV (4 bytes). len includes both IV and ICV.
  136. *
  137. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  138. * failure. If frame is OK, IV and ICV will be removed.
  139. */
  140. static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  141. {
  142. struct prism2_wep_data *wep = priv;
  143. u32 crc, klen, plen;
  144. u8 key[WEP_KEY_LEN + 3];
  145. u8 keyidx, *pos, icv[4];
  146. struct scatterlist sg;
  147. if (skb->len < hdr_len + 8)
  148. return -1;
  149. pos = skb->data + hdr_len;
  150. key[0] = *pos++;
  151. key[1] = *pos++;
  152. key[2] = *pos++;
  153. keyidx = *pos++ >> 6;
  154. if (keyidx != wep->key_idx)
  155. return -1;
  156. klen = 3 + wep->key_len;
  157. /* Copy rest of the WEP key (the secret part) */
  158. memcpy(key + 3, wep->key, wep->key_len);
  159. /* Apply RC4 to data and compute CRC32 over decrypted data */
  160. plen = skb->len - hdr_len - 8;
  161. crypto_cipher_setkey(wep->tfm, key, klen);
  162. sg.page = virt_to_page(pos);
  163. sg.offset = offset_in_page(pos);
  164. sg.length = plen + 4;
  165. crypto_cipher_decrypt(wep->tfm, &sg, &sg, plen + 4);
  166. crc = ~crc32_le(~0, pos, plen);
  167. icv[0] = crc;
  168. icv[1] = crc >> 8;
  169. icv[2] = crc >> 16;
  170. icv[3] = crc >> 24;
  171. if (memcmp(icv, pos + plen, 4) != 0) {
  172. /* ICV mismatch - drop frame */
  173. return -2;
  174. }
  175. /* Remove IV and ICV */
  176. memmove(skb->data + 4, skb->data, hdr_len);
  177. skb_pull(skb, 4);
  178. skb_trim(skb, skb->len - 4);
  179. return 0;
  180. }
  181. static int prism2_wep_set_key(void *key, int len, u8 * seq, void *priv)
  182. {
  183. struct prism2_wep_data *wep = priv;
  184. if (len < 0 || len > WEP_KEY_LEN)
  185. return -1;
  186. memcpy(wep->key, key, len);
  187. wep->key_len = len;
  188. return 0;
  189. }
  190. static int prism2_wep_get_key(void *key, int len, u8 * seq, void *priv)
  191. {
  192. struct prism2_wep_data *wep = priv;
  193. if (len < wep->key_len)
  194. return -1;
  195. memcpy(key, wep->key, wep->key_len);
  196. return wep->key_len;
  197. }
  198. static char *prism2_wep_print_stats(char *p, void *priv)
  199. {
  200. struct prism2_wep_data *wep = priv;
  201. p += sprintf(p, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  202. return p;
  203. }
  204. static struct ieee80211_crypto_ops ieee80211_crypt_wep = {
  205. .name = "WEP",
  206. .init = prism2_wep_init,
  207. .deinit = prism2_wep_deinit,
  208. .build_iv = prism2_wep_build_iv,
  209. .encrypt_mpdu = prism2_wep_encrypt,
  210. .decrypt_mpdu = prism2_wep_decrypt,
  211. .encrypt_msdu = NULL,
  212. .decrypt_msdu = NULL,
  213. .set_key = prism2_wep_set_key,
  214. .get_key = prism2_wep_get_key,
  215. .print_stats = prism2_wep_print_stats,
  216. .extra_mpdu_prefix_len = 4, /* IV */
  217. .extra_mpdu_postfix_len = 4, /* ICV */
  218. .owner = THIS_MODULE,
  219. };
  220. static int __init ieee80211_crypto_wep_init(void)
  221. {
  222. return ieee80211_register_crypto_ops(&ieee80211_crypt_wep);
  223. }
  224. static void __exit ieee80211_crypto_wep_exit(void)
  225. {
  226. ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep);
  227. }
  228. module_init(ieee80211_crypto_wep_init);
  229. module_exit(ieee80211_crypto_wep_exit);