ieee80211_crypt_wep.c 6.9 KB

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