本文主要是介绍TKIP中MIC值的算法及实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
针对WEP中CRC不能有效提供数据完整性保护的问题,TKIP 采用了带密钥的消息完整性校验算法MIC, 目前TKIP的 MIC 算法称为 Michael。
Michael 的认证密钥Kmic是 64 bits,分为左右各 32 bits 用(K0,K1)表示, Michael连接函数将消息M按32 bits分割成M0、M1、M2?Mn, 最后生成64 bits的MIC值, 接收方用共享的K和接收到的消息计算出MIC'与接收到的MIC进行校验, 具体过程如下:
Algorithm 1:Michael message processing
Input: Key (K0, K1) and message M0,?,MN
Output: MIC value (V0, V1)
MICHAEL((K0, K1) , (M0,?,MN))
(L,R)←(K0, K1)
for i=0 to N-1 do
L←L ⊕ Mi
(L, R)←b( L, R )
return (L,R)
Algorithm 2:Michael b function
Input: (L,R)
Output: (L,R)
b(L,R)
R←R⊕(L <<< 17)
L←(L + R) mod 2^^32
R←R⊕XSWAP(L)
L←(L + R) mod 2^^32
R←R⊕(L <<< 3)
L←(L + R) mod 2^^32
R←R⊕(L >>> 2)
L←(L + R) mod 2^^32
return (L,R)
> >>表示32 bits右旋转 (rotation) , <<<表示32 bits左旋转。2^^32表示2的32次方。XSWAP 是一个交换函数, XSWAP (ABCD) =BADC, 这里A、 B、 C、 D表示字节。
具体代码为:
unsigned long rol17(unsigned long w) // rotate left 17
{
register unsigned long t, q;
t = w << 17;
q = (w >> 15);
return(t|q);
}
unsigned long rol3(unsigned long w) // rotate left 3
{
register unsigned long t, q;
t = w << 3;
q = (w >> 29);
return(t|q);
}
unsigned long ror2(unsigned long w) // rotate right 2
{
register unsigned long t, q;
t = (w >> 2);
q = w << 30;
return(t|q);
}
#define MBLOCK(L, R) /
R = R ^ rol17(L); /
L += R; /
R ^= ((L & 0xff00ff00)>>8)|((L & 0x00ff00ff) << 8); /
L += R; /
R ^= rol3(L); /
L += R; /
R ^= ror2(L); /
L += R;
static unsigned long getw(unsigned char *cp)
{
register unsigned long t;
t = 0;
t = *cp++;
t |= (*cp++)<<8;
t |= (*cp++)<<16;
t |= (*cp++)<<24;
return(t);
}
void putw(unsigned long w, unsigned char *cp)
{
*cp++ = (short int)w; // MS compiler forces use of 0xff
*cp++ = (short int)(w>>8);
*cp++ = (short int)(w>>16);
*cp++ = (short int)(w>>24);
return;
}
// Michael integrity function
// pads the buffer (s) with up to 7 bytes
// if h is non-null, it is prepended to the buffer.
// The function appends an additional 8 bytes of Michael
// returns buffer len (payload+Michael)
//
int
Michael(unsigned char *key, unsigned char *s, int dlen)
{
register unsigned long M;
unsigned long L, R;
int len = dlen;
register unsigned char *sp, *cp;
L = getw(key); // L = *LL; R = *RR;
R = getw(key+4);
sp = s;
sp[len++] = 0x5a; // message padding
sp[len++] = 0; // 4 required
sp[len++] = 0;
sp[len++] = 0;
sp[len++] = 0;
while (len&0x3) { // word aligned
sp[len++] = 0;
}
sp = s;
while (len > 0) {
M = getw(sp); // M = *mp++; len -= 4;
sp+=4; len -= 4;
L ^= M; // Michael block function
MBLOCK(L, R);
}
cp = s+dlen;
putw(L, (unsigned char *)cp);
cp = s+dlen+4;
putw(R, (unsigned char *)cp);
return(len+8);
}
这篇关于TKIP中MIC值的算法及实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!