Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 9 | #include <linux/types.h> |
Harvey Harrison | 1bd3dff | 2008-05-14 16:26:16 -0700 | [diff] [blame] | 10 | #include <linux/bitops.h> |
| 11 | #include <asm/unaligned.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 12 | |
| 13 | #include "michael.h" |
| 14 | |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 15 | static void michael_block(struct michael_mic_ctx *mctx, u32 val) |
| 16 | { |
| 17 | mctx->l ^= val; |
| 18 | mctx->r ^= rol32(mctx->l, 17); |
| 19 | mctx->l += mctx->r; |
| 20 | mctx->r ^= ((mctx->l & 0xff00ff00) >> 8) | |
| 21 | ((mctx->l & 0x00ff00ff) << 8); |
| 22 | mctx->l += mctx->r; |
| 23 | mctx->r ^= rol32(mctx->l, 3); |
| 24 | mctx->l += mctx->r; |
| 25 | mctx->r ^= ror32(mctx->l, 2); |
| 26 | mctx->l += mctx->r; |
| 27 | } |
| 28 | |
| 29 | static void michael_mic_hdr(struct michael_mic_ctx *mctx, |
Harvey Harrison | a7b6f0c | 2008-05-14 16:26:18 -0700 | [diff] [blame] | 30 | const u8 *key, const u8 *da, const u8 *sa, u8 priority) |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 31 | { |
| 32 | mctx->l = get_unaligned_le32(key); |
| 33 | mctx->r = get_unaligned_le32(key + 4); |
| 34 | |
| 35 | /* |
| 36 | * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC |
| 37 | * calculation, but it is _not_ transmitted |
| 38 | */ |
| 39 | michael_block(mctx, get_unaligned_le32(da)); |
| 40 | michael_block(mctx, get_unaligned_le16(&da[4]) | |
| 41 | (get_unaligned_le16(sa) << 16)); |
| 42 | michael_block(mctx, get_unaligned_le32(&sa[2])); |
| 43 | michael_block(mctx, priority); |
| 44 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 45 | |
Harvey Harrison | a7b6f0c | 2008-05-14 16:26:18 -0700 | [diff] [blame] | 46 | void michael_mic(const u8 *key, const u8 *da, const u8 *sa, u8 priority, |
| 47 | const u8 *data, size_t data_len, u8 *mic) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 48 | { |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 49 | u32 val; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 50 | size_t block, blocks, left; |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 51 | struct michael_mic_ctx mctx; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 52 | |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 53 | michael_mic_hdr(&mctx, key, da, sa, priority); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 54 | |
| 55 | /* Real data */ |
| 56 | blocks = data_len / 4; |
| 57 | left = data_len % 4; |
| 58 | |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 59 | for (block = 0; block < blocks; block++) |
| 60 | michael_block(&mctx, get_unaligned_le32(&data[block * 4])); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 61 | |
| 62 | /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make |
| 63 | * total length a multiple of 4. */ |
| 64 | val = 0x5a; |
| 65 | while (left > 0) { |
| 66 | val <<= 8; |
| 67 | left--; |
| 68 | val |= data[blocks * 4 + left]; |
| 69 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 70 | |
Harvey Harrison | 1b19ca3 | 2008-05-14 16:26:17 -0700 | [diff] [blame] | 71 | michael_block(&mctx, val); |
| 72 | michael_block(&mctx, 0); |
| 73 | |
| 74 | put_unaligned_le32(mctx.l, mic); |
| 75 | put_unaligned_le32(mctx.r, mic + 4); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 76 | } |