Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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