blob: 4e151e0caeb76bfb9d149a94ab863c4a753f380a [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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 */
9
10#include <linux/types.h>
Harvey Harrison1bd3dff2008-05-14 16:26:16 -070011#include <linux/bitops.h>
12#include <asm/unaligned.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070013
14#include "michael.h"
15
Harvey Harrison1b19ca32008-05-14 16:26:17 -070016static 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
30static void michael_mic_hdr(struct michael_mic_ctx *mctx,
31 u8 *key, u8 *da, u8 *sa, u8 priority)
32{
33 mctx->l = get_unaligned_le32(key);
34 mctx->r = get_unaligned_le32(key + 4);
35
36 /*
37 * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC
38 * calculation, but it is _not_ transmitted
39 */
40 michael_block(mctx, get_unaligned_le32(da));
41 michael_block(mctx, get_unaligned_le16(&da[4]) |
42 (get_unaligned_le16(sa) << 16));
43 michael_block(mctx, get_unaligned_le32(&sa[2]));
44 michael_block(mctx, priority);
45}
Jiri Bencf0706e82007-05-05 11:45:53 -070046
Jiri Bencf0706e82007-05-05 11:45:53 -070047void michael_mic(u8 *key, u8 *da, u8 *sa, u8 priority,
48 u8 *data, size_t data_len, u8 *mic)
49{
Harvey Harrison1b19ca32008-05-14 16:26:17 -070050 u32 val;
Jiri Bencf0706e82007-05-05 11:45:53 -070051 size_t block, blocks, left;
Harvey Harrison1b19ca32008-05-14 16:26:17 -070052 struct michael_mic_ctx mctx;
Jiri Bencf0706e82007-05-05 11:45:53 -070053
Harvey Harrison1b19ca32008-05-14 16:26:17 -070054 michael_mic_hdr(&mctx, key, da, sa, priority);
Jiri Bencf0706e82007-05-05 11:45:53 -070055
56 /* Real data */
57 blocks = data_len / 4;
58 left = data_len % 4;
59
Harvey Harrison1b19ca32008-05-14 16:26:17 -070060 for (block = 0; block < blocks; block++)
61 michael_block(&mctx, get_unaligned_le32(&data[block * 4]));
Jiri Bencf0706e82007-05-05 11:45:53 -070062
63 /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make
64 * total length a multiple of 4. */
65 val = 0x5a;
66 while (left > 0) {
67 val <<= 8;
68 left--;
69 val |= data[blocks * 4 + left];
70 }
Jiri Bencf0706e82007-05-05 11:45:53 -070071
Harvey Harrison1b19ca32008-05-14 16:26:17 -070072 michael_block(&mctx, val);
73 michael_block(&mctx, 0);
74
75 put_unaligned_le32(mctx.l, mic);
76 put_unaligned_le32(mctx.r, mic + 4);
Jiri Bencf0706e82007-05-05 11:45:53 -070077}