rtllib_crypt_wep.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "rtllib.h"
  18. #include <linux/crypto.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/crc32.h>
  21. struct prism2_wep_data {
  22. u32 iv;
  23. #define WEP_KEY_LEN 13
  24. u8 key[WEP_KEY_LEN + 1];
  25. u8 key_len;
  26. u8 key_idx;
  27. struct crypto_blkcipher *tx_tfm;
  28. struct crypto_blkcipher *rx_tfm;
  29. };
  30. static void *prism2_wep_init(int keyidx)
  31. {
  32. struct prism2_wep_data *priv;
  33. priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
  34. if (priv == NULL)
  35. goto fail;
  36. priv->key_idx = keyidx;
  37. priv->tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  38. if (IS_ERR(priv->tx_tfm)) {
  39. pr_debug("rtllib_crypt_wep: could not allocate crypto API arc4\n");
  40. priv->tx_tfm = NULL;
  41. goto fail;
  42. }
  43. priv->rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  44. if (IS_ERR(priv->rx_tfm)) {
  45. pr_debug("rtllib_crypt_wep: could not allocate crypto API arc4\n");
  46. priv->rx_tfm = NULL;
  47. goto fail;
  48. }
  49. /* start WEP IV from a random value */
  50. get_random_bytes(&priv->iv, 4);
  51. return priv;
  52. fail:
  53. if (priv) {
  54. if (priv->tx_tfm)
  55. crypto_free_blkcipher(priv->tx_tfm);
  56. if (priv->rx_tfm)
  57. crypto_free_blkcipher(priv->rx_tfm);
  58. kfree(priv);
  59. }
  60. return NULL;
  61. }
  62. static void prism2_wep_deinit(void *priv)
  63. {
  64. struct prism2_wep_data *_priv = priv;
  65. if (_priv) {
  66. if (_priv->tx_tfm)
  67. crypto_free_blkcipher(_priv->tx_tfm);
  68. if (_priv->rx_tfm)
  69. crypto_free_blkcipher(_priv->rx_tfm);
  70. }
  71. kfree(priv);
  72. }
  73. /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
  74. * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
  75. * so the payload length increases with 8 bytes.
  76. *
  77. * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
  78. */
  79. static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
  80. {
  81. struct prism2_wep_data *wep = priv;
  82. u32 klen, len;
  83. u8 key[WEP_KEY_LEN + 3];
  84. u8 *pos;
  85. struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
  86. MAX_DEV_ADDR_SIZE);
  87. struct blkcipher_desc desc = {.tfm = wep->tx_tfm};
  88. u32 crc;
  89. u8 *icv;
  90. struct scatterlist sg;
  91. if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
  92. skb->len < hdr_len){
  93. pr_err("Error!!! headroom=%d tailroom=%d skblen=%d hdr_len=%d\n",
  94. skb_headroom(skb), skb_tailroom(skb), skb->len, hdr_len);
  95. return -1;
  96. }
  97. len = skb->len - hdr_len;
  98. pos = skb_push(skb, 4);
  99. memmove(pos, pos + 4, hdr_len);
  100. pos += hdr_len;
  101. klen = 3 + wep->key_len;
  102. wep->iv++;
  103. /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
  104. * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
  105. * can be used to speedup attacks, so avoid using them.
  106. */
  107. if ((wep->iv & 0xff00) == 0xff00) {
  108. u8 B = (wep->iv >> 16) & 0xff;
  109. if (B >= 3 && B < klen)
  110. wep->iv += 0x0100;
  111. }
  112. /* Prepend 24-bit IV to RC4 key and TX frame */
  113. *pos++ = key[0] = (wep->iv >> 16) & 0xff;
  114. *pos++ = key[1] = (wep->iv >> 8) & 0xff;
  115. *pos++ = key[2] = wep->iv & 0xff;
  116. *pos++ = wep->key_idx << 6;
  117. /* Copy rest of the WEP key (the secret part) */
  118. memcpy(key + 3, wep->key, wep->key_len);
  119. if (!tcb_desc->bHwSec) {
  120. /* Append little-endian CRC32 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. sg_init_one(&sg, pos, len+4);
  128. crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
  129. return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
  130. }
  131. return 0;
  132. }
  133. /* Perform WEP decryption on given struct buffer. Buffer includes whole WEP
  134. * part of the frame: IV (4 bytes), encrypted payload (including SNAP header),
  135. * ICV (4 bytes). len includes both IV and ICV.
  136. *
  137. * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
  138. * failure. If frame is OK, IV and ICV will be removed.
  139. */
  140. static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
  141. {
  142. struct prism2_wep_data *wep = priv;
  143. u32 klen, plen;
  144. u8 key[WEP_KEY_LEN + 3];
  145. u8 keyidx, *pos;
  146. struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
  147. MAX_DEV_ADDR_SIZE);
  148. struct blkcipher_desc desc = {.tfm = wep->rx_tfm};
  149. u32 crc;
  150. u8 icv[4];
  151. struct scatterlist sg;
  152. if (skb->len < hdr_len + 8)
  153. return -1;
  154. pos = skb->data + hdr_len;
  155. key[0] = *pos++;
  156. key[1] = *pos++;
  157. key[2] = *pos++;
  158. keyidx = *pos++ >> 6;
  159. if (keyidx != wep->key_idx)
  160. return -1;
  161. klen = 3 + wep->key_len;
  162. /* Copy rest of the WEP key (the secret part) */
  163. memcpy(key + 3, wep->key, wep->key_len);
  164. /* Apply RC4 to data and compute CRC32 over decrypted data */
  165. plen = skb->len - hdr_len - 8;
  166. if (!tcb_desc->bHwSec) {
  167. sg_init_one(&sg, pos, plen+4);
  168. crypto_blkcipher_setkey(wep->rx_tfm, key, klen);
  169. if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4))
  170. return -7;
  171. crc = ~crc32_le(~0, pos, plen);
  172. icv[0] = crc;
  173. icv[1] = crc >> 8;
  174. icv[2] = crc >> 16;
  175. icv[3] = crc >> 24;
  176. if (memcmp(icv, pos + plen, 4) != 0) {
  177. /* ICV mismatch - drop frame */
  178. return -2;
  179. }
  180. }
  181. /* Remove IV and ICV */
  182. memmove(skb->data + 4, skb->data, hdr_len);
  183. skb_pull(skb, 4);
  184. skb_trim(skb, skb->len - 4);
  185. return 0;
  186. }
  187. static int prism2_wep_set_key(void *key, int len, u8 *seq, void *priv)
  188. {
  189. struct prism2_wep_data *wep = priv;
  190. if (len < 0 || len > WEP_KEY_LEN)
  191. return -1;
  192. memcpy(wep->key, key, len);
  193. wep->key_len = len;
  194. return 0;
  195. }
  196. static int prism2_wep_get_key(void *key, int len, u8 *seq, void *priv)
  197. {
  198. struct prism2_wep_data *wep = priv;
  199. if (len < wep->key_len)
  200. return -1;
  201. memcpy(key, wep->key, wep->key_len);
  202. return wep->key_len;
  203. }
  204. static void prism2_wep_print_stats(struct seq_file *m, void *priv)
  205. {
  206. struct prism2_wep_data *wep = priv;
  207. seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
  208. }
  209. static struct lib80211_crypto_ops rtllib_crypt_wep = {
  210. .name = "R-WEP",
  211. .init = prism2_wep_init,
  212. .deinit = prism2_wep_deinit,
  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 rtllib_crypto_wep_init(void)
  225. {
  226. return lib80211_register_crypto_ops(&rtllib_crypt_wep);
  227. }
  228. static void __exit rtllib_crypto_wep_exit(void)
  229. {
  230. lib80211_unregister_crypto_ops(&rtllib_crypt_wep);
  231. }
  232. module_init(rtllib_crypto_wep_init);
  233. module_exit(rtllib_crypto_wep_exit);
  234. MODULE_LICENSE("GPL");