⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ieee80211_crypto_wep.c

📁 Linux下wifi实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/*- * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * Alternatively, this software may be distributed under the terms of the * GNU General Public License ("GPL") version 2 as published by the Free * Software Foundation. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $Id: ieee80211_crypto_wep.c 1443 2006-02-06 20:20:57Z mrenzmann $ *//* * IEEE 802.11 WEP crypto support. */#include <linux/config.h>#include <linux/version.h>#include <linux/module.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/random.h>#include <linux/init.h>#include "if_media.h"#include <net80211/ieee80211_var.h>static void *wep_attach(struct ieee80211vap *, struct ieee80211_key *);static void wep_detach(struct ieee80211_key *);static int wep_setkey(struct ieee80211_key *);static int wep_encap(struct ieee80211_key *, struct sk_buff *, u_int8_t);static int wep_decap(struct ieee80211_key *, struct sk_buff *, int);static int wep_enmic(struct ieee80211_key *, struct sk_buff *, int);static int wep_demic(struct ieee80211_key *, struct sk_buff *, int);static const struct ieee80211_cipher wep = {	.ic_name	= "WEP",	.ic_cipher	= IEEE80211_CIPHER_WEP,	.ic_header	= IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN,	.ic_trailer	= IEEE80211_WEP_CRCLEN,	.ic_miclen	= 0,	.ic_attach	= wep_attach,	.ic_detach	= wep_detach,	.ic_setkey	= wep_setkey,	.ic_encap	= wep_encap,	.ic_decap	= wep_decap,	.ic_enmic	= wep_enmic,	.ic_demic	= wep_demic,};static int wep_encrypt(struct ieee80211_key *, struct sk_buff *, int);static int wep_decrypt(struct ieee80211_key *, struct sk_buff *, int);struct wep_ctx {	struct ieee80211vap *wc_vap;	/* for diagnostics + statistics */	struct ieee80211com *wc_ic;	/* for diagnostics */	u_int32_t wc_iv;			/* initial vector for crypto */};static void *wep_attach(struct ieee80211vap *vap, struct ieee80211_key *k){	struct wep_ctx *ctx;	_MOD_INC_USE(THIS_MODULE, return NULL);	MALLOC(ctx, struct wep_ctx *, sizeof(struct wep_ctx),		M_DEVBUF, M_NOWAIT | M_ZERO);	if (ctx == NULL) {		vap->iv_stats.is_crypto_nomem++;		_MOD_DEC_USE(THIS_MODULE);		return NULL;	}	ctx->wc_vap = vap;	ctx->wc_ic = vap->iv_ic;	get_random_bytes(&ctx->wc_iv, sizeof(ctx->wc_iv));	return ctx;}static voidwep_detach(struct ieee80211_key *k){	struct wep_ctx *ctx = k->wk_private;	FREE(ctx, M_DEVBUF);	_MOD_DEC_USE(THIS_MODULE);}static intwep_setkey(struct ieee80211_key *k){	return k->wk_keylen >= 40 / NBBY;}#ifndef _BYTE_ORDER#error "Don't know native byte order"#endif/* * Add privacy headers appropriate for the specified key. */static intwep_encap(struct ieee80211_key *k, struct sk_buff *skb, u_int8_t keyid){	struct wep_ctx *ctx = k->wk_private;	struct ieee80211com *ic = ctx->wc_ic;	u_int32_t iv;	u_int8_t *ivp;	int hdrlen;	hdrlen = ieee80211_hdrspace(ic, skb->data);	/*	 * Copy down 802.11 header and add the IV + KeyID.	 */	ivp = skb_push(skb, wep.ic_header);	memmove(ivp, ivp + wep.ic_header, hdrlen);	ivp += hdrlen;	/*	 * XXX	 * IV must not duplicate during the lifetime of the key.	 * But no mechanism to renew keys is defined in IEEE 802.11	 * for WEP.  And the IV may be duplicated at other stations	 * because the session key itself is shared.  So we use a	 * pseudo random IV for now, though it is not the right way.	 *	 * NB: Rather than use a strictly random IV we select a	 * random one to start and then increment the value for	 * each frame.  This is an explicit tradeoff between	 * overhead and security.  Given the basic insecurity of	 * WEP this seems worthwhile.	 */	/*	 * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:	 * (B, 255, N) with 3 <= B < 16 and 0 <= N <= 255	 */	iv = ctx->wc_iv;	if ((iv & 0xff00) == 0xff00) {		int B = (iv & 0xff0000) >> 16;		if (3 <= B && B < 16)			iv += 0x0100;	}	ctx->wc_iv = iv + 1;	/*	 * NB: Preserve byte order of IV for packet	 *     sniffers; it doesn't matter otherwise.	 */#if _BYTE_ORDER == _BIG_ENDIAN	ivp[0] = iv >> 0;	ivp[1] = iv >> 8;	ivp[2] = iv >> 16;#else	ivp[2] = iv >> 0;	ivp[1] = iv >> 8;	ivp[0] = iv >> 16;#endif	ivp[3] = keyid;	/*	 * Finally, do software encrypt if neeed.	 */	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&	    !wep_encrypt(k, skb, hdrlen))		return 0;	return 1;}/* * Add MIC to the frame as needed. */static intwep_enmic(struct ieee80211_key *k, struct sk_buff *skb, int force){	return 1;}/* * Validate and strip privacy headers (and trailer) for a * received frame.  If necessary, decrypt the frame using * the specified key. */static intwep_decap(struct ieee80211_key *k, struct sk_buff *skb, int hdrlen){	struct wep_ctx *ctx = k->wk_private;	struct ieee80211vap *vap = ctx->wc_vap;	struct ieee80211_frame *wh;	wh = (struct ieee80211_frame *)skb->data;	/*	 * Check if the device handled the decrypt in hardware.	 * If so we just strip the header; otherwise we need to	 * handle the decrypt in software.	 */	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&	    !wep_decrypt(k, skb, hdrlen)) {		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,			"%s", "WEP ICV mismatch on decrypt");		vap->iv_stats.is_rx_wepfail++;		return 0;	}	/*	 * Copy up 802.11 header and strip crypto bits.	 */	memmove(skb->data + wep.ic_header, skb->data, hdrlen);	skb_pull(skb, wep.ic_header);	skb_trim(skb, skb->len - wep.ic_trailer);	return 1;}/* * Verify and strip MIC from the frame. */static intwep_demic(struct ieee80211_key *k, struct sk_buff *skb, int hdrlen){	return 1;}static const uint32_t crc32_table[256] = {	0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,	0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,	0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,	0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,	0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,	0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,	0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,	0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,	0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -