blob: a57502d9ffec45ea87f14f7d4cdecf6ae002ebe6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jiri Bencf0706e82007-05-05 11:45:53 -07002/*
3 * Michael MIC implementation - optimized for TKIP MIC operations
4 * Copyright 2002-2003, Instant802 Networks, Inc.
Jiri Bencf0706e82007-05-05 11:45:53 -07005 */
Jiri Bencf0706e82007-05-05 11:45:53 -07006#include <linux/types.h>
Harvey Harrison1bd3dff2008-05-14 16:26:16 -07007#include <linux/bitops.h>
Harvey Harrison8e8862b2008-07-02 11:05:35 -07008#include <linux/ieee80211.h>
Harvey Harrison1bd3dff2008-05-14 16:26:16 -07009#include <asm/unaligned.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070010
11#include "michael.h"
12
Harvey Harrison1b19ca32008-05-14 16:26:17 -070013static void michael_block(struct michael_mic_ctx *mctx, u32 val)
14{
15 mctx->l ^= val;
16 mctx->r ^= rol32(mctx->l, 17);
17 mctx->l += mctx->r;
18 mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) |
19 ((mctx->l & 0x00ff00ff) << 8);
20 mctx->l += mctx->r;
21 mctx->r ^= rol32(mctx->l, 3);
22 mctx->l += mctx->r;
23 mctx->r ^= ror32(mctx->l, 2);
24 mctx->l += mctx->r;
25}
26
Harvey Harrison8e8862b2008-07-02 11:05:35 -070027static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key,
28 struct ieee80211_hdr *hdr)
Harvey Harrison1b19ca32008-05-14 16:26:17 -070029{
Harvey Harrison8e8862b2008-07-02 11:05:35 -070030 u8 *da, *sa, tid;
31
32 da = ieee80211_get_DA(hdr);
33 sa = ieee80211_get_SA(hdr);
34 if (ieee80211_is_data_qos(hdr->frame_control))
Sara Sharona1f2ba04c2018-02-19 14:48:40 +020035 tid = ieee80211_get_tid(hdr);
Harvey Harrison8e8862b2008-07-02 11:05:35 -070036 else
37 tid = 0;
38
Harvey Harrison1b19ca32008-05-14 16:26:17 -070039 mctx->l = get_unaligned_le32(key);
40 mctx->r = get_unaligned_le32(key + 4);
41
42 /*
43 * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC
44 * calculation, but it is _not_ transmitted
45 */
46 michael_block(mctx, get_unaligned_le32(da));
47 michael_block(mctx, get_unaligned_le16(&da[4]) |
48 (get_unaligned_le16(sa) << 16));
49 michael_block(mctx, get_unaligned_le32(&sa[2]));
Harvey Harrison8e8862b2008-07-02 11:05:35 -070050 michael_block(mctx, tid);
Harvey Harrison1b19ca32008-05-14 16:26:17 -070051}
Jiri Bencf0706e82007-05-05 11:45:53 -070052
Harvey Harrison8e8862b2008-07-02 11:05:35 -070053void michael_mic(const u8 *key, struct ieee80211_hdr *hdr,
Harvey Harrisona7b6f0c2008-05-14 16:26:18 -070054 const u8 *data, size_t data_len, u8 *mic)
Jiri Bencf0706e82007-05-05 11:45:53 -070055{
Harvey Harrison1b19ca32008-05-14 16:26:17 -070056 u32 val;
Jiri Bencf0706e82007-05-05 11:45:53 -070057 size_t block, blocks, left;
Harvey Harrison1b19ca32008-05-14 16:26:17 -070058 struct michael_mic_ctx mctx;
Jiri Bencf0706e82007-05-05 11:45:53 -070059
Harvey Harrison8e8862b2008-07-02 11:05:35 -070060 michael_mic_hdr(&mctx, key, hdr);
Jiri Bencf0706e82007-05-05 11:45:53 -070061
62 /* Real data */
63 blocks = data_len / 4;
64 left = data_len % 4;
65
Harvey Harrison1b19ca32008-05-14 16:26:17 -070066 for (block = 0; block < blocks; block++)
67 michael_block(&mctx, get_unaligned_le32(&data[block * 4]));
Jiri Bencf0706e82007-05-05 11:45:53 -070068
69 /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make
70 * total length a multiple of 4. */
71 val = 0x5a;
72 while (left > 0) {
73 val <<= 8;
74 left--;
75 val |= data[blocks * 4 + left];
76 }
Jiri Bencf0706e82007-05-05 11:45:53 -070077
Harvey Harrison1b19ca32008-05-14 16:26:17 -070078 michael_block(&mctx, val);
79 michael_block(&mctx, 0);
80
81 put_unaligned_le32(mctx.l, mic);
82 put_unaligned_le32(mctx.r, mic + 4);
Jiri Bencf0706e82007-05-05 11:45:53 -070083}