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