- 论坛徽章:
- 0
|
谁能给我DES算法的详细说明及C源程序?
//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);
}
/*
*---------------------------------------------------------------------------
*
* FUNCTION: decrypt
*
* DESCRIPTION:
*
* Decrypts the inputted data with the inputted key identification.
* Resultant clear data will be written to DEA's data area.
*
* PARAMETERS:
*
* struct Dea * deaP <=>; DEA data object pointer
*
* RETURNS: ( void )
*
*---------------------------------------------------------------------------
*/
static void decrypt( LPDEA deaP )
{
doDea( deaP, false);
}
static void doDea( LPDEA deaP , Boolean encrF )
{
int iter; /* Iteration count */
int shifts; /* Local holding number of shifts */
permute( ipT, deaP->;data, deaP->;data, 64 );
permute( pc1T, deaP->;key, deaP->;perData, 28);
permute( pc1T + 28, deaP->;key, deaP->;perData + 4, 28);
if( encrF == false )
{
shiftLeft( deaP->;perData);
}
for( iter = 0; iter < 16; iter++ )
{
permute( expT, deaP->;data + 4, deaP->;temp, 48);
shifts = shiftsT[ iter ];
while( shifts--)
{
if( encrF)
{
shiftLeft( deaP->;perData);
}
else
{
shiftRight( deaP->;perData);
}
}
permute( pc2T, deaP->;perData, deaP->;key, 48 );
rXORk( deaP);
bitSelect( deaP);
dataSave( deaP);
}
swapLR( deaP);
permute( invipT, deaP->;data, deaP->;data, 64);
}
static void permute( bptr table, bptr src, bptr des, word dBits)
{
byte t[8];
word i;
for( i = 0; i < dBits; i++)
{
putBit( getBit( *( table + i) - 1, src), i, t );
}
memcpy( des, t, 8);
}
static void rXORk( LPDEA deaP)
{
int i;
for( i = 0; i < 6; i++)
{
deaP->;key ^= deaP->;temp;
}
}
static void dataSave( LPDEA deaP )
{
byte t[4]; /* Temporary 32 bit array */
int i; /* Counter */
for( i = 0; i < 4; i++ )
{
t = deaP->;data ^ deaP->;key;
}
for( i = 0; i < 4; i++ )
{
deaP->;data = deaP->;data[ i + 4 ];
deaP->;data[ i + 4 ] = t;
}
}
static void swapLR( LPDEA deaP)
{
byte t[4];
int i;
memcpy( t, deaP->;data, 4);
for( i = 0; i < 4; i++ )
{
deaP->;data = deaP->;data[ i + 4 ];
deaP->;data[ i + 4 ] = t;
}
}
static void bitSelect( LPDEA deaP )
{
Boolean hiByte;
byte value;
int cnt, i;
byte s[3];
byte t[8];
memcpy( s, deaP->;key + 3, 3);
bitExp( s, t+4 );
memcpy( s, deaP->;key, 3);
bitExp( s, t );
memcpy( deaP->;key, t, 8);
i = 0;
hiByte = true;
for( cnt = 0; cnt < 8; cnt++ )
{
value = readS( cnt, deaP->;key[ cnt ] );
if( hiByte )
{
t = value << 4;
}
else
{
t[ i++ ] |= ( value & 0x0f );
}
hiByte = hiByte ? false : true;
}
memcpy( deaP->;key, t, 4);
permute( pT, deaP->;key, deaP->;key, 32);
}
static byte readS( int ix, byte p)
{
return *( selectionT + ( ix << 6 ) + (( p & 0x03 ) << 4 ) + ( p >;>; 2 & 0x0f ) );
}
static void bitExp( bptr s, bptr d)
{
word c;
*( d+3) = *( s+2);
shll( d+3, &c);
bld( 1, d+3, &c);
bst( 0, d+3, &c);
bld( 6, d+3, &c);
bst( 1, d+3, &c);
*( d+3) &= 0x3f;
shll( s+2, &c);
rotxl( s+1, &c);
bst( 4, d+1, &c);
shll( s+1, &c);
bst( 3, d+1, &c);
shll( s+1, &c);
bst( 2, d+1, &c);
bld( 7, s+2, &c);
bst( 0, s+1, &c);
bld( 6, s+1, &c);
bst( 1, s+1, &c);
bld( 7, s+1, &c);
bst( 0, d+1, &c);
*( s+1) &= 0x3f;
*( d+2) = *( s+1);
shar( s, &c);
bst( 5, d+1, &c);
bld( 0, s, &c);
bst( 1, d+1, &c);
bld( 1, s, &c);
bst( 0, s, &c);
bld( 6, s, &c);
bst( 1, s, &c);
*s &= 0x3f;
*( d+1) &= 0x3f;
*d = *s;
}
#if 0
void Sign( byte* key,
byte* data,
word len,
byte* random,
byte* sign)
{
byte *dataPtr;
int i;
dataPtr = data ;
memcpy(sign , random, 8);
while( 1 )
{
if( dataPtr - data < len )
{
for(i=0; i<8; i++)
{
sign = sign^dataPtr ;
}
ecbEncrypt(sign, key) ;
dataPtr += 8 ;
}
else
{
return ;
}
}
}
#else
void Sign( byte* key,
byte* data,
word len,
byte* sign)
{
byte *dataPtr;
int i;
dataPtr = data ;
memcpy(sign , data, 8);
while( 1 )
{
ecbEncrypt(sign, key) ;
dataPtr += 8 ;
if( dataPtr - data < len )
{
for(i=0; i<8; i++)
{
sign = sign^dataPtr ;
}
}
else
{
return ;
}
}
}
#endif
//filename dea.h
#ifndef __DEA_H
#define __DEA_H
#include <string.h>;
#include "standard.h"
typedef struct Dea
{
byte data[8]; /* Data area to be encrypted/decrypted */
byte key[8]; /* Key Identification */
byte perData[8]; /* Permutation Area */
byte temp[8]; /* Temporary storage area */
} DEA, *LPDEA;
extern void encrypt( LPDEA deaP );
extern void decrypt( LPDEA deaP );
extern void doDea( LPDEA deaP, Boolean encr_flg );
extern void permute( bptr table, bptr src, bptr des, word dBits);
extern void rXORk( LPDEA deaP);
extern void dataSave( LPDEA deaP );
extern void swapLR( LPDEA deaP);
extern void bitSelect( LPDEA dP );
extern void bitExp( bptr s, bptr d);
extern byte readS( int ix, byte p);
extern void parse( cptr s, bptr d );
extern void xorF( void);
/*
Bit Manipulation Function Prototypes
*/
extern byte getBit( word bp, bptr src);
extern void putBit( byte bp, word b, bptr des);
extern void shiftLeft( bptr p );
extern void shiftRight( bptr p );
extern void rotxl( bptr p, wptr c);
extern void rotxr( bptr p, wptr c);
extern void shll( bptr p, wptr c);
extern void shlr( bptr p, wptr c);
extern void shar( bptr p, wptr c);
extern void bst( byte imm, bptr p, wptr c );
extern void bld( byte imm, bptr p, wptr c );
#endif /* __DEA_H */
//filename tables.h
#ifndef __TABLES_H
#define __TABLES_H
#include "standard.h"
static byte ipT[ 64 ] = {
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
/*--------------------------------------------------------------------------
Inverse of Initial Permutation Table
---------------------------------------------------------------------------*/
static byte invipT[ 64 ] = {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
/*--------------------------------------------------------------------------
Permuted Choice 1 Table
---------------------------------------------------------------------------*/
static byte pc1T[ 56 ] = {
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
/*--------------------------------------------------------------------------
Permuted Choice 2 Table
Important Note:
This table is different than the one in AS 2805.5-1985 ( see pg 8 )
such that the values greater than 28 in the original one should be
incremented by 4. This is because the last 4 bits of the 32 bit permuted
data are not used in Permuted Choice 2 permutation and therefore should
be skipped in the permute() routine.
---------------------------------------------------------------------------*/
static byte pc2T[ 48 ] = {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
45, 56, 35, 41, 51, 59,
34, 44, 55, 49, 37, 52,
48, 53, 43, 60, 38, 57,
50, 46, 54, 40, 33, 36
};
/*--------------------------------------------------------------------------
E-bit Selection Table
---------------------------------------------------------------------------*/
static byte expT[ 48] = {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};
/*--------------------------------------------------------------------------
P Function Table
---------------------------------------------------------------------------*/
static byte pT[ 32 ] = {
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};
/*--------------------------------------------------------------------------
Shift Schedule Table
---------------------------------------------------------------------------*/
static byte shiftsT[ 16 ] = {
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};
/*--------------------------------------------------------------------------
Selection Functions Table
---------------------------------------------------------------------------*/
static byte selectionT[ 512 ] =
{
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};
/* End Tables ------------------------------------------------------------*/
#endif /* __TABLES_H */ |
|