MSWSP: * add parse_lcid()
[wireshark-wip.git] / wsutil / airpdcap_wep.c
blob2ff4ca12b65efe124d028b5aee80a0822849aafd
1 /* airpcap_wep.c
3 * $Id$
5 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6 * Copyright (c) 2006 CACE Technologies, Davis (California)
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "config.h"
38 /************************************************************************/
39 /* File includes */
41 #include <glib.h>
42 #include "crc32.h"
44 /************************************************************************/
45 /* Note: copied from net80211/ieee80211_airpdcap_tkip.c */
46 #define S_SWAP(a,b) { guint8 t = S[a]; S[a] = S[b]; S[b] = t; }
48 /* Note: copied from FreeBSD source code, RELENG 6, */
49 /* sys/net80211/ieee80211_crypto_wep.c, 391 */
50 int AirPDcapWepDecrypt(
51 const guchar *seed,
52 const size_t seed_len,
53 guchar *cypher_text,
54 const size_t data_len)
56 guint32 i, j, k, crc;
57 guint8 S[256];
58 guint8 icv[4];
59 size_t buflen;
61 /* Generate key stream (RC4 Pseudo-Random Number Generator) */
62 for (i = 0; i < 256; i++)
63 S[i] = (guint8)i;
64 for (j = i = 0; i < 256; i++) {
65 j = (j + S[i] + seed[i % seed_len]) & 0xff;
66 S_SWAP(i, j);
69 /* Apply RC4 to data and compute CRC32 over decrypted data */
70 crc = ~(guint32)0;
71 buflen = data_len;
73 for (i = j = k = 0; k < buflen; k++) {
74 i = (i + 1) & 0xff;
75 j = (j + S[i]) & 0xff;
76 S_SWAP(i, j);
77 *cypher_text ^= S[(S[i] + S[j]) & 0xff];
78 crc = crc32_ccitt_table_lookup((crc ^ *cypher_text) & 0xff) ^ (crc >> 8);
79 cypher_text++;
82 crc = ~crc;
84 /* Encrypt little-endian CRC32 and verify that it matches with the received ICV */
85 icv[0] = (guint8)crc;
86 icv[1] = (guint8)(crc >> 8);
87 icv[2] = (guint8)(crc >> 16);
88 icv[3] = (guint8)(crc >> 24);
89 for (k = 0; k < 4; k++) {
90 i = (i + 1) & 0xff;
91 j = (j + S[i]) & 0xff;
92 S_SWAP(i, j);
93 if ((icv[k] ^ S[(S[i] + S[j]) & 0xff]) != *cypher_text++) {
94 /* ICV mismatch - drop frame */
95 return 1/*AIRPDCAP_RET_UNSUCCESS*/;
99 return 0/*AIRPDCAP_RET_SUCCESS*/;