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) + (A[B+1] << 8) + (A[B+2] << 16) + (A[B+3] << 24)
22 // Convert from UInt32 to Byte[] in a portable way
23 #define putUInt32(A, B, C) \
25 A[B + 0] = (uint8_t)(C & 0xff); \
26 A[B + 1] = (uint8_t)((C >> 8) & 0xff); \
27 A[B + 2] = (uint8_t)((C >> 16) & 0xff); \
28 A[B + 3] = (uint8_t)((C >> 24) & 0xff); \
31 // Reset the state to the empty message.
32 #define MichaelClear(A) \
40 void MichaelInitializeFunction(struct michel_mic_t *Mic, uint8_t *key)
43 Mic->K0 = getUInt32(key, 0);
44 Mic->K1 = getUInt32(key, 4);
50 #define MichaelBlockFunction(L, R) \
52 R ^= ROL32( L, 17 ); \
54 R ^= ((L & 0xff00ff00) >> 8) | ((L & 0x00ff00ff) << 8); \
63 void MichaelAppend(struct michel_mic_t *Mic, uint8_t *src, int nBytes)
67 addlen = 4 - Mic->nBytesInM;
70 memcpy(&Mic->M[Mic->nBytesInM], src, addlen);
71 Mic->nBytesInM += addlen;
75 if (Mic->nBytesInM < 4)
78 Mic->L ^= getUInt32(Mic->M, 0);
79 MichaelBlockFunction(Mic->L, Mic->R);
84 Mic->L ^= getUInt32(src, 0);
85 MichaelBlockFunction(Mic->L, Mic->R);
91 Mic->nBytesInM = nBytes;
92 memcpy(Mic->M, src, nBytes);
97 void MichaelGetMIC(struct michel_mic_t *Mic, uint8_t *dst)
99 uint8_t *data = Mic->M;
100 switch (Mic->nBytesInM) {
105 Mic->L ^= data[0] | 0x5a00;
108 Mic->L ^= data[0] | (data[1] << 8) | 0x5a0000;
111 Mic->L ^= data[0] | (data[1] << 8) | (data[2] << 16) |
115 MichaelBlockFunction(Mic->L, Mic->R);
116 MichaelBlockFunction(Mic->L, Mic->R);
117 // The appendByte function has already computed the result.
118 putUInt32(dst, 0, Mic->L);
119 putUInt32(dst, 4, Mic->R);
121 // Reset to the empty message.
125 void MichaelMICFunction(struct michel_mic_t *Mic, uint8_t *Key,
126 uint8_t *Data, int Len, uint8_t priority,
129 uint8_t pad_data[4] = { priority, 0, 0, 0 };
130 // Compute the MIC value
132 * IEEE802.11i page 47
133 * Figure 43g TKIP MIC processing format
134 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
135 * |6 |6 |1 |3 |M |1 |1 |1 |1 |1 |1 |1 |1 | Octet
136 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
137 * |DA|SA|Priority|0 |Data|M0|M1|M2|M3|M4|M5|M6|M7|
138 * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
140 MichaelInitializeFunction(Mic, Key);
141 MichaelAppend(Mic, (uint8_t *) Data, 12); /* |DA|SA| */
142 MichaelAppend(Mic, pad_data, 4); /* |Priority|0|0|0| */
143 MichaelAppend(Mic, (uint8_t *) (Data + 12), Len - 12); /* |Data| */
144 MichaelGetMIC(Mic, Result);