ieee80211_crypt_wep.c 7.3 KB

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