您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <gpxe/net80211.h>
  20. #include <gpxe/sec80211.h>
  21. #include <gpxe/crypto.h>
  22. #include <gpxe/arc4.h>
  23. #include <gpxe/crc32.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. /** @file
  28. *
  29. * The WEP wireless encryption method (insecure!)
  30. *
  31. * The data field in a WEP-encrypted packet contains a 3-byte
  32. * initialisation vector, one-byte Key ID field (only the bottom two
  33. * bits are ever used), encrypted data, and a 4-byte encrypted CRC of
  34. * the plaintext data, called the ICV. To decrypt it, the IV is
  35. * prepended to the shared key and the data stream (including ICV) is
  36. * run through the ARC4 stream cipher; if the ICV matches a CRC32
  37. * calculated on the plaintext, the packet is valid.
  38. *
  39. * For efficiency and code-size reasons, this file assumes it is
  40. * running on a little-endian machine.
  41. */
  42. /** Length of WEP initialisation vector */
  43. #define WEP_IV_LEN 3
  44. /** Length of WEP key ID byte */
  45. #define WEP_KID_LEN 1
  46. /** Length of WEP ICV checksum */
  47. #define WEP_ICV_LEN 4
  48. /** Maximum length of WEP key */
  49. #define WEP_MAX_KEY 16
  50. /** Amount of data placed before the encrypted bytes */
  51. #define WEP_HEADER_LEN 4
  52. /** Amount of data placed after the encrypted bytes */
  53. #define WEP_TRAILER_LEN 4
  54. /** Total WEP overhead bytes */
  55. #define WEP_OVERHEAD 8
  56. /** Context for WEP encryption and decryption */
  57. struct wep_ctx
  58. {
  59. /** Encoded WEP key
  60. *
  61. * The actual key bytes are stored beginning at offset 3, to
  62. * leave room for easily inserting the IV before a particular
  63. * operation.
  64. */
  65. u8 key[WEP_IV_LEN + WEP_MAX_KEY];
  66. /** Length of WEP key (not including IV bytes) */
  67. int keylen;
  68. /** ARC4 context */
  69. struct arc4_ctx arc4;
  70. };
  71. /**
  72. * Initialize WEP algorithm
  73. *
  74. * @v crypto 802.11 cryptographic algorithm
  75. * @v key WEP key to use
  76. * @v keylen Length of WEP key
  77. * @v rsc Initial receive sequence counter (unused)
  78. * @ret rc Return status code
  79. *
  80. * Standard key lengths are 5 and 13 bytes; 16-byte keys are
  81. * occasionally supported as an extension to the standard.
  82. */
  83. static int wep_init ( struct net80211_crypto *crypto, const void *key,
  84. int keylen, const void *rsc __unused )
  85. {
  86. struct wep_ctx *ctx = crypto->priv;
  87. ctx->keylen = ( keylen > WEP_MAX_KEY ? WEP_MAX_KEY : keylen );
  88. memcpy ( ctx->key + WEP_IV_LEN, key, ctx->keylen );
  89. return 0;
  90. }
  91. /**
  92. * Encrypt packet using WEP
  93. *
  94. * @v crypto 802.11 cryptographic algorithm
  95. * @v iob I/O buffer of plaintext packet
  96. * @ret eiob Newly allocated I/O buffer for encrypted packet, or NULL
  97. *
  98. * If memory allocation fails, @c NULL is returned.
  99. */
  100. static struct io_buffer * wep_encrypt ( struct net80211_crypto *crypto,
  101. struct io_buffer *iob )
  102. {
  103. struct wep_ctx *ctx = crypto->priv;
  104. struct io_buffer *eiob;
  105. struct ieee80211_frame *hdr;
  106. const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
  107. int datalen = iob_len ( iob ) - hdrlen;
  108. int newlen = hdrlen + datalen + WEP_OVERHEAD;
  109. u32 iv, icv;
  110. eiob = alloc_iob ( newlen );
  111. if ( ! eiob )
  112. return NULL;
  113. memcpy ( iob_put ( eiob, hdrlen ), iob->data, hdrlen );
  114. hdr = eiob->data;
  115. hdr->fc |= IEEE80211_FC_PROTECTED;
  116. /* Calculate IV, put it in the header (with key ID byte = 0), and
  117. set it up at the start of the encryption key. */
  118. iv = random() & 0xffffff; /* IV in bottom 3 bytes, top byte = KID = 0 */
  119. memcpy ( iob_put ( eiob, WEP_HEADER_LEN ), &iv, WEP_HEADER_LEN );
  120. memcpy ( ctx->key, &iv, WEP_IV_LEN );
  121. /* Encrypt the data using RC4 */
  122. cipher_setkey ( &arc4_algorithm, &ctx->arc4, ctx->key,
  123. ctx->keylen + WEP_IV_LEN );
  124. cipher_encrypt ( &arc4_algorithm, &ctx->arc4, iob->data + hdrlen,
  125. iob_put ( eiob, datalen ), datalen );
  126. /* Add ICV */
  127. icv = ~crc32_le ( ~0, iob->data + hdrlen, datalen );
  128. cipher_encrypt ( &arc4_algorithm, &ctx->arc4, &icv,
  129. iob_put ( eiob, WEP_ICV_LEN ), WEP_ICV_LEN );
  130. return eiob;
  131. }
  132. /**
  133. * Decrypt packet using WEP
  134. *
  135. * @v crypto 802.11 cryptographic algorithm
  136. * @v eiob I/O buffer of encrypted packet
  137. * @ret iob Newly allocated I/O buffer for plaintext packet, or NULL
  138. *
  139. * If a consistency check for the decryption fails (usually indicating
  140. * an invalid key), @c NULL is returned.
  141. */
  142. static struct io_buffer * wep_decrypt ( struct net80211_crypto *crypto,
  143. struct io_buffer *eiob )
  144. {
  145. struct wep_ctx *ctx = crypto->priv;
  146. struct io_buffer *iob;
  147. struct ieee80211_frame *hdr;
  148. const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
  149. int datalen = iob_len ( eiob ) - hdrlen - WEP_OVERHEAD;
  150. int newlen = hdrlen + datalen;
  151. u32 iv, icv, crc;
  152. iob = alloc_iob ( newlen );
  153. if ( ! iob )
  154. return NULL;
  155. memcpy ( iob_put ( iob, hdrlen ), eiob->data, hdrlen );
  156. hdr = iob->data;
  157. hdr->fc &= ~IEEE80211_FC_PROTECTED;
  158. /* Strip off IV and use it to initialize cryptosystem */
  159. memcpy ( &iv, eiob->data + hdrlen, 4 );
  160. iv &= 0xffffff; /* ignore key ID byte */
  161. memcpy ( ctx->key, &iv, WEP_IV_LEN );
  162. /* Decrypt the data using RC4 */
  163. cipher_setkey ( &arc4_algorithm, &ctx->arc4, ctx->key,
  164. ctx->keylen + WEP_IV_LEN );
  165. cipher_decrypt ( &arc4_algorithm, &ctx->arc4, eiob->data + hdrlen +
  166. WEP_HEADER_LEN, iob_put ( iob, datalen ), datalen );
  167. /* Strip off ICV and verify it */
  168. cipher_decrypt ( &arc4_algorithm, &ctx->arc4, eiob->data + hdrlen +
  169. WEP_HEADER_LEN + datalen, &icv, WEP_ICV_LEN );
  170. crc = ~crc32_le ( ~0, iob->data + hdrlen, datalen );
  171. if ( crc != icv ) {
  172. DBGC ( crypto, "WEP %p CRC mismatch: expect %08x, get %08x\n",
  173. crypto, icv, crc );
  174. free_iob ( iob );
  175. return NULL;
  176. }
  177. return iob;
  178. }
  179. /** WEP cryptosystem for 802.11 */
  180. struct net80211_crypto wep_crypto __net80211_crypto = {
  181. .algorithm = NET80211_CRYPT_WEP,
  182. .init = wep_init,
  183. .encrypt = wep_encrypt,
  184. .decrypt = wep_decrypt,
  185. .priv_len = sizeof ( struct wep_ctx ),
  186. };
  187. /**
  188. * Initialize trivial 802.11 security handshaker
  189. *
  190. * @v dev 802.11 device
  191. * @v ctx Security handshaker
  192. *
  193. * This simply fetches a WEP key from netX/key, and if it exists,
  194. * installs WEP cryptography on the 802.11 device. No real handshaking
  195. * is performed.
  196. */
  197. static int trivial_init ( struct net80211_device *dev )
  198. {
  199. u8 key[WEP_MAX_KEY]; /* support up to 128-bit keys */
  200. int len;
  201. int rc;
  202. if ( dev->associating &&
  203. dev->associating->crypto == NET80211_CRYPT_NONE )
  204. return 0; /* no crypto? OK. */
  205. len = fetch_setting ( netdev_settings ( dev->netdev ),
  206. &net80211_key_setting, key, WEP_MAX_KEY );
  207. if ( len <= 0 ) {
  208. DBGC ( dev, "802.11 %p cannot do WEP without a key\n", dev );
  209. return -EACCES;
  210. }
  211. /* Full 128-bit keys are a nonstandard extension, but they're
  212. utterly trivial to support, so we do. */
  213. if ( len != 5 && len != 13 && len != 16 ) {
  214. DBGC ( dev, "802.11 %p invalid WEP key length %d\n",
  215. dev, len );
  216. return -EINVAL;
  217. }
  218. DBGC ( dev, "802.11 %p installing %d-bit WEP\n", dev, len * 8 );
  219. rc = sec80211_install ( &dev->crypto, NET80211_CRYPT_WEP, key, len,
  220. NULL );
  221. if ( rc < 0 )
  222. return rc;
  223. return 0;
  224. }
  225. /**
  226. * Check for key change on trivial 802.11 security handshaker
  227. *
  228. * @v dev 802.11 device
  229. * @v ctx Security handshaker
  230. */
  231. static int trivial_change_key ( struct net80211_device *dev )
  232. {
  233. u8 key[WEP_MAX_KEY];
  234. int len;
  235. int change = 0;
  236. /* If going from WEP to clear, or something else to WEP, reassociate. */
  237. if ( ! dev->crypto || ( dev->crypto->init != wep_init ) )
  238. change ^= 1;
  239. len = fetch_setting ( netdev_settings ( dev->netdev ),
  240. &net80211_key_setting, key, WEP_MAX_KEY );
  241. if ( len <= 0 )
  242. change ^= 1;
  243. /* Changing crypto type => return nonzero to reassociate. */
  244. if ( change )
  245. return -EINVAL;
  246. /* Going from no crypto to still no crypto => nothing to do. */
  247. if ( len <= 0 )
  248. return 0;
  249. /* Otherwise, reinitialise WEP with new key. */
  250. return wep_init ( dev->crypto, key, len, NULL );
  251. }
  252. /** Trivial 802.11 security handshaker */
  253. struct net80211_handshaker trivial_handshaker __net80211_handshaker = {
  254. .protocol = NET80211_SECPROT_NONE,
  255. .init = trivial_init,
  256. .change_key = trivial_change_key,
  257. .priv_len = 0,
  258. };