ieee80211_crypto_wep.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* $OpenBSD: ieee80211_crypto_wep.c,v 1.13 2015/07/15 22:16:42 deraadt Exp $ */
  2. /*-
  3. * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * This code implements Wired Equivalent Privacy (WEP) defined in
  19. * IEEE Std 802.11-2007 section 8.2.1.
  20. */
  21. #include <sys/param.h>
  22. #include <sys/systm.h>
  23. #include <sys/mbuf.h>
  24. #include <sys/malloc.h>
  25. #include <sys/kernel.h>
  26. #include <sys/socket.h>
  27. #include <sys/endian.h>
  28. #include <net/if.h>
  29. #include <net/if_dl.h>
  30. #include <net/if_media.h>
  31. #include <net/if_arp.h>
  32. #include <netinet/in.h>
  33. #include <netinet/if_ether.h>
  34. #include <net80211/ieee80211_var.h>
  35. #include <net80211/ieee80211_crypto.h>
  36. #include <crypto/arc4.h>
  37. /* WEP software crypto context */
  38. struct ieee80211_wep_ctx {
  39. struct rc4_ctx rc4;
  40. u_int32_t iv;
  41. };
  42. /*
  43. * Initialize software crypto context. This function can be overridden
  44. * by drivers doing hardware crypto.
  45. */
  46. int
  47. ieee80211_wep_set_key(struct ieee80211com *ic, struct ieee80211_key *k)
  48. {
  49. struct ieee80211_wep_ctx *ctx;
  50. ctx = malloc(sizeof(*ctx), M_DEVBUF, M_NOWAIT | M_ZERO);
  51. if (ctx == NULL)
  52. return ENOMEM;
  53. k->k_priv = ctx;
  54. return 0;
  55. }
  56. void
  57. ieee80211_wep_delete_key(struct ieee80211com *ic, struct ieee80211_key *k)
  58. {
  59. if (k->k_priv != NULL)
  60. free(k->k_priv, M_DEVBUF, 0);
  61. k->k_priv = NULL;
  62. }
  63. /* shortcut */
  64. #define IEEE80211_WEP_HDRLEN \
  65. (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
  66. struct mbuf *
  67. ieee80211_wep_encrypt(struct ieee80211com *ic, struct mbuf *m0,
  68. struct ieee80211_key *k)
  69. {
  70. struct ieee80211_wep_ctx *ctx = k->k_priv;
  71. u_int8_t wepseed[16];
  72. const struct ieee80211_frame *wh;
  73. struct mbuf *n0, *m, *n;
  74. u_int8_t *ivp, *icvp;
  75. u_int32_t iv, crc;
  76. int left, moff, noff, len, hdrlen;
  77. MGET(n0, M_DONTWAIT, m0->m_type);
  78. if (n0 == NULL)
  79. goto nospace;
  80. if (m_dup_pkthdr(n0, m0, M_DONTWAIT))
  81. goto nospace;
  82. n0->m_pkthdr.len += IEEE80211_WEP_HDRLEN;
  83. n0->m_len = MHLEN;
  84. if (n0->m_pkthdr.len >= MINCLSIZE - IEEE80211_WEP_CRCLEN) {
  85. MCLGET(n0, M_DONTWAIT);
  86. if (n0->m_flags & M_EXT)
  87. n0->m_len = n0->m_ext.ext_size;
  88. }
  89. if (n0->m_len > n0->m_pkthdr.len)
  90. n0->m_len = n0->m_pkthdr.len;
  91. /* copy 802.11 header */
  92. wh = mtod(m0, struct ieee80211_frame *);
  93. hdrlen = ieee80211_get_hdrlen(wh);
  94. memcpy(mtod(n0, caddr_t), wh, hdrlen);
  95. /* select a new IV for every MPDU */
  96. iv = (ctx->iv != 0) ? ctx->iv : arc4random();
  97. /* skip weak IVs from Fluhrer/Mantin/Shamir */
  98. if (iv >= 0x03ff00 && (iv & 0xf8ff00) == 0x00ff00)
  99. iv += 0x000100;
  100. ctx->iv = iv + 1;
  101. ivp = mtod(n0, u_int8_t *) + hdrlen;
  102. ivp[0] = iv;
  103. ivp[1] = iv >> 8;
  104. ivp[2] = iv >> 16;
  105. ivp[3] = k->k_id << 6;
  106. /* compute WEP seed: concatenate IV and WEP Key */
  107. memcpy(wepseed, ivp, IEEE80211_WEP_IVLEN);
  108. memcpy(wepseed + IEEE80211_WEP_IVLEN, k->k_key, k->k_len);
  109. rc4_keysetup(&ctx->rc4, wepseed, IEEE80211_WEP_IVLEN + k->k_len);
  110. /* encrypt frame body and compute WEP ICV */
  111. m = m0;
  112. n = n0;
  113. moff = hdrlen;
  114. noff = hdrlen + IEEE80211_WEP_HDRLEN;
  115. left = m0->m_pkthdr.len - moff;
  116. crc = ~0;
  117. while (left > 0) {
  118. if (moff == m->m_len) {
  119. /* nothing left to copy from m */
  120. m = m->m_next;
  121. moff = 0;
  122. }
  123. if (noff == n->m_len) {
  124. /* n is full and there's more data to copy */
  125. MGET(n->m_next, M_DONTWAIT, n->m_type);
  126. if (n->m_next == NULL)
  127. goto nospace;
  128. n = n->m_next;
  129. n->m_len = MLEN;
  130. if (left >= MINCLSIZE - IEEE80211_WEP_CRCLEN) {
  131. MCLGET(n, M_DONTWAIT);
  132. if (n->m_flags & M_EXT)
  133. n->m_len = n->m_ext.ext_size;
  134. }
  135. if (n->m_len > left)
  136. n->m_len = left;
  137. noff = 0;
  138. }
  139. len = min(m->m_len - moff, n->m_len - noff);
  140. crc = ether_crc32_le_update(crc, mtod(m, caddr_t) + moff, len);
  141. rc4_crypt(&ctx->rc4, mtod(m, caddr_t) + moff,
  142. mtod(n, caddr_t) + noff, len);
  143. moff += len;
  144. noff += len;
  145. left -= len;
  146. }
  147. /* reserve trailing space for WEP ICV */
  148. if (M_TRAILINGSPACE(n) < IEEE80211_WEP_CRCLEN) {
  149. MGET(n->m_next, M_DONTWAIT, n->m_type);
  150. if (n->m_next == NULL)
  151. goto nospace;
  152. n = n->m_next;
  153. n->m_len = 0;
  154. }
  155. /* finalize WEP ICV */
  156. icvp = mtod(n, caddr_t) + n->m_len;
  157. crc = ~crc;
  158. icvp[0] = crc;
  159. icvp[1] = crc >> 8;
  160. icvp[2] = crc >> 16;
  161. icvp[3] = crc >> 24;
  162. rc4_crypt(&ctx->rc4, icvp, icvp, IEEE80211_WEP_CRCLEN);
  163. n->m_len += IEEE80211_WEP_CRCLEN;
  164. n0->m_pkthdr.len += IEEE80211_WEP_CRCLEN;
  165. m_freem(m0);
  166. return n0;
  167. nospace:
  168. ic->ic_stats.is_tx_nombuf++;
  169. m_freem(m0);
  170. m_freem(n0);
  171. return NULL;
  172. }
  173. struct mbuf *
  174. ieee80211_wep_decrypt(struct ieee80211com *ic, struct mbuf *m0,
  175. struct ieee80211_key *k)
  176. {
  177. struct ieee80211_wep_ctx *ctx = k->k_priv;
  178. struct ieee80211_frame *wh;
  179. u_int8_t wepseed[16];
  180. u_int32_t crc, crc0;
  181. u_int8_t *ivp;
  182. struct mbuf *n0, *m, *n;
  183. int hdrlen, left, moff, noff, len;
  184. wh = mtod(m0, struct ieee80211_frame *);
  185. hdrlen = ieee80211_get_hdrlen(wh);
  186. if (m0->m_pkthdr.len < hdrlen + IEEE80211_WEP_TOTLEN) {
  187. m_freem(m0);
  188. return NULL;
  189. }
  190. /* concatenate IV and WEP Key */
  191. ivp = (u_int8_t *)wh + hdrlen;
  192. memcpy(wepseed, ivp, IEEE80211_WEP_IVLEN);
  193. memcpy(wepseed + IEEE80211_WEP_IVLEN, k->k_key, k->k_len);
  194. rc4_keysetup(&ctx->rc4, wepseed, IEEE80211_WEP_IVLEN + k->k_len);
  195. MGET(n0, M_DONTWAIT, m0->m_type);
  196. if (n0 == NULL)
  197. goto nospace;
  198. if (m_dup_pkthdr(n0, m0, M_DONTWAIT))
  199. goto nospace;
  200. n0->m_pkthdr.len -= IEEE80211_WEP_TOTLEN;
  201. n0->m_len = MHLEN;
  202. if (n0->m_pkthdr.len >= MINCLSIZE) {
  203. MCLGET(n0, M_DONTWAIT);
  204. if (n0->m_flags & M_EXT)
  205. n0->m_len = n0->m_ext.ext_size;
  206. }
  207. if (n0->m_len > n0->m_pkthdr.len)
  208. n0->m_len = n0->m_pkthdr.len;
  209. /* copy 802.11 header and clear protected bit */
  210. memcpy(mtod(n0, caddr_t), wh, hdrlen);
  211. wh = mtod(n0, struct ieee80211_frame *);
  212. wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
  213. /* decrypt frame body and compute WEP ICV */
  214. m = m0;
  215. n = n0;
  216. moff = hdrlen + IEEE80211_WEP_HDRLEN;
  217. noff = hdrlen;
  218. left = n0->m_pkthdr.len - noff;
  219. crc = ~0;
  220. while (left > 0) {
  221. if (moff == m->m_len) {
  222. /* nothing left to copy from m */
  223. m = m->m_next;
  224. moff = 0;
  225. }
  226. if (noff == n->m_len) {
  227. /* n is full and there's more data to copy */
  228. MGET(n->m_next, M_DONTWAIT, n->m_type);
  229. if (n->m_next == NULL)
  230. goto nospace;
  231. n = n->m_next;
  232. n->m_len = MLEN;
  233. if (left >= MINCLSIZE) {
  234. MCLGET(n, M_DONTWAIT);
  235. if (n->m_flags & M_EXT)
  236. n->m_len = n->m_ext.ext_size;
  237. }
  238. if (n->m_len > left)
  239. n->m_len = left;
  240. noff = 0;
  241. }
  242. len = min(m->m_len - moff, n->m_len - noff);
  243. rc4_crypt(&ctx->rc4, mtod(m, caddr_t) + moff,
  244. mtod(n, caddr_t) + noff, len);
  245. crc = ether_crc32_le_update(crc, mtod(n, caddr_t) + noff, len);
  246. moff += len;
  247. noff += len;
  248. left -= len;
  249. }
  250. /* decrypt ICV and compare it with calculated ICV */
  251. m_copydata(m, moff, IEEE80211_WEP_CRCLEN, (caddr_t)&crc0);
  252. rc4_crypt(&ctx->rc4, (caddr_t)&crc0, (caddr_t)&crc0,
  253. IEEE80211_WEP_CRCLEN);
  254. crc = ~crc;
  255. if (crc != letoh32(crc0)) {
  256. ic->ic_stats.is_rx_decryptcrc++;
  257. m_freem(m0);
  258. m_freem(n0);
  259. return NULL;
  260. }
  261. m_freem(m0);
  262. return n0;
  263. nospace:
  264. ic->ic_stats.is_rx_nombuf++;
  265. m_freem(m0);
  266. m_freem(n0);
  267. return NULL;
  268. }