Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Michael MIC implementation - optimized for TKIP MIC operations
0004  * Copyright 2002-2003, Instant802 Networks, Inc.
0005  */
0006 #include <linux/types.h>
0007 #include <linux/bitops.h>
0008 #include <linux/ieee80211.h>
0009 #include <asm/unaligned.h>
0010 
0011 #include "michael.h"
0012 
0013 static void michael_block(struct michael_mic_ctx *mctx, u32 val)
0014 {
0015     mctx->l ^= val;
0016     mctx->r ^= rol32(mctx->l, 17);
0017     mctx->l += mctx->r;
0018     mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) |
0019            ((mctx->l & 0x00ff00ff) << 8);
0020     mctx->l += mctx->r;
0021     mctx->r ^= rol32(mctx->l, 3);
0022     mctx->l += mctx->r;
0023     mctx->r ^= ror32(mctx->l, 2);
0024     mctx->l += mctx->r;
0025 }
0026 
0027 static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key,
0028                 struct ieee80211_hdr *hdr)
0029 {
0030     u8 *da, *sa, tid;
0031 
0032     da = ieee80211_get_DA(hdr);
0033     sa = ieee80211_get_SA(hdr);
0034     if (ieee80211_is_data_qos(hdr->frame_control))
0035         tid = ieee80211_get_tid(hdr);
0036     else
0037         tid = 0;
0038 
0039     mctx->l = get_unaligned_le32(key);
0040     mctx->r = get_unaligned_le32(key + 4);
0041 
0042     /*
0043      * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC
0044      * calculation, but it is _not_ transmitted
0045      */
0046     michael_block(mctx, get_unaligned_le32(da));
0047     michael_block(mctx, get_unaligned_le16(&da[4]) |
0048                 (get_unaligned_le16(sa) << 16));
0049     michael_block(mctx, get_unaligned_le32(&sa[2]));
0050     michael_block(mctx, tid);
0051 }
0052 
0053 void michael_mic(const u8 *key, struct ieee80211_hdr *hdr,
0054          const u8 *data, size_t data_len, u8 *mic)
0055 {
0056     u32 val;
0057     size_t block, blocks, left;
0058     struct michael_mic_ctx mctx;
0059 
0060     michael_mic_hdr(&mctx, key, hdr);
0061 
0062     /* Real data */
0063     blocks = data_len / 4;
0064     left = data_len % 4;
0065 
0066     for (block = 0; block < blocks; block++)
0067         michael_block(&mctx, get_unaligned_le32(&data[block * 4]));
0068 
0069     /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make
0070      * total length a multiple of 4. */
0071     val = 0x5a;
0072     while (left > 0) {
0073         val <<= 8;
0074         left--;
0075         val |= data[blocks * 4 + left];
0076     }
0077 
0078     michael_block(&mctx, val);
0079     michael_block(&mctx, 0);
0080 
0081     put_unaligned_le32(mctx.l, mic);
0082     put_unaligned_le32(mctx.r, mic + 4);
0083 }