📄 interf.c
字号:
/* *****************************************************************************
* interf.c - define the WPI interface
*
* Copyright (c) 2004, Wellhope Corporation. All rights reserved.
*
* ------------------------------------------------------------------------------
* RELEASE:
*
* AUTHOR:
* ------------------------------------------------------------------------------
* PURPOSE:
* Defines the WPI interface
*
*
*
* ------------------------------------------------------------------------------
* REVISED HISTORY:
*
*
*
***************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "interf.h"
#include "global.h"
#include "ssf43.h"
extern int g_nSetKey_Flag=FALSE; //only for debug
/* *****************************************************************************
* [FUNCTION PROTOTYPE]: int wh_wpi_setKey(LPBYTE pbInitialKey,LPBYTE pbLoopKey)
* [DESCRIPTION]: Creat encrypt/decrypt key or MIC key,check cipher algorithm
* [PARAMETERS]:
* [IN] pbInitialKey 16 bytes init-key buffer address
* [OUT] pbLoopKey: 160 bytes encrypt/MIC Key result buffer address
* [INVOKING FUNCTION]: void en_key_ssf43(bit8 *m_key, bit32 *g_key)
* [RETURNED]:=0 Success
<0 Failed,return error number
***************************************************************************** */
int wh_wpi_setKey(LPBYTE pbInitialKey,LPBYTE pbLoopKey)
{
bit32* p1=NULL;
p1=(bit32*)pbLoopKey;
en_key_ssf43(pbInitialKey,p1);
pbLoopKey=(LPBYTE)p1;
g_nSetKey_Flag=TRUE; // only for debug
return 0;
}
/* *****************************************************************************
* [FUNCTION PROTOTYPE]: int wh_wpi_encrypt(LPBYTE pbMSDUPlain,ULONG ulMSDUPlainLen,
* LPBYTE pbPackageNumber,LPBYTE pbCipherLoopKey,
* LPBYTE pbMSDUCipher,ULONG* pulMSDUCipherLen))
* [DESCRIPTION]: perform WPI encrypt function
* [PARAMETERS]:
* [IN] ppbMSDUPlain MSDU plain text buffer address
* [IN] ulMSDUPlainLen MSDU plain text size(bytes)
* [IN] pbPackageNumber 16 bytes PN buffer address
* [IN] pbCipherLoopKey 160 bytes encrypt key buffer address
* [OUT] pbMSDUCipher MSDU cipher text buffer address
* [OUT] pulMSDUCipherLen MSDU cipher text size(bytes)
* [INVOKING FUNCTION]: void cipher_ssf43(bit32 *g_key, bit8 *in, bit8 *out)
* [RETURNED]:=0 Success
<0 Failed,return error number
***************************************************************************** */
int wh_wpi_encrypt(LPBYTE pbMSDUPlain,ULONG ulMSDUPlainLen,LPBYTE pbPackageNumber,
LPBYTE pbCipherLoopKey,LPBYTE pbMSDUCipher,ULONG* pulMSDUCipherLen)
{
unsigned int i,j;
ULONG inlength,residual;
bit32* enkey=NULL;
//bit8 inbuffer[16];
//bit8 outbuffer[16];
bit8* inbuff=NULL;
bit8* outbuff=NULL;
bit8* intemp=NULL;
bit8* outtemp=NULL;
bit8* tempbuff=NULL;
// assert wh_wpi_setKey() & g_nSetKey_Flag
//ASSERT(g_nSetKey_Flag);
assert(g_nSetKey_Flag);
//if pbMSDUCipher==NULL,return Cipher text size,allocate dynamic memory for pbMSDUCipher
if(pbMSDUCipher==NULL)
{
//return (int)(((ulMSDUPlainLen+16)/16)*16);
*pulMSDUCipherLen=((ulMSDUPlainLen+16)/16)*16;
return 0;
}
inlength=ulMSDUPlainLen/16;
residual=ulMSDUPlainLen%16;
if(residual!=0)
inlength++;
*pulMSDUCipherLen=ulMSDUPlainLen;
//pbMSDUCipher=new BYTE[inlength*16];
//pbMSDUCipher=(LPBYTE)malloc((int)(inlength*16));
//if(pbMSDUCipher==NULL)
// return WPI_ERROR_UNEXPECTED;
// initilzie inbuff,outbuff,intemp,outtemp buffer
inbuff=pbMSDUPlain;
outbuff=pbMSDUCipher;
enkey=(bit32*)pbCipherLoopKey;
//outtemp=new BYTE[16];
outtemp=(bit8*)malloc(16);
if(outtemp==NULL)
return WPI_ERROR_UNEXPECTED;
intemp=pbPackageNumber;
//encrypt the 1st plain text vector
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
for(i=0;i<16;i++)
outbuff[i]=inbuff[i]^outtemp[i];
inbuff+=16;
outbuff+=16;
//encrypt the 2nd~(N-1)th plain text vector
for(j=1;j<inlength-1;j++)
{
for(i=0;i<16;i++)
*intemp++=*outtemp++;
outtemp=outtemp-16;
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
for(i=0;i<16;i++)
outbuff[i]=inbuff[i]^outtemp[i];
inbuff+=16;
outbuff+=16;
}
//encrypt the last plain text vector
if(residual==0)
{
for(i=0;i<16;i++)
*intemp++=*outtemp++;
outtemp=outtemp-16;
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
for(i=0;i<16;i++)
outbuff[i]=inbuff[i]^outtemp[i];
}
else
{
for(i=0;i<16;i++)
*intemp++=*outtemp++;
outtemp=outtemp-16;
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
//for(i=0;i<16;i++)
// outbuff[i]=inbuff[i]^outtemp[i];
for(i=0;i<residual;i++)
outbuff[i]=inbuff[i]^outtemp[i];
}
//delete outtemp;
free(outtemp);
return 0;
}
/* *****************************************************************************
* [FUNCTION PROTOTYPE]: int wh_wpi_decrypt(LPBYTE pbMSDUCipher,ULONG ulMSDUCipherLen,
* LPBYTE pbPackageNumber,LPBYTE pbCipherLoopKey,
* LPBYTE pbMSDUPlain,ULONG* pulMSDUPlainLen)
* [DESCRIPTION]: perform WPI decrypt function
* [PARAMETERS]:
* [IN] ppbMSDUCipher MSDU Cipher text buffer address
* [IN] ulMSDUCipherLen MSDU Cipher text size(bytes)
* [IN] pbPackageNumber 16 bytes PN buffer address
* [IN] pbCipherLoopKey 160 bytes encrypt key buffer address
* [OUT] pbMSDUPlain MSDU plain text buffer address
* [OUT] pulMSDUPlainLen MSDU plain text size(bytes)
* [INVOKING FUNCTION]: void de_key_ssf43(bit8 *m_key, bit32 *g_key)
* void cipher_ssf43(bit32 *g_key, bit8 *in, bit8 *out)
* [RETURNED]:=0 Success
<0 Failed,return error number
***************************************************************************** */
int wh_wpi_decrypt(LPBYTE pbMSDUCipher,ULONG ulMSDUCipherLen,LPBYTE pbPackageNumber,
LPBYTE pbCipherLoopKey,LPBYTE pbMSDUPlain,ULONG* pulMSDUPlainLen)
{
unsigned int i,j;
ULONG inlength;
bit32* enkey=NULL;
bit8* inbuff=NULL;
bit8* outbuff=NULL;
bit8* intemp=NULL;
bit8* outtemp=NULL;
bit8* tempbuff=NULL;
// assert wh_wpi_setKey() & g_nSetKey_Flag
//ASSERT(g_nSetKey_Flag==TRUE);
assert(g_nSetKey_Flag);
//if pbMSDUPlain==NULL,return Plain text size,allocate dynamic memory for pbMSDUPlain
if(pbMSDUPlain==NULL)
{
//return (int)ulMSDUCipherLen;
*pulMSDUPlainLen=ulMSDUCipherLen;
return 0;
}
inlength=ulMSDUCipherLen/16;
*pulMSDUPlainLen=ulMSDUCipherLen;
//pbMSDUPlain=new BYTE[ulMSDUCipherLen];
//pbMSDUPlain=(LPBYTE)malloc((int)(ulMSDUCipherLen));
//if(pbMSDUPlain==NULL)
// return WPI_ERROR_UNEXPECTED;
// initilzie inbuff,outbuff,intemp,outtemp buffer
inbuff=pbMSDUCipher;
outbuff=pbMSDUPlain;
de_key_ssf43((bit32*)pbCipherLoopKey,enkey);
//outtemp=new BYTE[16];
outtemp=(bit8*)malloc(16);
if(outtemp==NULL)
return WPI_ERROR_UNEXPECTED;
intemp=pbPackageNumber;
// decrypt the 1st cipher text vector
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
for(i=0;i<16;i++)
outbuff[i]=inbuff[i]^outtemp[i];
inbuff+=16;
outbuff+=16;
// decrypt the 2nd~N th cipher text vector
for(j=1;j<inlength;j++)
{
for(i=0;i<16;i++)
*intemp++=*outtemp++;
outtemp=outtemp-16;
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
for(i=0;i<16;i++)
outbuff[i]=inbuff[i]^outtemp[i];
inbuff+=16;
outbuff+=16;
}
//delete outtemp;
free(outtemp);
return 0;
}
/* *****************************************************************************
* [FUNCTION PROTOTYPE]: int wh_wpi_getMIC(LPBYTE pbWPIPackage,ULONG ulWPIPackageLen,
* LPBYTE pbPackageNumber,LPBYTE pbMICLoopKey,LPBYTE pbWPIMIC)
* [DESCRIPTION]: get MIC
* [PARAMETERS]:
* [IN] pbWPIPackage WPI packet buffer address
* [IN] ulWPIPackageLen WPI packet size(bytes)
* [IN] pbPackageNumber 16 bytes PN buffer address
* [IN] pbMICLoopKey 160 bytes MIC key buffer address
* [OUT] pbWPIMIC 16 bytes MIC
* [INVOKING FUNCTION]: void cipher_ssf43(bit32 *g_key, bit8 *in, bit8 *out)
* [RETURNED]:=0 Success
<0 Failed,return error number
***************************************************************************** */
int wh_wpi_getMIC(LPBYTE pbWPIPackage,ULONG ulWPIPackageLen,LPBYTE pbPackageNumber,
LPBYTE pbMICLoopKey,LPBYTE pbWPIMIC)
{
unsigned int i,j;
ULONG WPIPlength,residual;
bit32* enkey=NULL;
bit8* inbuff=NULL;
//bit8* outbuff=NULL;
bit8* intemp=NULL;
bit8* outtemp=NULL;
bit8* tempbuff=NULL;
// assert wh_wpi_setKey() & g_nSetKey_Flag
//ASSERT(g_nSetKey_Flag==TRUE);
assert(g_nSetKey_Flag);
WPIPlength=ulWPIPackageLen/16;
residual=ulWPIPackageLen%16;
enkey=(bit32*)pbMICLoopKey;
// in CBC-MAC mode, for IV,apply cipher_ssf43()
inbuff=pbWPIPackage;
//outbuff=pbMSDUCipher;
intemp=pbPackageNumber;
//outtemp=new BYTE[16];
outtemp=(bit8*)malloc(16);
if(outtemp==NULL)
return WPI_ERROR_UNEXPECTED;
tempbuff=outtemp;
//cipher_ssf43(enkey,intemp,outtemp);
cipher_ssf43(enkey,intemp,tempbuff);
//in CBC-MAC mode, for the 1st~N th WPI packet vector,apply cipher-ssf43()
for(j=0;j<WPIPlength;j++)
{
for( i=0;i<16;i++)
intemp[i]=outtemp[i]^inbuff[i];
inbuff+=16;
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
}
//pbWPIMIC=(LPBYTE)outtemp;
pbWPIMIC=outtemp;
//in CBC-MAC mode, for the residual data,apply cipher-ssf43()
if(residual!=0)
{
for(i=0;i<residual;i++)
intemp[i]=outtemp[i]^inbuff[i];
for(i=residual;i<16;i++)
intemp[i]=outtemp[i];
tempbuff=outtemp;
cipher_ssf43(enkey,intemp,tempbuff);
}
//pbWPIMIC=(LPBYTE)outtemp;
pbWPIMIC=outtemp;
//delete outtemp;
free(outtemp);
return 0;
}
/* *****************************************************************************
* [FUNCTION PROTOTYPE]: int wh_wpi_verifyMIC(LPBYTE pbWPIPackage,ULONG ulWPIPackageLen,
* LPBYTE pbPackageNumber,LPBYTE pbMICLoopKey,LPBYTE pbWPIMIC)
* [DESCRIPTION]: verify WPI packet MIC
* [PARAMETERS]:
* [IN] pbWPIPackage WPI packet buffer address
* [IN] ulWPIPackageLen WPI packet size(bytes)
* [IN] pbPackageNumber 16 bytes PN buffer address
* [IN] pbMICLoopKey 160 bytes MIC key buffer address
* [IN] pbWPIMIC 16 bytes MIC
* [INVOKING FUNCTION]: int wh_wpi_getMIC(LPBYTE pbWPIPackage,ULONG ulWPIPackageLen,
* LPBYTE pbPackageNumber,LPBYTE pbMICLoopKey,LPBYTE pbWPIMIC)
* [RETURNED]:=0 Success
<0 Failed,return error number
***************************************************************************** */
int wh_wpi_verifyMIC(LPBYTE pbWPIPackage,ULONG ulWPIPackageLen,LPBYTE pbPackageNumber,
LPBYTE pbMICLoopKey,LPBYTE pbWPIMIC)
{
//int i,j;
bit8 getWPIMIC[16];
//for(i=0;i<16;i++)
// getWPIMIC[i]=pbWPIMIC[i];
// assert wh_wpi_setKey() & g_nSetKey_Flag
//ASSERT(g_nSetKey_Flag==TRUE);
assert(g_nSetKey_Flag);
memcpy(getWPIMIC,pbWPIMIC,16);
wh_wpi_getMIC(pbWPIPackage,ulWPIPackageLen,pbPackageNumber,
pbMICLoopKey,pbWPIMIC);
if(memcmp(getWPIMIC,pbWPIMIC,16)==0)
return 0;
else
return WPI_ERROR_UNEXPECTED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -