ieee80211_crypt_wep.c 6.4 KB

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