ieee80211_crypt_wep.c 6.1 KB

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