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