5 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6 * Copyright (c) 2006 CACE Technologies, Davis (California)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
38 /************************************************************************/
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(
52 const size_t seed_len
,
54 const size_t data_len
)
61 /* Generate key stream (RC4 Pseudo-Random Number Generator) */
62 for (i
= 0; i
< 256; i
++)
64 for (j
= i
= 0; i
< 256; i
++) {
65 j
= (j
+ S
[i
] + seed
[i
% seed_len
]) & 0xff;
69 /* Apply RC4 to data and compute CRC32 over decrypted data */
73 for (i
= j
= k
= 0; k
< buflen
; k
++) {
75 j
= (j
+ S
[i
]) & 0xff;
77 *cypher_text
^= S
[(S
[i
] + S
[j
]) & 0xff];
78 crc
= crc32_ccitt_table_lookup((crc
^ *cypher_text
) & 0xff) ^ (crc
>> 8);
84 /* Encrypt little-endian CRC32 and verify that it matches with the received ICV */
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
++) {
91 j
= (j
+ S
[i
]) & 0xff;
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*/;