diff options
Diffstat (limited to 'net/mac80211/michael.c')
-rw-r--r-- | net/mac80211/michael.c | 116 |
1 files changed, 49 insertions, 67 deletions
diff --git a/net/mac80211/michael.c b/net/mac80211/michael.c index 0f844f7895f1..408649bd4702 100644 --- a/net/mac80211/michael.c +++ b/net/mac80211/michael.c | |||
@@ -6,85 +6,68 @@ | |||
6 | * it under the terms of the GNU General Public License version 2 as | 6 | * it under the terms of the GNU General Public License version 2 as |
7 | * published by the Free Software Foundation. | 7 | * published by the Free Software Foundation. |
8 | */ | 8 | */ |
9 | |||
10 | #include <linux/types.h> | 9 | #include <linux/types.h> |
10 | #include <linux/bitops.h> | ||
11 | #include <linux/ieee80211.h> | ||
12 | #include <asm/unaligned.h> | ||
11 | 13 | ||
12 | #include "michael.h" | 14 | #include "michael.h" |
13 | 15 | ||
14 | static inline u32 rotr(u32 val, int bits) | 16 | static void michael_block(struct michael_mic_ctx *mctx, u32 val) |
15 | { | ||
16 | return (val >> bits) | (val << (32 - bits)); | ||
17 | } | ||
18 | |||
19 | |||
20 | static inline u32 rotl(u32 val, int bits) | ||
21 | { | ||
22 | return (val << bits) | (val >> (32 - bits)); | ||
23 | } | ||
24 | |||
25 | |||
26 | static inline u32 xswap(u32 val) | ||
27 | { | ||
28 | return ((val & 0xff00ff00) >> 8) | ((val & 0x00ff00ff) << 8); | ||
29 | } | ||
30 | |||
31 | |||
32 | #define michael_block(l, r) \ | ||
33 | do { \ | ||
34 | r ^= rotl(l, 17); \ | ||
35 | l += r; \ | ||
36 | r ^= xswap(l); \ | ||
37 | l += r; \ | ||
38 | r ^= rotl(l, 3); \ | ||
39 | l += r; \ | ||
40 | r ^= rotr(l, 2); \ | ||
41 | l += r; \ | ||
42 | } while (0) | ||
43 | |||
44 | |||
45 | static inline u32 michael_get32(u8 *data) | ||
46 | { | 17 | { |
47 | return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); | 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; | ||
48 | } | 28 | } |
49 | 29 | ||
50 | 30 | static void michael_mic_hdr(struct michael_mic_ctx *mctx, const u8 *key, | |
51 | static inline void michael_put32(u32 val, u8 *data) | 31 | struct ieee80211_hdr *hdr) |
52 | { | 32 | { |
53 | data[0] = val & 0xff; | 33 | u8 *da, *sa, tid; |
54 | data[1] = (val >> 8) & 0xff; | 34 | |
55 | data[2] = (val >> 16) & 0xff; | 35 | da = ieee80211_get_DA(hdr); |
56 | data[3] = (val >> 24) & 0xff; | 36 | sa = ieee80211_get_SA(hdr); |
37 | if (ieee80211_is_data_qos(hdr->frame_control)) | ||
38 | tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK; | ||
39 | else | ||
40 | tid = 0; | ||
41 | |||
42 | mctx->l = get_unaligned_le32(key); | ||
43 | mctx->r = get_unaligned_le32(key + 4); | ||
44 | |||
45 | /* | ||
46 | * A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC | ||
47 | * calculation, but it is _not_ transmitted | ||
48 | */ | ||
49 | michael_block(mctx, get_unaligned_le32(da)); | ||
50 | michael_block(mctx, get_unaligned_le16(&da[4]) | | ||
51 | (get_unaligned_le16(sa) << 16)); | ||
52 | michael_block(mctx, get_unaligned_le32(&sa[2])); | ||
53 | michael_block(mctx, tid); | ||
57 | } | 54 | } |
58 | 55 | ||
59 | 56 | void michael_mic(const u8 *key, struct ieee80211_hdr *hdr, | |
60 | void michael_mic(u8 *key, u8 *da, u8 *sa, u8 priority, | 57 | const u8 *data, size_t data_len, u8 *mic) |
61 | u8 *data, size_t data_len, u8 *mic) | ||
62 | { | 58 | { |
63 | u32 l, r, val; | 59 | u32 val; |
64 | size_t block, blocks, left; | 60 | size_t block, blocks, left; |
61 | struct michael_mic_ctx mctx; | ||
65 | 62 | ||
66 | l = michael_get32(key); | 63 | michael_mic_hdr(&mctx, key, hdr); |
67 | r = michael_get32(key + 4); | ||
68 | |||
69 | /* A pseudo header (DA, SA, Priority, 0, 0, 0) is used in Michael MIC | ||
70 | * calculation, but it is _not_ transmitted */ | ||
71 | l ^= michael_get32(da); | ||
72 | michael_block(l, r); | ||
73 | l ^= da[4] | (da[5] << 8) | (sa[0] << 16) | (sa[1] << 24); | ||
74 | michael_block(l, r); | ||
75 | l ^= michael_get32(&sa[2]); | ||
76 | michael_block(l, r); | ||
77 | l ^= priority; | ||
78 | michael_block(l, r); | ||
79 | 64 | ||
80 | /* Real data */ | 65 | /* Real data */ |
81 | blocks = data_len / 4; | 66 | blocks = data_len / 4; |
82 | left = data_len % 4; | 67 | left = data_len % 4; |
83 | 68 | ||
84 | for (block = 0; block < blocks; block++) { | 69 | for (block = 0; block < blocks; block++) |
85 | l ^= michael_get32(&data[block * 4]); | 70 | michael_block(&mctx, get_unaligned_le32(&data[block * 4])); |
86 | michael_block(l, r); | ||
87 | } | ||
88 | 71 | ||
89 | /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make | 72 | /* Partial block of 0..3 bytes and padding: 0x5a + 4..7 zeros to make |
90 | * total length a multiple of 4. */ | 73 | * total length a multiple of 4. */ |
@@ -94,11 +77,10 @@ void michael_mic(u8 *key, u8 *da, u8 *sa, u8 priority, | |||
94 | left--; | 77 | left--; |
95 | val |= data[blocks * 4 + left]; | 78 | val |= data[blocks * 4 + left]; |
96 | } | 79 | } |
97 | l ^= val; | ||
98 | michael_block(l, r); | ||
99 | /* last block is zero, so l ^ 0 = l */ | ||
100 | michael_block(l, r); | ||
101 | 80 | ||
102 | michael_put32(l, mic); | 81 | michael_block(&mctx, val); |
103 | michael_put32(r, mic + 4); | 82 | michael_block(&mctx, 0); |
83 | |||
84 | put_unaligned_le32(mctx.l, mic); | ||
85 | put_unaligned_le32(mctx.r, mic + 4); | ||
104 | } | 86 | } |