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