MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / net / Zydas / zdmic.c
blob34034d9a9ca989719d6c6610f42d0d5e53b3044a
1 #include "zd80211.h"
2 //
3 // Michael.cpp Reference implementation for Michael
4 //
5 // Copyright (c) 2001 by MacFergus BV
6 // All rights reserved,
7 //
10 U32 getUInt32(U8 *p)
11 // Convert from U8[] to U32 in a portable way
13 U32 res = 0;
14 int i;
16 for( i=0; i<4; i++ ){
17 res |= (*p++) << (8*i);
20 return res;
24 void putUInt32(U8 *p, U32 val)
25 // Convert from U32 to U8[] in a portable way
27 int i;
29 for(i=0; i<4; i++){
30 *p++ = (U8)(val & 0xff);
31 val >>= 8;
36 /***********************************************************************/
37 /* */
38 /* FUNCTION DESCRIPTION MICclear */
39 /* Initial variable require by MIC computation */
40 /* */
41 /* AUTHOR */
42 /* Liam,Hwu ZyDAS Technology Corporation */
43 /* */
44 /***********************************************************************/
45 void MICclear(MICvar *MIC)
47 // Reset the state to the empty message.
48 MIC->L = MIC->K0;
49 MIC->R = MIC->K1;
50 MIC->nBytesInM = 0;
51 MIC->M = 0;
55 /***********************************************************************/
56 /* */
57 /* FUNCTION DESCRIPTION MICsetKey */
58 /* Set MIC key (Tx or Rx) */
59 /* */
60 /* AUTHOR */
61 /* Liam,Hwu ZyDAS Technology Corporation */
62 /* */
63 /***********************************************************************/
64 void MICsetKey(U8 *key, MICvar *MIC)
66 // Set the key
67 MIC->K0 = getUInt32(key);
68 MIC->K1 = getUInt32(key + 4);
69 if (MIC)
71 #ifdef WPA_DEBUG
72 printk(KERN_ERR "mic->K0= %08x K1=%08x\n", (u32)MIC->K0,(u32)MIC->K1);
73 #endif
75 else
76 printk(KERN_ERR "pMic is NULL\n");
77 // and reset the message
78 MICclear(MIC);
82 /***********************************************************************/
83 /* */
84 /* FUNCTION DESCRIPTION MICappendByte */
85 /* Compute MIC for adding a single byte */
86 /* */
87 /* AUTHOR */
88 /* Liam,Hwu ZyDAS Technology Corporation */
89 /* */
90 /***********************************************************************/
91 void MICappendByte(U8 b, MICvar *MIC)
93 register int nBytesInM = MIC->nBytesInM;
94 register U32 M = MIC->M;
96 // Append the byte to our word-sized buffer
97 M |= b << (8* nBytesInM);
98 nBytesInM ++;
100 // Process the word if it is full.
101 if (nBytesInM > 3){
102 register U32 L = MIC->L;
103 register U32 R = MIC->R;
105 L ^= M;
106 R ^= ROL32(L, 17);
107 L += R;
108 R ^= ((L & 0xff00ff00) >> 8) | ((L & 0x00ff00ff) << 8);
109 L += R;
110 R ^= ROL32(L, 3);
111 L += R;
112 R ^= ROR32(L, 2);
113 L += R;
115 MIC->L = L;
116 MIC->R = R;
118 // Clear the buffer
119 M = 0;
120 nBytesInM = 0;
123 MIC->M = M;
124 MIC->nBytesInM = nBytesInM;
129 void MICappendArr(U8 *pb, MICvar *MIC, U32 Size)
131 int i;
132 U8 *pB = pb;
133 // Append the byte to our word-sized buffer
135 for (i=0; i<Size; i++){
136 MIC->M |= *pB << (8 * MIC->nBytesInM);
137 MIC->nBytesInM++;
139 // Process the word if it is full.
140 if(MIC->nBytesInM >= 4){
141 MIC->L ^= MIC->M;
142 MIC->R ^= ROL32(MIC->L, 17);
143 MIC->L += MIC->R;
144 MIC->R ^= ((MIC->L & 0xff00ff00) >> 8) | ((MIC->L & 0x00ff00ff) << 8);
145 MIC->L += MIC->R;
146 MIC->R ^= ROL32(MIC->L, 3);
147 MIC->L += MIC->R;
148 MIC->R ^= ROR32(MIC->L, 2);
149 MIC->L += MIC->R;
151 // Clear the buffer
152 MIC->M = 0;
153 MIC->nBytesInM = 0;
155 pB++;
160 /***********************************************************************/
161 /* */
162 /* FUNCTION DESCRIPTION MICappendByte */
163 /* Compute MIC for adding a single byte */
164 /* */
165 /* AUTHOR */
166 /* Liam,Hwu ZyDAS Technology Corporation */
167 /* */
168 /***********************************************************************/
169 U8 MicTailPadding[]={0x5a, 0, 0, 0, 0, 0, 0, 0, 0};
171 void MICgetMIC(U8 *dst, MICvar *MIC)
173 U16 loopCheck = 0;
174 // Append the minimum padding
175 //MICappendArr(MicTailPadding, MIC, 5+((4 -((MIC->nBytesInM+5) & 3)) & 3));
177 MICappendByte(0x5a, MIC);
178 MICappendByte(0, MIC);
179 MICappendByte(0, MIC);
180 MICappendByte(0, MIC);
181 MICappendByte(0, MIC);
183 // and then zeroes until the length is a multiple of 4
184 // TKIP or AES , MIC stuff check., not used in WEP
185 while( MIC->nBytesInM != 0 ){
186 if(loopCheck++ > 10000)
188 printk("infinite loop occurs in %s\n", __FUNCTION__);
189 loopCheck = 0;
190 break;
193 MICappendByte(0, MIC);
196 // The appendByte function has already computed the result.
197 putUInt32(dst, MIC->L);
198 putUInt32(dst+4, MIC->R);
200 // Reset to the empty message.
201 MICclear(MIC);