iPXE
wep.c
Go to the documentation of this file.
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 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 FILE_SECBOOT ( FORBIDDEN );
22 
23 #include <ipxe/net80211.h>
24 #include <ipxe/sec80211.h>
25 #include <ipxe/crypto.h>
26 #include <ipxe/arc4.h>
27 #include <ipxe/crc32.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 /** @file
33  *
34  * The WEP wireless encryption method (insecure!)
35  *
36  * The data field in a WEP-encrypted packet contains a 3-byte
37  * initialisation vector, one-byte Key ID field (only the bottom two
38  * bits are ever used), encrypted data, and a 4-byte encrypted CRC of
39  * the plaintext data, called the ICV. To decrypt it, the IV is
40  * prepended to the shared key and the data stream (including ICV) is
41  * run through the ARC4 stream cipher; if the ICV matches a CRC32
42  * calculated on the plaintext, the packet is valid.
43  *
44  * For efficiency and code-size reasons, this file assumes it is
45  * running on a little-endian machine.
46  */
47 
48 /** Length of WEP initialisation vector */
49 #define WEP_IV_LEN 3
50 
51 /** Length of WEP key ID byte */
52 #define WEP_KID_LEN 1
53 
54 /** Length of WEP ICV checksum */
55 #define WEP_ICV_LEN 4
56 
57 /** Maximum length of WEP key */
58 #define WEP_MAX_KEY 16
59 
60 /** Amount of data placed before the encrypted bytes */
61 #define WEP_HEADER_LEN 4
62 
63 /** Amount of data placed after the encrypted bytes */
64 #define WEP_TRAILER_LEN 4
65 
66 /** Total WEP overhead bytes */
67 #define WEP_OVERHEAD 8
68 
69 /** Context for WEP encryption and decryption */
70 struct wep_ctx
71 {
72  /** Encoded WEP key
73  *
74  * The actual key bytes are stored beginning at offset 3, to
75  * leave room for easily inserting the IV before a particular
76  * operation.
77  */
79 
80  /** Length of WEP key (not including IV bytes) */
81  int keylen;
82 
83  /** ARC4 context */
84  struct arc4_ctx arc4;
85 };
86 
87 /**
88  * Initialize WEP algorithm
89  *
90  * @v crypto 802.11 cryptographic algorithm
91  * @v key WEP key to use
92  * @v keylen Length of WEP key
93  * @v rsc Initial receive sequence counter (unused)
94  * @ret rc Return status code
95  *
96  * Standard key lengths are 5 and 13 bytes; 16-byte keys are
97  * occasionally supported as an extension to the standard.
98  */
99 static int wep_init ( struct net80211_crypto *crypto, const void *key,
100  int keylen, const void *rsc __unused )
101 {
102  struct wep_ctx *ctx = crypto->priv;
103 
104  ctx->keylen = ( keylen > WEP_MAX_KEY ? WEP_MAX_KEY : keylen );
105  memcpy ( ctx->key + WEP_IV_LEN, key, ctx->keylen );
106 
107  return 0;
108 }
109 
110 /**
111  * Encrypt packet using WEP
112  *
113  * @v crypto 802.11 cryptographic algorithm
114  * @v iob I/O buffer of plaintext packet
115  * @ret eiob Newly allocated I/O buffer for encrypted packet, or NULL
116  *
117  * If memory allocation fails, @c NULL is returned.
118  */
119 static struct io_buffer * wep_encrypt ( struct net80211_crypto *crypto,
120  struct io_buffer *iob )
121 {
122  struct wep_ctx *ctx = crypto->priv;
123  struct io_buffer *eiob;
124  struct ieee80211_frame *hdr;
125  const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
126  int datalen = iob_len ( iob ) - hdrlen;
127  int newlen = hdrlen + datalen + WEP_OVERHEAD;
128  u32 iv, icv;
129 
130  eiob = alloc_iob ( newlen );
131  if ( ! eiob )
132  return NULL;
133 
134  memcpy ( iob_put ( eiob, hdrlen ), iob->data, hdrlen );
135  hdr = eiob->data;
137 
138  /* Calculate IV, put it in the header (with key ID byte = 0), and
139  set it up at the start of the encryption key. */
140  iv = random() & 0xffffff; /* IV in bottom 3 bytes, top byte = KID = 0 */
141  memcpy ( iob_put ( eiob, WEP_HEADER_LEN ), &iv, WEP_HEADER_LEN );
142  memcpy ( ctx->key, &iv, WEP_IV_LEN );
143 
144  /* Encrypt the data using RC4 */
145  cipher_setkey ( &arc4_algorithm, &ctx->arc4, ctx->key,
146  ctx->keylen + WEP_IV_LEN );
147  cipher_encrypt ( &arc4_algorithm, &ctx->arc4, iob->data + hdrlen,
148  iob_put ( eiob, datalen ), datalen );
149 
150  /* Add ICV */
151  icv = ~crc32_le ( ~0, iob->data + hdrlen, datalen );
152  cipher_encrypt ( &arc4_algorithm, &ctx->arc4, &icv,
153  iob_put ( eiob, WEP_ICV_LEN ), WEP_ICV_LEN );
154 
155  return eiob;
156 }
157 
158 /**
159  * Decrypt packet using WEP
160  *
161  * @v crypto 802.11 cryptographic algorithm
162  * @v eiob I/O buffer of encrypted packet
163  * @ret iob Newly allocated I/O buffer for plaintext packet, or NULL
164  *
165  * If a consistency check for the decryption fails (usually indicating
166  * an invalid key), @c NULL is returned.
167  */
168 static struct io_buffer * wep_decrypt ( struct net80211_crypto *crypto,
169  struct io_buffer *eiob )
170 {
171  struct wep_ctx *ctx = crypto->priv;
172  struct io_buffer *iob;
173  struct ieee80211_frame *hdr;
174  const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
175  int datalen = iob_len ( eiob ) - hdrlen - WEP_OVERHEAD;
176  int newlen = hdrlen + datalen;
177  u32 iv, icv, crc;
178 
179  iob = alloc_iob ( newlen );
180  if ( ! iob )
181  return NULL;
182 
183  memcpy ( iob_put ( iob, hdrlen ), eiob->data, hdrlen );
184  hdr = iob->data;
185  hdr->fc &= ~IEEE80211_FC_PROTECTED;
186 
187  /* Strip off IV and use it to initialize cryptosystem */
188  memcpy ( &iv, eiob->data + hdrlen, 4 );
189  iv &= 0xffffff; /* ignore key ID byte */
190  memcpy ( ctx->key, &iv, WEP_IV_LEN );
191 
192  /* Decrypt the data using RC4 */
193  cipher_setkey ( &arc4_algorithm, &ctx->arc4, ctx->key,
194  ctx->keylen + WEP_IV_LEN );
195  cipher_decrypt ( &arc4_algorithm, &ctx->arc4, eiob->data + hdrlen +
196  WEP_HEADER_LEN, iob_put ( iob, datalen ), datalen );
197 
198  /* Strip off ICV and verify it */
199  cipher_decrypt ( &arc4_algorithm, &ctx->arc4, eiob->data + hdrlen +
201  crc = ~crc32_le ( ~0, iob->data + hdrlen, datalen );
202  if ( crc != icv ) {
203  DBGC ( crypto, "WEP %p CRC mismatch: expect %08x, get %08x\n",
204  crypto, icv, crc );
205  free_iob ( iob );
206  return NULL;
207  }
208  return iob;
209 }
210 
211 /** WEP cryptosystem for 802.11 */
212 struct net80211_crypto wep_crypto __net80211_crypto = {
214  .init = wep_init,
215  .encrypt = wep_encrypt,
216  .decrypt = wep_decrypt,
217  .priv_len = sizeof ( struct wep_ctx ),
218 };
219 
220 /**
221  * Initialize trivial 802.11 security handshaker
222  *
223  * @v dev 802.11 device
224  * @v ctx Security handshaker
225  *
226  * This simply fetches a WEP key from netX/key, and if it exists,
227  * installs WEP cryptography on the 802.11 device. No real handshaking
228  * is performed.
229  */
230 static int trivial_init ( struct net80211_device *dev )
231 {
232  u8 key[WEP_MAX_KEY]; /* support up to 128-bit keys */
233  int len;
234  int rc;
235 
236  if ( dev->associating &&
238  return 0; /* no crypto? OK. */
239 
241  &net80211_key_setting, key, WEP_MAX_KEY );
242 
243  if ( len <= 0 ) {
244  DBGC ( dev, "802.11 %p cannot do WEP without a key\n", dev );
245  return -EACCES;
246  }
247 
248  /* Full 128-bit keys are a nonstandard extension, but they're
249  utterly trivial to support, so we do. */
250  if ( len != 5 && len != 13 && len != 16 ) {
251  DBGC ( dev, "802.11 %p invalid WEP key length %d\n",
252  dev, len );
253  return -EINVAL;
254  }
255 
256  DBGC ( dev, "802.11 %p installing %d-bit WEP\n", dev, len * 8 );
257 
259  NULL );
260  if ( rc < 0 )
261  return rc;
262 
263  return 0;
264 }
265 
266 /**
267  * Check for key change on trivial 802.11 security handshaker
268  *
269  * @v dev 802.11 device
270  * @v ctx Security handshaker
271  */
272 static int trivial_change_key ( struct net80211_device *dev )
273 {
274  u8 key[WEP_MAX_KEY];
275  int len;
276  int change = 0;
277 
278  /* If going from WEP to clear, or something else to WEP, reassociate. */
279  if ( ! dev->crypto || ( dev->crypto->init != wep_init ) )
280  change ^= 1;
281 
283  &net80211_key_setting, key, WEP_MAX_KEY );
284  if ( len <= 0 )
285  change ^= 1;
286 
287  /* Changing crypto type => return nonzero to reassociate. */
288  if ( change )
289  return -EINVAL;
290 
291  /* Going from no crypto to still no crypto => nothing to do. */
292  if ( len <= 0 )
293  return 0;
294 
295  /* Otherwise, reinitialise WEP with new key. */
296  return wep_init ( dev->crypto, key, len, NULL );
297 }
298 
299 /** Trivial 802.11 security handshaker */
300 struct net80211_handshaker trivial_handshaker __net80211_handshaker = {
302  .init = trivial_init,
303  .change_key = trivial_change_key,
304  .priv_len = 0,
305 };
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
enum net80211_crypto_alg crypto
Cryptographic algorithm used on the network.
Definition: net80211.h:1087
#define iob_put(iobuf, len)
Definition: iobuf.h:125
No security handshaking.
Definition: net80211.h:102
Error codes.
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
Definition: arc4.h:10
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:153
#define WEP_MAX_KEY
Maximum length of WEP key.
Definition: wep.c:58
#define DBGC(...)
Definition: compiler.h:505
An 802.11 data or management frame without QoS or WDS header fields.
Definition: ieee80211.h:300
FILE_LICENCE(GPL2_OR_LATER)
#define WEP_IV_LEN
Length of WEP initialisation vector.
Definition: wep.c:49
int fetch_raw_setting(struct settings *settings, const struct setting *setting, void *data, size_t len)
Fetch value of setting.
Definition: settings.c:804
Cryptographic API.
#define EACCES
Permission denied.
Definition: errno.h:299
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
Network protected with WEP (awful RC4-based system)
Definition: net80211.h:145
int(* init)(struct net80211_crypto *crypto, const void *key, int keylen, const void *rsc)
Initialize cryptosystem using a given key.
Definition: net80211.h:707
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:131
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:587
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
Definitions for general secured-network routines.
u32 crc32_le(u32 seed, const void *data, size_t len)
Calculate 32-bit little-endian CRC checksum.
Definition: crc32.c:40
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:251
#define WEP_HEADER_LEN
Amount of data placed before the encrypted bytes.
Definition: wep.c:61
enum net80211_crypto_alg algorithm
The cryptographic algorithm implemented.
Definition: net80211.h:692
void * memcpy(void *dest, const void *src, size_t len) __nonnull
FILE_SECBOOT(FORBIDDEN)
struct net80211_crypto * crypto
802.11 cryptosystem for our current network
Definition: net80211.h:940
u8 key[WEP_IV_LEN+WEP_MAX_KEY]
Encoded WEP key.
Definition: wep.c:78
#define IEEE80211_TYP_FRAME_HEADER_LEN
Frame header length for frames we might work with.
Definition: ieee80211.h:60
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
ring len
Length.
Definition: dwmac.h:231
#define WEP_OVERHEAD
Total WEP overhead bytes.
Definition: wep.c:67
u8 rsc[8]
Receive sequence counter for GTK.
Definition: wpa.h:69
enum net80211_security_proto protocol
The security handshaking protocol implemented.
Definition: net80211.h:567
u16 datalen
Length of the data field in bytes, network byte order.
Definition: wpa.h:84
int sec80211_install(struct net80211_crypto **which, enum net80211_crypto_alg crypt, const void *key, int len, const void *rsc)
Install 802.11 cryptosystem.
Definition: sec80211.c:114
#define IEEE80211_FC_PROTECTED
802.11 Frame Control field: Protected flag
Definition: ieee80211.h:264
Context for WEP encryption and decryption.
Definition: wep.c:70
The iPXE 802.11 MAC layer.
int keylen
Length of WEP key (not including IV bytes)
Definition: wep.c:81
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:160
Structure encapsulating the complete state of an 802.11 device.
Definition: net80211.h:786
No security, an "Open" network.
Definition: net80211.h:131
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:32
static struct io_buffer * wep_decrypt(struct net80211_crypto *crypto, struct io_buffer *eiob)
Decrypt packet using WEP.
Definition: wep.c:168
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:261
struct net80211_wlan * associating
Network with which we are associating.
Definition: net80211.h:866
Interface to an 802.11 security handshaking protocol.
Definition: net80211.h:564
void * priv
Private data for the algorithm to store key and state info.
Definition: net80211.h:766
static int wep_init(struct net80211_crypto *crypto, const void *key, int keylen, const void *rsc __unused)
Initialize WEP algorithm.
Definition: wep.c:99
static int trivial_init(struct net80211_device *dev)
Initialize trivial 802.11 security handshaker.
Definition: wep.c:230
struct cipher_algorithm arc4_algorithm
Definition: arc4.c:118
struct net_device * netdev
The net_device that wraps us.
Definition: net80211.h:789
struct net80211_handshaker trivial_handshaker __net80211_handshaker
Trivial 802.11 security handshaker.
Definition: wep.c:300
#define WEP_ICV_LEN
Length of WEP ICV checksum.
Definition: wep.c:55
void * data
Start of data.
Definition: iobuf.h:53
static struct io_buffer * wep_encrypt(struct net80211_crypto *crypto, struct io_buffer *iob)
Encrypt packet using WEP.
Definition: wep.c:119
struct net80211_crypto wep_crypto __net80211_crypto
WEP cryptosystem for 802.11.
Definition: wep.c:212
static int trivial_change_key(struct net80211_device *dev)
Check for key change on trivial 802.11 security handshaker.
Definition: wep.c:272
Interface to an 802.11 cryptosystem.
Definition: net80211.h:689
struct arc4_ctx arc4
ARC4 context.
Definition: wep.c:84
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
uint8_t u8
Definition: stdint.h:20
union @391 key
Sense key.
Definition: scsi.h:18
uint32_t u32
Definition: stdint.h:24
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:235
A persistent I/O buffer.
Definition: iobuf.h:38