wep.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <gpxe/net80211.h>
00022 #include <gpxe/sec80211.h>
00023 #include <gpxe/crypto.h>
00024 #include <gpxe/arc4.h>
00025 #include <gpxe/crc32.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 
00030 /** @file
00031  *
00032  * The WEP wireless encryption method (insecure!)
00033  *
00034  * The data field in a WEP-encrypted packet contains a 3-byte
00035  * initialisation vector, one-byte Key ID field (only the bottom two
00036  * bits are ever used), encrypted data, and a 4-byte encrypted CRC of
00037  * the plaintext data, called the ICV. To decrypt it, the IV is
00038  * prepended to the shared key and the data stream (including ICV) is
00039  * run through the ARC4 stream cipher; if the ICV matches a CRC32
00040  * calculated on the plaintext, the packet is valid.
00041  *
00042  * For efficiency and code-size reasons, this file assumes it is
00043  * running on a little-endian machine.
00044  */
00045 
00046 /** Length of WEP initialisation vector */
00047 #define WEP_IV_LEN      3
00048 
00049 /** Length of WEP key ID byte */
00050 #define WEP_KID_LEN     1
00051 
00052 /** Length of WEP ICV checksum */
00053 #define WEP_ICV_LEN     4
00054 
00055 /** Maximum length of WEP key */
00056 #define WEP_MAX_KEY     16
00057 
00058 /** Amount of data placed before the encrypted bytes */
00059 #define WEP_HEADER_LEN  4
00060 
00061 /** Amount of data placed after the encrypted bytes */
00062 #define WEP_TRAILER_LEN 4
00063 
00064 /** Total WEP overhead bytes */
00065 #define WEP_OVERHEAD    8
00066 
00067 /** Context for WEP encryption and decryption */
00068 struct wep_ctx
00069 {
00070         /** Encoded WEP key
00071          *
00072          * The actual key bytes are stored beginning at offset 3, to
00073          * leave room for easily inserting the IV before a particular
00074          * operation.
00075          */
00076         u8 key[WEP_IV_LEN + WEP_MAX_KEY];
00077 
00078         /** Length of WEP key (not including IV bytes) */
00079         int keylen;
00080 
00081         /** ARC4 context */
00082         struct arc4_ctx arc4;
00083 };
00084 
00085 /**
00086  * Initialize WEP algorithm
00087  *
00088  * @v crypto    802.11 cryptographic algorithm
00089  * @v key       WEP key to use
00090  * @v keylen    Length of WEP key
00091  * @v rsc       Initial receive sequence counter (unused)
00092  * @ret rc      Return status code
00093  *
00094  * Standard key lengths are 5 and 13 bytes; 16-byte keys are
00095  * occasionally supported as an extension to the standard.
00096  */
00097 static int wep_init ( struct net80211_crypto *crypto, const void *key,
00098                       int keylen, const void *rsc __unused )
00099 {
00100         struct wep_ctx *ctx = crypto->priv;
00101 
00102         ctx->keylen = ( keylen > WEP_MAX_KEY ? WEP_MAX_KEY : keylen );
00103         memcpy ( ctx->key + WEP_IV_LEN, key, ctx->keylen );
00104 
00105         return 0;
00106 }
00107 
00108 /**
00109  * Encrypt packet using WEP
00110  *
00111  * @v crypto    802.11 cryptographic algorithm
00112  * @v iob       I/O buffer of plaintext packet
00113  * @ret eiob    Newly allocated I/O buffer for encrypted packet, or NULL
00114  *
00115  * If memory allocation fails, @c NULL is returned.
00116  */
00117 static struct io_buffer * wep_encrypt ( struct net80211_crypto *crypto,
00118                                         struct io_buffer *iob )
00119 {
00120         struct wep_ctx *ctx = crypto->priv;
00121         struct io_buffer *eiob;
00122         struct ieee80211_frame *hdr;
00123         const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
00124         int datalen = iob_len ( iob ) - hdrlen;
00125         int newlen = hdrlen + datalen + WEP_OVERHEAD;
00126         u32 iv, icv;
00127 
00128         eiob = alloc_iob ( newlen );
00129         if ( ! eiob )
00130                 return NULL;
00131 
00132         memcpy ( iob_put ( eiob, hdrlen ), iob->data, hdrlen );
00133         hdr = eiob->data;
00134         hdr->fc |= IEEE80211_FC_PROTECTED;
00135 
00136         /* Calculate IV, put it in the header (with key ID byte = 0), and
00137            set it up at the start of the encryption key. */
00138         iv = random() & 0xffffff; /* IV in bottom 3 bytes, top byte = KID = 0 */
00139         memcpy ( iob_put ( eiob, WEP_HEADER_LEN ), &iv, WEP_HEADER_LEN );
00140         memcpy ( ctx->key, &iv, WEP_IV_LEN );
00141 
00142         /* Encrypt the data using RC4 */
00143         cipher_setkey ( &arc4_algorithm, &ctx->arc4, ctx->key,
00144                         ctx->keylen + WEP_IV_LEN );
00145         cipher_encrypt ( &arc4_algorithm, &ctx->arc4, iob->data + hdrlen,
00146                          iob_put ( eiob, datalen ), datalen );
00147 
00148         /* Add ICV */
00149         icv = ~crc32_le ( ~0, iob->data + hdrlen, datalen );
00150         cipher_encrypt ( &arc4_algorithm, &ctx->arc4, &icv,
00151                          iob_put ( eiob, WEP_ICV_LEN ), WEP_ICV_LEN );
00152 
00153         return eiob;
00154 }
00155 
00156 /**
00157  * Decrypt packet using WEP
00158  *
00159  * @v crypto    802.11 cryptographic algorithm
00160  * @v eiob      I/O buffer of encrypted packet
00161  * @ret iob     Newly allocated I/O buffer for plaintext packet, or NULL
00162  *
00163  * If a consistency check for the decryption fails (usually indicating
00164  * an invalid key), @c NULL is returned.
00165  */
00166 static struct io_buffer * wep_decrypt ( struct net80211_crypto *crypto,
00167                                         struct io_buffer *eiob )
00168 {
00169         struct wep_ctx *ctx = crypto->priv;
00170         struct io_buffer *iob;
00171         struct ieee80211_frame *hdr;
00172         const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
00173         int datalen = iob_len ( eiob ) - hdrlen - WEP_OVERHEAD;
00174         int newlen = hdrlen + datalen;
00175         u32 iv, icv, crc;
00176 
00177         iob = alloc_iob ( newlen );
00178         if ( ! iob )
00179                 return NULL;
00180 
00181         memcpy ( iob_put ( iob, hdrlen ), eiob->data, hdrlen );
00182         hdr = iob->data;
00183         hdr->fc &= ~IEEE80211_FC_PROTECTED;
00184 
00185         /* Strip off IV and use it to initialize cryptosystem */
00186         memcpy ( &iv, eiob->data + hdrlen, 4 );
00187         iv &= 0xffffff;         /* ignore key ID byte */
00188         memcpy ( ctx->key, &iv, WEP_IV_LEN );
00189 
00190         /* Decrypt the data using RC4 */
00191         cipher_setkey ( &arc4_algorithm, &ctx->arc4, ctx->key,
00192                         ctx->keylen + WEP_IV_LEN );
00193         cipher_decrypt ( &arc4_algorithm, &ctx->arc4, eiob->data + hdrlen +
00194                          WEP_HEADER_LEN, iob_put ( iob, datalen ), datalen );
00195 
00196         /* Strip off ICV and verify it */
00197         cipher_decrypt ( &arc4_algorithm, &ctx->arc4, eiob->data + hdrlen +
00198                          WEP_HEADER_LEN + datalen, &icv, WEP_ICV_LEN );
00199         crc = ~crc32_le ( ~0, iob->data + hdrlen, datalen );
00200         if ( crc != icv ) {
00201                 DBGC ( crypto, "WEP %p CRC mismatch: expect %08x, get %08x\n",
00202                        crypto, icv, crc );
00203                 free_iob ( iob );
00204                 return NULL;
00205         }
00206         return iob;
00207 }
00208 
00209 /** WEP cryptosystem for 802.11 */
00210 struct net80211_crypto wep_crypto __net80211_crypto = {
00211         .algorithm = NET80211_CRYPT_WEP,
00212         .init = wep_init,
00213         .encrypt = wep_encrypt,
00214         .decrypt = wep_decrypt,
00215         .priv_len = sizeof ( struct wep_ctx ),
00216 };
00217 
00218 /**
00219  * Initialize trivial 802.11 security handshaker
00220  *
00221  * @v dev       802.11 device
00222  * @v ctx       Security handshaker
00223  *
00224  * This simply fetches a WEP key from netX/key, and if it exists,
00225  * installs WEP cryptography on the 802.11 device. No real handshaking
00226  * is performed.
00227  */
00228 static int trivial_init ( struct net80211_device *dev )
00229 {
00230         u8 key[WEP_MAX_KEY];    /* support up to 128-bit keys */
00231         int len;
00232         int rc;
00233 
00234         if ( dev->associating &&
00235              dev->associating->crypto == NET80211_CRYPT_NONE )
00236                 return 0;       /* no crypto? OK. */
00237 
00238         len = fetch_setting ( netdev_settings ( dev->netdev ),
00239                               &net80211_key_setting, key, WEP_MAX_KEY );
00240 
00241         if ( len <= 0 ) {
00242                 DBGC ( dev, "802.11 %p cannot do WEP without a key\n", dev );
00243                 return -EACCES;
00244         }
00245 
00246         /* Full 128-bit keys are a nonstandard extension, but they're
00247            utterly trivial to support, so we do. */
00248         if ( len != 5 && len != 13 && len != 16 ) {
00249                 DBGC ( dev, "802.11 %p invalid WEP key length %d\n",
00250                        dev, len );
00251                 return -EINVAL;
00252         }
00253 
00254         DBGC ( dev, "802.11 %p installing %d-bit WEP\n", dev, len * 8 );
00255 
00256         rc = sec80211_install ( &dev->crypto, NET80211_CRYPT_WEP, key, len,
00257                                 NULL );
00258         if ( rc < 0 )
00259                 return rc;
00260 
00261         return 0;
00262 }
00263 
00264 /**
00265  * Check for key change on trivial 802.11 security handshaker
00266  *
00267  * @v dev       802.11 device
00268  * @v ctx       Security handshaker
00269  */
00270 static int trivial_change_key ( struct net80211_device *dev )
00271 {
00272         u8 key[WEP_MAX_KEY];
00273         int len;
00274         int change = 0;
00275 
00276         /* If going from WEP to clear, or something else to WEP, reassociate. */
00277         if ( ! dev->crypto || ( dev->crypto->init != wep_init ) )
00278                 change ^= 1;
00279 
00280         len = fetch_setting ( netdev_settings ( dev->netdev ),
00281                               &net80211_key_setting, key, WEP_MAX_KEY );
00282         if ( len <= 0 )
00283                 change ^= 1;
00284 
00285         /* Changing crypto type => return nonzero to reassociate. */
00286         if ( change )
00287                 return -EINVAL;
00288 
00289         /* Going from no crypto to still no crypto => nothing to do. */
00290         if ( len <= 0 )
00291                 return 0;
00292 
00293         /* Otherwise, reinitialise WEP with new key. */
00294         return wep_init ( dev->crypto, key, len, NULL );
00295 }
00296 
00297 /** Trivial 802.11 security handshaker */
00298 struct net80211_handshaker trivial_handshaker __net80211_handshaker = {
00299         .protocol = NET80211_SECPROT_NONE,
00300         .init = trivial_init,
00301         .change_key = trivial_change_key,
00302         .priv_len = 0,
00303 };

Generated on Tue Apr 6 20:01:09 2010 for gPXE by  doxygen 1.5.7.1