ieee80211_crypt_wep.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
  66. static int prism2_wep_build_iv(struct sk_buff *skb, int hdr_len, void *priv)
  67. {
  68. struct prism2_wep_data *wep = priv;
  69. u32 klen, len;
  70. u8 *pos;
  71. if (skb_headroom(skb) < 4 || skb->len < hdr_len)
  72. return -1;
  73. len = skb->len - hdr_len;
  74. pos = skb_push(skb, 4);
  75. memmove(pos, pos + 4, hdr_len);
  76. pos += hdr_len;
  77. klen = 3 + wep->key_len;
  78. wep->iv++;
  79. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
  80. * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
  81. * can be used to speedup attacks, so avoid using them. */
  82. if ((wep->iv & 0xff00) == 0xff00) {
  83. u8 B = (wep->iv >> 16) & 0xff;
  84. if (B >= 3 && B < klen)
  85. wep->iv += 0x0100;
  86. }
  87. /* Prepend 24-bit IV to RC4 key and TX frame */
  88. *pos++ = (wep->iv >> 16) & 0xff;
  89. *pos++ = (wep->iv >> 8) & 0xff;
  90. *pos++ = wep->iv & 0xff;
  91. *pos++ = wep->key_idx << 6;
  92. return 0;
  93. }
  94. /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
  95. * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
  96. * so the payload length increases with 8 bytes.
  97. *
  98. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  99. */
  100. static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  101. {
  102. struct prism2_wep_data *wep = priv;
  103. u32 crc, klen, len;
  104. u8 *pos, *icv;
  105. struct scatterlist sg;
  106. u8 key[WEP_KEY_LEN + 3];
  107. /* other checks are in prism2_wep_build_iv */
  108. if (skb_tailroom(skb) < 4)
  109. return -1;
  110. /* add the IV to the frame */
  111. if (prism2_wep_build_iv(skb, hdr_len, priv))
  112. return -1;
  113. /* Copy the IV into the first 3 bytes of the key */
  114. memcpy(key, skb->data + hdr_len, 3);
  115. /* Copy rest of the WEP key (the secret part) */
  116. memcpy(key + 3, wep->key, wep->key_len);
  117. len = skb->len - hdr_len - 4;
  118. pos = skb->data + hdr_len + 4;
  119. klen = 3 + wep->key_len;
  120. /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
  121. crc = ~crc32_le(~0, pos, len);
  122. icv = skb_put(skb, 4);
  123. icv[0] = crc;
  124. icv[1] = crc >> 8;
  125. icv[2] = crc >> 16;
  126. icv[3] = crc >> 24;
  127. crypto_cipher_setkey(wep->tfm, key, klen);
  128. sg.page = virt_to_page(pos);
  129. sg.offset = offset_in_page(pos);
  130. sg.length = len + 4;
  131. crypto_cipher_encrypt(wep->tfm, &sg, &sg, len + 4);
  132. return 0;
  133. }
  134. /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
  135. * the frame: IV (4 bytes), encrypted payload (including SNAP header),
  136. * ICV (4 bytes). len includes both IV and ICV.
  137. *
  138. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  139. * failure. If frame is OK, IV and ICV will be removed.
  140. */
  141. static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  142. {
  143. struct prism2_wep_data *wep = priv;
  144. u32 crc, klen, plen;
  145. u8 key[WEP_KEY_LEN + 3];
  146. u8 keyidx, *pos, icv[4];
  147. struct scatterlist sg;
  148. if (skb->len < hdr_len + 8)
  149. return -1;
  150. pos = skb->data + hdr_len;
  151. key[0] = *pos++;
  152. key[1] = *pos++;
  153. key[2] = *pos++;
  154. keyidx = *pos++ >> 6;
  155. if (keyidx != wep->key_idx)
  156. return -1;
  157. klen = 3 + wep->key_len;
  158. /* Copy rest of the WEP key (the secret part) */
  159. memcpy(key + 3, wep->key, wep->key_len);
  160. /* Apply RC4 to data and compute CRC32 over decrypted data */
  161. plen = skb->len - hdr_len - 8;
  162. crypto_cipher_setkey(wep->tfm, key, klen);
  163. sg.page = virt_to_page(pos);
  164. sg.offset = offset_in_page(pos);
  165. sg.length = plen + 4;
  166. crypto_cipher_decrypt(wep->tfm, &sg, &sg, plen + 4);
  167. crc = ~crc32_le(~0, pos, plen);
  168. icv[0] = crc;
  169. icv[1] = crc >> 8;
  170. icv[2] = crc >> 16;
  171. icv[3] = crc >> 24;
  172. if (memcmp(icv, pos + plen, 4) != 0) {
  173. /* ICV mismatch - drop frame */
  174. return -2;
  175. }
  176. /* Remove IV and ICV */
  177. memmove(skb->data + 4, skb->data, hdr_len);
  178. skb_pull(skb, 4);
  179. skb_trim(skb, skb->len - 4);
  180. return 0;
  181. }
  182. static int prism2_wep_set_key(void *key, int len, u8 * seq, void *priv)
  183. {
  184. struct prism2_wep_data *wep = priv;
  185. if (len < 0 || len > WEP_KEY_LEN)
  186. return -1;
  187. memcpy(wep->key, key, len);
  188. wep->key_len = len;
  189. return 0;
  190. }
  191. static int prism2_wep_get_key(void *key, int len, u8 * seq, void *priv)
  192. {
  193. struct prism2_wep_data *wep = priv;
  194. if (len < wep->key_len)
  195. return -1;
  196. memcpy(key, wep->key, wep->key_len);
  197. return wep->key_len;
  198. }
  199. static char *prism2_wep_print_stats(char *p, void *priv)
  200. {
  201. struct prism2_wep_data *wep = priv;
  202. p += sprintf(p, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  203. return p;
  204. }
  205. static struct ieee80211_crypto_ops ieee80211_crypt_wep = {
  206. .name = "WEP",
  207. .init = prism2_wep_init,
  208. .deinit = prism2_wep_deinit,
  209. .build_iv = prism2_wep_build_iv,
  210. .encrypt_mpdu = prism2_wep_encrypt,
  211. .decrypt_mpdu = prism2_wep_decrypt,
  212. .encrypt_msdu = NULL,
  213. .decrypt_msdu = NULL,
  214. .set_key = prism2_wep_set_key,
  215. .get_key = prism2_wep_get_key,
  216. .print_stats = prism2_wep_print_stats,
  217. .extra_mpdu_prefix_len = 4, /* IV */
  218. .extra_mpdu_postfix_len = 4, /* ICV */
  219. .owner = THIS_MODULE,
  220. };
  221. static int __init ieee80211_crypto_wep_init(void)
  222. {
  223. return ieee80211_register_crypto_ops(&ieee80211_crypt_wep);
  224. }
  225. static void __exit ieee80211_crypto_wep_exit(void)
  226. {
  227. ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep);
  228. }
  229. module_init(ieee80211_crypto_wep_init);
  230. module_exit(ieee80211_crypto_wep_exit);