- UID
- 116743
- 性别
- 男
|
#include "SHAVM.h"
// Message buffer
xbyte SHAVM_Message[64];
// Hash buffer, in wrong order for Dallas MAC
xlong SHAVM_Hash[5];
// MAC buffer, in right order for Dallas MAC
xbyte SHAVM_MAC[20];
// Temp vars for SHA calculation
static xlong SHAVM_MTword[80];
static dlong SHAVM_Temp;
static dbyte SHAVM_cnt;
//constants used in SHA computation
static code long SHAVM_KTN[4] = { (long)0x5a827999, (long)0x6ed9eba1, (long)0x8f1bbcdc, (long)0xca62c1d6 };
//----------------------------------------------------------------------
// computes a SHA given the 64 byte MT digest buffer. The resulting 5
// long values are stored in the long array, hash.
//
// 'SHAVM_Message' - buffer containing the message digest
// 'SHAVM_Hash' - result buffer
// 'SHAVM_MAC' - result buffer, in order for Dallas part
//
void SHAVM_Compute()
{
for(SHAVM_cnt=0; SHAVM_cnt<16; SHAVM_cnt++) // 空出64B空间
{
SHAVM_MTword[SHAVM_cnt]
= (((long)SHAVM_Message[SHAVM_cnt*4]&0x00FF) << 24L)
| (((long)SHAVM_Message[SHAVM_cnt*4+1]&0x00FF) << 16L)
| (((long)SHAVM_Message[SHAVM_cnt*4+2]&0x00FF) << 8L)
| ((long)SHAVM_Message[SHAVM_cnt*4+3]&0x00FF);
}
for(; SHAVM_cnt<80; SHAVM_cnt++) //deal with 512B data
{
SHAVM_Temp
= SHAVM_MTword[SHAVM_cnt-3] ^ SHAVM_MTword[SHAVM_cnt-8]
^ SHAVM_MTword[SHAVM_cnt-14] ^ SHAVM_MTword[SHAVM_cnt-16];
SHAVM_MTword[SHAVM_cnt] //对最后一位处理实现0x01
= ((SHAVM_Temp << 1) & 0xFFFFFFFE)
| ((SHAVM_Temp >> 31) & 0x00000001);
}
SHAVM_Hash[0] = 0x67452301;
SHAVM_Hash[1] = 0xEFCDAB89;
SHAVM_Hash[2] = 0x98BADCFE;
SHAVM_Hash[3] = 0x10325476;
SHAVM_Hash[4] = 0xC3D2E1F0;
for(SHAVM_cnt=0; SHAVM_cnt<80; SHAVM_cnt++)
{
SHAVM_Temp
= ((SHAVM_Hash[0] << 5) & 0xFFFFFFE0)
| ((SHAVM_Hash[0] >> 27) & 0x0000001F);
if(SHAVM_cnt<20)
SHAVM_Temp += ((SHAVM_Hash[1]&SHAVM_Hash[2])|((~SHAVM_Hash[1])&SHAVM_Hash[3]));
else if(SHAVM_cnt<40)
SHAVM_Temp += (SHAVM_Hash[1]^SHAVM_Hash[2]^SHAVM_Hash[3]);
else if(SHAVM_cnt<60)
SHAVM_Temp += ((SHAVM_Hash[1]&SHAVM_Hash[2])
|(SHAVM_Hash[1]&SHAVM_Hash[3])
|(SHAVM_Hash[2]&SHAVM_Hash[3]));
else
SHAVM_Temp += (SHAVM_Hash[1]^SHAVM_Hash[2]^SHAVM_Hash[3]);
SHAVM_Temp += SHAVM_Hash[4] + SHAVM_KTN[SHAVM_cnt/20]
+ SHAVM_MTword[SHAVM_cnt];
SHAVM_Hash[4] = SHAVM_Hash[3]; //gain 5 hash value
SHAVM_Hash[3] = SHAVM_Hash[2];
SHAVM_Hash[2]
= ((SHAVM_Hash[1] << 30) & 0xC0000000)
| ((SHAVM_Hash[1] >> 2) & 0x3FFFFFFF);
SHAVM_Hash[1] = SHAVM_Hash[0];
SHAVM_Hash[0] = SHAVM_Temp;
}
//iButtons use LSB first, so we have to turn
//the result around a little bit. Instead of
//result A-B-C-D-E, our result is E-D-C-B-A,
//where each letter represents four bytes of
//the result.
for(SHAVM_cnt=0; SHAVM_cnt<5; SHAVM_cnt++) //gain 20 mac value
{
SHAVM_Temp = SHAVM_Hash[4-SHAVM_cnt];
SHAVM_MAC[((SHAVM_cnt)*4)+0] = (unsigned char)SHAVM_Temp;
SHAVM_Temp >>= 8;
SHAVM_MAC[((SHAVM_cnt)*4)+1] = (unsigned char)SHAVM_Temp;
SHAVM_Temp >>= 8;
SHAVM_MAC[((SHAVM_cnt)*4)+2] = (unsigned char)SHAVM_Temp;
SHAVM_Temp >>= 8;
SHAVM_MAC[((SHAVM_cnt)*4)+3] = (unsigned char)SHAVM_Temp;
}
}
这是一个SHA-1的算法.以前没接触过,原理的东西瞧了一大堆不理解,瞧程序又瞧不懂,贴上来问一问大家,解一下呀 |
关于freescale的更多信息资料,欢迎访问http://www.yuan-ying.com |
|