dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / wsutil / dot11decrypt_wep.c
blob37473750b8a5909d1c17552d0c4bb60ab91ea553
1 /* dot11decrypt_wep.c
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4 * Copyright (c) 2006 CACE Technologies, Davis (California)
5 * All rights reserved.
7 * SPDX-License-Identifier: BSD-3-Clause
8 */
10 #include "config.h"
12 /************************************************************************/
13 /* File includes */
15 #include "crc32.h"
17 /************************************************************************/
18 /* Note: copied from net80211/ieee80211_airpdcap_tkip.c */
19 #define S_SWAP(a,b) { uint8_t t = S[a]; S[a] = S[b]; S[b] = t; }
21 /* Note: copied from FreeBSD source code, RELENG 6, */
22 /* sys/net80211/ieee80211_crypto_wep.c, 391 */
23 int Dot11DecryptWepDecrypt(
24 const unsigned char *seed,
25 const size_t seed_len,
26 unsigned char *cypher_text,
27 const size_t data_len)
29 uint32_t i, j, k, crc;
30 uint8_t S[256];
31 uint8_t icv[4];
32 size_t buflen;
34 /* Generate key stream (RC4 Pseudo-Random Number Generator) */
35 for (i = 0; i < 256; i++)
36 S[i] = (uint8_t)i;
37 for (j = i = 0; i < 256; i++) {
38 j = (j + S[i] + seed[i % seed_len]) & 0xff;
39 S_SWAP(i, j);
42 /* Apply RC4 to data and compute CRC32 over decrypted data */
43 crc = ~(uint32_t)0;
44 buflen = data_len;
46 for (i = j = k = 0; k < buflen; k++) {
47 i = (i + 1) & 0xff;
48 j = (j + S[i]) & 0xff;
49 S_SWAP(i, j);
50 *cypher_text ^= S[(S[i] + S[j]) & 0xff];
51 crc = crc32_ccitt_table_lookup((crc ^ *cypher_text) & 0xff) ^ (crc >> 8);
52 cypher_text++;
55 crc = ~crc;
57 /* Encrypt little-endian CRC32 and verify that it matches with the received ICV */
58 icv[0] = (uint8_t)crc;
59 icv[1] = (uint8_t)(crc >> 8);
60 icv[2] = (uint8_t)(crc >> 16);
61 icv[3] = (uint8_t)(crc >> 24);
62 for (k = 0; k < 4; k++) {
63 i = (i + 1) & 0xff;
64 j = (j + S[i]) & 0xff;
65 S_SWAP(i, j);
66 if ((icv[k] ^ S[(S[i] + S[j]) & 0xff]) != *cypher_text++) {
67 /* ICV mismatch - drop frame */
68 return 1/*DOT11DECRYPT_RET_UNSUCCESS*/;
72 return 0/*DOT11DECRYPT_RET_SUCCESS*/;
76 * Editor modelines - https://www.wireshark.org/tools/modelines.html
78 * Local variables:
79 * c-basic-offset: 8
80 * tab-width: 8
81 * indent-tabs-mode: t
82 * End:
84 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
85 * :indentSize=8:tabSize=8:noTabs=false: