ieee80211_crypt_wep.c 6.1 KB

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