1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * Michael MIC implementation - optimized for 3 * Michael MIC implementation - optimized for TKIP MIC operations 4 * Copyright 2002-2003, Instant802 Networks, I 4 * Copyright 2002-2003, Instant802 Networks, Inc. 5 */ 5 */ 6 #include <linux/types.h> 6 #include <linux/types.h> 7 #include <linux/bitops.h> 7 #include <linux/bitops.h> 8 #include <linux/ieee80211.h> 8 #include <linux/ieee80211.h> 9 #include <linux/unaligned.h> !! 9 #include <asm/unaligned.h> 10 10 11 #include "michael.h" 11 #include "michael.h" 12 12 13 static void michael_block(struct michael_mic_c 13 static void michael_block(struct michael_mic_ctx *mctx, u32 val) 14 { 14 { 15 mctx->l ^= val; 15 mctx->l ^= val; 16 mctx->r ^= rol32(mctx->l, 17); 16 mctx->r ^= rol32(mctx->l, 17); 17 mctx->l += mctx->r; 17 mctx->l += mctx->r; 18 mctx->r ^= ((mctx->l & 0xff00ff00) >> 18 mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) | 19 ((mctx->l & 0x00ff00ff) << 19 ((mctx->l & 0x00ff00ff) << 8); 20 mctx->l += mctx->r; 20 mctx->l += mctx->r; 21 mctx->r ^= rol32(mctx->l, 3); 21 mctx->r ^= rol32(mctx->l, 3); 22 mctx->l += mctx->r; 22 mctx->l += mctx->r; 23 mctx->r ^= ror32(mctx->l, 2); 23 mctx->r ^= ror32(mctx->l, 2); 24 mctx->l += mctx->r; 24 mctx->l += mctx->r; 25 } 25 } 26 26 27 static void michael_mic_hdr(struct michael_mic 27 static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key, 28 struct ieee80211_h 28 struct ieee80211_hdr *hdr) 29 { 29 { 30 u8 *da, *sa, tid; 30 u8 *da, *sa, tid; 31 31 32 da = ieee80211_get_DA(hdr); 32 da = ieee80211_get_DA(hdr); 33 sa = ieee80211_get_SA(hdr); 33 sa = ieee80211_get_SA(hdr); 34 if (ieee80211_is_data_qos(hdr->frame_c 34 if (ieee80211_is_data_qos(hdr->frame_control)) 35 tid = ieee80211_get_tid(hdr); 35 tid = ieee80211_get_tid(hdr); 36 else 36 else 37 tid = 0; 37 tid = 0; 38 38 39 mctx->l = get_unaligned_le32(key); 39 mctx->l = get_unaligned_le32(key); 40 mctx->r = get_unaligned_le32(key + 4); 40 mctx->r = get_unaligned_le32(key + 4); 41 41 42 /* 42 /* 43 * A pseudo header (DA, SA, Priority, 43 * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC 44 * calculation, but it is _not_ transm 44 * calculation, but it is _not_ transmitted 45 */ 45 */ 46 michael_block(mctx, get_unaligned_le32 46 michael_block(mctx, get_unaligned_le32(da)); 47 michael_block(mctx, get_unaligned_le16 47 michael_block(mctx, get_unaligned_le16(&da[4]) | 48 (get_unaligned_le1 48 (get_unaligned_le16(sa) << 16)); 49 michael_block(mctx, get_unaligned_le32 49 michael_block(mctx, get_unaligned_le32(&sa[2])); 50 michael_block(mctx, tid); 50 michael_block(mctx, tid); 51 } 51 } 52 52 53 void michael_mic(const u8 *key, struct ieee802 53 void michael_mic(const u8 *key, struct ieee80211_hdr *hdr, 54 const u8 *data, size_t data_l 54 const u8 *data, size_t data_len, u8 *mic) 55 { 55 { 56 u32 val; 56 u32 val; 57 size_t block, blocks, left; 57 size_t block, blocks, left; 58 struct michael_mic_ctx mctx; 58 struct michael_mic_ctx mctx; 59 59 60 michael_mic_hdr(&mctx, key, hdr); 60 michael_mic_hdr(&mctx, key, hdr); 61 61 62 /* Real data */ 62 /* Real data */ 63 blocks = data_len / 4; 63 blocks = data_len / 4; 64 left = data_len % 4; 64 left = data_len % 4; 65 65 66 for (block = 0; block < blocks; block+ 66 for (block = 0; block < blocks; block++) 67 michael_block(&mctx, get_unali 67 michael_block(&mctx, get_unaligned_le32(&data[block * 4])); 68 68 69 /* Partial block of 0..3 bytes and pad 69 /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make 70 * total length a multiple of 4. */ 70 * total length a multiple of 4. */ 71 val = 0x5a; 71 val = 0x5a; 72 while (left > 0) { 72 while (left > 0) { 73 val <<= 8; 73 val <<= 8; 74 left--; 74 left--; 75 val |= data[blocks * 4 + left] 75 val |= data[blocks * 4 + left]; 76 } 76 } 77 77 78 michael_block(&mctx, val); 78 michael_block(&mctx, val); 79 michael_block(&mctx, 0); 79 michael_block(&mctx, 0); 80 80 81 put_unaligned_le32(mctx.l, mic); 81 put_unaligned_le32(mctx.l, mic); 82 put_unaligned_le32(mctx.r, mic + 4); 82 put_unaligned_le32(mctx.r, mic + 4); 83 } 83 } 84 84
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.