1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for KeyStream wireless LAN
5 * Copyright (C) 2005-2008 KeyStream Corp.
6 * Copyright (C) 2009 Renesas Technology Corp.
9 #include <asm/unaligned.h>
10 #include <linux/bitops.h>
11 #include <linux/string.h>
12 #include "michael_mic.h"
15 // Reset the state to the empty message.
16 static inline void michael_clear(struct michael_mic *mic)
23 static void michael_init(struct michael_mic *mic, u8 *key)
26 mic->k0 = get_unaligned_le32(key);
27 mic->k1 = get_unaligned_le32(key + 4);
33 static inline void michael_block(struct michael_mic *mic)
35 mic->r ^= rol32(mic->l, 17);
37 mic->r ^= ((mic->l & 0xff00ff00) >> 8) |
38 ((mic->l & 0x00ff00ff) << 8);
40 mic->r ^= rol32(mic->l, 3);
42 mic->r ^= ror32(mic->l, 2);
46 static void michael_append(struct michael_mic *mic, u8 *src, int bytes)
51 addlen = 4 - mic->m_bytes;
54 memcpy(&mic->m[mic->m_bytes], src, addlen);
55 mic->m_bytes += addlen;
62 mic->l ^= get_unaligned_le32(mic->m);
68 mic->l ^= get_unaligned_le32(src);
76 memcpy(mic->m, src, bytes);
80 static void michael_get_mic(struct michael_mic *mic, u8 *dst)
84 switch (mic->m_bytes) {
89 mic->l ^= data[0] | 0x5a00;
92 mic->l ^= data[0] | (data[1] << 8) | 0x5a0000;
95 mic->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
101 // The appendByte function has already computed the result.
102 put_unaligned_le32(mic->l, dst);
103 put_unaligned_le32(mic->r, dst + 4);
105 // Reset to the empty message.
109 void michael_mic_function(struct michael_mic *mic, u8 *key,
110 u8 *data, unsigned int len, u8 priority, u8 *result)
112 u8 pad_data[4] = { priority, 0, 0, 0 };
113 // Compute the MIC value
115 * IEEE802.11i page 47
116 * Figure 43g TKIP MIC processing format
117 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
118 * |6 |6 |1 |3 |M |1 |1 |1 |1 |1 |1 |1 |1 | Octet
119 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
120 * |DA|SA|Priority|0 |Data|M0|M1|M2|M3|M4|M5|M6|M7|
121 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
123 michael_init(mic, key);
124 michael_append(mic, data, 12); /* |DA|SA| */
125 michael_append(mic, pad_data, 4); /* |Priority|0|0|0| */
126 michael_append(mic, data + 12, len - 12); /* |Data| */
127 michael_get_mic(mic, result);