K2.6 patches and update.
[tomato.git] / release / src-rt / bcmcrypto / wep.c
blobc42c35fdf13b862780b9195c55330c84c06311fa
1 /*
2 * wep.c - WEP functions
4 * Copyright (C) 2010, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
8 * the contents of this file may not be disclosed to third parties, copied
9 * or duplicated in any form, in whole or in part, without the prior
10 * written permission of Broadcom Corporation.
12 * $Id: wep.c,v 1.4 2007-04-29 04:36:19 Exp $
15 #include <typedefs.h>
17 /* include wl driver config file if this file is compiled for driver */
18 #ifdef BCMDRIVER
19 #include <osl.h>
20 #else
21 #if defined(__GNUC__)
22 extern void bcopy(const void *src, void *dst, int len);
23 extern int bcmp(const void *b1, const void *b2, int len);
24 extern void bzero(void *b, int len);
25 #else
26 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
27 #define bcmp(b1, b2, len) memcmp((b1), (b2), (len))
28 #define bzero(b, len) memset((b), 0, (len))
29 #endif /* defined(__GNUC__) */
30 #endif /* BCMDRIVER */
32 #include <bcmutils.h>
33 #include <bcmcrypto/rc4.h>
34 #include <bcmcrypto/wep.h>
35 #include <proto/802.11.h>
37 /* WEP-encrypt a buffer */
38 /* assumes a contiguous buffer, with IV prepended, and with enough space at
39 * the end for the ICV
41 void
42 BCMROMFN(wep_encrypt)(uint buf_len, uint8 *buf, uint sec_len, uint8 *sec_data)
44 uint8 key_data[16];
45 uint32 ICV;
46 rc4_ks_t ks;
47 uint8 *body = buf + DOT11_IV_LEN;
48 uint body_len = buf_len - (DOT11_IV_LEN + DOT11_ICV_LEN);
49 uint8 *picv = body + body_len;
51 bcopy(buf, key_data, 3);
52 bcopy(sec_data, &key_data[3], sec_len);
54 prepare_key(key_data, sec_len + 3, &ks);
56 /* append ICV */
57 ICV = ~hndcrc32(body, body_len, CRC32_INIT_VALUE);
58 picv[0] = ICV & 0xff;
59 picv[1] = (ICV >> 8) & 0xff;
60 picv[2] = (ICV >> 16) & 0xff;
61 picv[3] = (ICV >> 24) & 0xff;
63 rc4(body, body_len + DOT11_ICV_LEN, &ks);
66 /* WEP-decrypt
67 * Assumes a contigous buffer, with IV prepended.
68 * Returns TRUE if ICV check passes, FALSE otherwise
71 bool
72 BCMROMFN(wep_decrypt)(uint buf_len, uint8 *buf, uint sec_len, uint8 *sec_data)
74 uint8 key_data[16];
75 rc4_ks_t ks;
77 bcopy(buf, key_data, 3);
78 bcopy(sec_data, &key_data[3], sec_len);
80 prepare_key(key_data, sec_len + 3, &ks);
82 rc4(buf + DOT11_IV_LEN, buf_len - DOT11_IV_LEN, &ks);
84 return (hndcrc32(buf + DOT11_IV_LEN, buf_len - DOT11_IV_LEN, CRC32_INIT_VALUE) ==
85 CRC32_GOOD_VALUE);