//filename dea.c
/*
*****************************************************************************
*
* ELEMENT NAME: dea.c
* DESCRIPTION: Des functions
*
* 文件名: dea.c
* 文件说明: DES 运算
*
*****************************************************************************
*/
#include "dea.h"
#include "tables.h"
static const byte bits[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
static const byte bits_[8]= { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
static void rotxl( bptr p, wptr c)
{
word c2;
c2 = *p & 0x80 ? true : false;
*p <<= 1;
*p = *c ? *p | 0x01 : *p & 0xfe;
*c = c2;
}
static void rotxr( bptr p, wptr c)
{
word c2;
c2 = *p & 0x01 ? true : false;
*p >;>;= 1;
*p = *c ? *p | 0x80 : *p & 0x7f;
*c = c2;
}
static void shll( bptr p, wptr c)
{
*c = *p & 0x80 ? true : false;
*p <<= 1;
}
static void shlr( bptr p, wptr c)
{
*c = *p & 0x01 ? true : false;
*p >;>;= 1;
}
static void shar( bptr p, wptr c)
{
byte b = *p & 0x80;
*c = *p & 0x01 ? true : false;
*p >;>;= 1;
*p |= b;
}
static void bst( byte imm, bptr p, wptr c )
{
*p = *c ? *p | bits_[ imm % 8 ] : *p & ~bits_[ imm % 8 ];
}
static void bld( byte imm, bptr p, wptr c )
{
*c = *p & bits_[ imm % 8 ] ? true : false;
}
static void shiftLeft( bptr p )
{
word c = false ;
shll( p + 3, &c);
rotxl( p + 2, &c);
rotxl( p + 1, &c);
rotxl( p, &c);
bst( 4, p + 3, &c);
shll( p + 7, &c);
rotxl( p + 6, &c);
rotxl( p + 5, &c);
rotxl( p + 4, &c);
bst( 4, p + 7, &c);
}
static void shiftRight( bptr p )
{
word c = false;
shlr( p , &c);
rotxr( p + 1, &c);
rotxr( p + 2, &c);
rotxr( p + 3, &c);
bld( 3, p + 3, &c);
bst( 7, p, &c);
shlr( p + 4, &c);
rotxr( p + 5, &c);
rotxr( p + 6, &c);
rotxr( p + 7, &c);
bld( 3, p + 7, &c);
bst( 7, p + 4, &c);
}
static byte getBit( word bp, bptr src)
{
return *( src + bp/8 ) & bits[ bp % 8 ];
}
static void putBit( byte b, word bp, bptr des)
{
bptr d = des + bp/8;
*d = b ? *d | bits[ bp % 8 ] : *d & ~bits[ bp % 8 ];
}
static DEA dea =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/*
*---------------------------------------------------------------------------
*
* FUNCTION: ecbEncrypt
*
* DESCRIPTION:
*
* Encrypts inputted 8-byte data block in ECB ( Electronic CodeBook )
* mode. Uses inputted 8-byte key. Overwrites the clean data.
*
* PARAMETERS:
*
* bptr data <=>; Pointer to input/output data area.
* bptr key <= Pointer to inputted key
*
* RETURNS: ( void )
*
*---------------------------------------------------------------------------
*/
void ecbEncrypt( bptr data, bptr key)
{
memcpy( dea.data, data, ;
memcpy( dea.key, key, ;
encrypt( &dea);
memcpy( data, dea.data, ;
}
/*
*---------------------------------------------------------------------------
*
* FUNCTION: cbcEncrypt
*
* DESCRIPTION:
*
* Encrypts inputted 8-byte data block in CBC ( Cipher Block Chaining )
* mode. Uses inputted 8-byte key, and vector.Overwrites the clean data.
*
* PARAMETERS:
*
* bptr data <=>; Pointer to input/output data area.
* bptr key <= Pointer to inputted key
* bptr key <=>; Pointer to encryption vector
*
* RETURNS: ( void )
*
*---------------------------------------------------------------------------
*/
void cbcEncrypt( bptr data, bptr key, bptr vector )
{
int i;
for( i = 0; i < 8; i++ )
{
*( data + i ) ^= *( vector + i);
}
memcpy( dea.data, data, ;
memcpy( dea.key, key, ;
encrypt( &dea);
memcpy( vector, dea.data, ;
memcpy( data, vector, ;
}
/*
*---------------------------------------------------------------------------
*
* FUNCTION: ecbDecrypt
*
* DESCRIPTION:
*
* Decrypts inputted 8-byte data block in ECB ( Electronic CodeBook )
* mode. Uses inputted 8-byte key. Overwrites the cipher data.
*
* PARAMETERS:
*
* bptr data <=>; Pointer to input/output data area.
* bptr key <= Pointer to inputted key
*
* RETURNS: ( void )
*
*---------------------------------------------------------------------------
*/
void ecbDecrypt( bptr data, bptr key)
{
memcpy( dea.data, data, ;
memcpy( dea.key, key, ;
decrypt( &dea);
memcpy( data, dea.data, ;
}
void rh_BlockXor(byte *a, byte *b, word length)
{
while (length--)
*(b++) ^= *(a++);
}
/********************************************************************************
输入参数:
Mac_SourceData :算MAC的数据源指针,从交易响应码开始
SouceData_Len :MAC数据源的长度
mackey :MASTERKEY指针
random :分散密钥的随即数指针
输出参数:
mac :结果
*********************************************************************************/
void Bank_GetMac( bptr Mac_SourceData, word SouceData_Len,bptr mackey ,bptr random ,bptr mac)
{
int bnum, i, j;
byte buf[8];
/*分散密钥*/
ecbEncrypt(random, mackey);
memset(buf, 0, 8);
if(SouceData_Len%8)
{
memset(Mac_SourceData+SouceData_Len, 0x00, 8-SouceData_Len%8);
SouceData_Len=SouceData_Len+8-SouceData_Len%8;
}
bnum = (SouceData_Len)/8;
for(i=0; i < bnum; i ++)
{
j = i * 8;
rh_BlockXor(Mac_SourceData+j, buf, 8);
}
ecbEncrypt(buf,random);
memcpy(mac, buf, 8);
return;
}
/*
*---------------------------------------------------------------------------
*
* FUNCTION: cbcDecrypt
*
* DESCRIPTION:
*
* Decrypts inputted 8-byte data block in CBC ( Cipher Block Chaining )
* mode. Uses inputted 8-byte key and vector.Overwrites the cipher data.
*
* PARAMETERS:
*
* bptr data <=>; Pointer to input/output data area.
* bptr key <= Pointer to inputted key
* bptr key <=>; Pointer to decryption vector
*
* RETURNS: ( void )
*
*---------------------------------------------------------------------------
*/
void cbcDecrypt( bptr data, bptr key, bptr vector )
{
int i;
byte vectorL [ 8 ]; /* Local vector */
memcpy( vectorL, data, 8);
memcpy( dea.data, data, 8);
memcpy( dea.key, key, 8);
decrypt( &dea);
memcpy( data, dea.data, 8);
for( i = 0; i < 8; i++)
{
*( data + i ) ^= *( vector + i);
}
memcpy( vector, vectorL, 8);
}
/*
*---------------------------------------------------------------------------
*
* FUNCTION: encrypt
*
* DESCRIPTION:
*
* Encrypts the inputted data with the inputted key identification.
* Resultant data will be written to DEA's data area.
*
* PARAMETERS:
*
* struct Dea * deaP <=>; DEA data object pointer
*
* RETURNS: ( void )
*
*---------------------------------------------------------------------------
*/
static void encrypt( LPDEA deaP )
{
doDea( deaP, true);
}
/*
*------------------------------------