| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Michael MIC implementation - optimized for TKIP MIC operations | 
 | 3 |  * Copyright 2002-2003, Instant802 Networks, Inc. | 
 | 4 |  * | 
 | 5 |  * This program is free software; you can redistribute it and/or modify | 
 | 6 |  * it under the terms of the GNU General Public License version 2 as | 
 | 7 |  * published by the Free Software Foundation. | 
 | 8 |  */ | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 9 | #include <linux/types.h> | 
| Harvey Harrison | 1bd3dff | 2008-05-14 16:26:16 -0700 | [diff] [blame] | 10 | #include <linux/bitops.h> | 
| Harvey Harrison | 8e8862b | 2008-07-02 11:05:35 -0700 | [diff] [blame] | 11 | #include <linux/ieee80211.h> | 
| Harvey Harrison | 1bd3dff | 2008-05-14 16:26:16 -0700 | [diff] [blame] | 12 | #include <asm/unaligned.h> | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 13 |  | 
 | 14 | #include "michael.h" | 
 | 15 |  | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 16 | static void michael_block(struct michael_mic_ctx *mctx, u32 val) | 
 | 17 | { | 
 | 18 | 	mctx->l ^= val; | 
 | 19 | 	mctx->r ^= rol32(mctx->l, 17); | 
 | 20 | 	mctx->l += mctx->r; | 
 | 21 | 	mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) | | 
 | 22 | 		   ((mctx->l & 0x00ff00ff) << 8); | 
 | 23 | 	mctx->l += mctx->r; | 
 | 24 | 	mctx->r ^= rol32(mctx->l, 3); | 
 | 25 | 	mctx->l += mctx->r; | 
 | 26 | 	mctx->r ^= ror32(mctx->l, 2); | 
 | 27 | 	mctx->l += mctx->r; | 
 | 28 | } | 
 | 29 |  | 
| Harvey Harrison | 8e8862b | 2008-07-02 11:05:35 -0700 | [diff] [blame] | 30 | static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key, | 
 | 31 | 			    struct ieee80211_hdr *hdr) | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 32 | { | 
| Harvey Harrison | 8e8862b | 2008-07-02 11:05:35 -0700 | [diff] [blame] | 33 | 	u8 *da, *sa, tid; | 
 | 34 |  | 
 | 35 | 	da = ieee80211_get_DA(hdr); | 
 | 36 | 	sa = ieee80211_get_SA(hdr); | 
 | 37 | 	if (ieee80211_is_data_qos(hdr->frame_control)) | 
 | 38 | 		tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK; | 
 | 39 | 	else | 
 | 40 | 		tid = 0; | 
 | 41 |  | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 42 | 	mctx->l = get_unaligned_le32(key); | 
 | 43 | 	mctx->r = get_unaligned_le32(key + 4); | 
 | 44 |  | 
 | 45 | 	/* | 
 | 46 | 	 * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC | 
 | 47 | 	 * calculation, but it is _not_ transmitted | 
 | 48 | 	 */ | 
 | 49 | 	michael_block(mctx, get_unaligned_le32(da)); | 
 | 50 | 	michael_block(mctx, get_unaligned_le16(&da[4]) | | 
 | 51 | 			    (get_unaligned_le16(sa) << 16)); | 
 | 52 | 	michael_block(mctx, get_unaligned_le32(&sa[2])); | 
| Harvey Harrison | 8e8862b | 2008-07-02 11:05:35 -0700 | [diff] [blame] | 53 | 	michael_block(mctx, tid); | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 54 | } | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 55 |  | 
| Harvey Harrison | 8e8862b | 2008-07-02 11:05:35 -0700 | [diff] [blame] | 56 | void michael_mic(const u8 *key, struct ieee80211_hdr *hdr, | 
| Harvey Harrison | a7b6f0c | 2008-05-14 16:26:18 -0700 | [diff] [blame] | 57 | 		 const u8 *data, size_t data_len, u8 *mic) | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 58 | { | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 59 | 	u32 val; | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 60 | 	size_t block, blocks, left; | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 61 | 	struct michael_mic_ctx mctx; | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 62 |  | 
| Harvey Harrison | 8e8862b | 2008-07-02 11:05:35 -0700 | [diff] [blame] | 63 | 	michael_mic_hdr(&mctx, key, hdr); | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 64 |  | 
 | 65 | 	/* Real data */ | 
 | 66 | 	blocks = data_len / 4; | 
 | 67 | 	left = data_len % 4; | 
 | 68 |  | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 69 | 	for (block = 0; block < blocks; block++) | 
 | 70 | 		michael_block(&mctx, get_unaligned_le32(&data[block * 4])); | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 71 |  | 
 | 72 | 	/* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make | 
 | 73 | 	 * total length a multiple of 4. */ | 
 | 74 | 	val = 0x5a; | 
 | 75 | 	while (left > 0) { | 
 | 76 | 		val <<= 8; | 
 | 77 | 		left--; | 
 | 78 | 		val |= data[blocks * 4 + left]; | 
 | 79 | 	} | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 80 |  | 
| Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 81 | 	michael_block(&mctx, val); | 
 | 82 | 	michael_block(&mctx, 0); | 
 | 83 |  | 
 | 84 | 	put_unaligned_le32(mctx.l, mic); | 
 | 85 | 	put_unaligned_le32(mctx.r, mic + 4); | 
| Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 86 | } |