2 * Driver for KeyStream wireless LAN
4 * Copyright (C) 2005-2008 KeyStream Corp.
5 * Copyright (C) 2009 Renesas Technology Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include "michael_mic.h"
16 // Rotation functions on 32 bit values
17 #define ROL32(A, n) (((A) << (n)) | (((A) >> (32 - (n))) & ((1UL << (n)) - 1)))
18 #define ROR32(A, n) ROL32((A), 32 - (n))
19 // Convert from Byte[] to UInt32 in a portable way
20 #define getUInt32(A, B) ((uint32_t)(A[B + 0] << 0) \
21 + (A[B + 1] << 8) + (A[B + 2] << 16) + (A[B + 3] << 24))
23 // Convert from UInt32 to Byte[] in a portable way
24 #define putUInt32(A, B, C) \
26 A[B + 0] = (uint8_t)(C & 0xff); \
27 A[B + 1] = (uint8_t)((C >> 8) & 0xff); \
28 A[B + 2] = (uint8_t)((C >> 16) & 0xff); \
29 A[B + 3] = (uint8_t)((C >> 24) & 0xff); \
32 // Reset the state to the empty message.
33 #define MichaelClear(A) \
41 void MichaelInitializeFunction(struct michael_mic_t *Mic, uint8_t *key)
44 Mic->K0 = getUInt32(key, 0);
45 Mic->K1 = getUInt32(key, 4);
51 #define MichaelBlockFunction(L, R) \
55 R ^= ((L & 0xff00ff00) >> 8) | ((L & 0x00ff00ff) << 8); \
64 void MichaelAppend(struct michael_mic_t *Mic, uint8_t *src, int nBytes)
69 addlen = 4 - Mic->nBytesInM;
72 memcpy(&Mic->M[Mic->nBytesInM], src, addlen);
73 Mic->nBytesInM += addlen;
77 if (Mic->nBytesInM < 4)
80 Mic->L ^= getUInt32(Mic->M, 0);
81 MichaelBlockFunction(Mic->L, Mic->R);
86 Mic->L ^= getUInt32(src, 0);
87 MichaelBlockFunction(Mic->L, Mic->R);
93 Mic->nBytesInM = nBytes;
94 memcpy(Mic->M, src, nBytes);
99 void MichaelGetMIC(struct michael_mic_t *Mic, uint8_t *dst)
103 switch (Mic->nBytesInM) {
108 Mic->L ^= data[0] | 0x5a00;
111 Mic->L ^= data[0] | (data[1] << 8) | 0x5a0000;
114 Mic->L ^= data[0] | (data[1] << 8) | (data[2] << 16) |
118 MichaelBlockFunction(Mic->L, Mic->R);
119 MichaelBlockFunction(Mic->L, Mic->R);
120 // The appendByte function has already computed the result.
121 putUInt32(dst, 0, Mic->L);
122 putUInt32(dst, 4, Mic->R);
124 // Reset to the empty message.
128 void MichaelMICFunction(struct michael_mic_t *Mic, u8 *Key,
129 u8 *Data, int Len, u8 priority,
132 u8 pad_data[4] = { priority, 0, 0, 0 };
133 // Compute the MIC value
135 * IEEE802.11i page 47
136 * Figure 43g TKIP MIC processing format
137 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
138 * |6 |6 |1 |3 |M |1 |1 |1 |1 |1 |1 |1 |1 | Octet
139 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
140 * |DA|SA|Priority|0 |Data|M0|M1|M2|M3|M4|M5|M6|M7|
141 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
143 MichaelInitializeFunction(Mic, Key);
144 MichaelAppend(Mic, (uint8_t *)Data, 12); /* |DA|SA| */
145 MichaelAppend(Mic, pad_data, 4); /* |Priority|0|0|0| */
146 MichaelAppend(Mic, (uint8_t *)(Data + 12), Len - 12); /* |Data| */
147 MichaelGetMIC(Mic, Result);