hostap_crypt_wep.c 6.4 KB

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